@@ -7,242 +7,24 @@ See [Examples](../../example.mjs) for usage.
77[ ![ install size] ( https://packagephobia.com/badge?p=@napi-rs/image )] ( https://packagephobia.com/result?p=@napi-rs/image )
88[ ![ Downloads] ( https://img.shields.io/npm/dm/@napi-rs/image.svg?sanitize=true )] ( https://npmcharts.com/compare/@napi-rs/image?minimal=true )
99
10- ## Encode:
11-
12- This library support the following formats conversation:
13-
14- - png => webp
15- - jpeg => webp
16- - png => avif
17- - jpeg => avif
18-
19- ### Webp
20-
21- #### Lossless encode
22-
23- ``` js
24- import { losslessEncodeWebp } from ' @napi-rs/image'
25-
26- /**
27- * @param {Buffer} `jpeg` or `png` buffer, throw error if mimetype mismatch
28- * @return {Buffer} Encoded lossless `webp` buffer
29- */
30- losslessEncodeWebp (fileBuffer)
31- ```
32-
33- #### Lossy encode
34-
35- ``` js
36- import { encodeWebp } from ' @napi-rs/image'
37-
38- /**
39- * The quality factor `quality_factor` ranges from 0 to 100 and controls the loss and quality during compression.
40- * The value 0 corresponds to low quality and small output sizes, whereas 100 is the highest quality and largest output size.
41- * https://developers.google.com/speed/webp/docs/api#simple_encoding_api
42- *
43- * @param {Buffer} `jpeg` or `png` buffer, throw error if mimetype mismatch
44- * @return {Buffer} encoded lossy `webp` buffer
45- */
46- encodeWebp (fileBuffer, qualityFactor)
47- ```
48-
49- ### AVIF
50-
51- ``` js
52- import { encodeAvif } from ' @napi-rs/image'
53-
54- /**
55- * @param {Buffer} `jpeg` or `png` buffer, throw error if mimetype mismatch
56- * @param {AvifConfig | undefined} optional `AVIF` encode config
57- * @return {Buffer} encoded `AVIF` buffer
58- */
59- encodeAvif (fileBuffer, options)
60- ```
61-
62- ` AVIF ` encode config:
63-
64- ``` ts
65- export interface AvifConfig {
66- /** 0-100 scale */
67- quality? : number | undefined | null
68- /** 0-100 scale */
69- alphaQuality? : number | undefined | null
70- /** rav1e preset 1 (slow) 10 (fast but crappy) */
71- speed? : number | undefined | null
72- /** True if RGBA input has already been premultiplied. It inserts appropriate metadata. */
73- premultipliedAlpha? : boolean | undefined | null
74- /** Which pixel format to use in AVIF file. RGB tends to give larger files. */
75- colorSpace? : ColorSpace | undefined | null
76- /** How many threads should be used (0 = match core count) */
77- threads? : number | undefined | null
78- }
79- export const enum ColorSpace {
80- YCbCr = 0 ,
81- RGB = 1 ,
82- }
83- ```
84-
85- ## Image optimize
86-
87- ### PNG
88-
89- #### Lossless
90-
91- ``` js
92- import { losslessCompressPng } from ' @napi-rs/image'
93-
94- /**
95- * @param {Buffer} un -optimized `png` buffer as input
96- * @param {PNGLosslessOptions | undefined} optional optimize options
97- * @returns {Buffer} optimized `png` buffer
98- */
99- losslessCompressPng (pngBuffer, options)
100- ```
101-
102- Optimize options:
103-
104- ``` ts
105- export interface PNGLosslessOptions {
106- /**
107- * Attempt to fix errors when decoding the input file rather than returning an Err.
108- * Default: `false`
109- */
110- fixErrors? : boolean | undefined | null
111- /**
112- * Write to output even if there was no improvement in compression.
113- * Default: `false`
114- */
115- force? : boolean | undefined | null
116- /** Which filters to try on the file (0-5) */
117- filter? : Array <number > | undefined | null
118- /**
119- * Whether to attempt bit depth reduction
120- * Default: `true`
121- */
122- bitDepthReduction? : boolean | undefined | null
123- /**
124- * Whether to attempt color type reduction
125- * Default: `true`
126- */
127- colorTypeReduction? : boolean | undefined | null
128- /**
129- * Whether to attempt palette reduction
130- * Default: `true`
131- */
132- paletteReduction? : boolean | undefined | null
133- /**
134- * Whether to attempt grayscale reduction
135- * Default: `true`
136- */
137- grayscaleReduction? : boolean | undefined | null
138- /**
139- * Whether to perform IDAT recoding
140- * If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
141- * Default: `true`
142- */
143- idatRecoding? : boolean | undefined | null
144- /** Whether to remove ***All non-critical headers*** on PNG */
145- strip? : boolean | undefined | null
146- /** Whether to use heuristics to pick the best filter and compression */
147- useHeuristics? : boolean | undefined | null
148- }
149- ```
150-
151- #### Lossy
152-
153- ``` js
154- import { pngQuantize } from ' @napi-rs/image'
155-
156- /**
157- * @param {Buffer} un -optimized `png` buffer
158- * @param {PNGQuantizeOptions | undefined} optional optimize options
159- * @returns {Buffer} optimized `png` buffer
160- */
161- pngQuantize (pngBuffer, options)
162- ```
163-
164- PNG quantize options:
165-
166- ``` ts
167- export interface PngQuantOptions {
168- /** default is 70 */
169- minQuality? : number | undefined | null
170- /** default is 99 */
171- maxQuality? : number | undefined | null
172- /**
173- * 1- 10
174- * Faster speeds generate images of lower quality, but may be useful for real-time generation of images.
175- * default: 5
176- */
177- speed? : number | undefined | null
178- /**
179- * Number of least significant bits to ignore.
180- * Useful for generating palettes for VGA, 15-bit textures, or other retro platforms.
181- */
182- posterization? : number | undefined | null
183- }
184- ```
185-
186- ### JPEG
187-
188- #### Lossless
189-
190- ``` js
191- import { compressJpeg } from ' @napi-rs/image'
192-
193- /**
194- * @param {Buffer} un -optimized `jpeg` buffer
195- * @param {JpegLosslessOptions | undefined} optional optimize options
196- * @returns {Buffer} optimized `jpeg` buffer
197- */
198- compressJpeg (buffer, options)
199- ```
200-
201- ` JPEG ` lossless optimize options:
202-
203- ``` ts
204- export interface PNGLosslessOptions {
205- /**
206- * Attempt to fix errors when decoding the input file rather than returning an Err.
207- * Default: `false`
208- */
209- fixErrors? : boolean | undefined | null
210- /**
211- * Write to output even if there was no improvement in compression.
212- * Default: `false`
213- */
214- force? : boolean | undefined | null
215- /** Which filters to try on the file (0-5) */
216- filter? : Array <number > | undefined | null
217- /**
218- * Whether to attempt bit depth reduction
219- * Default: `true`
220- */
221- bitDepthReduction? : boolean | undefined | null
222- /**
223- * Whether to attempt color type reduction
224- * Default: `true`
225- */
226- colorTypeReduction? : boolean | undefined | null
227- /**
228- * Whether to attempt palette reduction
229- * Default: `true`
230- */
231- paletteReduction? : boolean | undefined | null
232- /**
233- * Whether to attempt grayscale reduction
234- * Default: `true`
235- */
236- grayscaleReduction? : boolean | undefined | null
237- /**
238- * Whether to perform IDAT recoding
239- * If any type of reduction is performed, IDAT recoding will be performed regardless of this setting
240- * Default: `true`
241- */
242- idatRecoding? : boolean | undefined | null
243- /** Whether to remove ***All non-critical headers*** on PNG */
244- strip? : boolean | undefined | null
245- /** Whether to use heuristics to pick the best filter and compression */
246- useHeuristics? : boolean | undefined | null
247- }
248- ```
10+ ## Transformer:
11+
12+ This library support encode/decode these formats:
13+
14+ | Format | Input | Output |
15+ | --------- | ----------------------------------------- | --------------------------------------- |
16+ | RawPixels | RGBA 8 bits pixels | |
17+ | JPEG | Baseline and progressive | Baseline JPEG |
18+ | PNG | All supported color types | Same as decoding |
19+ | BMP | ✅ | Rgb8, Rgba8, Gray8, GrayA8 |
20+ | ICO | ✅ | ✅ |
21+ | TIFF | Baseline(no fax support) + LZW + PackBits | Rgb8, Rgba8, Gray8 |
22+ | WebP | No | ✅ |
23+ | AVIF | No | ✅ |
24+ | PNM | PBM, PGM, PPM, standard PAM | ✅ |
25+ | DDS | DXT1, DXT3, DXT5 | No |
26+ | TGA | ✅ | Rgb8, Rgba8, Bgr8, Bgra8, Gray8, GrayA8 |
27+ | OpenEXR | Rgb32F, Rgba32F (no dwa compression) | Rgb32F, Rgba32F (no dwa compression) |
28+ | farbfeld | ✅ | ✅ |
29+
30+ See [ index.d.ts] ( ./index.d.ts ) for API reference.
0 commit comments