00001 
00011 #include "TgaTool.h"
00012 
00013 
00014 using namespace jbxl;
00015 
00016 
00022 void  TGAImage::init(void) 
00023 { 
00024     xs      = 0;
00025     ys      = 0;
00026     col     = 0; 
00027     state   = 0;
00028     gp      = NULL;
00029 
00030     memset(hd, 0, TGA_HEADER_SIZE);
00031     return;
00032 }
00033 
00034 
00040 bool  TGAImage::isNull(void) 
00041 {   
00042     if (gp==NULL) return true; 
00043     return false;
00044 }
00045 
00046 
00050 void  TGAImage::clear(void) 
00051 {
00052     memset(gp, 0, xs*ys*col);
00053     return;
00054 }
00055 
00056 
00060 void  TGAImage::fill(uByte v) 
00061 {
00062     int i;
00063     for (i=0; i<xs*ys*col; i++) gp[i] = v;
00064 
00065     return;
00066 }
00067 
00068 
00074 void  TGAImage::free(void) 
00075 {  
00076     if (gp!=NULL) ::free(gp); 
00077     init();
00078 
00079     return;
00080 }
00081 
00082 
00086 void   TGAImage::set(int x, int y, int c) 
00087 { 
00088     getm(x, y, c); 
00089     if (gp==NULL) return;
00090 
00091     xs  = x;
00092     ys  = y;
00093     col = c;
00094     state = 0;
00095 
00096     memset(gp, 0, xs*ys*col);
00097 
00098     return;
00099 }
00100 
00101 
00105 void   TGAImage::getm(int x, int y, int c)
00106 {
00107     gp = (uByte*)malloc(x*y*c);
00108     if (gp==NULL) {
00109         state = JBXL_GRAPH_MEMORY_ERROR;
00110         return;
00111     }
00112     
00113     return;
00114 }
00115 
00116 
00117 
00119 
00132 TGAImage  jbxl::readTGAFile(const char* fname)
00133 {
00134     TGAImage tga;
00135     FILE*  fp;
00136 
00137     fp = fopen(fname, "rb");
00138     if (fp==NULL) {
00139         tga.gp   = NULL;
00140         tga.state = JBXL_GRAPH_OPFILE_ERROR;
00141         return tga;
00142     }
00143 
00144     tga = readTGAData(fp);
00145     fclose(fp);
00146 
00147     return tga;
00148 }
00149 
00150 
00163 TGAImage  jbxl::readTGAData(FILE* fp)
00164 {
00165     TGAImage tga;
00166 
00167     fseek(fp, 0, 0);
00168     tga.free();
00169 
00170     
00171     PRINT_MESG("**********************************************\n");
00172     PRINT_MESG("ERROR: jbxl::readTGAData() is not implemeted!!\n");
00173     PRINT_MESG("**********************************************\n");
00174     tga.state = JBXL_GRAPH_IVDARG_ERROR;
00175     
00176 
00177     return tga;
00178 }
00179 
00180 
00196 int  jbxl::writeTGAFile(const char* fname, TGAImage tga)
00197 {
00198     FILE*  fp;
00199     int    ret;
00200 
00201     if (fname==NULL) return JBXL_GRAPH_IVDARG_ERROR;
00202     if (tga.col<=0 || tga.col>4) return JBXL_GRAPH_IVDARG_ERROR;
00203     if (tga.gp==NULL) return JBXL_GRAPH_NODATA_ERROR;
00204 
00205     fp = fopen(fname, "wb");
00206     if (fp==NULL) {
00207         return JBXL_GRAPH_OPFILE_ERROR;
00208     }
00209 
00210     ret = writeTGAData(fp, tga);
00211     fclose(fp); 
00212 
00213     return ret;
00214 }
00215 
00216 
00232 int  jbxl::writeTGAData(FILE* fp, TGAImage tga)
00233 {
00234     if (fp==NULL) return JBXL_GRAPH_OPFILE_ERROR;
00235     if (tga.col<=0 || tga.col>4) return JBXL_GRAPH_IVDARG_ERROR;
00236     if (tga.gp==NULL) return JBXL_GRAPH_NODATA_ERROR;
00237 
00238     
00239     memset(tga.hd, 0, TGA_HEADER_SIZE);
00240     if (tga.col==3 || tga.col==4) tga.hd[2] = 2;    
00241     else                          tga.hd[2] = 3;    
00242 
00243     unsigned short int* size = (unsigned short int*)&(tga.hd[12]);
00244     if (is_little_endian()) {
00245         size[0] = tga.xs;
00246         size[1] = tga.ys;
00247     }
00248     else {
00249         size[0] = htons(tga.xs);
00250         size[1] = htons(tga.ys);
00251     }
00252 
00253     tga.hd[16] = tga.col*8;
00254     tga.hd[17] = 0x08 | 0x20;       
00255 
00256     fwrite(tga.hd, TGA_HEADER_SIZE, 1, fp);
00257 
00258     
00259     int len = tga.xs*tga.ys*tga.col;
00260     fwrite(tga.gp, len, 1, fp);
00261 
00262     return 0;
00263 }
00264 
00265