Первый коммит

This commit is contained in:
2025-02-03 15:16:12 +06:00
commit d40c3bad86
287 changed files with 124575 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
/*
* 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_COLOR_HPP_INCLUDED
#define PNGPP_COLOR_HPP_INCLUDED
#include "types.hpp"
namespace png
{
/**
* \brief PNG color struct extension. Adds constructors.
*/
struct color
: png_color
{
explicit color(byte r = 0, byte g = 0, byte b = 0)
{
this->red = r;
this->green = g;
this->blue = b;
}
/**
* \brief Initializes color with a copy of png_color object.
*/
color(png_color const& other)
{
this->red = other.red;
this->green = other.green;
this->blue = other.blue;
}
};
} // namespace png
#endif // PNGPP_COLOR_HPP_INCLUDED

112
Libs/png++/png++/config.hpp Normal file
View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 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_CONFIG_HPP_INCLUDED
#define PNGPP_CONFIG_HPP_INCLUDED
#include <stdlib.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <features.h>
#ifndef __USE_GNU
#define __MUSL__
#endif
#undef _GNU_SOURCE
#else
#include <features.h>
#ifndef __USE_GNU
#define __MUSL__
#endif
#endif
// Endianness test
#if defined(__GLIBC__) or defined(__MUSL__)
#include <endian.h>
#elif defined(_WIN32)
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __LITTLE_ENDIAN
#elif defined(__APPLE__)
#include <machine/endian.h>
#include <sys/_endian.h>
#elif defined(__FreeBSD__)
#include <machine/endian.h>
#include <sys/endian.h>
#elif defined(__sun)
#include <sys/isa_defs.h>
#else
#error Byte-order could not be detected.
#endif
// Determine C++11 features
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
#define PNGPP_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
// gcc c++11 support list
// http://gcc.gnu.org/projects/cxx0x.html
// gcc supports static_assert since 4.3
#if (PNGPP_GCC_VERSION >= 40300)
#define PNGPP_HAS_STATIC_ASSERT
#endif
// gcc supports std::move since 4.6
#if (PNGPP_GCC_VERSION >= 40600)
#define PNGPP_HAS_STD_MOVE
#endif
#undef PNGPP_GCC_VERSION
#elif defined(_MSC_VER)
// MS Visual C++ compiler supports static_assert and std::move since VS2010
// http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
#if (_MSC_VER >= 1600)
#define PNGPP_HAS_STATIC_ASSERT
#define PNGPP_HAS_STD_MOVE
#endif
#endif
#endif // PNGPP_CONFIG_HPP_INCLUDED

View File

@@ -0,0 +1,255 @@
/*
* 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_CONSUMER_HPP_INCLUDED
#define PNGPP_CONSUMER_HPP_INCLUDED
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <istream>
#include "config.hpp"
#include "error.hpp"
#include "streaming_base.hpp"
#include "reader.hpp"
#include "pixel_buffer.hpp"
namespace png
{
/**
* \brief Pixel consumer class template.
*
* Used as a base class for custom pixel consumer classes as well
* as inside image class implementation to read pixels into the
* pixel buffer.
*
* Encapsulates PNG %image reading procedure. In order to create
* a custom pixel %consumer use CRTP trick:
*
* \code
* class pixel_consumer
* : public png::consumer< pixel, pixel_consumer >
* {
* ...
* };
* \endcode
*
* Your pixel %consumer class should implement \c get_next_row()
* method and \c reset() method (optional). Their signatures are
* as follows:
*
* \code
* png::byte* get_next_row(png::uint_32 pos);
* void reset(size_t pass);
* \endcode
*
* The \c get_next_row() method is called every time a new row of
* %image data is available to the reader. The position of the row
* being read is passed as \c pos parameter. The \c pos takes
* values from \c 0 to \c <image_height>-1 inclusively. The
* method should return the starting address of a row buffer
* capable of storing appropriate amount of pixels (i.e. the width
* of the %image being read). The address should be casted to
* png::byte* pointer type using \c reinterpret_cast<> or a
* C-style cast.
*
* The optional \c reset() method is called every time the new
* pass of interlaced %image processing starts. The number of
* interlace pass is avaiable as the only parameter of the method.
* For non-interlaced images the method is called once prior to
* any calls to \c get_next_row(). The value of \c 0 is passed
* for the \c pass number.
*
* An optional template parameter \c info_holder encapsulates
* image_info storage policy. Using def_image_info_holder results
* in image_info object stored as a sub-object of the consumer
* class. You may specify image_info_ref_holder in order to use a
* reference to the externally stored image_info object. This way
* you will have to construct the consumer object passing the
* reference to image_info object.
*
* Also, you might want implement an %info holder object yourself
* to fine-tune your code. In any case, you can access the
* image_info object from your %consumer class methods using the
* following code:
*
* \code
* png::image_info& info = m_info_holder.get_info();
* \endcode
*
* An optional \c bool template parameter \c interlacing_supported
* specifies whether reading interlacing images is supported by
* your %consumer class. It defaults to \c false. An attempt to
* read an interlaced %image will result in discarding pixels
* obtained at all the interlacing passes except the last one.
*
* In order to fully support interlacing specify \c true for \c
* interlacing_supported parameter and implement \c reset()
* method.
*
* \see image, generator
*/
template< typename pixel,
class pixcon,
class info_holder = def_image_info_holder,
bool interlacing_supported = false >
class consumer
: public streaming_base< pixel, info_holder >
{
public:
typedef pixel_traits< pixel > traits;
/**
* \brief The default io transformation: does nothing.
*/
struct transform_identity
{
void operator()(io_base&) const {}
};
/**
* \brief Reads an image from the stream using default io
* transformation.
*/
template< typename istream >
void read(istream& stream)
{
read(stream, transform_identity());
}
/**
* \brief Reads an image from the stream using custom io
* transformation.
*
* Essentially, this method constructs a reader object and
* instructs it to read the image from the stream. It handles
* IO transformation, as well as interlaced image reading.
*/
template< typename istream, class transformation >
void read(istream& stream, transformation const& transform)
{
reader< istream > rd(stream);
rd.read_info();
transform(rd);
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (pixel_traits< pixel >::get_bit_depth() == 16)
{
#ifdef PNG_READ_SWAP_SUPPORTED
rd.set_swap();
#else
throw error("Cannot read 16-bit image: recompile with PNG_READ_SWAP_SUPPORTED.");
#endif
}
#endif
// interlace handling _must_ be set up prior to info update
size_t pass_count;
if (rd.get_interlace_type() != interlace_none)
{
#ifdef PNG_READ_INTERLACING_SUPPORTED
pass_count = rd.set_interlace_handling();
#else
throw error("Cannot read interlaced image: interlace handling disabled.");
#endif
}
else
{
pass_count = 1;
}
rd.update_info();
if (rd.get_color_type() != traits::get_color_type()
|| rd.get_bit_depth() != traits::get_bit_depth())
{
throw std::logic_error("color type and/or bit depth mismatch"
" in png::consumer::read()");
}
this->get_info() = rd.get_image_info();
pixcon* pixel_con = static_cast< pixcon* >(this);
if (pass_count > 1 && !interlacing_supported)
{
skip_interlaced_rows(rd, pass_count);
pass_count = 1;
}
read_rows(rd, pass_count, pixel_con);
rd.read_end_info();
}
protected:
typedef streaming_base< pixel, info_holder > base;
/**
* \brief Constructs a consumer object using passed image_info
* object to store image information.
*/
explicit consumer(image_info& info)
: base(info)
{
}
private:
template< typename istream >
void skip_interlaced_rows(reader< istream >& rd, size_t pass_count)
{
typedef std::vector< pixel > row;
typedef row_traits< row > row_traits_type;
row dummy_row(this->get_info().get_width());
for (size_t pass = 1; pass < pass_count; ++pass)
{
rd.read_row(reinterpret_cast< byte* >
(row_traits_type::get_data(dummy_row)));
}
}
template< typename istream >
void read_rows(reader< istream >& rd, size_t pass_count,
pixcon* pixel_con)
{
for (size_t pass = 0; pass < pass_count; ++pass)
{
pixel_con->reset(pass);
for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
{
rd.read_row(pixel_con->get_next_row(pos));
}
}
}
};
} // namespace png
#endif // PNGPP_CONSUMER_HPP_INCLUDED

View File

@@ -0,0 +1,352 @@
/*
* 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
{
/**
* \brief IO transformation class template. Converts %image %color
* space.
*/
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 = int(io.get_color_type()) & int(color_mask_alpha);
bool src_tRNS = io.has_chunk(chunk_tRNS);
bool dst_alpha = int(traits::get_color_type()) & int(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 = int(traits::get_color_type()) & int(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
/**
* \brief IO transformation class template. Converts %image %color
* space.
*
* This IO transformation class template is used to convert %color
* space of the source %image to the %color space of the target
* %image. An error with human-readable description is thrown
* when the %color space could not be converted. Often, this
* means that you have to recompile libpng with some more
* conversion options turned on.
*
* Not implemented--see specializations.
*
* \see image, image::read
*/
template< typename pixel >
struct convert_color_space
{
};
/**
* \brief Converts %image %color space. A specialization for
* rgb_pixel type.
*/
template<>
struct convert_color_space< rgb_pixel >
: detail::convert_color_space_impl< rgb_pixel >
{
};
/**
* \brief Converts %image %color space. A specialization for
* rgb_pixel_16 type.
*/
template<>
struct convert_color_space< rgb_pixel_16 >
: detail::convert_color_space_impl< rgb_pixel_16 >
{
};
/**
* \brief Converts %image %color space. A specialization for
* rgba_pixel type.
*/
template<>
struct convert_color_space< rgba_pixel >
: detail::convert_color_space_impl< rgba_pixel >
{
};
/**
* \brief Converts %image %color space. A specialization for
* rgba_pixel_16 type.
*/
template<>
struct convert_color_space< rgba_pixel_16 >
: detail::convert_color_space_impl< rgba_pixel_16 >
{
};
/**
* \brief Converts %image %color space. A specialization for
* gray_pixel type.
*/
template<>
struct convert_color_space< gray_pixel >
: detail::convert_color_space_impl< gray_pixel >
{
};
/**
* \brief Converts %image %color space. A specialization for
* gray_pixel_16 type.
*/
template<>
struct convert_color_space< gray_pixel_16 >
: detail::convert_color_space_impl< gray_pixel_16 >
{
};
/**
* \brief Converts %image %color space. A specialization for
* ga_pixel type.
*/
template<>
struct convert_color_space< ga_pixel >
: detail::convert_color_space_impl< ga_pixel >
{
};
/**
* \brief Converts %image %color space. A specialization for
* ga_pixel_16 type.
*/
template<>
struct convert_color_space< ga_pixel_16 >
: detail::convert_color_space_impl< ga_pixel_16 >
{
};
/**
* \brief Converts %image %color space. A specialization for
* index_pixel type.
*/
template<>
struct convert_color_space< index_pixel >
: detail::convert_color_space_impl< index_pixel >
{
};
} // namespace png
#endif // PNGPP_CONVERT_COLOR_SPACE_HPP_INCLUDED

View File

@@ -0,0 +1,74 @@
/*
* 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_END_INFO_HPP_INCLUDED
#define PNGPP_END_INFO_HPP_INCLUDED
#include "info_base.hpp"
namespace png
{
/**
* \brief Internal class to hold PNG ending %info.
*
* \see info, info_base
*/
class end_info
: public info_base
{
public:
end_info(io_base& io, png_struct* png)
: info_base(io, png)
{
}
void destroy()
{
assert(m_info);
png_destroy_info_struct(m_png, & m_info);
}
void read()
{
png_read_end(m_png, m_info);
}
void write() const
{
png_write_end(m_png, m_info);
}
// TODO: add methods to read/write text comments etc.
};
} // namespace png
#endif // PNGPP_END_INFO_HPP_INCLUDED

