@swc/anyformat-image 0.1.0 → 0.1.2

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/binding.d.ts CHANGED
@@ -9,6 +9,96 @@
9
9
  */
10
10
  export declare const __napiBindingTarget: 'native' | 'wasm32-wasi' | 'wasm32-wasip1'
11
11
 
12
+ /** Compares two images on the thread pool. */
13
+ export declare function compareImages(a: Uint8Array | RawImageInput, b: Uint8Array | RawImageInput, options?: CompareOptions | undefined | null): Promise<CompareResult>
14
+
15
+ /** Compares two images that are raw images or encoded files. */
16
+ export declare function compareImagesSync(a: Uint8Array | RawImageInput, b: Uint8Array | RawImageInput, options?: CompareOptions | undefined | null): CompareResult
17
+
18
+ /** Options of `compareImages`. */
19
+ export interface CompareOptions {
20
+ /** 'pixelmatch8', 'pixelmatch7', or 'pixelmatch6'. */
21
+ algorithm?: string
22
+ /** From 0 to 1. */
23
+ threshold?: number
24
+ /** Count anti-aliased pixels as differences. */
25
+ includeAA?: boolean
26
+ /** The opacity of unchanged pixels in the diff image. */
27
+ alpha?: number
28
+ /** The color of anti-aliased pixels. */
29
+ aaColor?: Array<number>
30
+ /** The color of different pixels. */
31
+ diffColor?: Array<number>
32
+ /** The color of different pixels that are darker in the second image. */
33
+ diffColorAlt?: Array<number> | null
34
+ /** Draw the diff on a transparent background. */
35
+ diffMask?: boolean
36
+ /** Blend semi-transparent pixels with a checkerboard. */
37
+ checkerboard?: boolean
38
+ /** Also calculate the largest diff count in any N x N window. */
39
+ windowSize?: number | null
40
+ /** One byte for each pixel. The comparison skips pixels with a non-zero value. */
41
+ ignoreMask?: Uint8Array | Uint8ClampedArray | null
42
+ /** The decode options for inputs that are encoded files. */
43
+ decode?: DecodeOptions
44
+ /** 'raw' or encode options. */
45
+ diff?: 'raw' | EncodeOptions | null
46
+ }
47
+
48
+ /** The result of `compareImages`. */
49
+ export interface CompareResult {
50
+ /** The width of the images. */
51
+ width: number
52
+ /** The height of the images. */
53
+ height: number
54
+ /** The number of different pixels. */
55
+ diffPixels: number
56
+ /** The number of different pixels that the comparison ignored as anti-aliasing. */
57
+ antialiasedPixels: number
58
+ /** The largest number of different pixels in any window. */
59
+ windowDiffPixels: number | null
60
+ /** The smallest rectangle that contains all different pixels. */
61
+ diffBounds: Rect | null
62
+ /** The diff image when `diff` is 'raw'. */
63
+ diffImage: RawImage | null
64
+ /** The encoded diff image when `diff` is encode options. */
65
+ diffEncoded: Buffer | null
66
+ }
67
+
68
+ /** Decodes an encoded image on the thread pool. */
69
+ export declare function decode(data: Uint8Array, options?: DecodeOptions | undefined | null): Promise<DecodedImage>
70
+
71
+ /** A decoded image and the metadata of its file. */
72
+ export interface DecodedImage {
73
+ /** The width in pixels, after the orientation is applied. */
74
+ width: number
75
+ /** The height in pixels, after the orientation is applied. */
76
+ height: number
77
+ /** The pixel format. */
78
+ pixelFormat: string
79
+ /** The pixel data. */
80
+ data: Buffer
81
+ /** The metadata of the file. */
82
+ info: ImageInfo
83
+ }
84
+
85
+ /** Options of `decode`. */
86
+ export interface DecodeOptions {
87
+ /** Accept only these formats. */
88
+ formats?: Array<string>
89
+ /** The maximum value of width * height. */
90
+ maxPixels?: number
91
+ /** 'apply' or 'ignore'. */
92
+ orientation?: string
93
+ /** The pixel format of the output. */
94
+ pixelFormat?: string
95
+ /** Options for PNG files. */
96
+ png?: PngDecodeOptionsInput
97
+ }
98
+
99
+ /** Decodes an encoded image. */
100
+ export declare function decodeSync(data: Uint8Array, options?: DecodeOptions | undefined | null): DecodedImage
101
+
12
102
  /**
13
103
  * Identifies the image format of `data` from its leading bytes.
14
104
  *
@@ -24,5 +114,131 @@ export interface DetectedFormat {
24
114
  mimeType: string
25
115
  }
26
116
 
117
+ /** Encodes a raw image on the thread pool. */
118
+ export declare function encode(image: RawImageInput, options: EncodeOptions): Promise<Buffer>
119
+
120
+ /** Options of `encode`. `format` selects the output format. */
121
+ export interface EncodeOptions {
122
+ /** The output format. */
123
+ format: string
124
+ /** The background color for an output without alpha. */
125
+ background?: Array<number>
126
+ /** PNG: the color type. */
127
+ colorType?: string
128
+ /** PNG: the compression level. */
129
+ compression?: string
130
+ /** PNG: the scanline filter. */
131
+ filter?: string
132
+ }
133
+
134
+ /** Encodes a raw image in the format that `options.format` selects. */
135
+ export declare function encodeSync(image: RawImageInput, options: EncodeOptions): Buffer
136
+
137
+ /** The operations that are available for a format. */
138
+ export interface FormatSupport {
139
+ /** The format. */
140
+ format: string
141
+ /** The MIME type of the format. */
142
+ mimeType: string
143
+ /** `readImageInfo` can read files of the format. */
144
+ readInfo: boolean
145
+ /** `decode` can decode files of the format. */
146
+ decode: boolean
147
+ /** `encode` can encode files of the format. */
148
+ encode: boolean
149
+ }
150
+
151
+ /** The header and the metadata of an encoded image. */
152
+ export interface ImageInfo {
153
+ /** The format of the file. */
154
+ format: string
155
+ /** The MIME type of the format. */
156
+ mimeType: string
157
+ /** The width that the file stores. */
158
+ width: number
159
+ /** The height that the file stores. */
160
+ height: number
161
+ /** The image has an alpha channel or a tRNS chunk. */
162
+ hasAlpha: boolean
163
+ /** The number of frames, pages, or sub-images. */
164
+ frameCount: number
165
+ /** The EXIF orientation from 1 to 8. */
166
+ orientation: number
167
+ /** The file contains an ICC profile. */
168
+ hasIccProfile: boolean
169
+ /** PNG: the bit depth of each sample. */
170
+ bitDepth: number | null
171
+ /** PNG: the color type. */
172
+ colorType: string | null
173
+ /** PNG: the file uses Adam7 interlacing. */
174
+ interlaced: boolean | null
175
+ /** PNG: the value of the gAMA chunk, or null when there is none or its value is 0. */
176
+ gamma: number | null
177
+ }
178
+
179
+ /**
180
+ * Returns the operations that are available for each format. The public
181
+ * API exposes the result as the `formatSupport` constant.
182
+ */
183
+ export declare function listFormatSupport(): Array<FormatSupport>
184
+
185
+ /** Decode options for PNG files. */
186
+ export interface PngDecodeOptionsInput {
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
193
+ }
194
+
195
+ /** A raw image that a function returns. */
196
+ export interface RawImage {
197
+ /** The width in pixels. */
198
+ width: number
199
+ /** The height in pixels. */
200
+ height: number
201
+ /** The pixel format. */
202
+ pixelFormat: string
203
+ /** The pixel data. */
204
+ data: Buffer
205
+ }
206
+
207
+ /** A raw image from JavaScript: pixels in memory, not an encoded file. */
208
+ export interface RawImageInput {
209
+ /** The width in pixels. */
210
+ width: number
211
+ /** The height in pixels. */
212
+ height: number
213
+ /** The pixel format. Default: 'rgba8'. */
214
+ pixelFormat?: string
215
+ /** The pixel data. */
216
+ data: Uint8Array | Uint8ClampedArray
217
+ }
218
+
219
+ /** Reads the header and the metadata of an encoded image on the thread pool. */
220
+ export declare function readImageInfo(data: Uint8Array, options?: ReadImageInfoOptions | undefined | null): Promise<ImageInfo>
221
+
222
+ /** Options of `readImageInfo`. */
223
+ export interface ReadImageInfoOptions {
224
+ /** Accept only these formats. */
225
+ formats?: Array<string>
226
+ }
227
+
228
+ /** Reads the header and the metadata of an encoded image. */
229
+ export declare function readImageInfoSync(data: Uint8Array, options?: ReadImageInfoOptions | undefined | null): ImageInfo
230
+
231
+ /** A rectangle in pixels. */
232
+ export interface Rect {
233
+ /** The left edge. */
234
+ x: number
235
+ /** The top edge. */
236
+ y: number
237
+ /** The width. */
238
+ width: number
239
+ /** The height. */
240
+ height: number
241
+ }
242
+
27
243
  /** Returns the identifiers of every format that `detect` can return. */
