Added code to load the contents of a file to memory on not-windows.

This commit is contained in:
Pavel Krajcevski 2012-08-26 20:53:55 -04:00
parent afd1e274fd
commit 13519fa019

View file

@ -1,4 +1,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "ImageFile.h" #include "ImageFile.h"
#ifdef PNG_FOUND #ifdef PNG_FOUND
@ -10,18 +14,22 @@ ImageFile::ImageFile(const char *filename) :
m_FileFormat( DetectFileFormat(filename) ) m_FileFormat( DetectFileFormat(filename) )
{ {
unsigned char *rawData = ReadFileData(filename); unsigned char *rawData = ReadFileData(filename);
if(rawData) {
LoadImage(rawData); LoadImage(rawData);
delete [] rawData; delete [] rawData;
} }
}
ImageFile::ImageFile(const char *filename, EImageFileFormat format) : ImageFile::ImageFile(const char *filename, EImageFileFormat format) :
m_FileFormat(format), m_FileFormat(format),
m_PixelData(0) m_PixelData(0)
{ {
unsigned char *rawData = ReadFileData(filename); unsigned char *rawData = ReadFileData(filename);
if(rawData) {
LoadImage(rawData); LoadImage(rawData);
delete [] rawData; delete [] rawData;
} }
}
ImageFile::~ImageFile() { ImageFile::~ImageFile() {
if(m_PixelData) { if(m_PixelData) {
@ -60,10 +68,41 @@ bool ImageFile::LoadImage(const unsigned char *rawImageData) {
#ifdef _MSC_VER #ifdef _MSC_VER
unsigned char *ImageFile::ReadFileData(const char *filename) { unsigned char *ImageFile::ReadFileData(const char *filename) {
//!FIXME! - Actually, implement me
assert(!"Not implemented!");
} }
#else #else
unsigned char *ImageFile::ReadFileData(const char *filename) { unsigned char *ImageFile::ReadFileData(const char *filename) {
FILE *fp = fopen(filename, "rb");
if(!fp) {
fprintf(stderr, "Error opening file for reading: %s\n", filename);
return 0;
}
// Check filesize
long fileSize = 0;
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
// Allocate data for file contents
unsigned char *rawData = new unsigned char[fileSize];
// Return stream to beginning of file
fseek(fp, 0, SEEK_SET);
assert(ftell(fp) == 0);
// Read all of the data
size_t bytesRead = fread(rawData, 1, fileSize, fp);
if(bytesRead != fileSize) {
assert(!"We didn't read as much data as we thought we had!");
fprintf(stderr, "Internal error: Incorrect file size assumption\n");
return 0;
}
// Close the file pointer
fclose(fp);
// Return the data..
return rawData;
} }
#endif #endif