C++ Zlib通货膨胀流未设置为PNG IDAT数据末尾的Z_流_端
为了学习,我正在尝试编写自己的PNG解码器。我用来解压缩数据的代码如下:C++ Zlib通货膨胀流未设置为PNG IDAT数据末尾的Z_流_端,c++,png,zlib,C++,Png,Zlib,为了学习,我正在尝试编写自己的PNG解码器。我用来解压缩数据的代码如下: //Stores the return code from the inflation stream int returnCode; //Tracks amount of data inflated unsigned int dataReturned; //Inflation stream z_stream stream; //Initalising the inflation state stream.zalloc
//Stores the return code from the inflation stream
int returnCode;
//Tracks amount of data inflated
unsigned int dataReturned;
//Inflation stream
z_stream stream;
//Initalising the inflation state
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = chunkSize;
stream.next_in = compressedPixelData;
//Beginning the inflation stream
cout << "Beginning inflation" << endl;
returnCode = inflateInit(&stream);
//Checks for any errors with the inflation stream
if (returnCode != Z_OK)
throw invalid_argument("Provided file is corrupted or does not use deflate as its compression algorithm");
//Pointing the stream to the output array
stream.avail_out = chunkSize;
stream.next_out = pixelData;
//Inflating the data
returnCode = inflate(&stream, Z_NO_FLUSH);
//Checking for errors
switch (returnCode) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
throw runtime_error("Error while decompressing pixel data. Either out of memory or the file is corrupted.");
}
dataReturned = chunkSize - stream.avail_out;
cout << dataReturned << endl;
//Ending the deflation stream and cleaning up
cout << "Return Code: " << returnCode << endl;
(void)inflateEnd(&stream);
delete[] compressedPixelData;
//存储充气流的返回代码
返回码;
//跟踪数据量
返回无符号整数数据;
//通货膨胀流
z_溪;
//启动通货膨胀状态
stream.zalloc=Z_NULL;
stream.zfree=Z_NULL;
stream.opaque=Z_NULL;
stream.avail_in=chunkSize;
stream.next_in=压缩像素数据;
//开始通货膨胀
正如Shawn指出的,解压后的数据比原始输入数据大。增加流的大小。avali_out
和输出缓冲区,pixelData
修复了该问题。根据,Z_stream_END
不是错误,与Z_OK
一样有效。错误代码是负数,这两个不是。您确定在pixelData
/流中分配了足够的空间。下一步退出并设置流。将退出
设置为正确的大小?还是在解压所有数据之前,一次充气呼叫就把空间填满了?将stream.avail\u in
和stream.avail\u out
设置为相同的值很奇怪。@Shawn我的理解是stream.avail\u in
是可解压缩的字节数,stream.avail\u out
是输出缓冲区中的可用空间。因为我已经将输出缓冲区设置为与完整输入缓冲区相同的大小,所以它们都是相同的值。@usr2564301我知道Z_STREAM\u END
不是错误状态。它表示要膨胀的数据流的结束。因为我已经传入了所有数据,所以我希望返回Z_STREAM\u END
,但是我得到的是Z_OK
。在解压缩时,输出大小通常大于输入大小,不过。。。如果您使用的是相同大小的数据,那么仍有需要解压缩的数据,而您使用的输出缓冲区中没有空间,因此Z_OK
。