claw  1.9.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@stuff-o-matic.com
24 */
30 #include <claw/graphic/targa.hpp>
31 
32 #include <claw/exception.hpp>
33 
34 //********************** targa::writer::file_output_buffer
35 //*********************
36 
37 namespace claw
38 {
39  namespace graphic
40  {
48  template <>
49  void targa::writer::file_output_buffer<
50  claw::graphic::rgba_pixel_8>::order_pixel_bytes(const pixel_type& p)
51  {
52  m_stream << p.components.blue << p.components.green << p.components.red
53  << p.components.alpha;
54  }
55  }
56 }
57 
58 //************************** targa::writer::writer
59 //*****************************
60 
66  : m_image(img)
67 {}
68 
75 claw::graphic::targa::writer::writer(const image& img, std::ostream& f,
76  bool rle)
77  : m_image(img)
78 {
79  save(f, rle);
80 }
81 
87 void claw::graphic::targa::writer::save(std::ostream& os, bool rle) const
88 {
89  header h(m_image.width(), m_image.height());
90 
91  if(rle)
92  h.image_type = rle_true_color;
93  else
94  h.image_type = true_color;
95 
96  os.write(reinterpret_cast<char*>(&h), sizeof(header));
97 
98  if(rle)
99  save_rle_true_color(os);
100  else
101  save_true_color(os);
102 
103  footer f;
104  os.write(reinterpret_cast<char*>(&f), sizeof(footer));
105 }
106 
111 void claw::graphic::targa::writer::save_true_color(std::ostream& os) const
112 {
113  file_output_buffer<rgba_pixel_8> output_buffer(os);
114 
115  for(const_iterator it = m_image.begin(); it != m_image.end(); ++it)
116  output_buffer.order_pixel_bytes(*it);
117 }
118 
123 void claw::graphic::targa::writer::save_rle_true_color(std::ostream& os) const
124 {
125  rle32_encoder encoder;
126  rle32_encoder::output_buffer_type output_buffer(os);
127 
128  for(unsigned int y = 0; y != m_image.height(); ++y)
129  encoder.encode(m_image[y].begin(), m_image[y].end(), output_buffer);
130 }
void save(std::ostream &f, bool rle) const
Save the content of the image in a stream.
struct claw::graphic::rgba_pixel::@15::@17 components
Component by component representation.
Base class for iterators on an image.
Definition: image.hpp:107
A simple class to use as exception with string message.
writer(const image &img)
Constructor.
Pixel pixel_type
The type of the pixels in the input buffer.
Definition: targa.hpp:405
A class to deal with images.
Definition: image.hpp:50
A class for targa pictures.
This is the main namespace.
Definition: application.hpp:49