tiff_dynamic_io.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_TIFF_DYNAMIC_IO_H
  10. #define GIL_TIFF_DYNAMIC_IO_H
  11. /// \file
  12. /// \brief Support for reading and writing TIFF files
  13. /// Requires libtiff!
  14. /// \author Hailin Jin and Lubomir Bourdev \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated June 10, 2006
  17. //
  18. // We are currently providing the following functions:
  19. // template <typename Images> void tiff_read_image(const char*,any_image<Images>)
  20. // template <typename Views> void tiff_write_view(const char*,any_image_view<Views>)
  21. //
  22. #include <string>
  23. #include <boost/mpl/bool.hpp>
  24. #include "../dynamic_image/dynamic_image_all.hpp"
  25. #include "io_error.hpp"
  26. #include "tiff_io.hpp"
  27. #include "dynamic_io.hpp"
  28. namespace boost { namespace gil {
  29. namespace detail {
  30. struct tiff_write_is_supported {
  31. template<typename View> struct apply
  32. : public mpl::bool_<tiff_write_support<View>::is_supported> {};
  33. };
  34. class tiff_writer_dynamic : public tiff_writer {
  35. public:
  36. typedef void result_type;
  37. tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {}
  38. template <typename Views>
  39. void write_view(const any_image_view<Views>& runtime_view) {
  40. dynamic_io_fnobj<tiff_write_is_supported, tiff_writer> op(this);
  41. apply_operation(runtime_view,op);
  42. }
  43. };
  44. class tiff_type_format_checker {
  45. int _bit_depth;
  46. int _color_type;
  47. public:
  48. tiff_type_format_checker(int bit_depth_in,int color_type_in) :
  49. _bit_depth(bit_depth_in),_color_type(color_type_in) {}
  50. template <typename Image>
  51. bool apply() {
  52. return tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
  53. tiff_read_support<typename Image::view_t>::color_type==_color_type;
  54. }
  55. };
  56. struct tiff_read_is_supported {
  57. template<typename View> struct apply
  58. : public mpl::bool_<tiff_read_support<View>::is_supported> {};
  59. };
  60. class tiff_reader_dynamic : public tiff_reader {
  61. public:
  62. tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {}
  63. template <typename Images>
  64. void read_image(any_image<Images>& im) {
  65. int width,height;
  66. unsigned short bps,photometric;
  67. TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
  68. TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
  69. TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
  70. TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
  71. if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
  72. io_error("tiff_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
  73. } else {
  74. im.recreate(width,height);
  75. dynamic_io_fnobj<tiff_read_is_supported, tiff_reader> op(this);
  76. apply_operation(view(im),op);
  77. }
  78. }
  79. };
  80. } // namespace detail
  81. /// \ingroup TIFF_IO
  82. /// \brief reads a TIFF image into a run-time instantiated image
  83. /// 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
  84. /// and creates a new image of that type with the dimensions specified by the image file.
  85. /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
  86. template <typename Images>
  87. inline void tiff_read_image(const char* filename,any_image<Images>& im) {
  88. detail::tiff_reader_dynamic m(filename);
  89. m.read_image(im);
  90. }
  91. /// \ingroup TIFF_IO
  92. /// \brief reads a TIFF image into a run-time instantiated image
  93. template <typename Images>
  94. inline void tiff_read_image(const std::string& filename,any_image<Images>& im) {
  95. tiff_read_image(filename.c_str(),im);
  96. }
  97. /// \ingroup TIFF_IO
  98. /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
  99. /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
  100. /// or if it fails to create the file.
  101. template <typename Views>
  102. inline void tiff_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
  103. detail::tiff_writer_dynamic m(filename);
  104. m.write_view(runtime_view);
  105. }
  106. /// \ingroup TIFF_IO
  107. /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
  108. template <typename Views>
  109. inline void tiff_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
  110. tiff_write_view(filename.c_str(),runtime_view);
  111. }
  112. } } // namespace boost::gil
  113. #endif