122
Libs/png++/png++/error.hpp Normal file
View File

@@ -0,0 +1,122 @@
/*
* 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_ERROR_HPP_INCLUDED
#define PNGPP_ERROR_HPP_INCLUDED
/* check if we have strerror_s or strerror_r, prefer the former which is C11 std */
#ifdef __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#define HAVE_STRERROR_S 1
#else
#undef HAVE_STRERROR_S
#endif
#include <string>
#include <stdexcept>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
namespace png
{
/**
* \brief Exception class to represent runtime errors related to
* png++ operation.
*/
class error
: public std::runtime_error
{
public:
/**
* \param message error description
*/
explicit error(std::string const& message)
: std::runtime_error(message)
{
}
};
/**
* \brief Exception class to represent standard library errors
* (generally IO).
*
* \see reader, writer
*/
class std_error
: public std::runtime_error
{
public:
/**
* Constructs an std_error object. The \a message string is
* appended with <tt>": "</tt> and the error description as
* returned by \c strerror(\a error).
*
* \param message error description
* \param error error number
*/
explicit std_error(std::string const& message, int errnum = errno)
: std::runtime_error((message + ": ") + thread_safe_strerror(errnum))
{
}
protected:
static std::string thread_safe_strerror(int errnum)
{
#define ERRBUF_SIZE 512
char buf[ERRBUF_SIZE] = { 0 };
#ifdef HAVE_STRERROR_S
strerror_s(buf, ERRBUF_SIZE, errnum);
return std::string(buf);
#else
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
strerror_r(errnum, buf, ERRBUF_SIZE);
return std::string(buf);
#else
/* GNU variant can return a pointer to static buffer instead of buf */
//return std::string(strerror_r(errnum, buf, ERRBUF_SIZE));
return std::string("ALT_0xcb89df87bc3a85639dd892798a9375d");
#endif
#endif
#undef ERRBUF_SIZE
}
};
} // namespace png
#endif // PNGPP_ERROR_HPP_INCLUDED

View File

@@ -0,0 +1,86 @@
/*
* 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_GA_PIXEL_HPP_INCLUDED
#define PNGPP_GA_PIXEL_HPP_INCLUDED
#include <limits>
#include "types.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief Basic Gray+Alpha pixel type.
*/
template< typename T >
struct basic_ga_pixel
{
typedef pixel_traits< basic_ga_pixel< T > > traits;
/**
* \brief Constructs basic_ga_pixel object from \a value and
* \a alpha components passed as parameters. Alpha defaults
* to full opacity.
*/
basic_ga_pixel(T value = 0, T alpha = traits::get_alpha_filler())
: value(value), alpha(alpha)
{
}
T value;
T alpha;
};
/**
* \brief The 8-bit Gray+Alpha pixel type.
*/
typedef basic_ga_pixel< byte > ga_pixel;
/**
* \brief The 16-bit Gray+Alpha pixel type.
*/
typedef basic_ga_pixel< uint_16 > ga_pixel_16;
/**
* \brief Pixel traits specialization for basic_ga_pixel.
*/
template< typename T >
struct pixel_traits< basic_ga_pixel< T > >
: basic_pixel_traits< basic_ga_pixel< T >, T, color_type_ga >,
basic_alpha_pixel_traits< T >
{
};
} // namespace png
#endif // PNGPP_GA_PIXEL_HPP_INCLUDED

View File

@@ -0,0 +1,204 @@
/*
* 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_GENERATOR_HPP_INCLUDED
#define PNGPP_GENERATOR_HPP_INCLUDED
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <ostream>
#include "config.hpp"
#include "error.hpp"
#include "streaming_base.hpp"
#include "writer.hpp"
namespace png
{
/**
* \brief Pixel generator class template.
*
* Used as a base class for custom pixel generator classes as well
* as inside image class implementation to write pixels from the
* pixel buffer.
*
* A usage example can be found in \c example/pixel_generator.cpp.
*
* Encapsulates PNG %image writing procedure. In order to create
* a custom pixel %generator use CRTP trick:
*
* \code
* class pixel_generator
* : public png::generator< pixel, pixel_generator >
* {
* ...
* };
* \endcode
*
* Your pixel %generator class should implement \c get_next_row()
* method and \c reset() method (optional). Their signatures are
* as follows:
*
* \code
* png::byte* get_next_row(png::uint_32 pos);
* void reset(size_t pass);
* \endcode
*
* The \c get_next_row() method is called every time a new row of
* %image data is needed by the writer. The position of the row
* being written is passed as \c pos parameter. The \c pos takes
* values from \c 0 to \c <image_height>-1 inclusively. The
* method should return the starting address of a row buffer
* storing an appropriate amount of pixels (i.e. the width of the
* %image being written). The address should be casted to
* png::byte* pointer type using \c reinterpret_cast<> or a
* C-style cast.
*
* The optional \c reset() method is called every time the new
* pass of interlaced %image processing starts. The number of
* interlace pass is avaiable as the only parameter of the method.
* For non-interlaced images the method is called once prior to
* any calls to \c get_next_row(). The value of \c 0 is passed
* for the \c pass number. You do not have to implement this
* method unless you are going to support interlaced %image
* generation.
*
* An optional template parameter \c info_holder encapsulated
* image_info storage policy. Please refer to consumer class
* documentation for the detailed description of this parameter.
*
* An optional \c bool template parameter \c interlacing_supported
* specifies whether writing interlacing images is supported by
* your %generator class. It defaults to \c false. An attempt to
* write an interlaced %image will result in throwing
* \c std::logic_error.
*
* In order to fully support interlacing specify \c true for \c
* interlacing_supported parameter and implement \c reset()
* method. You _must_ generate the same pixels for every pass to
* get the correct PNG %image output.
*
* \see image, consumer
*/
template< typename pixel,
class pixgen,
class info_holder = def_image_info_holder,
bool interlacing_supported = false >
class generator
: public streaming_base< pixel, info_holder >
{
public:
/**
* \brief Writes an image to the stream.
*
* Essentially, this method constructs a writer object and
* instructs it to write the image to the stream. It handles
* writing interlaced images as long as your generator class
* supports this.
*/
template< typename ostream >
void write(ostream& stream)
{
writer< ostream > wr(stream);
wr.set_image_info(this->get_info());
wr.write_info();
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (pixel_traits< pixel >::get_bit_depth() == 16)
{
#ifdef PNG_WRITE_SWAP_SUPPORTED
wr.set_swap();
#else
throw error("Cannot write 16-bit image: recompile with PNG_WRITE_SWAP_SUPPORTED.");
#endif
}
#endif
size_t pass_count;
if (this->get_info().get_interlace_type() != interlace_none)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
if (interlacing_supported)
{
pass_count = wr.set_interlace_handling();
}
else
{
throw std::logic_error("Cannot write interlaced image: generator does not support it.");
}
#else
throw error("Cannot write interlaced image: interlace handling disabled.");
#endif
}
else
{
pass_count = 1;
}
pixgen* pixel_gen = static_cast< pixgen* >(this);
for (size_t pass = 0; pass < pass_count; ++pass)
{
pixel_gen->reset(pass);
for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
{
wr.write_row(pixel_gen->get_next_row(pos));
}
}
wr.write_end_info();
}
protected:
typedef streaming_base< pixel, info_holder > base;
/**
* \brief Constructs a generator object using passed image_info
* object to store image information.
*/
explicit generator(image_info& info)
: base(info)
{
}
/**
* \brief Constructs a generator object prepared to generate
* an image of specified width and height.
*/
generator(size_t width, size_t height)
: base(width, height)
{
}
};
} // namespace png
#endif // PNGPP_GENERATOR_HPP_INCLUDED

View File

@@ -0,0 +1,111 @@
/*
* 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_GRAY_PIXEL_HPP_INCLUDED
#define PNGPP_GRAY_PIXEL_HPP_INCLUDED
#include "types.hpp"
#include "packed_pixel.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief The 8-bit Grayscale pixel type.
*/
typedef byte gray_pixel;
/**
* \brief The 16-bit Grayscale pixel type.
*/
typedef uint_16 gray_pixel_16;
/**
* \brief The packed gray pixel class template. The available
* specializations are for 1-, 2- and 4-bit pixels.
*/
template< int bits >
class packed_gray_pixel
: public packed_pixel< bits >
{
public:
packed_gray_pixel(byte value = 0)
: packed_pixel< bits >(value)
{
}
};
/**
* \brief The 1-bit Grayscale pixel type.
*/
typedef packed_gray_pixel< 1 > gray_pixel_1;
/**
* \brief The 2-bit Grayscale pixel type.
*/
typedef packed_gray_pixel< 2 > gray_pixel_2;
/**
* \brief The 4-bit Grayscale pixel type.
*/
typedef packed_gray_pixel< 4 > gray_pixel_4;
/**
* \brief Pixel traits specialization for gray_pixel.
*/
template<>
struct pixel_traits< gray_pixel >
: basic_pixel_traits< gray_pixel, byte, color_type_gray >
{
};
/**
* \brief Pixel traits specialization for gray_pixel_16.
*/
template<>
struct pixel_traits< gray_pixel_16 >
: basic_pixel_traits< gray_pixel_16, uint_16, color_type_gray >
{
};
/**
* \brief Pixel traits specialization for packed_gray_pixel.
*/
template< int bits >
struct pixel_traits< packed_gray_pixel< bits > >
: basic_pixel_traits< packed_gray_pixel< bits >, byte,
color_type_gray, /* channels = */ 1, bits >
{
};
} // namespace png
#endif // PNGPP_GRAY_PIXEL_HPP_INCLUDED

550
Libs/png++/png++/image.hpp Normal file
View File

