A library for working with phylogenetic and population genetic data.
v0.32.0
utils/formats/bmp/writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_BMP_WRITER_H_
2 #define GENESIS_UTILS_FORMATS_BMP_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cstdint>
35 #include <iosfwd>
36 #include <string>
37 #include <vector>
38 
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 class Color;
49 
50 template<typename T>
51 class Matrix;
52 
53 // =================================================================================================
54 // Bmp Writer
55 // =================================================================================================
56 
60 class BmpWriter
61 {
62 public:
63 
64  // ---------------------------------------------------------------------
65  // Typedefs and Enums
66  // ---------------------------------------------------------------------
67 
88  {
89  uint16_t bfType = 0x4D42; // B=42 M=4D
90  uint32_t bfSize;
91  uint16_t bfReserved1 = 0;
92  uint16_t bfReserved2 = 0;
93  uint32_t bfOffBits = 14 + 40;
94  };
95 
127  {
128  uint32_t biSize = 40;
129  uint32_t biWidth;
130  uint32_t biHeight;
131  uint16_t biPlanes = 1;
132  uint16_t biBitCount = 8;
133  uint32_t biCompression = 0;
134  uint32_t biSizeImage = 0;
135  uint32_t biXPelsPerMeter = 0;
136  uint32_t biYPelsPerMeter = 0;
137  uint32_t biClrUsed = 0;
138  uint32_t biClrImportant = 0;
139  };
140 
149  struct RgbQuad
150  {
151  unsigned char rgbBlue;
152  unsigned char rgbGreen;
153  unsigned char rgbRed;
154  unsigned char rgbReserved = 0;
155  };
156 
168  struct BitmapInfo {
170  std::vector<RgbQuad> bmiColors;
171  };
172 
173  // ---------------------------------------------------------------------
174  // Constructor and Rule of Five
175  // ---------------------------------------------------------------------
176 
178  BmpWriter() = default;
179 
181  ~BmpWriter() = default;
182 
184  BmpWriter( BmpWriter const& ) = default;
185 
187  BmpWriter( BmpWriter&& ) = default;
188 
190  BmpWriter& operator= ( BmpWriter const& ) = default;
191 
193  BmpWriter& operator= ( BmpWriter&& ) = default;
194 
195  // ---------------------------------------------------------------------
196  // Writing
197  // ---------------------------------------------------------------------
198 
206  void write(
207  Matrix<Color> const& image,
208  std::shared_ptr<utils::BaseOutputTarget> target
209  ) const;
210 
221  void write(
222  Matrix<unsigned char> const& image,
223  std::shared_ptr<utils::BaseOutputTarget> target
224  ) const;
225 
237  void write(
238  Matrix<unsigned char> const& image,
239  std::vector<Color> const& palette,
240  std::shared_ptr<utils::BaseOutputTarget> target
241  ) const;
242 
243  // ---------------------------------------------------------------------
244  // Internal Helpers
245  // ---------------------------------------------------------------------
246 
247 private:
248 
249  void write_uint16_( uint16_t data, std::ostream& target ) const;
250  void write_uint32_( uint32_t data, std::ostream& target ) const;
251 
252  void write_file_header_( BitmapFileheader const& header, std::ostream& target ) const;
253  void write_info_header_( BitmapInfoheader const& header, std::ostream& target ) const;
254 
255  void write_info_( BitmapInfo const& info, std::ostream& target ) const;
256 
257 };
258 
259 } // namespace utils
260 } // namespace genesis
261 
262 #endif // include guard
genesis::utils::BmpWriter::~BmpWriter
~BmpWriter()=default
Default destructor.
genesis::utils::BmpWriter::BitmapInfoheader::biClrImportant
uint32_t biClrImportant
Definition: utils/formats/bmp/writer.hpp:138
genesis::utils::BmpWriter::operator=
BmpWriter & operator=(BmpWriter const &)=default
Default copy assignemnt.
genesis::utils::BmpWriter::BitmapFileheader::bfType
uint16_t bfType
Definition: utils/formats/bmp/writer.hpp:89
genesis::utils::BmpWriter::BitmapInfoheader::biWidth
uint32_t biWidth
Definition: utils/formats/bmp/writer.hpp:129
genesis::utils::BmpWriter::BitmapInfoheader::biYPelsPerMeter
uint32_t biYPelsPerMeter
Definition: utils/formats/bmp/writer.hpp:136
genesis::utils::BmpWriter::BitmapInfoheader::biXPelsPerMeter
uint32_t biXPelsPerMeter
Definition: utils/formats/bmp/writer.hpp:135
genesis::utils::BmpWriter::BitmapFileheader::bfReserved1
uint16_t bfReserved1
Definition: utils/formats/bmp/writer.hpp:91
genesis::utils::BmpWriter::RgbQuad::rgbBlue
unsigned char rgbBlue
Definition: utils/formats/bmp/writer.hpp:151
genesis::utils::BmpWriter::RgbQuad::rgbGreen
unsigned char rgbGreen
Definition: utils/formats/bmp/writer.hpp:152
genesis::utils::BmpWriter::BitmapFileheader::bfReserved2
uint16_t bfReserved2
Definition: utils/formats/bmp/writer.hpp:92
genesis::utils::BmpWriter::BitmapFileheader::bfOffBits
uint32_t bfOffBits
Definition: utils/formats/bmp/writer.hpp:93
genesis::utils::BmpWriter::BitmapInfoheader::biPlanes
uint16_t biPlanes
Definition: utils/formats/bmp/writer.hpp:131
genesis::utils::Matrix
Definition: placement/function/emd.hpp:53
genesis::utils::BmpWriter::BitmapInfo::bmiColors
std::vector< RgbQuad > bmiColors
Definition: utils/formats/bmp/writer.hpp:170
genesis::utils::BmpWriter::BitmapInfoheader::biClrUsed
uint32_t biClrUsed
Definition: utils/formats/bmp/writer.hpp:137
genesis::utils::BmpWriter::write
void write(Matrix< Color > const &image, std::shared_ptr< utils::BaseOutputTarget > target) const
Write a full 24bit RGB Color image to an output target.
Definition: utils/formats/bmp/writer.cpp:53
genesis::utils::BmpWriter::BitmapInfoheader::biSize
uint32_t biSize
Definition: utils/formats/bmp/writer.hpp:128
genesis::utils::BmpWriter::RgbQuad
Palette entry that describes an RGB Color.
Definition: utils/formats/bmp/writer.hpp:149
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::BmpWriter::RgbQuad::rgbReserved
unsigned char rgbReserved
Definition: utils/formats/bmp/writer.hpp:154
genesis::utils::BmpWriter::BitmapInfo
Bitmap info that describes dimensions and color information.
Definition: utils/formats/bmp/writer.hpp:168
genesis::utils::BmpWriter::BitmapFileheader::bfSize
uint32_t bfSize
Definition: utils/formats/bmp/writer.hpp:90
genesis::utils::BmpWriter
Write Bitmap image files.
Definition: utils/formats/bmp/writer.hpp:60
genesis::utils::BmpWriter::RgbQuad::rgbRed
unsigned char rgbRed
Definition: utils/formats/bmp/writer.hpp:153
genesis::utils::BmpWriter::BitmapInfo::bmiHeader
BitmapInfoheader bmiHeader
Definition: utils/formats/bmp/writer.hpp:169
output_target.hpp
genesis::utils::BmpWriter::BmpWriter
BmpWriter()=default
Default constructor.
genesis::utils::BmpWriter::BitmapInfoheader::biBitCount
uint16_t biBitCount
Definition: utils/formats/bmp/writer.hpp:132
genesis::utils::BmpWriter::BitmapInfoheader::biCompression
uint32_t biCompression
Definition: utils/formats/bmp/writer.hpp:133
genesis::utils::BmpWriter::BitmapInfoheader::biSizeImage
uint32_t biSizeImage
Definition: utils/formats/bmp/writer.hpp:134
genesis::utils::BmpWriter::BitmapInfoheader
Bitmap Info Header.
Definition: utils/formats/bmp/writer.hpp:126
genesis::utils::BmpWriter::BitmapFileheader
Bitmap File Header.
Definition: utils/formats/bmp/writer.hpp:87
genesis::utils::BmpWriter::BitmapInfoheader::biHeight
uint32_t biHeight
Definition: utils/formats/bmp/writer.hpp:130