A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bitmap.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
40 
41 #include <algorithm>
42 #include <cassert>
43 #include <sstream>
44 
45 namespace genesis {
46 namespace sequence {
47 
48 // =================================================================================================
49 // Print
50 // =================================================================================================
51 
52 void PrinterBitmap::to_stream( SequenceSet const& set, std::ostream& outstream ) const
53 {
54  auto image = make_image_( set );
55  auto writer = utils::BmpWriter();
56  writer.to_stream( image, outstream );
57 }
58 
59 void PrinterBitmap::to_file( SequenceSet const& set, std::string const& filename ) const
60 {
61  auto image = make_image_( set );
62  auto writer = utils::BmpWriter();
63  writer.to_file( image, filename );
64 }
65 
66 // =================================================================================================
67 // Internal Helper Functions
68 // =================================================================================================
69 
70 utils::Matrix<utils::Color> PrinterBitmap::make_image_( SequenceSet const& set ) const
71 {
72  // Create a black image of the correct size.
73  size_t const max_line = longest_sequence_length( set );
74  auto image = utils::Matrix<utils::Color>(
75  set.size() * pixel_height_,
76  max_line * pixel_width_,
77  utils::Color( 0.0, 0.0, 0.0 )
78  );
79 
80  // Iterate the sequences in the set.
81  for( size_t r = 0; r < set.size(); ++r ) {
82  auto const& seq = set[r];
83 
84  // Iterate the chars of the sequence for this row.
85  for( size_t c = 0; c < seq.length(); ++c ) {
86 
87  // Find the color for the current char, or use black if no color available.
88  auto pixel = utils::Color( 0.0, 0.0, 0.0 );
89  auto color_it = color_map_.find( seq[c] );
90  if( color_it != color_map_.end() ) {
91  pixel = color_it->second;
92  }
93 
94  // Repeat the line as many times as needed.
95  for( size_t ph = 0; ph < pixel_height_; ++ph ) {
96 
97  // Set as many pixel in width as needed.
98  for( size_t pw = 0; pw < pixel_width_; ++pw ) {
99  image( r * pixel_height_ + ph, c * pixel_width_ + pw ) = pixel;
100  }
101  }
102  }
103  }
104 
105  return image;
106 }
107 
108 // =================================================================================================
109 // Properties
110 // =================================================================================================
111 
112 PrinterBitmap& PrinterBitmap::color_map( std::map<char, utils::Color> const& value )
113 {
114  color_map_ = value;
115  return *this;
116 }
117 
118 std::map<char, utils::Color> const& PrinterBitmap::color_map() const
119 {
120  return color_map_;
121 }
122 
124 {
125  if( value == 0 ) {
126  throw std::invalid_argument( "Pixel height has to be 1 or more." );
127  }
128  pixel_height_ = value;
129  return *this;
130 }
131 
133 {
134  return pixel_height_;
135 }
136 
138 {
139  if( value == 0 ) {
140  throw std::invalid_argument( "Pixel width has to be 1 or more." );
141  }
142  pixel_width_ = value;
143  return *this;
144 }
145 
147 {
148  return pixel_width_;
149 }
150 
151 } // namespace sequence
152 } // namespace genesis
Print the sites of a SequenceSet as pixels in a bitmap.
Definition: bitmap.hpp:74
Write Bitmap image files.
size_t pixel_height_per_char() const
Definition: bitmap.cpp:132
Header of Color class.
std::map< char, utils::Color > const & color_map() const
Get the currently set list of Colors for each Sequence character.
Definition: bitmap.cpp:118
Store a set of Sequences.
size_t longest_sequence_length(SequenceSet const &set)
Return the length of the longest Sequence in the SequenceSet.
void to_file(SequenceSet const &set, std::string const &filename) const
Definition: bitmap.cpp:59
void to_stream(SequenceSet const &set, std::ostream &outstream) const
Definition: bitmap.cpp:52
size_t pixel_width_per_char() const
Definition: bitmap.cpp:146