This class write an image in a bitmap file. More...
#include <bitmap.hpp>
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 image & | m_image |
The image from which we read the data. |
This class write an image in a bitmap file.
Definition at line 296 of file bitmap.hpp.
claw::graphic::bitmap::writer::writer | ( | const image & | img | ) |
Constructor.
img | The 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.
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().
void claw::graphic::bitmap::writer::init_header | ( | header & | h | ) | const [private] |
Initialize header's data, for saving.
h | Header 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::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().
{ 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.
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().
{ 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.
f | Destination file. |
Definition at line 61 of file bitmap_writer.cpp.
References init_header(), and save_data().
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.
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().
{ 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()
const image& claw::graphic::bitmap::writer::m_image [private] |
The image from which we read the data.
Definition at line 313 of file bitmap.hpp.
Referenced by init_header(), and save_data().