Jump To …
READMElib / psd / blend_mode.coffeelib / psd / channel_image.coffeelib / psd / color.coffeelib / psd / descriptor.coffeelib / psd / file.coffeelib / psd / header.coffeelib / psd / image.coffeelib / psd / image_export.coffeelib / psd / image_exports / png.coffeelib / psd / image_format.coffeelib / psd / image_formats / layer_raw.coffeelib / psd / image_formats / layer_rle.coffeelib / psd / image_formats / raw.coffeelib / psd / image_formats / rle.coffeelib / psd / image_mode.coffeelib / psd / image_modes / cmyk.coffeelib / psd / image_modes / greyscale.coffeelib / psd / image_modes / rgb.coffeelib / psd / init.coffeelib / psd / layer / blend_modes.coffeelib / psd / layer / blending_ranges.coffeelib / psd / layer / channel_image.coffeelib / psd / layer / helpers.coffeelib / psd / layer / info.coffeelib / psd / layer / mask.coffeelib / psd / layer / name.coffeelib / psd / layer / position_channels.coffeelib / psd / layer.coffeelib / psd / layer_info / blend_clipping_elements.coffeelib / psd / layer_info / blend_interior_elements.coffeelib / psd / layer_info / fill_opacity.coffeelib / psd / layer_info / gradient_fill.coffeelib / psd / layer_info / layer_id.coffeelib / psd / layer_info / layer_name_source.coffeelib / psd / layer_info / legacy_typetool.coffeelib / psd / layer_info / locked.coffeelib / psd / layer_info / metadata.coffeelib / psd / layer_info / nested_section_divider.coffeelib / psd / layer_info / object_effects.coffeelib / psd / layer_info / section_divider.coffeelib / psd / layer_info / solid_color.coffeelib / psd / layer_info / typetool.coffeelib / psd / layer_info / unicode_name.coffeelib / psd / layer_info / vector_mask.coffeelib / psd / layer_info / vector_origination.coffeelib / psd / layer_info / vector_stroke.coffeelib / psd / layer_info / vector_stroke_content.coffeelib / psd / layer_info.coffeelib / psd / layer_mask.coffeelib / psd / lazy_execute.coffeelib / psd / mask.coffeelib / psd / node.coffeelib / psd / nodes / ancestry.coffeelib / psd / nodes / build_preview.coffeelib / psd / nodes / group.coffeelib / psd / nodes / layer.coffeelib / psd / nodes / root.coffeelib / psd / nodes / search.coffeelib / psd / path_record.coffeelib / psd / resource.coffeelib / psd / resource_section.coffeelib / psd / resources / layer_comps.coffeelib / psd / resources.coffeelib / psd / util.coffeelib / psd.coffeeshims / init.coffeeshims / png.coffee

channel_image.coffee

lib/psd/
_           = require 'lodash'
Image       = require './image.coffee'
ImageFormat = require './image_format.coffee'

Represents an image for a single layer, which differs slightly in format from the full size preview image.

The full preview at the end of the PSD document has the same compression for all channels, whereas layer images define the compression per color channel. The dimensions can also differ per channel if we're parsing mask data (channel ID < -1).

module.exports = class ChannelImage extends Image
  @includes ImageFormat.LayerRAW
  @includes ImageFormat.LayerRLE

Creates a new ChannelImage.

  constructor: (file, header, @layer) ->

We copy the layer's width and height to private variables because, as you'll see below, the dimensions can change if we're parsing a mask channel.

    @_width = @layer.width
    @_height = @layer.height

    super(file, header)

    @channelsInfo = @layer.channelsInfo
    @hasMask = _.any @channelsInfo, (c) -> c.id < -1
    @opacity = @layer.opacity / 255.0
    @maskData = []

Skip parsing this image by jumping to the end of the data.

  skip: ->
    for chan in @channelsInfo
      @file.seek chan.length, true

The width of the image.

  width: -> @_width

The height of the image.

  height: -> @_height

The number of color channels in the image.

  channels: -> @layer.channels

Parse the image data. The resulting image data will be formatted to match the Javascript Canvas color format, e.g. [R, G, B, A, R, G, B, A].

  parse: ->
    @chanPos = 0
    for chan in @channelsInfo
      if chan.length <= 0
        @parseCompression()
        continue

      @chan = chan

If we're working with a mask channel, then the mask can define it's own dimensions separate from the image dimensions. We grab these dimensions from the layer's mask data.

      if chan.id < -1
        @_width = @layer.mask.width
        @_height = @layer.mask.height
      else
        @_width = @layer.width
        @_height = @layer.height

      @length = @_width * @_height
      start = @file.tell()

      @parseImageData()

      finish = @file.tell()

      if finish isnt start + @chan.length
        @file.seek(start + @chan.length)

    @_width = @layer.width
    @_height = @layer.height

    @processImageData()

Initiates parsing of the image data, which is based on the compression type of the channel. Every channel defines it's own compression type, unlike the full PSD preview, which has a single compression type for the entire image.

  parseImageData: ->
    @compression = @parseCompression()

    switch @compression
      when 0 then @parseRaw()
      when 1 then @parseRLE()
      when 2, 3 then @parseZip()
      else @file.seek(@endPos)

generated Tue May 12 2015 11:08:10 GMT-0400 (EDT)