X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fmodules%2Fmod_png%2Fmptr_png.cpp;h=19f82f35880b1edb0b9203134103fbfd1bb82d47;hb=9459638ad6797b8139f1e9f0715c96076dbf0890;hp=94c036c89b0907cc8dda177b03842c9c4009620e;hpb=0b7b0f0bcd16daf2d31a1bdec0e76c132bda801f;p=synfig.git diff --git a/synfig-core/trunk/src/modules/mod_png/mptr_png.cpp b/synfig-core/trunk/src/modules/mod_png/mptr_png.cpp index 94c036c..19f82f3 100644 --- a/synfig-core/trunk/src/modules/mod_png/mptr_png.cpp +++ b/synfig-core/trunk/src/modules/mod_png/mptr_png.cpp @@ -6,6 +6,7 @@ ** ** \legal ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** Copyright (c) 2007 Chris Moore ** ** This package is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License as @@ -162,6 +163,21 @@ png_mptr::png_mptr(const char *file_name) png_init_io(png_ptr, file); png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES); + png_read_info(png_ptr, info_ptr); + + int bit_depth,color_type,interlace_type, compression_type,filter_method; + png_uint_32 width,height; + + png_get_IHDR(png_ptr, info_ptr, &width, &height, + &bit_depth, &color_type, &interlace_type, + &compression_type, &filter_method); + + if (bit_depth == 16) + png_set_strip_16(png_ptr); + + if (bit_depth < 8) + png_set_packing(png_ptr); + double fgamma; if (png_get_gAMA(png_ptr, info_ptr, &fgamma)) { @@ -184,28 +200,31 @@ png_mptr::png_mptr(const char *file_name) png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback); + // man libpng tells me: + // You must use png_transforms and not call any + // png_set_transform() functions when you use png_read_png(). + // but we used png_set_gamma(), which may be why we were seeing a crash at the end + // png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL); - png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL); + png_read_update_info(png_ptr, info_ptr); + png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr); - int bit_depth,color_type,interlace_type, compression_type,filter_method; - png_uint_32 width,height; + // allocate buffer to read image data into + png_bytep *row_pointers=new png_bytep[height]; + png_byte *data = new png_byte[rowbytes*height]; + for (png_uint_32 i = 0; i < height; i++) + row_pointers[i] = &(data[rowbytes*i]); - png_get_IHDR(png_ptr, info_ptr, &width, &height, - &bit_depth, &color_type, &interlace_type, - &compression_type, &filter_method); + png_read_image(png_ptr, row_pointers); - png_bytep *row_pointers=new png_bytep[height]; - row_pointers = png_get_rows(png_ptr, info_ptr); - int x; - int y; + png_uint_32 x, y; surface_buffer.set_wh(width,height); switch(color_type) { case PNG_COLOR_TYPE_RGB: - DEBUGPOINT(); - for(y=0;ypalette[row_pointers[y][x]].red); float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green); float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue); + float a=1.0; + if(info_ptr->valid & PNG_INFO_tRNS) + a = (float)(unsigned char)png_ptr->trans[row_pointers[y][x]]*(1.0/255.0); surface_buffer[y][x]=Color( r, g, b, - 1.0 + a ); } break; @@ -305,30 +325,107 @@ png_mptr::png_mptr(const char *file_name) return; } - DEBUGPOINT(); - - // \fixme These shouldn't be uncommented, but for some - // reason, they crash the program. I will have to look into this - // later. This is a memory leak, but it shouldn't be too bad. - - /* png_read_end(png_ptr, end_info); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); - */ fclose(file); delete [] row_pointers; + delete [] data; + + trim = false; + + if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES")) + return; + + switch(color_type) + { + case PNG_COLOR_TYPE_RGB_ALPHA: + case PNG_COLOR_TYPE_GRAY_ALPHA: + case PNG_COLOR_TYPE_PALETTE: + for(y=0;yBORDER) min_y = y-BORDER; else min_y = 0; + + for(y=height-1;y>0;y--) + { + for(x=0;xBORDER) min_x = x-BORDER; else min_x = 0; + + for(x=width-1;x>0;x--) + { + for(y=0;y