format-png 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,371 +1,5 @@
1
- export type ColorType = "grayscale" | "rgb" | "indexed" | "grayscale-alpha" | "rgba";
2
- export interface PngHeader {
3
- width: number;
4
- height: number;
5
- bitDepth: number;
6
- colorType: ColorType;
7
- interlaced: boolean;
8
- }
9
- export type RenderingIntent = "perceptual" | "relative-colorimetric" | "saturation" | "absolute-colorimetric";
10
- /** A CIE 1931 chromaticity, for example `{ x: 0.3127, y: 0.329 }`. */
11
- export interface Chromaticity {
12
- x: number;
13
- y: number;
14
- }
15
- /** `cHRM`: the chromaticities of the white point and the primaries. */
16
- export interface Chromaticities {
17
- white: Chromaticity;
18
- red: Chromaticity;
19
- green: Chromaticity;
20
- blue: Chromaticity;
21
- }
22
- /**
23
- * `pHYs`: pixels per unit. With unit "meter" this is the intended pixel size;
24
- * with "unknown" only the ratio of `x` to `y` means anything.
25
- */
26
- export interface PhysicalDimensions {
27
- x: number;
28
- y: number;
29
- unit: "meter" | "unknown";
30
- }
31
- /** `tIME`: when the image was last modified, in UTC. `month` is 1 to 12. */
32
- export interface PngTime {
33
- year: number;
34
- month: number;
35
- day: number;
36
- hour: number;
37
- minute: number;
38
- second: number;
39
- }
40
- /** A `tEXt`, `zTXt` or `iTXt` chunk, decoded to a string whatever its encoding. */
41
- export interface PngText {
42
- /** For example "Title", "Author" or "Comment". */
43
- keyword: string;
44
- text: string;
45
- /** `iTXt` only, for example "en" or "nb-NO"; otherwise empty. */
46
- languageTag: string;
47
- /** `iTXt` only: the keyword in that language; otherwise empty. */
48
- translatedKeyword: string;
49
- chunkType: "tEXt" | "zTXt" | "iTXt";
50
- /** Whether the text was zlib-compressed in the file. */
51
- compressed: boolean;
52
- }
53
- /** `iCCP`: an embedded ICC color profile. */
54
- export interface IccProfile {
55
- /** Only meaningful to people, for example "ICC Profile". 1 to 79 characters of printable Latin-1. */
56
- name: string;
57
- /** The ICC profile, decompressed. Pass it to a color management library to apply it. */
58
- profile: Uint8Array;
59
- }
60
- /**
61
- * `cICP`: the color space as ITU-T H.273 code points, as video uses. sRGB is
62
- * primaries 1 and transfer 13; Display P3 is 12 and 13; HDR PQ is 9 and 16.
63
- * Takes precedence over every other color chunk.
64
- */
65
- export interface Cicp {
66
- colorPrimaries: number;
67
- transferFunction: number;
68
- /** Always 0 (RGB) in PNG. */
69
- matrixCoefficients: number;
70
- /** Whether samples use the full range, as almost all PNGs do, rather than video's narrow range. */
71
- fullRange: boolean;
72
- }
73
- /** `eXIf`: Exif metadata, such as the camera and orientation, kept raw. */
74
- export interface PngExif {
75
- /** Starts with a TIFF header; pass it to an Exif library to read the tags. */
76
- data: Uint8Array;
77
- byteOrder: "big-endian" | "little-endian";
78
- }
79
- /**
80
- * The known ancillary chunks, parsed. A field is absent if the image doesn't
81
- * have that chunk, or if it was invalid or misplaced and `strictAncillary` is off.
82
- */
83
- export interface PngMetadata {
84
- /** `gAMA`, for example 0.45455 (1/2.2). */
85
- gamma?: number;
86
- chromaticities?: Chromaticities;
87
- /** `sRGB`: the image is sRGB, with this rendering intent. */
88
- srgb?: RenderingIntent;
89
- physicalDimensions?: PhysicalDimensions;
90
- time?: PngTime;
91
- /** `tEXt`, `zTXt` and `iTXt`, in file order. A keyword may repeat. */
92
- text: PngText[];
93
- iccProfile?: IccProfile;
94
- cicp?: Cicp;
95
- exif?: PngExif;
96
- }
97
- export type ChunkPosition = "before-palette" | "before-image-data" | "after-image-data";
98
- /** A raw ancillary chunk, including private and unknown ones. */
99
- export interface PngChunk {
100
- /** The four-letter type, for example "tEXt". */
101
- type: string;
102
- /** The chunk's data, without length, type and CRC. */
103
- data: Uint8Array;
104
- /** Where the chunk was relative to `PLTE` and the image data. */
105
- position: ChunkPosition;
106
- }
107
- /** One chunk as it is in the file, from `readChunks`. */
108
- export interface PngRawChunk {
109
- /** The four-letter type, for example "IHDR" or "tEXt". */
110
- type: string;
111
- /** Where the chunk starts in the input: its length field. */
112
- offset: number;
113
- /** The chunk's data, without length, type and CRC. A view into the input, not a copy. */
114
- data: Uint8Array;
115
- /** The CRC stored in the file. */
116
- crc: number;
117
- /** Whether format-png parses this chunk type. */
118
- known: boolean;
119
- /** Uppercase first letter: needed to display the image. */
120
- critical: boolean;
121
- /** Uppercase second letter: defined or registered by the PNG spec, not private. */
122
- public: boolean;
123
- /** Lowercase fourth letter: editors may copy it even after changing the image. */
124
- safeToCopy: boolean;
125
- }
126
- /**
127
- * `tRNS`: one fully transparent gray value or RGB color, in the image's bit
128
- * depth, or an alpha per palette entry (entries past the end are opaque).
129
- */
130
- export type PngTransparency = {
131
- kind: "gray";
132
- value: number;
133
- } | {
134
- kind: "rgb";
135
- value: [number, number, number];
136
- } | {
137
- kind: "palette";
138
- alpha: Uint8Array;
139
- };
140
- /** Every chunk of a PNG, read and parsed without decompressing the image data. */
141
- export interface PngChunks {
142
- header: PngHeader;
143
- /** `PLTE`, as `[r, g, b]` entries in index order. Indexed images always have one. */
144
- palette?: [number, number, number][];
145
- transparency?: PngTransparency;
146
- /** Always collected, whatever `preserveMetadata` says. */
147
- metadata: PngMetadata;
148
- /** Every chunk from `IHDR` to `IEND`, in file order, including unknown ones. */
149
- chunks: PngRawChunk[];
150
- }
151
- /** Options for `readChunks`. */
152
- export interface ReadChunksOptions {
153
- /** Check each chunk's CRC. Default true. */
154
- validateCrc?: boolean;
155
- /** Throw for an invalid, misplaced or repeated metadata chunk instead of skipping it. Default false. */
156
- strictAncillary?: boolean;
157
- }
158
- export interface DecodeOptions {
159
- /** Check each chunk's CRC. Default true. */
160
- validateCrc?: boolean;
161
- /** Parse known ancillary chunks into `metadata`. Default false. */
162
- preserveMetadata?: boolean;
163
- /** Keep a raw copy of every ancillary chunk in `chunks`. Default false. */
164
- preserveChunks?: boolean;
165
- /** Throw for an invalid, misplaced or repeated metadata chunk instead of skipping it. Default false. */
166
- strictAncillary?: boolean;
167
- }
168
- /** 8-bit RGBA pixels, not premultiplied, rows packed with no padding. */
169
- export interface RgbaImage {
170
- width: number;
171
- height: number;
172
- data: Uint8ClampedArray;
173
- /** Empty unless decoded with `preserveMetadata`. */
174
- metadata: PngMetadata;
175
- /** In file order. Empty unless decoded with `preserveChunks`. */
176
- chunks: PngChunk[];
177
- }
178
- /**
179
- * Text to write as a `tEXt`, `zTXt` or `iTXt` chunk. A `PngText` from decoding
180
- * works as it is.
181
- */
182
- export interface PngTextInput {
183
- /** 1 to 79 characters of printable Latin-1, for example "Title" or "Author". */
184
- keyword: string;
185
- /** Latin-1 only for `tEXt` and `zTXt`; any Unicode for `iTXt`. */
186
- text: string;
187
- /** Default "tEXt". `zTXt` is always compressed. */
188
- chunkType?: PngText["chunkType"];
189
- /** `iTXt` only: whether to zlib-compress the text. Default false. */
190
- compressed?: boolean;
191
- /** `iTXt` only, for example "en" or "nb-NO". */
192
- languageTag?: string;
193
- /** `iTXt` only: the keyword in that language. */
194
- translatedKeyword?: string;
195
- }
196
- /** The metadata chunks to write. A `PngMetadata` from decoding works as it is. */
197
- export interface PngMetadataInput {
198
- gamma?: number;
199
- chromaticities?: Chromaticities;
200
- srgb?: RenderingIntent;
201
- physicalDimensions?: PhysicalDimensions;
202
- time?: PngTime;
203
- /** Written in order. */
204
- text?: PngTextInput[];
205
- /** The profile is given uncompressed; the encoder compresses it. */
206
- iccProfile?: IccProfile;
207
- cicp?: Cicp;
208
- /** Its TIFF header is checked; `byteOrder` is read from it, so it can be left out. */
209
- exif?: {
210
- data: Uint8Array;
211
- byteOrder?: PngExif["byteOrder"];
212
- };
213
- }
214
- /**
215
- * An image to encode, in the PNG's own pixel format: rows top to bottom with
216
- * each padded to a whole byte, 16-bit samples big-endian, pixels under 8 bits
217
- * packed most significant bits first, and palette indices for indexed images.
218
- */
219
- export interface PngImage {
220
- header: PngHeader;
221
- data: Uint8Array;
222
- /** `PLTE`, as `[r, g, b]` entries. Indexed images need one; grayscale images must not have one. */
223
- palette?: [number, number, number][];
224
- /** `tRNS`. Images with an alpha channel must not have one. */
225
- transparency?: PngTransparency;
226
- metadata?: PngMetadataInput;
227
- /** Raw ancillary chunks, written at their positions; see `EncodeOptions.keepUnsafeChunks`. */
228
- chunks?: PngChunk[];
229
- }
230
- /**
231
- * An image decoded in the file's own format by `decode`, without conversion:
232
- * 16-bit samples stay 16-bit (big-endian bytes, as in the file), so passing it
233
- * to `encode` writes the same pixels back.
234
- */
235
- export interface RawImage extends PngImage {
236
- /** Empty unless decoded with `preserveMetadata`. */
237
- metadata: PngMetadata;
238
- /** In file order. Empty unless decoded with `preserveChunks`. */
239
- chunks: PngChunk[];
240
- }
241
- /** 8-bit RGBA pixels to encode. An `RgbaImage` from decoding, or an `ImageData`, works as it is. */
242
- export interface RgbaImageInput {
243
- width: number;
244
- height: number;
245
- /** `width * height * 4` bytes, rows packed with no padding. */
246
- data: Uint8Array | Uint8ClampedArray;
247
- metadata?: PngMetadataInput;
248
- chunks?: PngChunk[];
249
- /** Write Adam7 interlaced. Default false. */
250
- interlaced?: boolean;
251
- }
252
- export type FilterType = "none" | "sub" | "up" | "average" | "paeth";
253
- export interface EncodeOptions {
254
- /** 0 (none) to 9 (smallest). Default 6. */
255
- compression?: number;
256
- /**
257
- * The kind of DEFLATE blocks. "dynamic" (the default) gives the smallest
258
- * files, "fixed" is a little faster, "stored" doesn't compress.
259
- */
260
- compressionStrategy?: "dynamic" | "fixed" | "stored";
261
- /**
262
- * How each row is filtered before compressing. "adaptive" (the default)
263
- * picks a filter per row; the others use that filter for every row.
264
- */
265
- filter?: "adaptive" | FilterType;
266
- /**
267
- * Also write raw chunks that aren't safe to copy, such as `bKGD`, whose data
268
- * depends on the pixels. Default false. Turn it on when re-encoding an image
269
- * with its pixels, header and palette unchanged.
270
- */
271
- keepUnsafeChunks?: boolean;
272
- /**
273
- * "auto" writes 8-bit RGB and RGBA images with at most 256 colors, counting
274
- * alpha, as indexed color at the smallest bit depth that fits. Lossless, and
275
- * often several times smaller for logos, icons and screenshots; unsafe-to-copy
276
- * chunks are then dropped, as they describe the old color type. Default "keep".
277
- */
278
- palette?: PaletteMode;
279
- /** Which ancillary chunks to leave out to make the file smaller. Default "keep". */
280
- strip?: StripChunks;
281
- }
282
- export type PaletteMode = "keep" | "auto";
283
- /**
284
- * Which ancillary chunks the encoder leaves out. `tRNS` and an indexed image's
285
- * palette are always kept.
286
- *
287
- * - "keep": write everything given.
288
- * - "safe": keep what changes how the image looks: `cICP`, `iCCP`, `sRGB`,
289
- * `gAMA`, `cHRM` and `pHYs`. Drop text, `tIME`, `eXIf`, raw chunks and an
290
- * RGB image's suggested palette. Exif orientation is lost with it.
291
- * - "all": keep nothing optional, for the smallest file. Colors may look
292
- * different in color-managed viewers such as browsers.
293
- */
294
- export type StripChunks = "keep" | "safe" | "all";
295
- /** Thrown for malformed PNGs; `message` says what's wrong. */
296
- export declare class PngError extends Error {
297
- name: string;
298
- }
299
- /**
300
- * Loads the WebAssembly module. Call it once before anything else; later calls
301
- * return the same promise.
302
- *
303
- * With no argument it uses the copy embedded in this package, in browsers,
304
- * bundlers and Node alike: nothing is fetched. You can instead pass a
305
- * `WebAssembly.Module` compiled from it, for example one a page compiled once
306
- * and posted to its workers.
307
- */
308
- export declare function init(source?: BufferSource | WebAssembly.Module | Response | Promise<Response>): Promise<void>;
309
- /**
310
- * Decodes a PNG in its own format, without conversion: every bit depth and
311
- * color type as stored, with its palette and transparency. Pass the result to
312
- * `encode` to re-encode it losslessly. Use `decodeRgba8` to display it.
313
- */
314
- export declare function decode(bytes: Uint8Array, options?: DecodeOptions): RawImage;
315
- /** Decodes a PNG to 8-bit RGBA. Pass options to also read metadata or raw chunks. */
316
- export declare function decodeRgba8(bytes: Uint8Array, options?: DecodeOptions): RgbaImage;
317
- /**
318
- * Reads and parses every chunk without decompressing the image data, which is
319
- * much faster than decoding. Known chunks are parsed; every chunk, including
320
- * private and unknown ones, is also returned raw in file order.
321
- */
322
- export declare function readChunks(bytes: Uint8Array, options?: ReadChunksOptions): PngChunks;
323
- /** Parses the data of a `tEXt`, `zTXt` or `iTXt` chunk on its own, for example one from `readChunks`. */
324
- export declare function parseText(type: PngText["chunkType"], data: Uint8Array): PngText;
325
- /** Pixels per inch from `pHYs`, or undefined if its unit isn't meters. */
326
- export declare function pixelsPerInch(dimensions: PhysicalDimensions): {
327
- x: number;
328
- y: number;
329
- } | undefined;
330
- /** Reads the image header without decoding pixels. */
331
- export declare function readHeader(bytes: Uint8Array): PngHeader;
332
- /**
333
- * Encodes an image in any PNG pixel format, with its palette, transparency,
334
- * metadata and raw chunks.
335
- */
336
- export declare function encode(image: PngImage, options?: EncodeOptions): Uint8Array;
337
- /**
338
- * Encodes 8-bit RGBA pixels: the reverse of `decodeRgba8`. Pass a decoded
339
- * `RgbaImage` to re-encode it with its metadata and chunks, or the `ImageData`
340
- * of a canvas.
341
- */
342
- export declare function encodeRgba8(image: RgbaImageInput, options?: EncodeOptions): Uint8Array;
343
- /** Wraps an image for `CanvasRenderingContext2D.putImageData`. Browser only. */
344
- export declare function toImageData(image: RgbaImage): ImageData;
345
- /**
346
- * A decoder for many images: it keeps its buffers in wasm memory between calls.
347
- * Call `free()` when you're done with it.
348
- */
349
- export declare class PngDecoder {
350
- #private;
351
- constructor(options?: DecodeOptions);
352
- decodeRgba8(bytes: Uint8Array): RgbaImage;
353
- /** Like the `decode` function. */
354
- decode(bytes: Uint8Array): RawImage;
355
- /** Like the `readChunks` function. Only the `validateCrc` and `strictAncillary` options apply. */
356
- readChunks(bytes: Uint8Array): PngChunks;
357
- free(): void;
358
- }
359
- /**
360
- * An encoder for many images: it keeps its compressor and buffers in wasm
361
- * memory between calls. Call `free()` when you're done with it.
362
- */
363
- export declare class PngEncoder {
364
- #private;
365
- constructor(options?: EncodeOptions);
366
- /** Like the `encode` function, with this encoder's options. */
367
- encode(image: PngImage): Uint8Array;
368
- /** Like the `encodeRgba8` function, with this encoder's options. */
369
- encodeRgba8(image: RgbaImageInput): Uint8Array;
370
- free(): void;
371
- }
1
+ export type * from "./core.js";
2
+ export { PngDecoder, PngEncoder, PngError, decode, decodeRgba8, encode, encodeRgba8, init, parseText, pixelsPerInch, readChunks, readHeader, toImageData, } from "./core.js";
3
+ export { createWorkerPool, WorkerPoolError } from "./pool.js";
4
+ export type { JobOptions, NodeWorkerLike, WorkerPool, WorkerPoolOptions } from "./pool.js";
5
+ export { decodeAsync, decodeRgba8Async, defaultWorkerPool, encodeAsync, encodeRgba8Async, parseTextAsync, readChunksAsync, readHeaderAsync, terminateDefaultWorkerPool, } from "./async.js";