Claw 1.7.3
bitmap_writer.cpp
Go to the documentation of this file.
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
30#include <claw/bitmap.hpp>
31#include <algorithm>
32
33/*----------------------------------------------------------------------------*/
39 : m_image(img)
40{
41
42} // bitmap::writer::writer()
43
44/*----------------------------------------------------------------------------*/
50claw::graphic::bitmap::writer::writer( const image& img, std::ostream& f )
51 : m_image(img)
52{
53 save(f);
54} // bitmap::writer::writer()
55
56/*----------------------------------------------------------------------------*/
61void claw::graphic::bitmap::writer::save( std::ostream& f ) const
62{
63 header h;
64
65 init_header(h);
66
67 f.write( reinterpret_cast<char*>(&h), sizeof(header) );
68
69 save_data( f );
70} // bitmap::writer::save()
71
72/*----------------------------------------------------------------------------*/
77void claw::graphic::bitmap::writer::save_data( std::ostream& f ) const
78{
79 unsigned int line;
80 unsigned int buffer_size = m_image.width() * 3;
81
82 // lines are 4-bytes aligned, so adjust buffer's size.
83 if (buffer_size % 4 != 0)
84 buffer_size += 4 - buffer_size % 4;
85
86 char* buffer = new char[buffer_size];
87
88 for (line = m_image.height(); line>0; )
89 {
90 --line;
91 pixel32_to_pixel24( buffer, m_image[line] );
92 f.write(buffer, buffer_size);
93 }
94
95 delete[] buffer;
96} // bitmap::writer::save_data()
97
98/*----------------------------------------------------------------------------*/
104void claw::graphic::bitmap::writer::pixel32_to_pixel24
105( char* dest, const scanline& src ) const
106{
107 unsigned int i24 = 0;
108 scanline::const_iterator first( src.begin() );
109 scanline::const_iterator last( src.end() );
110
111 for ( ; first!=last; ++first )
112 {
113 dest[i24++] = first->components.blue;
114 dest[i24++] = first->components.green;
115 dest[i24++] = first->components.red;
116 }
117} // bitmap::writer::pixel32_to_pixel24()
118
119/*----------------------------------------------------------------------------*/
124void claw::graphic::bitmap::writer::init_header( header& h ) const
125{
126 unsigned int adjusted_line = m_image.width() * 3;
127
128 if (m_image.width() % 4 != 0)
129 adjusted_line += 4 - m_image.width() % 4;
130
131 // for a 24 bpp bitmap.
132 h.id[0] = 'B';
133 h.id[1] = 'M';
134 h.file_size = adjusted_line * m_image.height() + sizeof(h);
135 h.nop = 0;
136
137 // there is no color pallet, so data is just after the h
138 h.data_offset = sizeof(h);
139 // default value for Windows' bitmaps.
140 h.header_size = 0x28;
141
142 h.width = m_image.width();
143 h.height = m_image.height();
144 h.layers = 1;
145 h.bpp = 24;
146 h.compression = BMP_COMPRESSION_RGB;
147 h.image_size = adjusted_line * m_image.height();
148 h.ppm_x = 0x2E23; // 11811
149 h.ppm_y = 0x2E23;
150 h.colors_count = 0;
151 h.importants_colors = 0;
152} // bitmap::writer::init_header()
153
A class for bitmap pictures.
Fuction object to get the first element of a std::pair.
writer(const image &img)
Constructor.
void save(std::ostream &f) const
Save the bitmap in a file.
A class to deal with images.
Definition image.hpp:50