TGA.hpp

00001 #ifndef __TGA_HPP
00002 #define __TGA_HPP
00003 
00004         struct  TGA
00005         {
00006                 unsigned char   commentlen,maptype,filetype;
00007                 unsigned short  maporg,maplen;
00008                 unsigned char   mapsize;
00009                 unsigned short  Xorigin,Yorigin,MaxX,MaxY;
00010                 unsigned char   DataBits,flags;
00011                 
00012                 enum    {       nodata =  0,
00013                                 rawmap =  1,rawrgb =  2, rawmon =  3,
00014                                 rlemap =  9,rlergb = 10, rlemon = 11
00015                         };
00016                 
00017                 enum
00018                 {
00019                         invertedflag = 0x20,
00020                         hasAlphaChannel = 0x08
00021                 };
00022 
00024                 void defRGB(unsigned short X,unsigned short Y); 
00025                 
00027                 void defRGBA(unsigned short X,unsigned short Y); 
00028                 
00030                 void def256(unsigned short X,unsigned short Y);
00031 
00032                 bool isRGBA() const { return DataBits==32; }
00033                 bool isRGB()  const { return DataBits==24; }
00034                 bool is256()  const { return DataBits==8;  }
00035                 
00036                 unsigned long dataoffset() const
00037                 {
00038                         if (DataBits==8) return 18 + maplen*3;
00039                         else             return 18;
00040                 }
00041                 
00042                 bool top_first() const  { return (flags&invertedflag) != 0; }
00043                 
00045                 void   read(const unsigned char hdr[18]);
00046                 
00048                 void   write(unsigned char hdr[18]) const;
00049         };
00050 
00051 inline void TGA::defRGB(unsigned short X,unsigned short Y)
00052 {
00053         Xorigin=0;      Yorigin=0;
00054         MaxX=X;         MaxY=Y;
00055 
00056         commentlen=0;
00057         maptype=nodata;
00058         filetype=rawrgb;
00059         maporg=0;       maplen=0;       mapsize=0;
00060         DataBits=24;
00061         flags=0;
00062 }
00063 
00064 inline void TGA::defRGBA(unsigned short X,unsigned short Y)
00065 {
00066         defRGB(X,Y);
00067         DataBits=32;
00068         flags |= hasAlphaChannel;
00069 }
00070 
00071 inline void TGA::def256(unsigned short X,unsigned short Y)
00072 {
00073         Xorigin=0;      Yorigin=0;
00074         MaxX=X;         MaxY=Y;
00075 
00076         commentlen=0;
00077         maptype=rawmap;
00078         filetype=rawmap;
00079         maporg=0;
00080         maplen=256;     // Anzahl der Eintraege ?
00081         mapsize=24;     // Bit pro Eintrag ?
00082         DataBits=8;
00083         flags=0;
00084 }
00085 
00086 inline void     TGA::read(const unsigned char hdr[18])
00087 {
00088         commentlen = hdr[0];
00089         maptype    = hdr[1];
00090         filetype   = hdr[2];
00091         maporg     = int(hdr[3]) + (int(hdr[4])<<8);
00092         maplen     = int(hdr[5]) + (int(hdr[6])<<8);
00093         mapsize    = hdr[7];
00094         Xorigin    = int(hdr[ 8]) + (int(hdr[ 9])<<8);  
00095         Yorigin    = int(hdr[10]) + (int(hdr[11])<<8);
00096         MaxX       = int(hdr[12]) + (int(hdr[13])<<8);  
00097         MaxY       = int(hdr[14]) + (int(hdr[15])<<8);  
00098         DataBits   = hdr[16];
00099         flags      = hdr[17];
00100 }
00101         
00102 inline void     TGA::write(unsigned char hdr[18]) const
00103 {
00104         hdr[0] = commentlen;
00105         hdr[1] = maptype;
00106         hdr[2] = filetype;
00107         hdr[3] = maporg;        hdr[4] = maporg>>8;
00108         hdr[5] = maplen;        hdr[6] = maplen>>8;
00109         hdr[7] = mapsize;
00110         hdr[8] = Xorigin;       hdr[9]  = Xorigin>>8;
00111         hdr[10] = Yorigin;      hdr[11] = Yorigin>>8;
00112         hdr[12] = MaxX;         hdr[13] = MaxX   >>8;
00113         hdr[14] = MaxY;         hdr[15] = MaxY   >>8;
00114         hdr[16] = DataBits;
00115         hdr[17] = flags;                
00116 }
00117 
00118 
00119 #endif // __TGA_HPP