示例#1
0
int
swf_button_record_parse(bitstream_t *bs, swf_button_record_t *button_record,
    swf_tag_t *tag) {
    int ret;
    ret = bitstream_getbits(bs, 2); // reserved bits, always 0
    if (ret) {
        fprintf(stderr, "swf_button_record_parse: bitstream_getbis(bs, 2)\n");
        return ret;
    }
    button_record->button_has_blend_mode  = bitstream_getbit(bs);
    button_record->button_has_filter_list = bitstream_getbit(bs);
    button_record->button_state_hittest = bitstream_getbit(bs);
    button_record->button_state_down = bitstream_getbit(bs);
    button_record->button_state_over = bitstream_getbit(bs);
    button_record->button_state_up = bitstream_getbit(bs);
    //
    button_record->character_id = bitstream_getbytesLE(bs, 2);
    button_record->place_depth = bitstream_getbytesLE(bs, 2);
    ret = swf_matrix_parse(bs, &(button_record->place_matrix));
    if (ret) {
        fprintf(stderr, "swf_button_record_parse: swf_matrix_parse failed\n");
        return ret;
    }
    if (tag->code == 34) { // DefineButton2
        ret = swf_cxformwithalpha_parse(bs, &(button_record->color_transform));
        if (ret) {
            fprintf(stderr, "swf_button_record_parse: swf_matrix_parse failed\n");
            return ret;
        }
    }
    //
    // FilterList, BlendMode
    //
    return 0;
}
示例#2
0
int
swf_tag_shape_input_detail(swf_tag_t *tag, struct swf_object_ *swf) {
    swf_tag_shape_detail_t *swf_tag_shape = tag->detail;
    unsigned char *data  = tag->data;
    unsigned long length = tag->length;
    bitstream_t *bs;
    int ret;
    (void) swf;
    if (swf_tag_shape == NULL) {
        fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape == NULL\n");
        return 1;
    }
    // parse context
    swf_tag_shape->_current_x = 0;
    swf_tag_shape->_current_y = 0;
    swf_tag_shape->_parse_condition = 0;

    bs = bitstream_open();
    bitstream_input(bs, data, length);
    
    swf_tag_shape->shape_id = bitstream_getbytesLE(bs, 2);
    ret = swf_rect_parse(bs, &(swf_tag_shape->rect));
    if (ret) {
        fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->rect parse failed. shape_id=%d\n", swf_tag_shape->shape_id);
        bitstream_close(bs);
        return ret;
    }

    // DefineMorphShape, DefineMorphShape2
    swf_tag_shape->is_morph = (tag->code == 46) || (tag->code == 84);
    // DefineShape4, DefineMorphShape2
    swf_tag_shape->has_strokes = (tag->code == 83) || (tag->code == 84);

    if (swf_tag_shape->is_morph) {
        ret = swf_rect_parse(bs, &(swf_tag_shape->rect_morph));
        if (ret) {
            fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->rect_morph parse failed. shape_id=%d\n", swf_tag_shape->shape_id);
            bitstream_close(bs);
            return ret;
        }
    }
    if (swf_tag_shape->has_strokes) {
        ret = swf_rect_parse(bs, &(swf_tag_shape->stroke_rect));
        if (ret) {
            fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect parse failed. shape_id=%d\n", swf_tag_shape->shape_id);
            bitstream_close(bs);
            return ret;
        }
        if (swf_tag_shape->is_morph) {
            ret = swf_rect_parse(bs, &(swf_tag_shape->stroke_rect_morph));
            if (ret) {
                fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_tag_shape->stroke_rect_morph parse failed. shape_id=%d\n", swf_tag_shape->shape_id);
                bitstream_close(bs);
                return ret;
            }
        }
        swf_tag_shape->define_shape_reserved = bitstream_getbits(bs, 6);
        swf_tag_shape->define_shape_non_scaling_strokes = bitstream_getbits(bs, 1);
        swf_tag_shape->define_shape_scaling_strokes = bitstream_getbits(bs, 1);
    }
    if (swf_tag_shape->is_morph) {
        bitstream_align(bs);
        swf_tag_shape->offset_morph = bitstream_getbytesLE(bs, 4);
        ret = swf_morph_shape_with_style_parse(bs, &swf_tag_shape->morph_shape_with_style, tag);
        if (ret) {
            fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_shape_with_style_parse swf_tag_shape->morph_shape_with_style failed. shape_id=%d\n", swf_tag_shape->shape_id);
	        bitstream_close(bs);
            return ret;
        }
    } else {
        ret = swf_shape_with_style_parse(bs, &swf_tag_shape->shape_with_style, tag);
        if (ret) {
            fprintf(stderr, "ERROR: swf_tag_shape_input_detail: swf_shape_with_style_parse swf_tag_shape->shape_with_style failed. shape_id=%d\n", swf_tag_shape->shape_id);
	        bitstream_close(bs);
            return ret;
        }
    }
    
    bitstream_close(bs);
    return 0;
}
示例#3
0
void *
pngconv_png2lossless(unsigned char *png_data, unsigned long png_data_len,
                     int *tag_no, int *format,
                     unsigned short *width, unsigned short *height,
                     void **colormap, int *colormap_count, int rgb15) {
    volatile png_structp png_ptr = NULL;
    volatile png_infop png_info_ptr = NULL;
    my_png_buffer png_buff;
    int bpp, color_type;
    png_uint_32 png_width = 0, png_height = 0;
    volatile png_bytepp png_image_data = NULL;
    register png_uint_32 x, y;
    void *image_data = NULL;
    png_color *palette = NULL;
    int palette_num = 0;
    png_bytep trans = NULL;
    int num_trans = 0;
    png_color_16p trans_values = NULL;

    if (png_data == NULL) {
        fprintf(stderr, "pngconv_png2lossless: png_data == NULL\n");
        return NULL;
    }
    if (png_sig_cmp((png_bytep)png_data, 0, 8)) { // bad signature
        fprintf(stderr, "pngconv_png2lossless: is not PNG!\n");
        return NULL;
    }
    png_ptr = png_create_read_struct
        (PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
    if (! png_ptr) {
        fprintf(stderr, "pngconv_png2lossless: can't create read_struct\n");
        return NULL;
    }
    if (setjmp(png_jmpbuf(png_ptr))) {
        fprintf(stderr, "pngconv_png2lossless: libpng error jump occured\n");
        png_destroy_read_struct((png_structpp) &png_ptr,
                                (png_infopp) &png_info_ptr, NULL);
        if (png_image_data) {
            for (y=0 ; y < png_height ; y++) {
                free(png_image_data[y]);
            }
            free(png_image_data);
        }
        return NULL;
    }
    png_info_ptr = png_create_info_struct(png_ptr);
    if (! png_info_ptr) {
        fprintf(stderr, "pngconv_png2lossless: can't create info_struct\n");
        png_destroy_read_struct ((png_structpp)&png_ptr, NULL, NULL);
        return NULL;
    }
    png_buff.data = png_data;
    png_buff.data_len = png_data_len;
    png_buff.data_offset = 0;

    png_data_read(png_ptr, &png_buff);
    png_read_info(png_ptr, png_info_ptr);
    png_get_IHDR(png_ptr, png_info_ptr,
                 &png_width, &png_height, &bpp, &color_type,
                 NULL, NULL, NULL);
    *width = (unsigned short) png_width;
    *height = (unsigned short) png_height;

    if ((rgb15 > 0) && (color_type !=  PNG_COLOR_TYPE_RGB)) { // warning
        fprintf(stderr, "rgb15 is %d but color_type is not PNG24\n", rgb15);
    }

    switch(color_type) {
    case PNG_COLOR_TYPE_PALETTE:
        *format = 3;
        png_get_PLTE(png_ptr, png_info_ptr, &palette, &palette_num);
        if (png_get_tRNS(png_ptr, png_info_ptr, &trans, &num_trans,
                         &trans_values) && (num_trans > 0)) {
            *tag_no = 36; // DefineBitsLossless2
        } else {
            *tag_no = 20; // DefineBitsLossless
        }
        break;
    case PNG_COLOR_TYPE_RGB:
        if (rgb15 > 0) {
            *format = 4;
        } else {
            *format = 5;
        }
        *tag_no = 20; /* DefineBitsLossless */
        break;
    case PNG_COLOR_TYPE_RGB_ALPHA:
        *format = 5;
        *tag_no = 36; /* DefineBitsLossless2 */
        if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_tRNS))
            png_set_tRNS_to_alpha(png_ptr);
        break;
    default:
        fprintf(stderr, "pngconv_png2lossless: color_type=%d not implemented yet.\n", color_type);
        png_destroy_read_struct((png_structpp)&png_ptr,
                                (png_infopp)&png_info_ptr, NULL);
        return NULL;
    }
    if (bpp > 8) {
        fprintf(stderr, "pngconv_png2lossless: bpp=%d not implemented yet. accept only bpp <= 8\n", bpp);
        png_destroy_read_struct((png_structpp)&png_ptr,
                                (png_infopp)&png_info_ptr, NULL);
        return NULL;
    }
    
    png_image_data = (png_bytepp) malloc(png_height * sizeof(png_bytep));
    for (y=0 ; y < png_height ; y++) {
        png_image_data[y] = (png_bytep) malloc(png_get_rowbytes(png_ptr, png_info_ptr));
    }
    png_read_image(png_ptr, png_image_data);
    /*
     * image copy
     */
    if (color_type == PNG_COLOR_TYPE_PALETTE) {
        register int i;
        unsigned char *indices_data;
        *colormap_count = palette_num;
        if (num_trans == 0) {
            swf_rgb_t *result_colormap = malloc(sizeof(swf_rgb_t) * palette_num);   // Lossless
            for (i=0 ; i < palette_num ; i++) {
                result_colormap[i].red   = palette[i].red;
                result_colormap[i].green = palette[i].green;
                result_colormap[i].blue  = palette[i].blue;
            }
            *colormap = result_colormap;
        } else {
              swf_rgba_t *result_colormap = malloc(sizeof(swf_rgba_t) * palette_num);   // Lossless2
            for (i=0 ; i < palette_num ; i++) {
                if (i < num_trans) {
                    int alpha_value = trans[i];
                    result_colormap[i].red   = palette[i].red   * alpha_value / 0xff;
                    result_colormap[i].green = palette[i].green * alpha_value / 0xff;
                    result_colormap[i].blue  = palette[i].blue  * alpha_value / 0xff;
                    result_colormap[i].alpha = alpha_value;
                } else {
                    result_colormap[i].red   = palette[i].red;
                    result_colormap[i].green = palette[i].green;
                    result_colormap[i].blue  = palette[i].blue;
                    result_colormap[i].alpha = 0xff; // opaque
                }
            }
            *colormap = result_colormap;
        }
        indices_data = malloc(((png_width+ 3) & -4) * png_height);
        i = 0;
        for (y=0 ; y < png_height ; y++) {
            bitstream_t *bs = bitstream_open();
            if (bs == NULL) {
                free(*colormap);
                *colormap = NULL;
                free(indices_data);
                for (y=0 ; y < png_height ; y++) {
                    free(png_image_data[y]);
                }
                free(png_image_data);
                return NULL;
            }
            bitstream_input(bs, png_image_data[y], png_width);
            for (x=0 ; x < png_width ; x++) {
                indices_data[i] = bitstream_getbits(bs, bpp);
                i++;
            }
            while (i % 4) { i++; } // 4byte alignment
            bitstream_close(bs);
        }
        image_data = indices_data;
    } else if (color_type == PNG_COLOR_TYPE_RGB) {
        swf_xrgb_t *xrgb_list;
        xrgb_list = malloc(sizeof(swf_xrgb_t) * png_width * png_height);
        for (y=0 ; y < png_height ; y++) {
            for (x=0 ; x < png_width ; x++) {
                xrgb_list[x+y*png_width].red   = png_image_data[y][3*x + 0];
                xrgb_list[x+y*png_width].green = png_image_data[y][3*x + 1];
                xrgb_list[x+y*png_width].blue  = png_image_data[y][3*x + 2];
            }
        }
        image_data = xrgb_list;
    } else { // PNG_COLOR_TYPE_RGB_ALPHA
        swf_argb_t *argb_list;
        argb_list = malloc(sizeof(swf_argb_t) * png_width * png_height);
        for (y=0 ; y < png_height ; y++) {
            for (x=0 ; x < png_width ; x++) {
                int alpha_value = png_image_data[y][4*x + 3];
                argb_list[x+y*png_width].red   = png_image_data[y][4*x + 0] * alpha_value / 0xff;
                argb_list[x+y*png_width].green = png_image_data[y][4*x + 1] * alpha_value / 0xff;
                argb_list[x+y*png_width].blue  = png_image_data[y][4*x + 2] * alpha_value / 0xff;
                argb_list[x+y*png_width].alpha = alpha_value;
            }
        }
        image_data = argb_list;
    }
    for (y=0 ; y < png_height ; y++) {
        free(png_image_data[y]);
    }
    free(png_image_data);
    /*
     * destruct
     */
    png_destroy_read_struct((png_structpp) &png_ptr,
                            (png_infopp) &png_info_ptr, NULL);
    return image_data;
}
示例#4
0
int main(int argc, char **argv) {
    char *png_filename = NULL;
    struct stat filestat;
    FILE *fp = NULL;
    my_png_buffer png_buff;
    int is_png;
    png_uint_32 png_width, png_height;
    int bpp, color_type;
    png_bytepp image_data;
    png_uint_32 x, y;
    png_color *palette = NULL;
    int palette_num = 0;
    png_bytep trans = NULL;
    int num_trans;
    png_color_16p trans_values = NULL;
    int palette_dump_unit;
    if (argc != 2) {
        char *program_filename = argv[0];
        fprintf(stderr, "Usage: %s <png_filename>\n", program_filename);
        return EXIT_FAILURE;
    }
    png_filename = argv[1];
//    printf("png_filename=%s\n", png_filename);

    if(stat(png_filename, &filestat)) {
        perror("stat pngfile");
        return EXIT_FAILURE;
    }
    png_buff.data_len = filestat.st_size;
    png_buff.data_offset = 0;
//    printf("png_buff.data_len=%lu\n", png_buff.data_len);

    fp = fopen(png_filename, "rb");
    if (! fp) {
        fprintf(stderr, "Can't open file(%s)\n", png_filename);
        return EXIT_FAILURE;
    }
    png_buff.data = calloc(png_buff.data_len, 1);
    fread(png_buff.data, 1, png_buff.data_len, fp);
    fclose(fp);
    is_png = png_check_sig((png_bytep)png_buff.data, 8);
    if (! is_png) {
        fprintf(stderr, "is not PNG!\n");
        free(png_buff.data);
        return EXIT_FAILURE;
    }
    png_structp png_ptr = png_create_read_struct
        (PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
    if (! png_ptr) {
        fprintf(stderr, "can't create read_struct\n");
        free(png_buff.data);
        return EXIT_FAILURE;
    }
    png_infop png_info_ptr = png_create_info_struct(png_ptr);
    if (! png_info_ptr) {
        fprintf(stderr, "can't create info_struct\n");
        png_destroy_read_struct (&png_ptr, NULL, NULL);
        free(png_buff.data);
        return EXIT_FAILURE;
    }
    png_data_read(png_ptr, &png_buff);
    png_read_info(png_ptr, png_info_ptr);
    png_get_IHDR(png_ptr, png_info_ptr,
                 &png_width, &png_height, &bpp, &color_type,
                 NULL, NULL, NULL);
    printf("(width, height)=(%lu,%lu) bpp=%d",
           png_width, png_height, bpp);
    printf(" color_type=%d", color_type);
    switch(color_type) {
      case PNG_COLOR_TYPE_GRAY:
	  printf("(GRAY)");
	  if (! png_get_tRNS(png_ptr, png_info_ptr, &trans, &num_trans,
			     &trans_values)) {
	      num_trans = 0;
	  }
          printf(" num_trans=%d", num_trans);
	  break;
      case PNG_COLOR_TYPE_GRAY_ALPHA:
	  printf("(GRAY_ALPHA)");
	  break;
      case PNG_COLOR_TYPE_RGB:
	  printf("(RGB)");
	if (png_get_PLTE(png_ptr, png_info_ptr, &palette, &palette_num)) {
	    printf(" palette_num=%d", palette_num);
	}
	  if (! png_get_tRNS(png_ptr, png_info_ptr, &trans, &num_trans,
			     &trans_values)) {
	      num_trans = 0;
	  }
          printf(" num_trans=%d", num_trans);
	  break;
      case PNG_COLOR_TYPE_RGB_ALPHA:
        printf("(RGB_ALPHA)");
	if (png_get_PLTE(png_ptr, png_info_ptr, &palette, &palette_num)) {
	    printf(" palette_num=%d", palette_num);
	}

	//        if (png_get_valid(png_ptr, png_info_ptr, PNG_INFO_tRNS))
	// png_set_tRNS_to_alpha(png_ptr);
        break;
      case PNG_COLOR_TYPE_PALETTE:
          printf("(PALETTE)");
          png_get_PLTE(png_ptr, png_info_ptr, &palette, &palette_num);
          printf(" palette_num=%d", palette_num);
          if (! png_get_tRNS(png_ptr, png_info_ptr, &trans, &num_trans,
                           &trans_values)) {
              num_trans = 0;
          }
          printf(" num_trans=%d", num_trans);
          break;
      default:
           printf("color_type(%d) not implement yet.\n",
                  color_type);
    }
    printf("\n");
    /*
      image
     */
    image_data = (png_bytepp) malloc(png_height * sizeof(png_bytep));
    for (y=0; y < png_height; y++) {
        image_data[y] = (png_bytep) malloc(png_get_rowbytes(png_ptr, png_info_ptr));
    }
    png_read_image(png_ptr, image_data);
    if (num_trans == 0) {
        palette_dump_unit = 8;
    } else {
        palette_dump_unit = 4;
    }
    if (color_type == PNG_COLOR_TYPE_PALETTE) {
        int i, j;
        for (i = 0 ; i < palette_num ; i+=palette_dump_unit ) {
            printf("[%03d]", i);
            for (j = 0 ; (j < palette_dump_unit) && ((i+j) < palette_num) ; j++) {
                if ((i+j) < num_trans) {
                    printf(" #%02x%02x%02x(%02x)",
                           palette[i+j].red,
                           palette[i+j].green,
                           palette[i+j].blue,
                           trans[i+j] & 0xff);
                } else {
                    printf(" #%02x%02x%02x",
                           palette[i+j].red,
                           palette[i+j].green,
                           palette[i+j].blue);
                }
            }
            printf("\n");
        }
        for (y=0; y < png_height; y++) {
            unsigned char *linedata = image_data[y];
            bitstream_t *bs = bitstream_open();
            bitstream_input(bs, linedata, png_get_rowbytes(png_ptr, png_info_ptr));
            printf("y=%lu: ", y);
            for (x=0; x < png_width; x++) {
                int colorindex = bitstream_getbits(bs, bpp);
                printf("%02x  ", colorindex);
            }
            bitstream_close(bs);
            printf("\n");
        }
    } else {
        for (y=0; y < png_height; y++) {
            printf("y=%lu: ", y);
            for (x=0; x < png_width; x++) {
                switch(color_type) {
                case PNG_COLOR_TYPE_GRAY:
                    printf("%02x ",
                           image_data[y][x]);
                    break;
                case PNG_COLOR_TYPE_RGB:
                    printf("%02x%02x%02x  ",
                           image_data[y][3*x],
                           image_data[y][3*x+1],
                           image_data[y][3*x+2]);
                    break;
                case PNG_COLOR_TYPE_RGB_ALPHA:
                    printf("%02x%02x%02x(%02x)  ",
                           image_data[y][4*x],
                           image_data[y][4*x+1],
                           image_data[y][4*x+2],
                           image_data[y][4*x+3]);

                    break;
                default:
                    break;
                }
            }
            printf("\n");
        }
    }
    /*
     * finish
     */
    for (y=0; y < png_height; y++) {
        free(image_data[y]);
    }
    free(image_data);
    png_destroy_read_struct(&png_ptr, &png_info_ptr, NULL);
    free(png_buff.data);
    return EXIT_SUCCESS;
}