psd 3.4.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +196 -149
  2. package/dist/descriptor.d.ts +20 -0
  3. package/dist/descriptor.d.ts.map +1 -0
  4. package/dist/descriptor.js +114 -0
  5. package/dist/descriptor.js.map +1 -0
  6. package/dist/engine-data.d.ts +11 -0
  7. package/dist/engine-data.d.ts.map +1 -0
  8. package/dist/engine-data.js +82 -0
  9. package/dist/engine-data.js.map +1 -0
  10. package/dist/image.d.ts +52 -0
  11. package/dist/image.d.ts.map +1 -0
  12. package/dist/image.js +301 -0
  13. package/dist/image.js.map +1 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/layer-info.d.ts +494 -0
  19. package/dist/layer-info.d.ts.map +1 -0
  20. package/dist/layer-info.js +529 -0
  21. package/dist/layer-info.js.map +1 -0
  22. package/dist/layer-styles.d.ts +160 -0
  23. package/dist/layer-styles.d.ts.map +1 -0
  24. package/dist/layer-styles.js +183 -0
  25. package/dist/layer-styles.js.map +1 -0
  26. package/dist/layer.d.ts +143 -0
  27. package/dist/layer.d.ts.map +1 -0
  28. package/dist/layer.js +249 -0
  29. package/dist/layer.js.map +1 -0
  30. package/dist/nodes.d.ts +233 -0
  31. package/dist/nodes.d.ts.map +1 -0
  32. package/dist/nodes.js +326 -0
  33. package/dist/nodes.js.map +1 -0
  34. package/dist/path-record.d.ts +34 -0
  35. package/dist/path-record.d.ts.map +1 -0
  36. package/dist/path-record.js +44 -0
  37. package/dist/path-record.js.map +1 -0
  38. package/dist/png.d.ts +10 -0
  39. package/dist/png.d.ts.map +1 -0
  40. package/dist/png.js +85 -0
  41. package/dist/png.js.map +1 -0
  42. package/dist/psd.d.ts +49 -0
  43. package/dist/psd.d.ts.map +1 -0
  44. package/dist/psd.js +106 -0
  45. package/dist/psd.js.map +1 -0
  46. package/dist/reader.d.ts +41 -0
  47. package/dist/reader.d.ts.map +1 -0
  48. package/dist/reader.js +100 -0
  49. package/dist/reader.js.map +1 -0
  50. package/dist/resources.d.ts +105 -0
  51. package/dist/resources.d.ts.map +1 -0
  52. package/dist/resources.js +173 -0
  53. package/dist/resources.js.map +1 -0
  54. package/dist/type-tool.d.ts +142 -0
  55. package/dist/type-tool.d.ts.map +1 -0
  56. package/dist/type-tool.js +222 -0
  57. package/dist/type-tool.js.map +1 -0
  58. package/package.json +26 -32
  59. package/src/descriptor.ts +141 -0
  60. package/src/engine-data.ts +86 -0
  61. package/src/image.ts +338 -0
  62. package/src/index.ts +38 -0
  63. package/src/layer-info.ts +578 -0
  64. package/src/layer-styles.ts +339 -0
  65. package/src/layer.ts +334 -0
  66. package/src/nodes.ts +411 -0
  67. package/src/path-record.ts +69 -0
  68. package/src/png.ts +92 -0
  69. package/src/psd.ts +133 -0
  70. package/src/reader.ts +125 -0
  71. package/src/resources.ts +273 -0
  72. package/src/type-tool.ts +270 -0
  73. package/.travis.yml +0 -5
  74. package/Cakefile +0 -61
  75. package/DEVELOPMENT.md +0 -27
  76. package/bower.json +0 -26
  77. package/docs/docco.css +0 -284
  78. package/docs/lib/psd/blend_mode.coffee.html +0 -72
  79. package/docs/lib/psd/channel_image.coffee.html +0 -81
  80. package/docs/lib/psd/color.coffee.html +0 -13
  81. package/docs/lib/psd/descriptor.coffee.html +0 -173
  82. package/docs/lib/psd/file.coffee.html +0 -78
  83. package/docs/lib/psd/header.coffee.html +0 -71
  84. package/docs/lib/psd/image.coffee.html +0 -94
  85. package/docs/lib/psd/image_export.coffee.html +0 -5
  86. package/docs/lib/psd/image_exports/png.coffee.html +0 -19
  87. package/docs/lib/psd/image_format.coffee.html +0 -8
  88. package/docs/lib/psd/image_formats/layer_raw.coffee.html +0 -9
  89. package/docs/lib/psd/image_formats/layer_rle.coffee.html +0 -10
  90. package/docs/lib/psd/image_formats/raw.coffee.html +0 -6
  91. package/docs/lib/psd/image_formats/rle.coffee.html +0 -37
  92. package/docs/lib/psd/image_mode.coffee.html +0 -7
  93. package/docs/lib/psd/image_modes/cmyk.coffee.html +0 -36
  94. package/docs/lib/psd/image_modes/greyscale.coffee.html +0 -17
  95. package/docs/lib/psd/image_modes/rgb.coffee.html +0 -32
  96. package/docs/lib/psd/init.coffee.html +0 -22
  97. package/docs/lib/psd/layer/blend_modes.coffee.html +0 -22
  98. package/docs/lib/psd/layer/blending_ranges.coffee.html +0 -29
  99. package/docs/lib/psd/layer/channel_image.coffee.html +0 -13
  100. package/docs/lib/psd/layer/helpers.coffee.html +0 -19
  101. package/docs/lib/psd/layer/info.coffee.html +0 -69
  102. package/docs/lib/psd/layer/mask.coffee.html +0 -11
  103. package/docs/lib/psd/layer/name.coffee.html +0 -15
  104. package/docs/lib/psd/layer/position_channels.coffee.html +0 -24
  105. package/docs/lib/psd/layer.coffee.html +0 -64
  106. package/docs/lib/psd/layer_info/blend_clipping_elements.coffee.html +0 -11
  107. package/docs/lib/psd/layer_info/blend_interior_elements.coffee.html +0 -11
  108. package/docs/lib/psd/layer_info/fill_opacity.coffee.html +0 -10
  109. package/docs/lib/psd/layer_info/gradient_fill.coffee.html +0 -12
  110. package/docs/lib/psd/layer_info/layer_id.coffee.html +0 -10
  111. package/docs/lib/psd/layer_info/layer_name_source.coffee.html +0 -10
  112. package/docs/lib/psd/layer_info/legacy_typetool.coffee.html +0 -82
  113. package/docs/lib/psd/layer_info/locked.coffee.html +0 -24
  114. package/docs/lib/psd/layer_info/metadata.coffee.html +0 -30
  115. package/docs/lib/psd/layer_info/nested_section_divider.coffee.html +0 -23
  116. package/docs/lib/psd/layer_info/object_effects.coffee.html +0 -12
  117. package/docs/lib/psd/layer_info/section_divider.coffee.html +0 -41
  118. package/docs/lib/psd/layer_info/solid_color.coffee.html +0 -24
  119. package/docs/lib/psd/layer_info/typetool.coffee.html +0 -121
  120. package/docs/lib/psd/layer_info/unicode_name.coffee.html +0 -14
  121. package/docs/lib/psd/layer_info/vector_mask.coffee.html +0 -37
  122. package/docs/lib/psd/layer_info/vector_origination.coffee.html +0 -12
  123. package/docs/lib/psd/layer_info/vector_stroke.coffee.html +0 -12
  124. package/docs/lib/psd/layer_info/vector_stroke_content.coffee.html +0 -12
  125. package/docs/lib/psd/layer_info.coffee.html +0 -11
  126. package/docs/lib/psd/layer_mask.coffee.html +0 -73
  127. package/docs/lib/psd/lazy_execute.coffee.html +0 -80
  128. package/docs/lib/psd/mask.coffee.html +0 -47
  129. package/docs/lib/psd/node.coffee.html +0 -100
  130. package/docs/lib/psd/nodes/ancestry.coffee.html +0 -47
  131. package/docs/lib/psd/nodes/build_preview.coffee.html +0 -6
  132. package/docs/lib/psd/nodes/group.coffee.html +0 -19
  133. package/docs/lib/psd/nodes/layer.coffee.html +0 -17
  134. package/docs/lib/psd/nodes/root.coffee.html +0 -61
  135. package/docs/lib/psd/nodes/search.coffee.html +0 -23
  136. package/docs/lib/psd/path_record.coffee.html +0 -73
  137. package/docs/lib/psd/resource.coffee.html +0 -20
  138. package/docs/lib/psd/resource_section.coffee.html +0 -16
  139. package/docs/lib/psd/resources/layer_comps.coffee.html +0 -31
  140. package/docs/lib/psd/resources.coffee.html +0 -44
  141. package/docs/lib/psd/util.coffee.html +0 -24
  142. package/docs/lib/psd.coffee.html +0 -85
  143. package/index.js +0 -2
  144. package/lib/psd/blend_mode.coffee +0 -78
  145. package/lib/psd/channel_image.coffee +0 -87
  146. package/lib/psd/color.coffee +0 -10
  147. package/lib/psd/descriptor.coffee +0 -189
  148. package/lib/psd/file.coffee +0 -88
  149. package/lib/psd/header.coffee +0 -83
  150. package/lib/psd/image.coffee +0 -107
  151. package/lib/psd/image_export.coffee +0 -2
  152. package/lib/psd/image_exports/png.coffee +0 -28
  153. package/lib/psd/image_format.coffee +0 -5
  154. package/lib/psd/image_formats/layer_raw.coffee +0 -6
  155. package/lib/psd/image_formats/layer_rle.coffee +0 -7
  156. package/lib/psd/image_formats/raw.coffee +0 -3
  157. package/lib/psd/image_formats/rle.coffee +0 -38
  158. package/lib/psd/image_mode.coffee +0 -4
  159. package/lib/psd/image_modes/cmyk.coffee +0 -36
  160. package/lib/psd/image_modes/greyscale.coffee +0 -14
  161. package/lib/psd/image_modes/rgb.coffee +0 -39
  162. package/lib/psd/init.coffee +0 -21
  163. package/lib/psd/layer/blend_modes.coffee +0 -20
  164. package/lib/psd/layer/blending_ranges.coffee +0 -28
  165. package/lib/psd/layer/channel_image.coffee +0 -10
  166. package/lib/psd/layer/helpers.coffee +0 -16
  167. package/lib/psd/layer/info.coffee +0 -76
  168. package/lib/psd/layer/mask.coffee +0 -8
  169. package/lib/psd/layer/name.coffee +0 -12
  170. package/lib/psd/layer/position_channels.coffee +0 -22
  171. package/lib/psd/layer.coffee +0 -64
  172. package/lib/psd/layer_info/artboard.coffee +0 -16
  173. package/lib/psd/layer_info/blend_clipping_elements.coffee +0 -8
  174. package/lib/psd/layer_info/blend_interior_elements.coffee +0 -8
  175. package/lib/psd/layer_info/fill_opacity.coffee +0 -7
  176. package/lib/psd/layer_info/gradient_fill.coffee +0 -9
  177. package/lib/psd/layer_info/layer_id.coffee +0 -7
  178. package/lib/psd/layer_info/layer_name_source.coffee +0 -7
  179. package/lib/psd/layer_info/legacy_typetool.coffee +0 -80
  180. package/lib/psd/layer_info/locked.coffee +0 -21
  181. package/lib/psd/layer_info/metadata.coffee +0 -27
  182. package/lib/psd/layer_info/nested_section_divider.coffee +0 -22
  183. package/lib/psd/layer_info/object_effects.coffee +0 -9
  184. package/lib/psd/layer_info/section_divider.coffee +0 -38
  185. package/lib/psd/layer_info/solid_color.coffee +0 -21
  186. package/lib/psd/layer_info/typetool.coffee +0 -172
  187. package/lib/psd/layer_info/unicode_name.coffee +0 -11
  188. package/lib/psd/layer_info/vector_mask.coffee +0 -35
  189. package/lib/psd/layer_info/vector_origination.coffee +0 -9
  190. package/lib/psd/layer_info/vector_stroke.coffee +0 -9
  191. package/lib/psd/layer_info/vector_stroke_content.coffee +0 -9
  192. package/lib/psd/layer_info.coffee +0 -8
  193. package/lib/psd/layer_mask.coffee +0 -73
  194. package/lib/psd/lazy_execute.coffee +0 -86
  195. package/lib/psd/mask.coffee +0 -52
  196. package/lib/psd/node.coffee +0 -150
  197. package/lib/psd/nodes/ancestry.coffee +0 -44
  198. package/lib/psd/nodes/build_preview.coffee +0 -3
  199. package/lib/psd/nodes/group.coffee +0 -16
  200. package/lib/psd/nodes/layer.coffee +0 -14
  201. package/lib/psd/nodes/root.coffee +0 -59
  202. package/lib/psd/nodes/search.coffee +0 -20
  203. package/lib/psd/path_record.coffee +0 -72
  204. package/lib/psd/resource.coffee +0 -17
  205. package/lib/psd/resource_section.coffee +0 -16
  206. package/lib/psd/resources/guides.coffee +0 -25
  207. package/lib/psd/resources/layer_comps.coffee +0 -28
  208. package/lib/psd/resources/layer_links.coffee +0 -17
  209. package/lib/psd/resources/resolution_info.coffee +0 -26
  210. package/lib/psd/resources.coffee +0 -41
  211. package/lib/psd/util.coffee +0 -23
  212. package/lib/psd.coffee +0 -92
  213. package/output/Logo_Glyph.png +0 -0
  214. package/output/Make a change and save..png +0 -0
  215. package/output/Matte.png +0 -0
  216. package/webpack.config.js +0 -23
