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

file.coffee

lib/psd/
{jspack} = require 'jspack'
iconv = require 'iconv-lite'
Color = require './color.coffee'
Util = require './util.coffee'

A file abstraction that stores the PSD file data, and assists in parsing it.

module.exports = class File
  FORMATS =
    Int:
      code: '>i'
      length: 4
    UInt:
      code: '>I'
      length: 4
    Short:
      code: '>h'
      length: 2
    UShort:
      code: '>H'
      length: 2
    Float:
      code: '>f'
      length: 4
    Double:
      code: '>d'
      length: 8
    LongLong:
      code: '>q'
      length: 8

  for own format, info of FORMATS then do (format, info) =>
    @::["read#{format}"] = -> @readf(info.code, info.length)[0]

The current cursor position in the file.

  pos: 0

Creates a new File with the given Uint8Array.

  constructor: (@data) ->

Returns the current cursor position.

  tell: -> @pos

Reads raw file data with no processing.

  read: (length) -> (@data[@pos++] for i in [0...length])

Reads file data and processes it with the given unpack format string. If the length is omitted, then it will be calculated automatically based on the format string.

  readf: (format, len = null) -> jspack.Unpack format, @read(len or jspack.CalcLength(format))

Moves the cursor without parsing data. If rel = false, then the cursor will be set to the given value, which effectively sets the position relative to the start of the file. If rel = true, then the cursor will be moved relative to the current position.

  seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt

Reads a String of the given length.

  readString: (length) -> String.fromCharCode.apply(null, @read(length)).replace /\u0000/g, ""

Reads a Unicode UTF-16BE encoded string.

  readUnicodeString: (length = null) ->
    length or= @readInt()
    iconv.decode(new Buffer(@read(length * 2)),'utf-16be').replace /\u0000/g, ""

Helper that reads a single byte.

  readByte: -> @read(1)[0]

Helper that reads a single byte and interprets it as a boolean.

  readBoolean: -> @readByte() isnt 0

Reads a 32-bit color space value.

  readSpaceColor: ->
    colorSpace = @readShort()
    colorComponent = (@readShort() >> 8) for i in [0...4]

    colorSpace: colorSpace, components: colorComponent

Adobe's lovely signed 32-bit fixed-point number with 8bits.24bits http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577409_17587

  readPathNumber: ->
    a = @readByte()
    
    arr = @read(3)
    b1 = arr[0] << 16
    b2 = arr[1] << 8
    b3 = arr[2]
    b = b1 | b2 | b3

    parseFloat(a, 10) + parseFloat(b / Math.pow(2, 24), 10)

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