@huh-david/bmp-js 0.5.0 → 0.6.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.
@@ -0,0 +1,832 @@
1
+ // src/binary.ts
2
+ function toUint8Array(input) {
3
+ if (input instanceof ArrayBuffer) {
4
+ return new Uint8Array(input);
5
+ }
6
+ return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
7
+ }
8
+ function assertInteger(name, value) {
9
+ if (!Number.isInteger(value) || value <= 0) {
10
+ throw new Error(`${name} must be a positive integer`);
11
+ }
12
+ }
13
+
14
+ // src/decoder.ts
15
+ var FILE_HEADER_SIZE = 14;
16
+ var INFO_HEADER_MIN = 40;
17
+ var CORE_HEADER_SIZE = 12;
18
+ function rowStride(width, bitPP) {
19
+ return Math.floor((bitPP * width + 31) / 32) * 4;
20
+ }
21
+ var BmpDecoder = class {
22
+ pos = 0;
23
+ bytes;
24
+ view;
25
+ options;
26
+ bottomUp = true;
27
+ dibStart = FILE_HEADER_SIZE;
28
+ paletteEntrySize = 4;
29
+ externalMaskOffset = 0;
30
+ maskRed = 0;
31
+ maskGreen = 0;
32
+ maskBlue = 0;
33
+ maskAlpha = 0;
34
+ fileSize;
35
+ reserved;
36
+ offset;
37
+ headerSize;
38
+ width;
39
+ height;
40
+ planes;
41
+ bitPP;
42
+ compress;
43
+ rawSize;
44
+ hr;
45
+ vr;
46
+ colors;
47
+ importantColors;
48
+ palette;
49
+ data;
50
+ constructor(input, options = {}) {
51
+ this.bytes = toUint8Array(input);
52
+ this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength);
53
+ this.options = {
54
+ treat16BitAs15BitAlpha: options.treat16BitAs15BitAlpha ?? false,
55
+ toRGBA: options.toRGBA ?? false
56
+ };
57
+ this.parseFileHeader();
58
+ this.parseDibHeader();
59
+ this.parsePalette();
60
+ this.pos = this.offset;
61
+ this.parseRGBA();
62
+ this.transformToRgbaIfNeeded();
63
+ }
64
+ ensureReadable(offset, size, context) {
65
+ if (offset < 0 || size < 0 || offset + size > this.bytes.length) {
66
+ throw new Error(`BMP decode out-of-range while reading ${context}`);
67
+ }
68
+ }
69
+ readUInt8(offset = this.pos) {
70
+ this.ensureReadable(offset, 1, "uint8");
71
+ if (offset === this.pos) this.pos += 1;
72
+ return this.view.getUint8(offset);
73
+ }
74
+ readUInt16LE(offset = this.pos) {
75
+ this.ensureReadable(offset, 2, "uint16");
76
+ if (offset === this.pos) this.pos += 2;
77
+ return this.view.getUint16(offset, true);
78
+ }
79
+ readInt16LE(offset = this.pos) {
80
+ this.ensureReadable(offset, 2, "int16");
81
+ if (offset === this.pos) this.pos += 2;
82
+ return this.view.getInt16(offset, true);
83
+ }
84
+ readUInt32LE(offset = this.pos) {
85
+ this.ensureReadable(offset, 4, "uint32");
86
+ if (offset === this.pos) this.pos += 4;
87
+ return this.view.getUint32(offset, true);
88
+ }
89
+ readInt32LE(offset = this.pos) {
90
+ this.ensureReadable(offset, 4, "int32");
91
+ if (offset === this.pos) this.pos += 4;
92
+ return this.view.getInt32(offset, true);
93
+ }
94
+ parseFileHeader() {
95
+ this.ensureReadable(0, FILE_HEADER_SIZE, "file header");
96
+ if (this.bytes[0] !== 66 || this.bytes[1] !== 77) {
97
+ throw new Error("Invalid BMP file signature");
98
+ }
99
+ this.pos = 2;
100
+ this.fileSize = this.readUInt32LE();
101
+ this.reserved = this.readUInt32LE();
102
+ this.offset = this.readUInt32LE();
103
+ if (this.offset < FILE_HEADER_SIZE || this.offset > this.bytes.length) {
104
+ throw new Error(`Invalid pixel data offset: ${this.offset}`);
105
+ }
106
+ }
107
+ parseDibHeader() {
108
+ this.pos = this.dibStart;
109
+ this.headerSize = this.readUInt32LE();
110
+ if (this.headerSize < CORE_HEADER_SIZE) {
111
+ throw new Error(`Unsupported DIB header size: ${this.headerSize}`);
112
+ }
113
+ this.ensureReadable(this.dibStart, this.headerSize, "DIB header");
114
+ if (this.headerSize === CORE_HEADER_SIZE) {
115
+ this.parseCoreHeader();
116
+ return;
117
+ }
118
+ if (this.headerSize < INFO_HEADER_MIN) {
119
+ throw new Error(`Unsupported DIB header size: ${this.headerSize}`);
120
+ }
121
+ this.parseInfoHeader();
122
+ }
123
+ parseCoreHeader() {
124
+ const width = this.readUInt16LE(this.dibStart + 4);
125
+ const height = this.readUInt16LE(this.dibStart + 6);
126
+ this.width = width;
127
+ this.height = height;
128
+ this.planes = this.readUInt16LE(this.dibStart + 8);
129
+ this.bitPP = this.readUInt16LE(this.dibStart + 10);
130
+ this.compress = 0;
131
+ this.rawSize = 0;
132
+ this.hr = 0;
133
+ this.vr = 0;
134
+ this.colors = 0;
135
+ this.importantColors = 0;
136
+ this.bottomUp = true;
137
+ this.paletteEntrySize = 3;
138
+ this.externalMaskOffset = this.dibStart + this.headerSize;
139
+ this.validateDimensions();
140
+ }
141
+ parseInfoHeader() {
142
+ const rawWidth = this.readInt32LE(this.dibStart + 4);
143
+ const rawHeight = this.readInt32LE(this.dibStart + 8);
144
+ this.width = rawWidth;
145
+ this.height = rawHeight;
146
+ this.planes = this.readUInt16LE(this.dibStart + 12);
147
+ this.bitPP = this.readUInt16LE(this.dibStart + 14);
148
+ this.compress = this.readUInt32LE(this.dibStart + 16);
149
+ this.rawSize = this.readUInt32LE(this.dibStart + 20);
150
+ this.hr = this.readUInt32LE(this.dibStart + 24);
151
+ this.vr = this.readUInt32LE(this.dibStart + 28);
152
+ this.colors = this.readUInt32LE(this.dibStart + 32);
153
+ this.importantColors = this.readUInt32LE(this.dibStart + 36);
154
+ this.paletteEntrySize = 4;
155
+ this.externalMaskOffset = this.dibStart + this.headerSize;
156
+ if (this.height < 0) {
157
+ this.height *= -1;
158
+ this.bottomUp = false;
159
+ }
160
+ if (this.width < 0) {
161
+ this.width *= -1;
162
+ }
163
+ if (this.bitPP === 16 && this.options.treat16BitAs15BitAlpha) {
164
+ this.bitPP = 15;
165
+ }
166
+ this.validateDimensions();
167
+ this.parseBitMasks();
168
+ }
169
+ validateDimensions() {
170
+ if (!Number.isInteger(this.width) || !Number.isInteger(this.height) || this.width <= 0 || this.height <= 0) {
171
+ throw new Error(`Invalid BMP dimensions: ${this.width}x${this.height}`);
172
+ }
173
+ }
174
+ parseBitMasks() {
175
+ if (!(this.bitPP === 16 || this.bitPP === 32) || !(this.compress === 3 || this.compress === 6)) {
176
+ return;
177
+ }
178
+ const inHeaderMaskStart = this.dibStart + 40;
179
+ const hasMasksInHeader = this.headerSize >= 52;
180
+ const maskStart = hasMasksInHeader ? inHeaderMaskStart : this.externalMaskOffset;
181
+ const maskCount = this.compress === 6 || this.headerSize >= 56 ? 4 : 3;
182
+ this.ensureReadable(maskStart, maskCount * 4, "bit masks");
183
+ this.maskRed = this.readUInt32LE(maskStart);
184
+ this.maskGreen = this.readUInt32LE(maskStart + 4);
185
+ this.maskBlue = this.readUInt32LE(maskStart + 8);
186
+ this.maskAlpha = maskCount >= 4 ? this.readUInt32LE(maskStart + 12) : 0;
187
+ if (!hasMasksInHeader) {
188
+ this.externalMaskOffset += maskCount * 4;
189
+ }
190
+ }
191
+ parsePalette() {
192
+ if (this.bitPP >= 16) {
193
+ return;
194
+ }
195
+ const colorCount = this.colors === 0 ? 1 << this.bitPP : this.colors;
196
+ if (colorCount <= 0) {
197
+ return;
198
+ }
199
+ const paletteStart = this.externalMaskOffset;
200
+ const paletteSize = colorCount * this.paletteEntrySize;
201
+ if (paletteStart + paletteSize > this.offset) {
202
+ throw new Error("Palette data overlaps or exceeds pixel data offset");
203
+ }
204
+ this.palette = new Array(colorCount);
205
+ for (let i = 0; i < colorCount; i += 1) {
206
+ const base = paletteStart + i * this.paletteEntrySize;
207
+ const blue = this.readUInt8(base);
208
+ const green = this.readUInt8(base + 1);
209
+ const red = this.readUInt8(base + 2);
210
+ const quad = this.paletteEntrySize === 4 ? this.readUInt8(base + 3) : 0;
211
+ this.palette[i] = { red, green, blue, quad };
212
+ }
213
+ }
214
+ parseRGBA() {
215
+ const pixelCount = this.width * this.height;
216
+ const len = pixelCount * 4;
217
+ this.data = new Uint8Array(len);
218
+ switch (this.bitPP) {
219
+ case 1:
220
+ this.bit1();
221
+ return;
222
+ case 4:
223
+ this.bit4();
224
+ return;
225
+ case 8:
226
+ this.bit8();
227
+ return;
228
+ case 15:
229
+ this.bit15();
230
+ return;
231
+ case 16:
232
+ this.bit16();
233
+ return;
234
+ case 24:
235
+ this.bit24();
236
+ return;
237
+ case 32:
238
+ this.bit32();
239
+ return;
240
+ default:
241
+ throw new Error(`Unsupported BMP bit depth: ${this.bitPP}`);
242
+ }
243
+ }
244
+ transformToRgbaIfNeeded() {
245
+ if (!this.options.toRGBA) {
246
+ return;
247
+ }
248
+ for (let i = 0; i < this.data.length; i += 4) {
249
+ const alpha = this.data[i] ?? 0;
250
+ const blue = this.data[i + 1] ?? 0;
251
+ const green = this.data[i + 2] ?? 0;
252
+ const red = this.data[i + 3] ?? 0;
253
+ this.data[i] = red;
254
+ this.data[i + 1] = green;
255
+ this.data[i + 2] = blue;
256
+ this.data[i + 3] = alpha;
257
+ }
258
+ }
259
+ getPaletteColor(index) {
260
+ const color = this.palette?.[index];
261
+ if (color) {
262
+ return color;
263
+ }
264
+ return { red: 255, green: 255, blue: 255, quad: 0 };
265
+ }
266
+ setPixel(destY, x, alpha, blue, green, red) {
267
+ const base = (destY * this.width + x) * 4;
268
+ this.data[base] = alpha;
269
+ this.data[base + 1] = blue;
270
+ this.data[base + 2] = green;
271
+ this.data[base + 3] = red;
272
+ }
273
+ bit1() {
274
+ const stride = rowStride(this.width, 1);
275
+ const bytesPerRow = Math.ceil(this.width / 8);
276
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
277
+ const rowStart = this.offset + srcRow * stride;
278
+ this.ensureReadable(rowStart, bytesPerRow, "1-bit row");
279
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
280
+ for (let x = 0; x < this.width; x += 1) {
281
+ const packed = this.readUInt8(rowStart + Math.floor(x / 8));
282
+ const bit = packed >> 7 - x % 8 & 1;
283
+ const rgb = this.getPaletteColor(bit);
284
+ this.setPixel(destY, x, 255, rgb.blue, rgb.green, rgb.red);
285
+ }
286
+ }
287
+ }
288
+ bit4() {
289
+ if (this.compress === 2) {
290
+ this.bit4Rle();
291
+ return;
292
+ }
293
+ const stride = rowStride(this.width, 4);
294
+ const bytesPerRow = Math.ceil(this.width / 2);
295
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
296
+ const rowStart = this.offset + srcRow * stride;
297
+ this.ensureReadable(rowStart, bytesPerRow, "4-bit row");
298
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
299
+ for (let x = 0; x < this.width; x += 1) {
300
+ const packed = this.readUInt8(rowStart + Math.floor(x / 2));
301
+ const idx = x % 2 === 0 ? (packed & 240) >> 4 : packed & 15;
302
+ const rgb = this.getPaletteColor(idx);
303
+ this.setPixel(destY, x, 255, rgb.blue, rgb.green, rgb.red);
304
+ }
305
+ }
306
+ }
307
+ bit8() {
308
+ if (this.compress === 1) {
309
+ this.bit8Rle();
310
+ return;
311
+ }
312
+ const stride = rowStride(this.width, 8);
313
+ const bytesPerRow = this.width;
314
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
315
+ const rowStart = this.offset + srcRow * stride;
316
+ this.ensureReadable(rowStart, bytesPerRow, "8-bit row");
317
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
318
+ for (let x = 0; x < this.width; x += 1) {
319
+ const idx = this.readUInt8(rowStart + x);
320
+ const rgb = this.getPaletteColor(idx);
321
+ this.setPixel(destY, x, 255, rgb.blue, rgb.green, rgb.red);
322
+ }
323
+ }
324
+ }
325
+ bit15() {
326
+ const stride = rowStride(this.width, 16);
327
+ const max = 31;
328
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
329
+ const rowStart = this.offset + srcRow * stride;
330
+ this.ensureReadable(rowStart, this.width * 2, "15-bit row");
331
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
332
+ for (let x = 0; x < this.width; x += 1) {
333
+ const value = this.readUInt16LE(rowStart + x * 2);
334
+ const blue = (value >> 0 & max) / max * 255;
335
+ const green = (value >> 5 & max) / max * 255;
336
+ const red = (value >> 10 & max) / max * 255;
337
+ const alpha = (value & 32768) !== 0 ? 255 : 0;
338
+ this.setPixel(destY, x, alpha, blue | 0, green | 0, red | 0);
339
+ }
340
+ }
341
+ }
342
+ scaleMasked(value, mask) {
343
+ if (mask === 0) return 0;
344
+ let shift = 0;
345
+ let bits = 0;
346
+ let m = mask;
347
+ while ((m & 1) === 0) {
348
+ shift += 1;
349
+ m >>>= 1;
350
+ }
351
+ while ((m & 1) === 1) {
352
+ bits += 1;
353
+ m >>>= 1;
354
+ }
355
+ const component = (value & mask) >>> shift;
356
+ if (bits >= 8) {
357
+ return component >>> bits - 8;
358
+ }
359
+ return component << 8 - bits & 255;
360
+ }
361
+ bit16() {
362
+ if (this.maskRed === 0 && this.maskGreen === 0 && this.maskBlue === 0) {
363
+ this.maskRed = 31744;
364
+ this.maskGreen = 992;
365
+ this.maskBlue = 31;
366
+ }
367
+ const stride = rowStride(this.width, 16);
368
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
369
+ const rowStart = this.offset + srcRow * stride;
370
+ this.ensureReadable(rowStart, this.width * 2, "16-bit row");
371
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
372
+ for (let x = 0; x < this.width; x += 1) {
373
+ const value = this.readUInt16LE(rowStart + x * 2);
374
+ const blue = this.scaleMasked(value, this.maskBlue);
375
+ const green = this.scaleMasked(value, this.maskGreen);
376
+ const red = this.scaleMasked(value, this.maskRed);
377
+ const alpha = this.maskAlpha !== 0 ? this.scaleMasked(value, this.maskAlpha) : 255;
378
+ this.setPixel(destY, x, alpha, blue, green, red);
379
+ }
380
+ }
381
+ }
382
+ bit24() {
383
+ const stride = rowStride(this.width, 24);
384
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
385
+ const rowStart = this.offset + srcRow * stride;
386
+ this.ensureReadable(rowStart, this.width * 3, "24-bit row");
387
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
388
+ for (let x = 0; x < this.width; x += 1) {
389
+ const base = rowStart + x * 3;
390
+ const blue = this.readUInt8(base);
391
+ const green = this.readUInt8(base + 1);
392
+ const red = this.readUInt8(base + 2);
393
+ this.setPixel(destY, x, 255, blue, green, red);
394
+ }
395
+ }
396
+ }
397
+ bit32() {
398
+ const stride = rowStride(this.width, 32);
399
+ for (let srcRow = 0; srcRow < this.height; srcRow += 1) {
400
+ const rowStart = this.offset + srcRow * stride;
401
+ this.ensureReadable(rowStart, this.width * 4, "32-bit row");
402
+ const destY = this.bottomUp ? this.height - 1 - srcRow : srcRow;
403
+ for (let x = 0; x < this.width; x += 1) {
404
+ const base = rowStart + x * 4;
405
+ if (this.compress === 3 || this.compress === 6) {
406
+ const pixel = this.readUInt32LE(base);
407
+ const red = this.scaleMasked(pixel, this.maskRed || 16711680);
408
+ const green = this.scaleMasked(pixel, this.maskGreen || 65280);
409
+ const blue = this.scaleMasked(pixel, this.maskBlue || 255);
410
+ const alpha = this.maskAlpha === 0 ? 255 : this.scaleMasked(pixel, this.maskAlpha);
411
+ this.setPixel(destY, x, alpha, blue, green, red);
412
+ } else {
413
+ const blue = this.readUInt8(base);
414
+ const green = this.readUInt8(base + 1);
415
+ const red = this.readUInt8(base + 2);
416
+ const alpha = this.readUInt8(base + 3);
417
+ this.setPixel(destY, x, alpha, blue, green, red);
418
+ }
419
+ }
420
+ }
421
+ }
422
+ bit8Rle() {
423
+ this.data.fill(255);
424
+ this.pos = this.offset;
425
+ let x = 0;
426
+ let y = this.bottomUp ? this.height - 1 : 0;
427
+ while (this.pos < this.bytes.length) {
428
+ const count = this.readUInt8();
429
+ const value = this.readUInt8();
430
+ if (count === 0) {
431
+ if (value === 0) {
432
+ x = 0;
433
+ y += this.bottomUp ? -1 : 1;
434
+ continue;
435
+ }
436
+ if (value === 1) {
437
+ break;
438
+ }
439
+ if (value === 2) {
440
+ x += this.readUInt8();
441
+ y += this.bottomUp ? -this.readUInt8() : this.readUInt8();
442
+ continue;
443
+ }
444
+ for (let i = 0; i < value; i += 1) {
445
+ const idx = this.readUInt8();
446
+ const rgb2 = this.getPaletteColor(idx);
447
+ if (x < this.width && y >= 0 && y < this.height) {
448
+ this.setPixel(y, x, 255, rgb2.blue, rgb2.green, rgb2.red);
449
+ }
450
+ x += 1;
451
+ }
452
+ if ((value & 1) === 1) {
453
+ this.pos += 1;
454
+ }
455
+ continue;
456
+ }
457
+ const rgb = this.getPaletteColor(value);
458
+ for (let i = 0; i < count; i += 1) {
459
+ if (x < this.width && y >= 0 && y < this.height) {
460
+ this.setPixel(y, x, 255, rgb.blue, rgb.green, rgb.red);
461
+ }
462
+ x += 1;
463
+ }
464
+ }
465
+ }
466
+ bit4Rle() {
467
+ this.data.fill(255);
468
+ this.pos = this.offset;
469
+ let x = 0;
470
+ let y = this.bottomUp ? this.height - 1 : 0;
471
+ while (this.pos < this.bytes.length) {
472
+ const count = this.readUInt8();
473
+ const value = this.readUInt8();
474
+ if (count === 0) {
475
+ if (value === 0) {
476
+ x = 0;
477
+ y += this.bottomUp ? -1 : 1;
478
+ continue;
479
+ }
480
+ if (value === 1) {
481
+ break;
482
+ }
483
+ if (value === 2) {
484
+ x += this.readUInt8();
485
+ y += this.bottomUp ? -this.readUInt8() : this.readUInt8();
486
+ continue;
487
+ }
488
+ let current = this.readUInt8();
489
+ for (let i = 0; i < value; i += 1) {
490
+ const nibble = i % 2 === 0 ? (current & 240) >> 4 : current & 15;
491
+ const rgb = this.getPaletteColor(nibble);
492
+ if (x < this.width && y >= 0 && y < this.height) {
493
+ this.setPixel(y, x, 255, rgb.blue, rgb.green, rgb.red);
494
+ }
495
+ x += 1;
496
+ if (i % 2 === 1 && i + 1 < value) {
497
+ current = this.readUInt8();
498
+ }
499
+ }
500
+ if ((value + 1 >> 1 & 1) === 1) {
501
+ this.pos += 1;
502
+ }
503
+ continue;
504
+ }
505
+ for (let i = 0; i < count; i += 1) {
506
+ const nibble = i % 2 === 0 ? (value & 240) >> 4 : value & 15;
507
+ const rgb = this.getPaletteColor(nibble);
508
+ if (x < this.width && y >= 0 && y < this.height) {
509
+ this.setPixel(y, x, 255, rgb.blue, rgb.green, rgb.red);
510
+ }
511
+ x += 1;
512
+ }
513
+ }
514
+ }
515
+ getData() {
516
+ return this.data;
517
+ }
518
+ };
519
+ function decode(bmpData, options) {
520
+ return new BmpDecoder(bmpData, options);
521
+ }
522
+
523
+ // src/encoder.ts
524
+ var FILE_HEADER_SIZE2 = 14;
525
+ var INFO_HEADER_SIZE = 40;
526
+ var BYTES_PER_PIXEL_ABGR = 4;
527
+ var SUPPORTED_BIT_DEPTHS = [1, 4, 8, 16, 24, 32];
528
+ function isSupportedBitDepth(value) {
529
+ return SUPPORTED_BIT_DEPTHS.includes(value);
530
+ }
531
+ function normalizeEncodeOptions(qualityOrOptions) {
532
+ if (typeof qualityOrOptions === "number" || typeof qualityOrOptions === "undefined") {
533
+ return {
534
+ orientation: "top-down",
535
+ bitPP: 24,
536
+ palette: []
537
+ };
538
+ }
539
+ return {
540
+ orientation: qualityOrOptions.orientation ?? "top-down",
541
+ bitPP: qualityOrOptions.bitPP ?? 24,
542
+ palette: qualityOrOptions.palette ?? []
543
+ };
544
+ }
545
+ var BmpEncoder = class {
546
+ pixelData;
547
+ width;
548
+ height;
549
+ options;
550
+ palette;
551
+ exactPaletteIndex = /* @__PURE__ */ new Map();
552
+ constructor(imgData, options) {
553
+ this.pixelData = imgData.data;
554
+ this.width = imgData.width;
555
+ this.height = imgData.height;
556
+ this.options = options;
557
+ this.palette = this.normalizePalette(options);
558
+ assertInteger("width", this.width);
559
+ assertInteger("height", this.height);
560
+ if (!isSupportedBitDepth(this.options.bitPP)) {
561
+ throw new Error(
562
+ `Unsupported encode bit depth: ${this.options.bitPP}. Supported: 1, 4, 8, 16, 24, 32.`
563
+ );
564
+ }
565
+ const minLength = this.width * this.height * BYTES_PER_PIXEL_ABGR;
566
+ if (this.pixelData.length < minLength) {
567
+ throw new Error(
568
+ `Image data is too short: expected at least ${minLength} bytes for ${this.width}x${this.height} ABGR data.`
569
+ );
570
+ }
571
+ for (let i = 0; i < this.palette.length; i += 1) {
572
+ const color = this.palette[i];
573
+ const key = this.paletteKey(color.quad, color.blue, color.green, color.red);
574
+ if (!this.exactPaletteIndex.has(key)) {
575
+ this.exactPaletteIndex.set(key, i);
576
+ }
577
+ }
578
+ }
579
+ normalizePalette(options) {
580
+ if (options.bitPP === 1) {
581
+ const palette = options.palette.length ? options.palette : [
582
+ { red: 255, green: 255, blue: 255, quad: 0 },
583
+ { red: 0, green: 0, blue: 0, quad: 0 }
584
+ ];
585
+ this.validatePalette(options.bitPP, palette);
586
+ return palette;
587
+ }
588
+ if (options.bitPP === 4 || options.bitPP === 8) {
589
+ if (options.palette.length === 0) {
590
+ throw new Error(`Encoding ${options.bitPP}-bit BMP requires a non-empty palette.`);
591
+ }
592
+ this.validatePalette(options.bitPP, options.palette);
593
+ return options.palette;
594
+ }
595
+ return [];
596
+ }
597
+ validatePalette(bitPP, palette) {
598
+ const maxSize = 1 << bitPP;
599
+ if (palette.length === 0 || palette.length > maxSize) {
600
+ throw new Error(
601
+ `Palette size ${palette.length} is invalid for ${bitPP}-bit BMP. Expected 1..${maxSize}.`
602
+ );
603
+ }
604
+ for (const color of palette) {
605
+ this.validateChannel("palette.red", color.red);
606
+ this.validateChannel("palette.green", color.green);
607
+ this.validateChannel("palette.blue", color.blue);
608
+ this.validateChannel("palette.quad", color.quad);
609
+ }
610
+ }
611
+ validateChannel(name, value) {
612
+ if (!Number.isInteger(value) || value < 0 || value > 255) {
613
+ throw new Error(`${name} must be an integer between 0 and 255.`);
614
+ }
615
+ }
616
+ rowStride() {
617
+ return Math.floor((this.options.bitPP * this.width + 31) / 32) * 4;
618
+ }
619
+ sourceY(fileRow) {
620
+ return this.options.orientation === "top-down" ? fileRow : this.height - 1 - fileRow;
621
+ }
622
+ sourceOffset(x, y) {
623
+ return (y * this.width + x) * BYTES_PER_PIXEL_ABGR;
624
+ }
625
+ paletteKey(alpha, blue, green, red) {
626
+ return ((alpha & 255) << 24 | (blue & 255) << 16 | (green & 255) << 8 | red & 255) >>> 0;
627
+ }
628
+ findPaletteIndex(a, b, g, r) {
629
+ const exact = this.exactPaletteIndex.get(this.paletteKey(a, b, g, r));
630
+ if (exact !== void 0) {
631
+ return exact;
632
+ }
633
+ let bestIndex = 0;
634
+ let bestDistance = Number.POSITIVE_INFINITY;
635
+ for (let i = 0; i < this.palette.length; i += 1) {
636
+ const color = this.palette[i];
637
+ const dr = color.red - r;
638
+ const dg = color.green - g;
639
+ const db = color.blue - b;
640
+ const da = color.quad - a;
641
+ const distance = dr * dr + dg * dg + db * db + da * da;
642
+ if (distance < bestDistance) {
643
+ bestDistance = distance;
644
+ bestIndex = i;
645
+ }
646
+ }
647
+ return bestIndex;
648
+ }
649
+ writePalette(output, paletteOffset) {
650
+ for (let i = 0; i < this.palette.length; i += 1) {
651
+ const color = this.palette[i];
652
+ const base = paletteOffset + i * 4;
653
+ output[base] = color.blue;
654
+ output[base + 1] = color.green;
655
+ output[base + 2] = color.red;
656
+ output[base + 3] = color.quad;
657
+ }
658
+ }
659
+ encode1Bit(output, pixelOffset, stride) {
660
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
661
+ const srcY = this.sourceY(fileRow);
662
+ const rowStart = pixelOffset + fileRow * stride;
663
+ for (let x = 0; x < this.width; x += 8) {
664
+ let packed = 0;
665
+ for (let bit = 0; bit < 8; bit += 1) {
666
+ const px = x + bit;
667
+ if (px >= this.width) {
668
+ break;
669
+ }
670
+ const source = this.sourceOffset(px, srcY);
671
+ const a = this.pixelData[source] ?? 255;
672
+ const b = this.pixelData[source + 1] ?? 0;
673
+ const g = this.pixelData[source + 2] ?? 0;
674
+ const r = this.pixelData[source + 3] ?? 0;
675
+ const idx = this.findPaletteIndex(a, b, g, r) & 1;
676
+ packed |= idx << 7 - bit;
677
+ }
678
+ output[rowStart + Math.floor(x / 8)] = packed;
679
+ }
680
+ }
681
+ }
682
+ encode4Bit(output, pixelOffset, stride) {
683
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
684
+ const srcY = this.sourceY(fileRow);
685
+ const rowStart = pixelOffset + fileRow * stride;
686
+ for (let x = 0; x < this.width; x += 2) {
687
+ const sourceA = this.sourceOffset(x, srcY);
688
+ const idxA = this.findPaletteIndex(
689
+ this.pixelData[sourceA] ?? 255,
690
+ this.pixelData[sourceA + 1] ?? 0,
691
+ this.pixelData[sourceA + 2] ?? 0,
692
+ this.pixelData[sourceA + 3] ?? 0
693
+ );
694
+ let idxB = 0;
695
+ if (x + 1 < this.width) {
696
+ const sourceB = this.sourceOffset(x + 1, srcY);
697
+ idxB = this.findPaletteIndex(
698
+ this.pixelData[sourceB] ?? 255,
699
+ this.pixelData[sourceB + 1] ?? 0,
700
+ this.pixelData[sourceB + 2] ?? 0,
701
+ this.pixelData[sourceB + 3] ?? 0
702
+ );
703
+ }
704
+ output[rowStart + Math.floor(x / 2)] = (idxA & 15) << 4 | idxB & 15;
705
+ }
706
+ }
707
+ }
708
+ encode8Bit(output, pixelOffset, stride) {
709
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
710
+ const srcY = this.sourceY(fileRow);
711
+ const rowStart = pixelOffset + fileRow * stride;
712
+ for (let x = 0; x < this.width; x += 1) {
713
+ const source = this.sourceOffset(x, srcY);
714
+ output[rowStart + x] = this.findPaletteIndex(
715
+ this.pixelData[source] ?? 255,
716
+ this.pixelData[source + 1] ?? 0,
717
+ this.pixelData[source + 2] ?? 0,
718
+ this.pixelData[source + 3] ?? 0
719
+ );
720
+ }
721
+ }
722
+ }
723
+ encode16Bit(output, view, pixelOffset, stride) {
724
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
725
+ const srcY = this.sourceY(fileRow);
726
+ const rowStart = pixelOffset + fileRow * stride;
727
+ for (let x = 0; x < this.width; x += 1) {
728
+ const source = this.sourceOffset(x, srcY);
729
+ const b = this.pixelData[source + 1] ?? 0;
730
+ const g = this.pixelData[source + 2] ?? 0;
731
+ const r = this.pixelData[source + 3] ?? 0;
732
+ const value = (r >> 3 & 31) << 10 | (g >> 3 & 31) << 5 | b >> 3 & 31;
733
+ view.setUint16(rowStart + x * 2, value, true);
734
+ }
735
+ }
736
+ }
737
+ encode24Bit(output, pixelOffset, stride) {
738
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
739
+ const srcY = this.sourceY(fileRow);
740
+ const rowStart = pixelOffset + fileRow * stride;
741
+ for (let x = 0; x < this.width; x += 1) {
742
+ const source = this.sourceOffset(x, srcY);
743
+ const target = rowStart + x * 3;
744
+ output[target] = this.pixelData[source + 1] ?? 0;
745
+ output[target + 1] = this.pixelData[source + 2] ?? 0;
746
+ output[target + 2] = this.pixelData[source + 3] ?? 0;
747
+ }
748
+ }
749
+ }
750
+ encode32Bit(output, pixelOffset, stride) {
751
+ for (let fileRow = 0; fileRow < this.height; fileRow += 1) {
752
+ const srcY = this.sourceY(fileRow);
753
+ const rowStart = pixelOffset + fileRow * stride;
754
+ for (let x = 0; x < this.width; x += 1) {
755
+ const source = this.sourceOffset(x, srcY);
756
+ const target = rowStart + x * 4;
757
+ output[target] = this.pixelData[source + 1] ?? 0;
758
+ output[target + 1] = this.pixelData[source + 2] ?? 0;
759
+ output[target + 2] = this.pixelData[source + 3] ?? 0;
760
+ output[target + 3] = this.pixelData[source] ?? 255;
761
+ }
762
+ }
763
+ }
764
+ encode() {
765
+ const stride = this.rowStride();
766
+ const imageSize = stride * this.height;
767
+ const paletteSize = this.palette.length * 4;
768
+ const offset = FILE_HEADER_SIZE2 + INFO_HEADER_SIZE + paletteSize;
769
+ const totalSize = offset + imageSize;
770
+ const output = new Uint8Array(totalSize);
771
+ const view = new DataView(output.buffer, output.byteOffset, output.byteLength);
772
+ output[0] = 66;
773
+ output[1] = 77;
774
+ view.setUint32(2, totalSize, true);
775
+ view.setUint32(6, 0, true);
776
+ view.setUint32(10, offset, true);
777
+ view.setUint32(14, INFO_HEADER_SIZE, true);
778
+ view.setInt32(18, this.width, true);
779
+ const signedHeight = this.options.orientation === "top-down" ? -this.height : this.height;
780
+ view.setInt32(22, signedHeight, true);
781
+ view.setUint16(26, 1, true);
782
+ view.setUint16(28, this.options.bitPP, true);
783
+ view.setUint32(30, 0, true);
784
+ view.setUint32(34, imageSize, true);
785
+ view.setUint32(38, 0, true);
786
+ view.setUint32(42, 0, true);
787
+ view.setUint32(46, this.palette.length, true);
788
+ view.setUint32(50, 0, true);
789
+ if (this.palette.length > 0) {
790
+ this.writePalette(output, FILE_HEADER_SIZE2 + INFO_HEADER_SIZE);
791
+ }
792
+ switch (this.options.bitPP) {
793
+ case 1:
794
+ this.encode1Bit(output, offset, stride);
795
+ break;
796
+ case 4:
797
+ this.encode4Bit(output, offset, stride);
798
+ break;
799
+ case 8:
800
+ this.encode8Bit(output, offset, stride);
801
+ break;
802
+ case 16:
803
+ this.encode16Bit(output, view, offset, stride);
804
+ break;
805
+ case 24:
806
+ this.encode24Bit(output, offset, stride);
807
+ break;
808
+ case 32:
809
+ this.encode32Bit(output, offset, stride);
810
+ break;
811
+ }
812
+ return output;
813
+ }
814
+ };
815
+ function encode(imgData, qualityOrOptions) {
816
+ const options = normalizeEncodeOptions(qualityOrOptions);
817
+ const encoder = new BmpEncoder(imgData, options);
818
+ const data = encoder.encode();
819
+ return {
820
+ data,
821
+ width: imgData.width,
822
+ height: imgData.height
823
+ };
824
+ }
825
+
826
+ export {
827
+ BmpDecoder,
828
+ decode,
829
+ BmpEncoder,
830
+ encode
831
+ };
832
+ //# sourceMappingURL=chunk-YH5DJH4H.js.map