svnno****@sourc*****
svnno****@sourc*****
2008年 10月 7日 (火) 01:38:22 JST
Revision: 58 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=swfed&view=rev&rev=58 Author: yoya Date: 2008-10-07 01:38:22 +0900 (Tue, 07 Oct 2008) Log Message: ----------- libpng のエラーハンドリング処理(setjmp 方式)を追加 今までは libpng 内でエラーが起こると、default の abort 処理に落ちてた Modified Paths: -------------- src/swf_png.c -------------- next part -------------- Modified: src/swf_png.c =================================================================== --- src/swf_png.c 2008-10-06 08:29:57 UTC (rev 57) +++ src/swf_png.c 2008-10-06 16:38:22 UTC (rev 58) @@ -80,13 +80,13 @@ int *tag_no, int *format, unsigned short *width, unsigned short *height, void **colormap, int *colormap_count) { - png_structp png_ptr; - png_infop png_info; + volatile png_structp png_ptr = NULL; + volatile png_infop png_info = NULL; my_png_buffer png_buff; int is_png; int bpp, color_type; - png_uint_32 png_width, png_height; - png_bytepp png_image_data; + png_uint_32 png_width = 0, png_height = 0; + volatile png_bytepp png_image_data = NULL; png_uint_32 x, y; void *image_data; png_color *palette = NULL; @@ -104,10 +104,22 @@ 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, 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 = png_create_info_struct(png_ptr); if (! png_info) { fprintf(stderr, "pngconv_png2lossless: can't create info_struct\n"); - png_destroy_read_struct (&png_ptr, NULL, NULL); + png_destroy_read_struct ((png_structpp)&png_ptr, NULL, NULL); return NULL; } png_buff.data = png_data; @@ -145,12 +157,14 @@ break; default: fprintf(stderr, "pngconv_png2lossless: color_type=%d not implemented yet.\n", color_type); - png_destroy_read_struct(&png_ptr, &png_info, NULL); + png_destroy_read_struct((png_structpp)&png_ptr, + (png_infopp)&png_info, 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_ptr, &png_info, NULL); + png_destroy_read_struct((png_structpp)&png_ptr, + (png_infopp)&png_info, NULL); return NULL; } @@ -211,24 +225,26 @@ /* * destruct */ - png_destroy_read_struct (&png_ptr, &png_info, NULL); + png_destroy_read_struct((png_structpp) &png_ptr, + (png_infopp) &png_info, NULL); return image_data; } unsigned char * pngconv_lossless2png(void *image_data, - unsigned short width, unsigned short height, + unsigned short width, unsigned short height, void *index_data, unsigned short index_data_count, int tag_no, int format, - unsigned long *length) { - png_structp png_ptr; - png_infop info_ptr; - my_png_buffer png_buff; - png_uint_32 png_width, png_height; + unsigned long *length) { + volatile png_structp png_ptr = NULL; + volatile png_infop info_ptr = NULL; + volatile my_png_buffer png_buff; + png_uint_32 png_width = 0, png_height = 0; int bpp, color_type; - png_bytepp png_image_data = NULL; + volatile png_bytepp png_image_data = NULL; png_uint_32 x, y; + volatile png_colorp png_palette = NULL; if ((format != 3) && (format != 5)) { fprintf(stderr, "jpegconv_lossless2png: format=%d not implemented yes.\n", format); return NULL; @@ -238,10 +254,24 @@ fprintf(stderr, "jpegconv_lossless2png: can't create write_struct\n"); return NULL; } + if (setjmp(png_jmpbuf(png_ptr))) { + fprintf(stderr, "pngconv_lossless2png: libpng error jump occured\n"); + free(png_palette); + if (png_image_data) { + for ( y = 0; y < png_height ; y++) { + free(png_image_data[y]); + } + free(png_image_data); + } + free(png_buff.data); + png_destroy_write_struct((png_structpp) &png_ptr, + (png_infopp) &info_ptr); + return NULL; + } info_ptr = png_create_info_struct(png_ptr); if (! info_ptr) { fprintf(stderr, "jpegconv_lossless2png: can't create info_struct\n"); - png_destroy_write_struct (&png_ptr, NULL); + png_destroy_write_struct((png_structpp) &png_ptr, NULL); return NULL; } // @@ -257,7 +287,8 @@ } else { fprintf(stderr, "jpegconv_lossless2png: format!=3 and tag_no=%d not implemented.\n", tag_no); - png_destroy_write_struct (&png_ptr, &info_ptr); + png_destroy_write_struct((png_structpp) &png_ptr, + (png_infopp) &info_ptr); return NULL; } png_set_filter(png_ptr, 0, PNG_ALL_FILTERS); @@ -267,10 +298,10 @@ PNG_FILTER_TYPE_DEFAULT); if (format == 3) { int i; - png_colorp png_palette; if (index_data_count == 0) { fprintf(stderr, "jpegconv_lossless2png: index_data_count == 0 at line(%d)\n", __LINE__); - png_destroy_write_struct (&png_ptr, &info_ptr); + png_destroy_write_struct((png_structpp) &png_ptr, + (png_infopp) &info_ptr); return NULL; } png_palette = (png_colorp) malloc(sizeof(png_color)*index_data_count); @@ -332,7 +363,7 @@ png_buff.data = NULL; png_buff.data_len = 0; png_buff.data_offset = 0; - png_data_write(png_ptr, &png_buff); + png_data_write((png_structp) png_ptr, (my_png_buffer*) &png_buff); png_write_info(png_ptr, info_ptr); png_write_image(png_ptr, png_image_data); @@ -342,7 +373,8 @@ free(png_image_data[y]); } free(png_image_data); - png_destroy_write_struct(&png_ptr, &info_ptr); + png_destroy_write_struct((png_structpp) &png_ptr, + (png_infopp) &info_ptr); *length = png_buff.data_offset; return png_buff.data; }