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.cc

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 /*
00028  *  Classes for creation and output of TGA image files
00029  *
00030  *  Copyright (C) 2003 Max-Planck-Society
00031  *  Author: Martin Reinecke
00032  */
00033 
00034 #include "tga_image.h"
00035 #include <fstream>
00036 #include "font_data.inc"
00037 
00038 using namespace std;
00039 
00040 const Font medium_bold_font = { 0, 128, 7, 13, medium_bold_font_data };
00041 const Font giant_font = { 0, 128, 9, 15, giant_font_data };
00042 
00043 void Palette::setPredefined (int num)
00044   {
00045   fv.clear(); cv.clear();
00046   switch(num)
00047     {
00048     case 0:
00049       add(0,Colour(0,0,0));
00050       add(1,Colour(1,1,1));
00051       break;
00052     case 1:
00053       add(0,Colour(0,0,0));
00054       add(0.4,Colour(0,0,0.5));
00055       add(0.75,Colour(0,0.6,1));
00056       add(1,Colour(1,1,1));
00057       break;
00058     case 4:
00059       add(0,Colour(0,0,.5));
00060       add(0.15,Colour(0,0,1));
00061       add(0.4,Colour(0,1,1));
00062       add(0.7,Colour(1,1,0));
00063       add(0.9,Colour(1,.33,0));
00064       add(1,Colour(.5,0,0));
00065       break;
00066     default:
00067       throw Message_error("Palette #"+dataToString(num)+" not yet supported.");
00068     }
00069   }
00070 
00071 void TGA_Image::write_char (int xpos, int ypos, const Colour &col, char c,
00072   int scale)
00073   {
00074   for (int i=0; i<font.xpix; ++i)
00075     for (int j=0; j<font.ypix; ++j)
00076       {
00077       int ofs = (c-font.offset)*font.xpix*font.ypix + j*font.xpix + i;
00078       if (font.data[ofs]>0)
00079         for (int m=0; m<scale; ++m)
00080           for (int n=0; n<scale; ++n)
00081             put_pixel(xpos+scale*i+m,ypos+scale*j+n,col);
00082       }
00083   }
00084 
00085 TGA_Image::TGA_Image ()
00086   : font(medium_bold_font) {}
00087 
00088 TGA_Image::TGA_Image (int xres, int yres)
00089   : font(medium_bold_font), pixel(xres,yres)
00090   {
00091   pixel.fill(Colour(0,0,0));
00092   }
00093 
00094 void TGA_Image::annotate (int xpos, int ypos, const Colour &col,
00095   const string &text, int scale)
00096   {
00097   for (unsigned int m=0; m<text.length(); ++m)
00098     write_char(xpos+m*scale*font.xpix, ypos, col, text[m],scale);
00099   }
00100 
00101 void TGA_Image::annotate_centered (int xpos, int ypos, const Colour &col,
00102   const string &text, int scale)
00103   {
00104   xpos-=(scale*text.length()*font.xpix)/2;
00105   ypos-=scale*font.ypix/2;
00106   annotate (xpos,ypos,col,text,scale);
00107   }
00108 
00109 void TGA_Image::set_font (const Font &fnt)
00110   {
00111   font = fnt;
00112   }
00113 
00114 void TGA_Image::write (const string &file) const
00115   {
00116   int xres = pixel.size1();
00117   int yres = pixel.size2();
00118 
00119   const char header[18] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00120     xres%256, xres/256, yres%256, yres/256, 24, 32 };
00121 
00122   ofstream out(file.c_str(), ios_base::out | ios_base::binary);
00123   planck_assert(out, "could not create file " + file);
00124 
00125   out.write (header, 18);
00126 
00127   for (int j=0; j<yres; ++j)
00128     for (int i=0; i<xres; ++i)
00129       {
00130       out.write(&(pixel[i][j].b),1);
00131       out.write(&(pixel[i][j].g),1);
00132       out.write(&(pixel[i][j].r),1);
00133       }
00134   }

Generated on Fri Jun 18 16:12:29 2010 for LevelS C++ support library
Privacy / Copyright
FIRST GOV Contact: NASA Home Page Site Manager:
Webmaster:

CL 03-2650