@jimp/js-ico 0.0.0-stage → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Oliver Moran
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,63 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
+ <img width="140" alt=""
3
+ src="https://raw.githubusercontent.com/jimp-dev/jimp/main/.github/assets/jimpy.png">
4
+ <h1>@jimp/js-ico</h1>
5
+ <p>Default Jimp ico encoder/decoder.</p>
6
+ </div>
2
7
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
8
+ Reads ICO and CUR files, and writes ICO files (`image/x-icon`, also available as `image/vnd.microsoft.icon`). It's one of the default formats of `jimp`.
9
+
10
+ ## Reading
11
+
12
+ An ICO file holds several images, usually the same icon at different sizes. Each image is a PNG, or a BMP with a 1 bit transparency mask. Jimp decodes the largest image, and of images the same size, the one with the most bits per pixel.
13
+
14
+ Pick another image by its position in the file with `index`, or by `size`: the smallest image whose longest side is at least that big, or the largest image if they're all smaller.
15
+
16
+ ```ts
17
+ import { Jimp } from "jimp";
18
+
19
+ const largest = await Jimp.read("favicon.ico");
20
+ const small = await Jimp.read("favicon.ico", { "image/x-icon": { size: 16 } });
21
+ const first = await Jimp.read("favicon.ico", { "image/x-icon": { index: 0 } });
22
+ ```
23
+
24
+ `readIcoEntries` lists the images, to read all of them:
25
+
26
+ ```ts
27
+ import { Jimp } from "jimp";
28
+ import { readIcoEntries } from "@jimp/js-ico";
29
+ import { readFile } from "node:fs/promises";
30
+
31
+ const buffer = await readFile("favicon.ico");
32
+
33
+ // [{ index: 0, width: 16, height: 16, bitDepth: 32, format: "bmp" }, ...]
34
+ const entries = readIcoEntries(buffer);
35
+ const images = await Promise.all(
36
+ entries.map(({ index }) => Jimp.read(buffer, { "image/x-icon": { index } })),
37
+ );
38
+ ```
39
+
40
+ ## Writing
41
+
42
+ Jimp writes the image as a PNG inside the ICO. Images larger than 256x256, the largest size an ICO can describe, are scaled down to fit. Pass `sizes` to write an image for each size instead, each scaled to fit in a square of that size:
43
+
44
+ ```ts
45
+ import { Jimp } from "jimp";
46
+
47
+ const image = await Jimp.read("logo.png");
48
+
49
+ await image.write("favicon.ico", { sizes: [16, 32, 48, 256] });
50
+ const buffer = await image.getBuffer("image/x-icon");
51
+ ```
52
+
53
+ The aspect ratio is kept, so make the image square first (e.g. with `cover` or `contain`) to get square icons.
54
+
55
+ ## Using it with a custom Jimp
56
+
57
+ ```ts
58
+ import { createJimp } from "@jimp/core";
59
+ import png from "@jimp/js-png";
60
+ import ico from "@jimp/js-ico";
61
+
62
+ const Jimp = createJimp({ formats: [png, ico] });
63
+ ```
@@ -0,0 +1,71 @@
1
+ import { Buffer } from "buffer";
2
+ import { Bitmap } from "@jimp/types";
3
+ /** An image in an ICO (or CUR) file */
4
+ export interface IcoEntry {
5
+ /** Position in the file, which the `index` decode option takes */
6
+ index: number;
7
+ width: number;
8
+ height: number;
9
+ /** Bits per pixel */
10
+ bitDepth: number;
11
+ /** Whether the image is stored as a PNG or a BMP */
12
+ format: "png" | "bmp";
13
+ }
14
+ export interface DecodeIcoOptions {
15
+ /**
16
+ * Decode the image at this position in the file. `readIcoEntries` lists
17
+ * the images.
18
+ */
19
+ index?: number;
20
+ /**
21
+ * Decode the image whose longest side best fits this size: the smallest
22
+ * image at least this big, or the largest image if they're all smaller.
23
+ * Of images the same size, the one with the most bits per pixel is used.
24
+ */
25
+ size?: number;
26
+ }
27
+ export interface EncodeIcoOptions {
28
+ /**
29
+ * Write an image for each size, scaled to fit in a square of that size,
30
+ * e.g. `[16, 32, 48, 256]`. Sizes are clamped to 1-256.
31
+ * By default the ICO holds just the image, scaled down to fit in 256x256
32
+ * if it's larger.
33
+ */
34
+ sizes?: number[];
35
+ }
36
+ /**
37
+ * List the images in an ICO or CUR file, to pick one to decode with the
38
+ * `index` decode option.
39
+ * @example
40
+ * ```ts
41
+ * import { Jimp } from "jimp";
42
+ * import { readIcoEntries } from "@jimp/js-ico";
43
+ *
44
+ * const icons = await Promise.all(
45
+ * readIcoEntries(buffer).map(({ index }) =>
46
+ * Jimp.read(buffer, { "image/x-icon": { index } })
47
+ * )
48
+ * );
49
+ * ```
50
+ */
51
+ export declare function readIcoEntries(data: Buffer | Uint8Array): IcoEntry[];
52
+ declare function decode(data: Buffer, options?: DecodeIcoOptions): Bitmap;
53
+ declare function encode(bitmap: Bitmap, { sizes }?: EncodeIcoOptions): Buffer<ArrayBuffer>;
54
+ /** The same format for the `image/vnd.microsoft.icon` MIME type */
55
+ export declare function msIcon(): {
56
+ mime: "image/vnd.microsoft.icon";
57
+ hasAlpha: true;
58
+ encode: typeof encode;
59
+ decode: typeof decode;
60
+ };
61
+ /**
62
+ * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.
63
+ */
64
+ export default function ico(): {
65
+ mime: "image/x-icon";
66
+ hasAlpha: true;
67
+ encode: typeof encode;
68
+ decode: typeof decode;
69
+ };
70
+ export {};
71
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,OAAO,EAAE,MAAM,EAAqB,MAAM,aAAa,CAAC;AAexD,uCAAuC;AACvC,MAAM,WAAW,QAAQ;IACvB,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,qBAAqB;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,MAAM,EAAE,KAAK,GAAG,KAAK,CAAC;CACvB;AAMD,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AA+ED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,QAAQ,EAAE,CAYpE;AAqHD,iBAAS,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,MAAM,CAIpE;AAqBD,iBAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,GAAE,gBAAqB,uBAwC/D;AAED,mEAAmE;AACnE,wBAAgB,MAAM;;;;;EAOrB;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,GAAG;;;;;EAO1B"}
@@ -0,0 +1,294 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.readIcoEntries = readIcoEntries;
37
+ exports.msIcon = msIcon;
38
+ exports.default = ico;
39
+ const buffer_1 = require("buffer");
40
+ const BMP = __importStar(require("bmp-ts"));
41
+ const js_png_1 = __importStar(require("@jimp/js-png"));
42
+ const plugin_resize_1 = require("@jimp/plugin-resize");
43
+ const PNG_SIGNATURE = buffer_1.Buffer.from([
44
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
45
+ ]);
46
+ /** The 6-byte ICONDIR is followed by a 16-byte ICONDIRENTRY per image */
47
+ const DIRECTORY_SIZE = 6;
48
+ const ENTRY_SIZE = 16;
49
+ /** The largest width and height a directory entry can store */
50
+ const MAX_SIZE = 256;
51
+ /** The bit depths bmp-ts decodes */
52
+ const BIT_DEPTHS = [1, 4, 8, 16, 24, 32];
53
+ /** Channels per PNG colour type */
54
+ const PNG_CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
55
+ function isPng(data) {
56
+ return (data.length >= PNG_SIGNATURE.length &&
57
+ PNG_SIGNATURE.equals(data.subarray(0, PNG_SIGNATURE.length)));
58
+ }
59
+ function readEntries(data) {
60
+ const type = data.length >= DIRECTORY_SIZE ? data.readUInt16LE(2) : 0;
61
+ // Type 1 is an icon and type 2 a cursor, which only adds a hotspot
62
+ if (data.length < DIRECTORY_SIZE || data.readUInt16LE(0) !== 0 || !type) {
63
+ throw new Error("Invalid ICO file: it doesn't start with an ICO header");
64
+ }
65
+ if (type > 2) {
66
+ throw new Error(`Invalid ICO file: unknown image type ${type}`);
67
+ }
68
+ const count = data.readUInt16LE(4);
69
+ if (count === 0) {
70
+ throw new Error("Invalid ICO file: it contains no images");
71
+ }
72
+ if (DIRECTORY_SIZE + count * ENTRY_SIZE > data.length) {
73
+ throw new Error(`Invalid ICO file: the directory of ${count} images is truncated`);
74
+ }
75
+ return Array.from({ length: count }, (_, index) => {
76
+ const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;
77
+ const size = data.readUInt32LE(entry + 8);
78
+ const offset = data.readUInt32LE(entry + 12);
79
+ if (offset + size > data.length) {
80
+ throw new Error(`Invalid ICO file: image ${index} is past the end of the file`);
81
+ }
82
+ const image = data.subarray(offset, offset + size);
83
+ // The directory's sizes can't describe images larger than 256 pixels, and
84
+ // its bit depth is often 0, so read them from the image itself
85
+ if (isPng(image)) {
86
+ if (image.length < 26) {
87
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
88
+ }
89
+ return {
90
+ index,
91
+ width: image.readUInt32BE(16),
92
+ height: image.readUInt32BE(20),
93
+ bitDepth: image[24] * (PNG_CHANNELS[image[25]] ?? 1),
94
+ format: "png",
95
+ data: image,
96
+ };
97
+ }
98
+ if (image.length < 40) {
99
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
100
+ }
101
+ return {
102
+ index,
103
+ width: image.readInt32LE(4),
104
+ // The height covers both the colours and the transparency mask
105
+ height: Math.floor(image.readInt32LE(8) / 2),
106
+ bitDepth: image.readUInt16LE(14),
107
+ format: "bmp",
108
+ data: image,
109
+ };
110
+ });
111
+ }
112
+ /**
113
+ * List the images in an ICO or CUR file, to pick one to decode with the
114
+ * `index` decode option.
115
+ * @example
116
+ * ```ts
117
+ * import { Jimp } from "jimp";
118
+ * import { readIcoEntries } from "@jimp/js-ico";
119
+ *
120
+ * const icons = await Promise.all(
121
+ * readIcoEntries(buffer).map(({ index }) =>
122
+ * Jimp.read(buffer, { "image/x-icon": { index } })
123
+ * )
124
+ * );
125
+ * ```
126
+ */
127
+ function readIcoEntries(data) {
128
+ const buffer = buffer_1.Buffer.from(data.buffer, data.byteOffset, data.byteLength);
129
+ return readEntries(buffer).map(({ index, width, height, bitDepth, format }) => ({
130
+ index,
131
+ width,
132
+ height,
133
+ bitDepth,
134
+ format,
135
+ }));
136
+ }
137
+ /** Whether `a` has more pixels, or as many and more bits per pixel */
138
+ function isLarger(a, b) {
139
+ const pixels = a.width * a.height - b.width * b.height;
140
+ return pixels > 0 || (pixels === 0 && a.bitDepth > b.bitDepth);
141
+ }
142
+ function pickEntry(entries, { index, size }) {
143
+ if (index !== undefined) {
144
+ const entry = entries[index];
145
+ if (!entry) {
146
+ throw new Error(`ICO image index ${index} is out of range, the file has ${entries.length} images`);
147
+ }
148
+ return entry;
149
+ }
150
+ const side = (entry) => Math.max(entry.width, entry.height);
151
+ const largeEnough = size === undefined ? [] : entries.filter((entry) => side(entry) >= size);
152
+ if (largeEnough.length === 0) {
153
+ return entries.reduce((a, b) => (isLarger(b, a) ? b : a));
154
+ }
155
+ return largeEnough.reduce((best, entry) => {
156
+ const difference = side(entry) - side(best);
157
+ return difference < 0 ||
158
+ (difference === 0 && entry.bitDepth > best.bitDepth)
159
+ ? entry
160
+ : best;
161
+ });
162
+ }
163
+ /**
164
+ * A BMP in an ICO has no file header and is followed by a 1 bit mask, where
165
+ * set bits are transparent. Its height counts the rows of both.
166
+ */
167
+ function decodeBmp({ data, index, width, height, bitDepth }) {
168
+ if (!BIT_DEPTHS.includes(bitDepth)) {
169
+ throw new Error(`Unsupported BMP bit depth ${bitDepth}`);
170
+ }
171
+ const headerSize = data.readUInt32LE(0);
172
+ const compression = data.readUInt32LE(16);
173
+ const colors = data.readUInt32LE(32);
174
+ // Mirror how bmp-ts finds the end of the header and palette
175
+ const masks = headerSize === 40 && compression === BMP.BmpCompression.BI_BIT_FIELDS
176
+ ? 12
177
+ : headerSize === 40 &&
178
+ compression === BMP.BmpCompression.BI_ALPHA_BIT_FIELDS
179
+ ? 16
180
+ : 0;
181
+ const paletteLength = bitDepth <= 8 || colors > 0 ? colors || 1 << bitDepth : 0;
182
+ const pixelOffset = headerSize + masks + paletteLength * 4;
183
+ if (pixelOffset > data.length) {
184
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
185
+ }
186
+ const stride = Math.ceil((width * bitDepth) / 32) * 4;
187
+ const maskOffset = pixelOffset + stride * height;
188
+ const maskStride = Math.ceil(width / 32) * 4;
189
+ // bmp-ts reads BMP files, so give it a file header and the real height
190
+ const file = buffer_1.Buffer.alloc(14 + data.length);
191
+ file.write("BM", 0, "latin1");
192
+ file.writeUInt32LE(file.length, 2);
193
+ file.writeUInt32LE(14 + pixelOffset, 10);
194
+ data.copy(file, 14);
195
+ file.writeInt32LE(height, 14 + 8);
196
+ const bitmap = BMP.decode(file, { toRGBA: true });
197
+ let hasAlpha = false;
198
+ // bmp-ts ignores the alpha byte of 32 bit pixels without an alpha mask, but
199
+ // in an ICO it is the alpha
200
+ if (bitDepth === 32) {
201
+ for (let y = 0; y < height; y++) {
202
+ const row = pixelOffset + (height - 1 - y) * stride;
203
+ for (let x = 0; x < width; x++) {
204
+ const alpha = data[row + x * 4 + 3];
205
+ bitmap.data[(y * width + x) * 4 + 3] = alpha;
206
+ hasAlpha ||= alpha !== 0;
207
+ }
208
+ }
209
+ }
210
+ // Use the mask for images without alpha, including 32 bit images whose
211
+ // alpha is all 0. Pixels the mask inverts, rather than hides, are made
212
+ // transparent too. Without a mask the image is opaque.
213
+ if (!hasAlpha) {
214
+ for (let y = 0; y < height; y++) {
215
+ const row = maskOffset + (height - 1 - y) * maskStride;
216
+ for (let x = 0; x < width; x++) {
217
+ const byte = row + (x >> 3);
218
+ const transparent = byte < data.length && (data[byte] >> (7 - (x & 7))) & 1;
219
+ bitmap.data[(y * width + x) * 4 + 3] = transparent ? 0 : 0xff;
220
+ }
221
+ }
222
+ }
223
+ return { data: bitmap.data, width, height };
224
+ }
225
+ function decode(data, options = {}) {
226
+ const entry = pickEntry(readEntries(data), options);
227
+ return entry.format === "png" ? (0, js_png_1.default)().decode(entry.data) : decodeBmp(entry);
228
+ }
229
+ /** Scale the image to fit in a square of the given size */
230
+ function scaleToFit(bitmap, size) {
231
+ const scale = size / Math.max(bitmap.width, bitmap.height);
232
+ if (scale === 1) {
233
+ return bitmap;
234
+ }
235
+ // Resize a copy, the bitmap is the image's own
236
+ const image = { bitmap: { ...bitmap, data: buffer_1.Buffer.from(bitmap.data) } };
237
+ plugin_resize_1.methods.resize(image, {
238
+ w: Math.max(1, Math.round(bitmap.width * scale)),
239
+ h: Math.max(1, Math.round(bitmap.height * scale)),
240
+ });
241
+ return image.bitmap;
242
+ }
243
+ function encode(bitmap, { sizes } = {}) {
244
+ const images = sizes?.length
245
+ ? [
246
+ ...new Set(sizes.map((size) => Math.min(Math.max(Math.round(size), 1), MAX_SIZE))),
247
+ ].map((size) => scaleToFit(bitmap, size))
248
+ : [
249
+ Math.max(bitmap.width, bitmap.height) > MAX_SIZE
250
+ ? scaleToFit(bitmap, MAX_SIZE)
251
+ : bitmap,
252
+ ];
253
+ // Older versions of Windows only read 32 bit RGBA PNGs in icons
254
+ const pngs = images.map((image) => (0, js_png_1.default)().encode(image, { colorType: js_png_1.PNGColorType.COLOR_ALPHA }));
255
+ const directory = buffer_1.Buffer.alloc(DIRECTORY_SIZE + images.length * ENTRY_SIZE);
256
+ let offset = directory.length;
257
+ directory.writeUInt16LE(1, 2);
258
+ directory.writeUInt16LE(images.length, 4);
259
+ images.forEach((image, index) => {
260
+ const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;
261
+ const data = pngs[index];
262
+ // 0 means 256
263
+ directory[entry] = image.width % MAX_SIZE;
264
+ directory[entry + 1] = image.height % MAX_SIZE;
265
+ // Planes and bits per pixel
266
+ directory.writeUInt16LE(1, entry + 4);
267
+ directory.writeUInt16LE(32, entry + 6);
268
+ directory.writeUInt32LE(data.length, entry + 8);
269
+ directory.writeUInt32LE(offset, entry + 12);
270
+ offset += data.length;
271
+ });
272
+ return buffer_1.Buffer.concat([directory, ...pngs]);
273
+ }
274
+ /** The same format for the `image/vnd.microsoft.icon` MIME type */
275
+ function msIcon() {
276
+ return {
277
+ mime: "image/vnd.microsoft.icon",
278
+ hasAlpha: true,
279
+ encode,
280
+ decode,
281
+ };
282
+ }
283
+ /**
284
+ * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.
285
+ */
286
+ function ico() {
287
+ return {
288
+ mime: "image/x-icon",
289
+ hasAlpha: true,
290
+ encode,
291
+ decode,
292
+ };
293
+ }
294
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAAgC;AAChC,MAAY,GAAG,mCAAe;AAC9B,uDAAiD;AACjD,uDAAwD;AAGxD,MAAM,aAAa,GAAG,eAAM,CAAC,IAAI,CAAC;IAChC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAA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,CAAC,MAAM,GAAG,QAAQ,CAAC;QAC/C,4BAA4B;QAC5B,SAAS,CAAC,aAAa,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACtC,SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACvC,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QAChD,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,GAAG,EAAE,CAAC,CAAC;QAC5C,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,OAAO,eAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;AAC7C,CAAC;AAED,mEAAmE;AACnE;IACE,OAAO;QACL,IAAI,EAAE,0BAA0B;QAChC,QAAQ,EAAE,IAAI;QACd,MAAM;QACN,MAAM;KACsC,CAAC;AACjD,CAAC;AAED;;GAEG;AACH;IACE,OAAO;QACL,IAAI,EAAE,cAAc;QACpB,QAAQ,EAAE,IAAI;QACd,MAAM;QACN,MAAM;KAC0B,CAAC;AACrC,CAAC","sourcesContent":["import { Buffer } from \"buffer\";\nimport * as BMP from \"bmp-ts\";\nimport png, { PNGColorType } from \"@jimp/js-png\";\nimport { methods as resize } from \"@jimp/plugin-resize\";\nimport { Bitmap, Format, JimpClass } from \"@jimp/types\";\n\nconst PNG_SIGNATURE = Buffer.from([\n 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,\n]);\n/** The 6-byte ICONDIR is followed by a 16-byte ICONDIRENTRY per image */\nconst DIRECTORY_SIZE = 6;\nconst ENTRY_SIZE = 16;\n/** The largest width and height a directory entry can store */\nconst MAX_SIZE = 256;\n/** The bit depths bmp-ts decodes */\nconst BIT_DEPTHS = [1, 4, 8, 16, 24, 32];\n/** Channels per PNG colour type */\nconst PNG_CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };\n\n/** An image in an ICO (or CUR) file */\nexport interface IcoEntry {\n /** Position in the file, which the `index` decode option takes */\n index: number;\n width: number;\n height: number;\n /** Bits per pixel */\n bitDepth: number;\n /** Whether the image is stored as a PNG or a BMP */\n format: \"png\" | \"bmp\";\n}\n\ninterface Entry extends IcoEntry {\n data: Buffer;\n}\n\nexport interface DecodeIcoOptions {\n /**\n * Decode the image at this position in the file. `readIcoEntries` lists\n * the images.\n */\n index?: number;\n /**\n * Decode the image whose longest side best fits this size: the smallest\n * image at least this big, or the largest image if they're all smaller.\n * Of images the same size, the one with the most bits per pixel is used.\n */\n size?: number;\n}\n\nexport interface EncodeIcoOptions {\n /**\n * Write an image for each size, scaled to fit in a square of that size,\n * e.g. `[16, 32, 48, 256]`. Sizes are clamped to 1-256.\n * By default the ICO holds just the image, scaled down to fit in 256x256\n * if it's larger.\n */\n sizes?: number[];\n}\n\nfunction isPng(data: Buffer) {\n return (\n data.length >= PNG_SIGNATURE.length &&\n PNG_SIGNATURE.equals(data.subarray(0, PNG_SIGNATURE.length))\n );\n}\n\nfunction readEntries(data: Buffer): Entry[] {\n const type = data.length >= DIRECTORY_SIZE ? data.readUInt16LE(2) : 0;\n\n // Type 1 is an icon and type 2 a cursor, which only adds a hotspot\n if (data.length < DIRECTORY_SIZE || data.readUInt16LE(0) !== 0 || !type) {\n throw new Error(\"Invalid ICO file: it doesn't start with an ICO header\");\n }\n\n if (type > 2) {\n throw new Error(`Invalid ICO file: unknown image type ${type}`);\n }\n\n const count = data.readUInt16LE(4);\n\n if (count === 0) {\n throw new Error(\"Invalid ICO file: it contains no images\");\n }\n\n if (DIRECTORY_SIZE + count * ENTRY_SIZE > data.length) {\n throw new Error(\n `Invalid ICO file: the directory of ${count} images is truncated`,\n );\n }\n\n return Array.from({ length: count }, (_, index) => {\n const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;\n const size = data.readUInt32LE(entry + 8);\n const offset = data.readUInt32LE(entry + 12);\n\n if (offset + size > data.length) {\n throw new Error(\n `Invalid ICO file: image ${index} is past the end of the file`,\n );\n }\n\n const image = data.subarray(offset, offset + size);\n\n // The directory's sizes can't describe images larger than 256 pixels, and\n // its bit depth is often 0, so read them from the image itself\n if (isPng(image)) {\n if (image.length < 26) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n return {\n index,\n width: image.readUInt32BE(16),\n height: image.readUInt32BE(20),\n bitDepth: image[24]! * (PNG_CHANNELS[image[25]!] ?? 1),\n format: \"png\",\n data: image,\n };\n }\n\n if (image.length < 40) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n return {\n index,\n width: image.readInt32LE(4),\n // The height covers both the colours and the transparency mask\n height: Math.floor(image.readInt32LE(8) / 2),\n bitDepth: image.readUInt16LE(14),\n format: \"bmp\",\n data: image,\n };\n });\n}\n\n/**\n * List the images in an ICO or CUR file, to pick one to decode with the\n * `index` decode option.\n * @example\n * ```ts\n * import { Jimp } from \"jimp\";\n * import { readIcoEntries } from \"@jimp/js-ico\";\n *\n * const icons = await Promise.all(\n * readIcoEntries(buffer).map(({ index }) =>\n * Jimp.read(buffer, { \"image/x-icon\": { index } })\n * )\n * );\n * ```\n */\nexport function readIcoEntries(data: Buffer | Uint8Array): IcoEntry[] {\n const buffer = Buffer.from(data.buffer, data.byteOffset, data.byteLength);\n\n return readEntries(buffer).map(\n ({ index, width, height, bitDepth, format }) => ({\n index,\n width,\n height,\n bitDepth,\n format,\n }),\n );\n}\n\n/** Whether `a` has more pixels, or as many and more bits per pixel */\nfunction isLarger(a: Entry, b: Entry) {\n const pixels = a.width * a.height - b.width * b.height;\n return pixels > 0 || (pixels === 0 && a.bitDepth > b.bitDepth);\n}\n\nfunction pickEntry(entries: Entry[], { index, size }: DecodeIcoOptions) {\n if (index !== undefined) {\n const entry = entries[index];\n\n if (!entry) {\n throw new Error(\n `ICO image index ${index} is out of range, the file has ${entries.length} images`,\n );\n }\n\n return entry;\n }\n\n const side = (entry: Entry) => Math.max(entry.width, entry.height);\n const largeEnough =\n size === undefined ? [] : entries.filter((entry) => side(entry) >= size);\n\n if (largeEnough.length === 0) {\n return entries.reduce((a, b) => (isLarger(b, a) ? b : a));\n }\n\n return largeEnough.reduce((best, entry) => {\n const difference = side(entry) - side(best);\n return difference < 0 ||\n (difference === 0 && entry.bitDepth > best.bitDepth)\n ? entry\n : best;\n });\n}\n\n/**\n * A BMP in an ICO has no file header and is followed by a 1 bit mask, where\n * set bits are transparent. Its height counts the rows of both.\n */\nfunction decodeBmp({ data, index, width, height, bitDepth }: Entry): Bitmap {\n if (!BIT_DEPTHS.includes(bitDepth)) {\n throw new Error(`Unsupported BMP bit depth ${bitDepth}`);\n }\n\n const headerSize = data.readUInt32LE(0);\n const compression = data.readUInt32LE(16);\n const colors = data.readUInt32LE(32);\n // Mirror how bmp-ts finds the end of the header and palette\n const masks =\n headerSize === 40 && compression === BMP.BmpCompression.BI_BIT_FIELDS\n ? 12\n : headerSize === 40 &&\n compression === BMP.BmpCompression.BI_ALPHA_BIT_FIELDS\n ? 16\n : 0;\n const paletteLength =\n bitDepth <= 8 || colors > 0 ? colors || 1 << bitDepth : 0;\n const pixelOffset = headerSize + masks + paletteLength * 4;\n\n if (pixelOffset > data.length) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n const stride = Math.ceil((width * bitDepth) / 32) * 4;\n const maskOffset = pixelOffset + stride * height;\n const maskStride = Math.ceil(width / 32) * 4;\n\n // bmp-ts reads BMP files, so give it a file header and the real height\n const file = Buffer.alloc(14 + data.length);\n\n file.write(\"BM\", 0, \"latin1\");\n file.writeUInt32LE(file.length, 2);\n file.writeUInt32LE(14 + pixelOffset, 10);\n data.copy(file, 14);\n file.writeInt32LE(height, 14 + 8);\n\n const bitmap = BMP.decode(file, { toRGBA: true });\n let hasAlpha = false;\n\n // bmp-ts ignores the alpha byte of 32 bit pixels without an alpha mask, but\n // in an ICO it is the alpha\n if (bitDepth === 32) {\n for (let y = 0; y < height; y++) {\n const row = pixelOffset + (height - 1 - y) * stride;\n\n for (let x = 0; x < width; x++) {\n const alpha = data[row + x * 4 + 3]!;\n\n bitmap.data[(y * width + x) * 4 + 3] = alpha;\n hasAlpha ||= alpha !== 0;\n }\n }\n }\n\n // Use the mask for images without alpha, including 32 bit images whose\n // alpha is all 0. Pixels the mask inverts, rather than hides, are made\n // transparent too. Without a mask the image is opaque.\n if (!hasAlpha) {\n for (let y = 0; y < height; y++) {\n const row = maskOffset + (height - 1 - y) * maskStride;\n\n for (let x = 0; x < width; x++) {\n const byte = row + (x >> 3);\n const transparent =\n byte < data.length && (data[byte]! >> (7 - (x & 7))) & 1;\n\n bitmap.data[(y * width + x) * 4 + 3] = transparent ? 0 : 0xff;\n }\n }\n }\n\n return { data: bitmap.data, width, height };\n}\n\nfunction decode(data: Buffer, options: DecodeIcoOptions = {}): Bitmap {\n const entry = pickEntry(readEntries(data), options);\n\n return entry.format === \"png\" ? png().decode(entry.data) : decodeBmp(entry);\n}\n\n/** Scale the image to fit in a square of the given size */\nfunction scaleToFit(bitmap: Bitmap, size: number): Bitmap {\n const scale = size / Math.max(bitmap.width, bitmap.height);\n\n if (scale === 1) {\n return bitmap;\n }\n\n // Resize a copy, the bitmap is the image's own\n const image = { bitmap: { ...bitmap, data: Buffer.from(bitmap.data) } };\n\n resize.resize(image as JimpClass, {\n w: Math.max(1, Math.round(bitmap.width * scale)),\n h: Math.max(1, Math.round(bitmap.height * scale)),\n });\n\n return image.bitmap;\n}\n\nfunction encode(bitmap: Bitmap, { sizes }: EncodeIcoOptions = {}) {\n const images = sizes?.length\n ? [\n ...new Set(\n sizes.map((size) =>\n Math.min(Math.max(Math.round(size), 1), MAX_SIZE),\n ),\n ),\n ].map((size) => scaleToFit(bitmap, size))\n : [\n Math.max(bitmap.width, bitmap.height) > MAX_SIZE\n ? scaleToFit(bitmap, MAX_SIZE)\n : bitmap,\n ];\n // Older versions of Windows only read 32 bit RGBA PNGs in icons\n const pngs = images.map((image) =>\n png().encode(image, { colorType: PNGColorType.COLOR_ALPHA }),\n );\n const directory = Buffer.alloc(DIRECTORY_SIZE + images.length * ENTRY_SIZE);\n let offset = directory.length;\n\n directory.writeUInt16LE(1, 2);\n directory.writeUInt16LE(images.length, 4);\n\n images.forEach((image, index) => {\n const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;\n const data = pngs[index]!;\n\n // 0 means 256\n directory[entry] = image.width % MAX_SIZE;\n directory[entry + 1] = image.height % MAX_SIZE;\n // Planes and bits per pixel\n directory.writeUInt16LE(1, entry + 4);\n directory.writeUInt16LE(32, entry + 6);\n directory.writeUInt32LE(data.length, entry + 8);\n directory.writeUInt32LE(offset, entry + 12);\n offset += data.length;\n });\n\n return Buffer.concat([directory, ...pngs]);\n}\n\n/** The same format for the `image/vnd.microsoft.icon` MIME type */\nexport function msIcon() {\n return {\n mime: \"image/vnd.microsoft.icon\",\n hasAlpha: true,\n encode,\n decode,\n } satisfies Format<\"image/vnd.microsoft.icon\">;\n}\n\n/**\n * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.\n */\nexport default function ico() {\n return {\n mime: \"image/x-icon\",\n hasAlpha: true,\n encode,\n decode,\n } satisfies Format<\"image/x-icon\">;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,71 @@
1
+ import { Buffer } from "buffer";
2
+ import { Bitmap } from "@jimp/types";
3
+ /** An image in an ICO (or CUR) file */
4
+ export interface IcoEntry {
5
+ /** Position in the file, which the `index` decode option takes */
6
+ index: number;
7
+ width: number;
8
+ height: number;
9
+ /** Bits per pixel */
10
+ bitDepth: number;
11
+ /** Whether the image is stored as a PNG or a BMP */
12
+ format: "png" | "bmp";
13
+ }
14
+ export interface DecodeIcoOptions {
15
+ /**
16
+ * Decode the image at this position in the file. `readIcoEntries` lists
17
+ * the images.
18
+ */
19
+ index?: number;
20
+ /**
21
+ * Decode the image whose longest side best fits this size: the smallest
22
+ * image at least this big, or the largest image if they're all smaller.
23
+ * Of images the same size, the one with the most bits per pixel is used.
24
+ */
25
+ size?: number;
26
+ }
27
+ export interface EncodeIcoOptions {
28
+ /**
29
+ * Write an image for each size, scaled to fit in a square of that size,
30
+ * e.g. `[16, 32, 48, 256]`. Sizes are clamped to 1-256.
31
+ * By default the ICO holds just the image, scaled down to fit in 256x256
32
+ * if it's larger.
33
+ */
34
+ sizes?: number[];
35
+ }
36
+ /**
37
+ * List the images in an ICO or CUR file, to pick one to decode with the
38
+ * `index` decode option.
39
+ * @example
40
+ * ```ts
41
+ * import { Jimp } from "jimp";
42
+ * import { readIcoEntries } from "@jimp/js-ico";
43
+ *
44
+ * const icons = await Promise.all(
45
+ * readIcoEntries(buffer).map(({ index }) =>
46
+ * Jimp.read(buffer, { "image/x-icon": { index } })
47
+ * )
48
+ * );
49
+ * ```
50
+ */
51
+ export declare function readIcoEntries(data: Buffer | Uint8Array): IcoEntry[];
52
+ declare function decode(data: Buffer, options?: DecodeIcoOptions): Bitmap;
53
+ declare function encode(bitmap: Bitmap, { sizes }?: EncodeIcoOptions): Buffer<ArrayBuffer>;
54
+ /** The same format for the `image/vnd.microsoft.icon` MIME type */
55
+ export declare function msIcon(): {
56
+ mime: "image/vnd.microsoft.icon";
57
+ hasAlpha: true;
58
+ encode: typeof encode;
59
+ decode: typeof decode;
60
+ };
61
+ /**
62
+ * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.
63
+ */
64
+ export default function ico(): {
65
+ mime: "image/x-icon";
66
+ hasAlpha: true;
67
+ encode: typeof encode;
68
+ decode: typeof decode;
69
+ };
70
+ export {};
71
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,OAAO,EAAE,MAAM,EAAqB,MAAM,aAAa,CAAC;AAexD,uCAAuC;AACvC,MAAM,WAAW,QAAQ;IACvB,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,qBAAqB;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,MAAM,EAAE,KAAK,GAAG,KAAK,CAAC;CACvB;AAMD,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AA+ED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,QAAQ,EAAE,CAYpE;AAqHD,iBAAS,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,MAAM,CAIpE;AAqBD,iBAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,GAAE,gBAAqB,uBAwC/D;AAED,mEAAmE;AACnE,wBAAgB,MAAM;;;;;EAOrB;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,GAAG;;;;;EAO1B"}
@@ -0,0 +1,256 @@
1
+ import { Buffer } from "buffer";
2
+ import * as BMP from "bmp-ts";
3
+ import png, { PNGColorType } from "@jimp/js-png";
4
+ import { methods as resize } from "@jimp/plugin-resize";
5
+ const PNG_SIGNATURE = Buffer.from([
6
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
7
+ ]);
8
+ /** The 6-byte ICONDIR is followed by a 16-byte ICONDIRENTRY per image */
9
+ const DIRECTORY_SIZE = 6;
10
+ const ENTRY_SIZE = 16;
11
+ /** The largest width and height a directory entry can store */
12
+ const MAX_SIZE = 256;
13
+ /** The bit depths bmp-ts decodes */
14
+ const BIT_DEPTHS = [1, 4, 8, 16, 24, 32];
15
+ /** Channels per PNG colour type */
16
+ const PNG_CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
17
+ function isPng(data) {
18
+ return (data.length >= PNG_SIGNATURE.length &&
19
+ PNG_SIGNATURE.equals(data.subarray(0, PNG_SIGNATURE.length)));
20
+ }
21
+ function readEntries(data) {
22
+ const type = data.length >= DIRECTORY_SIZE ? data.readUInt16LE(2) : 0;
23
+ // Type 1 is an icon and type 2 a cursor, which only adds a hotspot
24
+ if (data.length < DIRECTORY_SIZE || data.readUInt16LE(0) !== 0 || !type) {
25
+ throw new Error("Invalid ICO file: it doesn't start with an ICO header");
26
+ }
27
+ if (type > 2) {
28
+ throw new Error(`Invalid ICO file: unknown image type ${type}`);
29
+ }
30
+ const count = data.readUInt16LE(4);
31
+ if (count === 0) {
32
+ throw new Error("Invalid ICO file: it contains no images");
33
+ }
34
+ if (DIRECTORY_SIZE + count * ENTRY_SIZE > data.length) {
35
+ throw new Error(`Invalid ICO file: the directory of ${count} images is truncated`);
36
+ }
37
+ return Array.from({ length: count }, (_, index) => {
38
+ const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;
39
+ const size = data.readUInt32LE(entry + 8);
40
+ const offset = data.readUInt32LE(entry + 12);
41
+ if (offset + size > data.length) {
42
+ throw new Error(`Invalid ICO file: image ${index} is past the end of the file`);
43
+ }
44
+ const image = data.subarray(offset, offset + size);
45
+ // The directory's sizes can't describe images larger than 256 pixels, and
46
+ // its bit depth is often 0, so read them from the image itself
47
+ if (isPng(image)) {
48
+ if (image.length < 26) {
49
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
50
+ }
51
+ return {
52
+ index,
53
+ width: image.readUInt32BE(16),
54
+ height: image.readUInt32BE(20),
55
+ bitDepth: image[24] * (PNG_CHANNELS[image[25]] ?? 1),
56
+ format: "png",
57
+ data: image,
58
+ };
59
+ }
60
+ if (image.length < 40) {
61
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
62
+ }
63
+ return {
64
+ index,
65
+ width: image.readInt32LE(4),
66
+ // The height covers both the colours and the transparency mask
67
+ height: Math.floor(image.readInt32LE(8) / 2),
68
+ bitDepth: image.readUInt16LE(14),
69
+ format: "bmp",
70
+ data: image,
71
+ };
72
+ });
73
+ }
74
+ /**
75
+ * List the images in an ICO or CUR file, to pick one to decode with the
76
+ * `index` decode option.
77
+ * @example
78
+ * ```ts
79
+ * import { Jimp } from "jimp";
80
+ * import { readIcoEntries } from "@jimp/js-ico";
81
+ *
82
+ * const icons = await Promise.all(
83
+ * readIcoEntries(buffer).map(({ index }) =>
84
+ * Jimp.read(buffer, { "image/x-icon": { index } })
85
+ * )
86
+ * );
87
+ * ```
88
+ */
89
+ export function readIcoEntries(data) {
90
+ const buffer = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
91
+ return readEntries(buffer).map(({ index, width, height, bitDepth, format }) => ({
92
+ index,
93
+ width,
94
+ height,
95
+ bitDepth,
96
+ format,
97
+ }));
98
+ }
99
+ /** Whether `a` has more pixels, or as many and more bits per pixel */
100
+ function isLarger(a, b) {
101
+ const pixels = a.width * a.height - b.width * b.height;
102
+ return pixels > 0 || (pixels === 0 && a.bitDepth > b.bitDepth);
103
+ }
104
+ function pickEntry(entries, { index, size }) {
105
+ if (index !== undefined) {
106
+ const entry = entries[index];
107
+ if (!entry) {
108
+ throw new Error(`ICO image index ${index} is out of range, the file has ${entries.length} images`);
109
+ }
110
+ return entry;
111
+ }
112
+ const side = (entry) => Math.max(entry.width, entry.height);
113
+ const largeEnough = size === undefined ? [] : entries.filter((entry) => side(entry) >= size);
114
+ if (largeEnough.length === 0) {
115
+ return entries.reduce((a, b) => (isLarger(b, a) ? b : a));
116
+ }
117
+ return largeEnough.reduce((best, entry) => {
118
+ const difference = side(entry) - side(best);
119
+ return difference < 0 ||
120
+ (difference === 0 && entry.bitDepth > best.bitDepth)
121
+ ? entry
122
+ : best;
123
+ });
124
+ }
125
+ /**
126
+ * A BMP in an ICO has no file header and is followed by a 1 bit mask, where
127
+ * set bits are transparent. Its height counts the rows of both.
128
+ */
129
+ function decodeBmp({ data, index, width, height, bitDepth }) {
130
+ if (!BIT_DEPTHS.includes(bitDepth)) {
131
+ throw new Error(`Unsupported BMP bit depth ${bitDepth}`);
132
+ }
133
+ const headerSize = data.readUInt32LE(0);
134
+ const compression = data.readUInt32LE(16);
135
+ const colors = data.readUInt32LE(32);
136
+ // Mirror how bmp-ts finds the end of the header and palette
137
+ const masks = headerSize === 40 && compression === BMP.BmpCompression.BI_BIT_FIELDS
138
+ ? 12
139
+ : headerSize === 40 &&
140
+ compression === BMP.BmpCompression.BI_ALPHA_BIT_FIELDS
141
+ ? 16
142
+ : 0;
143
+ const paletteLength = bitDepth <= 8 || colors > 0 ? colors || 1 << bitDepth : 0;
144
+ const pixelOffset = headerSize + masks + paletteLength * 4;
145
+ if (pixelOffset > data.length) {
146
+ throw new Error(`Invalid ICO file: image ${index} is truncated`);
147
+ }
148
+ const stride = Math.ceil((width * bitDepth) / 32) * 4;
149
+ const maskOffset = pixelOffset + stride * height;
150
+ const maskStride = Math.ceil(width / 32) * 4;
151
+ // bmp-ts reads BMP files, so give it a file header and the real height
152
+ const file = Buffer.alloc(14 + data.length);
153
+ file.write("BM", 0, "latin1");
154
+ file.writeUInt32LE(file.length, 2);
155
+ file.writeUInt32LE(14 + pixelOffset, 10);
156
+ data.copy(file, 14);
157
+ file.writeInt32LE(height, 14 + 8);
158
+ const bitmap = BMP.decode(file, { toRGBA: true });
159
+ let hasAlpha = false;
160
+ // bmp-ts ignores the alpha byte of 32 bit pixels without an alpha mask, but
161
+ // in an ICO it is the alpha
162
+ if (bitDepth === 32) {
163
+ for (let y = 0; y < height; y++) {
164
+ const row = pixelOffset + (height - 1 - y) * stride;
165
+ for (let x = 0; x < width; x++) {
166
+ const alpha = data[row + x * 4 + 3];
167
+ bitmap.data[(y * width + x) * 4 + 3] = alpha;
168
+ hasAlpha ||= alpha !== 0;
169
+ }
170
+ }
171
+ }
172
+ // Use the mask for images without alpha, including 32 bit images whose
173
+ // alpha is all 0. Pixels the mask inverts, rather than hides, are made
174
+ // transparent too. Without a mask the image is opaque.
175
+ if (!hasAlpha) {
176
+ for (let y = 0; y < height; y++) {
177
+ const row = maskOffset + (height - 1 - y) * maskStride;
178
+ for (let x = 0; x < width; x++) {
179
+ const byte = row + (x >> 3);
180
+ const transparent = byte < data.length && (data[byte] >> (7 - (x & 7))) & 1;
181
+ bitmap.data[(y * width + x) * 4 + 3] = transparent ? 0 : 0xff;
182
+ }
183
+ }
184
+ }
185
+ return { data: bitmap.data, width, height };
186
+ }
187
+ function decode(data, options = {}) {
188
+ const entry = pickEntry(readEntries(data), options);
189
+ return entry.format === "png" ? png().decode(entry.data) : decodeBmp(entry);
190
+ }
191
+ /** Scale the image to fit in a square of the given size */
192
+ function scaleToFit(bitmap, size) {
193
+ const scale = size / Math.max(bitmap.width, bitmap.height);
194
+ if (scale === 1) {
195
+ return bitmap;
196
+ }
197
+ // Resize a copy, the bitmap is the image's own
198
+ const image = { bitmap: { ...bitmap, data: Buffer.from(bitmap.data) } };
199
+ resize.resize(image, {
200
+ w: Math.max(1, Math.round(bitmap.width * scale)),
201
+ h: Math.max(1, Math.round(bitmap.height * scale)),
202
+ });
203
+ return image.bitmap;
204
+ }
205
+ function encode(bitmap, { sizes } = {}) {
206
+ const images = sizes?.length
207
+ ? [
208
+ ...new Set(sizes.map((size) => Math.min(Math.max(Math.round(size), 1), MAX_SIZE))),
209
+ ].map((size) => scaleToFit(bitmap, size))
210
+ : [
211
+ Math.max(bitmap.width, bitmap.height) > MAX_SIZE
212
+ ? scaleToFit(bitmap, MAX_SIZE)
213
+ : bitmap,
214
+ ];
215
+ // Older versions of Windows only read 32 bit RGBA PNGs in icons
216
+ const pngs = images.map((image) => png().encode(image, { colorType: PNGColorType.COLOR_ALPHA }));
217
+ const directory = Buffer.alloc(DIRECTORY_SIZE + images.length * ENTRY_SIZE);
218
+ let offset = directory.length;
219
+ directory.writeUInt16LE(1, 2);
220
+ directory.writeUInt16LE(images.length, 4);
221
+ images.forEach((image, index) => {
222
+ const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;
223
+ const data = pngs[index];
224
+ // 0 means 256
225
+ directory[entry] = image.width % MAX_SIZE;
226
+ directory[entry + 1] = image.height % MAX_SIZE;
227
+ // Planes and bits per pixel
228
+ directory.writeUInt16LE(1, entry + 4);
229
+ directory.writeUInt16LE(32, entry + 6);
230
+ directory.writeUInt32LE(data.length, entry + 8);
231
+ directory.writeUInt32LE(offset, entry + 12);
232
+ offset += data.length;
233
+ });
234
+ return Buffer.concat([directory, ...pngs]);
235
+ }
236
+ /** The same format for the `image/vnd.microsoft.icon` MIME type */
237
+ export function msIcon() {
238
+ return {
239
+ mime: "image/vnd.microsoft.icon",
240
+ hasAlpha: true,
241
+ encode,
242
+ decode,
243
+ };
244
+ }
245
+ /**
246
+ * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.
247
+ */
248
+ export default function ico() {
249
+ return {
250
+ mime: "image/x-icon",
251
+ hasAlpha: true,
252
+ encode,
253
+ decode,
254
+ };
255
+ }
256
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,GAAG,MAAM,QAAQ,CAAC;AAC9B,OAAO,GAAG,EAAE,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGxD,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC;IAChC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;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AAC;QAC/C,4BAA4B;QAC5B,SAAS,CAAC,aAAa,CAAC,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACtC,SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACvC,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QAChD,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,GAAG,EAAE,CAAC,CAAC;QAC5C,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;AAC7C,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,MAAM;IACpB,OAAO;QACL,IAAI,EAAE,0BAA0B;QAChC,QAAQ,EAAE,IAAI;QACd,MAAM;QACN,MAAM;KACsC,CAAC;AACjD,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,GAAG;IACzB,OAAO;QACL,IAAI,EAAE,cAAc;QACpB,QAAQ,EAAE,IAAI;QACd,MAAM;QACN,MAAM;KAC0B,CAAC;AACrC,CAAC","sourcesContent":["import { Buffer } from \"buffer\";\nimport * as BMP from \"bmp-ts\";\nimport png, { PNGColorType } from \"@jimp/js-png\";\nimport { methods as resize } from \"@jimp/plugin-resize\";\nimport { Bitmap, Format, JimpClass } from \"@jimp/types\";\n\nconst PNG_SIGNATURE = Buffer.from([\n 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,\n]);\n/** The 6-byte ICONDIR is followed by a 16-byte ICONDIRENTRY per image */\nconst DIRECTORY_SIZE = 6;\nconst ENTRY_SIZE = 16;\n/** The largest width and height a directory entry can store */\nconst MAX_SIZE = 256;\n/** The bit depths bmp-ts decodes */\nconst BIT_DEPTHS = [1, 4, 8, 16, 24, 32];\n/** Channels per PNG colour type */\nconst PNG_CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };\n\n/** An image in an ICO (or CUR) file */\nexport interface IcoEntry {\n /** Position in the file, which the `index` decode option takes */\n index: number;\n width: number;\n height: number;\n /** Bits per pixel */\n bitDepth: number;\n /** Whether the image is stored as a PNG or a BMP */\n format: \"png\" | \"bmp\";\n}\n\ninterface Entry extends IcoEntry {\n data: Buffer;\n}\n\nexport interface DecodeIcoOptions {\n /**\n * Decode the image at this position in the file. `readIcoEntries` lists\n * the images.\n */\n index?: number;\n /**\n * Decode the image whose longest side best fits this size: the smallest\n * image at least this big, or the largest image if they're all smaller.\n * Of images the same size, the one with the most bits per pixel is used.\n */\n size?: number;\n}\n\nexport interface EncodeIcoOptions {\n /**\n * Write an image for each size, scaled to fit in a square of that size,\n * e.g. `[16, 32, 48, 256]`. Sizes are clamped to 1-256.\n * By default the ICO holds just the image, scaled down to fit in 256x256\n * if it's larger.\n */\n sizes?: number[];\n}\n\nfunction isPng(data: Buffer) {\n return (\n data.length >= PNG_SIGNATURE.length &&\n PNG_SIGNATURE.equals(data.subarray(0, PNG_SIGNATURE.length))\n );\n}\n\nfunction readEntries(data: Buffer): Entry[] {\n const type = data.length >= DIRECTORY_SIZE ? data.readUInt16LE(2) : 0;\n\n // Type 1 is an icon and type 2 a cursor, which only adds a hotspot\n if (data.length < DIRECTORY_SIZE || data.readUInt16LE(0) !== 0 || !type) {\n throw new Error(\"Invalid ICO file: it doesn't start with an ICO header\");\n }\n\n if (type > 2) {\n throw new Error(`Invalid ICO file: unknown image type ${type}`);\n }\n\n const count = data.readUInt16LE(4);\n\n if (count === 0) {\n throw new Error(\"Invalid ICO file: it contains no images\");\n }\n\n if (DIRECTORY_SIZE + count * ENTRY_SIZE > data.length) {\n throw new Error(\n `Invalid ICO file: the directory of ${count} images is truncated`,\n );\n }\n\n return Array.from({ length: count }, (_, index) => {\n const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;\n const size = data.readUInt32LE(entry + 8);\n const offset = data.readUInt32LE(entry + 12);\n\n if (offset + size > data.length) {\n throw new Error(\n `Invalid ICO file: image ${index} is past the end of the file`,\n );\n }\n\n const image = data.subarray(offset, offset + size);\n\n // The directory's sizes can't describe images larger than 256 pixels, and\n // its bit depth is often 0, so read them from the image itself\n if (isPng(image)) {\n if (image.length < 26) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n return {\n index,\n width: image.readUInt32BE(16),\n height: image.readUInt32BE(20),\n bitDepth: image[24]! * (PNG_CHANNELS[image[25]!] ?? 1),\n format: \"png\",\n data: image,\n };\n }\n\n if (image.length < 40) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n return {\n index,\n width: image.readInt32LE(4),\n // The height covers both the colours and the transparency mask\n height: Math.floor(image.readInt32LE(8) / 2),\n bitDepth: image.readUInt16LE(14),\n format: \"bmp\",\n data: image,\n };\n });\n}\n\n/**\n * List the images in an ICO or CUR file, to pick one to decode with the\n * `index` decode option.\n * @example\n * ```ts\n * import { Jimp } from \"jimp\";\n * import { readIcoEntries } from \"@jimp/js-ico\";\n *\n * const icons = await Promise.all(\n * readIcoEntries(buffer).map(({ index }) =>\n * Jimp.read(buffer, { \"image/x-icon\": { index } })\n * )\n * );\n * ```\n */\nexport function readIcoEntries(data: Buffer | Uint8Array): IcoEntry[] {\n const buffer = Buffer.from(data.buffer, data.byteOffset, data.byteLength);\n\n return readEntries(buffer).map(\n ({ index, width, height, bitDepth, format }) => ({\n index,\n width,\n height,\n bitDepth,\n format,\n }),\n );\n}\n\n/** Whether `a` has more pixels, or as many and more bits per pixel */\nfunction isLarger(a: Entry, b: Entry) {\n const pixels = a.width * a.height - b.width * b.height;\n return pixels > 0 || (pixels === 0 && a.bitDepth > b.bitDepth);\n}\n\nfunction pickEntry(entries: Entry[], { index, size }: DecodeIcoOptions) {\n if (index !== undefined) {\n const entry = entries[index];\n\n if (!entry) {\n throw new Error(\n `ICO image index ${index} is out of range, the file has ${entries.length} images`,\n );\n }\n\n return entry;\n }\n\n const side = (entry: Entry) => Math.max(entry.width, entry.height);\n const largeEnough =\n size === undefined ? [] : entries.filter((entry) => side(entry) >= size);\n\n if (largeEnough.length === 0) {\n return entries.reduce((a, b) => (isLarger(b, a) ? b : a));\n }\n\n return largeEnough.reduce((best, entry) => {\n const difference = side(entry) - side(best);\n return difference < 0 ||\n (difference === 0 && entry.bitDepth > best.bitDepth)\n ? entry\n : best;\n });\n}\n\n/**\n * A BMP in an ICO has no file header and is followed by a 1 bit mask, where\n * set bits are transparent. Its height counts the rows of both.\n */\nfunction decodeBmp({ data, index, width, height, bitDepth }: Entry): Bitmap {\n if (!BIT_DEPTHS.includes(bitDepth)) {\n throw new Error(`Unsupported BMP bit depth ${bitDepth}`);\n }\n\n const headerSize = data.readUInt32LE(0);\n const compression = data.readUInt32LE(16);\n const colors = data.readUInt32LE(32);\n // Mirror how bmp-ts finds the end of the header and palette\n const masks =\n headerSize === 40 && compression === BMP.BmpCompression.BI_BIT_FIELDS\n ? 12\n : headerSize === 40 &&\n compression === BMP.BmpCompression.BI_ALPHA_BIT_FIELDS\n ? 16\n : 0;\n const paletteLength =\n bitDepth <= 8 || colors > 0 ? colors || 1 << bitDepth : 0;\n const pixelOffset = headerSize + masks + paletteLength * 4;\n\n if (pixelOffset > data.length) {\n throw new Error(`Invalid ICO file: image ${index} is truncated`);\n }\n\n const stride = Math.ceil((width * bitDepth) / 32) * 4;\n const maskOffset = pixelOffset + stride * height;\n const maskStride = Math.ceil(width / 32) * 4;\n\n // bmp-ts reads BMP files, so give it a file header and the real height\n const file = Buffer.alloc(14 + data.length);\n\n file.write(\"BM\", 0, \"latin1\");\n file.writeUInt32LE(file.length, 2);\n file.writeUInt32LE(14 + pixelOffset, 10);\n data.copy(file, 14);\n file.writeInt32LE(height, 14 + 8);\n\n const bitmap = BMP.decode(file, { toRGBA: true });\n let hasAlpha = false;\n\n // bmp-ts ignores the alpha byte of 32 bit pixels without an alpha mask, but\n // in an ICO it is the alpha\n if (bitDepth === 32) {\n for (let y = 0; y < height; y++) {\n const row = pixelOffset + (height - 1 - y) * stride;\n\n for (let x = 0; x < width; x++) {\n const alpha = data[row + x * 4 + 3]!;\n\n bitmap.data[(y * width + x) * 4 + 3] = alpha;\n hasAlpha ||= alpha !== 0;\n }\n }\n }\n\n // Use the mask for images without alpha, including 32 bit images whose\n // alpha is all 0. Pixels the mask inverts, rather than hides, are made\n // transparent too. Without a mask the image is opaque.\n if (!hasAlpha) {\n for (let y = 0; y < height; y++) {\n const row = maskOffset + (height - 1 - y) * maskStride;\n\n for (let x = 0; x < width; x++) {\n const byte = row + (x >> 3);\n const transparent =\n byte < data.length && (data[byte]! >> (7 - (x & 7))) & 1;\n\n bitmap.data[(y * width + x) * 4 + 3] = transparent ? 0 : 0xff;\n }\n }\n }\n\n return { data: bitmap.data, width, height };\n}\n\nfunction decode(data: Buffer, options: DecodeIcoOptions = {}): Bitmap {\n const entry = pickEntry(readEntries(data), options);\n\n return entry.format === \"png\" ? png().decode(entry.data) : decodeBmp(entry);\n}\n\n/** Scale the image to fit in a square of the given size */\nfunction scaleToFit(bitmap: Bitmap, size: number): Bitmap {\n const scale = size / Math.max(bitmap.width, bitmap.height);\n\n if (scale === 1) {\n return bitmap;\n }\n\n // Resize a copy, the bitmap is the image's own\n const image = { bitmap: { ...bitmap, data: Buffer.from(bitmap.data) } };\n\n resize.resize(image as JimpClass, {\n w: Math.max(1, Math.round(bitmap.width * scale)),\n h: Math.max(1, Math.round(bitmap.height * scale)),\n });\n\n return image.bitmap;\n}\n\nfunction encode(bitmap: Bitmap, { sizes }: EncodeIcoOptions = {}) {\n const images = sizes?.length\n ? [\n ...new Set(\n sizes.map((size) =>\n Math.min(Math.max(Math.round(size), 1), MAX_SIZE),\n ),\n ),\n ].map((size) => scaleToFit(bitmap, size))\n : [\n Math.max(bitmap.width, bitmap.height) > MAX_SIZE\n ? scaleToFit(bitmap, MAX_SIZE)\n : bitmap,\n ];\n // Older versions of Windows only read 32 bit RGBA PNGs in icons\n const pngs = images.map((image) =>\n png().encode(image, { colorType: PNGColorType.COLOR_ALPHA }),\n );\n const directory = Buffer.alloc(DIRECTORY_SIZE + images.length * ENTRY_SIZE);\n let offset = directory.length;\n\n directory.writeUInt16LE(1, 2);\n directory.writeUInt16LE(images.length, 4);\n\n images.forEach((image, index) => {\n const entry = DIRECTORY_SIZE + index * ENTRY_SIZE;\n const data = pngs[index]!;\n\n // 0 means 256\n directory[entry] = image.width % MAX_SIZE;\n directory[entry + 1] = image.height % MAX_SIZE;\n // Planes and bits per pixel\n directory.writeUInt16LE(1, entry + 4);\n directory.writeUInt16LE(32, entry + 6);\n directory.writeUInt32LE(data.length, entry + 8);\n directory.writeUInt32LE(offset, entry + 12);\n offset += data.length;\n });\n\n return Buffer.concat([directory, ...pngs]);\n}\n\n/** The same format for the `image/vnd.microsoft.icon` MIME type */\nexport function msIcon() {\n return {\n mime: \"image/vnd.microsoft.icon\",\n hasAlpha: true,\n encode,\n decode,\n } satisfies Format<\"image/vnd.microsoft.icon\">;\n}\n\n/**\n * Reads ICO and CUR files and writes ICO files. Images are written as PNGs.\n */\nexport default function ico() {\n return {\n mime: \"image/x-icon\",\n hasAlpha: true,\n encode,\n decode,\n } satisfies Format<\"image/x-icon\">;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json CHANGED
@@ -1,6 +1,73 @@
1
1
  {
2
2
  "name": "@jimp/js-ico",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.1.0",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "repository": "jimp-dev/jimp",
8
+ "engines": {
9
+ "node": ">=22"
10
+ },
11
+ "scripts": {
12
+ "lint": "eslint .",
13
+ "test": "vitest",
14
+ "test:browser": "vitest --config vitest.config.browser.mjs",
15
+ "build": "tshy",
16
+ "dev": "tshy --watch",
17
+ "clean": "rimraf node_modules .tshy .tshy-build dist .turbo"
18
+ },
19
+ "author": "Andrew Lisowski <lisowski54@gmail.com>",
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "@jimp/config-eslint": "2.1.0",
23
+ "@jimp/config-typescript": "2.1.0",
24
+ "@jimp/config-vitest": "2.1.0",
25
+ "@jimp/core": "2.1.0",
26
+ "@jimp/test-utils": "2.1.0",
27
+ "@types/node": "^22.20.5",
28
+ "@vitest/browser": "^5.1.1",
29
+ "eslint": "^10.15.0",
30
+ "tshy": "~4.0.0",
31
+ "typescript": "^7.0.2",
32
+ "vite-plugin-node-polyfills": "^0.28.0",
33
+ "vitest": "^5.1.1"
34
+ },
35
+ "tshy": {
36
+ "exclude": [
37
+ "**/*.test.ts"
38
+ ],
39
+ "exports": {
40
+ "./package.json": "./package.json",
41
+ ".": "./src/index.ts"
42
+ }
43
+ },
44
+ "exports": {
45
+ "./package.json": "./package.json",
46
+ ".": {
47
+ "import": {
48
+ "types": "./dist/esm/index.d.ts",
49
+ "default": "./dist/esm/index.js"
50
+ },
51
+ "require": {
52
+ "types": "./dist/commonjs/index.d.ts",
53
+ "default": "./dist/commonjs/index.js"
54
+ }
55
+ }
56
+ },
57
+ "main": "./dist/commonjs/index.js",
58
+ "types": "./dist/commonjs/index.d.ts",
59
+ "type": "module",
60
+ "dependencies": {
61
+ "@jimp/js-png": "2.1.0",
62
+ "@jimp/plugin-resize": "2.1.0",
63
+ "@jimp/types": "2.1.0",
64
+ "bmp-ts": "^1.2.0",
65
+ "buffer": "^6.0.3"
66
+ },
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "sideEffects": false,
71
+ "module": "./dist/esm/index.js",
72
+ "gitHead": "8346e2329163e0c3ee26aaf8fe1001bac8b25a21"
73
+ }