claw  1.9.0
pcx_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/pcx.hpp>
31 
37  std::ostream& os)
38  : m_stream(os)
39 {}
40 
47  unsigned int n, pattern_type pattern)
48 {
49  if((pattern > 63) || (n > 1))
50  {
51  u_int_8 cnt = 0xC0 | (u_int_8)n;
52  m_stream.write(reinterpret_cast<char*>(&cnt), sizeof(u_int_8));
53  }
54 
55  m_stream.write(reinterpret_cast<char*>(&pattern), sizeof(u_int_8));
56 }
57 
61 unsigned int
63 {
64  return 1;
65 }
66 
70 unsigned int
72 {
73  return 63;
74 }
75 
81  : m_image(img)
82 {}
83 
89 claw::graphic::pcx::writer::writer(const image& img, std::ostream& f)
90  : m_image(img)
91 {
92  save(f);
93 }
94 
99 void claw::graphic::pcx::writer::save(std::ostream& os) const
100 {
101  const unsigned int bytes_per_line = m_image.width() + m_image.width() % 2;
102 
103  write_header(os, bytes_per_line);
104  save_rle_true_color(os, bytes_per_line);
105 }
106 
112 void claw::graphic::pcx::writer::write_header(
113  std::ostream& os, unsigned int bytes_per_line) const
114 {
115  header h;
116 
117  h.manufacturer = 10;
118  h.version = 5;
119  h.encoded = 1;
120  h.bpp = 8;
121  h.window.x_min = 0;
122  h.window.y_min = 0;
123  h.window.x_max = m_image.width() - 1;
124  h.window.y_max = m_image.height() - 1;
125  h.horizontal_dpi = 72; // arbitrary value
126  h.vertical_dpi = 72;
127  std::fill(h.color_map, h.color_map + 16, rgb_pixel_8(0, 0, 0));
128  h.reserved = 0;
129  h.color_planes = 3; // RGB
130  h.bytes_per_line = bytes_per_line;
131  h.palette_info = 0;
132  h.screen_size.horizontal = 0;
133  h.screen_size.vertical = 0;
134  std::fill(h.filler, h.filler + 54, 0);
135 
136  os.write(reinterpret_cast<char*>(&h), sizeof(header));
137 }
138 
144 void claw::graphic::pcx::writer::save_rle_true_color(
145  std::ostream& os, unsigned int bytes_per_line) const
146 {
147  std::vector<u_int_8> data(bytes_per_line, 0);
148 
149  rle_pcx_encoder encoder;
150  file_output_buffer output(os);
151 
152  for(unsigned int y = 0; y != m_image.height(); ++y)
153  {
154  // red
155  for(unsigned int x = 0; x != m_image.width(); ++x)
156  data[x] = m_image[y][x].components.red;
157 
158  encoder.encode(data.begin(), data.end(), output);
159 
160  // green
161  for(unsigned int x = 0; x != m_image.width(); ++x)
162  data[x] = m_image[y][x].components.green;
163 
164  encoder.encode(data.begin(), data.end(), output);
165 
166  // blue
167  for(unsigned int x = 0; x != m_image.width(); ++x)
168  data[x] = m_image[y][x].components.blue;
169 
170  encoder.encode(data.begin(), data.end(), output);
171  }
172 }
writer(const image &img)
Constructor.
Definition: pcx_writer.cpp:80
file_output_buffer(std::ostream &os)
Constructor.
Definition: pcx_writer.cpp:36
u_int_8 pattern_type
The typ of the output patterns.
Definition: pcx.hpp:298
unsigned int min_interesting() const
Get the minimum number of pixels needed for encoding.
Definition: pcx_writer.cpp:62
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition: pcx_writer.cpp:99
A class for pcx pictures.
void encode(unsigned int n, pattern_type pattern)
Encode a pixel data.
Definition: pcx_writer.cpp:46
A class to deal with images.
Definition: image.hpp:50
unsigned int max_encodable() const
Get the maximum number of pixel a code can encode.
Definition: pcx_writer.cpp:71