00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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 }