ag-psd 15.0.4 → 15.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.
package/src/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { Psd, ReadOptions, WriteOptions } from './psd';
2
+ import { PsdWriter, writePsd as writePsdInternal, getWriterBuffer, createWriter, getWriterBufferNoCopy } from './psdWriter';
3
+ import { PsdReader, readPsd as readPsdInternal, createReader } from './psdReader';
4
+ export * from './abr';
5
+ export * from './csh';
6
+ export { initializeCanvas } from './helpers';
7
+ export * from './psd';
8
+ import { fromByteArray } from 'base64-js';
9
+ export { PsdReader, PsdWriter };
10
+
11
+ interface BufferLike {
12
+ buffer: ArrayBuffer;
13
+ byteOffset: number;
14
+ byteLength: number;
15
+ }
16
+
17
+ export const byteArrayToBase64 = fromByteArray;
18
+
19
+ export function readPsd(buffer: ArrayBuffer | BufferLike, options?: ReadOptions): Psd {
20
+ const reader = 'buffer' in buffer ?
21
+ createReader(buffer.buffer, buffer.byteOffset, buffer.byteLength) :
22
+ createReader(buffer);
23
+ return readPsdInternal(reader, options);
24
+ }
25
+
26
+ export function writePsd(psd: Psd, options?: WriteOptions): ArrayBuffer {
27
+ const writer = createWriter();
28
+ writePsdInternal(writer, psd, options);
29
+ return getWriterBuffer(writer);
30
+ }
31
+
32
+ export function writePsdUint8Array(psd: Psd, options?: WriteOptions): Uint8Array {
33
+ const writer = createWriter();
34
+ writePsdInternal(writer, psd, options);
35
+ return getWriterBufferNoCopy(writer);
36
+ }
37
+
38
+ export function writePsdBuffer(psd: Psd, options?: WriteOptions): Buffer {
39
+ if (typeof Buffer === 'undefined') {
40
+ throw new Error('Buffer not supported on this platform');
41
+ }
42
+
43
+ return Buffer.from(writePsdUint8Array(psd, options));
44
+ }
@@ -0,0 +1,25 @@
1
+ import { createCanvas } from 'canvas';
2
+ import { initializeCanvas } from './index';
3
+ import { decodeJpeg } from './jpeg';
4
+
5
+ function createCanvasFromData(data: Uint8Array) {
6
+ const canvas = createCanvas(100, 100);
7
+
8
+ try {
9
+ const context = canvas.getContext('2d')!;
10
+ const imageData = decodeJpeg(data, (w, h) => context.createImageData(w, h));
11
+ canvas.width = imageData.width;
12
+ canvas.height = imageData.height;
13
+ context.putImageData(imageData, 0, 0);
14
+ } catch (e: any) {
15
+ console.error('JPEG decompression error', e.message);
16
+ }
17
+
18
+ return canvas;
19
+ }
20
+
21
+ initializeCanvas(createCanvas, createCanvasFromData);
22
+
23
+ export function initialize() {
24
+ initializeCanvas(createCanvas, createCanvasFromData);
25
+ }