aboutsummaryrefslogtreecommitdiffstats
path: root/src/layerdata/curves.coffee
blob: 142d21d68b66236919890b664a637a34c16554f0 (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
class PSDCurves
  constructor: (@layer, @length) ->
    @file = @layer.file
    @data =
      curve: []

  parse: ->
    start = @file.tell()

    # Data padding. Docs are wrong. Maybe Photoshop is wrong.
    # I don't know what's right anymore.
    @file.seek 1

    version = @file.readShortInt()
    assert version in [1, 4]

    tag = @file.readInt()
    @data.curveCount = 0
    for i in [0...32]
      # Count of the curves in the file
      @data.curveCount++ if tag & (1 << i)

    for i in [0...@data.curveCount]
      count = 0
      for j in [0...32]
        if tag & (1 << j)
          if count is i
            @data.curve[i] = channelIndex: j
            break

          count++

      @data.curve[i].pointCount = @file.getShortInt()
      assert @data.curve[i].pointCount >= 2
      assert @data.curve[i].pointCount <= 19

      for j in [0...@data.curve[i].pointCount]
        # Curve points. Each point is a pair of short ints where the
        # first number is the output value and the second is the input.
        @data.curve[i].outputValue[j] = @file.readShortInt()
        @data.curve[i].inputValue[j] = @file.readShortInt()

        assert @data.curve[i].outputValue[j] >= 0
        assert @data.curve[i].outputValue[j] <= 255
        assert @data.curve[i].inputValue[j] >= 0
        assert @data.curve[i].inputValue[j] <= 255

    # If this is true, we have some additional information to parse.
    if @file.tell() - start < @length - 4
      tag = @file.readString 4
      assert.equal tag, 'Crv '

      version = @file.readShortInt()
      assert version is 4

      curveCount = @file.readInt()
      assert.equal curveCount, @data.curveCount

      for i in [0...@data.curveCount]
        @data.curve[i].channelIndex = @file.readShortInt()
        pointCount = @file.readShortInt()
        assert pointCount is @data.curve[i].pointCount

        for j in [0...pointCount]
          outputValue = @file.getShortInt()
          inputValue = @file.getShortInt()
          assert.equal outputValue, @data.curve[i].outputValue[j]
          assert.equal inputValue, @data.curve[i].inputValue[j]

    # Only returned the parsed data
    @data