A library for working with phylogenetic and population genetic data.
v0.32.0
bitmap.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2023 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 <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
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 
53  SequenceSet const& set, std::shared_ptr<utils::BaseOutputTarget> target
54 ) const {
55  auto image = make_image_( set );
56  auto writer = utils::BmpWriter();
57  writer.write( image, target );
58 }
59 
60 // =================================================================================================
61 // Internal Helper Functions
62 // =================================================================================================
63 
64 utils::Matrix<utils::Color> PrinterBitmap::make_image_( SequenceSet const& set ) const
65 {
66  // Create a black image of the correct size.
67  size_t const max_line = longest_sequence_length( set );
68  auto image = utils::Matrix<utils::Color>(
69  set.size() * pixel_height_,
70  max_line * pixel_width_,
71  utils::Color( 0.0, 0.0, 0.0 )
72  );
73 
74  // Iterate the sequences in the set.
75  for( size_t r = 0; r < set.size(); ++r ) {
76  auto const& seq = set[r];
77 
78  // Iterate the chars of the sequence for this row.
79  for( size_t c = 0; c < seq.length(); ++c ) {
80 
81  // Find the color for the current char, or use black if no color available.
82  auto pixel = utils::Color( 0.0, 0.0, 0.0 );
83  auto color_it = color_map_.find( seq[c] );
84  if( color_it != color_map_.end() ) {
85  pixel = color_it->second;
86  }
87 
88  // Repeat the line as many times as needed.
89  for( size_t ph = 0; ph < pixel_height_; ++ph ) {
90 
91  // Set as many pixel in width as needed.
92  for( size_t pw = 0; pw < pixel_width_; ++pw ) {
93  image( r * pixel_height_ + ph, c * pixel_width_ + pw ) = pixel;
94  }
95  }
96  }
97  }
98 
99  return image;
100 }
101 
102 // =================================================================================================
103 // Properties
104 // =================================================================================================
105 
106 PrinterBitmap& PrinterBitmap::color_map( std::map<char, utils::Color> const& value )
107 {
108  color_map_ = value;
109  return *this;
110 }
111 
112 std::map<char, utils::Color> const& PrinterBitmap::color_map() const
113 {
114  return color_map_;
115 }
116 
118 {
119  if( value == 0 ) {
120  throw std::invalid_argument( "Pixel height has to be 1 or more." );
121  }
122  pixel_height_ = value;
123  return *this;
124 }
125 
127 {
128  return pixel_height_;
129 }
130 
132 {
133  if( value == 0 ) {
134  throw std::invalid_argument( "Pixel width has to be 1 or more." );
135  }
136  pixel_width_ = value;
137  return *this;
138 }
139 
141 {
142  return pixel_width_;
143 }
144 
145 } // namespace sequence
146 } // namespace genesis
genesis::utils::Color
Definition: color.hpp:47
sequence_set.hpp
genesis::sequence::SequenceSet::size
size_t size() const
Definition: sequence_set.hpp:93
genesis::sequence::PrinterBitmap::pixel_height_per_char
size_t pixel_height_per_char() const
Definition: bitmap.cpp:126
genesis::utils::Matrix
Definition: placement/function/emd.hpp:53
genesis::sequence::PrinterBitmap::write
void write(SequenceSet const &set, std::shared_ptr< utils::BaseOutputTarget > target) const
Definition: bitmap.cpp:52
functions.hpp
matrix.hpp
genesis::sequence::longest_sequence_length
size_t longest_sequence_length(SequenceSet const &set)
Return the length of the longest Sequence in the SequenceSet.
Definition: sequence/functions/functions.cpp:146
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::sequence::SequenceSet
Store a set of Sequences.
Definition: sequence_set.hpp:53
genesis::sequence::PrinterBitmap::pixel_width_per_char
size_t pixel_width_per_char() const
Definition: bitmap.cpp:140
color.hpp
Header of Color class.
genesis::sequence::PrinterBitmap::color_map
std::map< char, utils::Color > const & color_map() const
Get the currently set list of Colors for each Sequence character.
Definition: bitmap.cpp:112
bitmap.hpp
genesis::utils::BmpWriter
Write Bitmap image files.
Definition: utils/formats/bmp/writer.hpp:60
writer.hpp
genesis::sequence::PrinterBitmap
Print the sites of a SequenceSet as pixels in a bitmap.
Definition: bitmap.hpp:75
sequence.hpp
codes.hpp