add boost on mac

This commit is contained in:
Bassem Girgis
2019-08-10 16:38:17 -05:00
parent 861b918727
commit be945cb63b
14105 changed files with 2714968 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
//
// Copyright 2012 Christian Henning
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_BASE_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_BASE_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#include <boost/assert.hpp>
#include <memory>
namespace boost { namespace gil { namespace detail {
struct png_ptr_wrapper
{
png_ptr_wrapper()
: _struct( nullptr )
, _info ( nullptr )
{}
png_structp _struct;
png_infop _info;
};
///
/// Wrapper for libpng's png_struct and png_info object. Implements value semantics.
///
struct png_struct_info_wrapper
{
protected:
using png_ptr_t = std::shared_ptr<png_ptr_wrapper>;
protected:
///
/// Default Constructor
///
png_struct_info_wrapper( bool read = true )
: _png_ptr( new png_ptr_wrapper()
, ( ( read ) ? png_ptr_read_deleter : png_ptr_write_deleter )
)
{}
png_ptr_wrapper* get() { return _png_ptr.get(); }
png_ptr_wrapper const* get() const { return _png_ptr.get(); }
png_struct* get_struct() { return get()->_struct; }
png_struct const* get_struct() const { return get()->_struct; }
png_info* get_info() { return get()->_info; }
png_info const* get_info() const { return get()->_info; }
private:
static void png_ptr_read_deleter( png_ptr_wrapper* png_ptr )
{
if( png_ptr )
{
BOOST_ASSERT(png_ptr->_struct && png_ptr->_info);
png_destroy_read_struct( &png_ptr->_struct
, &png_ptr->_info
, nullptr
);
delete png_ptr;
png_ptr = nullptr;
}
}
static void png_ptr_write_deleter( png_ptr_wrapper* png_ptr )
{
if( png_ptr )
{
BOOST_ASSERT(png_ptr->_struct && png_ptr->_info);
png_destroy_write_struct( &png_ptr->_struct
, &png_ptr->_info
);
delete png_ptr;
png_ptr = nullptr;
}
}
private:
png_ptr_t _png_ptr;
};
} // namespace detail
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,46 @@
//
// Copyright 2008 Christian Henning, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_IS_ALLOWED_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_IS_ALLOWED_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#include <boost/mpl/bool_fwd.hpp>
#include <boost/mpl/for_each.hpp>
namespace boost { namespace gil { namespace detail {
template< typename View >
bool is_allowed( const image_read_info< png_tag >& info
, mpl::true_ // is read_and_no_convert
)
{
using pixel_t = typename get_pixel_type<View>::type;
using channel_t = typename channel_traits<typename element_type<pixel_t>::type>::value_type;
const png_num_channels::type dst_num_channels = num_channels< pixel_t >::value;
const png_bitdepth::type dst_bit_depth = detail::unsigned_integral_num_bits< channel_t >::value;
return dst_num_channels == info._num_channels
&& dst_bit_depth == info._bit_depth;
}
template< typename View >
bool is_allowed( const image_read_info< png_tag >& /* info */
, mpl::false_ // is read_and_convert
)
{
return true;
}
} // namespace detail
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,434 @@
//
// Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_READ_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_READ_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#include <boost/gil/extension/io/png/detail/reader_backend.hpp>
#include <boost/gil/extension/io/png/detail/is_allowed.hpp>
#include <boost/gil.hpp> // FIXME: Include what you use!
#include <boost/gil/io/base.hpp>
#include <boost/gil/io/conversion_policies.hpp>
#include <boost/gil/io/device.hpp>
#include <boost/gil/io/dynamic_io_new.hpp>
#include <boost/gil/io/reader_base.hpp>
#include <boost/gil/io/row_buffer_helper.hpp>
#include <boost/gil/io/typedefs.hpp>
namespace boost { namespace gil {
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#endif
///
/// PNG Reader
///
template< typename Device
, typename ConversionPolicy
>
class reader< Device
, png_tag
, ConversionPolicy
>
: public reader_base< png_tag
, ConversionPolicy >
, public reader_backend< Device
, png_tag
>
{
private:
using this_t = reader<Device, png_tag, ConversionPolicy>;
using cc_t = typename ConversionPolicy::color_converter_type;
public:
using backend_t = reader_backend<Device, png_tag>;
public:
reader( const Device& io_dev
, const image_read_settings< png_tag >& settings
)
: reader_base< png_tag
, ConversionPolicy
>()
, backend_t( io_dev
, settings
)
{}
reader( const Device& io_dev
, const typename ConversionPolicy::color_converter_type& cc
, const image_read_settings< png_tag >& settings
)
: reader_base< png_tag
, ConversionPolicy
>( cc )
, backend_t( io_dev
, settings
)
{}
template< typename View >
void apply( const View& view )
{
// The info structures are filled at this point.
// Now it's time for some transformations.
if( little_endian() )
{
if( this->_info._bit_depth == 16 )
{
// Swap bytes of 16 bit files to least significant byte first.
png_set_swap( this->get_struct() );
}
if( this->_info._bit_depth < 8 )
{
// swap bits of 1, 2, 4 bit packed pixel formats
png_set_packswap( this->get_struct() );
}
}
if( this->_info._color_type == PNG_COLOR_TYPE_PALETTE )
{
png_set_palette_to_rgb( this->get_struct() );
}
if( png_get_valid( this->get_struct(), this->get_info(), PNG_INFO_tRNS ) )
{
png_set_tRNS_to_alpha( this->get_struct() );
}
// Tell libpng to handle the gamma conversion for you. The final call
// is a good guess for PC generated images, but it should be configurable
// by the user at run time by the user. It is strongly suggested that
// your application support gamma correction.
if( this->_settings._apply_screen_gamma )
{
// png_set_gamma will change the image data!
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
png_set_gamma( this->get_struct()
, this->_settings._screen_gamma
, this->_info._file_gamma
);
#else
png_set_gamma( this->get_struct()
, this->_settings._screen_gamma
, this->_info._file_gamma
);
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
}
// Turn on interlace handling. REQUIRED if you are not using
// png_read_image(). To see how to handle interlacing passes,
// see the png_read_row() method below:
this->_number_passes = png_set_interlace_handling( this->get_struct() );
// The above transformation might have changed the bit_depth and color type.
png_read_update_info( this->get_struct()
, this->get_info()
);
this->_info._bit_depth = png_get_bit_depth( this->get_struct()
, this->get_info()
);
this->_info._num_channels = png_get_channels( this->get_struct()
, this->get_info()
);
this->_info._color_type = png_get_color_type( this->get_struct()
, this->get_info()
);
this->_scanline_length = png_get_rowbytes( this->get_struct()
, this->get_info()
);
switch( this->_info._color_type )
{
case PNG_COLOR_TYPE_GRAY:
{
switch( this->_info._bit_depth )
{
case 1: read_rows< gray1_image_t::view_t::reference >( view ); break;
case 2: read_rows< gray2_image_t::view_t::reference >( view ); break;
case 4: read_rows< gray4_image_t::view_t::reference >( view ); break;
case 8: read_rows< gray8_pixel_t >( view ); break;
case 16: read_rows< gray16_pixel_t >( view ); break;
default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
}
break;
}
case PNG_COLOR_TYPE_GA:
{
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
switch( this->_info._bit_depth )
{
case 8: read_rows< gray_alpha8_pixel_t > ( view ); break;
case 16: read_rows< gray_alpha16_pixel_t >( view ); break;
default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
}
#else
io_error( "gray_alpha isn't enabled. Use ENABLE_GRAY_ALPHA when building application." );
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
break;
}
case PNG_COLOR_TYPE_RGB:
{
switch( this->_info._bit_depth )
{
case 8: read_rows< rgb8_pixel_t > ( view ); break;
case 16: read_rows< rgb16_pixel_t >( view ); break;
default: io_error( "png_reader::read_data(): unknown combination of color type and bit depth" );
}
break;
}
case PNG_COLOR_TYPE_RGBA:
{
switch( this->_info._bit_depth )
{
case 8: read_rows< rgba8_pixel_t > ( view ); break;
case 16: read_rows< rgba16_pixel_t >( view ); break;
default: io_error( "png_reader_color_convert::read_data(): unknown combination of color type and bit depth" );
}
break;
}
default: io_error( "png_reader_color_convert::read_data(): unknown color type" );
}
// read rest of file, and get additional chunks in info_ptr
png_read_end( this->get_struct()
, nullptr
);
}
private:
template< typename ImagePixel
, typename View
>
void read_rows( const View& view )
{
using row_buffer_helper_t = detail::row_buffer_helper_view<ImagePixel>;
using it_t = typename row_buffer_helper_t::iterator_t;
using is_read_and_convert_t = typename is_same
<
ConversionPolicy,
detail::read_and_no_convert
>::type;
io_error_if( !detail::is_allowed< View >( this->_info
, is_read_and_convert_t()
)
, "Image types aren't compatible."
);
std::size_t rowbytes = png_get_rowbytes( this->get_struct()
, this->get_info()
);
row_buffer_helper_t buffer( rowbytes
, true
);
png_bytep row_ptr = (png_bytep)( &( buffer.data()[0]));
for( std::size_t pass = 0; pass < this->_number_passes; pass++ )
{
if( pass == this->_number_passes - 1 )
{
// skip lines if necessary
for( std::ptrdiff_t y = 0; y < this->_settings._top_left.y; ++y )
{
// Read the image using the "sparkle" effect.
png_read_rows( this->get_struct()
, &row_ptr
, nullptr
, 1
);
}
for( std::ptrdiff_t y = 0
; y < this->_settings._dim.y
; ++y
)
{
// Read the image using the "sparkle" effect.
png_read_rows( this->get_struct()
, &row_ptr
, nullptr
, 1
);
it_t first = buffer.begin() + this->_settings._top_left.x;
it_t last = first + this->_settings._dim.x; // one after last element
this->_cc_policy.read( first
, last
, view.row_begin( y ));
}
// Read the rest of the image. libpng needs that.
std::ptrdiff_t remaining_rows = static_cast< std::ptrdiff_t >( this->_info._height )
- this->_settings._top_left.y
- this->_settings._dim.y;
for( std::ptrdiff_t y = 0
; y < remaining_rows
; ++y
)
{
// Read the image using the "sparkle" effect.
png_read_rows( this->get_struct()
, &row_ptr
, nullptr
, 1
);
}
}
else
{
for( int y = 0; y < view.height(); ++y )
{
// Read the image using the "sparkle" effect.
png_read_rows( this->get_struct()
, &row_ptr
, nullptr
, 1
);
}
}
}
}
};
namespace detail {
struct png_type_format_checker
{
png_type_format_checker( png_bitdepth::type bit_depth
, png_color_type::type color_type
)
: _bit_depth ( bit_depth )
, _color_type( color_type )
{}
template< typename Image >
bool apply()
{
using is_supported_t = is_read_supported
<
typename get_pixel_type<typename Image::view_t>::type,
png_tag
>;
return is_supported_t::_bit_depth == _bit_depth
&& is_supported_t::_color_type == _color_type;
}
private:
png_bitdepth::type _bit_depth;
png_color_type::type _color_type;
};
struct png_read_is_supported
{
template< typename View >
struct apply : public is_read_supported< typename get_pixel_type< View >::type
, png_tag
>
{};
};
} // namespace detail
///
/// PNG Dynamic Image Reader
///
template< typename Device
>
class dynamic_image_reader< Device
, png_tag
>
: public reader< Device
, png_tag
, detail::read_and_no_convert
>
{
using parent_t = reader
<
Device,
png_tag,
detail::read_and_no_convert
>;
public:
dynamic_image_reader( const Device& io_dev
, const image_read_settings< png_tag >& settings
)
: parent_t( io_dev
, settings
)
{}
template< typename Images >
void apply( any_image< Images >& images )
{
detail::png_type_format_checker format_checker( this->_info._bit_depth
, this->_info._color_type
);
if( !construct_matched( images
, format_checker
))
{
io_error( "No matching image type between those of the given any_image and that of the file" );
}
else
{
this->init_image( images
, this->_settings
);
detail::dynamic_io_fnobj< detail::png_read_is_supported
, parent_t
> op( this );
apply_operation( view( images )
, op
);
}
}
};
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,678 @@
//
// Copyright 2012 Christian Henning
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_DETAIL_READER_BACKEND_HPP
#define BOOST_GIL_EXTENSION_IO_DETAIL_READER_BACKEND_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#include <boost/gil/extension/io/png/detail/base.hpp>
#include <boost/gil/extension/io/png/detail/supported_types.hpp>
namespace boost { namespace gil {
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#pragma warning(disable:4611) //interaction between '_setjmp' and C++ object destruction is non-portable
#endif
///
/// PNG Backend
///
template<typename Device >
struct reader_backend< Device
, png_tag
>
: public detail::png_struct_info_wrapper
{
public:
using format_tag_t = png_tag;
using this_t = reader_backend<Device, png_tag>;
public:
reader_backend( const Device& io_dev
, const image_read_settings< png_tag >& settings
)
: _io_dev( io_dev )
, _settings( settings )
, _info()
, _scanline_length( 0 )
, _number_passes( 0 )
{
read_header();
if( _settings._dim.x == 0 )
{
_settings._dim.x = _info._width;
}
if( _settings._dim.y == 0 )
{
_settings._dim.y = _info._height;
}
}
void read_header()
{
using boost::gil::detail::PNG_BYTES_TO_CHECK;
// check the file's first few bytes
byte_t buf[PNG_BYTES_TO_CHECK];
io_error_if( _io_dev.read( buf
, PNG_BYTES_TO_CHECK
) != PNG_BYTES_TO_CHECK
, "png_check_validity: failed to read image"
);
io_error_if( png_sig_cmp( png_bytep(buf)
, png_size_t(0)
, PNG_BYTES_TO_CHECK
) != 0
, "png_check_validity: invalid png image"
);
// Create and initialize the png_struct with the desired error handler
// functions. If you want to use the default stderr and longjump method,
// you can supply NULL for the last three parameters. We also supply the
// the compiler header file version, so that we know if the application
// was compiled with a compatible version of the library. REQUIRED
get()->_struct = png_create_read_struct( PNG_LIBPNG_VER_STRING
, nullptr // user_error_ptr
, nullptr // user_error_fn
, nullptr // user_warning_fn
);
io_error_if( get()->_struct == nullptr
, "png_reader: fail to call png_create_write_struct()"
);
png_uint_32 user_chunk_data[4];
user_chunk_data[0] = 0;
user_chunk_data[1] = 0;
user_chunk_data[2] = 0;
user_chunk_data[3] = 0;
png_set_read_user_chunk_fn( get_struct()
, user_chunk_data
, this_t::read_user_chunk_callback
);
// Allocate/initialize the memory for image information. REQUIRED.
get()->_info = png_create_info_struct( get_struct() );
if( get_info() == nullptr )
{
png_destroy_read_struct( &get()->_struct
, nullptr
, nullptr
);
io_error( "png_reader: fail to call png_create_info_struct()" );
}
// Set error handling if you are using the setjmp/longjmp method (this is
// the normal method of doing things with libpng). REQUIRED unless you
// set up your own error handlers in the png_create_read_struct() earlier.
if( setjmp( png_jmpbuf( get_struct() )))
{
//free all of the memory associated with the png_ptr and info_ptr
png_destroy_read_struct( &get()->_struct
, &get()->_info
, nullptr
);
io_error( "png is invalid" );
}
png_set_read_fn( get_struct()
, static_cast< png_voidp >( &this->_io_dev )
, this_t::read_data
);
// Set up a callback function that will be
// called after each row has been read, which you can use to control
// a progress meter or the like.
png_set_read_status_fn( get_struct()
, this_t::read_row_callback
);
// Set up a callback which implements user defined transformation.
// @todo
png_set_read_user_transform_fn( get_struct()
, png_user_transform_ptr( nullptr )
);
png_set_keep_unknown_chunks( get_struct()
, PNG_HANDLE_CHUNK_ALWAYS
, nullptr
, 0
);
// Make sure we read the signature.
// @todo make it an option
png_set_sig_bytes( get_struct()
, PNG_BYTES_TO_CHECK
);
// The call to png_read_info() gives us all of the information from the
// PNG file before the first IDAT (image data chunk). REQUIRED
png_read_info( get_struct()
, get_info()
);
///
/// Start reading the image information
///
// get PNG_IHDR chunk information from png_info structure
png_get_IHDR( get_struct()
, get_info()
, &this->_info._width
, &this->_info._height
, &this->_info._bit_depth
, &this->_info._color_type
, &this->_info._interlace_method
, &this->_info._compression_method
, &this->_info._filter_method
);
// get number of color channels in image
this->_info._num_channels = png_get_channels( get_struct()
, get_info()
);
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
// Get CIE chromacities and referenced white point
if( this->_settings._read_cie_chromacities )
{
this->_info._valid_cie_colors = png_get_cHRM( get_struct()
, get_info()
, &this->_info._white_x, &this->_info._white_y
, &this->_info._red_x, &this->_info._red_y
, &this->_info._green_x, &this->_info._green_y
, &this->_info._blue_x, &this->_info._blue_y
);
}
// get the gamma value
if( this->_settings._read_file_gamma )
{
this->_info._valid_file_gamma = png_get_gAMA( get_struct()
, get_info()
, &this->_info._file_gamma
);
if( this->_info._valid_file_gamma == false )
{
this->_info._file_gamma = 1.0;
}
}
#else
// Get CIE chromacities and referenced white point
if( this->_settings._read_cie_chromacities )
{
this->_info._valid_cie_colors = png_get_cHRM_fixed( get_struct()
, get_info()
, &this->_info._white_x, &this->_info._white_y
, &this->_info._red_x, &this->_info._red_y
, &this->_info._green_x, &this->_info._green_y
, &this->_info._blue_x, &this->_info._blue_y
);
}
// get the gamma value
if( this->_settings._read_file_gamma )
{
this->_info._valid_file_gamma = png_get_gAMA_fixed( get_struct()
, get_info()
, &this->_info._file_gamma
);
if( this->_info._valid_file_gamma == false )
{
this->_info._file_gamma = 1;
}
}
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
// get the embedded ICC profile data
if( this->_settings._read_icc_profile )
{
#if PNG_LIBPNG_VER_MINOR >= 5
png_charp icc_name = png_charp( nullptr );
png_bytep profile = png_bytep( nullptr );
this->_info._valid_icc_profile = png_get_iCCP( get_struct()
, get_info()
, &icc_name
, &this->_info._iccp_compression_type
, &profile
, &this->_info._profile_length
);
#else
png_charp icc_name = png_charp( NULL );
png_charp profile = png_charp( NULL );
this->_info._valid_icc_profile = png_get_iCCP( get_struct()
, get_info()
, &icc_name
, &this->_info._iccp_compression_type
, &profile
, &this->_info._profile_length
);
#endif
if( icc_name )
{
this->_info._icc_name.append( icc_name
, std::strlen( icc_name )
);
}
if( this->_info._profile_length != 0 )
{
std:: copy_n (profile, this->_info._profile_length, std:: back_inserter (this->_info._profile));
}
}
// get the rendering intent
if( this->_settings._read_intent )
{
this->_info._valid_intent = png_get_sRGB( get_struct()
, get_info()
, &this->_info._intent
);
}
// get image palette information from png_info structure
if( this->_settings._read_palette )
{
png_colorp palette = png_colorp( nullptr );
this->_info._valid_palette = png_get_PLTE( get_struct()
, get_info()
, &palette
, &this->_info._num_palette
);
if( this->_info._num_palette > 0 )
{
this->_info._palette.resize( this->_info._num_palette );
std::copy( palette
, palette + this->_info._num_palette
, &this->_info._palette.front()
);
}
}
// get background color
if( this->_settings._read_background )
{
png_color_16p background = png_color_16p( nullptr );
this->_info._valid_background = png_get_bKGD( get_struct()
, get_info()
, &background
);
if( background )
{
this->_info._background = *background;
}
}
// get the histogram
if( this->_settings._read_histogram )
{
png_uint_16p histogram = png_uint_16p( nullptr );
this->_info._valid_histogram = png_get_hIST( get_struct()
, get_info()
, &histogram
);
if( histogram )
{
// the number of values is set by the number of colors inside
// the palette.
if( this->_settings._read_palette == false )
{
png_colorp palette = png_colorp( nullptr );
png_get_PLTE( get_struct()
, get_info()
, &palette
, &this->_info._num_palette
);
}
std::copy( histogram
, histogram + this->_info._num_palette
, &this->_info._histogram.front()
);
}
}
// get screen offsets for the given image
if( this->_settings._read_screen_offsets )
{
this->_info._valid_offset = png_get_oFFs( get_struct()
, get_info()
, &this->_info._offset_x
, &this->_info._offset_y
, &this->_info._off_unit_type
);
}
// get pixel calibration settings
if( this->_settings._read_pixel_calibration )
{
png_charp purpose = png_charp ( nullptr );
png_charp units = png_charp ( nullptr );
png_charpp params = png_charpp( nullptr );
this->_info._valid_pixel_calibration = png_get_pCAL( get_struct()
, get_info()
, &purpose
, &this->_info._X0
, &this->_info._X1
, &this->_info._cal_type
, &this->_info._num_params
, &units
, &params
);
if( purpose )
{
this->_info._purpose.append( purpose
, std::strlen( purpose )
);
}
if( units )
{
this->_info._units.append( units
, std::strlen( units )
);
}
if( this->_info._num_params > 0 )
{
this->_info._params.resize( this->_info._num_params );
for( png_CAL_nparam::type i = 0
; i < this->_info._num_params
; ++i
)
{
this->_info._params[i].append( params[i]
, std::strlen( params[i] )
);
}
}
}
// get the physical resolution
if( this->_settings._read_physical_resolution )
{
this->_info._valid_resolution = png_get_pHYs( get_struct()
, get_info()
, &this->_info._res_x
, &this->_info._res_y
, &this->_info._phy_unit_type
);
}
// get the image resolution in pixels per meter.
if( this->_settings._read_pixels_per_meter )
{
this->_info._pixels_per_meter = png_get_pixels_per_meter( get_struct()
, get_info()
);
}
// get number of significant bits for each color channel
if( this->_settings._read_number_of_significant_bits )
{
png_color_8p sig_bits = png_color_8p( nullptr );
this->_info._valid_significant_bits = png_get_sBIT( get_struct()
, get_info()
, &sig_bits
);
// @todo Is there one or more colors?
if( sig_bits )
{
this->_info._sig_bits = *sig_bits;
}
}
#ifndef BOOST_GIL_IO_PNG_1_4_OR_LOWER
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
// get physical scale settings
if( this->_settings._read_scale_factors )
{
this->_info._valid_scale_factors = png_get_sCAL( get_struct()
, get_info()
, &this->_info._scale_unit
, &this->_info._scale_width
, &this->_info._scale_height
);
}
#else
#ifdef BOOST_GIL_IO_PNG_FIXED_POINT_SUPPORTED
if( this->_settings._read_scale_factors )
{
this->_info._valid_scale_factors = png_get_sCAL_fixed( get_struct()
, get_info()
, &this->_info._scale_unit
, &this->_info._scale_width
, &this->_info._scale_height
);
}
#else
if( this->_settings._read_scale_factors )
{
png_charp scale_width = nullptr;
png_charp scale_height = nullptr;
this->_info._valid_scale_factors = png_get_sCAL_s(
get_struct(), get_info(), &this->_info._scale_unit, &scale_width, &scale_height);
if (this->_info._valid_scale_factors)
{
if( scale_width )
{
this->_info._scale_width.append( scale_width
, std::strlen( scale_width )
);
}
if( scale_height )
{
this->_info._scale_height.append( scale_height
, std::strlen( scale_height )
);
}
}
}
#endif // BOOST_GIL_IO_PNG_FIXED_POINT_SUPPORTED
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
#endif // BOOST_GIL_IO_PNG_1_4_OR_LOWER
// get comments information from png_info structure
if( this->_settings._read_comments )
{
png_textp text = png_textp( nullptr );
this->_info._valid_text = png_get_text( get_struct()
, get_info()
, &text
, &this->_info._num_text
);
if( this->_info._num_text > 0 )
{
this->_info._text.resize( this->_info._num_text );
for( png_num_text::type i = 0
; i < this->_info._num_text
; ++i
)
{
this->_info._text[i]._compression = text[i].compression;
this->_info._text[i]._key.append( text[i].key
, std::strlen( text[i].key )
);
this->_info._text[i]._text.append( text[i].text
, std::strlen( text[i].text )
);
}
}
}
// get last modification time
if( this->_settings._read_last_modification_time )
{
png_timep mod_time = png_timep( nullptr );
this->_info._valid_modification_time = png_get_tIME( get_struct()
, get_info()
, &mod_time
);
if( mod_time )
{
this->_info._mod_time = *mod_time;
}
}
// get transparency data
if( this->_settings._read_transparency_data )
{
png_bytep trans = png_bytep ( nullptr );
png_color_16p trans_values = png_color_16p( nullptr );
this->_info._valid_transparency_factors = png_get_tRNS( get_struct()
, get_info()
, &trans
, &this->_info._num_trans
, &trans_values
);
if( trans )
{
//@todo What to do, here? How do I know the length of the "trans" array?
}
if( this->_info._num_trans )
{
this->_info._trans_values.resize( this->_info._num_trans );
std::copy( trans_values
, trans_values + this->_info._num_trans
, &this->_info._trans_values.front()
);
}
}
// @todo One day!
/*
if( false )
{
png_unknown_chunkp unknowns = png_unknown_chunkp( NULL );
int num_unknowns = static_cast< int >( png_get_unknown_chunks( get_struct()
, get_info()
, &unknowns
)
);
}
*/
}
/// Check if image is large enough.
void check_image_size( const point_t& img_dim )
{
if( _settings._dim.x > 0 )
{
if( img_dim.x < _settings._dim.x ) { io_error( "Supplied image is too small" ); }
}
else
{
if( img_dim.x < _info._width ) { io_error( "Supplied image is too small" ); }
}
if( _settings._dim.y > 0 )
{
if( img_dim.y < _settings._dim.y ) { io_error( "Supplied image is too small" ); }
}
else
{
if( img_dim.y < _info._height ) { io_error( "Supplied image is too small" ); }
}
}
protected:
static void read_data( png_structp png_ptr
, png_bytep data
, png_size_t length
)
{
static_cast<Device*>(png_get_io_ptr(png_ptr) )->read( data
, length );
}
static void flush( png_structp png_ptr )
{
static_cast<Device*>(png_get_io_ptr(png_ptr) )->flush();
}
static int read_user_chunk_callback( png_struct* /* png_ptr */
, png_unknown_chunkp /* chunk */
)
{
// @todo
return 0;
}
static void read_row_callback( png_structp /* png_ptr */
, png_uint_32 /* row_number */
, int /* pass */
)
{
// @todo
}
public:
Device _io_dev;
image_read_settings< png_tag > _settings;
image_read_info < png_tag > _info;
std::size_t _scanline_length;
std::size_t _number_passes;
};
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,167 @@
//
// Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_SCANLINE_READ_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_SCANLINE_READ_HPP
#include <boost/gil/extension/io/png/detail/is_allowed.hpp>
#include <boost/gil/extension/io/png/detail/reader_backend.hpp>
#include <boost/gil.hpp> // FIXME: Include what you use!
#include <boost/gil/io/base.hpp>
#include <boost/gil/io/conversion_policies.hpp>
#include <boost/gil/io/device.hpp>
#include <boost/gil/io/reader_base.hpp>
#include <boost/gil/io/row_buffer_helper.hpp>
#include <boost/gil/io/scanline_read_iterator.hpp>
#include <boost/gil/io/typedefs.hpp>
namespace boost { namespace gil {
///
/// PNG Reader
///
template< typename Device >
class scanline_reader< Device
, png_tag
>
: public reader_backend< Device
, png_tag
>
{
public:
using tag_t = png_tag;
using backend_t = reader_backend<Device, tag_t>;
using this_t = scanline_reader<Device, tag_t>;
using iterator_t = scanline_read_iterator<this_t>;
//
// Constructor
//
scanline_reader( const Device& io_dev
, const image_read_settings< png_tag >& settings
)
: reader_backend< Device
, png_tag
>( io_dev
, settings
)
{
initialize();
}
void read( byte_t* dst
, int
)
{
read_scanline( dst );
}
/// Skip over a scanline.
void skip( byte_t* dst, int )
{
read_scanline( dst );
}
iterator_t begin() { return iterator_t( *this ); }
iterator_t end() { return iterator_t( *this, this->_info._height ); }
private:
void initialize()
{
// Now it's time for some transformations.
if( little_endian() )
{
if( this->_info._bit_depth == 16 )
{
// Swap bytes of 16 bit files to least significant byte first.
png_set_swap( this->get()->_struct );
}
if( this->_info._bit_depth < 8 )
{
// swap bits of 1, 2, 4 bit packed pixel formats
png_set_packswap( this->get()->_struct );
}
}
if( this->_info._color_type == PNG_COLOR_TYPE_PALETTE )
{
png_set_palette_to_rgb( this->get()->_struct );
}
if( this->_info._num_trans > 0 )
{
png_set_tRNS_to_alpha( this->get()->_struct );
}
// Tell libpng to handle the gamma conversion for you. The final call
// is a good guess for PC generated images, but it should be configurable
// by the user at run time by the user. It is strongly suggested that
// your application support gamma correction.
if( this->_settings._apply_screen_gamma )
{
// png_set_gamma will change the image data!
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
png_set_gamma( this->get()->_struct
, this->_settings._screen_gamma
, this->_info._file_gamma
);
#else
png_set_gamma( this->get()->_struct
, this->_settings._screen_gamma
, this->_info._file_gamma
);
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
}
// Interlaced images are not supported.
this->_number_passes = png_set_interlace_handling( this->get()->_struct );
io_error_if( this->_number_passes != 1
, "scanline_read_iterator cannot read interlaced png images."
);
// The above transformation might have changed the bit_depth and color type.
png_read_update_info( this->get()->_struct
, this->get()->_info
);
this->_info._bit_depth = png_get_bit_depth( this->get()->_struct
, this->get()->_info
);
this->_info._num_channels = png_get_channels( this->get()->_struct
, this->get()->_info
);
this->_info._color_type = png_get_color_type( this->get()->_struct
, this->get()->_info
);
this->_scanline_length = png_get_rowbytes( this->get()->_struct
, this->get()->_info
);
}
void read_scanline( byte_t* dst )
{
png_read_row( this->get()->_struct
, dst
, NULL
);
}
};
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,362 @@
//
// Copyright 2007-2008 Christian Henning, Andreas Pokorny, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_SUPPORTED_TYPES_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_SUPPORTED_TYPES_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
#include <boost/gil/extension/toolbox/color_spaces/gray_alpha.hpp>
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
#include <cstddef>
namespace boost { namespace gil { namespace detail {
static const size_t PNG_BYTES_TO_CHECK = 4;
// Read support
template< png_bitdepth::type BitDepth
, png_color_type::type ColorType
>
struct png_rw_support_base
{
static const png_bitdepth::type _bit_depth = BitDepth;
static const png_color_type::type _color_type = ColorType;
};
template< typename Channel
, typename ColorSpace
>
struct png_read_support : read_support_false
, png_rw_support_base< 1
, PNG_COLOR_TYPE_GRAY
> {};
template< typename BitField
, bool Mutable
>
struct png_read_support< packed_dynamic_channel_reference< BitField
, 1
, Mutable
>
, gray_t
> : read_support_true
, png_rw_support_base< 1
, PNG_COLOR_TYPE_GRAY
> {};
template< typename BitField
, bool Mutable
>
struct png_read_support< packed_dynamic_channel_reference< BitField
, 2
, Mutable
>
, gray_t
> : read_support_true
, png_rw_support_base< 2
, PNG_COLOR_TYPE_GRAY
> {};
template< typename BitField
, bool Mutable
>
struct png_read_support< packed_dynamic_channel_reference< BitField
, 4
, Mutable
>
, gray_t
> : read_support_true
, png_rw_support_base< 4
, PNG_COLOR_TYPE_GRAY
> {};
template<>
struct png_read_support<uint8_t
, gray_t
> : read_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_GRAY
> {};
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_read_support<uint8_t
, gray_alpha_t
> : read_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_GA
> {};
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_read_support<uint8_t
, rgb_t
> : read_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_RGB
> {};
template<>
struct png_read_support<uint8_t
, rgba_t
> : read_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_RGBA
> {};
template<>
struct png_read_support<uint16_t
, gray_t
> : read_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_GRAY
> {};
template<>
struct png_read_support<uint16_t
, rgb_t
> : read_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_RGB
> {};
template<>
struct png_read_support<uint16_t
, rgba_t
> : read_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_RGBA
> {};
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_read_support<uint16_t
, gray_alpha_t
> : read_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_GA
> {};
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
// Write support
template< typename Channel
, typename ColorSpace
>
struct png_write_support : write_support_false
, png_rw_support_base< 1
, PNG_COLOR_TYPE_GRAY
> {};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 1
, Mutable
>
, gray_t
> : write_support_true
, png_rw_support_base< 1
, PNG_COLOR_TYPE_GRAY
>
{};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 1
, Mutable
> const
, gray_t
> : write_support_true
, png_rw_support_base< 1
, PNG_COLOR_TYPE_GRAY
>
{};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 2
, Mutable
>
, gray_t
> : write_support_true
, png_rw_support_base< 2
, PNG_COLOR_TYPE_GRAY
>
{};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 2
, Mutable
> const
, gray_t
> : write_support_true
, png_rw_support_base< 2
, PNG_COLOR_TYPE_GRAY
>
{};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 4
, Mutable
>
, gray_t
> : write_support_true
, png_rw_support_base< 4
, PNG_COLOR_TYPE_GRAY
>
{};
template< typename BitField
, bool Mutable
>
struct png_write_support< packed_dynamic_channel_reference< BitField
, 4
, Mutable
> const
, gray_t
> : write_support_true
, png_rw_support_base< 4
, PNG_COLOR_TYPE_GRAY
>
{};
template<>
struct png_write_support<uint8_t
, gray_t
> : write_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_GRAY
>
{};
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_write_support<uint8_t
, gray_alpha_t
> : write_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_GA
>
{};
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_write_support<uint8_t
, rgb_t
> : write_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_RGB
>
{};
template<>
struct png_write_support<uint8_t
, rgba_t
> : write_support_true
, png_rw_support_base< 8
, PNG_COLOR_TYPE_RGBA
>
{};
template<>
struct png_write_support<uint16_t
, gray_t
> : write_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_GRAY
>
{};
template<>
struct png_write_support<uint16_t
, rgb_t
> : write_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_RGB
>
{};
template<>
struct png_write_support<uint16_t
, rgba_t
> : write_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_RGBA
>
{};
#ifdef BOOST_GIL_IO_ENABLE_GRAY_ALPHA
template<>
struct png_write_support<uint16_t
, gray_alpha_t
> : write_support_true
, png_rw_support_base< 16
, PNG_COLOR_TYPE_GA
>
{};
#endif // BOOST_GIL_IO_ENABLE_GRAY_ALPHA
} // namespace detail
template< typename Pixel >
struct is_read_supported< Pixel
, png_tag
>
: mpl::bool_< detail::png_read_support< typename channel_type< Pixel >::type
, typename color_space_type< Pixel >::type
>::is_supported
>
{
using parent_t = detail::png_read_support
<
typename channel_type<Pixel>::type,
typename color_space_type<Pixel>::type
>;
static const png_bitdepth::type _bit_depth = parent_t::_bit_depth;
static const png_color_type::type _color_type = parent_t::_color_type;
};
template< typename Pixel >
struct is_write_supported< Pixel
, png_tag
>
: mpl::bool_< detail::png_write_support< typename channel_type< Pixel >::type
, typename color_space_type< Pixel >::type
>::is_supported
>
{
using parent_t = detail::png_write_support
<
typename channel_type<Pixel>::type,
typename color_space_type<Pixel>::type
>;
static const png_bitdepth::type _bit_depth = parent_t::_bit_depth;
static const png_color_type::type _color_type = parent_t::_color_type;
};
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,239 @@
//
// Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_WRITE_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_WRITE_HPP
#include <boost/gil/extension/io/png/detail/writer_backend.hpp>
#include <boost/gil/io/device.hpp>
#include <boost/gil/io/dynamic_io_new.hpp>
#include <boost/gil/io/row_buffer_helper.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/not.hpp>
#include <type_traits>
namespace boost { namespace gil {
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#endif
namespace detail {
struct png_write_is_supported
{
template< typename View >
struct apply
: public is_write_supported< typename get_pixel_type< View >::type
, png_tag
>
{};
};
} // namespace detail
///
/// PNG Writer
///
template< typename Device >
class writer< Device
, png_tag
>
: public writer_backend< Device
, png_tag
>
{
public:
using backend_t = writer_backend<Device, png_tag>;
writer( const Device& io_dev
, const image_write_info< png_tag >& info
)
: backend_t( io_dev
, info
)
{}
template< typename View >
void apply( const View& view )
{
io_error_if( view.width() == 0 && view.height() == 0
, "png format cannot handle empty views."
);
this->write_header( view );
write_view( view
, typename is_bit_aligned< typename View::value_type >::type()
);
}
private:
template<typename View>
void write_view( const View& view
, mpl::false_ // is bit aligned
)
{
using pixel_t = typename get_pixel_type<View>::type;
using png_rw_info = detail::png_write_support
<
typename channel_type<pixel_t>::type,
typename color_space_type<pixel_t>::type
>;
if( little_endian() )
{
set_swap< png_rw_info >();
}
std::vector< pixel< typename channel_type< View >::type
, layout<typename color_space_type< View >::type >
>
> row_buffer( view.width() );
for( int y = 0; y != view.height(); ++ y)
{
std::copy( view.row_begin( y )
, view.row_end ( y )
, row_buffer.begin()
);
png_write_row( this->get_struct()
, reinterpret_cast< png_bytep >( row_buffer.data() )
);
}
png_write_end( this->get_struct()
, this->get_info()
);
}
template<typename View>
void write_view( const View& view
, mpl::true_ // is bit aligned
)
{
using png_rw_info = detail::png_write_support
<
typename kth_semantic_element_type<typename View::value_type, 0>::type,
typename color_space_type<View>::type
>;
if (little_endian() )
{
set_swap< png_rw_info >();
}
detail::row_buffer_helper_view< View > row_buffer( view.width()
, false
);
for( int y = 0; y != view.height(); ++y )
{
std::copy( view.row_begin( y )
, view.row_end ( y )
, row_buffer.begin()
);
png_write_row( this->get_struct()
, reinterpret_cast< png_bytep >( row_buffer.data() )
);
}
png_free_data( this->get_struct()
, this->get_info()
, PNG_FREE_UNKN
, -1
);
png_write_end( this->get_struct()
, this->get_info()
);
}
template< typename Info > struct is_less_than_eight : mpl::less< mpl::int_< Info::_bit_depth >, mpl::int_< 8 > > {};
template< typename Info > struct is_equal_to_sixteen : mpl::equal_to< mpl::int_< Info::_bit_depth >, mpl::int_< 16 > > {};
template <typename Info>
void set_swap(typename std::enable_if<is_less_than_eight<Info>::value>::type* /*ptr*/ = 0)
{
png_set_packswap(this->get_struct());
}
template <typename Info>
void set_swap(typename std::enable_if<is_equal_to_sixteen<Info>::value>::type* /*ptr*/ = 0)
{
png_set_swap(this->get_struct());
}
template <typename Info>
void set_swap(
typename std::enable_if
<
mpl::and_
<
mpl::not_<is_less_than_eight<Info>>,
mpl::not_<is_equal_to_sixteen<Info>>
>::value
>::type* /*ptr*/ = nullptr)
{
}
};
///
/// PNG Dynamic Image Writer
///
template< typename Device >
class dynamic_image_writer< Device
, png_tag
>
: public writer< Device
, png_tag
>
{
using parent_t = writer<Device, png_tag>;
public:
dynamic_image_writer( const Device& io_dev
, const image_write_info< png_tag >& info
)
: parent_t( io_dev
, info
)
{}
template< typename Views >
void apply( const any_image_view< Views >& views )
{
detail::dynamic_io_fnobj< detail::png_write_is_supported
, parent_t
> op( this );
apply_operation( views, op );
}
};
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif
} // namespace gil
} // namespace boost
#endif

View File

@@ -0,0 +1,502 @@
//
// Copyright 2012 Christian Henning
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_EXTENSION_IO_PNG_DETAIL_WRITER_BACKEND_HPP
#define BOOST_GIL_EXTENSION_IO_PNG_DETAIL_WRITER_BACKEND_HPP
#include <boost/gil/extension/io/png/tags.hpp>
#include <boost/gil/extension/io/png/detail/base.hpp>
#include <boost/gil/extension/io/png/detail/supported_types.hpp>
#include <boost/gil/io/base.hpp>
#include <boost/gil/io/typedefs.hpp>
namespace boost { namespace gil {
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(push)
#pragma warning(disable:4512) //assignment operator could not be generated
#pragma warning(disable:4611) //interaction between '_setjmp' and C++ object destruction is non-portable
#endif
///
/// PNG Writer Backend
///
template< typename Device >
struct writer_backend< Device
, png_tag
>
: public detail::png_struct_info_wrapper
{
private:
using this_t = writer_backend<Device, png_tag>;
public:
using format_tag_t = png_tag;
///
/// Constructor
///
writer_backend( const Device& io_dev
, const image_write_info< png_tag >& info
)
: png_struct_info_wrapper( false )
, _io_dev( io_dev )
, _info( info )
{
// Create and initialize the png_struct with the desired error handler
// functions. If you want to use the default stderr and longjump method,
// you can supply NULL for the last three parameters. We also check that
// the library version is compatible with the one used at compile time,
// in case we are using dynamically linked libraries. REQUIRED.
get()->_struct = png_create_write_struct( PNG_LIBPNG_VER_STRING
, nullptr // user_error_ptr
, nullptr // user_error_fn
, nullptr // user_warning_fn
);
io_error_if( get_struct() == nullptr
, "png_writer: fail to call png_create_write_struct()"
);
// Allocate/initialize the image information data. REQUIRED
get()->_info = png_create_info_struct( get_struct() );
if( get_info() == nullptr )
{
png_destroy_write_struct( &get()->_struct
, nullptr
);
io_error( "png_writer: fail to call png_create_info_struct()" );
}
// Set error handling. REQUIRED if you aren't supplying your own
// error handling functions in the png_create_write_struct() call.
if( setjmp( png_jmpbuf( get_struct() )))
{
//free all of the memory associated with the png_ptr and info_ptr
png_destroy_write_struct( &get()->_struct
, &get()->_info
);
io_error( "png_writer: fail to call setjmp()" );
}
init_io( get_struct() );
}
protected:
template< typename View >
void write_header( const View& view )
{
using png_rw_info_t = detail::png_write_support
<
typename channel_type<typename get_pixel_type<View>::type>::type,
typename color_space_type<View>::type
>;
// Set the image information here. Width and height are up to 2^31,
// bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
// the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
// PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
// or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
// PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
// currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
png_set_IHDR( get_struct()
, get_info()
, static_cast< png_image_width::type >( view.width() )
, static_cast< png_image_height::type >( view.height() )
, static_cast< png_bitdepth::type >( png_rw_info_t::_bit_depth )
, static_cast< png_color_type::type >( png_rw_info_t::_color_type )
, _info._interlace_method
, _info._compression_type
, _info._filter_method
);
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
if( _info._valid_cie_colors )
{
png_set_cHRM( get_struct()
, get_info()
, _info._white_x
, _info._white_y
, _info._red_x
, _info._red_y
, _info._green_x
, _info._green_y
, _info._blue_x
, _info._blue_y
);
}
if( _info._valid_file_gamma )
{
png_set_gAMA( get_struct()
, get_info()
, _info._file_gamma
);
}
#else
if( _info._valid_cie_colors )
{
png_set_cHRM_fixed( get_struct()
, get_info()
, _info._white_x
, _info._white_y
, _info._red_x
, _info._red_y
, _info._green_x
, _info._green_y
, _info._blue_x
, _info._blue_y
);
}
if( _info._valid_file_gamma )
{
png_set_gAMA_fixed( get_struct()
, get_info()
, _info._file_gamma
);
}
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
if( _info._valid_icc_profile )
{
#if PNG_LIBPNG_VER_MINOR >= 5
png_set_iCCP( get_struct()
, get_info()
, const_cast< png_charp >( _info._icc_name.c_str() )
, _info._iccp_compression_type
, reinterpret_cast< png_const_bytep >( & (_info._profile.front ()) )
, _info._profile_length
);
#else
png_set_iCCP( get_struct()
, get_info()
, const_cast< png_charp >( _info._icc_name.c_str() )
, _info._iccp_compression_type
, const_cast< png_charp >( & (_info._profile.front()) )
, _info._profile_length
);
#endif
}
if( _info._valid_intent )
{
png_set_sRGB( get_struct()
, get_info()
, _info._intent
);
}
if( _info._valid_palette )
{
png_set_PLTE( get_struct()
, get_info()
, const_cast< png_colorp >( &_info._palette.front() )
, _info._num_palette
);
}
if( _info._valid_background )
{
png_set_bKGD( get_struct()
, get_info()
, const_cast< png_color_16p >( &_info._background )
);
}
if( _info._valid_histogram )
{
png_set_hIST( get_struct()
, get_info()
, const_cast< png_uint_16p >( &_info._histogram.front() )
);
}
if( _info._valid_offset )
{
png_set_oFFs( get_struct()
, get_info()
, _info._offset_x
, _info._offset_y
, _info._off_unit_type
);
}
if( _info._valid_pixel_calibration )
{
std::vector< const char* > params( _info._num_params );
for( std::size_t i = 0; i < params.size(); ++i )
{
params[i] = _info._params[ i ].c_str();
}
png_set_pCAL( get_struct()
, get_info()
, const_cast< png_charp >( _info._purpose.c_str() )
, _info._X0
, _info._X1
, _info._cal_type
, _info._num_params
, const_cast< png_charp >( _info._units.c_str() )
, const_cast< png_charpp >( &params.front() )
);
}
if( _info._valid_resolution )
{
png_set_pHYs( get_struct()
, get_info()
, _info._res_x
, _info._res_y
, _info._phy_unit_type
);
}
if( _info._valid_significant_bits )
{
png_set_sBIT( get_struct()
, get_info()
, const_cast< png_color_8p >( &_info._sig_bits )
);
}
#ifndef BOOST_GIL_IO_PNG_1_4_OR_LOWER
#ifdef BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
if( _info._valid_scale_factors )
{
png_set_sCAL( get_struct()
, get_info()
, this->_info._scale_unit
, this->_info._scale_width
, this->_info._scale_height
);
}
#else
#ifdef BOOST_GIL_IO_PNG_FIXED_POINT_SUPPORTED
if( _info._valid_scale_factors )
{
png_set_sCAL_fixed( get_struct()
, get_info()
, this->_info._scale_unit
, this->_info._scale_width
, this->_info._scale_height
);
}
#else
if( _info._valid_scale_factors )
{
png_set_sCAL_s( get_struct()
, get_info()
, this->_info._scale_unit
, const_cast< png_charp >( this->_info._scale_width.c_str() )
, const_cast< png_charp >( this->_info._scale_height.c_str() )
);
}
#endif // BOOST_GIL_IO_PNG_FIXED_POINT_SUPPORTED
#endif // BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
#endif // BOOST_GIL_IO_PNG_1_4_OR_LOWER
if( _info._valid_text )
{
std::vector< png_text > texts( _info._num_text );
for( std::size_t i = 0; i < texts.size(); ++i )
{
png_text pt;
pt.compression = _info._text[i]._compression;
pt.key = const_cast< png_charp >( this->_info._text[i]._key.c_str() );
pt.text = const_cast< png_charp >( this->_info._text[i]._text.c_str() );
pt.text_length = _info._text[i]._text.length();
texts[i] = pt;
}
png_set_text( get_struct()
, get_info()
, &texts.front()
, _info._num_text
);
}
if( _info._valid_modification_time )
{
png_set_tIME( get_struct()
, get_info()
, const_cast< png_timep >( &_info._mod_time )
);
}
if( _info._valid_transparency_factors )
{
int sample_max = ( 1u << _info._bit_depth );
/* libpng doesn't reject a tRNS chunk with out-of-range samples */
if( !( ( _info._color_type == PNG_COLOR_TYPE_GRAY
&& (int) _info._trans_values[0].gray > sample_max
)
|| ( _info._color_type == PNG_COLOR_TYPE_RGB
&&( (int) _info._trans_values[0].red > sample_max
|| (int) _info._trans_values[0].green > sample_max
|| (int) _info._trans_values[0].blue > sample_max
)
)
)
)
{
//@todo Fix that once reading transparency values works
/*
png_set_tRNS( get_struct()
, get_info()
, trans
, num_trans
, trans_values
);
*/
}
}
// Compression Levels - valid values are [0,9]
png_set_compression_level( get_struct()
, _info._compression_level
);
png_set_compression_mem_level( get_struct()
, _info._compression_mem_level
);
png_set_compression_strategy( get_struct()
, _info._compression_strategy
);
png_set_compression_window_bits( get_struct()
, _info._compression_window_bits
);
png_set_compression_method( get_struct()
, _info._compression_method
);
png_set_compression_buffer_size( get_struct()
, _info._compression_buffer_size
);
#ifdef BOOST_GIL_IO_PNG_DITHERING_SUPPORTED
// Dithering
if( _info._set_dithering )
{
png_set_dither( get_struct()
, &_info._dithering_palette.front()
, _info._dithering_num_palette
, _info._dithering_maximum_colors
, &_info._dithering_histogram.front()
, _info._full_dither
);
}
#endif // BOOST_GIL_IO_PNG_DITHERING_SUPPORTED
// Filter
if( _info._set_filter )
{
png_set_filter( get_struct()
, 0
, _info._filter
);
}
// Invert Mono
if( _info._invert_mono )
{
png_set_invert_mono( get_struct() );
}
// True Bits
if( _info._set_true_bits )
{
png_set_sBIT( get_struct()
, get_info()
, &_info._true_bits.front()
);
}
// sRGB Intent
if( _info._set_srgb_intent )
{
png_set_sRGB( get_struct()
, get_info()
, _info._srgb_intent
);
}
// Strip Alpha
if( _info._strip_alpha )
{
png_set_strip_alpha( get_struct() );
}
// Swap Alpha
if( _info._swap_alpha )
{
png_set_swap_alpha( get_struct() );
}
png_write_info( get_struct()
, get_info()
);
}
protected:
static void write_data( png_structp png_ptr
, png_bytep data
, png_size_t length
)
{
static_cast< Device* >( png_get_io_ptr( png_ptr ))->write( data
, length );
}
static void flush( png_structp png_ptr )
{
static_cast< Device* >(png_get_io_ptr(png_ptr) )->flush();
}
private:
void init_io( png_structp png_ptr )
{
png_set_write_fn( png_ptr
, static_cast< void* > ( &this->_io_dev )
, static_cast< png_rw_ptr > ( &this_t::write_data )
, static_cast< png_flush_ptr >( &this_t::flush )
);
}
public:
Device _io_dev;
image_write_info< png_tag > _info;
};
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
#pragma warning(pop)
#endif
} // namespace gil
} // namespace boost
#endif