@@ -0,0 +1,550 @@
/*
* 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_IMAGE_HPP_INCLUDED
#define PNGPP_IMAGE_HPP_INCLUDED
#include <fstream>
#include "pixel_buffer.hpp"
#include "generator.hpp"
#include "consumer.hpp"
#include "convert_color_space.hpp"
namespace png
{
/**
* \brief Class template to represent PNG image.
*
* The image consists of pixel data as well as additional %image
* %info like interlace type, compression method, palette (for
* colormap-based images) etc. Provides methods to read and write
* images from/to a generic stream and to manipulate %image pixels.
*
* The default pixel_buffer stores pixels in a vector of vectors, which
* is good for openning, editing or converting an image to any
* pixel type.
* But for simple and fast image unpacking to one memory chunk this approch
* is unacceptable, because it leads to multiple memory allocations, the
* unpacked image is spread across the memory and client code needs to
* gather it manualy. solid_pixel_buffer solves this problem, but with
* restriction: pixels with fractional number of bytes per channel are
* not allowed (see solid_pixel_buffer.hpp for details).
*/
template< typename pixel, typename pixel_buffer_type = pixel_buffer< pixel > >
class image
{
public:
/**
* \brief The pixel traits type for \c pixel.
*/
typedef pixel_traits< pixel > traits;
/**
* \brief The pixel buffer type for \c pixel.
*/
typedef pixel_buffer_type pixbuf;
/**
* \brief Represents a row of image pixel data.
*/
typedef typename pixbuf::row_type row_type;
typedef typename pixbuf::row_access row_access;
typedef typename pixbuf::row_const_access row_const_access;
/**
* \brief A transformation functor to convert any image to
* appropriate color space.
*/
typedef convert_color_space< pixel > transform_convert;
/**
* \brief The default io transformation: does nothing.
*/
struct transform_identity
{
void operator()(io_base&) const {}
};
/**
* \brief Constructs an empty image.
*/
image()
: m_info(make_image_info< pixel >())
{
}
/**
* \brief Constructs an empty image of specified width and height.
*/
image(uint_32 width, uint_32 height)
: m_info(make_image_info< pixel >())
{
resize(width, height);
}
/**
* \brief Constructs an image reading data from specified file
* using default converting transform.
*/
explicit image(std::string const& filename)
{
read(filename, transform_convert());
}
/**
* \brief Constructs an image reading data from specified file
* using custom transformaton.
*/
template< class transformation >
image(std::string const& filename,
transformation const& transform)
{
read(filename.c_str(), transform);
}
/**
* \brief Constructs an image reading data from specified file
* using default converting transform.
*/
explicit image(char const* filename)
{
read(filename, transform_convert());
}
/**
* \brief Constructs an image reading data from specified file
* using custom transformaton.
*/
template< class transformation >
image(char const* filename, transformation const& transform)
{
read(filename, transform);
}
/**
* \brief Constructs an image reading data from a stream using
* default converting transform.
*/
explicit image(std::istream& stream)
{
read_stream(stream, transform_convert());
}
/**
* \brief Constructs an image reading data from a stream using
* custom transformation.
*/
template< class transformation >
image(std::istream& stream, transformation const& transform)
{
read_stream(stream, transform);
}
/**
* \brief Reads an image from specified file using default
* converting transform.
*/
void read(std::string const& filename)
{
read(filename, transform_convert());
}
/**
* \brief Reads an image from specified file using custom
* transformaton.
*/
template< class transformation >
void read(std::string const& filename, transformation const& transform)
{
read(filename.c_str(), transform);
}
/**
* \brief Reads an image from specified file using default
* converting transform.
*/
void read(char const* filename)
{
read(filename, transform_convert());
}
/**
* \brief Reads an image from specified file using custom
* transformaton.
*/
template< class transformation >
void read(char const* filename, transformation const& transform)
{
std::ifstream stream(filename, std::ios::binary);
if (!stream.is_open())
{
throw std_error(filename);
}
stream.exceptions(std::ios::badbit);
read_stream(stream, transform);
}
/**
* \brief Reads an image from a stream using default
* converting transform.
*/
void read(std::istream& stream)
{
read_stream(stream, transform_convert());
}
/**
* \brief Reads an image from a stream using custom
* transformation.
*/
template< class transformation >
void read(std::istream& stream, transformation const& transform)
{
read_stream(stream, transform);
}
/**
* \brief Reads an image from a stream using default
* converting transform.
*/
template< class istream >
void read_stream(istream& stream)
{
read_stream(stream, transform_convert());
}
/**
* \brief Reads an image from a stream using custom
* transformation.
*/
template< class istream, class transformation >
void read_stream(istream& stream, transformation const& transform)
{
pixel_consumer pixcon(m_info, m_pixbuf);
pixcon.read(stream, transform);
}
/**
* \brief Writes an image to specified file.
*/
void write(std::string const& filename)
{
write(filename.c_str());
}
/**
* \brief Writes an image to specified file.
*/
void write(char const* filename)
{
std::ofstream stream(filename, std::ios::binary);
if (!stream.is_open())
{
throw std_error(filename);
}
stream.exceptions(std::ios::badbit);
write_stream(stream);
}
/**
* \brief Writes an image to a stream.
*/
template< class ostream >
void write_stream(ostream& stream)
{
pixel_generator pixgen(m_info, m_pixbuf);
pixgen.write(stream);
}
/**
* \brief Returns a reference to image pixel buffer.
*/
pixbuf& get_pixbuf()
{
return m_pixbuf;
}
/**
* \brief Returns a const reference to image pixel buffer.
*/
pixbuf const& get_pixbuf() const
{
return m_pixbuf;
}
/**
* \brief Replaces the image pixel buffer.
*
* \param buffer a pixel buffer object to take a copy from
*/
void set_pixbuf(pixbuf const& buffer)
{
m_pixbuf = buffer;
}
uint_32 get_width() const
{
return m_pixbuf.get_width();
}
uint_32 get_height() const
{
return m_pixbuf.get_height();
}
/**
* \brief Resizes the image pixel buffer.
*/
void resize(uint_32 width, uint_32 height)
{
m_pixbuf.resize(width, height);
m_info.set_width(width);
m_info.set_height(height);
}
/**
* \brief Returns a reference to the row of image data at
* specified index.
*
* \see pixel_buffer::get_row()
*/
row_access get_row(size_t index)
{
return m_pixbuf.get_row(index);
}
/**
* \brief Returns a const reference to the row of image data at
* specified index.
*
* \see pixel_buffer::get_row()
*/
row_const_access get_row(size_t index) const
{
return m_pixbuf.get_row(index);
}
/**
* \brief The non-checking version of get_row() method.
*/
row_access operator[](size_t index)
{
return m_pixbuf[index];
}
/**
* \brief The non-checking version of get_row() method.
*/
row_const_access operator[](size_t index) const
{
return m_pixbuf[index];
}
/**
* \brief Returns a pixel at (x,y) position.
*/
pixel get_pixel(size_t x, size_t y) const
{
return m_pixbuf.get_pixel(x, y);
}
/**
* \brief Replaces a pixel at (x,y) position.
*/
void set_pixel(size_t x, size_t y, pixel p)
{
m_pixbuf.set_pixel(x, y, p);
}
interlace_type get_interlace_type() const
{
return m_info.get_interlace_type();
}
void set_interlace_type(interlace_type interlace)
{
m_info.set_interlace_type(interlace);
}
compression_type get_compression_type() const
{
return m_info.get_compression_type();
}
void set_compression_type(compression_type compression)
{
m_info.set_compression_type(compression);
}
filter_type get_filter_type() const
{
return m_info.get_filter_type();
}
void set_filter_type(filter_type filter)
{
m_info.set_filter_type(filter);
}
/**
* \brief Returns a reference to the image palette.
*/
palette& get_palette()
{
return m_info.get_palette();
}
/**
* \brief Returns a const reference to the image palette.
*/
palette const& get_palette() const
{
return m_info.get_palette();
}
/**
* \brief Replaces the image palette.
*/
void set_palette(palette const& plte)
{
m_info.set_palette(plte);
}
tRNS const& get_tRNS() const
{
return m_info.get_tRNS();
}
tRNS& get_tRNS()
{
return m_info.get_tRNS();
}
void set_tRNS(tRNS const& trns)
{
m_info.set_tRNS(trns);
}
double get_gamma() const
{
return m_info.get_gamma();
}
void set_gamma(double gamma)
{
m_info.set_gamma(gamma);
}
protected:
/**
* \brief A common base class template for pixel_consumer and
* pixel_generator classes.
*/
template< typename base_impl >
class streaming_impl
: public base_impl
{
public:
streaming_impl(image_info& info, pixbuf& pixels)
: base_impl(info),
m_pixbuf(pixels)
{
}
/**
* \brief Returns the starting address of a \c pos-th row
* in the image's pixel buffer.
*/
byte* get_next_row(size_t pos)
{
typedef typename pixbuf::row_traits row_traits;
return reinterpret_cast< byte* >
(row_traits::get_data(m_pixbuf.get_row(pos)));
}
protected:
pixbuf& m_pixbuf;
};
/**
* \brief The pixel buffer adapter for reading pixel data.
*/
class pixel_consumer
: public streaming_impl< consumer< pixel,
pixel_consumer,
image_info_ref_holder,
/* interlacing = */ true > >
{
public:
pixel_consumer(image_info& info, pixbuf& pixels)
: streaming_impl< consumer< pixel,
pixel_consumer,
image_info_ref_holder,
true > >(info, pixels)
{
}
void reset(size_t pass)
{
if (pass == 0)
{
this->m_pixbuf.resize(this->get_info().get_width(),
this->get_info().get_height());
}
}
};
/**
* \brief The pixel buffer adapter for writing pixel data.
*/
class pixel_generator
: public streaming_impl< generator< pixel,
pixel_generator,
image_info_ref_holder,
/* interlacing = */ true > >
{
public:
pixel_generator(image_info& info, pixbuf& pixels)
: streaming_impl< generator< pixel,
pixel_generator,
image_info_ref_holder,
true > >(info, pixels)
{
}
};
image_info m_info;
pixbuf m_pixbuf;
};
} // namespace png
#endif // PNGPP_IMAGE_HPP_INCLUDED

View File

@@ -0,0 +1,215 @@
/*
* 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_IMAGE_INFO_HPP_INCLUDED
#define PNGPP_IMAGE_INFO_HPP_INCLUDED
#include "types.hpp"
#include "palette.hpp"
#include "tRNS.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief Holds information about PNG image.
*
* \see image, generator, consumer
*/
class image_info
{
public:
/**
* \brief Constructs the image_info object with default values
* for color_type, interlace_type, compression_method and
* filter_type.
*/
image_info()
: m_width(0),
m_height(0),
m_bit_depth(0),
m_color_type(color_type_none),
m_interlace_type(interlace_none),
m_compression_type(compression_type_default),
m_filter_type(filter_type_default),
m_gamma(0.0)
{
}
uint_32 get_width() const
{
return m_width;
}
void set_width(uint_32 width)
{
m_width = width;
}
uint_32 get_height() const
{
return m_height;
}
void set_height(uint_32 height)
{
m_height = height;
}
color_type get_color_type() const
{
return m_color_type;
}
void set_color_type(color_type color_space)
{
m_color_type = color_space;
}
int get_bit_depth() const
{
return m_bit_depth;
}
void set_bit_depth(int bit_depth)
{
m_bit_depth = bit_depth;
}
interlace_type get_interlace_type() const
{
return m_interlace_type;
}
void set_interlace_type(interlace_type interlace)
{
m_interlace_type = interlace;
}
compression_type get_compression_type() const
{
return m_compression_type;
}
void set_compression_type(compression_type compression)
{
m_compression_type = compression;
}
filter_type get_filter_type() const
{
return m_filter_type;
}
void set_filter_type(filter_type filter)
{
m_filter_type = filter;
}
palette const& get_palette() const
{
return m_palette;
}
palette& get_palette()
{
return m_palette;
}
void set_palette(palette const& plte)
{
m_palette = plte;
}
/**
* \brief Removes all entries from the palette.
*/
void drop_palette()
{
m_palette.clear();
}
tRNS const& get_tRNS() const
{
return m_tRNS;
}
tRNS& get_tRNS()
{
return m_tRNS;
}
void set_tRNS(tRNS const& trns)
{
m_tRNS = trns;
}
double get_gamma() const
{
return m_gamma;
}
void set_gamma(double gamma)
{
m_gamma = gamma;
}
protected:
uint_32 m_width;
uint_32 m_height;
int m_bit_depth;
color_type m_color_type;
interlace_type m_interlace_type;
compression_type m_compression_type;
filter_type m_filter_type;
palette m_palette;
tRNS m_tRNS;
double m_gamma;
};
/**
* \brief Returns an image_info object with color_type and
* bit_depth fields setup appropriate for the \c pixel type.
*/
template< typename pixel >
image_info
make_image_info()
{
typedef pixel_traits< pixel > traits;
image_info info;
info.set_color_type(traits::get_color_type());
info.set_bit_depth(traits::get_bit_depth());
return info;
}
} // namespace png
#endif // PNGPP_IMAGE_INFO_HPP_INCLUDED

View File

