-
Notifications
You must be signed in to change notification settings - Fork 4
/
decoder.coffee
131 lines (108 loc) · 4.03 KB
/
decoder.coffee
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
{ getBasename } = require './utils'
decoder = (buf) ->
offset = 0
readBytes = (count) ->
end = Math.min offset + count, buf.length
v = buf.slice offset, end
offset = end
v
readChars = (count = 1) -> readBytes(count).toString('ascii')
readUInt16 = -> offset += 2; v = buf.readUInt16BE(offset-2)
readUInt32 = -> offset += 4; v = buf.readUInt32BE(offset-4)
readString = ->
length = readUInt32()
v = ''
for i in [0...length]
v += String.fromCharCode(readUInt16())
v
extractValue = (str) ->
# remove wrapper double quote
value = str.replace /^"(.*)"$/, '$1'
# e.x: $$$/acb/Pantone/ProcessYellow=Process Yellow CP
if value.startsWith '$$$'
value = value.split('=')[1]
value = value.replace '^R', '®'
value = value.replace '^C', '©'
value
ColorSpace = (c) ->
switch c
when 0 then 'RGB'
when 1 then 'HSB'
when 2 then 'CMYK'
when 3 then 'Pantone'
when 4 then 'Focoltone'
when 5 then 'Trumatch'
when 6 then 'Toyo'
when 7 then 'LAB'
when 8 then 'Grayscale'
when 10 then 'HKS'
else
throw new Error "Unknown color space value (#{c})"
SpotProcessIdentifier = (v) ->
switch v
when 'spflspot' then 'Spot'
when 'spflproc' then 'Process'
else
new Error "Unknown Spot/Process Identifier(#{v})"
decode = ->
if readChars(4) isnt '8BCB'
throw new Error 'Invalid .acb data'
version = readUInt16()
identifier = readUInt16()
title = extractValue readString()
prefix = extractValue readString()
suffix = extractValue readString()
trimmed = suffix.trim()
description = extractValue readString()
colorCount = readUInt16()
pageSize = readUInt16()
pageSelectorOffset = readUInt16()
colorSpace = ColorSpace readUInt16()
channels = switch colorSpace
when 'CMYK' then 4
when 'Grayscale' then 1
else 3
records = {}
for i in [0...colorCount]
colorName = extractValue readString()
colorCode = readChars(6).trim()
colorCode = colorCode.replace /^0*(\d+)$/ , '$1'
colorCode = colorCode.replace 'X', '-'
raw = readBytes channels
# Skip dummy record
if !colorName and !colorCode
continue
if !colorName
tail = suffix.trim()
pos = colorCode.lastIndexOf trimmed
colorName = if pos >= 0 then colorCode[0...pos] else colorCode
record =
name: prefix + colorName + suffix
code: colorCode
record.components = switch colorSpace
when 'LAB'
record.components = [
raw[0] / 2.55 # 0% thru 100%
raw[1] - 128 # -128 thru 127
raw[2] - 128 # -128 thru 127
]
when 'CMYK'
record.components = [
(255 - raw[0]) / 2.55 # 0% thru 100%
(255 - raw[1]) / 2.55 # 0% thru 100%
(255 - raw[2]) / 2.55 # 0% thru 100%
(255 - raw[3]) / 2.55 # 0% thru 100%
]
when 'RGB'
record.components = [ raw[0], raw[1], raw[2] ]
records[record.name] = record
spotIdentifier = SpotProcessIdentifier readChars(8)
realColorCount = Object.keys(records).length
{
version, title, description, prefix, suffix, colorCount: realColorCount, pageSize
colorSpace, channels,
isSpot: spotIdentifier is 'Spot',
records
}
return { decode}
module.exports = decoder