mirror of
https://github.com/boostorg/gil.git
synced 2026-01-27 06:52:11 +00:00
cherry-pick
This commit is contained in:
@@ -444,6 +444,11 @@ void destruct_range_impl( It first
|
||||
++first;
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
template <typename It> GIL_FORCEINLINE
|
||||
void destruct_range_impl(It, It, mpl::false_) {}
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
template <typename It> BOOST_FORCEINLINE
|
||||
void destruct_range_impl( It
|
||||
@@ -583,7 +588,11 @@ void default_construct_range_impl(It first, It last, mpl::true_) {
|
||||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
template <typename It> BOOST_FORCEINLINE
|
||||
=======
|
||||
template <typename It> GIL_FORCEINLINE
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
void default_construct_range_impl(It, It, mpl::false_) {}
|
||||
|
||||
template <typename It> BOOST_FORCEINLINE
|
||||
|
||||
@@ -248,6 +248,12 @@ BOOST_STATIC_ASSERT((boost::is_integral<bits4>::value));
|
||||
template <int NumBits>
|
||||
class packed_channel_value {
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
typedef typename detail::num_value_fn< NumBits >::type num_value_t;
|
||||
static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
|
||||
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
public:
|
||||
typedef typename detail::min_fast_uint<NumBits>::type integer_t;
|
||||
|
||||
@@ -264,11 +270,20 @@ public:
|
||||
BOOST_STATIC_CONSTANT(bool, is_mutable=true);
|
||||
|
||||
packed_channel_value() {}
|
||||
<<<<<<< HEAD
|
||||
|
||||
packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v & low_bits_mask_t<NumBits>::sig_bits_fast ); }
|
||||
template <typename Scalar> packed_channel_value(Scalar v) { _value = packed_channel_value( static_cast< integer_t >( v ) ); }
|
||||
|
||||
static unsigned int num_bits() { return NumBits; }
|
||||
=======
|
||||
packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v % num_values ); }
|
||||
packed_channel_value(const packed_channel_value& v) : _value(v._value) {}
|
||||
template <typename Scalar> packed_channel_value(Scalar v) { _value = static_cast< integer_t >( v ) % num_values; }
|
||||
|
||||
static unsigned int num_bits() { return NumBits; }
|
||||
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
operator integer_t() const { return _value; }
|
||||
private:
|
||||
|
||||
@@ -93,7 +93,11 @@ public:
|
||||
|
||||
template <typename T> any_image& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image& operator=(const any_image& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
<<<<<<< HEAD
|
||||
template <typename Types> any_image& operator=(const any_image<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
=======
|
||||
template <typename Types> any_image& operator=(const any_image<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
void recreate(const point_t& dims, unsigned alignment=1) { apply_operation(*this,detail::recreate_image_fnobj(dims,alignment)); }
|
||||
void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1) { recreate(point2<std::ptrdiff_t>(width,height),alignment); }
|
||||
|
||||
@@ -76,7 +76,11 @@ public:
|
||||
|
||||
template <typename T> any_image_view& operator=(const T& obj) { parent_t::operator=(obj); return *this; }
|
||||
any_image_view& operator=(const any_image_view& v) { parent_t::operator=((const parent_t&)v); return *this;}
|
||||
<<<<<<< HEAD
|
||||
template <typename Types> any_image_view& operator=(const any_image_view<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
=======
|
||||
template <typename Types> any_image_view& operator=(const any_image_view<Types>& v) { parent_t::operator=((const variant<Types>&)v); return *this;}
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
|
||||
point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
|
||||
|
||||
@@ -141,7 +141,11 @@ namespace detail {
|
||||
template <typename T2, typename Op>
|
||||
struct reduce_bind1 {
|
||||
const T2& _t2;
|
||||
<<<<<<< HEAD
|
||||
Op& _op;
|
||||
=======
|
||||
Op& _op;
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
typedef typename Op::result_type result_type;
|
||||
|
||||
@@ -154,7 +158,11 @@ namespace detail {
|
||||
struct reduce_bind2 {
|
||||
const Bits1& _bits1;
|
||||
std::size_t _index1;
|
||||
<<<<<<< HEAD
|
||||
Op& _op;
|
||||
=======
|
||||
Op& _op;
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
typedef typename Op::result_type result_type;
|
||||
|
||||
|
||||
@@ -100,7 +100,12 @@ public:
|
||||
virtual ~variant() { apply_operation(*this, detail::destructor_op()); }
|
||||
|
||||
// Throws std::bad_cast if T is not in Types
|
||||
template <typename T> explicit variant(const T& obj){ _index=type_id<T>(); if (_index==NUM_TYPES) throw std::bad_cast(); detail::copy_construct_in_place(obj, _bits); }
|
||||
template <typename T> explicit variant(const T& obj){ _index=type_id<T>(); if (_index==NUM_TYPES) throw std::bad_cast(); detail::copy_construct_in_place(obj, _bits); }
|
||||
|
||||
template <typename Types2> explicit variant(const variant<Types2>& obj) : _index(apply_operation(obj,detail::type_to_index_fn<Types>())) {
|
||||
if (_index==NUM_TYPES) throw std::bad_cast();
|
||||
apply_operation(obj, detail::copy_construct_in_place_fn<base_t>(_bits));
|
||||
}
|
||||
|
||||
template <typename Types2> explicit variant(const variant<Types2>& obj) : _index(apply_operation(obj,detail::type_to_index_fn<Types>())) {
|
||||
if (_index==NUM_TYPES) throw std::bad_cast();
|
||||
@@ -132,7 +137,7 @@ public:
|
||||
private:
|
||||
template <typename T> static std::size_t type_id() { return detail::type_to_index<Types,T>::value; }
|
||||
|
||||
template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
|
||||
template <typename Cs> friend void swap(variant<Cs>& x, variant<Cs>& y);
|
||||
template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(variant<Types2>& var, UnaryOp op);
|
||||
template <typename Types2, typename UnaryOp> friend typename UnaryOp::result_type apply_operation(const variant<Types2>& var, UnaryOp op);
|
||||
template <typename Types1, typename Types2, typename BinaryOp> friend typename BinaryOp::result_type apply_operation(const variant<Types1>& arg1, const variant<Types2>& arg2, BinaryOp op);
|
||||
@@ -171,9 +176,15 @@ namespace detail {
|
||||
|
||||
template <typename Types>
|
||||
struct type_to_index_fn {
|
||||
<<<<<<< HEAD
|
||||
typedef std::size_t result_type;
|
||||
|
||||
template <typename T> result_type operator()(const T&) const { return detail::type_to_index<Types,T>::value; }
|
||||
=======
|
||||
typedef std::size_t result_type;
|
||||
|
||||
template <typename T> result_type operator()(const T&) const { return detail::type_to_index<Types,T>::value; }
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
141
include/boost/gil/extension/io/png_dynamic_io.hpp
Normal file
141
include/boost/gil/extension/io/png_dynamic_io.hpp
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
Copyright 2005-2007 Adobe Systems Incorporated
|
||||
|
||||
Use, modification and distribution are subject to the Boost Software License,
|
||||
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
See http://opensource.adobe.com/gil for most recent version including documentation.
|
||||
*/
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef GIL_PNG_DYNAMIC_IO_H
|
||||
#define GIL_PNG_DYNAMIC_IO_H
|
||||
|
||||
/// \file
|
||||
/// \brief Support for reading and writing PNG files
|
||||
/// Requires libpng and zlib!
|
||||
///
|
||||
/// \author Hailin Jin and Lubomir Bourdev \n
|
||||
/// Adobe Systems Incorporated
|
||||
/// \date 2005-2007 \n Last updated June 10, 2006
|
||||
//
|
||||
// We are currently providing the following functions:
|
||||
// template <typename Images> void png_read_image(const char*,any_image<Images>&)
|
||||
// template <typename Images> void png_read_image(FILE*,any_image<Images>&,std::size_t)
|
||||
// template <typename Views> void png_write_view(const char*,const any_image_view<View>&)
|
||||
// template <typename Views> void png_write_view(FILE*,const any_image_view<View>&)
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include "../dynamic_image/dynamic_image_all.hpp"
|
||||
#include "io_error.hpp"
|
||||
#include "png_io.hpp"
|
||||
#include "png_io_private.hpp"
|
||||
#include "dynamic_io.hpp"
|
||||
|
||||
namespace boost { namespace gil {
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct png_write_is_supported {
|
||||
template<typename View> struct apply
|
||||
: public mpl::bool_<png_write_support<View>::is_supported> {};
|
||||
};
|
||||
|
||||
class png_writer_dynamic : public png_writer {
|
||||
public:
|
||||
png_writer_dynamic(FILE* file ) : png_writer(file) {}
|
||||
png_writer_dynamic(const char* filename) : png_writer(filename){}
|
||||
|
||||
template <typename Views>
|
||||
void write_view(const any_image_view<Views>& runtime_view) {
|
||||
dynamic_io_fnobj<png_write_is_supported, png_writer> op(this);
|
||||
apply_operation(runtime_view,op);
|
||||
}
|
||||
};
|
||||
|
||||
class png_type_format_checker {
|
||||
int _bit_depth;
|
||||
int _color_type;
|
||||
public:
|
||||
png_type_format_checker(int bit_depth_in,int color_type_in) :
|
||||
_bit_depth(bit_depth_in),_color_type(color_type_in) {}
|
||||
template <typename Image>
|
||||
bool apply() {
|
||||
return png_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
|
||||
png_read_support<typename Image::view_t>::color_type==_color_type;
|
||||
}
|
||||
};
|
||||
|
||||
struct png_read_is_supported {
|
||||
template<typename View> struct apply
|
||||
: public mpl::bool_<png_read_support<View>::is_supported> {};
|
||||
};
|
||||
|
||||
class png_reader_dynamic : public png_reader {
|
||||
public:
|
||||
png_reader_dynamic(FILE* file) : png_reader(file) {}
|
||||
png_reader_dynamic(const char* filename) : png_reader(filename){}
|
||||
|
||||
template <typename Images>
|
||||
void read_image(any_image<Images>& im) {
|
||||
png_uint_32 width, height;
|
||||
int bit_depth, color_type, interlace_type;
|
||||
png_get_IHDR(_png_ptr, _info_ptr,
|
||||
&width, &height,&bit_depth,&color_type,&interlace_type,
|
||||
NULL, NULL);
|
||||
if (!construct_matched(im,png_type_format_checker(bit_depth,color_type))) {
|
||||
io_error("png_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
|
||||
} else {
|
||||
im.recreate(width,height);
|
||||
dynamic_io_fnobj<png_read_is_supported, png_reader> op(this);
|
||||
apply_operation(view(im),op);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \ingroup PNG_IO
|
||||
/// \brief reads a PNG image into a run-time instantiated image
|
||||
/// Opens the given png file name, selects the first type in Images whose color space and channel are compatible to those of the image file
|
||||
/// and creates a new image of that type with the dimensions specified by the image file.
|
||||
/// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
|
||||
template <typename Images>
|
||||
inline void png_read_image(const char* filename,any_image<Images>& im) {
|
||||
detail::png_reader_dynamic m(filename);
|
||||
m.read_image(im);
|
||||
}
|
||||
|
||||
/// \ingroup PNG_IO
|
||||
/// \brief reads a PNG image into a run-time instantiated image
|
||||
template <typename Images>
|
||||
inline void png_read_image(const std::string& filename,any_image<Images>& im) {
|
||||
png_read_image(filename.c_str(),im);
|
||||
}
|
||||
|
||||
/// \ingroup PNG_IO
|
||||
/// \brief Saves the currently instantiated view to a png file specified by the given png image file name.
|
||||
/// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
|
||||
/// or if it fails to create the file.
|
||||
template <typename Views>
|
||||
inline void png_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
|
||||
detail::png_writer_dynamic m(filename);
|
||||
m.write_view(runtime_view);
|
||||
}
|
||||
|
||||
/// \ingroup PNG_IO
|
||||
/// \brief Saves the currently instantiated view to a png file specified by the given png image file name.
|
||||
template <typename Views>
|
||||
inline void png_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
|
||||
png_write_view(filename.c_str(),runtime_view);
|
||||
}
|
||||
|
||||
} } // namespace boost::gil
|
||||
|
||||
#endif
|
||||
360
include/boost/gil/extension/io/png_io_private.hpp
Normal file
360
include/boost/gil/extension/io/png_io_private.hpp
Normal file
@@ -0,0 +1,360 @@
|
||||
/*
|
||||
Copyright 2005-2007 Adobe Systems Incorporated
|
||||
|
||||
Use, modification and distribution are subject to the Boost Software License,
|
||||
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
See http://stlab.adobe.com/gil for most recent version including documentation.
|
||||
*/
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef GIL_PNG_IO_PRIVATE_H
|
||||
#define GIL_PNG_IO_PRIVATE_H
|
||||
|
||||
/// \file
|
||||
/// \brief Internal support for reading and writing PNG files
|
||||
/// \author Hailin Jin and Lubomir Bourdev \n
|
||||
/// Adobe Systems Incorporated
|
||||
/// \date 2005-2007 \n Last updated August 14, 2007
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include "../../gil_all.hpp"
|
||||
#include "io_error.hpp"
|
||||
#include <png.h>
|
||||
|
||||
namespace boost { namespace gil {
|
||||
|
||||
namespace detail {
|
||||
|
||||
static const std::size_t PNG_BYTES_TO_CHECK = 4;
|
||||
|
||||
// lbourdev: These can be greatly simplified, for example:
|
||||
template <typename Cs> struct png_color_type {BOOST_STATIC_CONSTANT(int,color_type=0);};
|
||||
template<> struct png_color_type<gray_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY); };
|
||||
template<> struct png_color_type<rgb_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB); };
|
||||
template<> struct png_color_type<rgba_t> { BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA); };
|
||||
|
||||
template <typename Channel,typename ColorSpace> struct png_is_supported {BOOST_STATIC_CONSTANT(bool,value=false);};
|
||||
template <> struct png_is_supported<bits8,gray_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
template <> struct png_is_supported<bits8,rgb_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
template <> struct png_is_supported<bits8,rgba_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
template <> struct png_is_supported<bits16,gray_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
template <> struct png_is_supported<bits16,rgb_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
template <> struct png_is_supported<bits16,rgba_t> {BOOST_STATIC_CONSTANT(bool,value=true);};
|
||||
|
||||
template <typename Channel> struct png_bit_depth {BOOST_STATIC_CONSTANT(int,bit_depth=sizeof(Channel)*8);};
|
||||
|
||||
template <typename Channel,typename ColorSpace>
|
||||
struct png_read_support_private {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=false);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=0);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=0);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits8,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits8,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits8,rgba_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits16,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits16,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
|
||||
};
|
||||
template <>
|
||||
struct png_read_support_private<bits16,rgba_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
|
||||
};
|
||||
|
||||
template <typename Channel,typename ColorSpace>
|
||||
struct png_write_support_private {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=false);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=0);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=0);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits8,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits8,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits8,rgba_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits16,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_GRAY);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits16,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGB);
|
||||
};
|
||||
template <>
|
||||
struct png_write_support_private<bits16,rgba_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PNG_COLOR_TYPE_RGBA);
|
||||
};
|
||||
|
||||
class png_reader : public file_mgr {
|
||||
protected:
|
||||
png_structp _png_ptr;
|
||||
png_infop _info_ptr;
|
||||
|
||||
void init() {
|
||||
char buf[PNG_BYTES_TO_CHECK];
|
||||
// read in some of the signature bytes
|
||||
io_error_if(fread(buf, 1, PNG_BYTES_TO_CHECK, get()) != detail::PNG_BYTES_TO_CHECK,
|
||||
"png_check_validity: fail to read file");
|
||||
// compare the first PNG_BYTES_TO_CHECK bytes of the signature.
|
||||
io_error_if(png_sig_cmp((png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK)!=0,
|
||||
"png_check_validity: invalid png file");
|
||||
|
||||
_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
|
||||
io_error_if(_png_ptr==NULL,"png_get_file_size: fail to call png_create_write_struct()");
|
||||
// allocate/initialize the image information data
|
||||
_info_ptr = png_create_info_struct(_png_ptr);
|
||||
if (_info_ptr == NULL) {
|
||||
png_destroy_read_struct(&_png_ptr,NULL,NULL);
|
||||
io_error("png_get_file_size: fail to call png_create_info_struct()");
|
||||
}
|
||||
if (setjmp(png_jmpbuf(_png_ptr))) {
|
||||
//free all of the memory associated with the png_ptr and info_ptr
|
||||
png_destroy_read_struct(&_png_ptr, &_info_ptr, NULL);
|
||||
io_error("png_get_file_size: fail to call setjmp()");
|
||||
}
|
||||
png_init_io(_png_ptr, get());
|
||||
png_set_sig_bytes(_png_ptr,PNG_BYTES_TO_CHECK);
|
||||
png_read_info(_png_ptr, _info_ptr);
|
||||
if (little_endian() && png_get_bit_depth(_png_ptr,_info_ptr)>8)
|
||||
png_set_swap(_png_ptr);
|
||||
}
|
||||
public:
|
||||
png_reader(FILE* file ) : file_mgr(file) { init(); }
|
||||
png_reader(const char* filename) : file_mgr(filename, "rb") { init(); }
|
||||
|
||||
~png_reader() {
|
||||
png_destroy_read_struct(&_png_ptr,&_info_ptr,NULL);
|
||||
}
|
||||
point2<std::ptrdiff_t> get_dimensions() {
|
||||
return point2<std::ptrdiff_t>(png_get_image_width(_png_ptr,_info_ptr),
|
||||
png_get_image_height(_png_ptr,_info_ptr));
|
||||
}
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
png_uint_32 width, height;
|
||||
int bit_depth, color_type, interlace_type;
|
||||
png_get_IHDR(_png_ptr, _info_ptr,
|
||||
&width, &height,&bit_depth,&color_type,&interlace_type,
|
||||
NULL, NULL);
|
||||
io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
|
||||
"png_read_view: input view size does not match PNG file size");
|
||||
|
||||
if(png_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth!=bit_depth ||
|
||||
png_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type!=color_type)
|
||||
io_error("png_read_view: input view type is incompatible with the image type");
|
||||
|
||||
std::vector<pixel<typename channel_type<View>::type,
|
||||
layout<typename color_space_type<View>::type> > > row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::copy(row.begin(),row.end(),view.row_begin(y));
|
||||
}
|
||||
png_read_end(_png_ptr,NULL);
|
||||
}
|
||||
|
||||
template <typename Image>
|
||||
void read_image(Image& im) {
|
||||
im.recreate(get_dimensions());
|
||||
apply(view(im));
|
||||
}
|
||||
};
|
||||
|
||||
// This code will be simplified...
|
||||
template <typename CC>
|
||||
class png_reader_color_convert : public png_reader {
|
||||
private:
|
||||
CC _cc;
|
||||
public:
|
||||
png_reader_color_convert(FILE* file ,CC cc_in) : png_reader(file),_cc(cc_in) {}
|
||||
png_reader_color_convert(FILE* file ) : png_reader(file) {}
|
||||
png_reader_color_convert(const char* filename,CC cc_in) : png_reader(filename),_cc(cc_in) {}
|
||||
png_reader_color_convert(const char* filename) : png_reader(filename) {}
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
png_uint_32 width, height;
|
||||
int bit_depth, color_type, interlace_type;
|
||||
png_get_IHDR(_png_ptr, _info_ptr,
|
||||
&width, &height,&bit_depth,&color_type,&interlace_type,
|
||||
int_p_NULL, int_p_NULL);
|
||||
io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
|
||||
"png_reader_color_convert::apply(): input view size does not match PNG file size");
|
||||
switch (color_type) {
|
||||
case PNG_COLOR_TYPE_GRAY:
|
||||
switch (bit_depth) {
|
||||
case 8: {
|
||||
std::vector<gray8_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray8_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 16: {
|
||||
std::vector<gray16_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<gray16_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
|
||||
}
|
||||
break;
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
switch (bit_depth) {
|
||||
case 8: {
|
||||
std::vector<rgb8_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb8_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 16: {
|
||||
std::vector<rgb16_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgb16_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
|
||||
}
|
||||
break;
|
||||
case PNG_COLOR_TYPE_RGBA:
|
||||
switch (bit_depth) {
|
||||
case 8: {
|
||||
std::vector<rgba8_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba8_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 16: {
|
||||
std::vector<rgba16_pixel_t> row(width);
|
||||
for(png_uint_32 y=0;y<height;++y) {
|
||||
png_read_row(_png_ptr,(png_bytep)&row.front(),NULL);
|
||||
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<rgba16_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
|
||||
}
|
||||
break;
|
||||
default: io_error("png_reader_color_convert::apply(): unknown color type");
|
||||
}
|
||||
png_read_end(_png_ptr,NULL);
|
||||
}
|
||||
template <typename Image>
|
||||
void read_image(Image& im) {
|
||||
im.recreate(get_dimensions());
|
||||
apply(view(im));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class png_writer : public file_mgr {
|
||||
protected:
|
||||
png_structp _png_ptr;
|
||||
png_infop _info_ptr;
|
||||
|
||||
void init() {
|
||||
_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
|
||||
io_error_if(!_png_ptr,"png_write_initialize: fail to call png_create_write_struct()");
|
||||
_info_ptr = png_create_info_struct(_png_ptr);
|
||||
if (!_info_ptr) {
|
||||
png_destroy_write_struct(&_png_ptr,NULL);
|
||||
io_error("png_write_initialize: fail to call png_create_info_struct()");
|
||||
}
|
||||
if (setjmp(png_jmpbuf(_png_ptr))) {
|
||||
png_destroy_write_struct(&_png_ptr, &_info_ptr);
|
||||
io_error("png_write_initialize: fail to call setjmp(png_jmpbuf())");
|
||||
}
|
||||
png_init_io(_png_ptr,get());
|
||||
}
|
||||
public:
|
||||
png_writer(FILE* file ) : file_mgr(file) { init(); }
|
||||
png_writer(const char* filename) : file_mgr(filename, "wb") { init(); }
|
||||
|
||||
~png_writer() {
|
||||
png_destroy_write_struct(&_png_ptr,&_info_ptr);
|
||||
}
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
png_set_IHDR(_png_ptr, _info_ptr, view.width(), view.height(),
|
||||
png_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth,
|
||||
png_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type,
|
||||
PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
|
||||
png_write_info(_png_ptr,_info_ptr);
|
||||
if (little_endian() &&
|
||||
png_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth>8)
|
||||
png_set_swap(_png_ptr);
|
||||
std::vector<pixel<typename channel_type<View>::type,
|
||||
layout<typename color_space_type<View>::type> > > row(view.width());
|
||||
for(int y=0;y<view.height();++y) {
|
||||
std::copy(view.row_begin(y),view.row_end(y),row.begin());
|
||||
png_write_row(_png_ptr,(png_bytep)&row.front());
|
||||
}
|
||||
png_write_end(_png_ptr,_info_ptr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} } // namespace boost::gil
|
||||
|
||||
#endif
|
||||
511
include/boost/gil/extension/io/tiff_io.hpp
Normal file
511
include/boost/gil/extension/io/tiff_io.hpp
Normal file
@@ -0,0 +1,511 @@
|
||||
/*
|
||||
Copyright 2005-2007 Adobe Systems Incorporated
|
||||
|
||||
Use, modification and distribution are subject to the Boost Software License,
|
||||
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
See http://opensource.adobe.com/gil for most recent version including documentation.
|
||||
*/
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
#ifndef GIL_TIFF_IO_H
|
||||
#define GIL_TIFF_IO_H
|
||||
|
||||
/// \file
|
||||
/// \brief Support for reading and writing TIFF files
|
||||
/// Requires libtiff!
|
||||
/// \author Hailin Jin and Lubomir Bourdev \n
|
||||
/// Adobe Systems Incorporated
|
||||
/// \date 2005-2007 \n Last updated September 24, 2006
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <tiffio.h>
|
||||
#include "../../gil_all.hpp"
|
||||
#include "io_error.hpp"
|
||||
|
||||
namespace boost { namespace gil {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename Channel,typename ColorSpace>
|
||||
struct tiff_read_support_private {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=false);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=0);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=0);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits8,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits8,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits16,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits16,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits32f,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=32);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_read_support_private<bits32f,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=32);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
|
||||
template <typename Channel,typename ColorSpace>
|
||||
struct tiff_write_support_private {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=false);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=0);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=0);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits8,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits8,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=8);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits16,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits16,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=16);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits32f,gray_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=32);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_MINISBLACK);
|
||||
};
|
||||
template <>
|
||||
struct tiff_write_support_private<bits32f,rgb_t> {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=true);
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=32);
|
||||
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
|
||||
};
|
||||
|
||||
class tiff_reader {
|
||||
protected:
|
||||
TIFF *_tp;
|
||||
public:
|
||||
tiff_reader(const char* filename,tdir_t dirnum=0) {
|
||||
io_error_if((_tp=TIFFOpen(filename,"r"))==NULL,
|
||||
"tiff_reader: fail to open file");
|
||||
if(dirnum>0) {
|
||||
io_error_if(TIFFSetDirectory(_tp,dirnum)!=1,
|
||||
"tiff_reader: fail to set directory");
|
||||
}
|
||||
}
|
||||
~tiff_reader() { TIFFClose(_tp); }
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
unsigned short bps,photometric;
|
||||
point2<std::ptrdiff_t> dims=get_dimensions();
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps)!=1);
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric)!=1);
|
||||
io_error_if(dims!=view.dimensions(),
|
||||
"tiff_read_view: input view size does not match TIFF file size");
|
||||
io_error_if(tiff_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth!=bps ||
|
||||
tiff_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type!=photometric,
|
||||
"tiff_read_view: input view type is incompatible with the image type");
|
||||
std::size_t element_size=sizeof(pixel<typename channel_type<View>::type,
|
||||
layout<typename color_space_type<View>::type> >);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<pixel<typename channel_type<View>::type,
|
||||
layout<typename color_space_type<View>::type> > > row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::copy(row.begin(),row.begin()+view.width(),view.row_begin(y));
|
||||
}
|
||||
}
|
||||
point2<std::ptrdiff_t> get_dimensions() {
|
||||
int w,h;
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH, &w)!=1);
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&h)!=1);
|
||||
return point2<std::ptrdiff_t>(w,h);
|
||||
}
|
||||
|
||||
template <typename Image>
|
||||
void read_image(Image& im) {
|
||||
im.recreate(get_dimensions());
|
||||
apply(view(im));
|
||||
}
|
||||
};
|
||||
|
||||
// This code will be simplified...
|
||||
template <typename CC>
|
||||
class tiff_reader_color_convert : public tiff_reader {
|
||||
private:
|
||||
CC _cc;
|
||||
public:
|
||||
tiff_reader_color_convert(const char* filename,tdir_t dirnum=0) :
|
||||
tiff_reader(filename,dirnum) {}
|
||||
tiff_reader_color_convert(const char* filename,CC cc_in,tdir_t dirnum=0) :
|
||||
tiff_reader(filename,dirnum),_cc(cc_in) {}
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
point2<std::ptrdiff_t> dims=get_dimensions();
|
||||
unsigned short bps,photometric;
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps)!=1);
|
||||
io_error_if(TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric)!=1);
|
||||
io_error_if(dims!=view.dimensions(),
|
||||
"tiff_reader_color_convert::apply(): input view size does not match TIFF file size");
|
||||
switch (photometric) {
|
||||
case PHOTOMETRIC_MINISBLACK: {
|
||||
switch (bps) {
|
||||
case 8: {
|
||||
std::size_t element_size=sizeof(gray8_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<gray8_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<gray8_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 16: {
|
||||
std::size_t element_size=sizeof(gray16_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<gray16_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<gray16_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 32: {
|
||||
std::size_t element_size=sizeof(gray32f_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<gray32f_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<gray32f_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
io_error("tiff_reader_color_convert::apply(): unknown combination of color type and bit depth");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PHOTOMETRIC_RGB: {
|
||||
switch (bps) {
|
||||
case 8: {
|
||||
std::size_t element_size=sizeof(rgb8_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<rgb8_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<rgb8_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 16: {
|
||||
std::size_t element_size=sizeof(rgb16_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<rgb16_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<rgb16_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 32: {
|
||||
std::size_t element_size=sizeof(rgb32f_pixel_t);
|
||||
std::size_t size_to_allocate = (std::max)((std::size_t)view.width(),
|
||||
(std::size_t)(TIFFScanlineSize(_tp)+element_size-1)/element_size);
|
||||
std::vector<rgb32f_pixel_t> row(size_to_allocate);
|
||||
for (int y=0;y<view.height();++y) {
|
||||
io_error_if(TIFFReadScanline(_tp,&row.front(), y)!=1);
|
||||
std::transform(row.begin(),row.begin()+view.width(),view.row_begin(y),
|
||||
color_convert_deref_fn<rgb32f_ref_t,typename View::value_type,CC>(_cc));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
io_error("tiff_reader_color_convert::apply(): unknown combination of color type and bit depth");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// reads an image in incompatible format via TIFFReadRGBAImage
|
||||
rgba8_image_t rgbaImg(dims);
|
||||
io_error_if(!TIFFReadRGBAImage(_tp, dims.x, dims.y, (uint32*)&gil::view(rgbaImg)(0,0), 0),
|
||||
"tiff_reader_color_convert::unsupported image format");
|
||||
copy_and_convert_pixels(flipped_up_down_view(const_view(rgbaImg)), view, _cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename Image>
|
||||
void read_image(Image& im) {
|
||||
im.recreate(get_dimensions());
|
||||
apply(view(im));
|
||||
}
|
||||
};
|
||||
|
||||
class tiff_writer {
|
||||
protected:
|
||||
TIFF* _tp;
|
||||
public:
|
||||
tiff_writer(const char *filename) {
|
||||
io_error_if((_tp=TIFFOpen(filename,"w"))==NULL,
|
||||
"tiff_writer: fail to open file");
|
||||
}
|
||||
~tiff_writer() {TIFFClose(_tp);}
|
||||
template <typename View>
|
||||
void apply(const View& view) {
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_IMAGELENGTH, view.height())!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_IMAGEWIDTH, view.width())!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_PHOTOMETRIC, tiff_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_SAMPLESPERPIXEL,num_channels<View>::value)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_BITSPERSAMPLE, tiff_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth)!=1);
|
||||
io_error_if(TIFFSetField(_tp,TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(_tp, 0))!=1);
|
||||
std::vector<pixel<typename channel_type<View>::type,
|
||||
layout<typename color_space_type<View>::type> > > row(view.width());
|
||||
for (int y=0;y<view.height();++y) {
|
||||
std::copy(view.row_begin(y),view.row_end(y),row.begin());
|
||||
io_error_if(TIFFWriteScanline(_tp,&row.front(),y,0)!=1,
|
||||
"tiff_write_view: fail to write file");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Determines whether the given view type is supported for reading
|
||||
template <typename View>
|
||||
struct tiff_read_support {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=
|
||||
(detail::tiff_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::is_supported));
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=
|
||||
(detail::tiff_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth));
|
||||
BOOST_STATIC_CONSTANT(int,color_type=
|
||||
(detail::tiff_read_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type));
|
||||
};
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Returns the number of directories in the TIFF file
|
||||
inline int tiff_get_directory_count(const char* filename) {
|
||||
TIFF *tif;
|
||||
io_error_if((tif=TIFFOpen(filename,"r"))==NULL,
|
||||
"tiff_get_count: fail to open file");
|
||||
|
||||
int dircount = 0;
|
||||
do {
|
||||
dircount++;
|
||||
} while (TIFFReadDirectory(tif));
|
||||
|
||||
TIFFClose(tif);
|
||||
return dircount;
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Returns the width and height of the TIFF file at the specified location.
|
||||
/// Throws std::ios_base::failure if the location does not correspond to a valid TIFF file
|
||||
inline point2<std::ptrdiff_t> tiff_read_dimensions(const char* filename,tdir_t dirnum=0) {
|
||||
detail::tiff_reader m(filename,dirnum);
|
||||
return m.get_dimensions();
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Returns the width and height of the TIFF file at the specified location.
|
||||
/// Throws std::ios_base::failure if the location does not correspond to a valid TIFF file
|
||||
inline point2<std::ptrdiff_t> tiff_read_dimensions(const std::string& filename,tdir_t dirnum=0) {
|
||||
return tiff_read_dimensions(filename.c_str(),dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads the image specified by the given tiff image file name into the given view.
|
||||
/// Triggers a compile assert if the view color space and channel depth are not supported by the TIFF library or by the I/O extension.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
|
||||
/// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
|
||||
template <typename View>
|
||||
inline void tiff_read_view(const char* filename,const View& view,tdir_t dirnum=0) {
|
||||
BOOST_STATIC_ASSERT(tiff_read_support<View>::is_supported);
|
||||
detail::tiff_reader m(filename,dirnum);
|
||||
m.apply(view);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads the image specified by the given tiff image file name into the given view.
|
||||
template <typename View>
|
||||
inline void tiff_read_view(const std::string& filename,const View& view,tdir_t dirnum=0) {
|
||||
tiff_read_view(filename.c_str(),view,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it.
|
||||
/// Triggers a compile assert if the image color space or channel depth are not supported by the TIFF library or by the I/O extension.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
|
||||
/// compatible with the ones specified by Image
|
||||
template <typename Image>
|
||||
void tiff_read_image(const char* filename,Image& im,tdir_t dirnum=0) {
|
||||
BOOST_STATIC_ASSERT(tiff_read_support<typename Image::view_t>::is_supported);
|
||||
detail::tiff_reader m(filename,dirnum);
|
||||
m.read_image(im);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it.
|
||||
template <typename Image>
|
||||
inline void tiff_read_image(const std::string& filename,Image& im,tdir_t dirnum=0) {
|
||||
tiff_read_image(filename.c_str(),im,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.
|
||||
template <typename View,typename CC>
|
||||
inline void tiff_read_and_convert_view(const char* filename,const View& view,CC cc,tdir_t dirnum=0) {
|
||||
detail::tiff_reader_color_convert<CC> m(filename,cc,dirnum);
|
||||
m.apply(view);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its dimensions don't match the ones of the view.
|
||||
template <typename View>
|
||||
inline void tiff_read_and_convert_view(const char* filename,const View& view,tdir_t dirnum=0) {
|
||||
detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter(),dirnum);
|
||||
m.apply(view);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
|
||||
template <typename View,typename CC>
|
||||
inline void tiff_read_and_convert_view(const std::string& filename,const View& view,CC cc,tdir_t dirnum=0) {
|
||||
tiff_read_and_convert_view(filename.c_str(),view,cc,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Loads and color-converts the image specified by the given tiff image file name into the given view.
|
||||
template <typename View>
|
||||
inline void tiff_read_and_convert_view(const std::string& filename,const View& view,tdir_t dirnum=0) {
|
||||
tiff_read_and_convert_view(filename.c_str(),view,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file
|
||||
template <typename Image,typename CC>
|
||||
void tiff_read_and_convert_image(const char* filename,Image& im,CC cc,tdir_t dirnum=0) {
|
||||
detail::tiff_reader_color_convert<CC> m(filename,cc,dirnum);
|
||||
m.read_image(im);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
|
||||
/// Throws std::ios_base::failure if the file is not a valid TIFF file
|
||||
template <typename Image>
|
||||
void tiff_read_and_convert_image(const char* filename,Image& im,tdir_t dirnum=0) {
|
||||
detail::tiff_reader_color_convert<default_color_converter> m(filename,default_color_converter(),dirnum);
|
||||
m.read_image(im);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
|
||||
template <typename Image,typename CC>
|
||||
inline void tiff_read_and_convert_image(const std::string& filename,Image& im,CC cc,tdir_t dirnum=0) {
|
||||
tiff_read_and_convert_image(filename.c_str(),im,cc,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, loads and color-converts the pixels into it.
|
||||
template <typename Image>
|
||||
inline void tiff_read_and_convert_image(const std::string& filename,Image& im,tdir_t dirnum=0) {
|
||||
tiff_read_and_convert_image(filename.c_str(),im,dirnum);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Determines whether the given view type is supported for writing
|
||||
template <typename View>
|
||||
struct tiff_write_support {
|
||||
BOOST_STATIC_CONSTANT(bool,is_supported=
|
||||
(detail::tiff_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::is_supported));
|
||||
BOOST_STATIC_CONSTANT(int,bit_depth=
|
||||
(detail::tiff_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::bit_depth));
|
||||
BOOST_STATIC_CONSTANT(int,color_type=
|
||||
(detail::tiff_write_support_private<typename channel_type<View>::type,
|
||||
typename color_space_type<View>::type>::color_type));
|
||||
BOOST_STATIC_CONSTANT(bool, value=is_supported);
|
||||
};
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Saves the view to a tiff file specified by the given tiff image file name.
|
||||
/// Triggers a compile assert if the view color space and channel depth are not supported by the TIFF library or by the I/O extension.
|
||||
/// Throws std::ios_base::failure if it fails to create the file.
|
||||
template <typename View>
|
||||
inline void tiff_write_view(const char* filename,const View& view) {
|
||||
BOOST_STATIC_ASSERT(tiff_write_support<View>::is_supported);
|
||||
detail::tiff_writer m(filename);
|
||||
m.apply(view);
|
||||
}
|
||||
|
||||
/// \ingroup TIFF_IO
|
||||
/// \brief Saves the view to a tiff file specified by the given tiff image file name.
|
||||
template <typename View>
|
||||
inline void tiff_write_view(const std::string& filename,const View& view) {
|
||||
tiff_write_view(filename.c_str(),view);
|
||||
}
|
||||
|
||||
} } // namespace boost::gil
|
||||
|
||||
#endif
|
||||
@@ -55,7 +55,11 @@ namespace boost { namespace gil {
|
||||
///
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
<<<<<<< HEAD
|
||||
template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
|
||||
=======
|
||||
template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
class image {
|
||||
public:
|
||||
typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
|
||||
@@ -351,11 +355,16 @@ private:
|
||||
|
||||
std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
|
||||
|
||||
<<<<<<< HEAD
|
||||
typedef typename view_t::x_iterator x_iterator;
|
||||
=======
|
||||
typedef typename view_t::x_iterator x_iterator;
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
|
||||
// when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
|
||||
const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
|
||||
, num_channels< view_t >
|
||||
<<<<<<< HEAD
|
||||
, mpl::int_< 1 >
|
||||
>::type::value;
|
||||
|
||||
@@ -367,6 +376,19 @@ private:
|
||||
// return the size rounded up to the nearest byte
|
||||
return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
|
||||
/ byte_to_memunit<x_iterator>::value
|
||||
=======
|
||||
, mpl::int_< 1 >
|
||||
>::type::value;
|
||||
|
||||
std::size_t size_in_units = get_row_size_in_memunits(dimensions.x)*dimensions.y;
|
||||
|
||||
if (IsPlanar)
|
||||
size_in_units = size_in_units * _channels_in_image ;
|
||||
|
||||
// return the size rounded up to the nearest byte
|
||||
return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
|
||||
/ byte_to_memunit<x_iterator>::value
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
+ ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
|
||||
}
|
||||
|
||||
|
||||
@@ -81,9 +81,13 @@ struct packed_pixel {
|
||||
}
|
||||
packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) {
|
||||
BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==3));
|
||||
<<<<<<< HEAD
|
||||
gil::at_c<0>(*this) = chan0;
|
||||
gil::at_c<1>(*this) = chan1;
|
||||
gil::at_c<2>(*this) = chan2;
|
||||
=======
|
||||
gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2;
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
}
|
||||
packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) {
|
||||
BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==4));
|
||||
|
||||
@@ -117,7 +117,11 @@ private:
|
||||
void advance(std::ptrdiff_t d) { static_transform(*this,*this,std::bind2nd(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),d)); }
|
||||
reference dereference() const { return this->template deref<reference>(); }
|
||||
|
||||
<<<<<<< HEAD
|
||||
std::ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
|
||||
=======
|
||||
ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
bool equal(const planar_pixel_iterator& it) const { return gil::at_c<0>(*this)==gil::at_c<0>(it); }
|
||||
};
|
||||
|
||||
@@ -181,8 +185,13 @@ template <typename IC, typename C>
|
||||
inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
|
||||
|
||||
template <typename IC, typename C>
|
||||
<<<<<<< HEAD
|
||||
inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
|
||||
return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
|
||||
=======
|
||||
inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
|
||||
return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
}
|
||||
|
||||
template <typename IC>
|
||||
|
||||
@@ -128,7 +128,11 @@ void test_pixel_iterator() {
|
||||
buf[2] = 219;
|
||||
|
||||
iterator_t it( &buf[0], 0 );
|
||||
<<<<<<< HEAD
|
||||
|
||||
=======
|
||||
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
ref_t p1 = *it; it++;
|
||||
ref_t p2 = *it; it++;
|
||||
ref_t p3 = *it; it++;
|
||||
@@ -148,6 +152,7 @@ void test_pixel_iterator() {
|
||||
unsigned char v8 = get_color( p8, gray_color_t() );
|
||||
|
||||
// all values should be 110b ( 6 );
|
||||
<<<<<<< HEAD
|
||||
assert( v1 == 6 );
|
||||
assert( v2 == 6 );
|
||||
assert( v3 == 6 );
|
||||
@@ -156,6 +161,16 @@ void test_pixel_iterator() {
|
||||
assert( v6 == 6 );
|
||||
assert( v7 == 6 );
|
||||
assert( v8 == 6 );
|
||||
=======
|
||||
//assert( v1 == 6 );
|
||||
//assert( v2 == 6 );
|
||||
//assert( v3 == 6 );
|
||||
//assert( v4 == 6 );
|
||||
//assert( v5 == 6 );
|
||||
//assert( v6 == 6 );
|
||||
//assert( v7 == 6 );
|
||||
//assert( v8 == 6 );
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
}
|
||||
|
||||
// TODO: Make better tests. Use some code from below.
|
||||
@@ -337,6 +352,10 @@ BOOST_AUTO_TEST_SUITE(GIL_Tests)
|
||||
BOOST_AUTO_TEST_CASE(pixel_iterator_test)
|
||||
{
|
||||
test_pixel_iterator();
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
return 0;
|
||||
>>>>>>> 325700ffa2810faea2f41c77eded9992cbc7e91e
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user