A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Forward Declarations
44 // =================================================================================================
45 
46 class Color;
47 
48 template<typename T>
49 class Matrix;
50 
51 // =================================================================================================
52 // Bmp Writer
53 // =================================================================================================
54 
58 class BmpWriter
59 {
60 public:
61 
62  // ---------------------------------------------------------------------
63  // Typedefs and Enums
64  // ---------------------------------------------------------------------
65 
86  {
87  uint16_t bfType = 0x4D42; // B=42 M=4D
88  uint32_t bfSize;
89  uint16_t bfReserved1 = 0;
90  uint16_t bfReserved2 = 0;
91  uint32_t bfOffBits = 14 + 40;
92  };
93 
125  {
126  uint32_t biSize = 40;
127  uint32_t biWidth;
128  uint32_t biHeight;
129  uint16_t biPlanes = 1;
130  uint16_t biBitCount = 8;
131  uint32_t biCompression = 0;
132  uint32_t biSizeImage = 0;
133  uint32_t biXPelsPerMeter = 0;
134  uint32_t biYPelsPerMeter = 0;
135  uint32_t biClrUsed = 0;
136  uint32_t biClrImportant = 0;
137  };
138 
147  struct RgbQuad
148  {
149  unsigned char rgbBlue;
150  unsigned char rgbGreen;
151  unsigned char rgbRed;
152  unsigned char rgbReserved = 0;
153  };
154 
166  struct BitmapInfo {
168  std::vector<RgbQuad> bmiColors;
169  };
170 
171  // ---------------------------------------------------------------------
172  // Constructor and Rule of Five
173  // ---------------------------------------------------------------------
174 
176  BmpWriter() = default;
177 
179  ~BmpWriter() = default;
180 
182  BmpWriter( BmpWriter const& ) = default;
183 
185  BmpWriter( BmpWriter&& ) = default;
186 
188  BmpWriter& operator= ( BmpWriter const& ) = default;
189 
191  BmpWriter& operator= ( BmpWriter&& ) = default;
192 
193  // ---------------------------------------------------------------------
194  // Writing
195  // ---------------------------------------------------------------------
196 
200  void to_stream( Matrix<Color> const& image, std::ostream& outstream ) const;
201 
205  void to_file( Matrix<Color> const& image, std::string const& filename ) const;
206 
213  void to_stream( Matrix<unsigned char> const& image, std::ostream& outstream ) const;
214 
221  void to_file( Matrix<unsigned char> const& image, std::string const& filename ) const;
222 
230  void to_stream(
231  Matrix<unsigned char> const& image, std::vector<Color> const& palette, std::ostream& outstream
232  ) const;
233 
241  void to_file(
242  Matrix<unsigned char> const& image, std::vector<Color> const& palette, std::string const& filename
243  ) const;
244 
245  // ---------------------------------------------------------------------
246  // Internal Helpers
247  // ---------------------------------------------------------------------
248 
249 private:
250 
251  void write_uint16_( uint16_t data, std::ostream& target ) const;
252  void write_uint32_( uint32_t data, std::ostream& target ) const;
253 
254  void write_file_header_( BitmapFileheader const& header, std::ostream& target ) const;
255  void write_info_header_( BitmapInfoheader const& header, std::ostream& target ) const;
256 
257  void write_info_( BitmapInfo const& info, std::ostream& target ) const;
258 
259 };
260 
261 } // namespace utils
262 } // namespace genesis
263 
264 #endif // include guard
Bitmap info that describes dimensions and color information.
void to_file(Matrix< Color > const &image, std::string const &filename) const
Write a full 24bit RGB Color image to a file.
Write Bitmap image files.
BmpWriter()=default
Default constructor.
Palette entry that describes an RGB Color.
void to_stream(Matrix< Color > const &image, std::ostream &outstream) const
Write a full 24bit RGB Color image to a stream.
BmpWriter & operator=(BmpWriter const &)=default
Default copy assignemnt.
~BmpWriter()=default
Default destructor.