convert_color_space.hpp
Go to the documentation of this file.
/*
* Copyright (C) 2007,2008 Alex Shulgin
*
* This file is part of png++ the C++ wrapper for libpng. PNG++ is free
* software; the exact copying conditions are as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PNGPP_CONVERT_COLOR_SPACE_HPP_INCLUDED
#define PNGPP_CONVERT_COLOR_SPACE_HPP_INCLUDED
#include "error.hpp"
#include "rgb_pixel.hpp"
#include "rgba_pixel.hpp"
#include "gray_pixel.hpp"
#include "ga_pixel.hpp"
#include "index_pixel.hpp"
#include "reader.hpp"
#include "writer.hpp"
namespace png
{
namespace detail
{
template< typename pixel >
struct convert_color_space_impl
{
typedef pixel_traits< pixel > traits;
typedef typename traits::component_type component_type;
typedef basic_alpha_pixel_traits< component_type > alpha_traits;
template< class reader >
void operator()(reader& io) const
{
handle_16(io);
handle_alpha(io, alpha_traits::get_alpha_filler());
handle_palette(io);
handle_rgb(io);
handle_gray(io);
io.set_color_type(traits::get_color_type());
io.set_bit_depth(traits::get_bit_depth());
}
protected:
static void expand_8_to_16(png_struct*, png_row_info* row_info,
byte* row)
{
#ifdef DEBUG_EXPAND_8_16
printf("row: width=%d, bytes=%d, channels=%d\n",
row_info->width, row_info->rowbytes, row_info->channels);
printf("<= ");
dump_row(row, row_info->rowbytes);
#endif
for (uint_32 i = row_info->rowbytes; i-- > 0; )
{
row[2*i + 1] = row[i];
row[2*i + 0] = 0;
}
#ifdef DEBUG_EXPAND_8_16
printf("=> ");
dump_row(row, 2*row_info->rowbytes);
#endif
}
#ifdef DEBUG_EXPAND_8_16
static void dump_row(byte const* row, uint_32 width)
{
printf("{");
for (uint_32 i = 0; i < width; ++i)
{
printf(" %02x,", row[i]);
}
printf(" }\n");
}
#endif
template< class reader >
static void handle_16(reader& io)
{
if (io.get_bit_depth() == 16 && traits::get_bit_depth() == 8)
{
#ifdef PNG_READ_16_TO_8_SUPPORTED
io.set_strip_16();
#else
throw error("expected 8-bit data but found 16-bit; recompile with PNG_READ_16_TO_8_SUPPORTED");
#endif
}
if (io.get_bit_depth() != 16 && traits::get_bit_depth() == 16)
{
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
io.set_read_user_transform(expand_8_to_16);
io.set_user_transform_info(NULL, 16,
traits::get_channels());
#else
throw error("expected 16-bit data but found 8-bit; recompile with PNG_READ_USER_TRANSFORM_SUPPORTED");
#endif
}
}
template< class reader >
static void handle_alpha(reader& io, uint_32 filler)
{
bool src_alpha = (io.get_color_type() & color_mask_alpha);
bool src_tRNS = io.has_chunk(chunk_tRNS);
bool dst_alpha = traits::get_color_type() & color_mask_alpha;
if ((src_alpha || src_tRNS) && !dst_alpha)
{
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
io.set_strip_alpha();
#else
throw error("alpha channel unexpected; recompile with PNG_READ_STRIP_ALPHA_SUPPORTED");
#endif
}
if (!src_alpha && dst_alpha)
{
#if defined(PNG_tRNS_SUPPORTED) && defined(PNG_READ_EXPAND_SUPPORTED)
if (src_tRNS)
{
io.set_tRNS_to_alpha();
return;
}
#endif
#if defined(PNG_READ_FILLER_SUPPORTED) && !defined(PNG_1_0_X)
io.set_add_alpha(filler, filler_after);
#else
throw error("expected alpha channel but none found; recompile with PNG_READ_FILLER_SUPPORTED and be sure to use libpng > 1.0.x");
#endif
}
}
template< class reader >
static void handle_palette(reader& io)
{
bool src_palette =
io.get_color_type() == color_type_palette;
bool dst_palette =
traits::get_color_type() == color_type_palette;
if (src_palette && !dst_palette)
{
#ifdef PNG_READ_EXPAND_SUPPORTED
io.set_palette_to_rgb();
io.get_info().drop_palette();
#else
throw error("indexed colors unexpected; recompile with PNG_READ_EXPAND_SUPPORTED");
#endif
}
else if (!src_palette && dst_palette)
{
throw error("conversion to indexed colors is unsupported (yet)");
}
else if (src_palette && dst_palette
&& io.get_bit_depth() != traits::get_bit_depth())
{
if (traits::get_bit_depth() == 8)
{
#ifdef PNG_READ_PACK_SUPPORTED
io.set_packing();
#endif
}
else
{
throw error("cannot convert to indexed colors with bit-depth < 8");
}
}
}
template< class reader >
static void handle_rgb(reader& io)
{
bool src_rgb =
io.get_color_type() & (color_mask_rgb | color_mask_palette);
bool dst_rgb = traits::get_color_type() & color_mask_rgb;
if (src_rgb && !dst_rgb)
{
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
io.set_rgb_to_gray(/*rgb_to_gray_error*/);
#else
throw error("grayscale data expected; recompile with PNG_READ_RGB_TO_GRAY_SUPPORTED");
#endif
}
if (!src_rgb && dst_rgb)
{
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
io.set_gray_to_rgb();
#else
throw error("expected RGB data; recompile with PNG_READ_GRAY_TO_RGB_SUPPORTED");
#endif
}
}
template< class reader >
static void handle_gray(reader& io)
{
if ((io.get_color_type() & ~color_mask_alpha)
== color_type_gray)
{
if (io.get_bit_depth() < 8 && traits::get_bit_depth() >= 8)
{
#ifdef PNG_READ_EXPAND_SUPPORTED
io.set_gray_1_2_4_to_8();
#else
throw error("convert_color_space: expected 8-bit data; recompile with PNG_READ_EXPAND_SUPPORTED");
#endif
}
}
}
};
} // namespace detal
template< typename pixel >
struct convert_color_space
{
};
template<>
struct convert_color_space< rgb_pixel >
: detail::convert_color_space_impl< rgb_pixel >
{
};
template<>
struct convert_color_space< rgb_pixel_16 >
: detail::convert_color_space_impl< rgb_pixel_16 >
{
};
template<>
struct convert_color_space< rgba_pixel >
: detail::convert_color_space_impl< rgba_pixel >
{
};
template<>
struct convert_color_space< rgba_pixel_16 >
: detail::convert_color_space_impl< rgba_pixel_16 >
{
};
template<>
struct convert_color_space< gray_pixel >
: detail::convert_color_space_impl< gray_pixel >
{
};
template<>
struct convert_color_space< gray_pixel_16 >
: detail::convert_color_space_impl< gray_pixel_16 >
{
};
template<>
struct convert_color_space< ga_pixel >
: detail::convert_color_space_impl< ga_pixel >
{
};
template<>
struct convert_color_space< ga_pixel_16 >
: detail::convert_color_space_impl< ga_pixel_16 >
{
};
template<>
struct convert_color_space< index_pixel >
: detail::convert_color_space_impl< index_pixel >
{
};
} // namespace png
#endif // PNGPP_CONVERT_COLOR_SPACE_HPP_INCLUDED
png::detail::convert_color_space_impl::handle_gray
static void handle_gray(reader &io)
Definition: convert_color_space.hpp:221
gray_pixel.hpp
png::io_base::set_add_alpha
void set_add_alpha(uint_32 filler, filler_type type) const
Definition: io_base.hpp:301
png::basic_ga_pixel
Basic Gray+Alpha pixel type.
Definition: ga_pixel.hpp:46
png::io_base::set_read_user_transform
void set_read_user_transform(png_user_transform_ptr transform_fn)
Definition: io_base.hpp:400
png::basic_rgb_pixel
RGB pixel type.
Definition: rgb_pixel.hpp:44
png::io_base::get_bit_depth
int get_bit_depth() const
Definition: io_base.hpp:150
png::detail::convert_color_space_impl::handle_alpha
static void handle_alpha(reader &io, uint_32 filler)
Definition: convert_color_space.hpp:130
png::chunk_tRNS
Definition: types.hpp:104
png::color_mask_rgb
Definition: types.hpp:62
index_pixel.hpp
png::detail::convert_color_space_impl::handle_16
static void handle_16(reader &io)
Definition: convert_color_space.hpp:107
png::detail::convert_color_space_impl::handle_palette
static void handle_palette(reader &io)
Definition: convert_color_space.hpp:161
png::index_pixel
The 8-bit Indexed (colormap) pixel type.
Definition: index_pixel.hpp:44
png::color_mask_alpha
Definition: types.hpp:63
png::reader
The PNG reader class template. This is the low-level reading interface–use image class or consumer c...
Definition: reader.hpp:65
png::pixel_traits
Pixel traits class template.
Definition: pixel_traits.hpp:48
png::detail::convert_color_space_impl::dump_row
static void dump_row(byte const *row, uint_32 width)
Definition: convert_color_space.hpp:95
png::io_base::get_color_type
color_type get_color_type() const
Definition: io_base.hpp:140
png::io_base::set_strip_16
void set_strip_16() const
Definition: io_base.hpp:392
png::detail::convert_color_space_impl
IO transformation class template. Converts image color space.
Definition: convert_color_space.hpp:54
png::io_base::set_bit_depth
void set_bit_depth(int bit_depth)
Definition: io_base.hpp:155
png::gray_pixel_16
uint_16 gray_pixel_16
The 16-bit Grayscale pixel type.
Definition: gray_pixel.hpp:49
png::io_base::has_chunk
bool has_chunk(chunk id)
Definition: io_base.hpp:192
rgb_pixel.hpp
png::basic_rgba_pixel
RGBA pixel type.
Definition: rgba_pixel.hpp:44
png::uint_32
png_uint_32 uint_32
Definition: types.hpp:41
png::byte
png_byte byte
Definition: types.hpp:39
png::io_base::set_gray_to_rgb
void set_gray_to_rgb() const
Definition: io_base.hpp:228
png::basic_alpha_pixel_traits::get_alpha_filler
static component get_alpha_filler()
Returns the default alpha channel filler for full opacity.
Definition: pixel_traits.hpp:95
png::detail::convert_color_space_impl::operator()
void operator()(reader &io) const
Definition: convert_color_space.hpp:61
png::image_info::drop_palette
void drop_palette()
Removes all entries from the palette.
Definition: image_info.hpp:155
png::io_base::set_color_type
void set_color_type(color_type color_space)
Definition: io_base.hpp:145
png::io_base::get_info
info & get_info()
Definition: io_base.hpp:87
png::filler_after
Definition: types.hpp:69
png::detail::convert_color_space_impl::expand_8_to_16
static void expand_8_to_16(png_struct *, png_row_info *row_info, byte *row)
Definition: convert_color_space.hpp:74
reader.hpp
png::color_mask_palette
Definition: types.hpp:60
png::gray_pixel
byte gray_pixel
The 8-bit Grayscale pixel type.
Definition: gray_pixel.hpp:44
error.hpp
png::color_type_gray
Definition: types.hpp:49
png::io_base::set_packing
void set_packing() const
Definition: io_base.hpp:320
png::detail::convert_color_space_impl::handle_rgb
static void handle_rgb(reader &io)
Definition: convert_color_space.hpp:197
writer.hpp
png::detail::convert_color_space_impl::traits
pixel_traits< pixel > traits
Definition: convert_color_space.hpp:56
png::convert_color_space
IO transformation class template. Converts image color space.
Definition: convert_color_space.hpp:256
png::io_base::set_palette_to_rgb
void set_palette_to_rgb() const
Definition: io_base.hpp:206
rgba_pixel.hpp
png::detail::convert_color_space_impl::alpha_traits
basic_alpha_pixel_traits< component_type > alpha_traits
Definition: convert_color_space.hpp:58
png::io_base::set_tRNS_to_alpha
void set_tRNS_to_alpha() const
Definition: io_base.hpp:212
png
Definition: color.hpp:36
png::io_base::set_gray_1_2_4_to_8
void set_gray_1_2_4_to_8() const
Definition: io_base.hpp:200
png::io_base::set_user_transform_info
void set_user_transform_info(void *info, int bit_depth, int channels)
Definition: io_base.hpp:409
png::io_base::set_rgb_to_gray
void set_rgb_to_gray(rgb_to_gray_error_action error_action=rgb_to_gray_silent, double red_weight=-1.0, double green_weight=-1.0) const
Definition: io_base.hpp:236
png::color_type_palette
Definition: types.hpp:50
ga_pixel.hpp
png::error
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:57
png::detail::convert_color_space_impl::component_type
traits::component_type component_type
Definition: convert_color_space.hpp:57
png::io_base::set_strip_alpha
void set_strip_alpha() const
Definition: io_base.hpp:266
png::basic_alpha_pixel_traits
Basic pixel traits class template for pixels with alpha channel.
Definition: pixel_traits.hpp:89