pcx_writer.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <claw/pcx.hpp>
00031
00032
00037 claw::graphic::pcx::writer::file_output_buffer::file_output_buffer
00038 ( std::ostream& os )
00039 : m_stream(os)
00040 {
00041
00042 }
00043
00044
00050 void claw::graphic::pcx::writer::file_output_buffer::encode
00051 ( unsigned int n, pattern_type pattern )
00052 {
00053 if ( (pattern > 63) || (n > 1) )
00054 {
00055 u_int_8 cnt = 0xC0 | (u_int_8)n;
00056 m_stream.write( reinterpret_cast<char*>(&cnt), sizeof(u_int_8) );
00057 }
00058
00059 m_stream.write( reinterpret_cast<char*>(&pattern), sizeof(u_int_8));
00060 }
00061
00062
00066 unsigned int
00067 claw::graphic::pcx::writer::file_output_buffer::min_interesting() const
00068 {
00069 return 1;
00070 }
00071
00072
00076 unsigned int
00077 claw::graphic::pcx::writer::file_output_buffer::max_encodable() const
00078 {
00079 return 63;
00080 }
00081
00082
00083
00084
00085
00090 claw::graphic::pcx::writer::writer( const image& img )
00091 : m_image(img)
00092 {
00093
00094 }
00095
00096
00102 claw::graphic::pcx::writer::writer( const image& img, std::ostream& f )
00103 : m_image(img)
00104 {
00105 save(f);
00106 }
00107
00108
00113 void claw::graphic::pcx::writer::save( std::ostream& os ) const
00114 {
00115 const unsigned int bytes_per_line = m_image.width() + m_image.width() % 2;
00116
00117 write_header(os, bytes_per_line);
00118 save_rle_true_color(os, bytes_per_line);
00119 }
00120
00121
00127 void claw::graphic::pcx::writer::write_header
00128 ( std::ostream& os, unsigned int bytes_per_line ) const
00129 {
00130 header h;
00131
00132 h.manufacturer = 10;
00133 h.version = 5;
00134 h.encoded = 1;
00135 h.bpp = 8;
00136 h.window.x_min = 0;
00137 h.window.y_min = 0;
00138 h.window.x_max = m_image.width() - 1;
00139 h.window.y_max = m_image.height() - 1;
00140 h.horizontal_dpi = 72;
00141 h.vertical_dpi = 72;
00142 std::fill( h.color_map, h.color_map+16, rgb_pixel_8(0, 0, 0) );
00143 h.reserved = 0;
00144 h.color_planes = 3;
00145 h.bytes_per_line = bytes_per_line;
00146 h.palette_info = 0;
00147 h.screen_size.horizontal = 0;
00148 h.screen_size.vertical = 0;
00149 std::fill( h.filler, h.filler+54, 0 );
00150
00151 os.write( reinterpret_cast<char*>(&h), sizeof(header) );
00152 }
00153
00154
00160 void claw::graphic::pcx::writer::save_rle_true_color
00161 ( std::ostream& os, unsigned int bytes_per_line ) const
00162 {
00163 std::vector<u_int_8> data(bytes_per_line, 0);
00164
00165 rle_pcx_encoder encoder;
00166 file_output_buffer output(os);
00167
00168 for (unsigned int y=0; y!=m_image.height(); ++y)
00169 {
00170
00171 for (unsigned int x=0; x!=m_image.width(); ++x)
00172 data[x] = m_image[y][x].components.red;
00173
00174 encoder.encode( data.begin(), data.end(), output );
00175
00176
00177 for (unsigned int x=0; x!=m_image.width(); ++x)
00178 data[x] = m_image[y][x].components.green;
00179
00180 encoder.encode( data.begin(), data.end(), output );
00181
00182
00183 for (unsigned int x=0; x!=m_image.width(); ++x)
00184 data[x] = m_image[y][x].components.blue;
00185
00186 encoder.encode( data.begin(), data.end(), output );
00187 }
00188 }