cross-image 0.2.4 → 0.4.1

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.
Files changed (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +615 -333
  3. package/esm/mod.d.ts +6 -4
  4. package/esm/mod.js +4 -2
  5. package/esm/src/formats/apng.d.ts +7 -5
  6. package/esm/src/formats/apng.js +15 -10
  7. package/esm/src/formats/ascii.d.ts +3 -3
  8. package/esm/src/formats/ascii.js +1 -1
  9. package/esm/src/formats/avif.d.ts +3 -3
  10. package/esm/src/formats/avif.js +17 -7
  11. package/esm/src/formats/bmp.d.ts +3 -3
  12. package/esm/src/formats/bmp.js +2 -2
  13. package/esm/src/formats/dng.d.ts +1 -1
  14. package/esm/src/formats/dng.js +1 -1
  15. package/esm/src/formats/gif.d.ts +5 -5
  16. package/esm/src/formats/gif.js +17 -13
  17. package/esm/src/formats/heic.d.ts +3 -3
  18. package/esm/src/formats/heic.js +17 -7
  19. package/esm/src/formats/ico.d.ts +3 -3
  20. package/esm/src/formats/ico.js +4 -4
  21. package/esm/src/formats/jpeg.d.ts +3 -3
  22. package/esm/src/formats/jpeg.js +23 -11
  23. package/esm/src/formats/pam.d.ts +3 -3
  24. package/esm/src/formats/pam.js +2 -2
  25. package/esm/src/formats/pcx.d.ts +3 -3
  26. package/esm/src/formats/pcx.js +2 -2
  27. package/esm/src/formats/png.d.ts +4 -3
  28. package/esm/src/formats/png.js +9 -3
  29. package/esm/src/formats/png_base.d.ts +42 -1
  30. package/esm/src/formats/png_base.js +200 -10
  31. package/esm/src/formats/ppm.d.ts +3 -3
  32. package/esm/src/formats/ppm.js +2 -2
  33. package/esm/src/formats/tiff.d.ts +7 -18
  34. package/esm/src/formats/tiff.js +162 -27
  35. package/esm/src/formats/webp.d.ts +3 -3
  36. package/esm/src/formats/webp.js +11 -8
  37. package/esm/src/image.d.ts +26 -3
  38. package/esm/src/image.js +66 -22
  39. package/esm/src/types.d.ts +122 -4
  40. package/esm/src/utils/base64.d.ts +32 -0
  41. package/esm/src/utils/base64.js +173 -0
  42. package/esm/src/utils/gif_decoder.d.ts +4 -1
  43. package/esm/src/utils/gif_decoder.js +91 -65
  44. package/esm/src/utils/gif_encoder.d.ts +3 -1
  45. package/esm/src/utils/gif_encoder.js +4 -2
  46. package/esm/src/utils/image_processing.d.ts +31 -0
  47. package/esm/src/utils/image_processing.js +232 -70
  48. package/esm/src/utils/jpeg_decoder.d.ts +17 -4
  49. package/esm/src/utils/jpeg_decoder.js +448 -83
  50. package/esm/src/utils/jpeg_encoder.d.ts +15 -1
  51. package/esm/src/utils/jpeg_encoder.js +263 -24
  52. package/esm/src/utils/resize.js +51 -20
  53. package/esm/src/utils/tiff_deflate.d.ts +18 -0
  54. package/esm/src/utils/tiff_deflate.js +27 -0
  55. package/esm/src/utils/tiff_packbits.d.ts +24 -0
  56. package/esm/src/utils/tiff_packbits.js +90 -0
  57. package/esm/src/utils/webp_decoder.d.ts +3 -1
  58. package/esm/src/utils/webp_decoder.js +144 -63
  59. package/esm/src/utils/webp_encoder.js +5 -11
  60. package/package.json +1 -1
  61. package/script/mod.d.ts +6 -4
  62. package/script/mod.js +13 -3
  63. package/script/src/formats/apng.d.ts +7 -5
  64. package/script/src/formats/apng.js +15 -10
  65. package/script/src/formats/ascii.d.ts +3 -3
  66. package/script/src/formats/ascii.js +1 -1
  67. package/script/src/formats/avif.d.ts +3 -3
  68. package/script/src/formats/avif.js +17 -7
  69. package/script/src/formats/bmp.d.ts +3 -3
  70. package/script/src/formats/bmp.js +2 -2
  71. package/script/src/formats/dng.d.ts +1 -1
  72. package/script/src/formats/dng.js +1 -1
  73. package/script/src/formats/gif.d.ts +5 -5
  74. package/script/src/formats/gif.js +17 -13
  75. package/script/src/formats/heic.d.ts +3 -3
  76. package/script/src/formats/heic.js +17 -7
  77. package/script/src/formats/ico.d.ts +3 -3
  78. package/script/src/formats/ico.js +4 -4
  79. package/script/src/formats/jpeg.d.ts +3 -3
  80. package/script/src/formats/jpeg.js +23 -11
  81. package/script/src/formats/pam.d.ts +3 -3
  82. package/script/src/formats/pam.js +2 -2
  83. package/script/src/formats/pcx.d.ts +3 -3
  84. package/script/src/formats/pcx.js +2 -2
  85. package/script/src/formats/png.d.ts +4 -3
  86. package/script/src/formats/png.js +9 -3
  87. package/script/src/formats/png_base.d.ts +42 -1
  88. package/script/src/formats/png_base.js +200 -10
  89. package/script/src/formats/ppm.d.ts +3 -3
  90. package/script/src/formats/ppm.js +2 -2
  91. package/script/src/formats/tiff.d.ts +7 -18
  92. package/script/src/formats/tiff.js +162 -27
  93. package/script/src/formats/webp.d.ts +3 -3
  94. package/script/src/formats/webp.js +11 -8
  95. package/script/src/image.d.ts +26 -3
  96. package/script/src/image.js +64 -20
  97. package/script/src/types.d.ts +122 -4
  98. package/script/src/utils/base64.d.ts +32 -0
  99. package/script/src/utils/base64.js +179 -0
  100. package/script/src/utils/gif_decoder.d.ts +4 -1
  101. package/script/src/utils/gif_decoder.js +91 -65
  102. package/script/src/utils/gif_encoder.d.ts +3 -1
  103. package/script/src/utils/gif_encoder.js +4 -2
  104. package/script/src/utils/image_processing.d.ts +31 -0
  105. package/script/src/utils/image_processing.js +236 -70
  106. package/script/src/utils/jpeg_decoder.d.ts +17 -4
  107. package/script/src/utils/jpeg_decoder.js +448 -83
  108. package/script/src/utils/jpeg_encoder.d.ts +15 -1
  109. package/script/src/utils/jpeg_encoder.js +263 -24
  110. package/script/src/utils/resize.js +51 -20
  111. package/script/src/utils/tiff_deflate.d.ts +18 -0
  112. package/script/src/utils/tiff_deflate.js +31 -0
  113. package/script/src/utils/tiff_packbits.d.ts +24 -0
  114. package/script/src/utils/tiff_packbits.js +94 -0
  115. package/script/src/utils/webp_decoder.d.ts +3 -1
  116. package/script/src/utils/webp_decoder.js +144 -63
  117. package/script/src/utils/webp_encoder.js +5 -11
@@ -2,6 +2,19 @@
2
2
  * Image processing utilities for common operations like compositing,
3
3
  * level adjustments, and color manipulations.
4
4
  */
5
+ /**
6
+ * Detect system endianness
7
+ * Returns true if little-endian (most common), false if big-endian
8
+ */
9
+ function isLittleEndian() {
10
+ const buffer = new ArrayBuffer(4);
11
+ const uint32View = new Uint32Array(buffer);
12
+ const uint8View = new Uint8Array(buffer);
13
+ uint32View[0] = 0x01020304;
14
+ return uint8View[0] === 0x04;
15
+ }
16
+ // Cache the endianness check result
17
+ const IS_LITTLE_ENDIAN = isLittleEndian();
5
18
  /**
6
19
  * Composite one image on top of another at a specified position
7
20
  * @param base Base image data (RGBA)
@@ -26,17 +39,20 @@ export function composite(base, baseWidth, baseHeight, overlay, overlayWidth, ov
26
39
  const endY = Math.min(baseHeight, y + overlayHeight);
27
40
  // Iterate over the overlapping region
28
41
  for (let py = startY; py < endY; py++) {
42
+ const baseRowOffset = py * baseWidth * 4;
43
+ const overlayRowOffset = (py - y) * overlayWidth * 4;
29
44
  for (let px = startX; px < endX; px++) {
30
- // Calculate indices
31
- const baseIdx = (py * baseWidth + px) * 4;
32
- const overlayX = px - x;
33
- const overlayY = py - y;
34
- const overlayIdx = (overlayY * overlayWidth + overlayX) * 4;
45
+ // Calculate indices with pre-computed offsets
46
+ const baseIdx = baseRowOffset + px * 4;
47
+ const overlayIdx = overlayRowOffset + (px - x) * 4;
35
48
  // Get overlay pixel with opacity
36
49
  const overlayR = overlay[overlayIdx];
37
50
  const overlayG = overlay[overlayIdx + 1];
38
51
  const overlayB = overlay[overlayIdx + 2];
39
52
  const overlayA = (overlay[overlayIdx + 3] / 255) * finalOpacity;
53
+ // Skip if overlay is fully transparent
54
+ if (overlayA === 0)
55
+ continue;
40
56
  // Get base pixel
41
57
  const baseR = result[baseIdx];
42
58
  const baseG = result[baseIdx + 1];
@@ -44,11 +60,14 @@ export function composite(base, baseWidth, baseHeight, overlay, overlayWidth, ov
44
60
  const baseA = result[baseIdx + 3] / 255;
45
61
  // Alpha compositing using "over" operation
46
62
  const outA = overlayA + baseA * (1 - overlayA);
47
- if (outA > 0) {
48
- result[baseIdx] = Math.round((overlayR * overlayA + baseR * baseA * (1 - overlayA)) / outA);
49
- result[baseIdx + 1] = Math.round((overlayG * overlayA + baseG * baseA * (1 - overlayA)) / outA);
50
- result[baseIdx + 2] = Math.round((overlayB * overlayA + baseB * baseA * (1 - overlayA)) / outA);
51
- result[baseIdx + 3] = Math.round(outA * 255);
63
+ if (outA > 0.001) {
64
+ const invOverlayA = 1 - overlayA;
65
+ const baseWeight = baseA * invOverlayA;
66
+ const invOutA = 1 / outA;
67
+ result[baseIdx] = ((overlayR * overlayA + baseR * baseWeight) * invOutA + 0.5) | 0;
68
+ result[baseIdx + 1] = ((overlayG * overlayA + baseG * baseWeight) * invOutA + 0.5) | 0;
69
+ result[baseIdx + 2] = ((overlayB * overlayA + baseB * baseWeight) * invOutA + 0.5) | 0;
70
+ result[baseIdx + 3] = (outA * 255 + 0.5) | 0;
52
71
  }
53
72
  }
54
73
  }
@@ -62,11 +81,23 @@ export function composite(base, baseWidth, baseHeight, overlay, overlayWidth, ov
62
81
  */
63
82
  export function adjustBrightness(data, amount) {
64
83
  const result = new Uint8Array(data.length);
65
- const adjust = Math.max(-1, Math.min(1, amount)) * 255;
84
+ const clampedAmount = Math.max(-1, Math.min(1, amount));
85
+ const adjust = clampedAmount * 255;
86
+ // Pre-compute lookup table for clamping
87
+ // Range: -255 to 511 (data value 0-255 + adjust -255 to 255), offset by 255 for zero-based index
88
+ const LUT_SIZE = 767;
89
+ const LUT_OFFSET = 255;
90
+ const lut = new Uint8Array(LUT_SIZE);
91
+ for (let i = 0; i < LUT_SIZE; i++) {
92
+ const value = i - LUT_OFFSET;
93
+ lut[i] = value < 0 ? 0 : (value > 255 ? 255 : value);
94
+ }
95
+ // Use bitwise OR for fast rounding (equivalent to Math.round for positive numbers)
96
+ const adjustInt = (adjust + 0.5) | 0;
66
97
  for (let i = 0; i < data.length; i += 4) {
67
- result[i] = Math.max(0, Math.min(255, data[i] + adjust)); // R
68
- result[i + 1] = Math.max(0, Math.min(255, data[i + 1] + adjust)); // G
69
- result[i + 2] = Math.max(0, Math.min(255, data[i + 2] + adjust)); // B
98
+ result[i] = lut[data[i] + adjustInt + LUT_OFFSET]; // R
99
+ result[i + 1] = lut[data[i + 1] + adjustInt + LUT_OFFSET]; // G
100
+ result[i + 2] = lut[data[i + 2] + adjustInt + LUT_OFFSET]; // B
70
101
  result[i + 3] = data[i + 3]; // A
71
102
  }
72
103
  return result;
@@ -82,10 +113,16 @@ export function adjustContrast(data, amount) {
82
113
  const contrast = Math.max(-1, Math.min(1, amount));
83
114
  const factor = (259 * (contrast * 255 + 255)) /
84
115
  (255 * (259 - contrast * 255));
116
+ // Pre-compute lookup table for all possible pixel values
117
+ const lut = new Uint8Array(256);
118
+ for (let i = 0; i < 256; i++) {
119
+ const val = factor * (i - 128) + 128;
120
+ lut[i] = val < 0 ? 0 : (val > 255 ? 255 : Math.round(val));
121
+ }
85
122
  for (let i = 0; i < data.length; i += 4) {
86
- result[i] = Math.max(0, Math.min(255, factor * (data[i] - 128) + 128)); // R
87
- result[i + 1] = Math.max(0, Math.min(255, factor * (data[i + 1] - 128) + 128)); // G
88
- result[i + 2] = Math.max(0, Math.min(255, factor * (data[i + 2] - 128) + 128)); // B
123
+ result[i] = lut[data[i]]; // R
124
+ result[i + 1] = lut[data[i + 1]]; // G
125
+ result[i + 2] = lut[data[i + 2]]; // B
89
126
  result[i + 3] = data[i + 3]; // A
90
127
  }
91
128
  return result;
@@ -100,10 +137,16 @@ export function adjustExposure(data, amount) {
100
137
  const result = new Uint8Array(data.length);
101
138
  const stops = Math.max(-3, Math.min(3, amount));
102
139
  const multiplier = Math.pow(2, stops);
140
+ // Pre-compute lookup table for all possible pixel values
141
+ const lut = new Uint8Array(256);
142
+ for (let i = 0; i < 256; i++) {
143
+ const val = i * multiplier;
144
+ lut[i] = val > 255 ? 255 : (val + 0.5) | 0;
145
+ }
103
146
  for (let i = 0; i < data.length; i += 4) {
104
- result[i] = Math.max(0, Math.min(255, data[i] * multiplier)); // R
105
- result[i + 1] = Math.max(0, Math.min(255, data[i + 1] * multiplier)); // G
106
- result[i + 2] = Math.max(0, Math.min(255, data[i + 2] * multiplier)); // B
147
+ result[i] = lut[data[i]]; // R
148
+ result[i + 1] = lut[data[i + 1]]; // G
149
+ result[i + 2] = lut[data[i + 2]]; // B
107
150
  result[i + 3] = data[i + 3]; // A
108
151
  }
109
152
  return result;
@@ -310,15 +353,12 @@ export function crop(data, width, height, x, y, cropWidth, cropHeight) {
310
353
  const actualWidth = endX - startX;
311
354
  const actualHeight = endY - startY;
312
355
  const result = new Uint8Array(actualWidth * actualHeight * 4);
356
+ const rowBytes = actualWidth * 4;
357
+ // Copy entire rows at once for better performance
313
358
  for (let py = 0; py < actualHeight; py++) {
314
- for (let px = 0; px < actualWidth; px++) {
315
- const srcIdx = ((startY + py) * width + (startX + px)) * 4;
316
- const dstIdx = (py * actualWidth + px) * 4;
317
- result[dstIdx] = data[srcIdx];
318
- result[dstIdx + 1] = data[srcIdx + 1];
319
- result[dstIdx + 2] = data[srcIdx + 2];
320
- result[dstIdx + 3] = data[srcIdx + 3];
321
- }
359
+ const srcOffset = ((startY + py) * width + startX) * 4;
360
+ const dstOffset = py * rowBytes;
361
+ result.set(data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);
322
362
  }
323
363
  return { data: result, width: actualWidth, height: actualHeight };
324
364
  }
@@ -397,46 +437,52 @@ function generateGaussianKernel(radius, sigma) {
397
437
  export function gaussianBlur(data, width, height, radius = 1, sigma) {
398
438
  const clampedRadius = Math.max(1, Math.floor(radius));
399
439
  const kernel = generateGaussianKernel(clampedRadius, sigma);
440
+ const widthMinus1 = width - 1;
441
+ const heightMinus1 = height - 1;
400
442
  // Apply horizontal pass
401
443
  const temp = new Uint8Array(data.length);
402
444
  for (let y = 0; y < height; y++) {
445
+ const rowOffset = y * width * 4;
403
446
  for (let x = 0; x < width; x++) {
404
447
  let r = 0, g = 0, b = 0, a = 0;
405
448
  for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
406
- const px = Math.max(0, Math.min(width - 1, x + kx));
407
- const idx = (y * width + px) * 4;
449
+ const px = x + kx;
450
+ const clampedPx = px < 0 ? 0 : (px > widthMinus1 ? widthMinus1 : px);
451
+ const idx = rowOffset + clampedPx * 4;
408
452
  const weight = kernel[kx + clampedRadius];
409
453
  r += data[idx] * weight;
410
454
  g += data[idx + 1] * weight;
411
455
  b += data[idx + 2] * weight;
412
456
  a += data[idx + 3] * weight;
413
457
  }
414
- const outIdx = (y * width + x) * 4;
415
- temp[outIdx] = Math.round(r);
416
- temp[outIdx + 1] = Math.round(g);
417
- temp[outIdx + 2] = Math.round(b);
418
- temp[outIdx + 3] = Math.round(a);
458
+ const outIdx = rowOffset + x * 4;
459
+ temp[outIdx] = (r + 0.5) | 0;
460
+ temp[outIdx + 1] = (g + 0.5) | 0;
461
+ temp[outIdx + 2] = (b + 0.5) | 0;
462
+ temp[outIdx + 3] = (a + 0.5) | 0;
419
463
  }
420
464
  }
421
465
  // Apply vertical pass
422
466
  const result = new Uint8Array(data.length);
423
467
  for (let y = 0; y < height; y++) {
468
+ const rowOffset = y * width * 4;
424
469
  for (let x = 0; x < width; x++) {
425
470
  let r = 0, g = 0, b = 0, a = 0;
426
471
  for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
427
- const py = Math.max(0, Math.min(height - 1, y + ky));
428
- const idx = (py * width + x) * 4;
472
+ const py = y + ky;
473
+ const clampedPy = py < 0 ? 0 : (py > heightMinus1 ? heightMinus1 : py);
474
+ const idx = clampedPy * width * 4 + x * 4;
429
475
  const weight = kernel[ky + clampedRadius];
430
476
  r += temp[idx] * weight;
431
477
  g += temp[idx + 1] * weight;
432
478
  b += temp[idx + 2] * weight;
433
479
  a += temp[idx + 3] * weight;
434
480
  }
435
- const outIdx = (y * width + x) * 4;
436
- result[outIdx] = Math.round(r);
437
- result[outIdx + 1] = Math.round(g);
438
- result[outIdx + 2] = Math.round(b);
439
- result[outIdx + 3] = Math.round(a);
481
+ const outIdx = rowOffset + x * 4;
482
+ result[outIdx] = (r + 0.5) | 0;
483
+ result[outIdx + 1] = (g + 0.5) | 0;
484
+ result[outIdx + 2] = (b + 0.5) | 0;
485
+ result[outIdx + 3] = (a + 0.5) | 0;
440
486
  }
441
487
  }
442
488
  return result;
@@ -586,16 +632,30 @@ export function rotate90(data, width, height) {
586
632
  */
587
633
  export function rotate180(data, width, height) {
588
634
  const result = new Uint8Array(data.length);
589
- for (let y = 0; y < height; y++) {
590
- for (let x = 0; x < width; x++) {
591
- const srcIdx = (y * width + x) * 4;
592
- const dstX = width - 1 - x;
593
- const dstY = height - 1 - y;
594
- const dstIdx = (dstY * width + dstX) * 4;
595
- result[dstIdx] = data[srcIdx];
596
- result[dstIdx + 1] = data[srcIdx + 1];
597
- result[dstIdx + 2] = data[srcIdx + 2];
598
- result[dstIdx + 3] = data[srcIdx + 3];
635
+ // Only use Uint32Array optimization on little-endian systems to avoid byte order issues
636
+ if (IS_LITTLE_ENDIAN) {
637
+ // Use Uint32Array view for faster 4-byte (pixel) copying
638
+ // Note: Uint8Array buffers are guaranteed to be aligned for any TypedArray view
639
+ const src32 = new Uint32Array(data.buffer, data.byteOffset, width * height);
640
+ const dst32 = new Uint32Array(result.buffer, result.byteOffset, width * height);
641
+ const totalPixels = width * height;
642
+ for (let i = 0; i < totalPixels; i++) {
643
+ dst32[totalPixels - 1 - i] = src32[i];
644
+ }
645
+ }
646
+ else {
647
+ // Fallback for big-endian systems - byte-by-byte copying
648
+ for (let y = 0; y < height; y++) {
649
+ for (let x = 0; x < width; x++) {
650
+ const srcIdx = (y * width + x) * 4;
651
+ const dstX = width - 1 - x;
652
+ const dstY = height - 1 - y;
653
+ const dstIdx = (dstY * width + dstX) * 4;
654
+ result[dstIdx] = data[srcIdx];
655
+ result[dstIdx + 1] = data[srcIdx + 1];
656
+ result[dstIdx + 2] = data[srcIdx + 2];
657
+ result[dstIdx + 3] = data[srcIdx + 3];
658
+ }
599
659
  }
600
660
  }
601
661
  return result;
@@ -634,15 +694,33 @@ export function rotate270(data, width, height) {
634
694
  */
635
695
  export function flipHorizontal(data, width, height) {
636
696
  const result = new Uint8Array(data.length);
637
- for (let y = 0; y < height; y++) {
638
- for (let x = 0; x < width; x++) {
639
- const srcIdx = (y * width + x) * 4;
640
- const dstX = width - 1 - x;
641
- const dstIdx = (y * width + dstX) * 4;
642
- result[dstIdx] = data[srcIdx];
643
- result[dstIdx + 1] = data[srcIdx + 1];
644
- result[dstIdx + 2] = data[srcIdx + 2];
645
- result[dstIdx + 3] = data[srcIdx + 3];
697
+ // Only use Uint32Array optimization on little-endian systems to avoid byte order issues
698
+ if (IS_LITTLE_ENDIAN) {
699
+ // Use Uint32Array view for faster 4-byte (pixel) copying
700
+ // Note: Uint8Array buffers are guaranteed to be aligned for any TypedArray view
701
+ const src32 = new Uint32Array(data.buffer, data.byteOffset, width * height);
702
+ const dst32 = new Uint32Array(result.buffer, result.byteOffset, width * height);
703
+ for (let y = 0; y < height; y++) {
704
+ const rowStart = y * width;
705
+ for (let x = 0; x < width; x++) {
706
+ const srcIdx = rowStart + x;
707
+ const dstIdx = rowStart + (width - 1 - x);
708
+ dst32[dstIdx] = src32[srcIdx];
709
+ }
710
+ }
711
+ }
712
+ else {
713
+ // Fallback for big-endian systems - byte-by-byte copying
714
+ for (let y = 0; y < height; y++) {
715
+ for (let x = 0; x < width; x++) {
716
+ const srcIdx = (y * width + x) * 4;
717
+ const dstX = width - 1 - x;
718
+ const dstIdx = (y * width + dstX) * 4;
719
+ result[dstIdx] = data[srcIdx];
720
+ result[dstIdx + 1] = data[srcIdx + 1];
721
+ result[dstIdx + 2] = data[srcIdx + 2];
722
+ result[dstIdx + 3] = data[srcIdx + 3];
723
+ }
646
724
  }
647
725
  }
648
726
  return result;
@@ -656,16 +734,100 @@ export function flipHorizontal(data, width, height) {
656
734
  */
657
735
  export function flipVertical(data, width, height) {
658
736
  const result = new Uint8Array(data.length);
737
+ const rowBytes = width * 4;
738
+ // Copy entire rows at once for better performance
659
739
  for (let y = 0; y < height; y++) {
660
- for (let x = 0; x < width; x++) {
661
- const srcIdx = (y * width + x) * 4;
662
- const dstY = height - 1 - y;
663
- const dstIdx = (dstY * width + x) * 4;
664
- result[dstIdx] = data[srcIdx];
665
- result[dstIdx + 1] = data[srcIdx + 1];
666
- result[dstIdx + 2] = data[srcIdx + 2];
667
- result[dstIdx + 3] = data[srcIdx + 3];
668
- }
740
+ const srcOffset = y * rowBytes;
741
+ const dstOffset = (height - 1 - y) * rowBytes;
742
+ result.set(data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);
743
+ }
744
+ return result;
745
+ }
746
+ /**
747
+ * Convert RGB color to CMYK color space
748
+ * @param r Red component (0-255)
749
+ * @param g Green component (0-255)
750
+ * @param b Blue component (0-255)
751
+ * @returns CMYK values: [c (0-1), m (0-1), y (0-1), k (0-1)]
752
+ */
753
+ export function rgbToCmyk(r, g, b) {
754
+ // Normalize RGB values to 0-1 range
755
+ const rNorm = r / 255;
756
+ const gNorm = g / 255;
757
+ const bNorm = b / 255;
758
+ // Calculate K (key/black)
759
+ const k = 1 - Math.max(rNorm, gNorm, bNorm);
760
+ // If K is 1 (pure black), CMY are undefined but conventionally set to 0
761
+ if (k === 1) {
762
+ return [0, 0, 0, 1];
763
+ }
764
+ // Calculate CMY components
765
+ const c = (1 - rNorm - k) / (1 - k);
766
+ const m = (1 - gNorm - k) / (1 - k);
767
+ const y = (1 - bNorm - k) / (1 - k);
768
+ return [c, m, y, k];
769
+ }
770
+ /**
771
+ * Convert CMYK color to RGB color space
772
+ * @param c Cyan component (0-1)
773
+ * @param m Magenta component (0-1)
774
+ * @param y Yellow component (0-1)
775
+ * @param k Key/Black component (0-1)
776
+ * @returns RGB values: [r (0-255), g (0-255), b (0-255)]
777
+ */
778
+ export function cmykToRgb(c, m, y, k) {
779
+ // Convert CMYK to RGB
780
+ const r = 255 * (1 - c) * (1 - k);
781
+ const g = 255 * (1 - m) * (1 - k);
782
+ const b = 255 * (1 - y) * (1 - k);
783
+ return [
784
+ Math.round(Math.max(0, Math.min(255, r))),
785
+ Math.round(Math.max(0, Math.min(255, g))),
786
+ Math.round(Math.max(0, Math.min(255, b))),
787
+ ];
788
+ }
789
+ /**
790
+ * Convert RGBA image data to CMYK representation
791
+ * Returns a Float32Array with 4 values per pixel (C, M, Y, K) in 0-1 range
792
+ * @param data Image data (RGBA)
793
+ * @returns CMYK image data as Float32Array (4 values per pixel)
794
+ */
795
+ export function rgbaToCmyk(data) {
796
+ const pixelCount = data.length / 4;
797
+ const result = new Float32Array(pixelCount * 4);
798
+ for (let i = 0; i < data.length; i += 4) {
799
+ const r = data[i];
800
+ const g = data[i + 1];
801
+ const b = data[i + 2];
802
+ const [c, m, y, k] = rgbToCmyk(r, g, b);
803
+ const outIdx = (i / 4) * 4;
804
+ result[outIdx] = c;
805
+ result[outIdx + 1] = m;
806
+ result[outIdx + 2] = y;
807
+ result[outIdx + 3] = k;
808
+ }
809
+ return result;
810
+ }
811
+ /**
812
+ * Convert CMYK image data to RGBA representation
813
+ * @param cmykData CMYK image data (4 values per pixel in 0-1 range)
814
+ * @param alpha Optional alpha value for all pixels (0-255, default: 255)
815
+ * @returns RGBA image data
816
+ */
817
+ export function cmykToRgba(cmykData, alpha = 255) {
818
+ const pixelCount = cmykData.length / 4;
819
+ const result = new Uint8Array(pixelCount * 4);
820
+ for (let i = 0; i < cmykData.length; i += 4) {
821
+ const c = cmykData[i];
822
+ const m = cmykData[i + 1];
823
+ const y = cmykData[i + 2];
824
+ const k = cmykData[i + 3];
825
+ const [r, g, b] = cmykToRgb(c, m, y, k);
826
+ const outIdx = i;
827
+ result[outIdx] = r;
828
+ result[outIdx + 1] = g;
829
+ result[outIdx + 2] = b;
830
+ result[outIdx + 3] = alpha;
669
831
  }
670
832
  return result;
671
833
  }
@@ -1,10 +1,14 @@
1
1
  /**
2
- * Basic baseline JPEG decoder implementation
3
- * Supports baseline DCT JPEG images (the most common format)
2
+ * JPEG decoder implementation supporting both baseline and progressive DCT
4
3
  *
5
- * This is a simplified implementation that handles common JPEG files.
4
+ * Supports:
5
+ * - Baseline DCT (SOF0) - Sequential encoding
6
+ * - Progressive DCT (SOF2) - Multi-scan encoding with spectral selection and successive approximation
7
+ *
8
+ * This is a pure JavaScript implementation that handles common JPEG files.
6
9
  * For complex or non-standard JPEGs, the ImageDecoder API fallback is preferred.
7
10
  */
11
+ import type { ImageDecoderOptions } from "../types.js";
8
12
  export declare class JPEGDecoder {
9
13
  private data;
10
14
  private pos;
@@ -17,7 +21,15 @@ export declare class JPEGDecoder {
17
21
  private restartInterval;
18
22
  private bitBuffer;
19
23
  private bitCount;
20
- constructor(data: Uint8Array);
24
+ private options;
25
+ private isProgressive;
26
+ private spectralStart;
27
+ private spectralEnd;
28
+ private successiveHigh;
29
+ private successiveLow;
30
+ private scanComponentIds;
31
+ private eobRun;
32
+ constructor(data: Uint8Array, settings?: ImageDecoderOptions);
21
33
  decode(): Uint8Array;
22
34
  private readMarker;
23
35
  private readUint16;
@@ -33,6 +45,7 @@ export declare class JPEGDecoder {
33
45
  private decodeHuffman;
34
46
  private readBit;
35
47
  private receiveBits;
48
+ private receiveUnsignedBits;
36
49
  private idct;
37
50
  private convertToRGB;
38
51
  }