aboutsummaryrefslogtreecommitdiffstats
path: root/src/psdimage.coffee
blob: 03c1f5705384067fb30f3a6ddecc036167bc970d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# A PSDImage stores parsed image data for images contained within the PSD, and
# for the PSD itself.
class PSDImage
  COMPRESSIONS =
    0: 'Raw'
    1: 'RLE'
    2: 'ZIP'
    3: 'ZIPPrediction'

  channelsInfo: [
    {id: 0},
    {id: 1},
    {id: 2},
    {id: -1}
  ]

  constructor: (@file, @header) ->
    @numPixels = @getImageWidth() * @getImageHeight()
    @numPixels *= 2 if @getImageDepth() is 16

    @length = switch @getImageDepth()
      when 1 then (@getImageWidth() + 7) / 8 * @getImageHeight()
      when 16 then @getImageWidth() * @getImageHeight() * 2
      else @getImageWidth() * @getImageHeight()

    @channelLength = @length # in bytes
    @length *= @getImageChannels()

    @channelData = new Uint8Array(@length)

    @startPos = @file.tell()
    @endPos = @startPos + @length

    @pixelData = []

  parse: ->
    @compression = @parseCompression()

    # ZIP compression isn't implemented yet. Luckily this is pretty rare. Still,
    # it's a TODO.
    Log.debug "Image size: #{@length} (#{@getImageWidth()}x#{@getImageHeight()})"

    if @compression in [2, 3]
      Log.debug "ZIP compression not implemented yet, skipping."
      return @file.seek @endPos, false

    @parseImageData()

  skip: ->
    Log.debug "Skipping image data"
    @file.seek @length

  parseCompression: -> @file.readShortInt()

  parseImageData: ->
    Log.debug "Image compression: id=#{@compression}, name=#{COMPRESSIONS[@compression]}"

    switch @compression
      when 0 then @parseRaw()
      when 1 then @parseRLE()
      when 2, 3 then @parseZip()
      else
        Log.debug "Unknown image compression. Attempting to skip."
        return @file.seek @endPos, false

    @processImageData()

  # Parse the image data as raw pixel values. There is no compression used here.
  parseRaw: (length = @length) ->
    Log.debug "Attempting to parse RAW encoded image..."
    @channelData = []
    @channelData.push @file.read(1)[0] for i in [0...length]

    return true

  # Parse the image with RLE compression. This is the same as the TIFF standard format.
  # Contains the scanline byte lengths first, then the actual encoded image data.
  parseRLE: ->
    Log.debug "Attempting to parse RLE encoded image..."

    # RLE stores the scan line byte counts in the first chunk of data
    @byteCounts = @getByteCounts()

    Log.debug "Read byte counts. Current pos = #{@file.tell()}, Pixels = #{@length}"

    @parseChannelData()

  # Get the height of the image. This varies depending on whether we're parsing layer
  # channel data or not.
  getImageHeight: -> @header.rows
  getImageWidth: -> @header.cols
  getImageChannels: -> @header.channels
  getImageDepth: -> @header.depth

  getByteCounts: ->
    byteCounts = []
    for i in [0...@getImageChannels()]
      for j in [0...@getImageHeight()]
        byteCounts.push @file.readShortInt()

    byteCounts

  parseChannelData: ->
    # And then it stores the compressed image data
    chanPos = 0
    lineIndex = 0

    for i in [0...@getImageChannels()] # i = plane num
      Log.debug "Parsing channel ##{i}, Start = #{@file.tell()}"
      [chanPos, lineIndex] = @decodeRLEChannel(chanPos, lineIndex)

    return true

  decodeRLEChannel: (chanPos, lineIndex) ->
    for j in [0...@getImageHeight()]
      byteCount = @byteCounts[lineIndex++]
      start = @file.tell()

      while @file.tell() < start + byteCount
        [len] = @file.read(1)

        if len < 128
          len++
          data = @file.read len

          # memcpy!
          dataIndex = 0
          @channelData[k] = data[dataIndex++] for k in [chanPos...chanPos+len]

          chanPos += len
        else if len > 128
          len ^= 0xff
          len += 2

          [val] = @file.read(1)
          data = []
          data.push val for z in [0...len]

          dataIndex = 0
          @channelData[k] = data[dataIndex++] for k in [chanPos...chanPos+len]

          chanPos += len

    [chanPos, lineIndex]

  parseZip: (prediction = false) ->
    #stream = inflater.append @file.read(@length)
    #inflater.flush()

    # ZIP decompression not implemented until I can find a PSD that's actually using it
    @file.seek @endPos, false

  # Once we've read the image data, we need to organize it and/or convert the pixel
  # values to RGB so they're easier to work with and can be easily output to either
  # browser or file.
  processImageData: ->
    Log.debug "Processing parsed image data. #{@channelData.length} pixels read."

    switch @header.mode
      when 1 # Greyscale
        @combineGreyscale8Channel() if @getImageDepth() is 8
        @combineGreyscale16Channel() if @getImageDepth() is 16
      when 3 # RGBColor
        @combineRGB8Channel() if @getImageDepth() is 8
        @combineRGB16Channel() if @getImageDepth() is 16
      when 4 #CMKYColor
        @combineCMYK8Channel() if @getImageDepth() is 8
        @combineCMYK16Channel() if @getImageDepth() is 16
      when 7 # Multichannel
        @combineMultiChannel8()
      when 9 #LABColor
        @combineLAB8Channel() if @getImageDepth() is 8

    # Manually delete channel data to free up memory
    delete @channelData

  getAlphaValue: (alpha = 255) ->
    # Layer opacity
    alpha = alpha * (@layer.blendMode.opacity / 255) if @layer?
    alpha

  combineGreyscale8Channel: ->
    if @getImageChannels() is 2
      # Has alpha channel
      for i in [0...@numPixels]
        alpha = @channelData[i]
        grey = @channelData[@channelLength + i]

        @pixelData.push grey, grey, grey, @getAlphaValue(alpha)
    else
      for i in [0...@numPixels]
        @pixelData.push @channelData[i], @channelData[i], @channelData[i], @getAlphaValue()

  combineGreyscale16Channel: ->
    if @getImageChannels() is 2
      # Has alpha channel
      for i in [0...@numPixels] by 2
        alpha = Util.toUInt16 @channelData[i + 1], @channelData[i]
        grey = Util.toUInt16 @channelData[@channelLength + i + 1], @channelData[@channelLength + i]

        @pixelData.push grey, grey, grey, @getAlphaValue(alpha)
    else
      for i in [0...@numPixels] by 2
        pixel = Util.toUInt16 @channelData[i+1], @channelData[i]
        @pixelData.push pixel, pixel, pixel, @getAlphaValue()

  combineRGB8Channel: ->
    for i in [0...@numPixels]
      index = 0
      pixel = r: 0, g: 0, b: 0, a: 255

      for chan in @channelsInfo
        switch chan.id
          when -1
            if @getImageChannels() is 4
              pixel.a = @channelData[i + (@channelLength * index)]
            else continue
          when 0 then pixel.r = @channelData[i + (@channelLength * index)]
          when 1 then pixel.g = @channelData[i + (@channelLength * index)]
          when 2 then pixel.b = @channelData[i + (@channelLength * index)]

        index++

      @pixelData.push pixel.r, pixel.g, pixel.b, @getAlphaValue(pixel.a)
      

  combineRGB16Channel: ->
    for i in [0...@numPixels] by 2
      index = 0
      pixel = r: 0, g: 0, b: 0, a: 255

      for chan in @channelsInfo
        b1 = @channelData[i + (@channelLength * index) + 1]
        b2 = @channelData[i + (@channelLength * index)]

        switch chan.id
          when -1
            if @getImageChannels() is 4
              pixel.a = Util.toUInt16(b1, b2)
            else continue
          when 0 then pixel.r = Util.toUInt16(b1, b2)
          when 1 then pixel.g = Util.toUInt16(b1, b2)
          when 2 then pixel.b = Util.toUInt16(b1, b2)

        index++

      @pixelData.push pixel.r, pixel.g, pixel.b, @getAlphaValue(pixel.a)

  combineCMYK8Channel: ->
    for i in [0...@numPixels]
      if @getImageChannels() is 5
        a = @channelData[i]
        c = @channelData[i + @channelLength]
        m = @channelData[i + @channelLength * 2]
        y = @channelData[i + @channelLength * 3]
        k = @channelData[i + @channelLength * 4]
      else
        a = 255
        c = @channelData[i]
        m = @channelData[i + @channelLength]
        y = @channelData[i + @channelLength * 2]
        k = @channelData[i + @channelLength * 3]

      rgb = PSDColor.cmykToRGB(255 - c, 255 - m, 255 - y, 255 - k)

      @pixelData.push rgb.r, rgb.g, rgb.b, @getAlphaValue(a)

  combineCMYK16Channel: ->
    for i in [0...@numPixels] by 2
      if @getImageChannels() is 5
        a = @channelData[i]
        c = @channelData[i + @channelLength]
        m = @channelData[i + @channelLength * 2]
        y = @channelData[i + @channelLength * 3]
        k = @channelData[i + @channelLength * 3]
      else
        a = 255
        c = @channelData[i]
        m = @channelData[i + @channelLength]
        y = @channelData[i + @channelLength * 2]
        k = @channelData[i + @channelLength * 3]

      rgb = PSDColor.cmykToRGB(255 - c, 255 - m, 255 - y, 255 - k)

      @pixelData.push rgb.r, rgb.g, rgb.b, @getAlphaValue(a)

  combineLAB8Channel: ->
    for i in [0...@numPixels]
      if @getImageChannels() is 4
        alpha = @channelData[i]
        l = @channelData[i + @channelLength]
        a = @channelData[i + @channelLength * 2]
        b = @channelData[i + @channelLength * 3]
      else
        alpha = 255
        l = @channelData[i]
        a = @channelData[i + @channelLength]
        b = @channelData[i + @channelLength * 2]

      rgb = PSDColor.labToRGB l * 100 >> 8, a - 128, b - 128

      @pixelData.push rgb.r, rgb.g, rgb.b, @getAlphaValue(alpha)

  combineMultiChannel8: ->
    for i in [0...@numPixels]
      c = @channelData[i]
      m = @channelData[i + @channelLength]
      y = @channelData[i + @channelLength * 2]

      if @getImageChannels() is 4
        k = @channelData[i + @channelLength * 3]
      else
        k = 255

      rgb = PSDColor.cmykToRGB(255 - c, 255 - m, 255 - y, 255 - k)

      @pixelData.push rgb.r, rgb.g, rgb.b, @getAlphaValue(255)

      
  # Normally, the pixel data is stored in planar order, meaning all the red
  # values come first, then the green, then the blue. In the HTML canvas, the
  # pixel color values are grouped by pixel such that the values alternate
  # between R, G, B, A over and over.
  #
  # This will return the pixels in the same format that is used by the HTML 
  # canvas. It is a 1D array that consists of a single pixel's color values 
  # expressed from 0 to 255 in chunks of 4. Each chunk consists of red, 
  # green, blue, and alpha values, respectively.
  #
  # This means a pure-red single pixel is expressed as: `[255, 0, 0, 255]`
  toCanvasPixels: -> @pixelData

  toFile: (filename, cb) ->
    return cb() if @toCanvasPixels().length is 0

    png = @getPng()
    return cb() if png is null

    png.encode (image) -> fs.writeFile filename, image, cb

  toFileSync: (filename) ->
    return if @toCanvasPixels().length is 0
    
    png = @getPng()
    return if png is null

    image = png.encodeSync()
    fs.writeFileSync filename, image

  getPng: ->
    try
      {Png} = require 'png'
    catch e
      throw "Exporting PSDs to file requires the node-png library"

    buffer = new Buffer @toCanvasPixels().length
    pixelData = @toCanvasPixels()

    for i in [0...pixelData.length] by 4
      buffer[i] = pixelData[i]
      buffer[i+1] = pixelData[i+1]
      buffer[i+2] = pixelData[i+2]
      buffer[i+3] = 255 - pixelData[i+3] # Why is this inverted?

    try
      new Png buffer, @getImageWidth(), @getImageHeight(), 'rgba'
    catch e
      Log.debug e
      return null

  toCanvas: (canvas, width = null, height = null) ->
    if width is null and height is null
      canvas.width = @getImageWidth()
      canvas.height = @getImageHeight()

    context = canvas.getContext('2d')
    imageData = context.getImageData 0, 0, canvas.width, canvas.height
    pixelData = imageData.data

    pixelData[i] = pxl for pxl, i in @toCanvasPixels()

    context.putImageData imageData, 0, 0

  toImage: ->
    canvas = document.createElement 'canvas'
    @toCanvas canvas
    canvas.toDataURL "image/png"