Skip to content

Instantly share code, notes, and snippets.

@meltingice
Created February 29, 2012 00:36
Show Gist options
  • Save meltingice/1936535 to your computer and use it in GitHub Desktop.
Save meltingice/1936535 to your computer and use it in GitHub Desktop.
parseRLE: ->
Log.debug "Attempting to parse RLE encoded image..."
# RLE stores the scan line byte counts in the first chunk of data
byteCounts = []
for i in [[email protected]]
for j in [0...@height]
byteCounts.push @file.readShortInt()
Log.debug "Read byte counts. Current pos = #{@file.tell()}, Pixels = #{@length}"
# And then it stores the compressed image data
@channelData = []
@channelData.push 0 for x in [0...@length]
chanPos = 0
lineIndex = 0
for i in [[email protected]] # i = plane num
console.log "Parsing channel ##{i}, Start = #{@file.tell()}"
for j in [0...@height]
byteCount = byteCounts[lineIndex++]
start = @file.tell()
while @file.tell() < start + byteCount
[len] = @file.read(1)
if len < 128
len++
data = @file.read len
# memcpy!
@channelData[chanPos...chanPos+len] = data
chanPos += len
else if len > 128
len ^= 0xff
len += 2
[val] = @file.read(1)
data = []
data.push val for z in [0...len]
@channelData[chanPos...chanPos+len] = data
chanPos += len
switch @header.mode
when 3 # RGBColor
@combineRGB8Channel() if @header.depth is 8
@combineRGB16Channel() if @header.depth is 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment