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-2012 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 
00042 /*! \defgroup imagegroup Image creation */
00043 /*! \{ */
00044 
00045 /*! A very simple class for storing RGB colours. */
00046 class Colour
00047   {
00048   public:
00049     float r, /*!< the red component */
00050           g, /*!< the green component */
00051           b; /*!< the blue component */
00052 
00053     /*! Default constructor. Does not initialize \a r, \a g and \a b. */
00054     Colour() {}
00055     /*! Initializes the colour with \a R, \a G and \a B. */
00056     Colour (float R, float G, float B) : r(R), g(G), b(B) {}
00057     /*! Multiplies all components with \a fact. */
00058     const Colour &operator*= (float fact)
00059       { r*=fact; g*=fact; b*=fact; return *this; }
00060     /*! Returns the sum colour of \a *this and \a c2. */
00061     const Colour operator+ (const Colour &c2) const
00062       { return Colour(r+c2.r, g+c2.g, b+c2.b); }
00063     /*! Returns \a *this, scaled by \a f. */
00064     const Colour operator* (double f) const
00065       { return Colour(float(r*f), float(g*f), float(b*f)); }
00066   };
00067 
00068 /*! A class for mapping floting-point values into colours. */
00069 class Palette
00070   {
00071   private:
00072     std::vector<Colour> cv;
00073     std::vector<float> fv;
00074 
00075   public:
00076     /*! Adds a new data point \a f, with the corresponding colour \a c.
00077         The additions must be done in the order of ascending \a f. */
00078     void add (float f, const Colour &c)
00079       {
00080       fv.push_back(f);
00081       cv.push_back(c);
00082       }
00083     /*! Sets the palette to the predefined palette \a num. */
00084     void setPredefined(int num);
00085     /*! Returns the colour corresponding to the value \a f. The colour is
00086         determined by linear interpolation between neighbouring data points. */
00087     Colour Get_Colour (float f) const
00088       {
00089       if (f<=fv[0]) return cv[0];
00090       if (f>=fv[fv.size()-1]) return cv[cv.size()-1];
00091       int i=0;
00092       while (f>fv[i]) ++i;
00093       return cv[i-1]*((fv[i]-f)/(fv[i]-fv[i-1]))
00094            + cv[i]*((f-fv[i-1])/(fv[i]-fv[i-1]));
00095       }
00096   };
00097 
00098 class Colour8
00099   {
00100   private:
00101     void import (const Colour &col)
00102       {
00103       using namespace std;
00104       r = uint8(max(0,min(255,int(col.r*256))));
00105       g = uint8(max(0,min(255,int(col.g*256))));
00106       b = uint8(max(0,min(255,int(col.b*256))));
00107       }
00108 
00109   public:
00110     uint8 r,g,b;
00111 
00112     Colour8() {}
00113     Colour8 (uint8 R, uint8 G, uint8 B)
00114       : r(R), g(G), b(B) {}
00115     Colour8 (const Colour &col)
00116       { import (col); }
00117     const Colour8 &operator= (const Colour &col)
00118       { import (col); return *this; }
00119     bool operator== (const Colour8 &that)
00120       { return (r == that.r) && (g == that.g) && (b == that.b); }
00121     bool operator!= (const Colour8 &that)
00122       { return (r != that.r) || (g != that.g) || (b != that.b); }
00123   };
00124 
00125 class MP_Font
00126   {
00127   public:
00128     int offset, num_chars, xpix, ypix;
00129     const char *data;
00130   };
00131 
00132 extern const MP_Font medium_bold_font;
00133 extern const MP_Font giant_font;
00134 
00135 /*! Class for creating and storing image files. */
00136 class LS_Image
00137   {
00138   private:
00139     MP_Font font;
00140     arr2<Colour8> pixel;
00141 
00142     void write_char (int xpos, int ypos, const Colour &col, char c,
00143                      int scale=1);
00144 
00145   public:
00146     /*! */
00147     LS_Image ();
00148     /*! Creates an image object with a resolution of \a xres by \a yres. */
00149     LS_Image (int xres, int yres);
00150     /*! */
00151     ~LS_Image () {}
00152 
00153     /*! Fills the entire image with colour \a col. */
00154     void fill (const Colour &col) { pixel.fill(col); }
00155     /*! Sets the font used for annotations to \a fnt. */
00156     void set_font (const MP_Font &fnt);
00157     /*! Outputs the string \a text in colour \a col.
00158         \a xpos, \a ypos is the lower left corner;
00159         the font is scaled by \a scale. */
00160     void annotate (int xpos, int ypos, const Colour &col,
00161       const std::string &text, int scale=1);
00162     /*! Outputs the string \a text centered at position \a xpos, \a ypos
00163         in colour \a col. The font is scaled by \a scale. */
00164     void annotate_centered (int xpos, int ypos, const Colour &col,
00165       const std::string &text, int scale=1);
00166     /*! Sets the pixel \a i, \a j, to the colour \a col. */
00167     void put_pixel (tsize i, tsize j, const Colour &col)
00168       {
00169       if ((i<pixel.size1()) && (j<pixel.size2()))
00170         pixel[i][j] = col;
00171       }
00172     /*! Returns the colour of the pixel \a i, \a j, or black if the pixel
00173         lies outside of the image. */
00174     Colour8 get_pixel (tsize i, tsize j)
00175       {
00176       return ((i<pixel.size1()) && (j<pixel.size2())) ?
00177         pixel[i][j] : Colour8(0, 0, 0);
00178       }
00179 
00180     /*! Writes the image to \a file in uncompressed TGA format. */
00181     void write_TGA (const std::string &file) const;
00182 
00183     /*! Writes the image to \a file in run-length encoded TGA format. */
00184     void write_TGA_rle (const std::string &file) const;
00185 
00186     /*! Writes the image to \a file in binary PPM format. */
00187     void write_PPM (const std::string &file) const;
00188   };
00189 
00190 /*! \} */
00191 
00192 #endif

Generated on Wed Apr 24 11:31:17 2013 for LevelS C++ support library