claw  1.9.0
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@stuff-o-matic.com
24 */
30 #include <claw/graphic/bitmap.hpp>
31 
32 #include <algorithm>
33 
39  : m_image(img)
40 {}
41 
47 claw::graphic::bitmap::writer::writer(const image& img, std::ostream& f)
48  : m_image(img)
49 {
50  save(f);
51 }
52 
57 void claw::graphic::bitmap::writer::save(std::ostream& f) const
58 {
59  header h;
60 
61  init_header(h);
62 
63  f.write(reinterpret_cast<char*>(&h), sizeof(header));
64 
65  save_data(f);
66 }
67 
72 void claw::graphic::bitmap::writer::save_data(std::ostream& f) const
73 {
74  unsigned int line;
75  unsigned int buffer_size = m_image.width() * 3;
76 
77  // lines are 4-bytes aligned, so adjust buffer's size.
78  if(buffer_size % 4 != 0)
79  buffer_size += 4 - buffer_size % 4;
80 
81  char* buffer = new char[buffer_size];
82 
83  for(line = m_image.height(); line > 0;)
84  {
85  --line;
86  pixel32_to_pixel24(buffer, m_image[line]);
87  f.write(buffer, buffer_size);
88  }
89 
90  delete[] buffer;
91 }
92 
98 void claw::graphic::bitmap::writer::pixel32_to_pixel24(
99  char* dest, const scanline& src) const
100 {
101  unsigned int i24 = 0;
102  scanline::const_iterator first(src.begin());
103  scanline::const_iterator last(src.end());
104 
105  for(; first != last; ++first)
106  {
107  dest[i24++] = first->components.blue;
108  dest[i24++] = first->components.green;
109  dest[i24++] = first->components.red;
110  }
111 }
112 
117 void claw::graphic::bitmap::writer::init_header(header& h) const
118 {
119  unsigned int adjusted_line = m_image.width() * 3;
120 
121  if(m_image.width() % 4 != 0)
122  adjusted_line += 4 - m_image.width() % 4;
123 
124  // for a 24 bpp bitmap.
125  h.id[0] = 'B';
126  h.id[1] = 'M';
127  h.file_size = adjusted_line * m_image.height() + sizeof(h);
128  h.nop = 0;
129 
130  // there is no color pallet, so data is just after the h
131  h.data_offset = sizeof(h);
132  // default value for Windows' bitmaps.
133  h.header_size = 0x28;
134 
135  h.width = m_image.width();
136  h.height = m_image.height();
137  h.layers = 1;
138  h.bpp = 24;
139  h.compression = BMP_COMPRESSION_RGB;
140  h.image_size = adjusted_line * m_image.height();
141  h.ppm_x = 0x2E23; // 11811
142  h.ppm_y = 0x2E23;
143  h.colors_count = 0;
144  h.importants_colors = 0;
145 }
super::const_iterator const_iterator
Const iterator in the line.
Definition: image.hpp:82
void save(std::ostream &f) const
Save the bitmap in a file.
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:42
A class for bitmap pictures.
writer(const image &img)
Constructor.
A class to deal with images.
Definition: image.hpp:50