已解决的问题
我正在尝试读取.tga文件,提取标头和像素数据,检查一些标头值,然后将其写入新的.tga文件,但是它不能完全按照我想要的方式工作来。
struct TGAFILE {
char idLength;
char colourMapType;
char imageType;
short colourMapStart;
short colourMapNumEntries;
char bitsPerEntry;
short xOrigin;
short yOrigin;
short width;
short height;
char bitsPerPixel;
char* pixelData;
};
TGAFILE loadImage(const std::string& pathToImage) {
std::ifstream image(pathToImage, std::ios::binary);
if (!image.is_open()) {
throw FileNotFound();
}
TGAFILE tgaFile;
image.read(&tgaFile.idLength, sizeof(tgaFile.idLength));
image.read(&tgaFile.colourMapType, sizeof(tgaFile.colourMapType));
image.read(&tgaFile.imageType, sizeof(tgaFile.imageType));
image.read((char*)(&tgaFile.colourMapStart), sizeof(tgaFile.colourMapStart));
image.read((char*)(&tgaFile.colourMapNumEntries), sizeof(tgaFile.colourMapNumEntries));
image.read(&tgaFile.bitsPerEntry, sizeof(tgaFile.bitsPerEntry));
image.read((char*)(&tgaFile.xOrigin), sizeof(tgaFile.xOrigin));
image.read((char*)(&tgaFile.yOrigin), sizeof(tgaFile.yOrigin));
image.read((char*)(&tgaFile.width), sizeof(tgaFile.width));
image.read((char*)(&tgaFile.height), sizeof(tgaFile.height));
image.read(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
int imageDataSize = tgaFile.width * tgaFile.height * (tgaFile.bitsPerPixel / 8);
tgaFile.pixelData = new char[imageDataSize];
int originalPosition = (int)image.tellg();
image.read(tgaFile.pixelData, imageDataSize);
return tgaFile;
}
(检查一些标头值,但不更改任何内容)
void safeImage(TGAFILE tgaFile){
std::ofstream output("output.tga", std::ios::binary);
output.write(&tgaFile.idLength, sizeof(tgaFile.idLength));
output.write(&tgaFile.colourMapType, sizeof(tgaFile.colourMapType));
output.write(&tgaFile.imageType, sizeof(tgaFile.imageType));
output.write(reinterpret_cast<const char*>(&tgaFile.colourMapStart), sizeof(tgaFile.colourMapStart));
output.write(reinterpret_cast<const char*>(&tgaFile.colourMapNumEntries), sizeof(tgaFile.colourMapNumEntries));
output.write(&tgaFile.bitsPerEntry, sizeof(tgaFile.bitsPerEntry));
output.write(reinterpret_cast<const char*>(&tgaFile.xOrigin), sizeof(tgaFile.xOrigin));
output.write(reinterpret_cast<const char*>(&tgaFile.yOrigin), sizeof(tgaFile.yOrigin));
output.write(reinterpret_cast<const char*>(&tgaFile.width), sizeof(tgaFile.width));
output.write(reinterpret_cast<const char*>(&tgaFile.height), sizeof(tgaFile.height));
output.write(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
output.write(reinterpret_cast<const char*>(&tgaFile.pixelData), sizeof(tgaFile.pixelData));
output.close();
}
我认为output.write(reinterpret_cast<const char*>(&tgaFile.pixelData), sizeof(tgaFile.pixelData));
出了点问题,但是即使我没错,我仍然不知道那是什么。
我们将不胜感激。