NASA - Jet Propulsion Laboratory
    + View the NASA Portal
Search JPL
Jet Propulsion Laboratory Home Earth Solar System Stars & Galaxies Technology
Introduction Background Software Links

tga_image.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx 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  *  Healpix_cxx 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 Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*! \file tga_image.h
00028  *  Classes for creation and output of TGA image files
00029  *
00030  *  Copyright (C) 2003, 2006 Max-Planck-Society
00031  *  \author Martin Reinecke, David Larson
00032  */
00033 
00034 #ifndef PLANCK_TGA_IMAGE_H
00035 #define PLANCK_TGA_IMAGE_H
00036 
00037 #include <string>
00038 #include <vector>
00039 #include <algorithm>
00040 #include "arr.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(r*f, g*f, 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 = max(0,min(255,int(col.r*256)));
00105       g = max(0,min(255,int(col.g*256)));
00106       b = max(0,min(255,int(col.b*256)));
00107       }
00108 
00109   public:
00110     char r,g,b;
00111 
00112     Colour8() {}
00113     Colour8 (unsigned char R, unsigned char G, unsigned char 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 Font
00126   {
00127   public:
00128     int offset, num_chars, xpix, ypix;
00129     const char *data;
00130   };
00131 
00132 extern const Font medium_bold_font;
00133 extern const Font giant_font;
00134 
00135 /*! Class for creating and storing TGA image files. */
00136 class TGA_Image
00137   {
00138   private:
00139     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     TGA_Image ();
00148     /*! Creates an image object with a resolution of \a xres by \a yres. */
00149     TGA_Image (int xres, int yres);
00150     /*! */
00151     ~TGA_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 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 (int i, int j, const Colour &col)
00168       {
00169       if ((i>=0) && (i<pixel.size1()) && (j>=0) && (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 (int i, int j)
00175       {
00176       if ((i>=0) && (i<pixel.size1()) && (j>=0) && (j<pixel.size2()))
00177         return pixel[i][j];
00178       else
00179         return Colour8(0, 0, 0);
00180       }
00181 
00182     /*! Writes the image to \a file. */
00183     void write (const std::string &file) const;
00184   };
00185 
00186 /*! \} */
00187 
00188 #endif

Generated on Tue Jul 15 16:30:51 2008 for LevelS C++ support library
Privacy / Copyright
FIRST GOV Contact: NASA Home Page Site Manager:
Webmaster:

CL 03-2650