00001 #ifndef  __JBXL_CPP_TGA_TOOl_H_
00002 #define  __JBXL_CPP_TGA_TOOl_H_
00003 
00013 #include "Gdata.h"
00014 #include "xtools.h"
00015 
00016 
00017 #define  TGA_HEADER_SIZE  18
00018 
00019 
00020 namespace jbxl {
00021 
00022 
00024 
00025 class TGAImage 
00026 {
00027 public:
00028     int     xs;
00029     int     ys;
00030     int     col;
00031     int     state;
00032 
00033     uByte   hd[TGA_HEADER_SIZE];
00034     uByte*  gp;                 
00035 
00036 public:
00037     TGAImage(void)  { init();}
00038     virtual ~TGAImage(void) {}
00039 
00040     void    init(void);                                     
00041     bool    isNull(void);                                   
00042     void    clear(void);                                    
00043     void    fill(uByte v=(uByte)0);                         
00044     void    free(void);                                     
00045 
00046     uByte&  point(int x, int y, int c) { return gp[col*(y*xs + x) + c];}
00047     void    getm(int x, int y, int c);
00048     void    set (int x, int y, int c);
00049 };
00050 
00051 
00052 
00054 
00055 TGAImage    readTGAFile (const char* fname);
00056 TGAImage    readTGAData (FILE* fp);
00057 int         writeTGAFile(const char* fname, TGAImage tga);
00058 int         writeTGAData(FILE* fp, TGAImage tga);
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00076 template <typename T>  MSGraph<T> TGAImage2MSGraph(TGAImage tga)
00077 {
00078     MSGraph<T> vp;
00079 
00080     if (tga.isNull()) {
00081         vp.state = JBXL_GRAPH_NODATA_ERROR;
00082         return vp;
00083     }
00084 
00085     vp.set(tga.xs, tga.ys, tga.col);
00086     if (vp.isNull()) return vp;
00087     
00088     if      (tga.col==4) vp.color = GRAPH_COLOR_BGRA;
00089     else if (tga.col==3) vp.color = GRAPH_COLOR_BGR;
00090     else if (tga.col==2) vp.color = GRAPH_COLOR_MA;
00091     else if (tga.col==1) vp.color = GRAPH_COLOR_MONO;
00092     else {
00093         vp.state = JBXL_GRAPH_IVDARG_ERROR;
00094         return vp;
00095     }
00096 
00097     for (int k=0; k<tga.col; k++) {
00098         int zp = k*tga.xs*tga.ys;
00099         for (int j=0; j<tga.ys; j++) {
00100             int yp = zp + j*tga.xs;
00101             for (int i=0; i<tga.xs; i++) {
00102                 vp.gp[yp + i] = (T)tga.point(i, j, k);
00103             }
00104         }
00105     }
00106 
00107     return vp;
00108 }
00109 
00110 
00121 template <typename T>  TGAImage  MSGraph2TGAImage(MSGraph<T> vp)
00122 {
00123     TGAImage tga;
00124 
00125     if (vp.isNull()) {
00126         tga.state = JBXL_GRAPH_NODATA_ERROR;
00127         return tga;
00128     }
00129 
00130     tga.set(vp.xs, vp.ys, vp.zs);
00131     if (tga.isNull()) return tga;
00132 
00133     if (vp.color==GRAPH_COLOR_UNKNOWN) {
00134         if      (vp.zs==1) vp.color = GRAPH_COLOR_MONO;
00135         else if (vp.zs==3) vp.color = GRAPH_COLOR_RGB;
00136         else if (vp.zs==4) vp.color = GRAPH_COLOR_RGBA;
00137     }
00138 
00139     
00140     if (vp.color==GRAPH_COLOR_BGRA || vp.color==GRAPH_COLOR_BGR || vp.color==GRAPH_COLOR_MONO || vp.color==GRAPH_COLOR_MA) { 
00141         for (int k=0; k<tga.col; k++) {
00142             int zp = k*tga.xs*tga.ys;
00143             for (int j=0; j<tga.ys; j++) {
00144                 int yp = zp + j*tga.xs;
00145                 for (int i=0; i<tga.xs; i++) {
00146                     tga.point(i, j, k) = (uByte)vp.gp[yp + i];
00147                 }
00148             }
00149         }
00150     }
00151     
00152     else if (vp.color==GRAPH_COLOR_RGB || vp.color==GRAPH_COLOR_RGBA) { 
00153         for (int k=0; k<3; k++) {
00154             int zp = (2-k)*tga.xs*tga.ys;
00155             for (int j=0; j<tga.ys; j++) {
00156                 int yp = zp + j*tga.xs;
00157                 for (int i=0; i<tga.xs; i++) {
00158                     tga.point(i, j, k) = (uByte)vp.gp[yp + i];
00159                 }
00160             }
00161         }
00162         if (vp.color==GRAPH_COLOR_RGBA) {   
00163             int zp = 3*tga.xs*tga.ys;
00164             for (int j=0; j<tga.ys; j++) {
00165                 int yp = zp + j*tga.xs;
00166                 for (int i=0; i<tga.xs; i++) {
00167                     tga.point(i, j, 3) = (uByte)vp.gp[yp + i];
00168                 }
00169             }
00170         }
00171     }
00172     
00173     else if (vp.color==GRAPH_COLOR_ABGR) { 
00174         for (int j=0; j<tga.ys; j++) {      
00175             int yp = j*tga.xs;
00176             for (int i=0; i<tga.xs; i++) {
00177                 tga.point(i, j, 3) = (uByte)vp.gp[yp + i];
00178             }
00179         }
00180         for (int k=1; k<4; k++) {
00181             int zp = k*tga.xs*tga.ys;
00182             for (int j=0; j<tga.ys; j++) {
00183                 int yp = zp + j*tga.xs;
00184                 for (int i=0; i<tga.xs; i++) {
00185                     tga.point(i, j, k-1) = (uByte)vp.gp[yp + i];
00186                 }
00187             }
00188         }
00189     }
00190     
00191     else if (vp.color==GRAPH_COLOR_ARGB) { 
00192         for (int j=0; j<tga.ys; j++) {      
00193             int yp = j*tga.xs;
00194             for (int i=0; i<tga.xs; i++) {
00195                 tga.point(i, j, 3) = (uByte)vp.gp[yp + i];
00196             }
00197         }
00198         for (int k=1; k<4; k++) {
00199             int zp = (4-k)*tga.xs*tga.ys;
00200             for (int j=0; j<tga.ys; j++) {
00201                 int yp = zp + j*tga.xs;
00202                 for (int i=0; i<tga.xs; i++) {
00203                     tga.point(i, j, k-1) = (uByte)vp.gp[yp + i];
00204                 }
00205             }
00206         }
00207     }
00208     else {
00209         tga.state = JBXL_GRAPH_IVDARG_ERROR;
00210         tga.free();
00211     }
00212 
00213     return tga;
00214 }
00215 
00216 
00217 }       
00218 
00219 
00220 
00221 #endif
00222