@@ -0,0 +1,112 @@
/*
* 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_INDEX_PIXEL_HPP_INCLUDED
#define PNGPP_INDEX_PIXEL_HPP_INCLUDED
#include "types.hpp"
#include "packed_pixel.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief The 8-bit Indexed (colormap) pixel type.
*/
class index_pixel
{
public:
index_pixel(byte index = 0)
: m_index(index)
{
}
operator byte() const
{
return m_index;
}
private:
byte m_index;
};
/**
* \brief The packed indexed pixel class template. The available
* specializations are for 1-, 2- and 4-bit pixels.
*/
template< int bits >
class packed_index_pixel
: public packed_pixel< bits >
{
public:
packed_index_pixel(byte value = 0)
: packed_pixel< bits >(value)
{
}
};
/**
* \brief The 1-bit Indexed pixel type.
*/
typedef packed_index_pixel< 1 > index_pixel_1;
/**
* \brief The 1-bit Indexed pixel type.
*/
typedef packed_index_pixel< 2 > index_pixel_2;
/**
* \brief The 1-bit Indexed pixel type.
*/
typedef packed_index_pixel< 4 > index_pixel_4;
/**
* \brief Pixel traits specialization for index_pixel.
*/
template<>
struct pixel_traits< index_pixel >
: basic_pixel_traits< index_pixel, byte, color_type_palette >
{
};
/**
* \brief Pixel traits specialization for packed_index_pixel.
*/
template< int bits >
struct pixel_traits< packed_index_pixel< bits > >
: basic_pixel_traits< packed_index_pixel< bits >, byte,
color_type_palette, /* channels = */ 1, bits >
{
};
} // namespace png
#endif // PNGPP_INDEX_PIXEL_HPP_INCLUDED

186
Libs/png++/png++/info.hpp Normal file
View File

@@ -0,0 +1,186 @@
/*
* 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_INFO_HPP_INCLUDED
#define PNGPP_INFO_HPP_INCLUDED
#include <cassert>
#include "info_base.hpp"
#include "image_info.hpp"
namespace png
{
/**
* \brief Holds information about PNG image. Adapter class for IO
* image operations.
*/
class info
: public info_base,
public image_info
{
public:
info(io_base& io, png_struct* png)
: info_base(io, png)
{
}
void read()
{
assert(m_png);
assert(m_info);
png_read_info(m_png, m_info);
png_get_IHDR(m_png,
m_info,
& m_width,
& m_height,
reinterpret_cast< int* >(& m_bit_depth),
reinterpret_cast< int* >(& m_color_type),
reinterpret_cast< int* >(& m_interlace_type),
reinterpret_cast< int* >(& m_compression_type),
reinterpret_cast< int* >(& m_filter_type));
if (png_get_valid(m_png, m_info, chunk_PLTE) == chunk_PLTE)
{
png_color* colors = 0;
int count = 0;
png_get_PLTE(m_png, m_info, & colors, & count);
m_palette.assign(colors, colors + count);
}
#ifdef PNG_tRNS_SUPPORTED
if (png_get_valid(m_png, m_info, chunk_tRNS) == chunk_tRNS)
{
if (m_color_type == color_type_palette)
{
int count;
byte* values;
if (png_get_tRNS(m_png, m_info, & values, & count, NULL)
!= PNG_INFO_tRNS)
{
throw error("png_get_tRNS() failed");
}
m_tRNS.assign(values, values + count);
}
}
#endif
#ifdef PNG_gAMA_SUPPORTED
if (png_get_valid(m_png, m_info, chunk_gAMA) == chunk_gAMA)
{
#ifdef PNG_FLOATING_POINT_SUPPORTED
if (png_get_gAMA(m_png, m_info, &m_gamma) != PNG_INFO_gAMA)
{
throw error("png_get_gAMA() failed");
}
#else
png_fixed_point gamma = 0;
if (png_get_gAMA_fixed(m_png, m_info, &gamma) != PNG_INFO_gAMA)
{
throw error("png_get_gAMA_fixed() failed");
}
m_gamma = gamma / 100000.0;
#endif
}
#endif
}
void write() const
{
assert(m_png);
assert(m_info);
sync_ihdr();
if (m_color_type == color_type_palette)
{
if (! m_palette.empty())
{
png_set_PLTE(m_png, m_info,
const_cast< color* >(& m_palette[0]),
(int) m_palette.size());
}
if (! m_tRNS.empty())
{
#ifdef PNG_tRNS_SUPPORTED
png_set_tRNS(m_png, m_info,
const_cast< byte* >(& m_tRNS[0]),
m_tRNS.size(),
NULL);
#else
throw error("attempted to write tRNS chunk; recompile with PNG_tRNS_SUPPORTED");
#endif
}
}
if (m_gamma > 0)
{
#ifdef PNG_gAMA_SUPPORTED
#ifdef PNG_FLOATING_POINT_SUPPORTED
png_set_gAMA(m_png, m_info, m_gamma);
#else
png_set_gAMA_fixed(m_png, m_info,
(png_fixed_point)(m_gamma * 100000));
#endif
#else
throw error("attempted to write gAMA chunk; recompile with PNG_gAMA_SUPPORTED");
#endif
}
png_write_info(m_png, m_info);
}
void update()
{
assert(m_png);
assert(m_info);
sync_ihdr();
png_read_update_info(m_png, m_info);
}
protected:
void sync_ihdr(void) const
{
png_set_IHDR(m_png,
m_info,
m_width,
m_height,
m_bit_depth,
m_color_type,
m_interlace_type,
m_compression_type,
m_filter_type);
}
};
} // namespace png
#endif // PNGPP_INFO_HPP_INCLUDED

View File

@@ -0,0 +1,77 @@
/*
* 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_INFO_BASE_HPP_INCLUDED
#define PNGPP_INFO_BASE_HPP_INCLUDED
#include <cassert>
#include "error.hpp"
#include "types.hpp"
namespace png
{
class io_base;
/**
* \brief Internal class to hold PNG info or end_info.
*/
class info_base
{
info_base(info_base const&);
info_base& operator=(info_base const&);
public:
info_base(io_base& io, png_struct* png)
: m_io(io),
m_png(png),
m_info(png_create_info_struct(m_png))
{
}
png_info* get_png_info() const
{
return m_info;
}
png_info** get_png_info_ptr()
{
return & m_info;
}
protected:
io_base& m_io;
png_struct* m_png;
png_info* m_info;
};
} // namespace png
#endif // PNGPP_INFO_BASE_HPP_INCLUDED

View File

@@ -0,0 +1,467 @@
/*
* 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_IO_BASE_HPP_INCLUDED
#define PNGPP_IO_BASE_HPP_INCLUDED
#include <cassert>
#include <cstdio>
#include <cstdarg>
#include "error.hpp"
#include "info.hpp"
#include "end_info.hpp"
static void
trace_io_transform(char const* fmt, ...)
{
#ifdef DEBUG_IO_TRANSFORM
va_list va;
va_start(va, fmt);
fprintf(stderr, "TRANSFORM_IO: ");
vfprintf(stderr, fmt, va);
va_end(va);
#endif
}
#define TRACE_IO_TRANSFORM trace_io_transform
namespace png
{
/**
* \brief Base class for PNG reader/writer classes.
*
* \see reader, writer
*/
class io_base
{
io_base(io_base const&);
io_base& operator=(io_base const&);
public:
explicit io_base(png_struct* png)
: m_png(png),
m_info(*this, m_png),
m_end_info(*this, m_png)
{
}
~io_base()
{
assert(! m_png);
assert(! m_info.get_png_info());
assert(! m_end_info.get_png_info());
}
png_struct* get_png_struct() const
{
return m_png;
}
info& get_info()
{
return m_info;
}
info const& get_info() const
{
return m_info;
}
image_info const& get_image_info() const
{
return m_info;
}
void set_image_info(image_info const& info)
{
static_cast< image_info& >(m_info) = info; // slice it
}
end_info& get_end_info()
{
return m_end_info;
}
end_info const& get_end_info() const
{
return m_end_info;
}
//////////////////////////////////////////////////////////////////////
// info accessors
//
uint_32 get_width() const
{
return m_info.get_width();
}
void set_width(uint_32 width)
{
m_info.set_width(width);
}
uint_32 get_height() const
{
return m_info.get_height();
}
void set_height(uint_32 height)
{
m_info.set_height(height);
}
color_type get_color_type() const
{
return m_info.get_color_type();
}
void set_color_type(color_type color_space)
{
m_info.set_color_type(color_space);
}
int get_bit_depth() const
{
return m_info.get_bit_depth();
}
void set_bit_depth(int bit_depth)
{
m_info.set_bit_depth(bit_depth);
}
interlace_type get_interlace_type() const
{
return m_info.get_interlace_type();
}
void set_interlace_type(interlace_type interlace)
{
m_info.set_interlace_type(interlace);
}
compression_type get_compression_type() const
{
return m_info.get_compression_type();
}
void set_compression_type(compression_type compression)
{
m_info.set_compression_type(compression);
}
filter_type get_filter_type() const
{
return m_info.get_filter_type();
}
void set_filter_type(filter_type filter)
{
m_info.set_filter_type(filter);
}
//////////////////////////////////////////////////////////////////////
bool has_chunk(chunk id)
{
return png_get_valid(m_png,
m_info.get_png_info(),
uint_32(id)) == uint_32(id);
}
#if defined(PNG_READ_EXPAND_SUPPORTED)
void set_gray_1_2_4_to_8() const
{
TRACE_IO_TRANSFORM("png_set_expand_gray_1_2_4_to_8\n");
png_set_expand_gray_1_2_4_to_8(m_png);
}
void set_palette_to_rgb() const
{
TRACE_IO_TRANSFORM("png_set_palette_to_rgb\n");
png_set_palette_to_rgb(m_png);
}
void set_tRNS_to_alpha() const
{
TRACE_IO_TRANSFORM("png_set_tRNS_to_alpha\n");
png_set_tRNS_to_alpha(m_png);
}
#endif // defined(PNG_READ_EXPAND_SUPPORTED)
#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
void set_bgr() const
{
TRACE_IO_TRANSFORM("png_set_bgr\n");
png_set_bgr(m_png);
}
#endif
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
void set_gray_to_rgb() const
{
TRACE_IO_TRANSFORM("png_set_gray_to_rgb\n");
png_set_gray_to_rgb(m_png);
}
#endif
#ifdef PNG_FLOATING_POINT_SUPPORTED
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
{
TRACE_IO_TRANSFORM("png_set_rgb_to_gray: error_action=%d,"
" red_weight=%lf, green_weight=%lf\n",
error_action, red_weight, green_weight);
png_set_rgb_to_gray(m_png, error_action, red_weight, green_weight);
}
#else
void set_rgb_to_gray(rgb_to_gray_error_action error_action
= rgb_to_gray_silent,
fixed_point red_weight = -1,
fixed_point green_weight = -1) const
{
TRACE_IO_TRANSFORM("png_set_rgb_to_gray_fixed: error_action=%d,"
" red_weight=%d, green_weight=%d\n",
error_action, red_weight, green_weight);
png_set_rgb_to_gray_fixed(m_png, error_action,
red_weight, green_weight);
}
#endif // PNG_FLOATING_POINT_SUPPORTED
//////////////////////////////////////////////////////////////////////
// alpha channel transformations
//
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
void set_strip_alpha() const
{
TRACE_IO_TRANSFORM("png_set_strip_alpha\n");
png_set_strip_alpha(m_png);
}
#endif
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) \
|| defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
void set_swap_alpha() const
{
TRACE_IO_TRANSFORM("png_set_swap_alpha\n");
png_set_swap_alpha(m_png);
}
#endif
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) \
|| defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
void set_invert_alpha() const
{
TRACE_IO_TRANSFORM("png_set_invert_alpha\n");
png_set_invert_alpha(m_png);
}
#endif
#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
void set_filler(uint_32 filler, filler_type type) const
{
TRACE_IO_TRANSFORM("png_set_filler: filler=%08x, type=%d\n",
filler, type);
png_set_filler(m_png, filler, type);
}
#if !defined(PNG_1_0_X)
void set_add_alpha(uint_32 filler, filler_type type) const
{
TRACE_IO_TRANSFORM("png_set_add_alpha: filler=%08x, type=%d\n",
filler, type);
png_set_add_alpha(m_png, filler, type);
}
#endif
#endif // PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED
#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
void set_swap() const
{
TRACE_IO_TRANSFORM("png_set_swap\n");
png_set_swap(m_png);
}
#endif
#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
void set_packing() const
{
TRACE_IO_TRANSFORM("png_set_packing\n");
png_set_packing(m_png);
}
#endif
#if defined(PNG_READ_PACKSWAP_SUPPORTED) \
|| defined(PNG_WRITE_PACKSWAP_SUPPORTED)
void set_packswap() const
{
TRACE_IO_TRANSFORM("png_set_packswap\n");
png_set_packswap(m_png);
}
#endif
#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
void set_shift(byte red_bits, byte green_bits, byte blue_bits,
byte alpha_bits = 0) const
{
TRACE_IO_TRANSFORM("png_set_shift: red_bits=%d, green_bits=%d,"
" blue_bits=%d, alpha_bits=%d\n",
red_bits, green_bits, blue_bits, alpha_bits);
if (get_color_type() != color_type_rgb
|| get_color_type() != color_type_rgb_alpha)
{
throw error("set_shift: expected RGB or RGBA color type");
}
color_info bits;
bits.red = red_bits;
bits.green = green_bits;
bits.blue = blue_bits;
bits.alpha = alpha_bits;
png_set_shift(m_png, & bits);
}
void set_shift(byte gray_bits, byte alpha_bits = 0) const
{
TRACE_IO_TRANSFORM("png_set_shift: gray_bits=%d, alpha_bits=%d\n",
gray_bits, alpha_bits);
if (get_color_type() != color_type_gray
|| get_color_type() != color_type_gray_alpha)
{
throw error("set_shift: expected Gray or Gray+Alpha color type");
}
color_info bits;
bits.gray = gray_bits;
bits.alpha = alpha_bits;
png_set_shift(m_png, & bits);
}
#endif // PNG_READ_SHIFT_SUPPORTED || PNG_WRITE_SHIFT_SUPPORTED
#if defined(PNG_READ_INTERLACING_SUPPORTED) \
|| defined(PNG_WRITE_INTERLACING_SUPPORTED)
int set_interlace_handling() const
{
TRACE_IO_TRANSFORM("png_set_interlace_handling\n");
return png_set_interlace_handling(m_png);
}
#endif
#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
void set_invert_mono() const
{
TRACE_IO_TRANSFORM("png_set_invert_mono\n");
png_set_invert_mono(m_png);
}
#endif
#if defined(PNG_READ_16_TO_8_SUPPORTED)
void set_strip_16() const
{
TRACE_IO_TRANSFORM("png_set_strip_16\n");
png_set_strip_16(m_png);
}
#endif
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
void set_read_user_transform(png_user_transform_ptr transform_fn)
{
TRACE_IO_TRANSFORM("png_set_read_user_transform_fn\n");
png_set_read_user_transform_fn(m_png, transform_fn);
}
#endif
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) \
|| defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
void set_user_transform_info(void* info, int bit_depth, int channels)
{
TRACE_IO_TRANSFORM("png_set_user_transform_info: bit_depth=%d,"
" channels=%d\n", bit_depth, channels);
png_set_user_transform_info(m_png, info, bit_depth, channels);
}
#endif
protected:
void* get_io_ptr() const
{
return png_get_io_ptr(m_png);
}
void set_error(char const* message)
{
assert(message);
m_error = message;
}
void reset_error()
{
m_error.clear();
}
/*
std::string const& get_error() const
{
return m_error;
}
*/
bool is_error() const
{
return !m_error.empty();
}
void raise_error()
{
longjmp(png_jmpbuf(m_png), -1);
}
static void raise_error(png_struct* png, char const* message)
{
io_base* io = static_cast< io_base* >(png_get_error_ptr(png));
io->set_error(message);
io->raise_error();
}
png_struct* m_png;
info m_info;
end_info m_end_info;
std::string m_error;
};
} // namespace png
#endif // PNGPP_IO_BASE_HPP_INCLUDED

