File: pcx_writer.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (188 lines) | stat: -rw-r--r-- 5,787 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
  CLAW - a C++ Library Absolutely Wonderful

  CLAW is a free library without any particular aim but being useful to 
  anyone.

  Copyright (C) 2005-2011 Julien Jorge

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: julien.jorge@gamned.org
*/
/**
 * \file pcx_writer.cpp
 * \brief Implementation of the pcx::writer class.
 * \author Julien Jorge
 */
#include <claw/pcx.hpp>

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param os The stream in which we write the encoded data.
 */
claw::graphic::pcx::writer::file_output_buffer::file_output_buffer
( std::ostream& os )
  : m_stream(os)
{

} // pcx::writer::file_output_buffer::file_output_buffer()

/*----------------------------------------------------------------------------*/
/**
 * \brief Encode a pixel data.
 * \param n The number of time the pixel appears.
 * \param pattern The data of the pixel.
 */
void claw::graphic::pcx::writer::file_output_buffer::encode
( unsigned int n, pattern_type pattern )
{
  if ( (pattern > 63) || (n > 1) )
    {
      u_int_8 cnt = 0xC0 | (u_int_8)n;
      m_stream.write( reinterpret_cast<char*>(&cnt), sizeof(u_int_8) );
    }

  m_stream.write( reinterpret_cast<char*>(&pattern), sizeof(u_int_8));
} // pcx::writer::file_output_buffer::encode()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the minimum number of pixels needed for encoding.
 */
unsigned int
claw::graphic::pcx::writer::file_output_buffer::min_interesting() const
{
  return 1;
} // pcx::writer::file_output_buffer::min_interesting()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the maximum number of pixel a code can encode.
 */
unsigned int
claw::graphic::pcx::writer::file_output_buffer::max_encodable() const
{
  return 63;
} // pcx::writer::file_output_buffer::max_encodable()




/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param img The image to save.
 */
claw::graphic::pcx::writer::writer( const image& img )
  : m_image(img)
{

} // pcx::writer::writer()

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param img The image to save.
 * \param f The file in which we save the data.
 */
claw::graphic::pcx::writer::writer( const image& img, std::ostream& f )
  : m_image(img)
{
  save(f);
} // pcx::writer::writer()

/*----------------------------------------------------------------------------*/
/**
 * \brief Save the content of the image in a stream.
 * \param os The stream in which we write.
 */
void claw::graphic::pcx::writer::save( std::ostream& os ) const
{
  const unsigned int bytes_per_line = m_image.width() + m_image.width() % 2;

  write_header(os, bytes_per_line);
  save_rle_true_color(os, bytes_per_line);
} // pcx::writer::save()

/*----------------------------------------------------------------------------*/
/**
 * \brief Save the header of the image in a stream.
 * \param os The stream in which we write.
 * \param bytes_per_line Number of bytes per decoded line.
 */
void claw::graphic::pcx::writer::write_header
( std::ostream& os, unsigned int bytes_per_line ) const
{
  header h;

  h.manufacturer   = 10;
  h.version        = 5;
  h.encoded        = 1;
  h.bpp            = 8;
  h.window.x_min   = 0;
  h.window.y_min   = 0;
  h.window.x_max   = m_image.width() - 1;
  h.window.y_max   = m_image.height() - 1;
  h.horizontal_dpi = 72; // arbitrary value
  h.vertical_dpi   = 72;
  std::fill( h.color_map, h.color_map+16, rgb_pixel_8(0, 0, 0) );
  h.reserved               = 0;
  h.color_planes           = 3; // RGB
  h.bytes_per_line         = bytes_per_line;
  h.palette_info           = 0;
  h.screen_size.horizontal = 0;
  h.screen_size.vertical   = 0;
  std::fill( h.filler, h.filler+54, 0 );

  os.write( reinterpret_cast<char*>(&h), sizeof(header) );
} // pcx::writer::write_header()

/*----------------------------------------------------------------------------*/
/**
 * \brief Save the content of the image in a stream.
 * \param os The stream in which we write.
 * \param bytes_per_line Number of bytes per decoded line.
 */
void claw::graphic::pcx::writer::save_rle_true_color
( std::ostream& os, unsigned int bytes_per_line ) const
{
  std::vector<u_int_8> data(bytes_per_line, 0);

  rle_pcx_encoder encoder;
  file_output_buffer output(os);

  for (unsigned int y=0; y!=m_image.height(); ++y)
    {
      // red
      for (unsigned int x=0; x!=m_image.width(); ++x)
        data[x] = m_image[y][x].components.red;

      encoder.encode( data.begin(), data.end(), output );

      // green
      for (unsigned int x=0; x!=m_image.width(); ++x)
        data[x] = m_image[y][x].components.green;

      encoder.encode( data.begin(), data.end(), output );

      // blue
      for (unsigned int x=0; x!=m_image.width(); ++x)
        data[x] = m_image[y][x].components.blue;

      encoder.encode( data.begin(), data.end(), output );
    }
} // pcx::writer::save_rle_true_color()