来自 PNG 的数据膨胀仅对特定图像成功?

Inflate data from PNG only succeed with specific image?

我在尝试从 PNG 图像文件的 IDAT 块中扩充数据时遇到了一些问题。到目前为止,我能够读取 IHDR 块和 return 正确的数据。但是,当我尝试解压缩以获取文件的原始 RGB 值时,我只能在 1 个图像上成功,而从其他图像中获取 0。这是我用于从 zlib 膨胀的代码。

unsigned char temp[4];
fread(&temp,1,4,pf);
unsigned int ihdr_size = convert_endian(temp);

unsigned char blk[ihdr_size + 8];
fread(blk,1,ihdr_size + 8,pf);

if (check_ihdr(blk) == -1){
    fclose(pf);
    return -1;
}
this->width  = convert_endian(blk+4);
this->height = convert_endian(blk+8);
long area = this->width*this->height*3 +this->height+1;
printf("%ld x %ld\n",this->width, this->height);
printf("Bit depth: %1x\n", (unsigned char)blk[12]);
printf("Color type: %1x\n", (unsigned char)blk[13]);

while(fread(&temp,1,4,pf) > 0){
    unsigned int bsize = convert_endian(temp);
    unsigned char chunk[bsize+8] = {0};
    fread(chunk,1, bsize+8,pf);   //8 extra for block name and CRC
    if (chunk_type(chunk)== 1){ //IDAT
        this->picture = new unsigned char[area];

        z_stream infstream;
        infstream.zalloc = Z_NULL;
        infstream.zfree = Z_NULL;
        infstream.opaque = Z_NULL;
        infstream.avail_in = bsize; // size of input
        infstream.next_in = chunk+4; // input char array (without chunk name)
        infstream.avail_out = area; // size of output
        infstream.next_out = this->picture; // output char array

        inflateInit(&infstream);
        if (int er = inflate(&infstream, Z_NO_FLUSH) != Z_STREAM_END){
            printf("Error!\n");
        }
        inflateEnd(&infstream);
        printf("OUT:%d %ld\n", infstream.avail_out, area);
        for (long i = 0; i < 1000; i+=4){
            printf("%x %x %x\n",this->picture[i], this->picture[i+1], this->picture[i+2]);
        }


    }
    printf("B:%x\n",bsize);
    printf("%c%c%c%c\n",chunk[0], chunk[1], chunk[2], chunk[3]);
}

从上面,我已经成功地得到了这张图片的RGB值 image#1 然而,当我尝试使用这张图片时image2 (相同的图像但被裁剪),程序只能 return RGB 数据的前 3 个字节(其余为零)。是不是我用这段代码错误地实现了什么?

您没有正确解释结果。你有 +this->height 的原因是图像的每一行前面都有一个过滤器字节。对于"succeeded"的图像,过滤字节全部为零,表示没有过滤。这意味着像素值直接出现在解压缩的数据中。即使在那种情况下,您也会显示不正确的像素值字节,因为您没有查看并越过过滤器字节。

没有"succeed"的图片,第一行使用滤镜1,后面几行使用滤镜2。滤镜1为Sub滤镜,说明第一个像素值之后的前一个像素值减去前一个像素值。 2 表示 Up 过滤器,其中第一行之后的每个像素值都减去该位置的前一行像素值。这就是为什么几乎所有(但 不是 全部)第一个之后的值都是零的原因。仔细看结果。