Commit 4c8ab8d6 authored by Ryan LeFevre's avatar Ryan LeFevre

RGB image processing complete

parent 969ddd48
...@@ -38,3 +38,10 @@ module.exports = class File ...@@ -38,3 +38,10 @@ module.exports = class File
seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt
readString: (length) -> @read(length).toString() readString: (length) -> @read(length).toString()
# readUInt: ->
# @data.readUInt32(@pos)
# @pos += 4
# readInt: ->
# @data.readInt32(@pos)
# @pos += 4
\ No newline at end of file
module.exports = class Image {Module} = require 'coffeescript-module'
ImageFormat = require './image_format.coffee'
ImageMode = require './image_mode.coffee'
module.exports = class Image extends Module
@includes ImageFormat.RAW
@includes ImageFormat.RLE
@includes ImageMode.RGB
COMPRESSIONS = [
'Raw'
'RLE'
'ZIP'
'ZIPPrediction'
]
constructor: (@file, @header) -> constructor: (@file, @header) ->
@width = @header.width @numPixels = @width() * @height()
@height = @header.height @numPixels *= 2 if @depth() is 16
@calculateLength()
@channelData = []
@pixelData = []
@opacity = 1.0
@hasMask = false
@startPos = @file.tell()
@endPos = @startPos + @length
@channelsInfo = [
{id: 0}
{id: 1}
{id: 2}
]
@channelsInfo.push {id: -1} if @channels() is 4
for attr in ['width', 'height', 'channels', 'depth', 'mode'] then do (attr) =>
@::[attr] = -> @header[attr]
calculateLength: ->
@length = switch @depth()
when 1 then (@width() + 7) / 8 * @height()
when 16 then @width() * @height() * 2
else @width() * @height()
@channelLength = @length
@length *= @channels()
parse: -> parse: ->
console.log "parse!" @compression = @parseCompression()
if @compression in [2, 3]
@file.seek @endPos
return
@parseImageData()
parseCompression: -> @file.readShort()
parseImageData: ->
switch @compression
when 0 then @parseRaw()
when 1 then @parseRLE()
when 2, 3 then @parseZip()
else @file.seek(@endPos)
@processImageData()
processImageData: ->
switch @mode()
when 1 then @combineGreyscaleChannel()
when 3 then @combineRgbChannel()
when 4 then @combineCmykChannel()
@channelData = null
\ No newline at end of file
module.exports =
RAW: require('./image_formats/raw.coffee')
RLE: require('./image_formats/rle.coffee')
\ No newline at end of file
module.exports =
parseRaw: ->
@channelData = @file.read(@length)
\ No newline at end of file
module.exports =
parseRLE: ->
@byteCounts = @parseByteCounts()
@parseChannelData()
parseByteCounts: ->
@file.readShort() for i in [0...(@channels() * @height())]
parseChannelData: ->
@chanPos = 0
@lineIndex = 0
for i in [0...@channels()]
@decodeRLEChannel()
@lineIndex += @height()
decodeRLEChannel: ->
for j in [0...@height()]
byteCount = @byteCounts[@lineIndex + j]
finish = @file.tell() + byteCount
while @file.tell() < finish
len = @file.read(1)[0]
if len < 128
len += 1
@channelData.splice @chanPos, 0, @file.read(len)...
@chanPos += len
else if len > 128
len ^= 0xff
len += 2
val = @file.read(1)[0]
@channelData[@chanPos++] = val for i in [0...len]
\ No newline at end of file
module.exports =
RGB: require('./image_modes/rgb.coffee')
\ No newline at end of file
module.exports =
combineRgbChannel: ->
rgbChannels = @channelsInfo
.map (ch) -> ch.id
.filter (ch) -> ch >= -1
for i in [0...@numPixels]
r = g = b = 0
a = 255
for chan, index in rgbChannels
val = @channelData[i + (@channelLength * index)]
switch chan
when -1 then a = val
when 0 then r = val
when 1 then g = val
when 2 then b = val
@pixelData.push r, g, b, a
\ No newline at end of file
...@@ -6,7 +6,7 @@ module.exports = class LayerMask ...@@ -6,7 +6,7 @@ module.exports = class LayerMask
@mergedAlpha = false @mergedAlpha = false
@globalMask = null @globalMask = null
skip: -> @file.seek @file.readInt() skip: -> @file.seek @file.readInt(), true
parse: -> parse: ->
maskSize = @file.readInt() maskSize = @file.readInt()
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
"version": "0.0.1", "version": "0.0.1",
"dependencies": { "dependencies": {
"coffee-script": "~ 1.7.1", "coffee-script": "~ 1.7.1",
"jspack": "~ 0.0.3" "jspack": "~ 0.0.3",
"coffeescript-module": "~ 0.0.2"
}, },
"devDependencies": { "devDependencies": {
"coffeeify": "~ 0.6.0" "coffeeify": "~ 0.6.0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment