PNG IDAT规范 [英] PNG IDAT specification
问题描述
我正在讨论W3 PNG规范(从头开始创建PNG库),我终于找到了如何创建绿色1x1图像。
I'm going over the W3 PNG specification (creating a PNG library from scratch) and I've finally found how to create a green 1x1 image.
现在我正在尝试创建一个混合的红色,绿色和蓝色像素的更大图像。让我们说一个4x4的图像。可悲的是,我得到了所有像素混合,其中一些是黑色或粉红色。
Now I'm trying to create a bigger image of mixed Red, Green and Blue pixels. Let's say a 4x4 image. Sadly I'm getting all the pixels mixed and some of them are Black or Pink.
详细信息:
- 签名:确定
- IHDR:OK
- 宽度:4
- Heigh:4
- 比特深度:8
- 颜色:2
- 过滤器:0
- 压缩:0
- Interlace:0
- Signature: OK
- IHDR: OK
- Width: 4
- Heigh: 4
- Bit depth: 8
- Color: 2
- Filter: 0
- Compression: 0
- Interlace: 0
* IDATA块:
- 4字节长度:Zlib放气后DATA的字节数
- 4 * 1字节类型:IDAT
- X字节数据:4 * 4bytes无符号整数,Zlib放气,一个接一个,网络字节顺序
- 255蓝色**
- 65280 for green **
- 16711680 for red **
- 4 bytes length: Number of bytes of the DATA after Zlib deflate
- 4*1 byte type: IDAT
- X bytes data: 4*4bytes unsigned integers, Zlib deflated, one after the other, Network byte order
- 255 for blue**
- 65280 for green**
- 16711680 for red**
**按位结果:
alpha<<24 | red<<16 | green<<8 | blue
使用alpha,blue,green和red取0到255之间的值
With alpha, blue, green and red taking values from 0 to 255
这有什么问题?
推荐答案
PNG像素是RGBA顺序,而不是ARGB,所以你需要写
The PNG pixels are in RGBA order, not ARGB, so you'd need to write
alpha | red<<24 | green << 16 | blue <<8
但您正在编写颜色类型2,因此您的像素应为3个字节每个
而不是4;你不能真正用4字节整数
对它们进行编码。因此要么将颜色类型更改为6,要么将
切换为将每个样本写为单个字节。But you are writing color type 2, so your pixels should be 3 bytes each instead of four; you can't really encode them in 4-byte integers as you've done. So either change the color type to 6 or switch to writing each sample as an individual byte.
此外,您需要在每行的开头添加一个过滤字节。零将为你工作
。因此,在您的示例4x4 RGB图像中,您需要为
行写入13个字节Also you need a filter byte at the beginning of each row. Zero will work for you. So in your example 4x4 RGB image you will need to write 13 bytes per row
0 R G B R G B R G B R G B 0 R G B R G B R G B R G B 0 R G B R G B R G B R G B 0 R G B R G B R G B R G B
然后将所有行字节连接成一个字节流和zlib-compress那个。它可以全部进入一个IDAT块。如果您需要编写较小的IDAT块,则必须首先对图像进行zlib压缩,然后将zlib输出拆分为连续IDAT块中的块。
then concatenate all the row bytes into one byte stream and zlib-compress that. It can all go into one IDAT chunk. If you need to write smaller IDAT chunks, you have to zlib-compress the image first, then split the zlib output into pieces that you put in consecutive IDAT chunks.
这篇关于PNG IDAT规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!