Skip to content

Commit

Permalink
BUG: 12235 Accomodate SystemPNG newer than ITK version.
Browse files Browse the repository at this point in the history
Newer versions make the png and info structures private,
requiring the use of access functions, and there is a
API change as reported here

http://public.kitware.com/Bug/view.php?id=12235

BUG: The call to png_set_sCAL was failing with 1.5.2

I do a version check to suppress this call for versions 1.4 &
1.5.  Based on my understanding of the call, and the comments
about it in the source code, it is pretty useless anyway.
I tried to set the units to PNG_SCALE_METER and divide the
spacing by 1000 (since ITK spacing is in MM) and the png library
no longer raised an exception, but it complained about invalid
units.

Change-Id: I388e2a92cf51c7fb42887055a5e59ab152e8f523
  • Loading branch information
Kent Williams committed Jun 10, 2011
1 parent 7fb3f39 commit 74b5e6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Modules/IO/PNG/src/itkPNGImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C"
void itkPNGWriteErrorFunction( png_structp png_ptr,
png_const_charp itkNotUsed(error_msg) )
{
longjmp(png_ptr->jmpbuf, 1);
longjmp(png_jmpbuf(png_ptr), 1);
}
}

Expand Down Expand Up @@ -207,7 +207,11 @@ void PNGImageIO::Read(void *buffer)
// minimum of a byte per pixel
if ( colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8 )
{
#if (PNG_LIBPNG_VER_MAJOR < 2 && PNG_LIBPNG_VER_MINOR < 4)
png_set_gray_1_2_4_to_8(png_ptr);
#else
png_set_expand_gray_1_2_4_to_8(png_ptr);
#endif
}

// add alpha if any alpha found
Expand All @@ -223,10 +227,19 @@ void PNGImageIO::Read(void *buffer)
#endif
}

#if (PNG_LIBPNG_VER_MAJOR < 2 && PNG_LIBPNG_VER_MINOR < 5)
if ( info_ptr->valid & PNG_INFO_sBIT )
{
png_set_shift( png_ptr, &( info_ptr->sig_bit ) );
}
#else
if ( png_get_valid(png_ptr, info_ptr,PNG_INFO_sBIT ) )
{
png_color_8p bits;
png_get_sBIT(png_ptr,info_ptr,&bits);
png_set_shift( png_ptr, bits);
}
#endif
// have libpng handle interlacing
//int number_of_passes = png_set_interlace_handling(png_ptr);
// update the info now that we have defined the filters
Expand Down Expand Up @@ -338,7 +351,11 @@ void PNGImageIO::ReadImageInformation()
// minimum of a byte per pixel
if ( colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8 )
{
#if (PNG_LIBPNG_VER_MAJOR < 2 && PNG_LIBPNG_VER_MINOR < 4)
png_set_gray_1_2_4_to_8(png_ptr);
#else
png_set_expand_gray_1_2_4_to_8(png_ptr);
#endif
}

// add alpha if any alpha found
Expand Down Expand Up @@ -490,7 +507,7 @@ void PNGImageIO::WriteSlice(const std::string & fileName, const void *buffer)
#if !defined( _MSC_VER ) || _MSC_VER != 1310
png_set_error_fn(png_ptr, png_ptr,
itkPNGWriteErrorFunction, itkPNGWriteWarningFunction);
if ( setjmp(png_ptr->jmpbuf) )
if ( setjmp(png_jmpbuf(png_ptr)) )
{
fclose(fp);
itkExceptionMacro( "Error while writing Slice to file: "
Expand Down Expand Up @@ -553,8 +570,10 @@ void PNGImageIO::WriteSlice(const std::string & fileName, const void *buffer)
// set the unit_type to unknown. if we add units to ITK, we should
// convert pixel size to meters and store units as meters (png
// has three set of units: meters, radians, and unknown).
#if (PNG_LIBPNG_VER_MAJOR < 2 && PNG_LIBPNG_VER_MINOR < 4)
png_set_sCAL(png_ptr, info_ptr, PNG_SCALE_UNKNOWN, colSpacing,
rowSpacing);
#endif

//std::cout << "PNG_INFO_sBIT: " << PNG_INFO_sBIT << std::endl;

Expand Down
1 change: 1 addition & 0 deletions Modules/ThirdParty/PNG/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if(ITK_USE_SYSTEM_PNG)
)
set(ITK-PNG_SYSTEM_INCLUDE_DIRS
${PNG_INCLUDE_DIRS}
${PNG_INCLUDE_DIR}
)
set(ITK-PNG_LIBRARIES "${PNG_LIBRARIES}")
set(ITK-PNG_NO_SRC 1)
Expand Down

0 comments on commit 74b5e6e

Please sign in to comment.