pcx_writer.cpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
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 } // pcx::writer::file_output_buffer::file_output_buffer()
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 } // pcx::writer::file_output_buffer::encode()
00061 
00062 /*----------------------------------------------------------------------------*/
00066 unsigned int
00067 claw::graphic::pcx::writer::file_output_buffer::min_interesting() const
00068 {
00069   return 1;
00070 } // pcx::writer::file_output_buffer::min_interesting()
00071 
00072 /*----------------------------------------------------------------------------*/
00076 unsigned int
00077 claw::graphic::pcx::writer::file_output_buffer::max_encodable() const
00078 {
00079   return 63;
00080 } // pcx::writer::file_output_buffer::max_encodable()
00081 
00082 
00083 
00084 
00085 /*----------------------------------------------------------------------------*/
00090 claw::graphic::pcx::writer::writer( const image& img )
00091   : m_image(img)
00092 {
00093 
00094 } // pcx::writer::writer()
00095 
00096 /*----------------------------------------------------------------------------*/
00102 claw::graphic::pcx::writer::writer( const image& img, std::ostream& f )
00103   : m_image(img)
00104 {
00105   save(f);
00106 } // pcx::writer::writer()
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 } // pcx::writer::save()
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; // arbitrary value
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; // RGB
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 } // pcx::writer::write_header()
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       // red
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       // green
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       // blue
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 } // pcx::writer::save_rle_true_color()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1