00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "ExCImage.h"
00025
00026 bool LoadBMP(tImage *image,std::string strFileName)
00027 {
00028 return LoadBMP(image,strFileName.data());
00029 }
00030
00031 bool LoadTGA(tImage *image,std::string strFileName)
00032 {
00033 return LoadTGA(image,strFileName.data());
00034 }
00035
00036 bool LoadJPG(tImage *image,std::string strFileName)
00037 {
00038 return LoadJPG(image,strFileName.data());
00039 }
00040
00041 bool LoadBMP(tImage *image,const char *strFileName)
00042 {
00043
00044 FILE *file;
00045 unsigned long size;
00046 unsigned long i;
00047 unsigned short int planes;
00048 unsigned short int channels;
00049 char temp;
00050
00051
00052 if ((file = fopen(strFileName, "rb"))==NULL)
00053 {
00054 printf("File Not Found : %s\n",strFileName);
00055 return false;
00056 }
00057 #ifdef UNIX_SRC
00058 unsigned short int is_bmp;
00059 if (! fread (&is_bmp, sizeof (short int), 1, file))
00060 {
00061 printf("cannot read %s.\n", strFileName);
00062 return false;
00063 }
00064
00065
00066 if (is_bmp != 19778)
00067 {
00068 printf("%s is not a valid bmp file.\n", strFileName);
00069 return false;
00070 }
00071
00072 fseek (file, 8, SEEK_CUR);
00073
00074 long int bfOffBits;
00075 if (! fread (&bfOffBits, sizeof (long int), 1, file))
00076 {
00077 printf("Error reading %s.\n", strFileName);
00078 return false;
00079 }
00080
00081
00082
00083
00084 fseek(file, 4, SEEK_CUR);
00085 #else
00086 fseek(file, 18, SEEK_SET);
00087 #endif
00088
00089 if ((i = fread(&image->sizeX, 4, 1, file)) != 1)
00090 {
00091 printf("Error reading width from %s.\n", strFileName);
00092 return false;
00093 }
00094
00095
00096 if ((i = fread(&image->sizeY, 4, 1, file)) != 1)
00097 {
00098 printf("Error reading height from %s.\n", strFileName);
00099 return false;
00100 }
00101
00102
00103
00104 size = image->sizeX * image->sizeY * 3;
00105
00106
00107 if ((fread(&planes, 2, 1, file)) != 1)
00108 {
00109 printf("Error reading planes from %s.\n", strFileName);
00110 return false;
00111 }
00112
00113 if (planes != 1)
00114 {
00115 printf("Planes from %s is not 1: %u.\n", strFileName, planes);
00116 return false;
00117 }
00118
00119
00120 if ((i = fread(&channels, 2, 1, file)) != 1)
00121 {
00122 printf("Error reading channels from %s.\n", strFileName);
00123 return false;
00124 }
00125 if (channels != 24)
00126 {
00127 printf("channels from %s is not 24: %u\n", strFileName, channels);
00128 return false;
00129 }
00130
00131
00132 fseek(file, 24, SEEK_CUR);
00133
00134 image->data = new unsigned char[size];
00135
00136
00137
00138 if (image->data == NULL)
00139 {
00140 printf("Error allocating memory for color-corrected image data");
00141 return false;
00142 }
00143
00144 if ((i = fread(image->data, size, 1, file)) != 1)
00145 {
00146 printf("Error reading image data from %s.\n", strFileName);
00147 return false;
00148 }
00149
00150 for (i=0;i<size;i+=3)
00151 {
00152
00153 temp = image->data[i];
00154 image->data[i] = image->data[i+2];
00155 image->data[i+2] = temp;
00156 }
00157
00158 image->type=GL_RGB;
00159 return true;
00160 }
00161
00162 bool LoadTGA(tImage *image,const char *strFileName)
00163 {
00164 WORD width = 0, height = 0;
00165 GLbyte length = 0;
00166 GLbyte imageType = 0;
00167 GLbyte bits = 0;
00168 FILE *pFile = NULL;
00169 int channels = 0;
00170 int stride = 0;
00171 int i = 0;
00172
00173
00174 if((pFile = fopen(strFileName, "rb")) == NULL)
00175 {
00176
00177 printf("Unable to load TGA File!");
00178 return false;
00179 }
00180
00181
00182 image = (tImage*)malloc(sizeof(tImage));
00183
00184
00185 fread(&length, sizeof(GLbyte), 1, pFile);
00186
00187
00188 fseek(pFile,1,SEEK_CUR);
00189
00190
00191 fread(&imageType, sizeof(GLbyte), 1, pFile);
00192
00193
00194 fseek(pFile, 9, SEEK_CUR);
00195
00196
00197 fread(&width, sizeof(WORD), 1, pFile);
00198 fread(&height, sizeof(WORD), 1, pFile);
00199 fread(&bits, sizeof(GLbyte), 1, pFile);
00200
00201
00202 fseek(pFile, length + 1, SEEK_CUR);
00203
00204
00205 if(imageType != TGA_RLE)
00206 {
00207
00208 if(bits == 24 || bits == 32)
00209 {
00210
00211
00212 channels = bits / 8;
00213 stride = channels * width;
00214 image->data = ((unsigned char*)malloc(sizeof(unsigned char)*stride*height));
00215
00216
00217 for(uint y = 0; y < height; y++)
00218 {
00219
00220 unsigned char *pLine = &(image->data[stride * y]);
00221
00222
00223 fread(pLine, stride, 1, pFile);
00224
00225
00226
00227 for(i = 0; i < stride; i += channels)
00228 {
00229 int temp = pLine[i];
00230 pLine[i] = pLine[i + 2];
00231 pLine[i + 2] = temp;
00232 }
00233 }
00234 }
00235
00236 else if(bits == 16)
00237 {
00238 unsigned short pixels = 0;
00239 int r=0, g=0, b=0;
00240
00241
00242
00243 channels = 3;
00244 stride = channels * width;
00245 image->data = ((unsigned char*)malloc(sizeof(unsigned char)*stride*height));
00246
00247
00248 for(uint i = 0; i < width*height; i++)
00249 {
00250
00251 fread(&pixels, sizeof(unsigned short), 1, pFile);
00252
00253
00254 b = (pixels & 0x1f) << 3;
00255 g = ((pixels >> 5) & 0x1f) << 3;
00256 r = ((pixels >> 10) & 0x1f) << 3;
00257
00258
00259
00260 image->data[i * 3 + 0] = r;
00261 image->data[i * 3 + 1] = g;
00262 image->data[i * 3 + 2] = b;
00263 }
00264 }
00265
00266 else
00267 return false;
00268 }
00269
00270 else
00271 {
00272
00273 GLbyte rleID = 0;
00274 int colorsRead = 0;
00275 channels = bits / 8;
00276 stride = channels * width;
00277
00278
00279
00280 image->data = ((unsigned char*)malloc(sizeof(unsigned char)*stride*height));
00281 GLbyte *pColors = ((GLbyte*)malloc(sizeof(GLbyte)*channels));
00282
00283
00284 while((unsigned)i < width*height)
00285 {
00286
00287 fread(&rleID, sizeof(GLbyte), 1, pFile);
00288
00289
00290 if(rleID < (unsigned)128)
00291 {
00292
00293 rleID++;
00294
00295
00296 while(rleID)
00297 {
00298
00299 fread(pColors, sizeof(GLbyte) * channels, 1, pFile);
00300
00301
00302 image->data[colorsRead + 0] = pColors[2];
00303 image->data[colorsRead + 1] = pColors[1];
00304 image->data[colorsRead + 2] = pColors[0];
00305
00306
00307 if(bits == 32)
00308 image->data[colorsRead + 3] = pColors[3];
00309
00310
00311
00312 i++;
00313 rleID--;
00314 colorsRead += channels;
00315 }
00316 }
00317
00318 else
00319 {
00320
00321 rleID -= 127;
00322
00323
00324 fread(pColors, sizeof(GLbyte) * channels, 1, pFile);
00325
00326
00327 while(rleID)
00328 {
00329
00330 image->data[colorsRead + 0] = pColors[2];
00331 image->data[colorsRead + 1] = pColors[1];
00332 image->data[colorsRead + 2] = pColors[0];
00333
00334
00335 if(bits == 32)
00336 image->data[colorsRead + 3] = pColors[3];
00337
00338
00339
00340 i++;
00341 rleID--;
00342 colorsRead += channels;
00343 }
00344
00345 }
00346
00347 }
00348 }
00349
00350
00351 fclose(pFile);
00352
00353
00354 image->channels = channels;
00355 image->sizeX = width;
00356 image->sizeY = height;
00357
00358
00359 return true;
00360 }
00361
00362 bool LoadJPG(tImage *image,const char *strFileName)
00363 {
00364 return true;
00365 }
00366
00367
00368
00369