Commit 565c4a6e authored by Ryan LeFevre's avatar Ryan LeFevre

Implement layer blend modes and info

parent 691412d9
This diff is collapsed.
......@@ -4,6 +4,7 @@ psd = PSD.fromFile('./examples/images/example.psd');
psd.parse();
console.log(psd.header.export());
psd.image.saveAsPng('./output.png').then(function () {
console.log("Finished!");
});
\ No newline at end of file
// psd.image.saveAsPng('./output.png').then(function () {
// console.log("Finished!");
// });
console.log(psd.layers.map(function (l) { return l.export(); }));
\ No newline at end of file
......@@ -16,6 +16,9 @@ module.exports = class PSD
@parsed = false
@header = null
Object.defineProperty @, 'layers',
get: -> @layerMask.layers
parse: ->
return if @parsed
......
{Module} = require 'coffeescript-module'
module.exports = class BlendMode extends Module
@aliasProperty 'blendingMode', 'mode'
# All of the blend modes are stored in the PSD file with a specific key.
# This is the mapping of that key to its readable name.
BLEND_MODES = {
norm: 'normal',
dark: 'darken',
lite: 'lighten',
hue: 'hue',
sat: 'saturation',
colr: 'color',
lum: 'luminosity',
mul: 'multiply',
scrn: 'screen',
diss: 'dissolve',
over: 'overlay',
hLit: 'hard_light',
sLit: 'soft_light',
diff: 'difference',
smud: 'exclusion',
div: 'color_dodge',
idiv: 'color_burn',
lbrn: 'linear_burn',
lddg: 'linear_dodge',
vLit: 'vivid_light',
lLit: 'linear_light',
pLit: 'pin_light',
hMix: 'hard_mix',
pass: 'passthru',
dkCl: 'darker_color',
lgCl: 'lighter_color',
fsub: 'subtract',
fdiv: 'divide'
}
constructor: (@file) ->
@blendKey = null
@opacity = null
@clipping = null
@flags = null
@mode = null
parse: ->
@file.seek 4, true
@blendKey = @file.readString(4).trim()
@opacity = @file.readByte()
@clipping = @file.readByte()
@flags = @file.readByte()
@mode = BLEND_MODES[@blendKey]
@clipped = @clipping is 1
@visible = !((@flags & (0x01 << 1)) > 0)
@file.seek 1, true
opacityPercentage: -> @opacity * 100 / 255
......@@ -38,10 +38,4 @@ module.exports = class File
seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt
readString: (length) -> @read(length).toString()
# readUInt: ->
# @data.readUInt32(@pos)
# @pos += 4
# readInt: ->
# @data.readInt32(@pos)
# @pos += 4
\ No newline at end of file
readByte: -> @read(1)[0]
\ No newline at end of file
{Module} = require 'coffeescript-module'
module.exports = class Layer extends Module
@includes require('./layer/position_channels.coffee')
@includes require('./layer/blend_modes.coffee')
# @includes require('./layer/blending_ranges.coffee')
# @includes require('./layer/channel_image.coffee')
constructor: (@file, @header) ->
@mask = {}
@blendingRanges = {}
@adjustments = {}
@channelsInfo = []
@blendMode = {}
@groupLayer = null
@infoKeys = []
parse: ->
@parsePositionAndChannels()
@parseBlendModes()
extraLen = @file.readInt()
@layerEnd = @file.tell() + extraLen
@file.seek @layerEnd
return @
export: ->
top: @top
right: @right
bottom: @bottom
left: @left
width: @width
height: @height
opacity: @opacity
visible: @visible
clipped: @clipped
\ No newline at end of file
BlendMode = require '../blend_mode.coffee'
module.exports =
parseBlendModes: ->
@blendMode = new BlendMode(@file)
@blendMode.parse()
@opacity = @blendMode.opacity
@visible = @blendMode.visible
@clipped = @blendMode.clipped
hidden: -> not @visible
# TODO: check section divider
blendingMode: ->
@blendMode.mode
\ No newline at end of file
module.exports =
parsePositionAndChannels: ->
@top = @file.readInt()
@left = @file.readInt()
@bottom = @file.readInt()
@right = @file.readInt()
@channels = @file.readShort()
@rows = @height = @bottom - @top
@cols = @width = @right - @left
for i in [0...@channels]
id = @file.readShort()
length = @file.readInt()
@channelsInfo.push id: id, length: length
Util = require('./util.coffee')
Layer = require('./layer.coffee')
module.exports = class LayerMask
constructor: (@file, @header) ->
......@@ -11,8 +12,6 @@ module.exports = class LayerMask
parse: ->
maskSize = @file.readInt()
finish = maskSize + @file.tell()
@file.seek finish
return
return if maskSize <= 0
......@@ -25,3 +24,9 @@ module.exports = class LayerMask
layerCount = Math.abs layerCount
@mergedAlpha = true
for i in [0...layerCount]
@layers.push new Layer(@file, @header).parse()
@layers.reverse()
@file.seek finish
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