@swc/anyformat-image 0.0.0 → 0.1.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/LICENSE +202 -0
- package/binding.d.ts +244 -0
- package/binding.js +848 -0
- package/index.d.ts +338 -0
- package/index.js +266 -0
- package/package.json +50 -3
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// The public type definitions of @swc/anyformat-image. See index.js.
|
|
2
|
+
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
|
|
5
|
+
// Only the declarations with `export` are part of the API.
|
|
6
|
+
export {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A Buffer that this package returns. It is backed by an ArrayBuffer, never a
|
|
10
|
+
* SharedArrayBuffer, so web APIs such as Blob accept it. With older
|
|
11
|
+
* @types/node versions, this is Buffer.
|
|
12
|
+
*/
|
|
13
|
+
type OwnedBuffer = ReturnType<typeof Buffer.alloc>
|
|
14
|
+
|
|
15
|
+
/** The identifier of an image format, as `detect` returns it. */
|
|
16
|
+
export type ImageFormat =
|
|
17
|
+
| 'png'
|
|
18
|
+
| 'jpeg'
|
|
19
|
+
| 'gif'
|
|
20
|
+
| 'webp'
|
|
21
|
+
| 'avif'
|
|
22
|
+
| 'heic'
|
|
23
|
+
| 'jxl'
|
|
24
|
+
| 'tiff'
|
|
25
|
+
| 'bmp'
|
|
26
|
+
| 'ico'
|
|
27
|
+
| 'qoi'
|
|
28
|
+
|
|
29
|
+
/** The `code` of an error of this package. */
|
|
30
|
+
export type ImageErrorCode =
|
|
31
|
+
| 'ERR_UNSUPPORTED_FORMAT'
|
|
32
|
+
| 'ERR_IMAGE_DECODE'
|
|
33
|
+
| 'ERR_IMAGE_ENCODE'
|
|
34
|
+
| 'ERR_IMAGE_LIMIT'
|
|
35
|
+
| 'ERR_INVALID_IMAGE'
|
|
36
|
+
| 'ERR_IMAGE_SIZE_MISMATCH'
|
|
37
|
+
| 'ERR_INVALID_ARG_VALUE'
|
|
38
|
+
| 'ERR_INVALID_ARG_TYPE'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An error of this package. The functions do not export a class: check
|
|
42
|
+
* `code` to find the kind of error.
|
|
43
|
+
*/
|
|
44
|
+
export interface ImageError extends Error {
|
|
45
|
+
code: ImageErrorCode
|
|
46
|
+
/** The format that the error is about, when it is known. */
|
|
47
|
+
format?: ImageFormat
|
|
48
|
+
/** The input of `compareImages` that caused the error. */
|
|
49
|
+
input?: 'a' | 'b'
|
|
50
|
+
/** ERR_IMAGE_SIZE_MISMATCH: the size of `a`. */
|
|
51
|
+
expected?: { width: number; height: number }
|
|
52
|
+
/** ERR_IMAGE_SIZE_MISMATCH: the size of `b`. */
|
|
53
|
+
actual?: { width: number; height: number }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A format identified by `detect`. */
|
|
57
|
+
export interface DetectedFormat {
|
|
58
|
+
/** The stable lowercase identifier of the format, for example `"png"`. */
|
|
59
|
+
format: ImageFormat
|
|
60
|
+
/** The most common MIME type of the format. */
|
|
61
|
+
mimeType: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Identifies the image format of `data` from its leading bytes.
|
|
66
|
+
*
|
|
67
|
+
* Returns `null` when the format is not recognized.
|
|
68
|
+
*/
|
|
69
|
+
export declare function detect(data: Uint8Array): DetectedFormat | null
|
|
70
|
+
|
|
71
|
+
/** Returns the identifiers of every format that `detect` can return. */
|
|
72
|
+
export declare function supportedFormats(): ImageFormat[]
|
|
73
|
+
|
|
74
|
+
/** The operations that are available for a format. */
|
|
75
|
+
export interface FormatSupport {
|
|
76
|
+
readonly format: ImageFormat
|
|
77
|
+
readonly mimeType: string
|
|
78
|
+
/** `readImageInfo` can read files of the format. */
|
|
79
|
+
readonly readInfo: boolean
|
|
80
|
+
/** `decode` can decode files of the format. */
|
|
81
|
+
readonly decode: boolean
|
|
82
|
+
/** `encode` can encode files of the format. */
|
|
83
|
+
readonly encode: boolean
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The operations that are available for each format that `detect` identifies. */
|
|
87
|
+
export declare const formatSupport: readonly FormatSupport[]
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The layout of the pixels. `'rgba8'` is 8-bit RGBA, non-premultiplied,
|
|
91
|
+
* row-major, with no padding between rows.
|
|
92
|
+
*/
|
|
93
|
+
export type PixelFormat = 'rgba8'
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A decoded image: pixels in memory, not an encoded file. A pngjs `PNG`
|
|
97
|
+
* object and an `ImageData` object match this interface.
|
|
98
|
+
*/
|
|
99
|
+
export interface RawImage<D extends Uint8Array | Uint8ClampedArray = Uint8Array | Uint8ClampedArray> {
|
|
100
|
+
/** An integer from 1 to 2^31 - 1. */
|
|
101
|
+
width: number
|
|
102
|
+
/** An integer from 1 to 2^31 - 1. */
|
|
103
|
+
height: number
|
|
104
|
+
/** Default: 'rgba8'. */
|
|
105
|
+
pixelFormat?: PixelFormat | undefined
|
|
106
|
+
/** `width * height * 4` bytes for 'rgba8'. */
|
|
107
|
+
data: D
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A raw image that a function of this package returns. */
|
|
111
|
+
export interface RawImageBuffer extends RawImage<OwnedBuffer> {
|
|
112
|
+
pixelFormat: PixelFormat
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface ImageInfoBase {
|
|
116
|
+
format: ImageFormat
|
|
117
|
+
mimeType: string
|
|
118
|
+
/** The width that the file stores, before the orientation is applied. */
|
|
119
|
+
width: number
|
|
120
|
+
/** The height that the file stores, before the orientation is applied. */
|
|
121
|
+
height: number
|
|
122
|
+
/** True when the image has an alpha channel or a tRNS chunk. */
|
|
123
|
+
hasAlpha: boolean
|
|
124
|
+
/** The number of frames, pages, or sub-images. 1 for a still image. */
|
|
125
|
+
frameCount: number
|
|
126
|
+
/** The EXIF orientation from 1 to 8. 1 when the file has no orientation. */
|
|
127
|
+
orientation: number
|
|
128
|
+
/** True when the file contains an ICC profile. */
|
|
129
|
+
hasIccProfile: boolean
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type PngColorType = 'gray' | 'gray-alpha' | 'rgb' | 'rgba' | 'indexed'
|
|
133
|
+
|
|
134
|
+
/** The info of a PNG file. */
|
|
135
|
+
export interface PngInfo extends ImageInfoBase {
|
|
136
|
+
format: 'png'
|
|
137
|
+
/** The bit depth of each sample: 1, 2, 4, 8, or 16. */
|
|
138
|
+
bitDepth: number
|
|
139
|
+
colorType: PngColorType
|
|
140
|
+
interlaced: boolean
|
|
141
|
+
/** The value of the gAMA chunk, or null when there is none or its value is 0. */
|
|
142
|
+
gamma: number | null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The info of a format that has no format-specific fields yet. */
|
|
146
|
+
export interface OtherImageInfo extends ImageInfoBase {
|
|
147
|
+
format: Exclude<ImageFormat, 'png'>
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The header and the metadata of an encoded image. `format` selects the type. */
|
|
151
|
+
export type ImageInfo = PngInfo | OtherImageInfo
|
|
152
|
+
|
|
153
|
+
export interface ReadImageInfoOptions {
|
|
154
|
+
/** Accept only these formats. Default: every format that readImageInfo can read. */
|
|
155
|
+
formats?: readonly ImageFormat[] | null | undefined
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Parses the header and the metadata of an encoded image. Does not decompress
|
|
160
|
+
* the pixel data.
|
|
161
|
+
*/
|
|
162
|
+
export declare function readImageInfo(data: Uint8Array, options?: ReadImageInfoOptions | null): Promise<ImageInfo>
|
|
163
|
+
/** The synchronous form of `readImageInfo`. */
|
|
164
|
+
export declare function readImageInfoSync(data: Uint8Array, options?: ReadImageInfoOptions | null): ImageInfo
|
|
165
|
+
|
|
166
|
+
export interface DecodeOptions {
|
|
167
|
+
/**
|
|
168
|
+
* Accept only these formats. A file of another format fails with
|
|
169
|
+
* ERR_UNSUPPORTED_FORMAT. Default: every format that has a decoder.
|
|
170
|
+
*/
|
|
171
|
+
formats?: readonly ImageFormat[] | null | undefined
|
|
172
|
+
/**
|
|
173
|
+
* The maximum value of width * height. A larger image fails with
|
|
174
|
+
* ERR_IMAGE_LIMIT. Default: 268435456 (16384 * 16384).
|
|
175
|
+
*/
|
|
176
|
+
maxPixels?: number | null | undefined
|
|
177
|
+
/**
|
|
178
|
+
* 'apply' rotates and flips the pixels as the EXIF orientation tells, as
|
|
179
|
+
* browsers do. 'ignore' returns the pixels as the file stores them.
|
|
180
|
+
* Default: 'apply'.
|
|
181
|
+
*/
|
|
182
|
+
orientation?: 'apply' | 'ignore' | null | undefined
|
|
183
|
+
/** Default: 'rgba8'. */
|
|
184
|
+
pixelFormat?: PixelFormat | null | undefined
|
|
185
|
+
/** Options for PNG files. */
|
|
186
|
+
png?: {
|
|
187
|
+
/**
|
|
188
|
+
* true: reject a bad CRC in IHDR, PLTE, or IDAT, and ignore an ancillary
|
|
189
|
+
* chunk with a bad CRC. false: use every chunk without a CRC check.
|
|
190
|
+
* Default: true.
|
|
191
|
+
*/
|
|
192
|
+
checkCrc?: boolean | null | undefined
|
|
193
|
+
} | null | undefined
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface DecodedImage extends RawImageBuffer {
|
|
197
|
+
/** The metadata of the file. */
|
|
198
|
+
info: ImageInfo
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Decodes an encoded image. Detects the format with `detect`. */
|
|
202
|
+
export declare function decode(data: Uint8Array, options?: DecodeOptions | null): Promise<DecodedImage>
|
|
203
|
+
/** The synchronous form of `decode`. */
|
|
204
|
+
export declare function decodeSync(data: Uint8Array, options?: DecodeOptions | null): DecodedImage
|
|
205
|
+
|
|
206
|
+
interface EncodeOptionsBase {
|
|
207
|
+
/**
|
|
208
|
+
* The background color for an output without alpha. The encoder blends each
|
|
209
|
+
* pixel with this color. Default: [255, 255, 255].
|
|
210
|
+
*/
|
|
211
|
+
background?: readonly [number, number, number] | null | undefined
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface PngEncodeOptions extends EncodeOptionsBase {
|
|
215
|
+
format: 'png'
|
|
216
|
+
/**
|
|
217
|
+
* Default: 'rgba'. 'auto' reduces the color type and keeps every pixel
|
|
218
|
+
* unchanged; the file is not always smaller. 'rgb', 'gray', and
|
|
219
|
+
* 'gray-alpha' can lose data.
|
|
220
|
+
*/
|
|
221
|
+
colorType?: 'rgba' | 'rgb' | 'gray' | 'gray-alpha' | 'auto' | null | undefined
|
|
222
|
+
/** Default: 'balanced'. */
|
|
223
|
+
compression?: 'none' | 'fastest' | 'fast' | 'balanced' | 'high' | null | undefined
|
|
224
|
+
/** The scanline filter. Default: the filter that suits the compression level. */
|
|
225
|
+
filter?: 'adaptive' | 'none' | 'sub' | 'up' | 'average' | 'paeth' | null | undefined
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Encode options. `format` selects the output format. */
|
|
229
|
+
export type EncodeOptions = PngEncodeOptions
|
|
230
|
+
|
|
231
|
+
/** Encodes a raw image in the format that `options.format` selects. */
|
|
232
|
+
export declare function encode(image: RawImage, options: EncodeOptions): Promise<OwnedBuffer>
|
|
233
|
+
/** The synchronous form of `encode`. */
|
|
234
|
+
export declare function encodeSync(image: RawImage, options: EncodeOptions): OwnedBuffer
|
|
235
|
+
|
|
236
|
+
/** A raw image or an encoded file. */
|
|
237
|
+
export type ImageInput = RawImage | Uint8Array
|
|
238
|
+
|
|
239
|
+
export interface CompareOptions {
|
|
240
|
+
/** The pixelmatch version whose results to reproduce. Default: 'pixelmatch8'. */
|
|
241
|
+
algorithm?: 'pixelmatch8' | 'pixelmatch7' | 'pixelmatch6' | null | undefined
|
|
242
|
+
/** From 0 to 1. Default: 0.1. */
|
|
243
|
+
threshold?: number | null | undefined
|
|
244
|
+
/** Count anti-aliased pixels as differences. Default: false. */
|
|
245
|
+
includeAA?: boolean | null | undefined
|
|
246
|
+
/** The opacity of unchanged pixels in the diff image, from 0 to 1. Default: 0.1. */
|
|
247
|
+
alpha?: number | null | undefined
|
|
248
|
+
/** Default: [255, 255, 0]. */
|
|
249
|
+
aaColor?: readonly [number, number, number] | null | undefined
|
|
250
|
+
/** Default: [255, 0, 0]. */
|
|
251
|
+
diffColor?: readonly [number, number, number] | null | undefined
|
|
252
|
+
/** The color of pixels that are darker in the second image. Default: diffColor. */
|
|
253
|
+
diffColorAlt?: readonly [number, number, number] | null | undefined
|
|
254
|
+
/** Draw the diff on a transparent background. Default: false. */
|
|
255
|
+
diffMask?: boolean | null | undefined
|
|
256
|
+
/** Ignored by 'pixelmatch6'. Default: true. */
|
|
257
|
+
checkerboard?: boolean | null | undefined
|
|
258
|
+
/** Also calculate the largest diff count in any N x N window. Default: null. */
|
|
259
|
+
windowSize?: number | null | undefined
|
|
260
|
+
/** One byte for each pixel. The comparison skips pixels with a non-zero value. */
|
|
261
|
+
ignoreMask?: Uint8Array | Uint8ClampedArray | null | undefined
|
|
262
|
+
/** The decode options for inputs that are encoded files. */
|
|
263
|
+
decode?: DecodeOptions | null | undefined
|
|
264
|
+
/**
|
|
265
|
+
* The diff output. 'raw' returns the diff image in `diffImage`. Encode
|
|
266
|
+
* options return an encoded file in `diffEncoded`. Default: null.
|
|
267
|
+
*/
|
|
268
|
+
diff?: 'raw' | EncodeOptions | null | undefined
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface Rect {
|
|
272
|
+
x: number
|
|
273
|
+
y: number
|
|
274
|
+
width: number
|
|
275
|
+
height: number
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export interface CompareResult {
|
|
279
|
+
width: number
|
|
280
|
+
height: number
|
|
281
|
+
/** The number of different pixels. The return value of pixelmatch without windowSize. */
|
|
282
|
+
diffPixels: number
|
|
283
|
+
/** The number of different pixels that the comparison ignored as anti-aliasing. */
|
|
284
|
+
antialiasedPixels: number
|
|
285
|
+
/**
|
|
286
|
+
* The largest number of different pixels in any windowSize x windowSize
|
|
287
|
+
* window. The return value of pixelmatch with windowSize. null when
|
|
288
|
+
* windowSize is not set.
|
|
289
|
+
*/
|
|
290
|
+
windowDiffPixels: number | null
|
|
291
|
+
/** The smallest rectangle that contains all different pixels, or null. */
|
|
292
|
+
diffBounds: Rect | null
|
|
293
|
+
/** The diff image when `diff` is 'raw'. Otherwise null. */
|
|
294
|
+
diffImage: RawImageBuffer | null
|
|
295
|
+
/** The encoded diff image when `diff` is encode options. Otherwise null. */
|
|
296
|
+
diffEncoded: OwnedBuffer | null
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The result of a comparison with `diff: 'raw'`. */
|
|
300
|
+
export interface CompareResultWithImage extends CompareResult {
|
|
301
|
+
diffImage: RawImageBuffer
|
|
302
|
+
diffEncoded: null
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** The result of a comparison with encode options in `diff`. */
|
|
306
|
+
export interface CompareResultWithEncoded extends CompareResult {
|
|
307
|
+
diffImage: null
|
|
308
|
+
diffEncoded: OwnedBuffer
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Compares two images pixel by pixel with the algorithm of pixelmatch. Each
|
|
313
|
+
* input is a raw image or an encoded file. The function copies the input
|
|
314
|
+
* buffers before it starts, so the caller can change them after the call.
|
|
315
|
+
*/
|
|
316
|
+
export declare function compareImages(
|
|
317
|
+
a: ImageInput,
|
|
318
|
+
b: ImageInput,
|
|
319
|
+
options: CompareOptions & { diff: 'raw' },
|
|
320
|
+
): Promise<CompareResultWithImage>
|
|
321
|
+
export declare function compareImages(
|
|
322
|
+
a: ImageInput,
|
|
323
|
+
b: ImageInput,
|
|
324
|
+
options: CompareOptions & { diff: EncodeOptions },
|
|
325
|
+
): Promise<CompareResultWithEncoded>
|
|
326
|
+
export declare function compareImages(a: ImageInput, b: ImageInput, options?: CompareOptions | null): Promise<CompareResult>
|
|
327
|
+
/** The synchronous form of `compareImages`. */
|
|
328
|
+
export declare function compareImagesSync(
|
|
329
|
+
a: ImageInput,
|
|
330
|
+
b: ImageInput,
|
|
331
|
+
options: CompareOptions & { diff: 'raw' },
|
|
332
|
+
): CompareResultWithImage
|
|
333
|
+
export declare function compareImagesSync(
|
|
334
|
+
a: ImageInput,
|
|
335
|
+
b: ImageInput,
|
|
336
|
+
options: CompareOptions & { diff: EncodeOptions },
|
|
337
|
+
): CompareResultWithEncoded
|
|
338
|
+
export declare function compareImagesSync(a: ImageInput, b: ImageInput, options?: CompareOptions | null): CompareResult
|
package/index.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// The public entry point of @swc/anyformat-image.
|
|
4
|
+
//
|
|
5
|
+
// `napi build` generates `binding.js` and `binding.d.ts`. This file selects the
|
|
6
|
+
// exports of the generated binding that are the public API. Optional feature
|
|
7
|
+
// packages will connect to the public API here.
|
|
8
|
+
|
|
9
|
+
const binding = require('./binding.js')
|
|
10
|
+
|
|
11
|
+
// The codes that napi-rs gives to an argument of a wrong type.
|
|
12
|
+
const TYPE_ERROR_CODES = new Set([
|
|
13
|
+
'InvalidArg',
|
|
14
|
+
'ObjectExpected',
|
|
15
|
+
'StringExpected',
|
|
16
|
+
'NameExpected',
|
|
17
|
+
'FunctionExpected',
|
|
18
|
+
'NumberExpected',
|
|
19
|
+
'BooleanExpected',
|
|
20
|
+
'ArrayExpected',
|
|
21
|
+
'BigintExpected',
|
|
22
|
+
'DateExpected',
|
|
23
|
+
'ArrayBufferExpected',
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
// Reports an argument of a wrong type as Node.js does: a TypeError with the
|
|
27
|
+
// code ERR_INVALID_ARG_TYPE.
|
|
28
|
+
function normalizeError(error) {
|
|
29
|
+
if (error instanceof TypeError && error.code === undefined) {
|
|
30
|
+
error.code = 'ERR_INVALID_ARG_TYPE'
|
|
31
|
+
return error
|
|
32
|
+
}
|
|
33
|
+
if (error instanceof Error && TYPE_ERROR_CODES.has(error.code)) {
|
|
34
|
+
const typeError = new TypeError(error.message)
|
|
35
|
+
typeError.code = 'ERR_INVALID_ARG_TYPE'
|
|
36
|
+
return typeError
|
|
37
|
+
}
|
|
38
|
+
return error
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Replaces the stack frames of an error with the frames of the call.
|
|
42
|
+
function withStack(error, stack) {
|
|
43
|
+
if (error instanceof Error && typeof stack === 'string') {
|
|
44
|
+
const frames = stack.slice(stack.indexOf('\n'))
|
|
45
|
+
error.stack = `${error.name}: ${error.message}${frames}`
|
|
46
|
+
}
|
|
47
|
+
return error
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Describes a value for an error message, as Node.js does.
|
|
51
|
+
function describe(value) {
|
|
52
|
+
if (value === null) return 'null'
|
|
53
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
|
|
54
|
+
return `an instance of ${Object.prototype.toString.call(value).slice(8, -1)}`
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(value)) return 'an array'
|
|
57
|
+
if (typeof value === 'object') return 'an object'
|
|
58
|
+
return `type ${typeof value}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function invalidType(name, expected, value) {
|
|
62
|
+
const error = new TypeError(`The ${name} must be ${expected}. Received ${describe(value)}`)
|
|
63
|
+
error.code = 'ERR_INVALID_ARG_TYPE'
|
|
64
|
+
return error
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function invalidOptions(name, value) {
|
|
68
|
+
return invalidType(name, 'an object', value)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isUint8Array(value) {
|
|
72
|
+
return ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === '[object Uint8Array]'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isPixelData(value) {
|
|
76
|
+
const tag = Object.prototype.toString.call(value)
|
|
77
|
+
return ArrayBuffer.isView(value) && (tag === '[object Uint8Array]' || tag === '[object Uint8ClampedArray]')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Checks the arguments that napi-rs reports with unclear messages.
|
|
81
|
+
function checkFile(name, value) {
|
|
82
|
+
if (!isUint8Array(value)) throw invalidType(`"${name}" argument`, 'a Uint8Array, for example a Buffer', value)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Returns a plain copy of a raw image, so that the native code reads each
|
|
86
|
+
// property once. Throws for a property of a wrong type.
|
|
87
|
+
function normalizeRawImage(name, value) {
|
|
88
|
+
if (value === null || typeof value !== 'object' || ArrayBuffer.isView(value)) {
|
|
89
|
+
throw invalidType(`"${name}" argument`, 'a raw image object', value)
|
|
90
|
+
}
|
|
91
|
+
const { width, height, pixelFormat, data } = value
|
|
92
|
+
if (typeof width !== 'number') throw invalidType(`"${name}.width" property`, 'a number', width)
|
|
93
|
+
if (typeof height !== 'number') throw invalidType(`"${name}.height" property`, 'a number', height)
|
|
94
|
+
if (pixelFormat !== undefined && pixelFormat !== null && typeof pixelFormat !== 'string') {
|
|
95
|
+
throw invalidType(`"${name}.pixelFormat" property`, 'a string', pixelFormat)
|
|
96
|
+
}
|
|
97
|
+
if (!isPixelData(data)) {
|
|
98
|
+
throw invalidType(`"${name}.data" property`, 'a Uint8Array or a Uint8ClampedArray', data)
|
|
99
|
+
}
|
|
100
|
+
return pixelFormat === undefined || pixelFormat === null ? { width, height, data } : { width, height, pixelFormat, data }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeImageInput(name, value) {
|
|
104
|
+
if (isUint8Array(value)) return value
|
|
105
|
+
try {
|
|
106
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value === null || typeof value !== 'object') {
|
|
107
|
+
throw invalidType(`"${name}" argument`, 'a Uint8Array (an encoded file) or a raw image object', value)
|
|
108
|
+
}
|
|
109
|
+
return normalizeRawImage(name, value)
|
|
110
|
+
} catch (error) {
|
|
111
|
+
// Name the input, as the errors of the native code do.
|
|
112
|
+
if (error.code === 'ERR_INVALID_ARG_TYPE') error.input = name
|
|
113
|
+
throw error
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The type of each option. The native code checks the values, and these
|
|
118
|
+
// checks give clear messages for values of a wrong type.
|
|
119
|
+
const STRING = { name: 'a string', test: (value) => typeof value === 'string' }
|
|
120
|
+
const NUMBER = { name: 'a number', test: (value) => typeof value === 'number' }
|
|
121
|
+
const BOOLEAN = { name: 'a boolean', test: (value) => typeof value === 'boolean' }
|
|
122
|
+
const STRINGS = {
|
|
123
|
+
name: 'an array of strings',
|
|
124
|
+
test: (value) => Array.isArray(value) && value.every((item) => typeof item === 'string'),
|
|
125
|
+
}
|
|
126
|
+
const COLOR = {
|
|
127
|
+
name: 'an array of three numbers',
|
|
128
|
+
test: (value) => Array.isArray(value) && Array.from(value).every((item) => typeof item === 'number'),
|
|
129
|
+
}
|
|
130
|
+
const PIXEL_MASK = { name: 'a Uint8Array or a Uint8ClampedArray', test: isPixelData }
|
|
131
|
+
|
|
132
|
+
// The options of each options object. Reading the known names, and not the
|
|
133
|
+
// own keys, also reads values from a prototype and from getters, as
|
|
134
|
+
// pixelmatch does.
|
|
135
|
+
const PNG_DECODE_OPTIONS = { checkCrc: BOOLEAN }
|
|
136
|
+
const DECODE_OPTIONS = {
|
|
137
|
+
formats: STRINGS,
|
|
138
|
+
maxPixels: NUMBER,
|
|
139
|
+
orientation: STRING,
|
|
140
|
+
pixelFormat: STRING,
|
|
141
|
+
png: PNG_DECODE_OPTIONS,
|
|
142
|
+
}
|
|
143
|
+
const READ_INFO_OPTIONS = { formats: STRINGS }
|
|
144
|
+
const ENCODE_OPTIONS = {
|
|
145
|
+
format: STRING,
|
|
146
|
+
colorType: STRING,
|
|
147
|
+
compression: STRING,
|
|
148
|
+
filter: STRING,
|
|
149
|
+
background: COLOR,
|
|
150
|
+
}
|
|
151
|
+
const COMPARE_OPTIONS = {
|
|
152
|
+
algorithm: STRING,
|
|
153
|
+
threshold: NUMBER,
|
|
154
|
+
includeAA: BOOLEAN,
|
|
155
|
+
alpha: NUMBER,
|
|
156
|
+
aaColor: COLOR,
|
|
157
|
+
diffColor: COLOR,
|
|
158
|
+
diffColorAlt: COLOR,
|
|
159
|
+
diffMask: BOOLEAN,
|
|
160
|
+
checkerboard: BOOLEAN,
|
|
161
|
+
windowSize: NUMBER,
|
|
162
|
+
ignoreMask: PIXEL_MASK,
|
|
163
|
+
decode: DECODE_OPTIONS,
|
|
164
|
+
diff: { or: STRING, options: ENCODE_OPTIONS },
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Returns a plain copy of an options object for the native function. A
|
|
168
|
+
// property that is null or undefined is left out, so that it has its default
|
|
169
|
+
// value. `name` names the object in error messages, for example
|
|
170
|
+
// "options.decode".
|
|
171
|
+
function normalizeOptions(options, schema, name) {
|
|
172
|
+
if (options === undefined || options === null) return undefined
|
|
173
|
+
if (typeof options !== 'object' || Array.isArray(options) || ArrayBuffer.isView(options)) {
|
|
174
|
+
throw invalidOptions(name.includes('.') ? `"${name}" property` : `"${name}" argument`, options)
|
|
175
|
+
}
|
|
176
|
+
const copy = {}
|
|
177
|
+
for (const key of Object.keys(schema)) {
|
|
178
|
+
const value = options[key]
|
|
179
|
+
if (value === undefined || value === null) continue
|
|
180
|
+
copy[key] = normalizeOption(value, schema[key], `${name}.${key}`)
|
|
181
|
+
}
|
|
182
|
+
return copy
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function normalizeOption(value, type, name) {
|
|
186
|
+
if (type.or) {
|
|
187
|
+
// `diff`: a string ('raw') or encode options.
|
|
188
|
+
if (type.or.test(value)) return value
|
|
189
|
+
if (typeof value !== 'object') throw invalidType(`"${name}" property`, `${type.or.name} or an object`, value)
|
|
190
|
+
const options = normalizeOptions(value, type.options, name)
|
|
191
|
+
if (options.format === undefined) {
|
|
192
|
+
throw invalidType(`"${name}.format" property`, "a string, for example 'png'", undefined)
|
|
193
|
+
}
|
|
194
|
+
return options
|
|
195
|
+
}
|
|
196
|
+
if (!type.test) return normalizeOptions(value, type, name)
|
|
197
|
+
if (!type.test(value)) throw invalidType(`"${name}" property`, type.name, value)
|
|
198
|
+
return value
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Wraps a native function. `prepare` checks and copies the arguments before
|
|
202
|
+
// the options. `optionsIndex` is the position of the options argument, and
|
|
203
|
+
// `schema` describes it. The options of `encode` are required.
|
|
204
|
+
function wrap(fn, isAsync, prepare, optionsIndex = -1, schema = null, required = false) {
|
|
205
|
+
return function wrapped(...args) {
|
|
206
|
+
// The native code rejects the promise from the event loop, so the error
|
|
207
|
+
// has no stack frames. Record the frames of the call.
|
|
208
|
+
const site = {}
|
|
209
|
+
if (isAsync) Error.captureStackTrace(site, wrapped)
|
|
210
|
+
let result
|
|
211
|
+
try {
|
|
212
|
+
if (prepare) prepare(args)
|
|
213
|
+
if (optionsIndex >= 0) {
|
|
214
|
+
const options = args[optionsIndex]
|
|
215
|
+
if (required && (options === undefined || options === null)) {
|
|
216
|
+
throw invalidOptions('"options" argument', options)
|
|
217
|
+
}
|
|
218
|
+
args[optionsIndex] = normalizeOptions(options, schema, 'options')
|
|
219
|
+
if (required && args[optionsIndex].format === undefined) {
|
|
220
|
+
throw invalidType('"options.format" property', "a string, for example 'png'", undefined)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
// An error of the checks, or an error that a getter of the caller
|
|
225
|
+
// threw. Keep it as it is.
|
|
226
|
+
if (isAsync) return Promise.reject(error)
|
|
227
|
+
throw error
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
result = fn(...args)
|
|
231
|
+
} catch (error) {
|
|
232
|
+
// An asynchronous function always returns a promise. The native
|
|
233
|
+
// functions check their arguments before they start the work.
|
|
234
|
+
if (isAsync) return Promise.reject(normalizeError(error))
|
|
235
|
+
throw normalizeError(error)
|
|
236
|
+
}
|
|
237
|
+
if (!isAsync) return result
|
|
238
|
+
return result.catch((error) => {
|
|
239
|
+
throw withStack(normalizeError(error), site.stack)
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const checkData = (args) => checkFile('data', args[0])
|
|
245
|
+
const checkImage = (args) => {
|
|
246
|
+
args[0] = normalizeRawImage('image', args[0])
|
|
247
|
+
}
|
|
248
|
+
const checkInputs = (args) => {
|
|
249
|
+
args[0] = normalizeImageInput('a', args[0])
|
|
250
|
+
args[1] = normalizeImageInput('b', args[1])
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
module.exports.detect = wrap(binding.detect, false, checkData)
|
|
254
|
+
module.exports.supportedFormats = wrap(binding.supportedFormats, false)
|
|
255
|
+
module.exports.formatSupport = Object.freeze(
|
|
256
|
+
binding.listFormatSupport().map((support) => Object.freeze(support)),
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
module.exports.readImageInfo = wrap(binding.readImageInfo, true, checkData, 1, READ_INFO_OPTIONS)
|
|
260
|
+
module.exports.readImageInfoSync = wrap(binding.readImageInfoSync, false, checkData, 1, READ_INFO_OPTIONS)
|
|
261
|
+
module.exports.decode = wrap(binding.decode, true, checkData, 1, DECODE_OPTIONS)
|
|
262
|
+
module.exports.decodeSync = wrap(binding.decodeSync, false, checkData, 1, DECODE_OPTIONS)
|
|
263
|
+
module.exports.encode = wrap(binding.encode, true, checkImage, 1, ENCODE_OPTIONS, true)
|
|
264
|
+
module.exports.encodeSync = wrap(binding.encodeSync, false, checkImage, 1, ENCODE_OPTIONS, true)
|
|
265
|
+
module.exports.compareImages = wrap(binding.compareImages, true, checkInputs, 2, COMPARE_OPTIONS)
|
|
266
|
+
module.exports.compareImagesSync = wrap(binding.compareImagesSync, false, checkInputs, 2, COMPARE_OPTIONS)
|
package/package.json
CHANGED
|
@@ -1,14 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swc/anyformat-image",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "High-performance image processing for Node.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/swc-project/anyformat.git",
|
|
9
9
|
"directory": "bindings/node/image"
|
|
10
10
|
},
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"types": "index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"binding.js",
|
|
17
|
+
"binding.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"napi": {
|
|
20
|
+
"binaryName": "anyformat-image",
|
|
21
|
+
"targets": [
|
|
22
|
+
"x86_64-apple-darwin",
|
|
23
|
+
"aarch64-apple-darwin",
|
|
24
|
+
"x86_64-pc-windows-msvc",
|
|
25
|
+
"aarch64-pc-windows-msvc",
|
|
26
|
+
"x86_64-unknown-linux-gnu",
|
|
27
|
+
"aarch64-unknown-linux-gnu",
|
|
28
|
+
"x86_64-unknown-linux-musl",
|
|
29
|
+
"aarch64-unknown-linux-musl"
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">= 18"
|
|
34
|
+
},
|
|
11
35
|
"publishConfig": {
|
|
12
36
|
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@napi-rs/cli": "^3.11.1",
|
|
40
|
+
"pixelmatch": "8.0.0",
|
|
41
|
+
"pixelmatch-5": "npm:pixelmatch@5.3.0",
|
|
42
|
+
"pixelmatch-6": "npm:pixelmatch@6.0.0",
|
|
43
|
+
"pixelmatch-7": "npm:pixelmatch@7.2.0",
|
|
44
|
+
"pngjs": "7.0.0"
|
|
45
|
+
},
|
|
46
|
+
"optionalDependencies": {
|
|
47
|
+
"@swc/anyformat-image-darwin-x64": "0.1.1",
|
|
48
|
+
"@swc/anyformat-image-darwin-arm64": "0.1.1",
|
|
49
|
+
"@swc/anyformat-image-win32-x64-msvc": "0.1.1",
|
|
50
|
+
"@swc/anyformat-image-win32-arm64-msvc": "0.1.1",
|
|
51
|
+
"@swc/anyformat-image-linux-x64-gnu": "0.1.1",
|
|
52
|
+
"@swc/anyformat-image-linux-arm64-gnu": "0.1.1",
|
|
53
|
+
"@swc/anyformat-image-linux-x64-musl": "0.1.1",
|
|
54
|
+
"@swc/anyformat-image-linux-arm64-musl": "0.1.1"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "napi build --platform --js binding.js --dts binding.d.ts --release",
|
|
58
|
+
"build:debug": "napi build --platform --js binding.js --dts binding.d.ts",
|
|
59
|
+
"test": "node --test"
|
|
13
60
|
}
|
|
14
|
-
}
|
|
61
|
+
}
|
package/README.md
DELETED