writer_backend.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Copyright 2012 Christian Henning
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_IO_JPEG_DETAIL_WRITER_BACKEND_HPP
  9. #define BOOST_GIL_EXTENSION_IO_JPEG_DETAIL_WRITER_BACKEND_HPP
  10. #include <boost/gil/extension/io/jpeg/tags.hpp>
  11. #include <boost/gil/extension/io/jpeg/detail/base.hpp>
  12. #include <memory>
  13. namespace boost { namespace gil {
  14. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  15. #pragma warning(push)
  16. #pragma warning(disable:4512) //assignment operator could not be generated
  17. #pragma warning(disable:4611) //interaction between '_setjmp' and C++ object destruction is non-portable
  18. #endif
  19. namespace detail {
  20. ///
  21. /// Wrapper for libjpeg's compress object. Implements value semantics.
  22. ///
  23. struct jpeg_compress_wrapper
  24. {
  25. protected:
  26. using jpeg_compress_ptr_t = std::shared_ptr<jpeg_compress_struct>;
  27. protected:
  28. ///
  29. /// Default Constructor
  30. ///
  31. jpeg_compress_wrapper()
  32. : _jpeg_compress_ptr( new jpeg_compress_struct()
  33. , jpeg_compress_deleter
  34. )
  35. {}
  36. jpeg_compress_struct* get() { return _jpeg_compress_ptr.get(); }
  37. const jpeg_compress_struct* get() const { return _jpeg_compress_ptr.get(); }
  38. private:
  39. static void jpeg_compress_deleter( jpeg_compress_struct* jpeg_compress_ptr )
  40. {
  41. if( jpeg_compress_ptr )
  42. {
  43. jpeg_destroy_compress( jpeg_compress_ptr );
  44. delete jpeg_compress_ptr;
  45. jpeg_compress_ptr = nullptr;
  46. }
  47. }
  48. private:
  49. jpeg_compress_ptr_t _jpeg_compress_ptr;
  50. };
  51. } // namespace detail
  52. ///
  53. /// JPEG Writer Backend
  54. ///
  55. template< typename Device >
  56. struct writer_backend< Device
  57. , jpeg_tag
  58. >
  59. : public jpeg_io_base
  60. , public detail::jpeg_compress_wrapper
  61. {
  62. public:
  63. using format_tag_t = jpeg_tag;
  64. public:
  65. ///
  66. /// Constructor
  67. ///
  68. writer_backend( const Device& io_dev
  69. , const image_write_info< jpeg_tag >& info
  70. )
  71. : _io_dev( io_dev )
  72. , _info( info )
  73. {
  74. get()->err = jpeg_std_error( &_jerr );
  75. get()->client_data = this;
  76. // Error exit handler: does not return to caller.
  77. _jerr.error_exit = &writer_backend< Device, jpeg_tag >::error_exit;
  78. // Fire exception in case of error.
  79. if( setjmp( _mark )) { raise_error(); }
  80. _dest._jdest.free_in_buffer = sizeof( buffer );
  81. _dest._jdest.next_output_byte = buffer;
  82. _dest._jdest.init_destination = reinterpret_cast< void(*) ( j_compress_ptr ) >( &writer_backend< Device, jpeg_tag >::init_device );
  83. _dest._jdest.empty_output_buffer = reinterpret_cast< boolean(*)( j_compress_ptr ) >( &writer_backend< Device, jpeg_tag >::empty_buffer );
  84. _dest._jdest.term_destination = reinterpret_cast< void(*) ( j_compress_ptr ) >( &writer_backend< Device, jpeg_tag >::close_device );
  85. _dest._this = this;
  86. jpeg_create_compress( get() );
  87. get()->dest = &_dest._jdest;
  88. }
  89. ~writer_backend()
  90. {
  91. jpeg_finish_compress ( get() );
  92. jpeg_destroy_compress( get() );
  93. }
  94. protected:
  95. struct gil_jpeg_destination_mgr
  96. {
  97. jpeg_destination_mgr _jdest;
  98. writer_backend< Device
  99. , jpeg_tag
  100. >* _this;
  101. };
  102. static void init_device( jpeg_compress_struct* cinfo )
  103. {
  104. gil_jpeg_destination_mgr* dest = reinterpret_cast< gil_jpeg_destination_mgr* >( cinfo->dest );
  105. dest->_jdest.free_in_buffer = sizeof( dest->_this->buffer );
  106. dest->_jdest.next_output_byte = dest->_this->buffer;
  107. }
  108. static boolean empty_buffer( jpeg_compress_struct* cinfo )
  109. {
  110. gil_jpeg_destination_mgr* dest = reinterpret_cast< gil_jpeg_destination_mgr* >( cinfo->dest );
  111. dest->_this->_io_dev.write( dest->_this->buffer
  112. , buffer_size
  113. );
  114. writer_backend<Device,jpeg_tag>::init_device( cinfo );
  115. return static_cast<boolean>(TRUE);
  116. }
  117. static void close_device( jpeg_compress_struct* cinfo )
  118. {
  119. writer_backend< Device
  120. , jpeg_tag
  121. >::empty_buffer( cinfo );
  122. gil_jpeg_destination_mgr* dest = reinterpret_cast< gil_jpeg_destination_mgr* >( cinfo->dest );
  123. dest->_this->_io_dev.flush();
  124. }
  125. void raise_error()
  126. {
  127. io_error( "Cannot write jpeg file." );
  128. }
  129. static void error_exit( j_common_ptr cinfo )
  130. {
  131. writer_backend< Device, jpeg_tag >* mgr = reinterpret_cast< writer_backend< Device, jpeg_tag >* >( cinfo->client_data );
  132. longjmp( mgr->_mark, 1 );
  133. }
  134. public:
  135. Device _io_dev;
  136. image_write_info< jpeg_tag > _info;
  137. gil_jpeg_destination_mgr _dest;
  138. static const unsigned int buffer_size = 1024;
  139. JOCTET buffer[buffer_size];
  140. };
  141. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  142. #pragma warning(pop)
  143. #endif
  144. } // namespace gil
  145. } // namespace boost
  146. #endif