View File

@@ -0,0 +1,84 @@
/*
* 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_PACKED_PIXEL_HPP_INCLUDED
#define PNGPP_PACKED_PIXEL_HPP_INCLUDED
#include "types.hpp"
namespace png
{
namespace detail
{
template< int bits > class allowed_bit_depth;
template<> class allowed_bit_depth< 1 > {};
template<> class allowed_bit_depth< 2 > {};
template<> class allowed_bit_depth< 4 > {};
} // namespace detail
/**
* \brief The packed pixel class template.
*
* \see packed_gray_pixel, packed_index_pixel
*/
template< int bits >
class packed_pixel
: detail::allowed_bit_depth< bits >
{
public:
packed_pixel(byte value = 0)
: m_value(value & get_bit_mask())
{
}
operator byte() const
{
return m_value;
}
static int get_bit_depth()
{
return bits;
}
static byte get_bit_mask()
{
return (1 << bits) - 1;
}
private:
byte m_value;
};
} // namespace png
#endif // PNGPP_PACKED_PIXEL_HPP_INCLUDED

View File

@@ -0,0 +1,48 @@
/*
* 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_PALETTE_HPP_INCLUDED
#define PNGPP_PALETTE_HPP_INCLUDED
#include <vector>
#include "color.hpp"
namespace png
{
/**
* \brief The palette type. Currently implemented as \c std::vector
* of png::color.
*/
typedef std::vector< color > palette;
} // namespace png
#endif // PNGPP_PALETTE_HPP_INCLUDED

View File

@@ -0,0 +1,497 @@
/*
* 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_PIXEL_BUFFER_HPP_INCLUDED
#define PNGPP_PIXEL_BUFFER_HPP_INCLUDED
#include <cassert>
#include <cstddef>
#include <stdexcept>
#include <vector>
#include "packed_pixel.hpp"
#include "gray_pixel.hpp"
#include "index_pixel.hpp"
namespace png
{
/**
* \brief The pixel row traits class template. Provides a common
* way to get starting address of the row for packed and unpacked
* row types.
*
* Not implemented--see specializations.
*/
template< typename row > class row_traits;
/**
* \brief The basic class template to represent image pixel data.
*/
template< typename pixel,
typename row,
class traits = row_traits< row > >
class basic_pixel_buffer
{
public:
/**
* \brief A row of pixel data.
*/
typedef row row_type;
typedef row_type& row_access;
typedef row_type const& row_const_access;
typedef traits row_traits;
/**
* \brief Constructs an empty 0x0 pixel buffer object.
*/
basic_pixel_buffer()
: m_width(0),
m_height(0)
{
}
/**
* \brief Constructs an empty pixel buffer object.
*/
basic_pixel_buffer(uint_32 width, uint_32 height)
: m_width(0),
m_height(0)
{
resize(width, height);
}
uint_32 get_width() const
{
return m_width;
}
uint_32 get_height() const
{
return m_height;
}
/**
* \brief Resizes the pixel buffer.
*
* If new width or height is greater than the original,
* expanded pixels are filled with value of \a pixel().
*/
void resize(uint_32 width, uint_32 height)
{
m_width = width;
m_height = height;
m_rows.resize(height);
for (typename row_vec::iterator r = m_rows.begin();
r != m_rows.end();
++r)
{
r->resize(width);
}
}
/**
* \brief Returns a reference to the row of image data at
* specified index.
*
* Checks the index before returning a row: an instance of
* std::out_of_range is thrown if \c index is greater than \c
* height.
*/
row_access get_row(size_t index)
{
return m_rows.at(index);
}
/**
* \brief Returns a const reference to the row of image data at
* specified index.
*
* The checking version.
*/
row_const_access get_row(size_t index) const
{
return m_rows.at(index);
}
/**
* \brief The non-checking version of get_row() method.
*/
row_access operator[](size_t index)
{
return m_rows[index];
}
/**
* \brief The non-checking version of get_row() method.
*/
row_const_access operator[](size_t index) const
{
return m_rows[index];
}
/**
* \brief Replaces the row at specified index.
*/
void put_row(size_t index, row_type const& r)
{
assert(r.size() == m_width);
m_rows.at(index) = r;
}
/**
* \brief Returns a pixel at (x,y) position.
*/
pixel get_pixel(size_t x, size_t y) const
{
return get_row(y).at(x);
}
/**
* \brief Replaces a pixel at (x,y) position.
*/
void set_pixel(size_t x, size_t y, pixel p)
{
get_row(y).at(x) = p;
}
protected:
uint_32 m_width;
uint_32 m_height;
typedef std::vector< row_type > row_vec;
row_vec m_rows;
};
/**
* \brief The row_traits specialization for unpacked pixel rows.
*/
template< typename pixel >
class row_traits< std::vector< pixel > >
{
public:
/**
* \brief Returns the starting address of the row.
*/
static pixel* get_data(std::vector< pixel >& vec)
{
assert(vec.size());
return & vec[0];
}
};
/**
* The pixel_buffer specialization for unpacked pixels.
*/
template< typename pixel >
class pixel_buffer
: public basic_pixel_buffer< pixel, std::vector< pixel > >
{
public:
pixel_buffer()
{
}
pixel_buffer(uint_32 width, uint_32 height)
: basic_pixel_buffer< pixel, std::vector< pixel > >(width, height)
{
}
};
namespace detail
{
template< class pixel, typename reference >
class basic_packed_pixel_proxy
{
public:
explicit basic_packed_pixel_proxy(reference ref)
: m_ref(ref),
m_shift(0)
{
}
basic_packed_pixel_proxy(reference ref, size_t index)
: m_ref(ref),
m_shift(get_shift(index))
{
}
operator pixel() const
{
return pixel((m_ref >> m_shift) & pixel::get_bit_mask());
}
protected:
/*
* bits: . . .
* 1: 7 6 5 4 3 2 1 0
* 2: 6 4 2 0
* 4: 4 0
*/
static size_t get_shift(size_t index)
{
int const bits = pixel::get_bit_depth();
return (8 - bits) - (index % get_pixels_per_byte()) * bits;
}
static size_t get_pixels_per_byte()
{
return 8 / pixel::get_bit_depth();
}
reference m_ref;
size_t m_shift;
};
template< class pixel >
class const_packed_pixel_proxy
: public basic_packed_pixel_proxy< pixel, byte const& >
{
public:
const_packed_pixel_proxy(byte const& ref, size_t index)
: basic_packed_pixel_proxy< pixel, byte const& >(ref, index)
{
}
};
template< class pixel >
class packed_pixel_proxy
: public basic_packed_pixel_proxy< pixel, byte& >
{
public:
typedef basic_packed_pixel_proxy< pixel, byte& > basic_proxy;
packed_pixel_proxy(byte& ref, size_t index)
: basic_proxy(ref, index)
{
}
packed_pixel_proxy(packed_pixel_proxy const& other)
: basic_proxy(other.m_ref)
{
this->m_shift = other.m_shift;
}
packed_pixel_proxy& operator=(packed_pixel_proxy const& other)
{
return *this = static_cast< pixel >(other);
}
template< typename reference >
packed_pixel_proxy&
operator=(basic_packed_pixel_proxy< pixel, reference > const& other)
{
return *this = static_cast< pixel >(other);
}
packed_pixel_proxy& operator=(pixel p)
{
this->m_ref = (this->m_ref
& ~(pixel::get_bit_mask() << this->m_shift))
| (p << this->m_shift);
return *this;
}
};
} // namespace detail
/**
* \brief The packed pixel row class template.
*
* Stores the pixel row as a std::vector of byte-s, providing
* access to individual packed pixels via proxy objects.
*/
template< class pixel >
class packed_pixel_row
{
public:
/**
* \brief Constructs a pixel row object for \c size packed pixels.
*/
explicit packed_pixel_row(size_t size = 0)
{
resize(size);
}
size_t size() const
{
return m_size;
}
/**
* \brief Resizes the pixel row to hold up to \c size packed pixels.
*/
void resize(size_t size)
{
m_vec.resize(size / get_pixels_per_byte()
+ (size % get_pixels_per_byte() ? 1 : 0));
m_size = size;
}
/**
* \brief The immutable packed pixel proxy type.
*/
typedef detail::const_packed_pixel_proxy< pixel > const_pixel_proxy;
/**
* \brief The mutable packed pixel proxy type.
*/
typedef detail::packed_pixel_proxy< pixel > pixel_proxy;
/**
* \brief Returns an immutable proxy the to the pixel at \c
* index.
*/
const_pixel_proxy at(size_t index) const
{
return const_pixel_proxy(m_vec.at(index / get_pixels_per_byte()),
index);
}
/**
* \brief Returns a mutable proxy the to the pixel at \c
* index.
*/
pixel_proxy at(size_t index)
{
return pixel_proxy(m_vec.at(index / get_pixels_per_byte()),
index);
}
/**
* \brief Returns an immutable proxy the to the pixel at \c
* index. The non-checking version.
*/
const_pixel_proxy operator[](size_t index) const
{
return const_pixel_proxy(m_vec[index / get_pixels_per_byte()],
index);
}
/**
* \brief Returns n mutable proxy the to the pixel at \c
* index. The non-checking version.
*/
pixel_proxy operator[](size_t index)
{
return pixel_proxy(m_vec[index / get_pixels_per_byte()],
index);
}
/**
* \brief Returns the starting address of the row.
*/
byte* get_data()
{
assert(m_vec.size());
return & m_vec[0];
}
private:
static size_t get_pixels_per_byte()
{
return 8 / pixel::get_bit_depth();
}
std::vector< byte > m_vec;
size_t m_size;
};
/**
* \brief The row_traits class template specialization for packed
* pixel row type.
*/
template< typename pixel >
class row_traits< packed_pixel_row< pixel > >
{
public:
/**
* \brief Returns the starting address of the row.
*/
static byte* get_data(packed_pixel_row< pixel >& row)
{
return row.get_data();
}
};
/**
* \brief The pixel buffer class template specialization for the
* packed_gray_pixel type.
*/
template< int bits >
class pixel_buffer< packed_gray_pixel< bits > >
: public basic_pixel_buffer< packed_gray_pixel< bits >,
packed_pixel_row< packed_gray_pixel
< bits > > >
{
public:
typedef packed_gray_pixel< bits > pixel_type;
typedef packed_pixel_row< pixel_type > pixel_row_type;
pixel_buffer()
{
}
pixel_buffer(uint_32 width, uint_32 height)
: basic_pixel_buffer< pixel_type,
pixel_row_type >(width, height)
{
}
};
/**
* \brief The pixel buffer class template specialization for the
* packed_index_pixel type.
*/
template< int bits >
class pixel_buffer< packed_index_pixel< bits > >
: public basic_pixel_buffer< packed_index_pixel< bits >,
packed_pixel_row< packed_index_pixel
< bits > > >
{
public:
typedef packed_index_pixel< bits > pixel_type;
typedef packed_pixel_row< pixel_type > pixel_row_type;
pixel_buffer()
{
}
pixel_buffer(uint_32 width, uint_32 height)
: basic_pixel_buffer< pixel_type,
pixel_row_type >(width, height)
{
}
};
} // namespace png
#endif // PNGPP_PIXEL_BUFFER_HPP_INCLUDED

