ls_image.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file ls_image.h
00026  *  Classes for creation and output of image files
00027  *
00028  *  Copyright (C) 2003-2015 Max-Planck-Society
00029  *  \author Martin Reinecke, David Larson
00030  */
00031 
00032 #ifndef PLANCK_LS_IMAGE_H
00033 #define PLANCK_LS_IMAGE_H
00034 
00035 #include <string>
00036 #include <vector>
00037 #include <iostream>
00038 #include <algorithm>
00039 #include "arr.h"
00040 #include "datatypes.h"
00041 #include "colour.h"
00042 #include "linear_map.h"
00043 
00044 /*! \defgroup imagegroup Image creation */
00045 /*! \{ */
00046 
00047 /*! A class for mapping floting-point values into colours. */
00048 class Palette: public linearMap<Colour>
00049   {
00050   public:
00051     /*! Adds a new data point \a f, with the corresponding colour \a c.
00052         The additions must be done in the order of ascending \a f. */
00053     void add (float f, const Colour &c)
00054       { addVal (f, c); }
00055     void addb (uint8 f, uint8 r,uint8 g, uint8 b)
00056       { addVal (f, Colour(r/255.,g/255.,b/255.)); }
00057     /*! Sets the palette to the predefined palette \a num. */
00058     void setPredefined(int num);
00059     /*! Returns the colour corresponding to the value \a f. The colour is
00060         determined by linear interpolation between neighbouring data points. */
00061     Colour Get_Colour (float f) const
00062       { return getVal_const(f); }
00063   };
00064 
00065 class Colour8
00066   {
00067   private:
00068     void import (const Colour &col)
00069       {
00070       using namespace std;
00071       r = uint8(max(0,min(255,int(col.r*256))));
00072       g = uint8(max(0,min(255,int(col.g*256))));
00073       b = uint8(max(0,min(255,int(col.b*256))));
00074       }
00075 
00076   public:
00077     uint8 r,g,b;
00078 
00079     Colour8() {}
00080     Colour8 (uint8 R, uint8 G, uint8 B)
00081       : r(R), g(G), b(B) {}
00082     Colour8 (const Colour &col)
00083       { import (col); }
00084     const Colour8 &operator= (const Colour &col)
00085       { import (col); return *this; }
00086     bool operator== (const Colour8 &that)
00087       { return (r == that.r) && (g == that.g) && (b == that.b); }
00088     bool operator!= (const Colour8 &that)
00089       { return (r != that.r) || (g != that.g) || (b != that.b); }
00090   };
00091 
00092 class MP_Font
00093   {
00094   public:
00095     int offset, num_chars, xpix, ypix;
00096     const char *data;
00097   };
00098 
00099 extern const MP_Font medium_bold_font;
00100 extern const MP_Font giant_font;
00101 
00102 /*! Class for creating and storing image files. */
00103 class LS_Image
00104   {
00105   private:
00106     MP_Font font;
00107     arr2<Colour8> pixel;
00108 
00109     void write_char (int xpos, int ypos, const Colour &col, char c,
00110                      int scale=1);
00111 
00112   public:
00113     /*! */
00114     LS_Image ();
00115     /*! Creates an image object with a resolution of \a xres by \a yres. */
00116     LS_Image (int xres, int yres);
00117     /*! */
00118     ~LS_Image () {}
00119 
00120     /*! Fills the entire image with colour \a col. */
00121     void fill (const Colour &col) { pixel.fill(col); }
00122     /*! Sets the font used for annotations to \a fnt. */
00123     void set_font (const MP_Font &fnt);
00124     /*! Outputs the string \a text in colour \a col.
00125         \a xpos, \a ypos is the lower left corner;
00126         the font is scaled by \a scale. */
00127     void annotate (int xpos, int ypos, const Colour &col,
00128       const std::string &text, int scale=1);
00129     /*! Outputs the string \a text centered at position \a xpos, \a ypos
00130         in colour \a col. The font is scaled by \a scale. */
00131     void annotate_centered (int xpos, int ypos, const Colour &col,
00132       const std::string &text, int scale=1);
00133     /*! Sets the pixel \a i, \a j, to the colour \a col. */
00134     void put_pixel (tsize i, tsize j, const Colour &col)
00135       {
00136       if ((i<pixel.size1()) && (j<pixel.size2()))
00137         pixel[i][j] = col;
00138       }
00139     /*! Returns the colour of the pixel \a i, \a j, or black if the pixel
00140         lies outside of the image. */
00141     Colour8 get_pixel (tsize i, tsize j)
00142       {
00143       return ((i<pixel.size1()) && (j<pixel.size2())) ?
00144         pixel[i][j] : Colour8(0, 0, 0);
00145       }
00146 
00147     /*! Writes the image to \a file in uncompressed TGA format. */
00148     void write_TGA (const std::string &file) const;
00149 
00150     /*! Writes the image to \a file in run-length encoded TGA format. */
00151     void write_TGA_rle (const std::string &file) const;
00152 
00153     /*! Writes the image to \a file in binary PPM format. */
00154     void write_PPM (const std::string &file) const;
00155   };
00156 
00157 /*! \} */
00158 
00159 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library