bitmap_writer.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/bitmap.hpp>
#include <algorithm>
/*----------------------------------------------------------------------------*/
claw::graphic::bitmap::writer::writer( const image& img )
: m_image(img)
{
} // bitmap::writer::writer()
/*----------------------------------------------------------------------------*/
claw::graphic::bitmap::writer::writer( const image& img, std::ostream& f )
: m_image(img)
{
save(f);
} // bitmap::writer::writer()
/*----------------------------------------------------------------------------*/
void claw::graphic::bitmap::writer::save( std::ostream& f ) const
{
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
{
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()
/*----------------------------------------------------------------------------*/
void claw::graphic::bitmap::writer::pixel32_to_pixel24
( char* dest, const scanline& src ) const
{
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::init_header( header& h ) const
{
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()
claw::graphic::image::scanline
One line in the image.
Definition: image.hpp:59
claw::graphic::bitmap::file_structure::header::id
char id[2]
File identifier (must be &#39;BM&#39;).
Definition: bitmap.hpp:79
claw::graphic::bitmap::file_structure::header::width
unsigned int width
Image&#39;s width.
Definition: bitmap.hpp:94
claw::graphic::bitmap::file_structure::header::bpp
unsigned short bpp
Bits per pixel.
Definition: bitmap.hpp:103
claw::graphic::bitmap::file_structure::header::layers
unsigned short layers
Number of layers.
Definition: bitmap.hpp:100
claw::graphic::image::scanline::const_iterator
super::const_iterator const_iterator
Const iterator in the line.
Definition: image.hpp:81
claw::graphic::image::height
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:159
claw::graphic::bitmap::file_structure::header::image_size
unsigned int image_size
Image&#39;s size (bytes).
Definition: bitmap.hpp:109
claw::graphic::bitmap::writer::save
void save(std::ostream &f) const
Save the bitmap in a file.
Definition: bitmap_writer.cpp:61
claw::first
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:44
claw::graphic::image::scanline::end
iterator end()
Get en iterator past the last pixel.
Definition: image.cpp:63
bitmap.hpp
A class for bitmap pictures.
claw::graphic::bitmap::file_structure::header::header_size
unsigned int header_size
Header&#39;s size.
Definition: bitmap.hpp:91
claw::graphic::bitmap::file_structure::header
Header of a bitmap file.
Definition: bitmap.hpp:76
claw::graphic::image::width
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:147
claw::graphic::image::scanline::begin
iterator begin()
Get an iterator on the first pixel.
Definition: image.cpp:54
claw::graphic::bitmap::file_structure::header::nop
unsigned int nop
not used.
Definition: bitmap.hpp:85
claw::graphic::bitmap::file_structure::header::colors_count
unsigned int colors_count
Number of colors.
Definition: bitmap.hpp:118
claw::graphic::bitmap::file_structure::header::file_size
unsigned int file_size
File&#39;s size.
Definition: bitmap.hpp:82
claw::graphic::bitmap::file_structure::header::data_offset
unsigned int data_offset
Begininf of the datas.
Definition: bitmap.hpp:88
claw::graphic::bitmap::file_structure::header::importants_colors
unsigned int importants_colors
Number of important colors.
Definition: bitmap.hpp:121
claw::graphic::bitmap::file_structure::header::ppm_y
unsigned int ppm_y
Vertical resolution (pixels per meter).
Definition: bitmap.hpp:115
claw::graphic::bitmap::file_structure::header::height
unsigned int height
Image&#39;s height.
Definition: bitmap.hpp:97
claw::graphic::bitmap::writer::writer
writer(const image &img)
Constructor.
Definition: bitmap_writer.cpp:38
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::graphic::bitmap::file_structure::header::ppm_x
unsigned int ppm_x
Horizontal resolution (pixels per meter).
Definition: bitmap.hpp:112
claw::graphic::bitmap::file_structure::header::compression
unsigned int compression
Compression algorithm.
Definition: bitmap.hpp:106