def initialize(data)
data = StringIO.new(data.dup)
data.read(8)
@palette = ""
@img_data = ""
@transparency = {}
loop do
chunk_size = data.read(4).unpack("N")[0]
section = data.read(4)
case section
when 'IHDR'
values = data.read(chunk_size).unpack("NNCCCCC")
@width = values[0]
@height = values[1]
@bits = values[2]
@color_type = values[3]
@compression_method = values[4]
@filter_method = values[5]
@interlace_method = values[6]
when 'PLTE'
@palette << data.read(chunk_size)
when 'IDAT'
@img_data << data.read(chunk_size)
when 'tRNS'
@transparency = {}
case @color_type
when 3
@transparency[:indexed] = data.read(chunk_size).unpack("C*")
short = 255 - @transparency[:indexed].size
@transparency[:indexed] += ([255] * short) if short > 0
when 0
grayval = data.read(chunk_size).unpack("n").first
@transparency[:grayscale] = grayval
when 2
@transparency[:rgb] = data.read(chunk_size).unpack("nnn")
end
when 'IEND'
break
else
data.seek(data.pos + chunk_size)
end
data.read(4)
end
unfilter_image_data if alpha_channel?
end