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 #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
00043
00044
00045
00046 class Colour
00047 {
00048 public:
00049 float r,
00050 g,
00051 b;
00052
00053
00054 Colour() {}
00055
00056 Colour (float R, float G, float B) : r(R), g(G), b(B) {}
00057
00058 const Colour &operator*= (float fact)
00059 { r*=fact; g*=fact; b*=fact; return *this; }
00060
00061 const Colour operator+ (const Colour &c2) const
00062 { return Colour(r+c2.r, g+c2.g, b+c2.b); }
00063
00064 const Colour operator* (double f) const
00065 { return Colour(r*f, g*f, b*f); }
00066 };
00067
00068
00069 class Palette
00070 {
00071 private:
00072 std::vector<Colour> cv;
00073 std::vector<float> fv;
00074
00075 public:
00076
00077
00078 void add (float f, const Colour &c)
00079 {
00080 fv.push_back(f);
00081 cv.push_back(c);
00082 }
00083
00084 void setPredefined(int num);
00085
00086
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
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
00149 TGA_Image (int xres, int yres);
00150
00151 ~TGA_Image () {}
00152
00153
00154 void fill (const Colour &col) { pixel.fill(col); }
00155
00156 void set_font (const Font &fnt);
00157
00158
00159
00160 void annotate (int xpos, int ypos, const Colour &col,
00161 const std::string &text, int scale=1);
00162
00163
00164 void annotate_centered (int xpos, int ypos, const Colour &col,
00165 const std::string &text, int scale=1);
00166
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
00173
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
00183 void write (const std::string &file) const;
00184 };
00185
00186
00187
00188 #endif