@@ -1,189 +0,0 @@
1
- # A descriptor is a block of data that describes a complex data structure of some kind.
2
- # It was added sometime around Photoshop 5.0 and it superceded a few legacy things such
3
- # as layer names and type data. The benefit of the Descriptor is that it is self-contained,
4
- # and allows us to dynamically define data of any size. It's always represented by an Object
5
- # at the root.
6
- module.exports = class Descriptor
7
- # Creates a new Descriptor.
8
- constructor: (@file) ->
9
- # The object that will store the resulting data.
10
- @data = {}
11
-
12
- # Parses the Descriptor at the current location in the file.
13
- parse: ->
14
- @data.class = @parseClass()
15
-
16
- # The descriptor defines the number of items it contains at the root.
17
- numItems = @file.readInt()
18
-
19
- # Each item consists of a key/value combination, which is why our
20
- # descriptor is stored as an object instead of an array at the root.
21
- for i in [0...numItems]
22
- [id, value] = @parseKeyItem()
23
- @data[id] = value
24
-
25
- @data
26
-
27
- # ## Note
28
- # The rest of the methods in this class are considered private. You will never
29
- # call any of them from outside this class.
30
-
31
- # Parses a class representation, which consists of a name and a unique ID.
32
- parseClass: ->
33
- name: @file.readUnicodeString()
34
- id: @parseId()
35
-
36
- # Parses an ID, which is a unique String.
37
- parseId: ->
38
- len = @file.readInt()
39
- if len is 0 then @file.readString(4) else @file.readString(len)
40
-
41
- # Parses a key/item value, which consists of an ID and an Item of any type.
42
- parseKeyItem: ->
43
- id = @parseId()
44
- value = @parseItem()
45
- return [id, value]
46
-
47
- # Parses an Item, which can be one of many types of data, depending on the key.
48
- parseItem: (type = null) ->
49
- type = @file.readString(4) unless type?
50
-
51
- switch type
52
- when 'bool' then @parseBoolean()
53
- when 'type', 'GlbC' then @parseClass()
54
- when 'Objc', 'GlbO' then new Descriptor(@file).parse()
55
- when 'doub' then @parseDouble()
56
- when 'enum' then @parseEnum()
57
- when 'alis' then @parseAlias()
58
- when 'Pth' then @parseFilePath()
59
- when 'long' then @parseInteger()
60
- when 'comp' then @parseLargeInteger()
61
- when 'VlLs' then @parseList()
62
- when 'ObAr' then @parseObjectArray()
63
- when 'tdta' then @parseRawData()
64
- when 'obj ' then @parseReference()
65
- when 'TEXT' then @file.readUnicodeString()
66
- when 'UntF' then @parseUnitDouble()
67
- when 'UnFl' then @parseUnitFloat()
68
-
69
- parseBoolean: -> @file.readBoolean()
70
- parseDouble: -> @file.readDouble()
71
- parseInteger: -> @file.readInt()
72
- parseLargeInteger: -> @file.readLongLong()
73
- parseIdentifier: -> @file.readInt()
74
- parseIndex: -> @file.readInt()
75
- parseOffset: -> @file.readInt()
76
-
77
- # Parses a Property, which consists of a class and a unique ID.
78
- parseProperty: ->
79
- class: @parseClass()
80
- id: @parseId()
81
-
82
- # Parses an enumerator, which consists of 2 IDs, one of which is
83
- # the type, and the other is the value.
84
- parseEnum: ->
85
- type: @parseId()
86
- value: @parseId()
87
-
88
- # Parses an enumerator reference, which consists of a class and
89
- # 2 IDs: a type and value.
90
- parseEnumReference: ->
91
- class: @parseClass()
92
- type: @parseId()
93
- value: @parseId()
94
-
95
- # Parses an Alias, which is a string of arbitrary length.
96
- parseAlias: ->
97
- len = @file.readInt()
98
- @file.readString(len)
99
-
100
- # Parses a file path, which consists of a 4 character signature
101
- # and a path.
102
- parseFilePath: ->
103
- len = @file.readInt()
104
- sig = @file.readString(4)
105
-
106
- # Little endian. Who knows.
107
- pathSize = @file.read('<i')
108
- numChars = @file.read('<i')
109
-
110
- path = @file.readUnicodeString(numChars)
111
-
112
- sig: sig
113
- path: path
114
-
115
- # Parses a list/array of Items.
116
- parseList: ->
117
- count = @file.readInt()
118
- items = []
119
-
120
- for i in [0...count]
121
- items.push @parseItem()
122
-
123
- items
124
-
125
- # Not documented anywhere and unsure of the data format. Luckily, this
126
- # type is extremely rare. In fact, it's so rare, that I've never run into it
127
- # among any of my PSDs.
128
- parseObjectArray: ->
129
- throw "Descriptor object array not implemented yet @ #{@file.tell()}"
130
-
131
- # Parses raw byte data of arbitrary length.
132
- parseRawData: ->
133
- len = @file.readInt()
134
- @file.read(len)
135
-
136
- # Parses a Reference, which is an array of items of multiple types.
137
- parseReference: ->
138
- numItems = @file.readInt()
139
- items = []
140
-
141
- for i in [0...numItems]
142
- type = @file.readString(4)
143
- value = switch type
144
- when 'prop' then @parseProperty()
145
- when 'Clss' then @parseClass()
146
- when 'Enmr' then @parseEnumReference()
147
- when 'Idnt' then @parseIdentifier()
148
- when 'indx' then @parseIndex()
149
- when 'name' then @file.readUnicodeString()
150
- when 'rele' then @parseOffset()
151
-
152
- items.push type: type, value: value
153
-
154
- items
155
-
156
- # Parses a double with a unit, such as angle, percent, pixels, etc.
157
- # Returns an object with an ID, a unit, and a value.
158
- parseUnitDouble: ->
159
- unitId = @file.readString(4)
160
- unit = switch unitId
161
- when '#Ang' then 'Angle'
162
- when '#Rsl' then 'Density'
163
- when '#Rlt' then 'Distance'
164
- when '#Nne' then 'None'
165
- when '#Prc' then 'Percent'
166
- when '#Pxl' then 'Pixels'
167
- when '#Mlm' then 'Millimeters'
168
- when '#Pnt' then 'Points'
169
-
170
- value = @file.readDouble()
171
- id: unitId, unit: unit, value: value
172
-
173
- # Parses a float with a unit, such as angle, percent, pixels, etc.
174
- # Returns an object with an ID, a unit, and a value.
175
- parseUnitFloat: ->
176
- unitId = @file.readString(4)
177
- unit = switch unitId
178
- when '#Ang' then 'Angle'
179
- when '#Rsl' then 'Density'
180
- when '#Rlt' then 'Distance'
181
- when '#Nne' then 'None'
182
- when '#Prc' then 'Percent'
183
- when '#Pxl' then 'Pixels'
184
- when '#Mlm' then 'Millimeters'
185
- when '#Pnt' then 'Points'
186
-
187
- value = @file.readFloat()
188
- id: unitId, unit: unit, value: value
189
-
@@ -1,88 +0,0 @@
1
- {jspack} = require 'jspack'
2
- iconv = require 'iconv-lite'
3
- Color = require './color.coffee'
4
- Util = require './util.coffee'
5
-
6
- # A file abstraction that stores the PSD file data, and
7
- # assists in parsing it.
8
- module.exports = class File
9
- FORMATS =
10
- Int:
11
- code: '>i'
12
- length: 4
13
- UInt:
14
- code: '>I'
15
- length: 4
16
- Short:
17
- code: '>h'
18
- length: 2
19
- UShort:
20
- code: '>H'
21
- length: 2
22
- Float:
23
- code: '>f'
24
- length: 4
25
- Double:
26
- code: '>d'
27
- length: 8
28
- LongLong:
29
- code: '>q'
30
- length: 8
31
-
32
- for own format, info of FORMATS then do (format, info) =>
33
- @::["read#{format}"] = -> @readf(info.code, info.length)[0]
34
-
35
- # The current cursor position in the file.
36
- pos: 0
37
-
38
- # Creates a new File with the given Uint8Array.
39
- constructor: (@data) ->
40
-
41
- # Returns the current cursor position.
42
- tell: -> @pos
43
-
44
- # Reads raw file data with no processing.
45
- read: (length) -> (@data[@pos++] for i in [0...length])
46
-
47
- # Reads file data and processes it with the given unpack format string. If the length is
48
- # omitted, then it will be calculated automatically based on the format string.
49
- readf: (format, len = null) -> jspack.Unpack format, @read(len or jspack.CalcLength(format))
50
-
51
- # Moves the cursor without parsing data. If `rel = false`, then the cursor will be set to the
52
- # given value, which effectively sets the position relative to the start of the file. If
53
- # `rel = true`, then the cursor will be moved relative to the current position.
54
- seek: (amt, rel = false) -> if rel then @pos += amt else @pos = amt
55
-
56
- # Reads a String of the given length.
57
- readString: (length) -> String.fromCharCode.apply(null, @read(length)).replace /\u0000/g, ""
58
-
59
- # Reads a Unicode UTF-16BE encoded string.
60
- readUnicodeString: (length = null) ->
61
- length or= @readInt()
62
- iconv.decode(new Buffer(@read(length * 2)),'utf-16be').replace /\u0000/g, ""
63
-
64
- # Helper that reads a single byte.
65
- readByte: -> @read(1)[0]
66
-
67
- # Helper that reads a single byte and interprets it as a boolean.
68
- readBoolean: -> @readByte() isnt 0
69
-
70
- # Reads a 32-bit color space value.
71
- readSpaceColor: ->
72
- colorSpace = @readShort()
73
- colorComponent = (@readShort() >> 8) for i in [0...4]
74
-
75
- colorSpace: colorSpace, components: colorComponent
76
-
77
- # Adobe's lovely signed 32-bit fixed-point number with 8bits.24bits
78
- # http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577409_17587
79
- readPathNumber: ->
80
- a = @readByte()
81
-
82
- arr = @read(3)
83
- b1 = arr[0] << 16
84
- b2 = arr[1] << 8
85
- b3 = arr[2]
86
- b = b1 | b2 | b3
87
-
88
- parseFloat(a, 10) + parseFloat(b / Math.pow(2, 24), 10)
@@ -1,83 +0,0 @@
1
- {Module} = require 'coffeescript-module'
2
-
3
- # Represents the header of the PSD, which is the first thing always parsed.
4
- # The header stores important information about the PSD such as the dimensions
5
- # and the color depth.
6
- module.exports = class Header extends Module
7
- @aliasProperty 'height', 'rows'
8
- @aliasProperty 'width', 'cols'
9
-
10
- # All of the color modes are stored internally as a short from 0-15.
11
- # This is a mapping of that value to a human-readable name.
12
- MODES = [
13
- 'Bitmap'
14
- 'GrayScale'
15
- 'IndexedColor'
16
- 'RGBColor'
17
- 'CMYKColor'
18
- 'HSLColor'
19
- 'HSBColor'
20
- 'Multichannel'
21
- 'Duotone'
22
- 'LabColor'
23
- 'Gray16'
24
- 'RGB48'
25
- 'Lab48'
26
- 'CMYK64'
27
- 'DeepMultichannel'
28
- 'Duotone16'
29
- ]
30
-
31
- # The signature of the PSD. Should be 8BPS.
32
- sig: null
33
-
34
- # The version of the PSD. Should be 1.
35
- version: null
36
-
37
- # The number of color channels in the PSD.
38
- channels: null
39
-
40
- # The height of the PSD. Can also be accessed with `height`.
41
- rows: null
42
-
43
- # The width of the PSD. Can also be accessed with `width`.
44
- cols: null
45
-
46
- # The bit depth of the PSD.
47
- depth: null
48
-
49
- # The color mode of the PSD.
50
- mode: null
51
-
52
- # Creates a new Header.
53
- # @param [File] file The PSD file.
54
- constructor: (@file) ->
55
-
56
- # Parses the header data.
57
- parse: ->
58
- @sig = @file.readString(4)
59
- if @sig != '8BPS'
60
- throw new Error('Invalid file signature detected. Got: '+@sig+'. Expected 8BPS.')
61
- @version = @file.readUShort()
62
-
63
- @file.seek 6, true
64
-
65
- @channels = @file.readUShort()
66
- @rows = @height = @file.readUInt()
67
- @cols = @width = @file.readUInt()
68
- @depth = @file.readUShort()
69
- @mode = @file.readUShort()
70
-
71
- colorDataLen = @file.readUInt()
72
- @file.seek colorDataLen, true
73
-
74
- # Converts the color mode key to a readable version.
75
- modeName: -> MODES[@mode]
76
-
77
- # Exports all of the header data in a basic object.
78
- export: ->
79
- data = {}
80
- for key in ['sig', 'version', 'channels', 'rows', 'cols', 'depth', 'mode']
81
- data[key] = @[key]
82
-
83
- data
@@ -1,107 +0,0 @@
1
- {Module} = require 'coffeescript-module'
2
-
3
- ImageFormat = require './image_format.coffee'
4
- ImageMode = require './image_mode.coffee'
5
- Export = require './image_export.coffee'
6
-
7
- # Represents the full preview image at the end of the PSD document. For this image, the
8
- # compression is defined for all channels, and there is no mask data. The width and height
9
- # are derived from the PSD header, which is the full size of the PSD document.
10
- module.exports = class Image extends Module
11
- # Here we include all of our important mixins.
12
- @includes ImageFormat.RAW
13
- @includes ImageFormat.RLE
14
- @includes ImageMode.Greyscale
15
- @includes ImageMode.RGB
16
- @includes ImageMode.CMYK
17
- @includes Export.PNG
18
-
19
- # Images can be 1 of 4 different compression types. RLE is the most prevalent, followed by
20
- # RAW. ZIP compression only happens under special circumstances, and is somewhat rare.
21
- COMPRESSIONS = [
22
- 'Raw'
23
- 'RLE'
24
- 'ZIP'
25
- 'ZIPPrediction'
26
- ]
27
-
28
- constructor: (@file, @header) ->
29
- # We can easily calculate the number of pixels with the width and height.
30
- @numPixels = @width() * @height()
31
- @numPixels *= 2 if @depth() is 16
32
-
33
- @calculateLength()
34
-
35
- # The resulting array that stores the pixel data, formatted in RGBA format.
36
- @pixelData = new Uint8Array(@channelLength * 4)
37
- @maskData = new Uint8Array(@maskLength * 4)
38
-
39
- # This temporarily holds the raw channel data after it's been parsed, but not
40
- # processed.
41
- @channelData = new Uint8Array(@length + @maskLength)
42
-
43
- @opacity = 1.0
44
- @hasMask = false
45
-
46
- @startPos = @file.tell()
47
- @endPos = @startPos + @length
48
-
49
- @setChannelsInfo()
50
-
51
- # Some helper methods that grab data from the PSD header.
52
- for attr in ['width', 'height', 'channels', 'depth', 'mode'] then do (attr) =>
53
- @::[attr] = -> @header[attr]
54
-
55
- # Sets the channel info based on the PSD color mode.
56
- setChannelsInfo: ->
57
- switch @mode()
58
- when 1 then @setGreyscaleChannels()
59
- when 3 then @setRgbChannels()
60
- when 4 then @setCmykChannels()
61
-
62
- # Calculates the length of the image data.
63
- calculateLength: ->
64
- @length = switch @depth()
65
- when 1 then (@width() + 7) / 8 * @height()
66
- when 16 then @width() * @height() * 2
67
- else @width() * @height()
68
-
69
- @channelLength = @length
70
- @length *= @channels()
71
-
72
- if @layer and @layer.mask.size
73
- @maskLength = @layer.mask.width * @layer.mask.height
74
- else
75
- @maskLength = 0
76
-
77
- # Parses the image and formats the image data.
78
- parse: ->
79
- @compression = @parseCompression()
80
-
81
- if @compression in [2, 3]
82
- @file.seek @endPos
83
- return
84
-
85
- @parseImageData()
86
-
87
- # Parses the compression mode.
88
- parseCompression: -> @file.readShort()
89
-
90
- # Parses the image data based on the compression mode.
91
- parseImageData: ->
92
- switch @compression
93
- when 0 then @parseRaw()
94
- when 1 then @parseRLE()
95
- when 2, 3 then @parseZip()
96
- else @file.seek(@endPos)
97
-
98
- @processImageData()
99
-
100
- # Processes the parsed image data based on the color mode.
101
- processImageData: ->
102
- switch @mode()
103
- when 1 then @combineGreyscaleChannel()
104
- when 3 then @combineRgbChannel()
105
- when 4 then @combineCmykChannel()
106
-
107
- @channelData = null
@@ -1,2 +0,0 @@
1
- module.exports =
2
- PNG: require('./image_exports/png.coffee')
@@ -1,28 +0,0 @@
1
- fs = require 'fs'
2
- {PNG} = require 'pngjs'
3
- RSVP = require 'rsvp'
4
-
5
- module.exports =
6
- toPng: ->
7
- png = new PNG(filterType: 4, width: @width(), height: @height())
8
- png.data = @pixelData
9
- png
10
-
11
- saveAsPng: (output) ->
12
- new RSVP.Promise (resolve, reject) =>
13
- @toPng()
14
- .pack()
15
- .pipe(fs.createWriteStream(output))
16
- .on 'finish', resolve
17
-
18
- maskToPng: ->
19
- png = new PNG(filterType: 4, width: @layer.mask.width, height: @layer.mask.height)
20
- png.data = @maskData
21
- png
22
-
23
- saveMaskAsPng: (output) ->
24
- new RSVP.Promise (resolve, reject) =>
25
- @maskToPng()
26
- .pack()
27
- .pipe(fs.createWriteStream(output))
28
- .on 'finish', resolve
@@ -1,5 +0,0 @@
1
- module.exports =
2
- RAW: require('./image_formats/raw.coffee')
3
- RLE: require('./image_formats/rle.coffee')
4
- LayerRLE: require('./image_formats/layer_rle.coffee')
5
- LayerRAW: require('./image_formats/layer_raw.coffee')
@@ -1,6 +0,0 @@
1
- module.exports =
2
- parseRaw: ->
3
- for i in [@chanPos...(@chanPos + @chan.length - 2)]
4
- @channelData[i] = @file.readByte()
5
-
6
- @chanPos += (@chan.length - 2)
@@ -1,7 +0,0 @@
1
- module.exports =
2
- parseByteCounts: ->
3
- @file.readShort() for i in [0...@height()]
4
-
5
- parseChannelData: ->
6
- @lineIndex = 0
7
- @decodeRLEChannel()
@@ -1,3 +0,0 @@
1
- module.exports =
2
- parseRaw: ->
3
- @channelData.set @file.read(@length)
@@ -1,38 +0,0 @@
1
- module.exports =
2
- parseRLE: ->
3
- @byteCounts = @parseByteCounts()
4
- @parseChannelData()
5
-
6
- parseByteCounts: ->
7
- @file.readShort() for i in [0...(@channels() * @height())]
8
-
9
- parseChannelData: ->
10
- @chanPos = 0
11
- @lineIndex = 0
12
- for i in [0...@channels()]
13
- @decodeRLEChannel()
14
- @lineIndex += @height()
15
-
16
- decodeRLEChannel: ->
17
- for j in [0...@height()]
18
- byteCount = @byteCounts[@lineIndex + j]
19
- finish = @file.tell() + byteCount
20
-
21
- while @file.tell() < finish
22
- len = @file.read(1)[0]
23
-
24
- if len < 128
25
- len += 1
26
- #@channelData.splice @chanPos, 0, @file.read(len)...
27
- data = @file.read(len)
28
- @channelData.set data, @chanPos
29
- @chanPos += len
30
- else if len > 128
31
- len ^= 0xff
32
- len += 2
33
-
34
- val = @file.read(1)[0]
35
- #@channelData[@chanPos++] = val for i in [0...len]
36
- @channelData.fill(val, @chanPos, @chanPos+len)
37
- @chanPos += len
38
-
@@ -1,4 +0,0 @@
1
- module.exports =
2
- Greyscale: require('./image_modes/greyscale.coffee')
3
- RGB: require('./image_modes/rgb.coffee')
4
- CMYK: require('./image_modes/cmyk.coffee')
@@ -1,36 +0,0 @@
1
- Color = require '../color.coffee'
2
- module.exports =
3
- setCmykChannels: ->
4
- @channelsInfo = [
5
- { id: 0 },
6
- { id: 1 },
7
- { id: 2 },
8
- { id: 3 }
9
- ]
10
-
11
- @channelsInfo.push { id: -1 } if @channels() is 5
12
-
13
- combineCmykChannel: ->
14
- cmykChannels = @channelsInfo
15
- .map (ch) -> ch.id
16
- .filter (ch) -> ch >= -1
17
-
18
- for i in [0...@numPixels]
19
- c = m = y = k = 0
20
- a = 255
21
-
22
- for chan, index in cmykChannels
23
- val = @channelData[i + (@channelLength * index)]
24
-
25
- switch chan
26
- when -1 then a = val
27
- when 0 then c = val
28
- when 1 then m = val
29
- when 2 then y = val
30
- when 3 then k = val
31
-
32
- [r, g, b] = Color.cmykToRgb(255 - c, 255 - m, 255 - y, 255 - k)
33
- @pixelData.set([r, g, b, a], i*4)
34
-
35
- @readMaskData(cmykChannels)
36
-
@@ -1,14 +0,0 @@
1
- module.exports =
2
- setGreyscaleChannels: ->
3
- @channelsInfo = [{id: 0}]
4
- @channelsInfo.push {id: -1} if @channels() is 2
5
-
6
- combineGreyscaleChannel: ->
7
- for i in [0...@numPixels]
8
- grey = @channelData[i]
9
- alpha = if @channels() is 2
10
- @channelData[@channelLength + i]
11
- else
12
- 255
13
-
14
- @pixelData.set([grey, grey, grey, alpha], i*4)
@@ -1,39 +0,0 @@
1
- module.exports =
2
- setRgbChannels: ->
3
- @channelsInfo = [
4
- {id: 0}
5
- {id: 1}
6
- {id: 2}
7
- ]
8
-
9
- @channelsInfo.push {id: -1} if @channels() is 4
10
-
11
- combineRgbChannel: ->
12
- rgbChannels = @channelsInfo
13
- .map (ch) -> ch.id
14
- .filter (ch) -> ch >= -1 # Mask data is -2
15
-
16
- for i in [0...@numPixels]
17
- r = g = b = 0
18
- a = 255
19
-
20
- for chan, index in rgbChannels
21
- val = @channelData[i + (@channelLength * index)]
22
-
23
- switch chan
24
- when -1 then a = val
25
- when 0 then r = val
26
- when 1 then g = val
27
- when 2 then b = val
28
- @pixelData.set([r, g, b, a], i*4)
29
-
30
- @readMaskData(rgbChannels)
31
-
32
- readMaskData: (rgbChannels) ->
33
-
34
- if @hasMask
35
- maskPixels = @layer.mask.width * @layer.mask.height
36
- offset = @channelLength * rgbChannels.length
37
- for i in [0...maskPixels]
38
- val = @channelData[i + offset]
39
- @maskData.set([0, 0, 0, val], i*4)
@@ -1,21 +0,0 @@
1
- fs = require 'fs'
2
- RSVP = require 'rsvp'
3
-
4
- # The init methods for the NodeJS version of PSD.js. When compiled for
5
- # the web, browserify will overwrite this file with a browser-based shim.
6
- module.exports =
7
- extended: (PSD) ->
8
- # Instantiates a new PSD object synchronously from the given
9
- # file path.
10
- @fromFile = (file) -> new PSD(fs.readFileSync(file))
11
-
12
- # Instantiates a new PSD object asynchronously from the given
13
- # file path. This method also parses the PSD for you.
14
- @open = (file) ->
15
- new RSVP.Promise (resolve, reject) ->
16
- fs.readFile file, (err, data) =>
17
- return reject(err) if err
18
-
19
- psd = new PSD(data)
20
- psd.parse()
21
- resolve(psd)