21 #include <itksys/SystemTools.hxx>
32 png_const_charp itkNotUsed(error_msg))
34 longjmp(png_ptr->jmpbuf, 1);
42 png_const_charp itkNotUsed(warning_msg))
70 std::string filename = file;
73 itkDebugMacro(<<
"No filename specified.");
89 unsigned char header[8];
91 bool is_png = !png_sig_cmp(header, 0, 8);
96 png_structp png_ptr = png_create_read_struct
97 (PNG_LIBPNG_VER_STRING, (png_voidp)
NULL,
104 png_infop info_ptr = png_create_info_struct(png_ptr);
107 png_destroy_read_struct(&png_ptr,
108 (png_infopp)NULL, (png_infopp)NULL);
112 png_infop end_info = png_create_info_struct(png_ptr);
115 png_destroy_read_struct(&png_ptr, &info_ptr,
119 png_destroy_read_struct(&png_ptr, &info_ptr,
140 itkExceptionMacro(
"PNGImageIO could not open file: "
144 << itksys::SystemTools::GetLastSystemError());
147 unsigned char header[8];
148 fread(header, 1, 8, fp);
149 bool is_png = !png_sig_cmp(header, 0, 8);
152 itkExceptionMacro(
"File is not png type: " << this->
GetFileName());
155 png_structp png_ptr = png_create_read_struct
156 (PNG_LIBPNG_VER_STRING, (png_voidp)
NULL,
160 itkExceptionMacro(
"File is not png type" << this->
GetFileName());
164 png_infop info_ptr = png_create_info_struct(png_ptr);
167 png_destroy_read_struct(&png_ptr,
168 (png_infopp)NULL, (png_infopp)NULL);
169 itkExceptionMacro(
"File is not png type " << this->
GetFileName());
173 png_infop end_info = png_create_info_struct(png_ptr);
176 png_destroy_read_struct(&png_ptr, &info_ptr,
178 itkExceptionMacro(
"File is not png type " << this->
GetFileName());
183 #if !defined(MSC_VER) || _MSC_VER != 1310
184 if( setjmp( png_jmpbuf( png_ptr ) ) )
186 png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
187 itkExceptionMacro(
"File is not png type " << this->
GetFileName());
192 png_init_io(png_ptr, fp);
193 png_set_sig_bytes(png_ptr, 8);
195 png_read_info(png_ptr, info_ptr);
197 png_uint_32 width, height;
198 int bitDepth, colorType, interlaceType;
199 int compression_type, filter_method;
200 png_get_IHDR(png_ptr, info_ptr,
202 &bitDepth, &colorType, &interlaceType,
203 &compression_type, &filter_method);
206 if (colorType == PNG_COLOR_TYPE_PALETTE)
208 png_set_palette_to_rgb(png_ptr);
212 if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
214 png_set_gray_1_2_4_to_8(png_ptr);
218 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
220 png_set_tRNS_to_alpha(png_ptr);
225 #ifndef ITK_WORDS_BIGENDIAN
226 png_set_swap(png_ptr);
230 if (info_ptr->valid & PNG_INFO_sBIT)
232 png_set_shift(png_ptr, &(info_ptr->sig_bit));
237 png_read_update_info(png_ptr, info_ptr);
239 unsigned long rowbytes = png_get_rowbytes(png_ptr, info_ptr);
240 unsigned char *tempImage =
static_cast<unsigned char*
>(buffer);
241 png_bytep *row_pointers =
new png_bytep [height];
242 for (
unsigned int ui = 0; ui < height; ++ui)
244 row_pointers[ui] = tempImage + rowbytes*ui;
246 png_read_image(png_ptr, row_pointers);
247 delete [] row_pointers;
249 png_read_end(png_ptr, NULL);
250 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
296 unsigned char header[8];
297 fread(header, 1, 8, fp);
298 bool is_png = !png_sig_cmp(header, 0, 8);
303 png_structp png_ptr = png_create_read_struct
304 (PNG_LIBPNG_VER_STRING, (png_voidp)
NULL,
311 png_infop info_ptr = png_create_info_struct(png_ptr);
314 png_destroy_read_struct(&png_ptr,
315 (png_infopp)NULL, (png_infopp)NULL);
319 png_infop end_info = png_create_info_struct(png_ptr);
322 png_destroy_read_struct(&png_ptr, &info_ptr,
327 png_init_io(png_ptr, fp);
328 png_set_sig_bytes(png_ptr, 8);
330 png_read_info(png_ptr, info_ptr);
332 png_uint_32 width, height;
333 int bitDepth, colorType, interlaceType;
334 int compression_type, filter_method;
335 png_get_IHDR(png_ptr, info_ptr,
337 &bitDepth, &colorType, &interlaceType,
338 &compression_type, &filter_method);
341 if (colorType == PNG_COLOR_TYPE_PALETTE)
343 png_set_palette_to_rgb(png_ptr);
347 if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
349 png_set_gray_1_2_4_to_8(png_ptr);
353 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
355 png_set_tRNS_to_alpha(png_ptr);
359 png_read_update_info(png_ptr, info_ptr);
386 double px_width = 1.0, px_height = 1.0;
387 int units = PNG_SCALE_UNKNOWN;
388 png_get_sCAL(png_ptr, info_ptr, &units, &px_width, &px_height);
395 png_destroy_read_struct(&png_ptr, &info_ptr,
403 std::string filename = name;
410 std::string::size_type pngPos = filename.rfind(
".png");
411 if ( (pngPos != std::string::npos)
412 && (pngPos == filename.length() - 4) )
417 pngPos = filename.rfind(
".PNG");
418 if ( (pngPos != std::string::npos)
419 && (pngPos == filename.length() - 4) )
440 volatile const unsigned char *outPtr = ( (
const unsigned char *) buffer);
456 volatile int bitDepth;
473 ::itk::ExceptionObject excp(__FILE__, __LINE__,
"PNG supports unsigned char and unsigned short", ITK_LOCATION);
478 png_structp png_ptr = png_create_write_struct
479 (PNG_LIBPNG_VER_STRING, (png_voidp)
NULL, NULL, NULL);
482 itkExceptionMacro(<<
"Unable to write PNG file! png_create_write_struct failed.");
485 png_infop info_ptr = png_create_info_struct(png_ptr);
488 png_destroy_write_struct(&png_ptr,
490 itkExceptionMacro(<<
"Unable to write PNG file!. png_create_info_struct failed.");
493 png_init_io(png_ptr, fp);
496 #if !defined(_MSC_VER) || _MSC_VER != 1310
497 png_set_error_fn(png_ptr, png_ptr,
499 if (setjmp(png_ptr->jmpbuf))
502 itkExceptionMacro(
"Error while writing Slice to file: "
506 << itksys::SystemTools::GetLastSystemError());
515 case 1: colorType = PNG_COLOR_TYPE_GRAY;
517 case 2: colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
519 case 3: colorType = PNG_COLOR_TYPE_RGB;
521 default: colorType = PNG_COLOR_TYPE_RGB_ALPHA;
525 png_uint_32 width, height;
526 double rowSpacing, colSpacing;
541 png_set_IHDR(png_ptr, info_ptr, width, height,
542 bitDepth, colorType, PNG_INTERLACE_NONE,
543 PNG_COMPRESSION_TYPE_DEFAULT,
544 PNG_FILTER_TYPE_DEFAULT);
558 png_set_sCAL(png_ptr, info_ptr, PNG_SCALE_UNKNOWN, colSpacing,
563 png_write_info(png_ptr, info_ptr);
567 #ifndef ITK_WORDS_BIGENDIAN
568 png_set_swap(png_ptr);
571 png_byte **row_pointers =
new png_byte *[height];
572 int rowInc = width*numComp*bitDepth/8;
573 for (
unsigned int ui = 0; ui < height; ui++)
575 row_pointers[ui] =
const_cast<png_byte *
>(outPtr);
576 outPtr =
const_cast<unsigned char *
>(outPtr) + rowInc;
578 png_write_image(png_ptr, row_pointers);
579 png_write_end(png_ptr, info_ptr);
581 delete [] row_pointers;
582 png_destroy_write_struct(&png_ptr, &info_ptr);