Claw  1.7.3
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@gamned.org
24 */
30 #include <claw/pcx.hpp>
31 
32 /*----------------------------------------------------------------------------*/
38 ( std::ostream& os )
39  : m_stream(os)
40 {
41 
42 } // pcx::writer::file_output_buffer::file_output_buffer()
43 
44 /*----------------------------------------------------------------------------*/
51 ( unsigned int n, pattern_type pattern )
52 {
53  if ( (pattern > 63) || (n > 1) )
54  {
55  u_int_8 cnt = 0xC0 | (u_int_8)n;
56  m_stream.write( reinterpret_cast<char*>(&cnt), sizeof(u_int_8) );
57  }
58 
59  m_stream.write( reinterpret_cast<char*>(&pattern), sizeof(u_int_8));
60 } // pcx::writer::file_output_buffer::encode()
61 
62 /*----------------------------------------------------------------------------*/
66 unsigned int
68 {
69  return 1;
70 } // pcx::writer::file_output_buffer::min_interesting()
71 
72 /*----------------------------------------------------------------------------*/
76 unsigned int
78 {
79  return 63;
80 } // pcx::writer::file_output_buffer::max_encodable()
81 
82 
83 
84 
85 /*----------------------------------------------------------------------------*/
91  : m_image(img)
92 {
93 
94 } // pcx::writer::writer()
95 
96 /*----------------------------------------------------------------------------*/
102 claw::graphic::pcx::writer::writer( const image& img, std::ostream& f )
103  : m_image(img)
104 {
105  save(f);
106 } // pcx::writer::writer()
107 
108 /*----------------------------------------------------------------------------*/
113 void claw::graphic::pcx::writer::save( std::ostream& os ) const
114 {
115  const unsigned int bytes_per_line = m_image.width() + m_image.width() % 2;
116 
117  write_header(os, bytes_per_line);
118  save_rle_true_color(os, bytes_per_line);
119 } // pcx::writer::save()
120 
121 /*----------------------------------------------------------------------------*/
127 void claw::graphic::pcx::writer::write_header
128 ( std::ostream& os, unsigned int bytes_per_line ) const
129 {
130  header h;
131 
132  h.manufacturer = 10;
133  h.version = 5;
134  h.encoded = 1;
135  h.bpp = 8;
136  h.window.x_min = 0;
137  h.window.y_min = 0;
138  h.window.x_max = m_image.width() - 1;
139  h.window.y_max = m_image.height() - 1;
140  h.horizontal_dpi = 72; // arbitrary value
141  h.vertical_dpi = 72;
142  std::fill( h.color_map, h.color_map+16, rgb_pixel_8(0, 0, 0) );
143  h.reserved = 0;
144  h.color_planes = 3; // RGB
145  h.bytes_per_line = bytes_per_line;
146  h.palette_info = 0;
147  h.screen_size.horizontal = 0;
148  h.screen_size.vertical = 0;
149  std::fill( h.filler, h.filler+54, 0 );
150 
151  os.write( reinterpret_cast<char*>(&h), sizeof(header) );
152 } // pcx::writer::write_header()
153 
154 /*----------------------------------------------------------------------------*/
160 void claw::graphic::pcx::writer::save_rle_true_color
161 ( std::ostream& os, unsigned int bytes_per_line ) const
162 {
163  std::vector<u_int_8> data(bytes_per_line, 0);
164 
165  rle_pcx_encoder encoder;
166  file_output_buffer output(os);
167 
168  for (unsigned int y=0; y!=m_image.height(); ++y)
169  {
170  // red
171  for (unsigned int x=0; x!=m_image.width(); ++x)
172  data[x] = m_image[y][x].components.red;
173 
174  encoder.encode( data.begin(), data.end(), output );
175 
176  // green
177  for (unsigned int x=0; x!=m_image.width(); ++x)
178  data[x] = m_image[y][x].components.green;
179 
180  encoder.encode( data.begin(), data.end(), output );
181 
182  // blue
183  for (unsigned int x=0; x!=m_image.width(); ++x)
184  data[x] = m_image[y][x].components.blue;
185 
186  encoder.encode( data.begin(), data.end(), output );
187  }
188 } // pcx::writer::save_rle_true_color()