@huh-david/bmp-js 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 @丝刀口
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 ADDED
@@ -0,0 +1,78 @@
1
+ # @huh-david/bmp-js
2
+
3
+ A pure TypeScript BMP encoder/decoder for Node.js.
4
+
5
+ ## Features
6
+
7
+ - Decoding for BMP bit depths: 1, 4, 8, 15, 16, 24, 32
8
+ - Decoding support for RLE-4 and RLE-8 compressed BMPs
9
+ - Encoding output as 24-bit BMP
10
+ - Dual package output: ESM + CommonJS
11
+ - First-class TypeScript types
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ pnpm add @huh-david/bmp-js
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### ESM
22
+
23
+ ```ts
24
+ import { decode, encode } from "@huh-david/bmp-js";
25
+ import { readFileSync, writeFileSync } from "node:fs";
26
+
27
+ const input = readFileSync("./image.bmp");
28
+ const decoded = decode(input);
29
+
30
+ const encoded = encode({
31
+ data: decoded.data,
32
+ width: decoded.width,
33
+ height: decoded.height,
34
+ });
35
+
36
+ writeFileSync("./roundtrip.bmp", encoded.data);
37
+ ```
38
+
39
+ ### CommonJS
40
+
41
+ ```js
42
+ const bmp = require("@huh-david/bmp-js");
43
+ const fs = require("node:fs");
44
+
45
+ const decoded = bmp.decode(fs.readFileSync("./image.bmp"));
46
+ const encoded = bmp.encode(decoded);
47
+
48
+ fs.writeFileSync("./roundtrip.bmp", encoded.data);
49
+ ```
50
+
51
+ ## Data layout
52
+
53
+ Decoded pixel data is a byte buffer in `ABGR` order.
54
+
55
+ - `A`: alpha
56
+ - `B`: blue
57
+ - `G`: green
58
+ - `R`: red
59
+
60
+ ## Development
61
+
62
+ ```bash
63
+ pnpm install
64
+ pnpm check
65
+ ```
66
+
67
+ Useful scripts:
68
+
69
+ - `pnpm build`
70
+ - `pnpm test`
71
+ - `pnpm test:watch`
72
+ - `pnpm lint`
73
+ - `pnpm format`
74
+ - `pnpm typecheck`
75
+
76
+ ## License
77
+
78
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,569 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ BmpDecoder: () => BmpDecoder,
24
+ BmpEncoder: () => BmpEncoder,
25
+ decode: () => decode,
26
+ default: () => index_default,
27
+ encode: () => encode
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/decoder.ts
32
+ var BmpDecoder = class {
33
+ pos = 0;
34
+ buffer;
35
+ isWithAlpha;
36
+ bottomUp = true;
37
+ maskRed = 0;
38
+ maskGreen = 0;
39
+ maskBlue = 0;
40
+ mask0 = 0;
41
+ fileSize;
42
+ reserved;
43
+ offset;
44
+ headerSize;
45
+ width;
46
+ height;
47
+ planes;
48
+ bitPP;
49
+ compress;
50
+ rawSize;
51
+ hr;
52
+ vr;
53
+ colors;
54
+ importantColors;
55
+ palette;
56
+ data;
57
+ constructor(buffer, isWithAlpha = false) {
58
+ this.buffer = buffer;
59
+ this.isWithAlpha = isWithAlpha;
60
+ const flag = this.buffer.toString("utf-8", 0, this.pos += 2);
61
+ if (flag !== "BM") {
62
+ throw new Error("Invalid BMP File");
63
+ }
64
+ this.parseHeader();
65
+ this.parseRGBA();
66
+ }
67
+ parseHeader() {
68
+ this.fileSize = this.buffer.readUInt32LE(this.pos);
69
+ this.pos += 4;
70
+ this.reserved = this.buffer.readUInt32LE(this.pos);
71
+ this.pos += 4;
72
+ this.offset = this.buffer.readUInt32LE(this.pos);
73
+ this.pos += 4;
74
+ this.headerSize = this.buffer.readUInt32LE(this.pos);
75
+ this.pos += 4;
76
+ this.width = this.buffer.readUInt32LE(this.pos);
77
+ this.pos += 4;
78
+ this.height = this.buffer.readInt32LE(this.pos);
79
+ this.pos += 4;
80
+ this.planes = this.buffer.readUInt16LE(this.pos);
81
+ this.pos += 2;
82
+ this.bitPP = this.buffer.readUInt16LE(this.pos);
83
+ this.pos += 2;
84
+ this.compress = this.buffer.readUInt32LE(this.pos);
85
+ this.pos += 4;
86
+ this.rawSize = this.buffer.readUInt32LE(this.pos);
87
+ this.pos += 4;
88
+ this.hr = this.buffer.readUInt32LE(this.pos);
89
+ this.pos += 4;
90
+ this.vr = this.buffer.readUInt32LE(this.pos);
91
+ this.pos += 4;
92
+ this.colors = this.buffer.readUInt32LE(this.pos);
93
+ this.pos += 4;
94
+ this.importantColors = this.buffer.readUInt32LE(this.pos);
95
+ this.pos += 4;
96
+ if (this.bitPP === 16 && this.isWithAlpha) {
97
+ this.bitPP = 15;
98
+ }
99
+ if (this.bitPP < 15) {
100
+ const len = this.colors === 0 ? 1 << this.bitPP : this.colors;
101
+ this.palette = new Array(len);
102
+ for (let i = 0; i < len; i += 1) {
103
+ const blue = this.buffer.readUInt8(this.pos++);
104
+ const green = this.buffer.readUInt8(this.pos++);
105
+ const red = this.buffer.readUInt8(this.pos++);
106
+ const quad = this.buffer.readUInt8(this.pos++);
107
+ this.palette[i] = { red, green, blue, quad };
108
+ }
109
+ }
110
+ if (this.height < 0) {
111
+ this.height *= -1;
112
+ this.bottomUp = false;
113
+ }
114
+ }
115
+ parseRGBA() {
116
+ const len = this.width * this.height * 4;
117
+ this.data = Buffer.alloc(len);
118
+ switch (this.bitPP) {
119
+ case 1:
120
+ this.bit1();
121
+ return;
122
+ case 4:
123
+ this.bit4();
124
+ return;
125
+ case 8:
126
+ this.bit8();
127
+ return;
128
+ case 15:
129
+ this.bit15();
130
+ return;
131
+ case 16:
132
+ this.bit16();
133
+ return;
134
+ case 24:
135
+ this.bit24();
136
+ return;
137
+ case 32:
138
+ this.bit32();
139
+ return;
140
+ default:
141
+ throw new Error(`Unsupported BMP bit depth: ${this.bitPP}`);
142
+ }
143
+ }
144
+ getPaletteColor(index) {
145
+ const color = this.palette?.[index];
146
+ if (color) {
147
+ return color;
148
+ }
149
+ return {
150
+ red: 255,
151
+ green: 255,
152
+ blue: 255,
153
+ quad: 0
154
+ };
155
+ }
156
+ bit1() {
157
+ const xLen = Math.ceil(this.width / 8);
158
+ const mode = xLen % 4;
159
+ for (let y = this.height - 1; y >= 0; y -= 1) {
160
+ const line = this.bottomUp ? y : this.height - 1 - y;
161
+ for (let x = 0; x < xLen; x += 1) {
162
+ const b = this.buffer.readUInt8(this.pos++);
163
+ const location = line * this.width * 4 + x * 8 * 4;
164
+ for (let i = 0; i < 8; i += 1) {
165
+ if (x * 8 + i >= this.width) {
166
+ break;
167
+ }
168
+ const rgb = this.getPaletteColor(b >> 7 - i & 1);
169
+ this.data[location + i * 4] = 0;
170
+ this.data[location + i * 4 + 1] = rgb.blue;
171
+ this.data[location + i * 4 + 2] = rgb.green;
172
+ this.data[location + i * 4 + 3] = rgb.red;
173
+ }
174
+ }
175
+ if (mode !== 0) {
176
+ this.pos += 4 - mode;
177
+ }
178
+ }
179
+ }
180
+ bit4() {
181
+ if (this.compress === 2) {
182
+ let setPixelData2 = function(rgbIndex) {
183
+ const rgb = this.getPaletteColor(rgbIndex);
184
+ this.data[location] = 0;
185
+ this.data[location + 1] = rgb.blue;
186
+ this.data[location + 2] = rgb.green;
187
+ this.data[location + 3] = rgb.red;
188
+ location += 4;
189
+ };
190
+ var setPixelData = setPixelData2;
191
+ this.data.fill(255);
192
+ let location = 0;
193
+ let lines = this.bottomUp ? this.height - 1 : 0;
194
+ let lowNibble = false;
195
+ while (location < this.data.length) {
196
+ const a = this.buffer.readUInt8(this.pos++);
197
+ const b = this.buffer.readUInt8(this.pos++);
198
+ if (a === 0) {
199
+ if (b === 0) {
200
+ lines += this.bottomUp ? -1 : 1;
201
+ location = lines * this.width * 4;
202
+ lowNibble = false;
203
+ continue;
204
+ }
205
+ if (b === 1) {
206
+ break;
207
+ }
208
+ if (b === 2) {
209
+ const x = this.buffer.readUInt8(this.pos++);
210
+ const y = this.buffer.readUInt8(this.pos++);
211
+ lines += this.bottomUp ? -y : y;
212
+ location += y * this.width * 4 + x * 4;
213
+ continue;
214
+ }
215
+ let c = this.buffer.readUInt8(this.pos++);
216
+ for (let i = 0; i < b; i += 1) {
217
+ if (lowNibble) {
218
+ setPixelData2.call(this, c & 15);
219
+ } else {
220
+ setPixelData2.call(this, (c & 240) >> 4);
221
+ }
222
+ if ((i & 1) === 1 && i + 1 < b) {
223
+ c = this.buffer.readUInt8(this.pos++);
224
+ }
225
+ lowNibble = !lowNibble;
226
+ }
227
+ if ((b + 1 >> 1 & 1) === 1) {
228
+ this.pos += 1;
229
+ }
230
+ } else {
231
+ for (let i = 0; i < a; i += 1) {
232
+ if (lowNibble) {
233
+ setPixelData2.call(this, b & 15);
234
+ } else {
235
+ setPixelData2.call(this, (b & 240) >> 4);
236
+ }
237
+ lowNibble = !lowNibble;
238
+ }
239
+ }
240
+ }
241
+ return;
242
+ }
243
+ const xLen = Math.ceil(this.width / 2);
244
+ const mode = xLen % 4;
245
+ for (let y = this.height - 1; y >= 0; y -= 1) {
246
+ const line = this.bottomUp ? y : this.height - 1 - y;
247
+ for (let x = 0; x < xLen; x += 1) {
248
+ const b = this.buffer.readUInt8(this.pos++);
249
+ const location = line * this.width * 4 + x * 2 * 4;
250
+ const before = b >> 4;
251
+ const after = b & 15;
252
+ let rgb = this.getPaletteColor(before);
253
+ this.data[location] = 0;
254
+ this.data[location + 1] = rgb.blue;
255
+ this.data[location + 2] = rgb.green;
256
+ this.data[location + 3] = rgb.red;
257
+ if (x * 2 + 1 >= this.width) {
258
+ break;
259
+ }
260
+ rgb = this.getPaletteColor(after);
261
+ this.data[location + 4] = 0;
262
+ this.data[location + 5] = rgb.blue;
263
+ this.data[location + 6] = rgb.green;
264
+ this.data[location + 7] = rgb.red;
265
+ }
266
+ if (mode !== 0) {
267
+ this.pos += 4 - mode;
268
+ }
269
+ }
270
+ }
271
+ bit8() {
272
+ if (this.compress === 1) {
273
+ let setPixelData2 = function(rgbIndex) {
274
+ const rgb = this.getPaletteColor(rgbIndex);
275
+ this.data[location] = 0;
276
+ this.data[location + 1] = rgb.blue;
277
+ this.data[location + 2] = rgb.green;
278
+ this.data[location + 3] = rgb.red;
279
+ location += 4;
280
+ };
281
+ var setPixelData = setPixelData2;
282
+ this.data.fill(255);
283
+ let location = 0;
284
+ let lines = this.bottomUp ? this.height - 1 : 0;
285
+ while (location < this.data.length) {
286
+ const a = this.buffer.readUInt8(this.pos++);
287
+ const b = this.buffer.readUInt8(this.pos++);
288
+ if (a === 0) {
289
+ if (b === 0) {
290
+ lines += this.bottomUp ? -1 : 1;
291
+ location = lines * this.width * 4;
292
+ continue;
293
+ }
294
+ if (b === 1) {
295
+ break;
296
+ }
297
+ if (b === 2) {
298
+ const x = this.buffer.readUInt8(this.pos++);
299
+ const y = this.buffer.readUInt8(this.pos++);
300
+ lines += this.bottomUp ? -y : y;
301
+ location += y * this.width * 4 + x * 4;
302
+ continue;
303
+ }
304
+ for (let i = 0; i < b; i += 1) {
305
+ const c = this.buffer.readUInt8(this.pos++);
306
+ setPixelData2.call(this, c);
307
+ }
308
+ if ((b & 1) === 1) {
309
+ this.pos += 1;
310
+ }
311
+ } else {
312
+ for (let i = 0; i < a; i += 1) {
313
+ setPixelData2.call(this, b);
314
+ }
315
+ }
316
+ }
317
+ return;
318
+ }
319
+ const mode = this.width % 4;
320
+ for (let y = this.height - 1; y >= 0; y -= 1) {
321
+ const line = this.bottomUp ? y : this.height - 1 - y;
322
+ for (let x = 0; x < this.width; x += 1) {
323
+ const b = this.buffer.readUInt8(this.pos++);
324
+ const location = line * this.width * 4 + x * 4;
325
+ const rgb = this.getPaletteColor(b);
326
+ this.data[location] = 0;
327
+ this.data[location + 1] = rgb.blue;
328
+ this.data[location + 2] = rgb.green;
329
+ this.data[location + 3] = rgb.red;
330
+ }
331
+ if (mode !== 0) {
332
+ this.pos += 4 - mode;
333
+ }
334
+ }
335
+ }
336
+ bit15() {
337
+ const difW = this.width % 3;
338
+ const m = 31;
339
+ for (let y = this.height - 1; y >= 0; y -= 1) {
340
+ const line = this.bottomUp ? y : this.height - 1 - y;
341
+ for (let x = 0; x < this.width; x += 1) {
342
+ const value = this.buffer.readUInt16LE(this.pos);
343
+ this.pos += 2;
344
+ const blue = (value & m) / m * 255;
345
+ const green = (value >> 5 & m) / m * 255;
346
+ const red = (value >> 10 & m) / m * 255;
347
+ const alpha = value >> 15 !== 0 ? 255 : 0;
348
+ const location = line * this.width * 4 + x * 4;
349
+ this.data[location] = alpha;
350
+ this.data[location + 1] = blue | 0;
351
+ this.data[location + 2] = green | 0;
352
+ this.data[location + 3] = red | 0;
353
+ }
354
+ this.pos += difW;
355
+ }
356
+ }
357
+ bit16() {
358
+ const difW = this.width % 2 * 2;
359
+ this.maskRed = 31744;
360
+ this.maskGreen = 992;
361
+ this.maskBlue = 31;
362
+ this.mask0 = 0;
363
+ if (this.compress === 3) {
364
+ this.maskRed = this.buffer.readUInt32LE(this.pos);
365
+ this.pos += 4;
366
+ this.maskGreen = this.buffer.readUInt32LE(this.pos);
367
+ this.pos += 4;
368
+ this.maskBlue = this.buffer.readUInt32LE(this.pos);
369
+ this.pos += 4;
370
+ this.mask0 = this.buffer.readUInt32LE(this.pos);
371
+ this.pos += 4;
372
+ }
373
+ const ns = [0, 0, 0];
374
+ for (let i = 0; i < 16; i += 1) {
375
+ if ((this.maskRed >> i & 1) !== 0) ns[0] += 1;
376
+ if ((this.maskGreen >> i & 1) !== 0) ns[1] += 1;
377
+ if ((this.maskBlue >> i & 1) !== 0) ns[2] += 1;
378
+ }
379
+ ns[1] += ns[0];
380
+ ns[2] += ns[1];
381
+ ns[0] = 8 - ns[0];
382
+ ns[1] -= 8;
383
+ ns[2] -= 8;
384
+ for (let y = this.height - 1; y >= 0; y -= 1) {
385
+ const line = this.bottomUp ? y : this.height - 1 - y;
386
+ for (let x = 0; x < this.width; x += 1) {
387
+ const value = this.buffer.readUInt16LE(this.pos);
388
+ this.pos += 2;
389
+ const blue = (value & this.maskBlue) << ns[0];
390
+ const green = (value & this.maskGreen) >> ns[1];
391
+ const red = (value & this.maskRed) >> ns[2];
392
+ const location = line * this.width * 4 + x * 4;
393
+ this.data[location] = 0;
394
+ this.data[location + 1] = blue;
395
+ this.data[location + 2] = green;
396
+ this.data[location + 3] = red;
397
+ }
398
+ this.pos += difW;
399
+ }
400
+ }
401
+ bit24() {
402
+ for (let y = this.height - 1; y >= 0; y -= 1) {
403
+ const line = this.bottomUp ? y : this.height - 1 - y;
404
+ for (let x = 0; x < this.width; x += 1) {
405
+ const blue = this.buffer.readUInt8(this.pos++);
406
+ const green = this.buffer.readUInt8(this.pos++);
407
+ const red = this.buffer.readUInt8(this.pos++);
408
+ const location = line * this.width * 4 + x * 4;
409
+ this.data[location] = 0;
410
+ this.data[location + 1] = blue;
411
+ this.data[location + 2] = green;
412
+ this.data[location + 3] = red;
413
+ }
414
+ this.pos += this.width % 4;
415
+ }
416
+ }
417
+ bit32() {
418
+ if (this.compress === 3) {
419
+ this.maskRed = this.buffer.readUInt32LE(this.pos);
420
+ this.pos += 4;
421
+ this.maskGreen = this.buffer.readUInt32LE(this.pos);
422
+ this.pos += 4;
423
+ this.maskBlue = this.buffer.readUInt32LE(this.pos);
424
+ this.pos += 4;
425
+ this.mask0 = this.buffer.readUInt32LE(this.pos);
426
+ this.pos += 4;
427
+ for (let y = this.height - 1; y >= 0; y -= 1) {
428
+ const line = this.bottomUp ? y : this.height - 1 - y;
429
+ for (let x = 0; x < this.width; x += 1) {
430
+ const alpha = this.buffer.readUInt8(this.pos++);
431
+ const blue = this.buffer.readUInt8(this.pos++);
432
+ const green = this.buffer.readUInt8(this.pos++);
433
+ const red = this.buffer.readUInt8(this.pos++);
434
+ const location = line * this.width * 4 + x * 4;
435
+ this.data[location] = alpha;
436
+ this.data[location + 1] = blue;
437
+ this.data[location + 2] = green;
438
+ this.data[location + 3] = red;
439
+ }
440
+ }
441
+ return;
442
+ }
443
+ for (let y = this.height - 1; y >= 0; y -= 1) {
444
+ const line = this.bottomUp ? y : this.height - 1 - y;
445
+ for (let x = 0; x < this.width; x += 1) {
446
+ const blue = this.buffer.readUInt8(this.pos++);
447
+ const green = this.buffer.readUInt8(this.pos++);
448
+ const red = this.buffer.readUInt8(this.pos++);
449
+ const alpha = this.buffer.readUInt8(this.pos++);
450
+ const location = line * this.width * 4 + x * 4;
451
+ this.data[location] = alpha;
452
+ this.data[location + 1] = blue;
453
+ this.data[location + 2] = green;
454
+ this.data[location + 3] = red;
455
+ }
456
+ }
457
+ }
458
+ getData() {
459
+ return this.data;
460
+ }
461
+ };
462
+ function decode(bmpData) {
463
+ return new BmpDecoder(bmpData);
464
+ }
465
+
466
+ // src/encoder.ts
467
+ var BmpEncoder = class {
468
+ buffer;
469
+ width;
470
+ height;
471
+ extraBytes;
472
+ rgbSize;
473
+ headerInfoSize;
474
+ flag = "BM";
475
+ reserved = 0;
476
+ offset = 54;
477
+ fileSize;
478
+ planes = 1;
479
+ bitPP = 24;
480
+ compress = 0;
481
+ hr = 0;
482
+ vr = 0;
483
+ colors = 0;
484
+ importantColors = 0;
485
+ pos = 0;
486
+ constructor(imgData) {
487
+ this.buffer = imgData.data;
488
+ this.width = imgData.width;
489
+ this.height = imgData.height;
490
+ this.extraBytes = this.width % 4;
491
+ this.rgbSize = this.height * (3 * this.width + this.extraBytes);
492
+ this.headerInfoSize = 40;
493
+ this.fileSize = this.rgbSize + this.offset;
494
+ }
495
+ encode() {
496
+ const tempBuffer = Buffer.alloc(this.offset + this.rgbSize);
497
+ tempBuffer.write(this.flag, this.pos, 2);
498
+ this.pos += 2;
499
+ tempBuffer.writeUInt32LE(this.fileSize, this.pos);
500
+ this.pos += 4;
501
+ tempBuffer.writeUInt32LE(this.reserved, this.pos);
502
+ this.pos += 4;
503
+ tempBuffer.writeUInt32LE(this.offset, this.pos);
504
+ this.pos += 4;
505
+ tempBuffer.writeUInt32LE(this.headerInfoSize, this.pos);
506
+ this.pos += 4;
507
+ tempBuffer.writeUInt32LE(this.width, this.pos);
508
+ this.pos += 4;
509
+ tempBuffer.writeInt32LE(-this.height, this.pos);
510
+ this.pos += 4;
511
+ tempBuffer.writeUInt16LE(this.planes, this.pos);
512
+ this.pos += 2;
513
+ tempBuffer.writeUInt16LE(this.bitPP, this.pos);
514
+ this.pos += 2;
515
+ tempBuffer.writeUInt32LE(this.compress, this.pos);
516
+ this.pos += 4;
517
+ tempBuffer.writeUInt32LE(this.rgbSize, this.pos);
518
+ this.pos += 4;
519
+ tempBuffer.writeUInt32LE(this.hr, this.pos);
520
+ this.pos += 4;
521
+ tempBuffer.writeUInt32LE(this.vr, this.pos);
522
+ this.pos += 4;
523
+ tempBuffer.writeUInt32LE(this.colors, this.pos);
524
+ this.pos += 4;
525
+ tempBuffer.writeUInt32LE(this.importantColors, this.pos);
526
+ this.pos += 4;
527
+ let i = 0;
528
+ const rowBytes = 3 * this.width + this.extraBytes;
529
+ for (let y = 0; y < this.height; y += 1) {
530
+ for (let x = 0; x < this.width; x += 1) {
531
+ const p = this.pos + y * rowBytes + x * 3;
532
+ i += 1;
533
+ tempBuffer[p] = this.buffer.readUInt8(i++);
534
+ tempBuffer[p + 1] = this.buffer.readUInt8(i++);
535
+ tempBuffer[p + 2] = this.buffer.readUInt8(i++);
536
+ }
537
+ if (this.extraBytes > 0) {
538
+ const fillOffset = this.pos + y * rowBytes + this.width * 3;
539
+ tempBuffer.fill(0, fillOffset, fillOffset + this.extraBytes);
540
+ }
541
+ }
542
+ return tempBuffer;
543
+ }
544
+ };
545
+ function encode(imgData, quality = 100) {
546
+ void quality;
547
+ const encoder = new BmpEncoder(imgData);
548
+ const data = encoder.encode();
549
+ return {
550
+ data,
551
+ width: imgData.width,
552
+ height: imgData.height
553
+ };
554
+ }
555
+
556
+ // src/index.ts
557
+ var bmp = {
558
+ encode,
559
+ decode
560
+ };
561
+ var index_default = bmp;
562
+ // Annotate the CommonJS export names for ESM import in node:
563
+ 0 && (module.exports = {
564
+ BmpDecoder,
565
+ BmpEncoder,
566
+ decode,
567
+ encode
568
+ });
569
+ //# sourceMappingURL=index.cjs.map