在學(xué)習(xí) PNG 的編碼規(guī)范,看了 PNG文件結(jié)構(gòu)分析,大概知道圖像的數(shù)據(jù)儲(chǔ)存在 IDAT 數(shù)據(jù)塊里,不過(guò)不是原始數(shù)據(jù),而是對(duì)其采用 Zlib 壓縮。
于是我在 Python 里 import zlib
,對(duì) PNG 的圖像數(shù)據(jù)進(jìn)行解壓,似乎不能夠100%還原圖像數(shù)據(jù)。下面是我的操作過(guò)程。
這是我測(cè)試的 PNG 圖像:
圖像大小是2*3像素,像素 RGBA 值如下:
#eb6100ff #22ac38ff #ea68a2ff
#22ac38ff #ea68a2ff #eb6100ff
對(duì)應(yīng)的十六進(jìn)制數(shù)據(jù):
89504e470d0a1a0a0000000d49484452000000030000000208060000009d74661a0000002349444154089963789dc8f05f698dc5ff57198bfe332aadb1f87f624f1603e3cf380600af940c90637dc1550000000049454e44ae426082
根據(jù) PNG 規(guī)范定義,IDAT 數(shù)據(jù)庫(kù)除去數(shù)據(jù)塊長(zhǎng)度碼、數(shù)據(jù)塊類型碼、CRC碼,剩下的就是 IDAT 的內(nèi)容數(shù)據(jù)塊:089963789dc8f05f698dc5ff57198bfe332aadb1f87f624f1603e3cf380600af940c90
然后在 Python 2.7 用 zlib 對(duì)其進(jìn)行解壓:
import zlib
bitData = '089963789dc8f05f698dc5ff57198bfe332aadb1f87f624f1603e3cf380600af940c90'
print zlib.decompress(bitData.decode('hex')).encode('hex')
得到輸出:00eb6100ff22ac38ffea68a2ff0122ac38ffc8bc6a0001f95e00
除去掃描行序列00
和01
得到每一個(gè)像素的 RGBA 值:
#eb6100ff #22ac38ff #ea68a2ff
#22ac38ff #c8bc6a00 #01f95e00
對(duì)比前面像素的 RGBA 值,可見第2行的最后2個(gè)像素(c8bc6a00
和01f95e00
)的 RGBA 不是原來(lái)的值。不知是哪個(gè)環(huán)節(jié)出了問(wèn)題,希望 SF 的大神解答下,先謝過(guò)了,跪~
在查看對(duì)比這個(gè)PNG解壓源碼與題主的操作流程,發(fā)現(xiàn)問(wèn)題在于少了undo_filter
這一步。
在源碼png.py中
...
while len(a) >= rb + 1:
filter_type = a[0]
scanline = a[1:rb+1]
del a[:rb+1]
recon = self.undo_filter(filter_type, scanline, recon)
...
可以看出,每行的第一個(gè)字節(jié)是代表filter_type
, 不是行號(hào)?。ㄒ?yàn)橐膊恍枰。?br>00
eb6100ff22ac38ffea68a2ff01
22ac38ffc8bc6a0001f95e00
這里的00是沒(méi)有用濾波器,01是指用了減法濾波器
減法濾波器的定義:
def sub():
"""Undo sub filter."""
ai = 0
# Loops starts at index fu. Observe that the initial part
# of the result is already filled in correctly with
# scanline.
for i in range(fu, len(result)):
x = scanline[i]
a = result[ai]
result[i] = (x + a) & 0xff
ai += 1
c8bc6a00+22ac38ff=ea68a2ff (注意忽略字節(jié)進(jìn)位,即 &ff的作用)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。