我目前正在编写我自己的png阅读器,我正在研究各个块的读取,并且它似乎正确地读取了前两个块,但是当涉及到IDAT块时,它显示为 产生了一个荒谬的大小。PNG块读取器,无效长度
bool LoadImage(char * path, Image * target)
{
std::ifstream file;
file.open("Lenna.png", std::ios::in | std::ios::binary);
if(!file.is_open())
return -false;
std::cout << "Opened file : Lenna.png" << std::endl;
struct stat filestatus;
if (stat("Lenna.png", &filestatus) != 0)
return false;
unsigned int fileSize = filestatus.st_size;
unsigned int bytesRead = 8;
file.seekg(8, std::ios::beg);
while(bytesRead < fileSize)
{
//Read the length, type, then the data and at last the crc(Cyclic redundancy crap, whatever that may be)
char length [4];
file.read(length, 4);
//Reverse the 4 bytes due to network type writing.
unsigned int dataLength = (length[0] << 24) | (length[1] << 16) | (length[2] << 8) | length[3];
char type [4];
file.read(type, 4);
char * data = new char[dataLength];
file.read(data, dataLength);
char crc [4];
file.read(crc, 4);
bytesRead += 12 + dataLength;
}
return true;
}
使用调试器读取所述第一2块作为
类型:IDHR
长度:13个字节
类型:sRGB的
长度:1个字节
类型:IDAT
长度:4294967201个字节
这是大约2.3 gb的数据和png是462kb。任何想法为什么会出错?
源图片:http://i.cubeupload.com/sCHXFV.png
文件中相应字节的值是什么? –
这里是十六进制编辑器中初始值的截图。 http://i.cubeupload.com/YiruKg.png – Xyro
请将该十六进制转储的文本内容粘贴到您的问题中。 –