28
244
  export declare function supportedFormats(): Array<string>
package/binding.js CHANGED
@@ -94,8 +94,8 @@ function requireNative() {
94
94
  try {
95
95
  const binding = require('@swc/anyformat-image-android-arm64')
96
96
  const bindingPackageVersion = require('@swc/anyformat-image-android-arm64/package.json').version
97
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
98
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
97
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
98
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
99
99
  }
100
100
  return binding
101
101
  } catch (e) {
@@ -110,8 +110,8 @@ function requireNative() {
110
110
  try {
111
111
  const binding = require('@swc/anyformat-image-android-arm-eabi')
112
112
  const bindingPackageVersion = require('@swc/anyformat-image-android-arm-eabi/package.json').version
113
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
114
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
113
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
114
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
115
115
  }
116
116
  return binding
117
117
  } catch (e) {
@@ -131,8 +131,8 @@ function requireNative() {
131
131
  try {
132
132
  const binding = require('@swc/anyformat-image-win32-x64-gnu')
133
133
  const bindingPackageVersion = require('@swc/anyformat-image-win32-x64-gnu/package.json').version
134
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
135
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
134
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
135
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
136
136
  }
137
137
  return binding
138
138
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('@swc/anyformat-image-win32-x64-msvc')
149
149
  const bindingPackageVersion = require('@swc/anyformat-image-win32-x64-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -164,8 +164,8 @@ function requireNative() {
164
164
  try {
165
165
  const binding = require('@swc/anyformat-image-win32-ia32-msvc')
166
166
  const bindingPackageVersion = require('@swc/anyformat-image-win32-ia32-msvc/package.json').version
167
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
168
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
167
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
168
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
169
169
  }
170
170
  return binding
171
171
  } catch (e) {
@@ -180,8 +180,8 @@ function requireNative() {
180
180
  try {
181
181
  const binding = require('@swc/anyformat-image-win32-arm64-msvc')
182
182
  const bindingPackageVersion = require('@swc/anyformat-image-win32-arm64-msvc/package.json').version
183
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
184
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
183
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
184
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
185
  }
186
186
  return binding
187
187
  } catch (e) {
@@ -199,8 +199,8 @@ function requireNative() {
199
199
  try {
200
200
  const binding = require('@swc/anyformat-image-darwin-universal')
201
201
  const bindingPackageVersion = require('@swc/anyformat-image-darwin-universal/package.json').version
202
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
203
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
202
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
203
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
204
204
  }
205
205
  return binding
206
206
  } catch (e) {
@@ -215,8 +215,8 @@ function requireNative() {
215
215
  try {
216
216
  const binding = require('@swc/anyformat-image-darwin-x64')
217
217
  const bindingPackageVersion = require('@swc/anyformat-image-darwin-x64/package.json').version
218
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
219
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
218
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
219
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
220
220
  }
221
221
  return binding
222
222
  } catch (e) {
@@ -231,8 +231,8 @@ function requireNative() {
231
231
  try {
232
232
  const binding = require('@swc/anyformat-image-darwin-arm64')
233
233
  const bindingPackageVersion = require('@swc/anyformat-image-darwin-arm64/package.json').version
234
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
235
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
234
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
235
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
236
236
  }
237
237
  return binding
238
238
  } catch (e) {
@@ -251,8 +251,8 @@ function requireNative() {
251
251
  try {
252
252
  const binding = require('@swc/anyformat-image-freebsd-x64')
253
253
  const bindingPackageVersion = require('@swc/anyformat-image-freebsd-x64/package.json').version
254
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
255
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
254
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
255
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
256
256
  }
257
257
  return binding
258
258
  } catch (e) {
@@ -267,8 +267,8 @@ function requireNative() {
267
267
  try {
268
268
  const binding = require('@swc/anyformat-image-freebsd-arm64')
269
269
  const bindingPackageVersion = require('@swc/anyformat-image-freebsd-arm64/package.json').version
270
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
271
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
270
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
271
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
272
272
  }
273
273
  return binding
274
274
  } catch (e) {
@@ -283,8 +283,8 @@ function requireNative() {
283
283
  try {
284
284
  const binding = require('@swc/anyformat-image-freebsd-ppc64')
285
285
  const bindingPackageVersion = require('@swc/anyformat-image-freebsd-ppc64/package.json').version
286
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
287
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
286
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
287
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
288
288
  }
289
289
  return binding
290
290
  } catch (e) {
@@ -304,8 +304,8 @@ function requireNative() {
304
304
  try {
305
305
  const binding = require('@swc/anyformat-image-linux-x64-musl')
306
306
  const bindingPackageVersion = require('@swc/anyformat-image-linux-x64-musl/package.json').version
307
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
308
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
307
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
308
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
309
309
  }
310
310
  return binding
311
311
  } catch (e) {
@@ -320,8 +320,8 @@ function requireNative() {
320
320
  try {
321
321
  const binding = require('@swc/anyformat-image-linux-x64-gnu')
322
322
  const bindingPackageVersion = require('@swc/anyformat-image-linux-x64-gnu/package.json').version
323
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
324
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
323
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
324
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
325
325
  }
326
326
  return binding
327
327
  } catch (e) {
@@ -338,8 +338,8 @@ function requireNative() {
338
338
  try {
339
339
  const binding = require('@swc/anyformat-image-linux-arm64-musl')
340
340
  const bindingPackageVersion = require('@swc/anyformat-image-linux-arm64-musl/package.json').version
341
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
342
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
341
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
342
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
343
343
  }
344
344
  return binding
345
345
  } catch (e) {
@@ -354,8 +354,8 @@ function requireNative() {
354
354
  try {
355
355
  const binding = require('@swc/anyformat-image-linux-arm64-gnu')
356
356
  const bindingPackageVersion = require('@swc/anyformat-image-linux-arm64-gnu/package.json').version
357
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
358
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
357
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
358
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
359
359
  }
360
360
  return binding
361
361
  } catch (e) {
@@ -372,8 +372,8 @@ function requireNative() {
372
372
  try {
373
373
  const binding = require('@swc/anyformat-image-linux-arm-musleabihf')
374
374
  const bindingPackageVersion = require('@swc/anyformat-image-linux-arm-musleabihf/package.json').version
375
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
376
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
375
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
376
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
377
377
  }
378
378
  return binding
379
379
  } catch (e) {
@@ -388,8 +388,8 @@ function requireNative() {
388
388
  try {
389
389
  const binding = require('@swc/anyformat-image-linux-arm-gnueabihf')
390
390
  const bindingPackageVersion = require('@swc/anyformat-image-linux-arm-gnueabihf/package.json').version
391
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
392
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
391
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
392
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
393
393
  }
394
394
  return binding
395
395
  } catch (e) {
@@ -406,8 +406,8 @@ function requireNative() {
406
406
  try {
407
407
  const binding = require('@swc/anyformat-image-linux-loong64-musl')
408
408
  const bindingPackageVersion = require('@swc/anyformat-image-linux-loong64-musl/package.json').version
409
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
410
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
409
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
410
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
411
411
  }
412
412
  return binding
413
413
  } catch (e) {
@@ -422,8 +422,8 @@ function requireNative() {
422
422
  try {
423
423
  const binding = require('@swc/anyformat-image-linux-loong64-gnu')
424
424
  const bindingPackageVersion = require('@swc/anyformat-image-linux-loong64-gnu/package.json').version
425
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
426
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
425
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
426
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
427
427
  }
428
428
  return binding
429
429
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('@swc/anyformat-image-linux-riscv64-musl')
442
442
  const bindingPackageVersion = require('@swc/anyformat-image-linux-riscv64-musl/package.json').version
443
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('@swc/anyformat-image-linux-riscv64-gnu')
458
458
  const bindingPackageVersion = require('@swc/anyformat-image-linux-riscv64-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -473,8 +473,8 @@ function requireNative() {
473
473
  try {
474
474
  const binding = require('@swc/anyformat-image-linux-ppc64-gnu')
475
475
  const bindingPackageVersion = require('@swc/anyformat-image-linux-ppc64-gnu/package.json').version
476
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
477
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
476
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
477
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
478
478
  }
479
479
  return binding
480
480
  } catch (e) {
@@ -489,8 +489,8 @@ function requireNative() {
489
489
  try {
490
490
  const binding = require('@swc/anyformat-image-linux-s390x-gnu')
491
491
  const bindingPackageVersion = require('@swc/anyformat-image-linux-s390x-gnu/package.json').version
492
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
493
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
492
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
493
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
494
494
  }
495
495
  return binding
496
496
  } catch (e) {
@@ -509,8 +509,8 @@ function requireNative() {
509
509
  try {
510
510
  const binding = require('@swc/anyformat-image-openharmony-arm64')
511
511
  const bindingPackageVersion = require('@swc/anyformat-image-openharmony-arm64/package.json').version
512
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
513
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
512
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
513
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
514
514
  }
515
515
  return binding
516
516
  } catch (e) {
@@ -525,8 +525,8 @@ function requireNative() {
525
525
  try {
526
526
  const binding = require('@swc/anyformat-image-openharmony-x64')
527
527
  const bindingPackageVersion = require('@swc/anyformat-image-openharmony-x64/package.json').version
528
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
529
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
528
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
529
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
530
530
  }
531
531
  return binding
532
532
  } catch (e) {
@@ -541,8 +541,8 @@ function requireNative() {
541
541
  try {
542
542
  const binding = require('@swc/anyformat-image-openharmony-arm')
543
543
  const bindingPackageVersion = require('@swc/anyformat-image-openharmony-arm/package.json').version
544
- if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
545
- throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
544
+ if (bindingPackageVersion !== '0.1.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
545
+ throw new Error(`Native binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
546
546
  }
547
547
  return binding
548
548
  } catch (e) {
@@ -683,8 +683,8 @@ if (!nativeBinding || forceWasi) {
683
683
  if (!candidateFailed) {
684
684
  if (process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
685
685
  const bindingPackageVersion = require('@swc/anyformat-image-wasm32-wasi/package.json').version
686
- if (bindingPackageVersion !== '0.1.0') {
687
- throw new Error(`WASI binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
686
+ if (bindingPackageVersion !== '0.1.2') {
687
+ throw new Error(`WASI binding package version mismatch, expected 0.1.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
688
688
  }
689
689
  }
690
690
  wasiBinding = require('@swc/anyformat-image-wasm32-wasi')
@@ -835,5 +835,14 @@ function __napiStampBindingTarget(exportsObject, target) {
835
835
  // linked import resolves to `undefined`.
836
836
  module.exports.__napiBindingTarget = __napiStampBindingTarget(nativeBinding, __napiLoadedBindingTarget)
837
837
  module.exports = nativeBinding
838
+ module.exports.compareImages = nativeBinding.compareImages
839
+ module.exports.compareImagesSync = nativeBinding.compareImagesSync
840
+ module.exports.decode = nativeBinding.decode
841
+ module.exports.decodeSync = nativeBinding.decodeSync
838
842
  module.exports.detect = nativeBinding.detect
843
+ module.exports.encode = nativeBinding.encode
844
+ module.exports.encodeSync = nativeBinding.encodeSync
845
+ module.exports.listFormatSupport = nativeBinding.listFormatSupport
846
+ module.exports.readImageInfo = nativeBinding.readImageInfo
847
+ module.exports.readImageInfoSync = nativeBinding.readImageInfoSync
839
848
  module.exports.supportedFormats = nativeBinding.supportedFormats
package/index.d.ts CHANGED
@@ -1,4 +1,338 @@
1
1
  // The public type definitions of @swc/anyformat-image. See index.js.
2
2
 
3
- export { detect, supportedFormats } from './binding.js'
4
- export type { DetectedFormat } from './binding.js'
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 CHANGED
@@ -8,5 +8,259 @@
8
8
 
9
9
  const binding = require('./binding.js')
10
10
 
11
- module.exports.detect = binding.detect
12
- module.exports.supportedFormats = binding.supportedFormats
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@swc/anyformat-image",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "High-performance image processing for Node.js",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -23,10 +23,15 @@
23
23
  "aarch64-apple-darwin",
24
24
  "x86_64-pc-windows-msvc",
25
25
  "aarch64-pc-windows-msvc",
26
+ "i686-pc-windows-msvc",
26
27
  "x86_64-unknown-linux-gnu",
27
28
  "aarch64-unknown-linux-gnu",
28
29
  "x86_64-unknown-linux-musl",
29
- "aarch64-unknown-linux-musl"
30
+ "aarch64-unknown-linux-musl",
31
+ "armv7-unknown-linux-gnueabihf",
32
+ "aarch64-linux-android",
33
+ "armv7-linux-androideabi",
34
+ "x86_64-unknown-freebsd"
30
35
  ]
31
36
  },
32
37
  "engines": {
@@ -36,17 +41,27 @@
36
41
  "access": "public"
37
42
  },
38
43
  "devDependencies": {
39
- "@napi-rs/cli": "^3.11.1"
44
+ "@napi-rs/cli": "^3.11.1",
45
+ "pixelmatch": "8.0.0",
46
+ "pixelmatch-5": "npm:pixelmatch@5.3.0",
47
+ "pixelmatch-6": "npm:pixelmatch@6.0.0",
48
+ "pixelmatch-7": "npm:pixelmatch@7.2.0",
49
+ "pngjs": "7.0.0"
40
50
  },
41
51
  "optionalDependencies": {
42
- "@swc/anyformat-image-darwin-x64": "0.1.0",
43
- "@swc/anyformat-image-darwin-arm64": "0.1.0",
44
- "@swc/anyformat-image-win32-x64-msvc": "0.1.0",
45
- "@swc/anyformat-image-win32-arm64-msvc": "0.1.0",
46
- "@swc/anyformat-image-linux-x64-gnu": "0.1.0",
47
- "@swc/anyformat-image-linux-arm64-gnu": "0.1.0",
48
- "@swc/anyformat-image-linux-x64-musl": "0.1.0",
49
- "@swc/anyformat-image-linux-arm64-musl": "0.1.0"
52
+ "@swc/anyformat-image-darwin-x64": "0.1.2",
53
+ "@swc/anyformat-image-darwin-arm64": "0.1.2",
54
+ "@swc/anyformat-image-win32-x64-msvc": "0.1.2",
55
+ "@swc/anyformat-image-win32-arm64-msvc": "0.1.2",
56
+ "@swc/anyformat-image-win32-ia32-msvc": "0.1.2",
57
+ "@swc/anyformat-image-linux-x64-gnu": "0.1.2",
58
+ "@swc/anyformat-image-linux-arm64-gnu": "0.1.2",
59
+ "@swc/anyformat-image-linux-x64-musl": "0.1.2",
60
+ "@swc/anyformat-image-linux-arm64-musl": "0.1.2",
61
+ "@swc/anyformat-image-linux-arm-gnueabihf": "0.1.2",
62
+ "@swc/anyformat-image-android-arm64": "0.1.2",
63
+ "@swc/anyformat-image-android-arm-eabi": "0.1.2",
64
+ "@swc/anyformat-image-freebsd-x64": "0.1.2"
50
65
  },
51
66
  "scripts": {
52
67
  "build": "napi build --platform --js binding.js --dts binding.d.ts --release",