我目前正在编写自己的 png 阅读器,我正在阅读单个块,它似乎正确读取了前 2 个块,但是当涉及到 IDAT 块时,它的大小很可笑。
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