ag-psd 16.0.0 → 17.0.1

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.
package/src/psdReader.ts CHANGED
@@ -67,6 +67,11 @@ export function readUint16(reader: PsdReader) {
67
67
  return reader.view.getUint16(reader.offset - 2, false);
68
68
  }
69
69
 
70
+ export function readUint16LE(reader: PsdReader) {
71
+ reader.offset += 2;
72
+ return reader.view.getUint16(reader.offset - 2, true);
73
+ }
74
+
70
75
  export function readInt32(reader: PsdReader) {
71
76
  reader.offset += 4;
72
77
  return reader.view.getInt32(reader.offset - 4, false);
@@ -153,6 +158,20 @@ export function readUnicodeStringWithLength(reader: PsdReader, length: number) {
153
158
  return text;
154
159
  }
155
160
 
161
+ export function readUnicodeStringWithLengthLE(reader: PsdReader, length: number) {
162
+ let text = '';
163
+
164
+ while (length--) {
165
+ const value = readUint16LE(reader);
166
+
167
+ if (value || length > 0) { // remove trailing \0
168
+ text += String.fromCharCode(value);
169
+ }
170
+ }
171
+
172
+ return text;
173
+ }
174
+
156
175
  export function readAsciiString(reader: PsdReader, length: number) {
157
176
  let text = '';
158
177
 
@@ -379,15 +398,15 @@ function readLayerRecord(reader: PsdReader, psd: Psd, options: ReadOptionsExt) {
379
398
  const channels: ChannelInfo[] = [];
380
399
 
381
400
  for (let i = 0; i < channelCount; i++) {
382
- let channelID = readInt16(reader) as ChannelID;
383
- let channelLength = readUint32(reader);
401
+ let id = readInt16(reader) as ChannelID;
402
+ let length = readUint32(reader);
384
403
 
385
404
  if (options.large) {
386
- if (channelLength !== 0) throw new Error('Sizes larger than 4GB are not supported');
387
- channelLength = readUint32(reader);
405
+ if (length !== 0) throw new Error('Sizes larger than 4GB are not supported');
406
+ length = readUint32(reader);
388
407
  }
389
408
 
390
- channels.push({ id: channelID, length: channelLength });
409
+ channels.push({ id, length });
391
410
  }
392
411
 
393
412
  checkSignature(reader, '8BIM');
@@ -401,11 +420,11 @@ function readLayerRecord(reader: PsdReader, psd: Psd, options: ReadOptionsExt) {
401
420
  const flags = readUint8(reader);
402
421
  layer.transparencyProtected = (flags & 0x01) !== 0;
403
422
  layer.hidden = (flags & 0x02) !== 0;
423
+ if (flags & 0x20) layer.effectsOpen = true;
404
424
  // 0x04 - obsolete
405
425
  // 0x08 - 1 for Photoshop 5.0 and later, tells if bit 4 has useful information
406
426
  // 0x10 - pixel data irrelevant to appearance of document
407
- // 0x20 - ???
408
- // if (flags & 0x20) (layer as any)._2 = true; // TEMP !!!!
427
+ // 0x20 - effects/filters panel is expanded
409
428
 
410
429
  skipBytes(reader, 1);
411
430
 
package/src/psdWriter.ts CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  offsetForChannel, createImageData, fromBlendMode, ChannelID, Compression, clamp,
5
5
  LayerMaskFlags, MaskParams, ColorSpace, Bounds, largeAdditionalInfoKeys, RAW_IMAGE_DATA, writeDataZipWithoutPrediction
6
6
  } from './helpers';
7
- import { ExtendedWriteOptions, hasMultiEffects, infoHandlers } from './additionalInfo';
7
+ import { ExtendedWriteOptions, infoHandlers } from './additionalInfo';
8
8
  import { resourceHandlers } from './imageResources';
9
9
 
10
10
  export interface PsdWriter {
@@ -43,11 +43,21 @@ export function writeUint16(writer: PsdWriter, value: number) {
43
43
  writer.view.setUint16(offset, value, false);
44
44
  }
45
45
 
46
+ export function writeUint16LE(writer: PsdWriter, value: number) {
47
+ const offset = addSize(writer, 2);
48
+ writer.view.setUint16(offset, value, true);
49
+ }
50
+
46
51
  export function writeInt32(writer: PsdWriter, value: number) {
47
52
  const offset = addSize(writer, 4);
48
53
  writer.view.setInt32(offset, value, false);
49
54
  }
50
55
 
56
+ export function writeInt32LE(writer: PsdWriter, value: number) {
57
+ const offset = addSize(writer, 4);
58
+ writer.view.setInt32(offset, value, true);
59
+ }
60
+
51
61
  export function writeUint32(writer: PsdWriter, value: number) {
52
62
  const offset = addSize(writer, 4);
53
63
  writer.view.setUint32(offset, value, false);
@@ -102,6 +112,7 @@ export function writePascalString(writer: PsdWriter, text: string, padTo: number
102
112
 
103
113
  for (let i = 0; i < length; i++) {
104
114
  const code = text.charCodeAt(i);
115
+ // writeUint8(writer, code); // for testing
105
116
  writeUint8(writer, code < 128 ? code : '?'.charCodeAt(0));
106
117
  }
107
118
 
@@ -110,14 +121,23 @@ export function writePascalString(writer: PsdWriter, text: string, padTo: number
110
121
  }
111
122
  }
112
123
 
113
- export function writeUnicodeString(writer: PsdWriter, text: string) {
114
- writeUint32(writer, text.length);
115
-
124
+ export function writeUnicodeStringWithoutLength(writer: PsdWriter, text: string) {
116
125
  for (let i = 0; i < text.length; i++) {
117
126
  writeUint16(writer, text.charCodeAt(i));
118
127
  }
119
128
  }
120
129
 
130
+ export function writeUnicodeStringWithoutLengthLE(writer: PsdWriter, text: string) {
131
+ for (let i = 0; i < text.length; i++) {
132
+ writeUint16LE(writer, text.charCodeAt(i));
133
+ }
134
+ }
135
+
136
+ export function writeUnicodeString(writer: PsdWriter, text: string) {
137
+ writeUint32(writer, text.length);
138
+ writeUnicodeStringWithoutLength(writer, text);
139
+ }
140
+
121
141
  export function writeUnicodeStringWithPadding(writer: PsdWriter, text: string) {
122
142
  writeUint32(writer, text.length + 1);
123
143
 
@@ -304,10 +324,7 @@ function writeLayerInfo(tempBuffer: Uint8Array, writer: PsdWriter, psd: Psd, glo
304
324
  if (layer.vectorMask || (layer.sectionDivider && layer.sectionDivider.type !== SectionDividerType.Other)) {
305
325
  flags |= 0x10; // pixel data irrelevant to appearance of document
306
326
  }
307
- if (layer.effects && hasMultiEffects(layer.effects)) { // TODO: this is not correct
308
- flags |= 0x20; // just guessing this one, might be completely incorrect
309
- }
310
- // if ('_2' in layer) flags |= 0x20; // TEMP!!!
327
+ if (layer.effectsOpen) flags |= 0x20;
311
328
 
312
329
  writeUint8(writer, flags);
313
330
  writeUint8(writer, 0); // filler