2011-09-03 38 views
7

我想了解数据如何存储到IDAT块中。我正在写一个PHP类,我可以检索大部分块信息,但是我得到的IDAT与我的图像的像素不匹配:如何使用PNG的IDAT块?

enter image description here它是2×2px具有alpha(bitdepth 8)的truecolour。

但当我解释IDAT的数据是这样的:

current(unpack('H*',gzuncompress($idat_data))); 

我得到

00000000ffffff00ffffff000000

我不明白它如何能匹配像素。或者是我的代码破坏数据?

感谢您的帮助!

编辑:我得到

08d705c101010000008010ff4f1750a93029e405fb

为十六进制的压缩数据,这样看来我解压后亏几个字节。

enter image description here

回答

7

使用gzinflate尝试,但跳过第2个字节和最后4首。

$contents = file_get_contents($in_filename); 
$pos = 8; // skip header 

$color_types = array('Greyscale','unknown','Truecolour','Indexed-color','Greyscale with alpha','unknown','Truecolor with alpha'); 
$len = strlen($contents); 
$safety = 1000; 
do { 
    list($unused,$chunk_len) = unpack('N', substr($contents,$pos,4)); 

    $chunk_type = substr($contents,$pos+4,4); 

    $chunk_data = substr($contents,$pos+8,$chunk_len); 

    list($unused,$chunk_crc) = unpack('N', substr($contents,$pos+8+$chunk_len,4)); 
    echo "chunk length:$chunk_len(dec) 0x" . sprintf('%08x',$chunk_len) . "h<br>\n"; 
    echo "chunk crc :0x" . sprintf('%08x',$chunk_crc) . "h<br>\n"; 
    echo "chunk type :$chunk_type<br>\n"; 
    echo "chunk data $chunk_type bytes:<br>\n" . chunk_split(bin2hex($chunk_data)) . "<br>\n"; 
    switch($chunk_type) { 
     case 'IHDR': 
     list($unused,$width,$height) = unpack('N2', substr($chunk_data,0,8)); 
     list($unused,$depth,$Color_type,$Compression_method,$Filter_method,$Interlace_method) = unpack('C*', substr($chunk_data,8)); 
     echo "Width:$width,Height:$height,depth:$depth,Color_type:$Color_type(" . $color_types[$Color_type] . "),Compression_method:$Compression_method,Filter_method:$Filter_method,Interlace_method:$Interlace_method<br>\n"; 
     $bytes_per_pixel = $depth/8; 
     break; 

     case 'PLTE': 
     $palette = array(); 
     for($i=0;$i<$chunk_len;$i+=3) { 
      $tupl = bin2hex(substr($chunk_data,$i,3)); 
      $palette[] = $tupl; 
      if($i && ($i % 30 == 0)) { 
       echo "<br>\n"; 
      } 
      echo '<span style="color:' . $tupl . ';">[' . $tupl . ']</span>'; 
     } 
     echo print_r($palette,true) . "<br>"; 
     break; 

     case 'IDAT': 
     $compressed = substr($chunk_data,2,$chunk_len - 6); // 2 bytes on the front and 4 at the end 
     $decompressed = gzinflate($compressed); 
     echo "decompressed chunk data " . strlen($decompressed) . " bytes:<br>\n" . chunk_split(bin2hex($decompressed),2 + $width * $bytes_per_pixel * 2) . "<br>\n"; 
     for($row=0; $row<$height; $row++) { 
      for($col=1; $col<=$width; $col++) { 
       $index = (int)substr($decompressed,((int)$row*($width+1)+$col),1); 
       echo '<span style="color:' . $palette[$index] . ';">' . $index . '</span>'; 
      } 
      echo "<br>\n"; 
     } 
     // TODO use filters described here: 
     // http://www.w3.org/TR/PNG/#9Filters 
     // first byte of scan line is filter type 
     break; 

    } 
    $pos += $chunk_len + 12; 
    echo "<hr>"; 
} while(($pos < $len) && --$safety); 
+0

谢谢,现在膨胀工作,但我得到“00000000ffffff00ffffff000000 “(14个字节),它们如何用于获取像素? – MatTheCat

+0

为了获得良好的压缩效果,PNG格式在压缩之前应用滤镜。这些滤波器的作用如下:如果两条扫描线彼此几乎相同,则较低线上与上述像素相匹配的像素将变为零。所以当你完成你有一个零碎的零和压缩是非常好的。所以你需要反转,并在解压缩后撤销过滤器。见http://www.w3.org/TR/PNG/#9过滤器 – Charlie

+0

正确,过滤器“将扫描行中的字节序列转换为以过滤器类型字节开头的等长字节序列”。所以我不应该有18个字节的未压缩数据(1“字节深度”* 4通道* 4像素+ 2个滤波器)? – MatTheCat

4
00000000 ffffff00 ffffff00 0000xxxx 
black white white black 

这就是我能告诉什么(这是正确的)......但你缺少在最后2个字节。

+1

我认为每个扫描线都有一个过滤器类型的字节?丢失的字节可能来自不良的解压缩算法? – MatTheCat

+0

我并没有真正阅读过PNG,但你所呈现的数据似乎与你应该得到的数据相对应,除非它不是全部......所以我个人无法帮你解释为什么这可能会发生:http://www.w3.org/TR/PNG/#11IDAT @leonbloy可能是正确的关于多个IDAT块,但我觉得很奇怪,一个如此之小的块将被拆分...是你确定你正在解压所有的字节? – Andreas

+0

感谢但http://www.w3.org/TR/PNG/#4Concepts.EncodingFiltering它似乎过滤器类型应该在数据中存在,所以更多的字节将被丢失? (我知道在我的情况下只有一个IDAT块)^^) – MatTheCat

3

为了增加@Andreas(+1)的解析,有两两件事需要注意:

  1. PNG文件可以(而且经常有)很多IDAT块,它们必须被连接起来以恢复压缩的zlib流。 http://www.w3.org/TR/PNG/#10CompressionFSL

  2. Gzip/Compress/Deflate都是相关的,但并不完全相同。 PNG使用deflate/inflate。我会跟gzdeflate/gzinflate

+0

我试过了,但是在使用gzinflate = /时(我的图像只有一个IDAT块),我得到一个数据错误 – MatTheCat

+0

@MatTheCat我已经使用Java Deflater/Infalter类实现了PNG读取/写入,它的工作完美无瑕。也许你会尝试剥离前两个字节? 'gzinflate(substr($ idat_data,2)'?http://www.php.net/manual/en/function.gzinflate.php#70875 – leonbloy

+0

我刚注意到IDAT块的长度部分小于它的数据长度,我认为这是问题的来源,但我无法猜测为什么 – MatTheCat