// this is the function to call when we want to load an image tImageTGA * tgaLoad(const char *filename) { FILE *file; tImageTGA *info; int mode,total; // allocate memory for the info struct and check! info = (tImageTGA *)malloc(sizeof(tImageTGA)); if (info == NULL) return(NULL); // open the file for reading (binary mode) file = fopen(filename, "rb"); if (file == NULL) { info->status = TGA_ERROR_FILE_OPEN; return(info); } // load the header tgaLoadHeader(file,info); // check for errors when loading the header if (ferror(file)) { info->status = TGA_ERROR_READING_FILE; fclose(file); return(info); } // check if the image is color indexed if (info->type == 1) { info->status = TGA_ERROR_INDEXED_COLOR; fclose(file); return(info); } // check for other types (compressed images) if ((info->type != 2) && (info->type !=3) && (info->type !=10) ) { info->status = TGA_ERROR_COMPRESSED_FILE; fclose(file); return(info); } // mode equals the number of image components mode = info->pixelDepth / 8; // total is the number of unsigned chars to read total = info->height * info->width * mode; // allocate memory for image pixels info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total); // check to make sure we have the memory required if (info->imageData == NULL) { info->status = TGA_ERROR_MEMORY; fclose(file); return(info); } // finally load the image pixels if ( info->type == 10 ) tgaLoadRLEImageData(file, info); else tgaLoadImageData(file,info); // check for errors when reading the pixels if (ferror(file)) { info->status = TGA_ERROR_READING_FILE; fclose(file); return(info); } fclose(file); info->status = TGA_OK; if ( info->flipped ) { tgaFlipImage( info ); if ( info->flipped ) info->status = TGA_ERROR_MEMORY; } return(info); }
// this is the function to call when we want to load an image tImageTGA * tgaLoad(const char *pszFilename) { int mode,total; tImageTGA *info = NULL; CCFileData data(pszFilename, "rb"); unsigned long nSize = data.getSize(); unsigned char* pBuffer = data.getBuffer(); do { CC_BREAK_IF(! pBuffer); info = (tImageTGA *)malloc(sizeof(tImageTGA)); // get the file header info if (! tgaLoadHeader(pBuffer, nSize, info)) { info->status = TGA_ERROR_MEMORY; break; } // check if the image is color indexed if (info->type == 1) { info->status = TGA_ERROR_INDEXED_COLOR; break; } // check for other types (compressed images) if ((info->type != 2) && (info->type !=3) && (info->type !=10) ) { info->status = TGA_ERROR_COMPRESSED_FILE; break; } // mode equals the number of image components mode = info->pixelDepth / 8; // total is the number of unsigned chars to read total = info->height * info->width * mode; // allocate memory for image pixels info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total); // check to make sure we have the memory required if (info->imageData == NULL) { info->status = TGA_ERROR_MEMORY; break; } bool bLoadImage = false; // finally load the image pixels if ( info->type == 10 ) { bLoadImage = tgaLoadRLEImageData(pBuffer, nSize, info); } else { bLoadImage = tgaLoadImageData(pBuffer, nSize, info); } // check for errors when reading the pixels if (! bLoadImage) { info->status = TGA_ERROR_READING_FILE; break; } info->status = TGA_OK; if ( info->flipped ) { tgaFlipImage( info ); if ( info->flipped ) { info->status = TGA_ERROR_MEMORY; } } } while(0); return info; }