View File

@@ -0,0 +1,103 @@
/*
* 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_PIXEL_TRAITS_HPP_INCLUDED
#define PNGPP_PIXEL_TRAITS_HPP_INCLUDED
#include <limits>
#include "types.hpp"
namespace png
{
/**
* \brief Pixel traits class template.
*
* Provides information about pixel color type and components bit depth.
* Not implemented--see specializations.
*
* \see pixel_traits<rgb_pixel>, pixel_traits<rgba_pixel>
*/
template< typename pixel > struct pixel_traits;
/**
* \brief Basic pixel traits class template.
*
* Provides common implementation for various pixel_traits<>
* specializations.
*/
template< typename pixel,
typename component,
color_type pixel_color_type,
int channels_value = sizeof(pixel) / sizeof(component),
int bit_depth_value = std::numeric_limits< component >::digits >
struct basic_pixel_traits
{
typedef pixel pixel_type;
typedef component component_type;
static color_type get_color_type()
{
return pixel_color_type;
}
static const int channels = channels_value;
static int get_channels()
{
return channels;
}
static const int bit_depth = bit_depth_value;
static int get_bit_depth()
{
return bit_depth;
}
};
/**
* \brief Basic pixel traits class template for pixels with alpha
* channel.
*/
template< typename component >
struct basic_alpha_pixel_traits
{
/**
* \brief Returns the default alpha channel filler for full
* opacity.
*/
static component get_alpha_filler()
{
return std::numeric_limits< component >::max();
}
};
} // namespace png
#endif // PNGPP_PIXEL_TRAITS_HPP_INCLUDED

296
Libs/png++/png++/png.hpp Normal file
View File

@@ -0,0 +1,296 @@
/*
* 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_PNG_HPP_INCLUDED
#define PNGPP_PNG_HPP_INCLUDED
#include <png.h>
#include "config.hpp"
#include "types.hpp"
#include "error.hpp"
#include "color.hpp"
#include "palette.hpp"
#include "tRNS.hpp"
#include "packed_pixel.hpp"
#include "rgb_pixel.hpp"
#include "rgba_pixel.hpp"
#include "gray_pixel.hpp"
#include "ga_pixel.hpp"
#include "index_pixel.hpp"
#include "info_base.hpp"
#include "info.hpp"
#include "end_info.hpp"
#include "io_base.hpp"
#include "reader.hpp"
#include "writer.hpp"
#include "generator.hpp"
#include "consumer.hpp"
#include "pixel_buffer.hpp"
#include "solid_pixel_buffer.hpp"
#include "require_color_space.hpp"
#include "convert_color_space.hpp"
#include "image.hpp"
/**
* \mainpage
*
* \section sec_intro Introduction
*
* This is the documentation for png++ the C++ wrapper for libpng.
* This page documents png++ version 0.2.
*
* PNG++ aims to provide simple yet powerful C++ interface to libpng,
* the PNG reference implementation library. PNG++ is free software
* distributed under a modified variant of BSD <a
* href="http://www.nongnu.org/pngpp/license.html">license</a>.
*
* \section sec_news News
*
* - Added support for tRNS chunk.
* - Added non-std IO streams support.
* - Fixed 16-bit endianness problems.
* - Improved test script.
*
* \section sec_getting_started Getting started
*
* The following code demonstrates how to read and write PNG images
* using png++:
*
* \code
* png::image< png::rgb_pixel > image("input.png");
* image.write("output.png");
* \endcode
*
* The code reads an image from the file named \c input.png, then
* writes the image to a file named \c output.png. The image class
* template allows you to specify the desired pixel type for the image
* data. The available pixel types include: RGB, Grayscale and
* Indexed pixels. Some of the pixel types can have an alpha channel.
*
* The png++ naturally supports reading PNG images of any %color type
* into RGB or Grayscale pixel buffers (with optional alpha channel).
* This is particularly useful for an image viewer if it needs to
* display the PNG image on, for example, RGB device regardless of the
* image %color type.
*
* On the other hand one might want to read only images of particular
* type. With png++ you can specify it this way:
*
* \code
* png::image< png::rgb_pixel > image("rgb.png", png::require_color_space< png::rgb_pixel >());
* \endcode
*
* \section sec_installing Installing
*
* PNG++ comes as a set of header files and does not require
* compilation to be installed. For the same reason there are no
* binary packages for png++.
*
* \subsection subs_prerequisites Prerequisites
*
* - png++ works with libpng-1.2.x.
* - png++ compiles with g++-4.1 and g++-4.2. Other version should
* work well too.
* - png++ relies on GNU make for compiling tests and examples; in
* particular it uses "remaking makefiles" feature
* - Documentation is produced using <a
* href="http://www.doxygen.org/">doxygen</a>. See the bottom of
* this page for doxygen version used to compile these docs.
*
* \subsection subs_installing_pngpp Installing png++
*
* Follow these instructions in order to install png++:
*
* -# Unpack source package:
* \verbatim $ tar -zxf png++-0.2.x.tar.gz -C ~/src \endverbatim
* -# Go to your brand new png++ sources directory:
* \verbatim $ cd ~/src/png++-0.2.x \endverbatim
* -# Issue \c make to test how it's doing:
* \verbatim $ make \endverbatim
* This will compile examples in the \c example directory. If everything
* goes well, try \verbatim $ make test \endverbatim (or \c make \c
* check which is the same as above) to run the test suite. If tests
* do not produce error messages then probably all is OK.
* -# Now you can create documentation (optional). Use
* \verbatim $ make docs \endverbatim
* to run \c doxygen in the sources directory.
* -# Now it is time to become \c root and install png++ into your
* system. It's OK to issue \c make \c install under ordinary user
* permissions if you want to install png++ into your home
* directory. Run the following command:
* \verbatim $ make install PREFIX=$HOME \endverbatim
* to copy png++ header files to \c ~/include/png++ and documentation
* files to <tt>~/share/doc/png++-0.2.x</tt>. Without a \c PREFIX png++
* installs to \c /usr/local.
*
* \section sec_working_with_images Working with images
*
* In png++ you can create new images like this:
*
* \code
* #include <png++/png.hpp>
* //...
* png::image< png::rgb_pixel > image(128, 128);
* for (png::uint_32 y = 0; y < image.get_height(); ++y)
* {
* for (png::uint_32 x = 0; x < image.get_width(); ++x)
* {
* image[y][x] = png::rgb_pixel(x, y, x + y);
* // non-checking equivalent of image.set_pixel(x, y, ...);
* }
* }
* image.write("rgb.png");
* \endcode
*
* Optionally, you may specify
* \code
* image.set_interlace_type(png::interlace_adam7)
* \endcode
* to produce an interlaced image.
*
* If you are writing an indexed colors image, you should provide a
* palette (colormap). One of the ways to do this is the following:
*
* \code
* #include <png++/png.hpp>
* //...
* png::image< png::index_pixel > image;
* png::palette pal(256);
* for (size_t i = 0; i < pal.size(); ++i)
* {
* pal[i] = png::color(i, 255 - i, i);
* }
* image.set_palette(pal);
* ...
* image.write("palette.png");
* \endcode
*
* It is not absolutely necessary to have the whole image data in
* memory in order to write a PNG file. You can use generator class
* template to write the image row-by-row. An example of this is
* provided in \c example/pixel_generator.cpp bundled with the sources
* package.
*
* The same holds for reading images too. You can use consumer class
* template in order to read the image data row-by-row. This might
* help in applications which have to deal with large PNG images but
* do not want to read the entire image into memory.
*
* You can read or write images from/to generic IO stream, not only
* file on disk. Check out \c image::read(std::istream&),
* \c image::write(std::ostream&) overloads in the reference manual.
*
* \section sec_compiling_user Compiling your programs
*
* Use the following command to compile your program:
*
* \verbatim $ g++ -c example.cpp `libpng-config --cflags` \endverbatim
*
* and the following to link it:
*
* \verbatim $ g++ -o example example.o `libpng-config --ldflags` \endverbatim
*
* When compiling you should add \c -I \c $PREFIX/include if you have
* installed png++ to non-standard location, like your home directory.
*
* In your program, the line
*
* \code
* #include <png++/png.hpp>
* \endcode
*
* brings in all the header files in png++ which should be suitable
* for the most of the applications. You may include only the headers
* you really use, for example:
*
* \code
* #include <png++/image.hpp>
* #include <png++/rgb_pixel.hpp>
* \endcode
*
* If do not want to install png++ headers you still can compile your
* programs. Just create a subdirectory named \c png++ somewhere in
* your project tree and copy all of the \c .hpp files in png++
* distribution there. Then use appropriate compiler options to add
* this directory into the header search path.
*
* \section sec_further Further reading
*
* - To get information about specific features, use reference (can be
* reached from the top of this page).
* - If you are looking for more example code, please go to the \c
* example/ directory of the source distribution. You may also find
* sources in the \c test/ directory insightful (well, somewhat).
* - Have a question? Check out \ref sec_help section below.
* - Of course, your ultimate source for learning is the source
* code. <tt>:-)</tt>
*
* \section sec_download Download
*
* The project is hosted at Savannah:
* http://savannah.nongnu.org/projects/pngpp/
*
* Released source packages can be found here:
* http://download.savannah.nongnu.org/releases/pngpp/
*
* Also, you can check out sources directly from SVN repository:
* svn://svn.sv.nongnu.org/pngpp/trunk/ or
* http://svn.sv.nongnu.org/svn/pngpp/trunk/ (for people w/o outgoing svn).
*
* Online version of this documentation can be found here:
* http://www.nongnu.org/pngpp/doc/
*
* \section sec_bugs Bugs
*
* The following is a list of known bugs and limitations:
*
* - Lacks support for output transformations
* - Lacks support for optional/unknown chunks in PNG data stream
* - Documentation sucks <tt>;-)</tt>
*
* To report bugs, please use Savannah bug tracker:
* http://savannah.nongnu.org/bugs/?group=pngpp&func=additem
*
* Do not forget to check if the bug was already filed. <tt>:-)</tt>
*
* \section sec_help Getting help
*
* There is a mailing list for developers:
* http://lists.nongnu.org/mailman/listinfo/pngpp-devel
*
* You can also contact me by dropping a mail to <alex.shulgin@gmail.com>.
*
* Happy hacking!
*
* Alex Shulgin
*/
#endif // PNGPP_PNG_HPP_INCLUDED

