【问题标题】:Inflate data from PNG only succeed with specific image?来自PNG的膨胀数据仅在特定图像上成功?
【发布时间】:2017-03-29 04:18:47
【问题描述】:

我在尝试从 PNG 图像文件的 IDAT 块中膨胀数据时遇到了一些麻烦。到目前为止,我能够读取 IHDR 块并返回正确的数据。但是,当我尝试解压缩以获取文件的原始 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 (相同的图像但被裁剪),程序只能返回 RGB 数据的前 3 个字节(其余为 0)。是不是我用这段代码错误地实现了一些东西?

【问题讨论】:

    标签: png zlib inflate


    【解决方案1】:

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

    对于没有“成功”的图像,第一行使用过滤器1,后续行使用过滤器2。过滤器1是Sub过滤器,表示第一行之后的前面的像素值已经从它们中减去了之前的像素值。 2 表示 Up 过滤器,其中第一行之后的每个像素值都减去该位置的前一行像素值。这就是为什么在第一个值之后几乎所有(但不是所有)值都为零的原因。更仔细地查看结果。

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2012-05-25
      相关资源
      最近更新 更多