claw::graphic::bitmap::writer Class Reference

This class write an image in a bitmap file. More...

#include <bitmap.hpp>

Inheritance diagram for claw::graphic::bitmap::writer:
claw::graphic::bitmap::file_structure

List of all members.

Public Member Functions

 writer (const image &img)
 Constructor.
 writer (const image &img, std::ostream &f)
 Constructor.
void save (std::ostream &f) const
 Save the bitmap in a file.

Private Member Functions

void save_data (std::ostream &f) const
 Saves a 24 bpp bitmap file.
void pixel32_to_pixel24 (char *dest, const scanline &src) const
 Converts a pixel32 scanline to a BGR array.
void init_header (header &h) const
 Initialize header's data, for saving.

Private Attributes

const imagem_image
 The image from which we read the data.

Detailed Description

This class write an image in a bitmap file.

Author:
Julien Jorge

Definition at line 296 of file bitmap.hpp.


Constructor & Destructor Documentation

claw::graphic::bitmap::writer::writer ( const image img  ) 

Constructor.

Parameters:
img The image to save.

Definition at line 38 of file bitmap_writer.cpp.

00039   : m_image(img)
00040 {
00041 
00042 } // bitmap::writer::writer()

claw::graphic::bitmap::writer::writer ( const image img,
std::ostream &  f 
)

Constructor.

Parameters:
img The image to save.
f The file in which we save the data.

Definition at line 50 of file bitmap_writer.cpp.

References save().

00051   : m_image(img)
00052 {
00053   save(f);
00054 } // bitmap::writer::writer()


Member Function Documentation

void claw::graphic::bitmap::writer::init_header ( header h  )  const [private]

Initialize header's data, for saving.

Parameters:
h Header to initialize.

Definition at line 124 of file bitmap_writer.cpp.

References claw::graphic::bitmap::file_structure::BMP_COMPRESSION_RGB, claw::graphic::bitmap::file_structure::header::bpp, claw::graphic::bitmap::file_structure::header::colors_count, claw::graphic::bitmap::file_structure::header::compression, claw::graphic::bitmap::file_structure::header::data_offset, claw::graphic::bitmap::file_structure::header::file_size, claw::graphic::bitmap::file_structure::header::header_size, claw::graphic::bitmap::file_structure::header::height, claw::graphic::image::height(), claw::graphic::bitmap::file_structure::header::id, claw::graphic::bitmap::file_structure::header::image_size, claw::graphic::bitmap::file_structure::header::importants_colors, claw::graphic::bitmap::file_structure::header::layers, m_image, claw::graphic::bitmap::file_structure::header::nop, claw::graphic::bitmap::file_structure::header::ppm_x, claw::graphic::bitmap::file_structure::header::ppm_y, claw::graphic::bitmap::file_structure::header::width, and claw::graphic::image::width().

Referenced by save().

00125 {
00126   unsigned int adjusted_line = m_image.width() * 3;
00127 
00128   if (m_image.width() % 4 != 0)
00129     adjusted_line += 4 - m_image.width() % 4;
00130 
00131   // for a 24 bpp bitmap.
00132   h.id[0] = 'B'; 
00133   h.id[1] = 'M';
00134   h.file_size = adjusted_line * m_image.height() + sizeof(h);
00135   h.nop = 0;
00136 
00137   // there is no color pallet, so data is just after the h
00138   h.data_offset = sizeof(h);
00139   // default value for Windows' bitmaps.
00140   h.header_size = 0x28;
00141 
00142   h.width = m_image.width();
00143   h.height = m_image.height();
00144   h.layers = 1;
00145   h.bpp = 24;
00146   h.compression = BMP_COMPRESSION_RGB;
00147   h.image_size = adjusted_line * m_image.height();
00148   h.ppm_x = 0x2E23;     // 11811
00149   h.ppm_y = 0x2E23;
00150   h.colors_count = 0;
00151   h.importants_colors = 0;
00152 } // bitmap::writer::init_header()

void claw::graphic::bitmap::writer::pixel32_to_pixel24 ( char *  dest,
const scanline src 
) const [private]

Converts a pixel32 scanline to a BGR array.

Parameters:
dest (out) Filled array.
src Scanline to convert.

Definition at line 105 of file bitmap_writer.cpp.

References claw::graphic::image::scanline::begin(), and claw::graphic::image::scanline::end().

Referenced by save_data().

00106 {
00107   unsigned int i24 = 0;
00108   scanline::const_iterator first( src.begin() );
00109   scanline::const_iterator last( src.end() );
00110 
00111   for ( ; first!=last; ++first )
00112     {
00113       dest[i24++] = first->components.blue;
00114       dest[i24++] = first->components.green;
00115       dest[i24++] = first->components.red;
00116     }
00117 } // bitmap::writer::pixel32_to_pixel24()

void claw::graphic::bitmap::writer::save ( std::ostream &  f  )  const

Save the bitmap in a file.

Parameters:
f Destination file.

Definition at line 61 of file bitmap_writer.cpp.

References init_header(), and save_data().

Referenced by writer().

00062 {
00063   header h;
00064 
00065   init_header(h);
00066         
00067   f.write( reinterpret_cast<char*>(&h), sizeof(header) );
00068 
00069   save_data( f );
00070 } // bitmap::writer::save()

void claw::graphic::bitmap::writer::save_data ( std::ostream &  f  )  const [private]

Saves a 24 bpp bitmap file.

Parameters:
f Bitmap file.

Definition at line 77 of file bitmap_writer.cpp.

References claw::graphic::image::height(), m_image, pixel32_to_pixel24(), and claw::graphic::image::width().

Referenced by save().

00078 {
00079   unsigned int line;
00080   unsigned int buffer_size = m_image.width() * 3;
00081 
00082   // lines are 4-bytes aligned, so adjust buffer's size.
00083   if (buffer_size % 4 != 0) 
00084     buffer_size += 4 - buffer_size % 4;
00085 
00086   char* buffer = new char[buffer_size];
00087 
00088   for (line = m_image.height(); line>0; )
00089     {
00090       --line;
00091       pixel32_to_pixel24( buffer, m_image[line] );
00092       f.write(buffer, buffer_size);
00093     }
00094 
00095   delete[] buffer;
00096 } // bitmap::writer::save_data()


Member Data Documentation

The image from which we read the data.

Definition at line 313 of file bitmap.hpp.

Referenced by init_header(), and save_data().


The documentation for this class was generated from the following files:

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