179
Libs/png++/png++/reader.hpp Normal file
View File

@@ -0,0 +1,179 @@
/*
* 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_READER_HPP_INCLUDED
#define PNGPP_READER_HPP_INCLUDED
#include <cassert>
#include "io_base.hpp"
namespace png
{
/**
* \brief The PNG reader class template. This is the low-level
* reading interface--use image class or consumer class to
* actually read images.
*
* The \c istream template parameter specifies the type of input
* stream to work with. The \c istream class should implement the
* minimum of the following interface:
*
* \code
* class my_istream
* {
* public:
* void read(char*, size_t);
* bool good();
* };
* \endcode
*
* With the semantics similar to the \c std::istream. Naturally,
* \c std::istream fits this requirement and can be used with the
* reader class as is.
*
* \see image, consumer, writer, io_base
*/
template< class istream >
class reader
: public io_base
{
public:
/**
* \brief Constructs a reader prepared to read PNG image from
* a \a stream.
*/
explicit reader(istream& stream)
: io_base(png_create_read_struct(PNG_LIBPNG_VER_STRING,
static_cast< io_base* >(this),
raise_error,
0))
{
png_set_read_fn(m_png, & stream, read_data);
}
~reader()
{
png_destroy_read_struct(& m_png,
m_info.get_png_info_ptr(),
m_end_info.get_png_info_ptr());
}
/**
* \brief Reads the whole PNG data stream into memory. Not
* particularly useful.
*/
void read_png()
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
png_read_png(m_png,
m_info.get_png_info(),
/* transforms = */ 0,
/* params = */ 0);
}
/**
* \brief Reads info about PNG image.
*/
void read_info()
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
m_info.read();
}
/**
* \brief Reads a row of image data at a time.
*/
void read_row(byte* bytes)
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
png_read_row(m_png, bytes, 0);
}
/**
* \brief Reads ending info about PNG image.
*/
void read_end_info()
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
m_end_info.read();
}
void update_info()
{
m_info.update();
}
private:
static void read_data(png_struct* png, byte* data, png_size_t length)
{
io_base* io = static_cast< io_base* >(png_get_error_ptr(png));
reader* rd = static_cast< reader* >(io);
rd->reset_error();
istream* stream = reinterpret_cast< istream* >(png_get_io_ptr(png));
try
{
stream->read(reinterpret_cast< char* >(data), length);
if (!stream->good())
{
rd->set_error("istream::read() failed");
}
}
catch (std::exception const& error)
{
rd->set_error(error.what());
}
catch (...)
{
assert(!"read_data: caught something wrong");
rd->set_error("read_data: caught something wrong");
}
if (rd->is_error())
{
rd->raise_error();
}
}
};
} // namespace png
#endif // PNGPP_READER_HPP_INCLUDED

View File

@@ -0,0 +1,171 @@
/*
* 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_REQUIRE_COLOR_SPACE_HPP_INCLUDED
#define PNGPP_REQUIRE_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 "io_base.hpp"
namespace png
{
namespace detail
{
template< typename pixel >
struct wrong_color_space
{
inline static char const* error_msg();
};
template<> inline char const*
wrong_color_space< rgb_pixel >::error_msg()
{
return "8-bit RGB color space required";
}
template<> inline char const*
wrong_color_space< rgb_pixel_16 >::error_msg()
{
return "16-bit RGB color space required";
}
template<> inline char const*
wrong_color_space< rgba_pixel >::error_msg()
{
return "8-bit RGBA color space required";
}
template<> inline char const*
wrong_color_space< rgba_pixel_16 >::error_msg()
{
return "16-bit RGBA color space required";
}
template<> inline char const*
wrong_color_space< gray_pixel >::error_msg()
{
return "8-bit Grayscale color space required";
}
template<> inline char const*
wrong_color_space< gray_pixel_1 >::error_msg()
{
return "1-bit Grayscale color space required";
}
template<> inline char const*
wrong_color_space< gray_pixel_2 >::error_msg()
{
return "2-bit Grayscale color space required";
}
template<> inline char const*
wrong_color_space< gray_pixel_4 >::error_msg()
{
return "4-bit Grayscale color space required";
}
template<> inline char const*
wrong_color_space< gray_pixel_16 >::error_msg()
{
return "16-bit Grayscale color space required";
}
template<> inline char const*
wrong_color_space< ga_pixel >::error_msg()
{
return "8-bit Gray+Alpha color space required";
}
template<> inline char const*
wrong_color_space< ga_pixel_16 >::error_msg()
{
return "16-bit Gray+Alpha color space required";
}
template<> inline char const*
wrong_color_space< index_pixel >::error_msg()
{
return "8-bit Colormap color space required";
}
template<> inline char const*
wrong_color_space< index_pixel_1 >::error_msg()
{
return "1-bit Colormap color space required";
}
template<> inline char const*
wrong_color_space< index_pixel_2 >::error_msg()
{
return "2-bit Colormap color space required";
}
template<> inline char const*
wrong_color_space< index_pixel_4 >::error_msg()
{
return "4-bit Colormap color space required";
}
} // namespace detail
/**
* \brief IO transformation class template. Enforces image color space.
*
* This IO transformation class template used to enforce source image
* color space.
*
* \see image, image::read
*/
template< typename pixel >
struct require_color_space
{
typedef pixel_traits< pixel > traits;
void operator()(io_base& io) const
{
if (io.get_color_type() != traits::get_color_type()
|| io.get_bit_depth() != traits::get_bit_depth())
{
throw error(detail::wrong_color_space< pixel >::error_msg());
}
}
};
} // namespace png
#endif // PNGPP_REQUIRE_COLOR_SPACE_HPP_INCLUDED

View File

@@ -0,0 +1,90 @@
/*
* 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_RGB_PIXEL_HPP_INCLUDED
#define PNGPP_RGB_PIXEL_HPP_INCLUDED
#include "types.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief RGB pixel type.
*/
template< typename T >
struct basic_rgb_pixel
{
/**
* \brief Default constructor. Initializes all components
* with zeros.
*/
basic_rgb_pixel()
: red(0), green(0), blue(0)
{
}
/**
* \brief Constructs rgb_pixel object from \a red, \a green
* and \a blue components passed as parameters.
*/
basic_rgb_pixel(T red, T green, T blue)
: red(red), green(green), blue(blue)
{
}
T red;
T green;
T blue;
};
/**
* The 8-bit RGB pixel type.
*/
typedef basic_rgb_pixel< byte > rgb_pixel;
/**
* The 16-bit RGB pixel type.
*/
typedef basic_rgb_pixel< uint_16 > rgb_pixel_16;
/**
* \brief Pixel traits specialization for basic_rgb_pixel.
*/
template< typename T >
struct pixel_traits< basic_rgb_pixel< T > >
: basic_pixel_traits< basic_rgb_pixel< T >, T, color_type_rgb >
{
};
} // namespace png
#endif // PNGPP_RGB_PIXEL_HPP_INCLUDED

View File

@@ -0,0 +1,96 @@
/*
* 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_RGBA_PIXEL_HPP_INCLUDED
#define PNGPP_RGBA_PIXEL_HPP_INCLUDED
#include "types.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief RGBA pixel type.
*/
template< typename T >
struct basic_rgba_pixel
{
typedef pixel_traits< basic_rgba_pixel< T > > traits;
/**
* \brief Default constructor. Initializes all components
* with zeros.
*/
basic_rgba_pixel()
: red(0), green(0), blue(0), alpha(0)
{
}
/**
* \brief Constructs rgba_pixel object from \a red, \a green,
* \a blue and \a alpha components passed as parameters.
* Alpha defaults to full opacity.
*/
basic_rgba_pixel(T red, T green, T blue,
T alpha = traits::get_alpha_filler())
: red(red), green(green), blue(blue), alpha(alpha)
{
}
T red;
T green;
T blue;
T alpha;
};
/**
* The 8-bit RGBA pixel type.
*/
typedef basic_rgba_pixel< byte > rgba_pixel;
/**
* The 16-bit RGBA pixel type.
*/
typedef basic_rgba_pixel< uint_16 > rgba_pixel_16;
/**
* \brief Pixel traits specialization for basic_rgba_pixel.
*/
template< typename T >
struct pixel_traits< basic_rgba_pixel< T > >
: basic_pixel_traits< basic_rgba_pixel< T >, T, color_type_rgba >,
basic_alpha_pixel_traits< T >
{
};
} // namespace png
#endif // PNGPP_RGBA_PIXEL_HPP_INCLUDED

View File

