Claw  1.7.3
code/targa_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-2011 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@gamned.org
00024 */
00030 #include <claw/targa.hpp>
00031 #include <claw/exception.hpp>
00032 
00033 
00034 //********************** targa::writer::file_output_buffer *********************
00035 
00036 
00037 
00038 
00039 namespace claw
00040 {
00041   namespace graphic
00042   {
00043     /*------------------------------------------------------------------------*/
00051     template< >
00052     void targa::writer::file_output_buffer<claw::graphic::rgba_pixel_8>::
00053     order_pixel_bytes( const pixel_type& p )
00054     {
00055       m_stream << p.components.blue << p.components.green
00056          << p.components.red << p.components.alpha;
00057     } // targa::writer::file_output_buffer::order_pixel_bytes()
00058   } // namespace graphic
00059 } // namespace claw
00060 
00061 
00062 //************************** targa::writer::writer *****************************
00063 
00064 
00065 
00066 
00067 /*----------------------------------------------------------------------------*/
00072 claw::graphic::targa::writer::writer( const image& img )
00073   : m_image(img)
00074 {
00075 
00076 } // targa::writer::writer()
00077 
00078 /*----------------------------------------------------------------------------*/
00085 claw::graphic::targa::writer::writer
00086 ( const image& img, std::ostream& f, bool rle )
00087   : m_image(img)
00088 {
00089   save(f, rle);
00090 } // targa::writer::writer()
00091 
00092 /*----------------------------------------------------------------------------*/
00098 void claw::graphic::targa::writer::save( std::ostream& os, bool rle ) const
00099 {
00100   header h( m_image.width(), m_image.height() );
00101 
00102   if (rle)
00103     h.image_type = rle_true_color;
00104   else
00105     h.image_type = true_color;
00106 
00107   os.write( reinterpret_cast<char*>(&h), sizeof(header) );
00108 
00109   if (rle)
00110     save_rle_true_color(os);
00111   else
00112     save_true_color(os);
00113 
00114   footer f;
00115   os.write( reinterpret_cast<char*>(&f), sizeof(footer) );
00116 } // targa::writer::save()
00117 
00118 /*----------------------------------------------------------------------------*/
00123 void claw::graphic::targa::writer::save_true_color( std::ostream& os ) const
00124 {
00125   file_output_buffer<rgba_pixel_8> output_buffer(os);
00126 
00127   for (const_iterator it=m_image.begin(); it!=m_image.end(); ++it)
00128     output_buffer.order_pixel_bytes(*it);
00129 } // targa::writer::save_true_color()
00130 
00131 /*----------------------------------------------------------------------------*/
00136 void claw::graphic::targa::writer::save_rle_true_color( std::ostream& os ) const
00137 {
00138   rle32_encoder encoder;
00139   rle32_encoder::output_buffer_type output_buffer(os);
00140 
00141   for ( unsigned int y=0; y!=m_image.height(); ++y )
00142     encoder.encode( m_image[y].begin(), m_image[y].end(), output_buffer );
00143 } // targa::writer::save_rle_true_color()