CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Member Functions | Private Member Functions | Private Attributes
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:
imgThe image to save.

Definition at line 38 of file bitmap_writer.cpp.

  : m_image(img)
{

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

Constructor.

Parameters:
imgThe image to save.
fThe file in which we save the data.

Definition at line 50 of file bitmap_writer.cpp.

References save().

  : m_image(img)
{
  save(f);
} // bitmap::writer::writer()

Member Function Documentation

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

Initialize header's data, for saving.

Parameters:
hHeader to initialize.

Definition at line 124 of file bitmap_writer.cpp.

References 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::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, claw::graphic::bitmap::file_structure::header::nop, claw::graphic::bitmap::file_structure::header::ppm_x, claw::graphic::bitmap::file_structure::header::ppm_y, and claw::graphic::bitmap::file_structure::header::width.

{
  unsigned int adjusted_line = m_image.width() * 3;

  if (m_image.width() % 4 != 0)
    adjusted_line += 4 - m_image.width() % 4;

  // for a 24 bpp bitmap.
  h.id[0] = 'B'; 
  h.id[1] = 'M';
  h.file_size = adjusted_line * m_image.height() + sizeof(h);
  h.nop = 0;

  // there is no color pallet, so data is just after the h
  h.data_offset = sizeof(h);
  // default value for Windows' bitmaps.
  h.header_size = 0x28;

  h.width = m_image.width();
  h.height = m_image.height();
  h.layers = 1;
  h.bpp = 24;
  h.compression = BMP_COMPRESSION_RGB;
  h.image_size = adjusted_line * m_image.height();
  h.ppm_x = 0x2E23;     // 11811
  h.ppm_y = 0x2E23;
  h.colors_count = 0;
  h.importants_colors = 0;
} // 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.
srcScanline to convert.

Definition at line 105 of file bitmap_writer.cpp.

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

{
  unsigned int i24 = 0;
  scanline::const_iterator first( src.begin() );
  scanline::const_iterator last( src.end() );

  for ( ; first!=last; ++first )
    {
      dest[i24++] = first->components.blue;
      dest[i24++] = first->components.green;
      dest[i24++] = first->components.red;
    }
} // bitmap::writer::pixel32_to_pixel24()
void claw::graphic::bitmap::writer::save ( std::ostream &  f) const

Save the bitmap in a file.

Parameters:
fDestination file.

Definition at line 61 of file bitmap_writer.cpp.

Referenced by writer().

{
  header h;

  init_header(h);
        
  f.write( reinterpret_cast<char*>(&h), sizeof(header) );

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

Saves a 24 bpp bitmap file.

Parameters:
fBitmap file.

Definition at line 77 of file bitmap_writer.cpp.

{
  unsigned int line;
  unsigned int buffer_size = m_image.width() * 3;

  // lines are 4-bytes aligned, so adjust buffer's size.
  if (buffer_size % 4 != 0) 
    buffer_size += 4 - buffer_size % 4;

  char* buffer = new char[buffer_size];

  for (line = m_image.height(); line>0; )
    {
      --line;
      pixel32_to_pixel24( buffer, m_image[line] );
      f.write(buffer, buffer_size);
    }

  delete[] buffer;
} // bitmap::writer::save_data()

Member Data Documentation

The image from which we read the data.

Definition at line 313 of file bitmap.hpp.


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