Tuesday, 17 September 2013

decompress GIF with multiple data-blocks

decompress GIF with multiple data-blocks

I wrote python script that takes GIF data like this :
['8c', '2d', '99', '87', '2a', '1c', 'dc']
and turn it to this:
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
first I get all the data in all the blocks and turn it to a stream of bits
>> I am not sure about this step .I don't know if I have to decompress all
the blocks at once as one stream or decompress them Individually
for byte in data:
bit=bin(int(m,16))[2:]
while len(bit)<8:
bit='0'+bit
c.append(bit[::-1] )
stream= ''.join(c)
decompress(stream)
here is the decompress function
def decompress(s,InitialCodeSize):
index=[]
table=Initialize(InitialCodeSize)
codesize = InitialCodeSize + 1
ClearCode=int(math.pow(2, InitialCodeSize))
L=int(math.pow(2, codesize))
code = int(s[:codesize][::-1],2)
s=s[codesize:]
c = table[code]
old = c
for i in c:
index.append(i)
while s !='':
code = int(s[:codesize][::-1],2)
s=s[codesize:]
if code == ClearCode:
table=Initialize(InitialCodeSize)
codesize = InitialCodeSize + 1
if code in list(xrange(len(table ))):
c=table[code]
table.append(old + [c[0]])
else:
table.append(old + [old[0]])
c= old + [old[0]]
for i in c:
index.append(i)
old=c
if len(table) == L and codesize< 12:
codesize=codesize+1
L=int(math.pow(2, codesize))
return index
everything work just fine when the image has few blocks but when it has
more blocks it always return smaller number of pixels
for example if the image 522*200 =104400 pixels it give me only 61806
pixels I though maybe the GIF format uses RLE encoding

No comments:

Post a Comment