@@ -0,0 +1,243 @@
/*
* 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_SOLID_PIXEL_BUFFER_HPP_INCLUDED
#define PNGPP_SOLID_PIXEL_BUFFER_HPP_INCLUDED
#include <cassert>
#include <cstddef>
#include <climits>
#include <stdexcept>
#include <vector>
#include "config.hpp"
#include "packed_pixel.hpp"
#include "gray_pixel.hpp"
#include "index_pixel.hpp"
namespace png
{
/**
* \brief Pixel buffer, that stores pixels as continuous memory chunk.
* solid_pixel_buffer is useful when user whats to open png, do some
* changes and fetch to buffer to draw (as texture for example).
*/
template< typename pixel >
class solid_pixel_buffer
{
public:
typedef pixel_traits< pixel > pixel_traits_t;
struct row_traits
{
typedef pixel* row_access;
typedef const pixel* row_const_access;
static byte* get_data(row_access row)
{
return reinterpret_cast<byte*>(row);
}
};
/**
* \brief A row of pixel data.
*/
typedef typename row_traits::row_access row_access;
typedef typename row_traits::row_const_access row_const_access;
typedef row_access row_type;
/**
* \brief Constructs an empty 0x0 pixel buffer object.
*/
solid_pixel_buffer()
: m_width(0),
m_height(0),
m_stride(0)
{
}
/**
* \brief Constructs an empty pixel buffer object.
*/
solid_pixel_buffer(uint_32 width, uint_32 height)
: m_width(0),
m_height(0),
m_stride(0)
{
resize(width, height);
}
uint_32 get_width() const
{
return m_width;
}
uint_32 get_height() const
{
return m_height;
}
/**
* \brief Resizes the pixel buffer.
*
* If new width or height is greater than the original,
* expanded pixels are filled with value of \a pixel().
*/
void resize(uint_32 width, uint_32 height)
{
m_width = width;
m_height = height;
m_stride = m_width * bytes_per_pixel;
m_bytes.resize(height * m_stride);
}
/**
* \brief Returns a reference to the row of image data at
* specified index.
*
* Checks the index before returning a row: an instance of
* std::out_of_range is thrown if \c index is greater than \c
* height.
*/
row_access get_row(size_t index)
{
return reinterpret_cast<row_access>(&m_bytes.at(index * m_stride));
}
/**
* \brief Returns a const reference to the row of image data at
* specified index.
*
* The checking version.
*/
row_const_access get_row(size_t index) const
{
return (row_const_access)(&m_bytes.at(index * m_stride));
}
/**
* \brief The non-checking version of get_row() method.
*/
row_access operator[](size_t index)
{
return (row_access)(&m_bytes[index * m_stride]);
}
/**
* \brief The non-checking version of get_row() method.
*/
row_const_access operator[](size_t index) const
{
return (row_const_access)(&m_bytes[index * m_stride]);
}
/**
* \brief Replaces the row at specified index.
*/
void put_row(size_t index, row_const_access r)
{
row_access row = get_row();
for (uint_32 i = 0; i < m_width; ++i)
*row++ = *r++;
}
/**
* \brief Returns a pixel at (x,y) position.
*/
pixel get_pixel(size_t x, size_t y) const
{
size_t index = (y * m_width + x) * bytes_per_pixel;
return *reinterpret_cast< const pixel* >(&m_bytes.at(index));
}
/**
* \brief Replaces a pixel at (x,y) position.
*/
void set_pixel(size_t x, size_t y, pixel p)
{
size_t index = (y * m_width + x) * bytes_per_pixel;
*reinterpret_cast< pixel* >(&m_bytes.at(index)) = p;
}
/**
* \brief Provides easy constant read access to underlying byte-buffer.
*/
const std::vector< byte >& get_bytes() const
{
return m_bytes;
}
#ifdef PNGPP_HAS_STD_MOVE
/**
* \brief Moves the buffer to client code (c++11 only) .
*/
std::vector< byte > fetch_bytes()
{
m_width = 0;
m_height = 0;
m_stride = 0;
// the buffer is moved outside without copying and leave m_bytes empty.
return std::move(m_bytes);
}
#endif
protected:
static const size_t bytes_per_pixel = pixel_traits_t::channels *
pixel_traits_t::bit_depth / CHAR_BIT;
protected:
uint_32 m_width;
uint_32 m_height;
size_t m_stride;
std::vector< byte > m_bytes;
#ifdef PNGPP_HAS_STATIC_ASSERT
static_assert(pixel_traits_t::bit_depth % CHAR_BIT == 0,
"Bit_depth should consist of integer number of bytes");
static_assert(sizeof(pixel) * CHAR_BIT ==
pixel_traits_t::channels * pixel_traits_t::bit_depth,
"pixel type should contain channels data only");
#endif
};
/**
* \brief solid_pixel_buffer for packed_pixel is not implemented now.
* Should there be a gap between rows? How to deal with last
* useless bits in last byte in buffer?
*/
template< int bits >
class solid_pixel_buffer< packed_pixel< bits > >;
} // namespace png
#endif // PNGPP_solid_pixel_buffer_HPP_INCLUDED

View File

@@ -0,0 +1,128 @@
/*
* 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_STREAMING_BASE_HPP_INCLUDED
#define PNGPP_STREAMING_BASE_HPP_INCLUDED
#include <cassert>
#include "image_info.hpp"
#include "pixel_traits.hpp"
namespace png
{
/**
* \brief The default image_info holder class. Stores image_info
* member object.
*/
class def_image_info_holder
{
public:
def_image_info_holder(image_info const& info)
: m_info(info)
{
}
image_info& get_info()
{
return m_info;
}
private:
image_info m_info;
};
/**
* \brief An image_info holder class. Stores a reference to the
* image_info object. The image_info object itself should be
* stored elsewhere.
*/
class image_info_ref_holder
{
public:
image_info_ref_holder(image_info& info)
: m_info(info)
{
}
image_info& get_info()
{
return m_info;
}
private:
image_info& m_info;
};
/**
* \brief A base class template for consumer and generator
* classes. Provides default \c reset() method implementation as
* well as \c info_holder policy.
*/
template< typename pixel, class info_holder >
class streaming_base
{
public:
typedef pixel_traits< pixel > traits;
explicit streaming_base(image_info& info)
: m_info_holder(info)
{
}
streaming_base(uint_32 width, uint_32 height)
: m_info_holder(make_image_info< pixel >())
{
get_info().set_width(width);
get_info().set_height(height);
}
image_info const& get_info() const
{
return m_info_holder.get_info();
}
protected:
void reset(size_t /*pass*/)
{
// nothing to do in the most general case
}
image_info& get_info()
{
return m_info_holder.get_info();
}
info_holder m_info_holder;
};
} // namespace png
#endif // PNGPP_STREAMING_BASE_HPP_INCLUDED

48
Libs/png++/png++/tRNS.hpp Normal file
View File

@@ -0,0 +1,48 @@
/*
* 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_TRNS_HPP_INCLUDED
#define PNGPP_TRNS_HPP_INCLUDED
#include <vector>
#include "color.hpp"
namespace png
{
/**
* \brief The palette transparency map type. Currently
* implemented as \c std::vector of png::byte.
*/
typedef std::vector< byte > tRNS;
} // namespace png
#endif // PNGPP_TRNS_HPP_INCLUDED

120
Libs/png++/png++/types.hpp Normal file
View File

@@ -0,0 +1,120 @@
/*
* 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_TYPES_HPP_INCLUDED
#define PNGPP_TYPES_HPP_INCLUDED
#include <png.h>
namespace png
{
typedef png_byte byte;
typedef png_uint_16 uint_16;
typedef png_uint_32 uint_32;
typedef png_fixed_point fixed_point;
typedef png_color_8 color_info;
typedef png_color_16 color_info_16;
enum color_type
{
color_type_none = -1,
color_type_gray = PNG_COLOR_TYPE_GRAY,
color_type_palette = PNG_COLOR_TYPE_PALETTE,
color_type_rgb = PNG_COLOR_TYPE_RGB,
color_type_rgb_alpha = PNG_COLOR_TYPE_RGB_ALPHA,
color_type_gray_alpha = PNG_COLOR_TYPE_GRAY_ALPHA,
color_type_rgba = PNG_COLOR_TYPE_RGBA,
color_type_ga = PNG_COLOR_TYPE_GA
};
enum color_mask
{
color_mask_palette = PNG_COLOR_MASK_PALETTE,
color_mask_color = PNG_COLOR_MASK_COLOR,
color_mask_rgb = color_mask_color,
color_mask_alpha = PNG_COLOR_MASK_ALPHA
};
enum filler_type
{
filler_before = PNG_FILLER_BEFORE,
filler_after = PNG_FILLER_AFTER
};
enum rgb_to_gray_error_action
{
rgb_to_gray_silent = 1,
rgb_to_gray_warning = 2,
rgb_to_gray_error = 3
};
enum interlace_type
{
interlace_none = PNG_INTERLACE_NONE,
interlace_adam7 = PNG_INTERLACE_ADAM7
};
enum compression_type
{
compression_type_base = PNG_COMPRESSION_TYPE_BASE,
compression_type_default = PNG_COMPRESSION_TYPE_DEFAULT
};
enum filter_type
{
filter_type_base = PNG_FILTER_TYPE_BASE,
intrapixel_differencing = PNG_INTRAPIXEL_DIFFERENCING,
filter_type_default = PNG_FILTER_TYPE_DEFAULT
};
enum chunk
{
chunk_gAMA = PNG_INFO_gAMA,
chunk_sBIT = PNG_INFO_sBIT,
chunk_cHRM = PNG_INFO_cHRM,
chunk_PLTE = PNG_INFO_PLTE,
chunk_tRNS = PNG_INFO_tRNS,
chunk_bKGD = PNG_INFO_bKGD,
chunk_hIST = PNG_INFO_hIST,
chunk_pHYs = PNG_INFO_pHYs,
chunk_oFFs = PNG_INFO_oFFs,
chunk_tIME = PNG_INFO_tIME,
chunk_pCAL = PNG_INFO_pCAL,
chunk_sRGB = PNG_INFO_sRGB,
chunk_iCCP = PNG_INFO_iCCP,
chunk_sPLT = PNG_INFO_sPLT,
chunk_sCAL = PNG_INFO_sCAL,
chunk_IDAT = PNG_INFO_IDAT
};
} // namespace png
#endif // PNGPP_TYPES_HPP_INCLUDED

199
Libs/png++/png++/writer.hpp Normal file
View File

@@ -0,0 +1,199 @@
/*
* 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_WRITER_HPP_INCLUDED
#define PNGPP_WRITER_HPP_INCLUDED
#include <cassert>
#include "io_base.hpp"
namespace png
{
/**
* \brief PNG writer class template. This is the low-level
* writing interface--use image class or generator class to
* actually write images.
*
* The \c ostream template parameter specifies the type of output
* stream to work with. The \c ostream class should implement the
* minimum of the following interface:
*
* \code
* class my_ostream
* {
* public:
* void write(char const*, size_t);
* void flush();
* bool good();
* };
* \endcode
*
* With the semantics similar to the \c std::ostream. Naturally,
* \c std::ostream fits this requirement and can be used with the
* writer class as is.
*
* \see image, reader, generator, io_base
*/
template< class ostream >
class writer
: public io_base
{
public:
/**
* \brief Constructs a writer prepared to write PNG image into
* a \a stream.
*/
explicit writer(ostream& stream)
: io_base(png_create_write_struct(PNG_LIBPNG_VER_STRING,
static_cast< io_base* >(this),
raise_error,
0))
{
png_set_write_fn(m_png, & stream, write_data, flush_data);
}
~writer()
{
m_end_info.destroy();
png_destroy_write_struct(& m_png, m_info.get_png_info_ptr());
}
void write_png() const
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
png_write_png(m_png,
m_info.get_png_info(),
/* transforms = */ 0,
/* params = */ 0);
}
/**
* \brief Write info about PNG image.
*/
void write_info() const
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
m_info.write();
}
/**
* \brief Writes a row of image data at a time.
*/
void write_row(byte* bytes)
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
png_write_row(m_png, bytes);
}
/**
* \brief Reads ending info about PNG image.
*/
void write_end_info() const
{
if (setjmp(png_jmpbuf(m_png)))
{
throw error(m_error);
}
m_end_info.write();
}
private:
static void write_data(png_struct* png, byte* data, png_size_t length)
{
io_base* io = static_cast< io_base* >(png_get_error_ptr(png));
writer* wr = static_cast< writer* >(io);
wr->reset_error();
ostream* stream = reinterpret_cast< ostream* >(png_get_io_ptr(png));
try
{
stream->write(reinterpret_cast< char* >(data), length);
if (!stream->good())
{
wr->set_error("ostream::write() failed");
}
}
catch (std::exception const& error)
{
wr->set_error(error.what());
}
catch (...)
{
assert(!"caught something wrong");
wr->set_error("write_data: caught something wrong");
}
if (wr->is_error())
{
wr->raise_error();
}
}
static void flush_data(png_struct* png)
{
io_base* io = static_cast< io_base* >(png_get_error_ptr(png));
writer* wr = static_cast< writer* >(io);
wr->reset_error();
ostream* stream = reinterpret_cast< ostream* >(png_get_io_ptr(png));
try
{
stream->flush();
if (!stream->good())
{
wr->set_error("ostream::flush() failed");
}
}
catch (std::exception const& error)
{
wr->set_error(error.what());
}
catch (...)
{
assert(!"caught something wrong");
wr->set_error("flush_data: caught something wrong");
}
if (wr->is_error())
{
wr->raise_error();
}
}
};
} // namespace png
#endif // PNGPP_WRITER_HPP_INCLUDED