Claw  1.7.3
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  /*------------------------------------------------------------------------*/
51  template< >
54  {
55  m_stream << p.components.blue << p.components.green
56  << p.components.red << p.components.alpha;
57  } // targa::writer::file_output_buffer::order_pixel_bytes()
58  } // namespace graphic
59 } // namespace claw
60 
61 
62 //************************** targa::writer::writer *****************************
63 
64 
65 
66 
67 /*----------------------------------------------------------------------------*/
73  : m_image(img)
74 {
75 
76 } // targa::writer::writer()
77 
78 /*----------------------------------------------------------------------------*/
86 ( const image& img, std::ostream& f, bool rle )
87  : m_image(img)
88 {
89  save(f, rle);
90 } // targa::writer::writer()
91 
92 /*----------------------------------------------------------------------------*/
98 void claw::graphic::targa::writer::save( std::ostream& os, bool rle ) const
99 {
100  header h( m_image.width(), m_image.height() );
101 
102  if (rle)
103  h.image_type = rle_true_color;
104  else
105  h.image_type = true_color;
106 
107  os.write( reinterpret_cast<char*>(&h), sizeof(header) );
108 
109  if (rle)
110  save_rle_true_color(os);
111  else
112  save_true_color(os);
113 
114  footer f;
115  os.write( reinterpret_cast<char*>(&f), sizeof(footer) );
116 } // targa::writer::save()
117 
118 /*----------------------------------------------------------------------------*/
123 void claw::graphic::targa::writer::save_true_color( std::ostream& os ) const
124 {
125  file_output_buffer<rgba_pixel_8> output_buffer(os);
126 
127  for (const_iterator it=m_image.begin(); it!=m_image.end(); ++it)
128  output_buffer.order_pixel_bytes(*it);
129 } // targa::writer::save_true_color()
130 
131 /*----------------------------------------------------------------------------*/
136 void claw::graphic::targa::writer::save_rle_true_color( std::ostream& os ) const
137 {
138  rle32_encoder encoder;
139  rle32_encoder::output_buffer_type output_buffer(os);
140 
141  for ( unsigned int y=0; y!=m_image.height(); ++y )
142  encoder.encode( m_image[y].begin(), m_image[y].end(), output_buffer );
143 } // targa::writer::save_rle_true_color()
claw::graphic::targa::writer::file_output_buffer::order_pixel_bytes
void order_pixel_bytes(const pixel_type &p)
Write a pixel in the stream and set its value in the good order.
claw::graphic::targa::writer::file_output_buffer::pixel_type
Pixel pixel_type
The type of the pixels in the input buffer.
Definition: targa.hpp:426
claw::graphic::targa::save
void save(std::ostream &os, bool rle) const
Save the content of the image in a stream.
Definition: targa.cpp:72
claw::graphic::targa::file_structure::header::image_type
char image_type
Image type.
Definition: targa.hpp:88
claw::graphic::targa::file_structure::footer
Footer of a targa file.
Definition: targa.hpp:154
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::graphic::targa::file_structure::header
Header of a targa file.
Definition: targa.hpp:76
claw::graphic::targa::writer::save
void save(std::ostream &f, bool rle) const
Save the content of the image in a stream.
Definition: targa_writer.cpp:98
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
exception.hpp
A simple class to use as exception with string message.
claw::graphic::targa::writer::writer
writer(const image &img)
Constructor.
Definition: targa_writer.cpp:72
claw::graphic::image::base_iterator
Base class for iterators on an image.
Definition: image.hpp:106
targa.hpp
A class for targa pictures.