Claw  1.7.0
targa_writer.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <claw/targa.hpp>
31 #include <claw/exception.hpp>
32 
33 
34 //********************** targa::writer::file_output_buffer *********************
35 
36 
37 
38 
39 namespace claw
40 {
41  namespace graphic
42  {
43  /*------------------------------------------------------------------------*/
50  template< >
51  void targa::writer::file_output_buffer<claw::graphic::rgba_pixel_8>::
52  order_pixel_bytes( const pixel_type& p )
53  {
54  m_stream << p.components.blue << p.components.green
55  << p.components.red << p.components.alpha;
56  } // targa::writer::file_output_buffer::order_pixel_bytes()
57  } // namespace graphic
58 } // namespace claw
59 
60 
61 //************************** targa::writer::writer *****************************
62 
63 
64 
65 
66 /*----------------------------------------------------------------------------*/
72  : m_image(img)
73 {
74 
75 } // targa::writer::writer()
76 
77 /*----------------------------------------------------------------------------*/
85 ( const image& img, std::ostream& f, bool rle )
86  : m_image(img)
87 {
88  save(f, rle);
89 } // targa::writer::writer()
90 
91 /*----------------------------------------------------------------------------*/
97 void claw::graphic::targa::writer::save( std::ostream& os, bool rle ) const
98 {
99  header h( m_image.width(), m_image.height() );
100 
101  if (rle)
102  h.image_type = rle_true_color;
103  else
104  h.image_type = true_color;
105 
106  os.write( reinterpret_cast<char*>(&h), sizeof(header) );
107 
108  if (rle)
109  save_rle_true_color(os);
110  else
111  save_true_color(os);
112 
113  footer f;
114  os.write( reinterpret_cast<char*>(&f), sizeof(footer) );
115 } // targa::writer::save()
116 
117 /*----------------------------------------------------------------------------*/
122 void claw::graphic::targa::writer::save_true_color( std::ostream& os ) const
123 {
124  file_output_buffer<rgba_pixel_8> output_buffer(os);
125 
126  for (const_iterator it=m_image.begin(); it!=m_image.end(); ++it)
127  output_buffer.order_pixel_bytes(*it);
128 } // targa::writer::save_true_color()
129 
130 /*----------------------------------------------------------------------------*/
135 void claw::graphic::targa::writer::save_rle_true_color( std::ostream& os ) const
136 {
137  rle32_encoder encoder;
138  rle32_encoder::output_buffer_type output_buffer(os);
139 
140  for ( unsigned int y=0; y!=m_image.height(); ++y )
141  encoder.encode( m_image[y].begin(), m_image[y].end(), output_buffer );
142 } // targa::writer::save_rle_true_color()