2 // Copyright 2010-2012 Kenneth Riddile, Christian Henning
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
8 #ifndef BOOST_GIL_EXTENSION_IO_TARGA_DETAIL_WRITE_HPP
9 #define BOOST_GIL_EXTENSION_IO_TARGA_DETAIL_WRITE_HPP
11 #include <boost/gil/extension/io/targa/tags.hpp>
12 #include <boost/gil/extension/io/targa/detail/writer_backend.hpp>
14 #include <boost/gil/io/base.hpp>
15 #include <boost/gil/io/device.hpp>
16 #include <boost/gil/io/dynamic_io_new.hpp>
20 namespace boost { namespace gil {
22 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
24 #pragma warning(disable:4512) //assignment operator could not be generated
29 template < int N > struct get_targa_view_type {};
30 template <> struct get_targa_view_type< 3 > { using type = bgr8_view_t; };
31 template <> struct get_targa_view_type< 4 > { using type = bgra8_view_t; };
33 struct targa_write_is_supported
35 template< typename View >
37 : public is_write_supported< typename get_pixel_type< View >::type
48 template< typename Device >
52 : public writer_backend< Device
57 using backend_t = writer_backend<Device, targa_tag>;
61 writer( const Device& io_dev
62 , const image_write_info< targa_tag >& info
69 template<typename View>
70 void apply( const View& view )
77 template< typename View >
78 void write( const View& view )
80 uint8_t bit_depth = static_cast<uint8_t>( num_channels<View>::value * 8 );
82 // write the TGA header
83 this->_io_dev.write_uint8( 0 ); // offset
84 this->_io_dev.write_uint8( targa_color_map_type::_rgb );
85 this->_io_dev.write_uint8( targa_image_type::_rgb );
86 this->_io_dev.write_uint16( 0 ); // color map start
87 this->_io_dev.write_uint16( 0 ); // color map length
88 this->_io_dev.write_uint8( 0 ); // color map depth
89 this->_io_dev.write_uint16( 0 ); // x origin
90 this->_io_dev.write_uint16( 0 ); // y origin
91 this->_io_dev.write_uint16( static_cast<uint16_t>( view.width() ) ); // width in pixels
92 this->_io_dev.write_uint16( static_cast<uint16_t>( view.height() ) ); // height in pixels
93 this->_io_dev.write_uint8( bit_depth );
97 this->_io_dev.write_uint8( 8 ); // 8-bit alpha channel descriptor
101 this->_io_dev.write_uint8( 0 );
105 , typename detail::get_targa_view_type< num_channels< View >::value >::type
110 template< typename View
113 void write_image( const View& view )
115 size_t row_size = view.width() * num_channels<View>::value;
116 byte_vector_t buffer( row_size );
117 std::fill( buffer.begin(), buffer.end(), 0 );
120 TGA_View row = interleaved_view( view.width()
122 , reinterpret_cast<typename TGA_View::value_type*>( &buffer.front() )
126 for( typename View::y_coord_t y = view.height() - 1; y > -1; --y )
128 copy_pixels( subimage_view( view
130 , static_cast<int>( y )
131 , static_cast<int>( view.width() )
137 this->_io_dev.write( &buffer.front(), row_size );
144 /// TARGA Dynamic Image Writer
146 template< typename Device >
147 class dynamic_image_writer< Device
150 : public writer< Device
154 using parent_t = writer<Device, targa_tag>;
158 dynamic_image_writer( const Device& io_dev
159 , const image_write_info< targa_tag >& info
166 template< typename ...Views >
167 void apply( const any_image_view< Views... >& views )
169 detail::dynamic_io_fnobj< detail::targa_write_is_supported
173 apply_operation( views, op );
177 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)