1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
struct TARGAFILE {
char idLength;
char colourMapType;
char imageType;
short colourMapStart;
short colourMapNumEntries;
char bitsPerEntry;
short xOrigin;
short yOrigin;
short width;
short height;
char bitsPerPixel;
char* pixelData;
};
TARGAFILE fetchImage(const std::string& pathToImage) {
std::ifstream image(pathToImage, std::ios::binary);
if (!image.is_open()) {
throw FileNotFound();
}
TARGAFILE targaFile;
image.read(&targaFile.idLength, sizeof(targaFile.idLength));
image.read(&targaFile.colourMapType, sizeof(targaFile.colourMapType));
image.read(&targaFile.imageType, sizeof(targaFile.imageType));
image.read((char*)(&targaFile.colourMapStart), sizeof(targaFile.colourMapStart));
image.read((char*)(&targaFile.colourMapNumEntries), sizeof(targaFile.colourMapNumEntries));
image.read(&targaFile.bitsPerEntry, sizeof(targaFile.bitsPerEntry));
image.read((char*)(&targaFile.xOrigin), sizeof(targaFile.xOrigin));
image.read((char*)(&targaFile.yOrigin), sizeof(targaFile.yOrigin));
image.read((char*)(&targaFile.width), sizeof(targaFile.width));
image.read((char*)(&targaFile.height), sizeof(targaFile.height));
image.read(&targaFile.bitsPerPixel, sizeof(targaFile.bitsPerPixel));
int imageDataSize = targaFile.width * targaFile.height * (targaFile.bitsPerPixel / 8);
targaFile.pixelData = new char[imageDataSize];
int originalPosition = (int)image.tellg();
image.read(targaFile.pixelData, imageDataSize);
return targaFile;
}
void CheckHdrImage(TARGAFILE targaFile){
std::ofstream output("test.tga", std::ios::binary);
output.write(&targaFile.idLength, sizeof(targaFile.idLength));
output.write(&targaFile.colourMapType, sizeof(targaFile.colourMapType));
output.write(&targaFile.imageType, sizeof(targaFile.imageType));
output.write(reinterpret_cast<const char*>(&targaFile.colourMapStart), sizeof(targaFile.colourMapStart));
output.write(reinterpret_cast<const char*>(&targaFile.colourMapNumEntries), sizeof(targaFile.colourMapNumEntries));
output.write(&targaFile.bitsPerEntry, sizeof(targaFile.bitsPerEntry));
output.write(reinterpret_cast<const char*>(&targaFile.xOrigin), sizeof(targaFile.xOrigin));
output.write(reinterpret_cast<const char*>(&targaFile.yOrigin), sizeof(targaFile.yOrigin));
output.write(reinterpret_cast<const char*>(&targaFile.width), sizeof(targaFile.width));
output.write(reinterpret_cast<const char*>(&targaFile.height), sizeof(targaFile.height));
output.write(&targaFile.bitsPerPixel, sizeof(targaFile.bitsPerPixel));
output.write(reinterpret_cast<const char*>(&targaFile.pixelData), sizeof(targaFile.pixelData));
output.close();
}
|