Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:我想写以前读过的.tga文件_C++_Ofstream_Tga - Fatal编程技术网

C++ C++;:我想写以前读过的.tga文件

C++ C++;:我想写以前读过的.tga文件,c++,ofstream,tga,C++,Ofstream,Tga,我读入了.tga头和图像数据块,并想将其写入一个新文件,但它不会,而且我不知道什么地方可能出错。这可能是我一直错过的一些简单的事情 非常感谢您的帮助 struct TGAFILE { char idLength; char colourMapType; char imageType; short colourMapStart; short colourMapNumEntries; short xOrigin; short yOrigin;

我读入了.tga头和图像数据块,并想将其写入一个新文件,但它不会,而且我不知道什么地方可能出错。这可能是我一直错过的一些简单的事情

非常感谢您的帮助

struct TGAFILE {
    char idLength;
    char colourMapType;
    char imageType;
    short colourMapStart;
    short colourMapNumEntries;
    short xOrigin;
    short yOrigin;
    char bitsPerPixel;
    char* pixelData;
};

struct TGAHELPERDATA {
    short width;
    short height;
    char bitsPerEntry;
    char descriptor;
};

TGAFILE loadImage(const std::string& pathToImage) {
    std::ifstream image(pathToImage, std::ios::binary);
    if (!image.is_open()) {
        throw FileNotFound();
    }
    TGAFILE tgaFile;
    TGAHELPERDATA tgaHelperData;
    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(&tgaHelperData.bitsPerEntry, sizeof(tgaHelperData.bitsPerEntry));
    image.read((char*)(&tgaFile.xOrigin), sizeof(tgaFile.xOrigin));
    image.read((char*)(&tgaFile.yOrigin), sizeof(tgaFile.yOrigin));
    image.read((char*)(&tgaHelperData.width), sizeof(tgaHelperData.width));
    image.read((char*)(&tgaHelperData.height), sizeof(tgaHelperData.height));
    image.read(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
    image.read(&tgaHelperData.descriptor, sizeof(tgaHelperData.descriptor));

     int imageDataSize = tgaHelperData.width * tgaHelperData.height * (tgaFile.bitsPerPixel / 8);

    tgaFile.pixelData = new char[imageDataSize];

    int originalPosition = (int)image.tellg();
    image.read(tgaFile.pixelData, imageDataSize);
    safeImage(tgaFile, tgaHelperData, imageDataSize);
    return tgaFile;
}

void safeImage(TGAFILE tgaFile, TGAHELPERDATA tgaHelperData, int imageDataSize){
    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(&tgaHelperData.bitsPerEntry, sizeof(&tgaHelperData.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*>(&tgaHelperData.width), sizeof(tgaHelperData.width));
    output.write(reinterpret_cast<const char*>(&tgaHelperData.height), sizeof(tgaHelperData.height));
    output.write(&tgaFile.bitsPerPixel, sizeof(tgaFile.bitsPerPixel));
    output.write(&tgaHelperData.descriptor, sizeof(tgaHelperData.descriptor));
    output.write(tgaFile.pixelData, imageDataSize);
    output.close();
}
struct文件{
字符长度;
字符颜色映射类型;
字符图像类型;
短时间启动;
短颜色;
短xOrigin;
短约里金;
字符比特像素;
字符*像素数据;
};
结构帮助数据{
短宽度;
矮;
字符比特率;
字符描述符;
};
TGAFILE loadImage(常量std::string和pathToImage){
std::ifstream映像(pathToImage,std::ios::binary);
如果(!image.is_open()){
抛出FileNotFound();
}
tga文件;
TGAHELPERDATA TGAHELPERDATA;
image.read(&tgaFile.idLength,sizeof(tgaFile.idLength));
image.read(&tgaFile.colormaptype,sizeof(tgaFile.colormaptype));
image.read(&tgaFile.imageType,sizeof(tgaFile.imageType));
image.read((char*)(&tgaFile.colormapstart)、sizeof(tgaFile.colormapstart));
image.read((char*)(&tgaFile.colormapnumentries),sizeof(tgaFile.colormapnumentries));
image.read(&tgaHelperData.bitsPerEntry,sizeof(tgaHelperData.bitsPerEntry));
read((char*)(&tgaFile.xOrigin)、sizeof(tgaFile.xOrigin));
image.read((char*)(&tgaFile.yOrigin),sizeof(tgaFile.yOrigin));
image.read((char*)(&tgaheloperdata.width)、sizeof(tgaheloperdata.width));
image.read((char*)(&tgaheloperdata.height)、sizeof(tgaheloperdata.height));
image.read(&tgaFile.bitsPerPixel,sizeof(tgaFile.bitsPerPixel));
image.read(&tgaHelperData.descriptor,sizeof(tgaHelperData.descriptor));
int imageDataSize=tgaHelperData.width*tgaHelperData.height*(tgaFile.bitsPerPixel/8);
tgaFile.pixelData=新字符[imageDataSize];
int originalPosition=(int)image.tellg();
image.read(tgaFile.pixelData,imageDataSize);
safeImage(tgaFile、TGAHeloperData、imageDataSize);
返回文件;
}
void safeImage(TGAFILE TGAFILE、tgaheloperdata tgaheloperdata、int-imageDataSize){
std::of流输出(“output.tga”,std::ios::binary);
output.write(&tgaFile.idLength,sizeof(tgaFile.idLength));
output.write(&tgaFile.colormaptype,sizeof(tgaFile.colormaptype));
output.write(&tgaFile.imageType,sizeof(tgaFile.imageType));
write(reinterpret_cast(&tgaFile.colormapstart),sizeof(tgaFile.colormapstart));
write(reinterpret_cast(&tgaFile.colormapnumentries),sizeof(tgaFile.colormapnumentries));
output.write(&tgaHelperData.bitsPerEntry,sizeof(&tgaHelperData.bitsPerEntry));
write(reinterpret_cast(&tgaFile.xOrigin),sizeof(tgaFile.xOrigin));
write(reinterpret_cast(&tgaFile.yOrigin),sizeof(tgaFile.yOrigin));
write(reinterpret_cast(&tgaHelperData.width),sizeof(tgaHelperData.width));
write(reinterpret_cast(&tgaHelperData.height),sizeof(tgaHelperData.height));
output.write(&tgaFile.bitsPerPixel,sizeof(tgaFile.bitsPerPixel));
output.write(&tgaHelperData.descriptor,sizeof(tgaHelperData.descriptor));
output.write(tgaFile.pixelData,imageDataSize);
output.close();
}

tgaheloperdata
的内容位于
tgaFile结构中时,它起作用。现在我将其拆分,它将不再工作,我无法打开输出文件,我现在感到困惑

逐字节比较输入和输出文件。有一些工具可以做到这一点。找出差异所在,然后查看写出该部分文件的特定代码部分,您将得到答案。什么是
tgaheloperdatatime
<代码>TGAHeloperData
从文件读取的数据为discarded@SamVarshavchik我使用
fc/b“pathOne”比较了命令提示符中的文件“pathTwo
,它说原始文件比编写的文件长,这可能是有意义的,因为我在图像数据之后遗漏了不重要的元数据block@VTT呃,就在那里犯了个错误。应该是
tgaheloperdata
。修好了。为什么要丢弃struct
tgaheloperdata
?或者你是说,bc'safeImage(…)'将'tgaheloperdatime'作为参数,这是一个错误(虽然不是在我的原始代码中,所以不是这样)好吧,也许“不重要的元数据”并不像你想的那么重要?