Added boost header

This commit is contained in:
Christophe Riccio
2012-01-08 01:26:07 +00:00
parent 9c3faaca40
commit c7d752cdf8
8946 changed files with 1732316 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
/*
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_DYNAMIC_IO_H
#define GIL_DYNAMIC_IO_H
/// \file
/// \brief Generic io functions for dealing with dynamic images
//
/// \author Hailin Jin and Lubomir Bourdev \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated May 30, 2006
#include <boost/mpl/at.hpp>
#include <boost/mpl/size.hpp>
#include "../../gil_config.hpp"
#include "io_error.hpp"
#include "../dynamic_image/any_image.hpp"
namespace boost { namespace gil {
namespace detail {
template <long N>
struct construct_matched_t {
template <typename Images,typename Pred>
static bool apply(any_image<Images>& im,Pred pred) {
if (pred.template apply<typename mpl::at_c<Images,N-1>::type>()) {
typename mpl::at_c<Images,N-1>::type x;
im.move_in(x);
return true;
} else return construct_matched_t<N-1>::apply(im,pred);
}
};
template <>
struct construct_matched_t<0> {
template <typename Images,typename Pred>
static bool apply(any_image<Images>&,Pred) {return false;}
};
// A function object that can be passed to apply_operation.
// Given a predicate IsSupported taking a view type and returning an MPL boolean,
// calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
template <typename IsSupported, typename OpClass>
class dynamic_io_fnobj {
OpClass* _op;
template <typename View>
void apply(const View& view,mpl::true_ ) {_op->apply(view);}
template <typename View>
void apply(const View& view,mpl::false_) {io_error("dynamic_io: unsupported view type for the given file format");}
public:
dynamic_io_fnobj(OpClass* op) : _op(op) {}
typedef void result_type;
template <typename View>
void operator()(const View& view) {apply(view,typename IsSupported::template apply<View>::type());}
};
} // namespace detail
/// \brief Within the any_image, constructs an image with the given dimensions
/// and a type that satisfies the given predicate
template <typename Images,typename Pred>
inline bool construct_matched(any_image<Images>& im,Pred pred) {
return detail::construct_matched_t<mpl::size<Images>::value>::apply(im,pred);
}
} } // namespace boost::gil
#endif

View File

@@ -0,0 +1,51 @@
/*
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_IO_ERROR_H
#define GIL_IO_ERROR_H
/// \file
/// \brief Handle input-output errors
/// \author Lubomir Bourdev and Hailin Jin \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated on May 30, 2006
#include <ios>
#include "../../gil_config.hpp"
#include <boost/shared_ptr.hpp>
namespace boost { namespace gil {
inline void io_error(const char* descr) { throw std::ios_base::failure(descr); }
inline void io_error_if(bool expr, const char* descr="") { if (expr) io_error(descr); }
namespace detail {
class file_mgr {
protected:
shared_ptr<FILE> _fp;
struct null_deleter { void operator()(void const*) const {} };
file_mgr(FILE* file) : _fp(file, null_deleter()) {}
file_mgr(const char* filename, const char* flags) {
FILE* fp;
io_error_if((fp=fopen(filename,flags))==NULL, "file_mgr: failed to open file");
_fp=shared_ptr<FILE>(fp,fclose);
}
public:
FILE* get() { return _fp.get(); }
};
}
} } // namespace boost::gil
#endif

View File

@@ -0,0 +1,130 @@
/*
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_JPEG_DYNAMIC_IO_H
#define GIL_JPEG_DYNAMIC_IO_H
/// \file
/// \brief Support for reading and writing JPEG files
/// Requires libjpeg
///
/// \author Hailin Jin and Lubomir Bourdev \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated June 10, 2006
#include <stdio.h>
#include <string>
#include <boost/mpl/bool.hpp>
#include <boost/shared_ptr.hpp>
#include "../dynamic_image/dynamic_image_all.hpp"
#include "io_error.hpp"
#include "jpeg_io.hpp"
#include "jpeg_io_private.hpp"
#include "dynamic_io.hpp"
namespace boost { namespace gil {
namespace detail {
struct jpeg_write_is_supported {
template<typename View> struct apply
: public mpl::bool_<jpeg_write_support<View>::is_supported> {};
};
class jpeg_writer_dynamic : public jpeg_writer {
int _quality;
public:
jpeg_writer_dynamic(FILE* file, int quality=100) : jpeg_writer(file) , _quality(quality) {}
jpeg_writer_dynamic(const char* filename, int quality=100) : jpeg_writer(filename), _quality(quality) {}
template <typename Views>
void write_view(const any_image_view<Views>& runtime_view) {
dynamic_io_fnobj<jpeg_write_is_supported, jpeg_writer> op(this);
apply_operation(runtime_view,op);
}
};
class jpeg_type_format_checker {
J_COLOR_SPACE _color_type;
public:
jpeg_type_format_checker(J_COLOR_SPACE color_type_in) :
_color_type(color_type_in) {}
template <typename Image>
bool apply() {
return jpeg_read_support<typename Image::view_t>::color_type==_color_type;
}
};
struct jpeg_read_is_supported {
template<typename View> struct apply
: public mpl::bool_<jpeg_read_support<View>::is_supported> {};
};
class jpeg_reader_dynamic : public jpeg_reader {
public:
jpeg_reader_dynamic(FILE* file) : jpeg_reader(file) {}
jpeg_reader_dynamic(const char* filename) : jpeg_reader(filename){}
template <typename Images>
void read_image(any_image<Images>& im) {
if (!construct_matched(im,detail::jpeg_type_format_checker(_cinfo.out_color_space))) {
io_error("jpeg_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
} else {
im.recreate(get_dimensions());
dynamic_io_fnobj<jpeg_read_is_supported, jpeg_reader> op(this);
apply_operation(view(im),op);
}
}
};
} // namespace detail
/// \ingroup JPEG_IO
/// \brief reads a JPEG image into a run-time instantiated image
/// Opens the given JPEG 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 jpeg_read_image(const char* filename,any_image<Images>& im) {
detail::jpeg_reader_dynamic m(filename);
m.read_image(im);
}
/// \ingroup JPEG_IO
/// \brief reads a JPEG image into a run-time instantiated image
template <typename Images>
inline void jpeg_read_image(const std::string& filename,any_image<Images>& im) {
jpeg_read_image(filename.c_str(),im);
}
/// \ingroup JPEG_IO
/// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg 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 jpeg_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
detail::jpeg_writer_dynamic m(filename);
m.write_view(runtime_view);
}
/// \ingroup JPEG_IO
/// \brief Saves the currently instantiated view to a jpeg file specified by the given jpeg image file name.
template <typename Views>
inline void jpeg_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
jpeg_write_view(filename.c_str(),runtime_view);
}
} } // namespace boost::gil
#endif

View File

@@ -0,0 +1,202 @@
/*
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_JPEG_IO_H
#define GIL_JPEG_IO_H
/// \file
/// \brief Support for reading and writing JPEG files
/// Requires libjpeg
/// \author Hailin Jin and Lubomir Bourdev \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated September 24, 2006
#include <cstdio>
#include <algorithm>
#include <string>
#include <boost/static_assert.hpp>
#include <boost/shared_ptr.hpp>
extern "C" {
#include <jpeglib.h>
}
#include "io_error.hpp"
#include "jpeg_io_private.hpp"
namespace boost { namespace gil {
/// \ingroup JPEG_IO
/// \brief Determines whether the given view type is supported for reading
template <typename View>
struct jpeg_read_support {
BOOST_STATIC_CONSTANT(bool,is_supported=
(detail::jpeg_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::is_supported));
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
(detail::jpeg_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type));
BOOST_STATIC_CONSTANT(bool, value=is_supported);
};
/// \ingroup JPEG_IO
/// \brief Returns the width and height of the JPEG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
inline point2<std::ptrdiff_t> jpeg_read_dimensions(const char* filename) {
detail::jpeg_reader m(filename);
return m.get_dimensions();
}
/// \ingroup JPEG_IO
/// \brief Returns the width and height of the JPEG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
inline point2<std::ptrdiff_t> jpeg_read_dimensions(const std::string& filename) {
return jpeg_read_dimensions(filename.c_str());
}
/// \ingroup JPEG_IO
/// \brief Loads the image specified by the given jpeg image file name into the given view.
/// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid JPEG 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 jpeg_read_view(const char* filename,const View& view) {
BOOST_STATIC_ASSERT(jpeg_read_support<View>::is_supported);
detail::jpeg_reader m(filename);
m.apply(view);
}
/// \ingroup JPEG_IO
/// \brief Loads the image specified by the given jpeg image file name into the given view.
template <typename View>
inline void jpeg_read_view(const std::string& filename,const View& view) {
jpeg_read_view(filename.c_str(),view);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg 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 JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not
/// compatible with the ones specified by Image
template <typename Image>
inline void jpeg_read_image(const char* filename,Image& im) {
BOOST_STATIC_ASSERT(jpeg_read_support<typename Image::view_t>::is_supported);
detail::jpeg_reader m(filename);
m.read_image(im);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it.
template <typename Image>
inline void jpeg_read_image(const std::string& filename,Image& im) {
jpeg_read_image(filename.c_str(),im);
}
/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
template <typename View,typename CC>
inline void jpeg_read_and_convert_view(const char* filename,const View& view,CC cc) {
detail::jpeg_reader_color_convert<CC> m(filename,cc);
m.apply(view);
}
/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
template <typename View>
inline void jpeg_read_and_convert_view(const char* filename,const View& view) {
detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
m.apply(view);
}
/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
template <typename View,typename CC>
inline void jpeg_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
jpeg_read_and_convert_view(filename.c_str(),view);
}
/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
template <typename View>
inline void jpeg_read_and_convert_view(const std::string& filename,const View& view) {
jpeg_read_and_convert_view(filename.c_str(),view);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid JPEG file
template <typename Image,typename CC>
inline void jpeg_read_and_convert_image(const char* filename,Image& im,CC cc) {
detail::jpeg_reader_color_convert<CC> m(filename,cc);
m.read_image(im);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid JPEG file
template <typename Image>
inline void jpeg_read_and_convert_image(const char* filename,Image& im) {
detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
m.read_image(im);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
template <typename Image,typename CC>
inline void jpeg_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
jpeg_read_and_convert_image(filename.c_str(),im);
}
/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
template <typename Image>
inline void jpeg_read_and_convert_image(const std::string& filename,Image& im) {
jpeg_read_and_convert_image(filename.c_str(),im);
}
/// \ingroup JPEG_IO
/// \brief Determines whether the given view type is supported for writing
template <typename View>
struct jpeg_write_support {
BOOST_STATIC_CONSTANT(bool,is_supported=
(detail::jpeg_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::is_supported));
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
(detail::jpeg_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type));
BOOST_STATIC_CONSTANT(bool, value=is_supported);
};
/// \ingroup JPEG_IO
/// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
/// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if it fails to create the file.
template <typename View>
inline void jpeg_write_view(const char* filename,const View& view,int quality=100) {
BOOST_STATIC_ASSERT(jpeg_write_support<View>::is_supported);
detail::jpeg_writer m(filename);
m.apply(view,quality);
}
/// \ingroup JPEG_IO
/// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
template <typename View>
inline void jpeg_write_view(const std::string& filename,const View& view,int quality=100) {
jpeg_write_view(filename.c_str(),view,quality);
}
} } // namespace boost::gil
#endif

View File

@@ -0,0 +1,226 @@
/*
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_JPEG_IO_PRIVATE_H
#define GIL_JPEG_IO_PRIVATE_H
/// \file
/// \brief Internal support for reading and writing JPEG files
/// \author Hailin Jin and Lubomir Bourdev \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated September 24, 2006
#include <stdio.h>
#include <boost/static_assert.hpp>
#include <vector>
#include "../../gil_all.hpp"
#include "io_error.hpp"
#include <jpeglib.h>
namespace boost { namespace gil {
namespace detail {
// lbourdev: What is the advantage of having channel and colorspace together? Are there cases where they are interrelated?
template <typename Channel,typename ColorSpace>
struct jpeg_read_support_private {
BOOST_STATIC_CONSTANT(bool,is_supported=false);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
};
template <>
struct jpeg_read_support_private<bits8,gray_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
};
template <>
struct jpeg_read_support_private<bits8,rgb_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
};
template <>
struct jpeg_read_support_private<bits8,cmyk_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
};
template <typename Channel,typename ColorSpace>
struct jpeg_write_support_private {
BOOST_STATIC_CONSTANT(bool,is_supported=false);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
};
template <>
struct jpeg_write_support_private<bits8,gray_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
};
template <>
struct jpeg_write_support_private<bits8,rgb_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
};
template <>
struct jpeg_write_support_private<bits8,cmyk_t> {
BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
BOOST_STATIC_CONSTANT(bool,is_supported=true);
BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
};
class jpeg_reader : public file_mgr {
protected:
jpeg_decompress_struct _cinfo;
jpeg_error_mgr _jerr;
void init() {
_cinfo.err=jpeg_std_error(&_jerr);
jpeg_create_decompress(&_cinfo);
jpeg_stdio_src(&_cinfo,_fp.get());
jpeg_read_header(&_cinfo,TRUE);
}
public:
jpeg_reader(FILE* file) : file_mgr(file) { init(); }
jpeg_reader(const char* filename) : file_mgr(filename, "rb") { init(); }
~jpeg_reader() { jpeg_destroy_decompress(&_cinfo); }
template <typename View>
void apply(const View& view) {
jpeg_start_decompress(&_cinfo); // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...
io_error_if(_cinfo.data_precision!=8,"jpeg_reader::apply(): this image file is not supported");
io_error_if(_cinfo.out_color_space!=jpeg_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type,
"jpeg_reader::apply(): input view type does not match the image file");
io_error_if(view.dimensions() != get_dimensions(), "jpeg_reader::apply(): input view dimensions do not match the image file");
std::vector<pixel<bits8,layout<typename color_space_type<View>::type> > > row(view.width());
JSAMPLE* row_address=(JSAMPLE*)&row.front();
for(int y=0;y<view.height();++y) {
io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
"jpeg_reader::apply(): fail to read JPEG file");
std::copy(row.begin(),row.end(),view.row_begin(y));
}
jpeg_finish_decompress(&_cinfo);
}
template <typename Image>
void read_image(Image& im) {
im.recreate(get_dimensions());
apply(view(im));
}
point2<std::ptrdiff_t> get_dimensions() const {
return point2<std::ptrdiff_t>(_cinfo.image_width,_cinfo.image_height);
}
};
// This code will be simplified...
template <typename CC>
class jpeg_reader_color_convert : public jpeg_reader {
private:
CC _cc;
public:
jpeg_reader_color_convert(FILE* file,CC cc_in) : jpeg_reader(file),_cc(cc_in) {}
jpeg_reader_color_convert(FILE* file) : jpeg_reader(file) {}
jpeg_reader_color_convert(const char* filename,CC cc_in) : jpeg_reader(filename),_cc(cc_in) {}
jpeg_reader_color_convert(const char* filename) : jpeg_reader(filename) {}
template <typename View>
void apply(const View& view) {
jpeg_start_decompress(&_cinfo); // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...
io_error_if(_cinfo.data_precision!=8,"jpeg_reader_color_covert::apply(): this image file is not supported");
io_error_if(view.dimensions() != get_dimensions(), "jpeg_reader_color_covert::apply(): input view dimensions don't match the image file");
switch (_cinfo.out_color_space) {
case JCS_GRAYSCALE: {
std::vector<gray8_pixel_t> row(view.width());
JSAMPLE* row_address=(JSAMPLE*)&row.front();
for(int y=0;y<view.height();++y) {
io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
"jpeg_reader_color_covert::apply(): fail to read JPEG file");
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 JCS_RGB: {
std::vector<rgb8_pixel_t> row(view.width());
JSAMPLE* row_address=(JSAMPLE*)&row.front();
for(int y=0;y<view.height();++y) {
io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
"jpeg_reader_color_covert::apply(): fail to read JPEG file");
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 JCS_CMYK: {
std::vector<cmyk8_pixel_t> row(view.width());
JSAMPLE* row_address=(JSAMPLE*)&row.front();
for(int y=0;y<view.height();++y) {
io_error_if(jpeg_read_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1)!=1,
"jpeg_reader_color_covert::apply(): fail to read JPEG file");
std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn<cmyk8_ref_t, typename View::value_type,CC>(_cc));
}
break;
}
default:
io_error("jpeg_reader_color_covert::apply(): unknown color type");
}
jpeg_finish_decompress(&_cinfo);
}
template <typename Image>
void read_image(Image& im) {
im.recreate(get_dimensions());
apply(view(im));
}
};
class jpeg_writer : public file_mgr {
jpeg_compress_struct _cinfo;
jpeg_error_mgr _jerr;
void init() {
_cinfo.err=jpeg_std_error(&_jerr);
jpeg_create_compress(&_cinfo);
jpeg_stdio_dest(&_cinfo,_fp.get());
}
public:
jpeg_writer(FILE* file) : file_mgr(file) { init(); }
jpeg_writer(const char* filename) : file_mgr(filename, "wb") { init(); }
~jpeg_writer() { jpeg_destroy_compress(&_cinfo); }
template <typename View>
void apply(const View& view,int quality=100) {
_cinfo.image_width = (JDIMENSION)view.width();
_cinfo.image_height = (JDIMENSION)view.height();
_cinfo.input_components=num_channels<View>::value;
_cinfo.in_color_space = jpeg_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type;
jpeg_set_defaults(&_cinfo);
jpeg_set_quality(&_cinfo, quality, TRUE);
jpeg_start_compress(&_cinfo, TRUE);
std::vector<pixel<bits8,layout<typename color_space_type<View>::type> > > row(view.width());
JSAMPLE* row_address=(JSAMPLE*)&row.front();
for (int y=0;y<view.height(); ++y) {
std::copy(view.row_begin(y),view.row_end(y),row.begin());
io_error_if(jpeg_write_scanlines(&_cinfo,(JSAMPARRAY)&row_address,1) != 1,
"jpeg_writer::apply(): fail to write file");
}
jpeg_finish_compress(&_cinfo);
}
};
} // namespace detail
} } // namespace boost::gil
#endif

View 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

View File

@@ -0,0 +1,214 @@
/*
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_IO_H
#define GIL_PNG_IO_H
/// \file
/// \brief Support for reading and writing PNG files
/// Requires libpng and zlib!
//
// We are currently providing the following functions:
// point2<std::ptrdiff_t> png_read_dimensions(const char*)
// template <typename View> void png_read_view(const char*,const View&)
// template <typename View> void png_read_image(const char*,image<View>&)
// template <typename View> void png_write_view(const char*,const View&)
// template <typename View> struct png_read_support;
// template <typename View> struct png_write_support;
//
/// \author Hailin Jin and Lubomir Bourdev \n
/// Adobe Systems Incorporated
/// \date 2005-2007 \n Last updated September 24, 2006
#include <stdio.h>
#include <string>
extern "C" {
#include "png.h"
}
#include <boost/static_assert.hpp>
#include "../../gil_config.hpp"
#include "../../utilities.hpp"
#include "io_error.hpp"
#include "png_io_private.hpp"
namespace boost { namespace gil {
/// \ingroup PNG_IO
/// \brief Returns the width and height of the PNG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
inline point2<std::ptrdiff_t> png_read_dimensions(const char *filename) {
detail::png_reader m(filename);
return m.get_dimensions();
}
/// \ingroup PNG_IO
/// \brief Returns the width and height of the PNG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
inline point2<std::ptrdiff_t> png_read_dimensions(const std::string& filename) {
return png_read_dimensions(filename.c_str());
}
/// \ingroup PNG_IO
/// \brief Determines whether the given view type is supported for reading
template <typename View>
struct png_read_support {
BOOST_STATIC_CONSTANT(bool,is_supported=
(detail::png_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::is_supported));
BOOST_STATIC_CONSTANT(int,bit_depth=
(detail::png_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::bit_depth));
BOOST_STATIC_CONSTANT(int,color_type=
(detail::png_read_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type));
BOOST_STATIC_CONSTANT(bool, value=is_supported);
};
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name into the given view.
/// Triggers a compile assert if the view color space and channel depth are not supported by the PNG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid PNG 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 png_read_view(const char* filename,const View& view) {
BOOST_STATIC_ASSERT(png_read_support<View>::is_supported);
detail::png_reader m(filename);
m.apply(view);
}
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name into the given view.
template <typename View>
inline void png_read_view(const std::string& filename,const View& view) {
png_read_view(filename.c_str(),view);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png 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 PNG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not
/// compatible with the ones specified by Image
template <typename Image>
inline void png_read_image(const char* filename,Image& im) {
BOOST_STATIC_ASSERT(png_read_support<typename Image::view_t>::is_supported);
detail::png_reader m(filename);
m.read_image(im);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it.
template <typename Image>
inline void png_read_image(const std::string& filename,Image& im) {
png_read_image(filename.c_str(),im);
}
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
/// Throws std::ios_base::failure if the file is not a valid PNG file, or if its dimensions don't match the ones of the view.
template <typename View,typename CC>
inline void png_read_and_convert_view(const char* filename,const View& view,CC cc) {
detail::png_reader_color_convert<CC> m(filename,cc);
m.apply(view);
}
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
/// Throws std::ios_base::failure if the file is not a valid PNG file, or if its dimensions don't match the ones of the view.
template <typename View>
inline void png_read_and_convert_view(const char* filename,const View& view) {
detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
m.apply(view);
}
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
template <typename View,typename CC>
inline void png_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
png_read_and_convert_view(filename.c_str(),view,cc);
}
/// \ingroup PNG_IO
/// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
template <typename View>
inline void png_read_and_convert_view(const std::string& filename,const View& view) {
png_read_and_convert_view(filename.c_str(),view);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid PNG file
template <typename Image,typename CC>
inline void png_read_and_convert_image(const char* filename,Image& im,CC cc) {
detail::png_reader_color_convert<CC> m(filename,cc);
m.read_image(im);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid PNG file
template <typename Image>
inline void png_read_and_convert_image(const char* filename,Image& im) {
detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
m.read_image(im);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
template <typename Image,typename CC>
inline void png_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
png_read_and_convert_image(filename.c_str(),im,cc);
}
/// \ingroup PNG_IO
/// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
template <typename Image>
inline void png_read_and_convert_image(const std::string& filename,Image& im) {
png_read_and_convert_image(filename.c_str(),im);
}
/// \ingroup PNG_IO
/// \brief Determines whether the given view type is supported for writing
template <typename View>
struct png_write_support {
BOOST_STATIC_CONSTANT(bool,is_supported=
(detail::png_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::is_supported));
BOOST_STATIC_CONSTANT(int,bit_depth=
(detail::png_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::bit_depth));
BOOST_STATIC_CONSTANT(int,color_type=
(detail::png_write_support_private<typename channel_type<View>::type,
typename color_space_type<View>::type>::color_type));
BOOST_STATIC_CONSTANT(bool, value=is_supported);
};
/// \ingroup PNG_IO
/// \brief Saves the view to a png file specified by the given png image file name.
/// Triggers a compile assert if the view color space and channel depth are not supported by the PNG library or by the I/O extension.
/// Throws std::ios_base::failure if it fails to create the file.
template <typename View>
inline void png_write_view(const char* filename,const View& view) {
BOOST_STATIC_ASSERT(png_write_support<View>::is_supported);
detail::png_writer m(filename);
m.apply(view);
}
/// \ingroup PNG_IO
/// \brief Saves the view to a png file specified by the given png image file name.
template <typename View>
inline void png_write_view(const std::string& filename,const View& view) {
png_write_view(filename.c_str(),view);
}
} } // namespace boost::gil
#endif

View 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

View File

@@ -0,0 +1,135 @@
/*
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_DYNAMIC_IO_H
#define GIL_TIFF_DYNAMIC_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 June 10, 2006
//
// We are currently providing the following functions:
// template <typename Images> void tiff_read_image(const char*,any_image<Images>)
// template <typename Views> void tiff_write_view(const char*,any_image_view<Views>)
//
#include <string>
#include <boost/mpl/bool.hpp>
#include "../dynamic_image/dynamic_image_all.hpp"
#include "io_error.hpp"
#include "tiff_io.hpp"
#include "dynamic_io.hpp"
namespace boost { namespace gil {
namespace detail {
struct tiff_write_is_supported {
template<typename View> struct apply
: public mpl::bool_<tiff_write_support<View>::is_supported> {};
};
class tiff_writer_dynamic : public tiff_writer {
public:
typedef void result_type;
tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {}
template <typename Views>
void write_view(const any_image_view<Views>& runtime_view) {
dynamic_io_fnobj<tiff_write_is_supported, tiff_writer> op(this);
apply_operation(runtime_view,op);
}
};
class tiff_type_format_checker {
int _bit_depth;
int _color_type;
public:
tiff_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 tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
tiff_read_support<typename Image::view_t>::color_type==_color_type;
}
};
struct tiff_read_is_supported {
template<typename View> struct apply
: public mpl::bool_<tiff_read_support<View>::is_supported> {};
};
class tiff_reader_dynamic : public tiff_reader {
public:
tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {}
template <typename Images>
void read_image(any_image<Images>& im) {
int width,height;
unsigned short bps,photometric;
TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
io_error("tiff_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<tiff_read_is_supported, tiff_reader> op(this);
apply_operation(view(im),op);
}
}
};
} // namespace detail
/// \ingroup TIFF_IO
/// \brief reads a TIFF image into a run-time instantiated image
/// Opens the given tiff 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 tiff_read_image(const char* filename,any_image<Images>& im) {
detail::tiff_reader_dynamic m(filename);
m.read_image(im);
}
/// \ingroup TIFF_IO
/// \brief reads a TIFF image into a run-time instantiated image
template <typename Images>
inline void tiff_read_image(const std::string& filename,any_image<Images>& im) {
tiff_read_image(filename.c_str(),im);
}
/// \ingroup TIFF_IO
/// \brief Saves the currently instantiated view to a tiff file specified by the given tiff 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 tiff_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
detail::tiff_writer_dynamic m(filename);
m.write_view(runtime_view);
}
/// \ingroup TIFF_IO
/// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
template <typename Views>
inline void tiff_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
tiff_write_view(filename.c_str(),runtime_view);
}
} } // namespace boost::gil
#endif

View 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