LevelS C++ support library  3.82
ls_image.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file ls_image.h
26  * Classes for creation and output of image files
27  *
28  * Copyright (C) 2003-2015 Max-Planck-Society
29  * \author Martin Reinecke, David Larson
30  */
31 
32 #ifndef PLANCK_LS_IMAGE_H
33 #define PLANCK_LS_IMAGE_H
34 
35 #include <string>
36 #include <vector>
37 #include <iostream>
38 #include <algorithm>
39 #include "arr.h"
40 #include "datatypes.h"
41 #include "colour.h"
42 #include "linear_map.h"
43 
44 /*! \defgroup imagegroup Image creation */
45 /*! \{ */
46 
47 /*! A class for mapping floting-point values into colours. */
48 class Palette: public linearMap<Colour>
49  {
50  public:
51  /*! Adds a new data point \a f, with the corresponding colour \a c.
52  The additions must be done in the order of ascending \a f. */
53  void add (float f, const Colour &c)
54  { addVal (f, c); }
55  void addb (uint8 f, uint8 r,uint8 g, uint8 b)
56  { addVal (f/255., Colour(r/255.,g/255.,b/255.)); }
57  /*! Sets the palette to the predefined palette \a num. */
58  void setPredefined(int num);
59  /*! Returns the colour corresponding to the value \a f. The colour is
60  determined by linear interpolation between neighbouring data points. */
61  Colour Get_Colour (float f) const
62  { return getVal_const(f); }
63  };
64 
65 class Colour8
66  {
67  private:
68  void import (const Colour &col)
69  {
70  using namespace std;
71  r = uint8(max(0,min(255,int(col.r*256))));
72  g = uint8(max(0,min(255,int(col.g*256))));
73  b = uint8(max(0,min(255,int(col.b*256))));
74  }
75 
76  public:
77  uint8 r,g,b;
78 
79  Colour8() {}
80  Colour8 (uint8 R, uint8 G, uint8 B)
81  : r(R), g(G), b(B) {}
82  Colour8 (const Colour &col)
83  { import (col); }
84  const Colour8 &operator= (const Colour &col)
85  { import (col); return *this; }
86  bool operator== (const Colour8 &that)
87  { return (r == that.r) && (g == that.g) && (b == that.b); }
88  bool operator!= (const Colour8 &that)
89  { return (r != that.r) || (g != that.g) || (b != that.b); }
90  };
91 
92 class MP_Font
93  {
94  public:
95  int offset, num_chars, xpix, ypix;
96  const char *data;
97  };
98 
99 extern const MP_Font medium_bold_font;
100 extern const MP_Font giant_font;
101 
102 /*! Class for creating and storing image files. */
103 class LS_Image
104  {
105  private:
106  MP_Font font;
107  arr2<Colour8> pixel;
108 
109  void write_char (int xpos, int ypos, const Colour &col, char c,
110  int scale=1);
111 
112  public:
113  /*! */
114  LS_Image ();
115  /*! Creates an image object with a resolution of \a xres by \a yres. */
116  LS_Image (int xres, int yres);
117  /*! */
118  ~LS_Image () {}
119 
120  /*! Fills the entire image with colour \a col. */
121  void fill (const Colour &col) { pixel.fill(col); }
122  /*! Sets the font used for annotations to \a fnt. */
123  void set_font (const MP_Font &fnt);
124  /*! Outputs the string \a text in colour \a col.
125  \a xpos, \a ypos is the lower left corner;
126  the font is scaled by \a scale. */
127  void annotate (int xpos, int ypos, const Colour &col,
128  const std::string &text, int scale=1);
129  /*! Outputs the string \a text centered at position \a xpos, \a ypos
130  in colour \a col. The font is scaled by \a scale. */
131  void annotate_centered (int xpos, int ypos, const Colour &col,
132  const std::string &text, int scale=1);
133  /*! Sets the pixel \a i, \a j, to the colour \a col. */
134  void put_pixel (tsize i, tsize j, const Colour &col)
135  {
136  if ((i<pixel.size1()) && (j<pixel.size2()))
137  pixel[i][j] = col;
138  }
139  /*! Returns the colour of the pixel \a i, \a j, or black if the pixel
140  lies outside of the image. */
141  Colour8 get_pixel (tsize i, tsize j)
142  {
143  return ((i<pixel.size1()) && (j<pixel.size2())) ?
144  pixel[i][j] : Colour8(0, 0, 0);
145  }
146 
147  /*! Writes the image to \a file in uncompressed TGA format. */
148  void write_TGA (const std::string &file) const;
149 
150  /*! Writes the image to \a file in run-length encoded TGA format. */
151  void write_TGA_rle (const std::string &file) const;
152 
153  /*! Writes the image to \a file in binary PPM format. */
154  void write_PPM (const std::string &file) const;
155  };
156 
157 /*! \} */
158 
159 #endif
Colour8 get_pixel(tsize i, tsize j)
Definition: ls_image.h:141
void put_pixel(tsize i, tsize j, const Colour &col)
Definition: ls_image.h:134
void add(float f, const Colour &c)
Definition: ls_image.h:53
void fill(const Colour &col)
Definition: ls_image.h:121
tsize size1() const
Definition: arr.h:375
std::size_t tsize
Definition: datatypes.h:116
Colour Get_Colour(float f) const
Definition: ls_image.h:61
void setPredefined(int num)
Definition: ls_image.cc:45
tsize size2() const
Definition: arr.h:377
void fill(const T &val)
Definition: arr.h:412

Generated on Thu Jul 28 2022 17:32:06 for LevelS C++ support library