cross-image 0.2.3 → 0.4.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.
Files changed (105) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +292 -74
  3. package/esm/mod.d.ts +6 -4
  4. package/esm/mod.js +4 -2
  5. package/esm/src/formats/apng.d.ts +17 -5
  6. package/esm/src/formats/apng.js +104 -9
  7. package/esm/src/formats/ascii.d.ts +13 -3
  8. package/esm/src/formats/ascii.js +25 -1
  9. package/esm/src/formats/avif.d.ts +96 -0
  10. package/esm/src/formats/avif.js +607 -0
  11. package/esm/src/formats/bmp.d.ts +13 -3
  12. package/esm/src/formats/bmp.js +75 -2
  13. package/esm/src/formats/dng.d.ts +14 -2
  14. package/esm/src/formats/dng.js +27 -5
  15. package/esm/src/formats/gif.d.ts +18 -5
  16. package/esm/src/formats/gif.js +160 -14
  17. package/esm/src/formats/heic.d.ts +96 -0
  18. package/esm/src/formats/heic.js +608 -0
  19. package/esm/src/formats/ico.d.ts +13 -3
  20. package/esm/src/formats/ico.js +32 -4
  21. package/esm/src/formats/jpeg.d.ts +10 -3
  22. package/esm/src/formats/jpeg.js +99 -11
  23. package/esm/src/formats/pam.d.ts +13 -3
  24. package/esm/src/formats/pam.js +68 -2
  25. package/esm/src/formats/pcx.d.ts +13 -3
  26. package/esm/src/formats/pcx.js +47 -2
  27. package/esm/src/formats/png.d.ts +15 -3
  28. package/esm/src/formats/png.js +89 -2
  29. package/esm/src/formats/png_base.js +2 -5
  30. package/esm/src/formats/ppm.d.ts +13 -3
  31. package/esm/src/formats/ppm.js +36 -2
  32. package/esm/src/formats/tiff.d.ts +14 -18
  33. package/esm/src/formats/tiff.js +219 -20
  34. package/esm/src/formats/webp.d.ts +10 -3
  35. package/esm/src/formats/webp.js +103 -8
  36. package/esm/src/image.d.ts +20 -3
  37. package/esm/src/image.js +65 -21
  38. package/esm/src/types.d.ts +74 -4
  39. package/esm/src/utils/gif_decoder.d.ts +4 -1
  40. package/esm/src/utils/gif_decoder.js +91 -65
  41. package/esm/src/utils/image_processing.js +144 -70
  42. package/esm/src/utils/jpeg_decoder.d.ts +17 -4
  43. package/esm/src/utils/jpeg_decoder.js +448 -83
  44. package/esm/src/utils/jpeg_encoder.d.ts +15 -1
  45. package/esm/src/utils/jpeg_encoder.js +263 -24
  46. package/esm/src/utils/resize.js +51 -20
  47. package/esm/src/utils/tiff_deflate.d.ts +18 -0
  48. package/esm/src/utils/tiff_deflate.js +27 -0
  49. package/esm/src/utils/tiff_packbits.d.ts +24 -0
  50. package/esm/src/utils/tiff_packbits.js +90 -0
  51. package/esm/src/utils/webp_decoder.d.ts +3 -1
  52. package/esm/src/utils/webp_decoder.js +144 -63
  53. package/esm/src/utils/webp_encoder.js +5 -11
  54. package/package.json +18 -1
  55. package/script/mod.d.ts +6 -4
  56. package/script/mod.js +7 -3
  57. package/script/src/formats/apng.d.ts +17 -5
  58. package/script/src/formats/apng.js +104 -9
  59. package/script/src/formats/ascii.d.ts +13 -3
  60. package/script/src/formats/ascii.js +25 -1
  61. package/script/src/formats/avif.d.ts +96 -0
  62. package/script/src/formats/avif.js +611 -0
  63. package/script/src/formats/bmp.d.ts +13 -3
  64. package/script/src/formats/bmp.js +75 -2
  65. package/script/src/formats/dng.d.ts +14 -2
  66. package/script/src/formats/dng.js +27 -5
  67. package/script/src/formats/gif.d.ts +18 -5
  68. package/script/src/formats/gif.js +160 -14
  69. package/script/src/formats/heic.d.ts +96 -0
  70. package/script/src/formats/heic.js +612 -0
  71. package/script/src/formats/ico.d.ts +13 -3
  72. package/script/src/formats/ico.js +32 -4
  73. package/script/src/formats/jpeg.d.ts +10 -3
  74. package/script/src/formats/jpeg.js +99 -11
  75. package/script/src/formats/pam.d.ts +13 -3
  76. package/script/src/formats/pam.js +68 -2
  77. package/script/src/formats/pcx.d.ts +13 -3
  78. package/script/src/formats/pcx.js +47 -2
  79. package/script/src/formats/png.d.ts +15 -3
  80. package/script/src/formats/png.js +89 -2
  81. package/script/src/formats/png_base.js +2 -5
  82. package/script/src/formats/ppm.d.ts +13 -3
  83. package/script/src/formats/ppm.js +36 -2
  84. package/script/src/formats/tiff.d.ts +14 -18
  85. package/script/src/formats/tiff.js +219 -20
  86. package/script/src/formats/webp.d.ts +10 -3
  87. package/script/src/formats/webp.js +103 -8
  88. package/script/src/image.d.ts +20 -3
  89. package/script/src/image.js +64 -20
  90. package/script/src/types.d.ts +74 -4
  91. package/script/src/utils/gif_decoder.d.ts +4 -1
  92. package/script/src/utils/gif_decoder.js +91 -65
  93. package/script/src/utils/image_processing.js +144 -70
  94. package/script/src/utils/jpeg_decoder.d.ts +17 -4
  95. package/script/src/utils/jpeg_decoder.js +448 -83
  96. package/script/src/utils/jpeg_encoder.d.ts +15 -1
  97. package/script/src/utils/jpeg_encoder.js +263 -24
  98. package/script/src/utils/resize.js +51 -20
  99. package/script/src/utils/tiff_deflate.d.ts +18 -0
  100. package/script/src/utils/tiff_deflate.js +31 -0
  101. package/script/src/utils/tiff_packbits.d.ts +24 -0
  102. package/script/src/utils/tiff_packbits.js +94 -0
  103. package/script/src/utils/webp_decoder.d.ts +3 -1
  104. package/script/src/utils/webp_decoder.js +144 -63
  105. package/script/src/utils/webp_encoder.js +5 -11
@@ -38,7 +38,7 @@ export class BMPFormat {
38
38
  * @param data Raw BMP image data
39
39
  * @returns Decoded image data with RGBA pixels
40
40
  */
41
- decode(data) {
41
+ decode(data, _options) {
42
42
  if (!this.canDecode(data)) {
43
43
  throw new Error("Invalid BMP signature");
44
44
  }
@@ -115,7 +115,7 @@ export class BMPFormat {
115
115
  * @param imageData Image data to encode
116
116
  * @returns Encoded BMP image bytes
117
117
  */
118
- encode(imageData) {
118
+ encode(imageData, _options) {
119
119
  const { width, height, data, metadata } = imageData;
120
120
  // Calculate sizes
121
121
  const bytesPerPixel = 4; // We'll encode as 32-bit RGBA
@@ -168,4 +168,77 @@ export class BMPFormat {
168
168
  }
169
169
  return Promise.resolve(result);
170
170
  }
171
+ /**
172
+ * Get the list of metadata fields supported by BMP format
173
+ */
174
+ getSupportedMetadata() {
175
+ return [
176
+ "dpiX", // Pixels per meter in header
177
+ "dpiY", // Pixels per meter in header
178
+ ];
179
+ }
180
+ /**
181
+ * Extract metadata from BMP data without fully decoding the pixel data
182
+ * @param data Raw BMP data
183
+ * @returns Extracted metadata or undefined
184
+ */
185
+ extractMetadata(data) {
186
+ if (!this.canDecode(data)) {
187
+ return Promise.resolve(undefined);
188
+ }
189
+ const metadata = {
190
+ format: "bmp",
191
+ frameCount: 1,
192
+ bitDepth: 24,
193
+ colorType: "rgb",
194
+ };
195
+ // Read BMP header to determine version
196
+ const headerSize = readUint32LE(data, 14);
197
+ if (headerSize >= 40) {
198
+ // BITMAPINFOHEADER or later
199
+ const bitsPerPixel = readUint16LE(data, 28);
200
+ metadata.bitDepth = bitsPerPixel;
201
+ if (bitsPerPixel === 1 || bitsPerPixel === 8) {
202
+ metadata.colorType = "indexed";
203
+ }
204
+ else if (bitsPerPixel === 24) {
205
+ metadata.colorType = "rgb";
206
+ }
207
+ else if (bitsPerPixel === 32) {
208
+ metadata.colorType = "rgba";
209
+ }
210
+ const compression = readUint32LE(data, 30);
211
+ if (compression === 0) {
212
+ metadata.compression = "none";
213
+ }
214
+ else if (compression === 1) {
215
+ metadata.compression = "rle8";
216
+ }
217
+ else if (compression === 2) {
218
+ metadata.compression = "rle4";
219
+ }
220
+ else if (compression === 3) {
221
+ metadata.compression = "bitfields";
222
+ }
223
+ else {
224
+ metadata.compression = `unknown-${compression}`;
225
+ }
226
+ // DPI information (pixels per meter)
227
+ if (headerSize >= 40) {
228
+ const xPelsPerMeter = readUint32LE(data, 38);
229
+ const yPelsPerMeter = readUint32LE(data, 42);
230
+ if (xPelsPerMeter > 0) {
231
+ metadata.dpiX = Math.round(xPelsPerMeter * 0.0254);
232
+ }
233
+ if (yPelsPerMeter > 0) {
234
+ metadata.dpiY = Math.round(yPelsPerMeter * 0.0254);
235
+ }
236
+ }
237
+ }
238
+ else {
239
+ // BITMAPCOREHEADER
240
+ metadata.compression = "none";
241
+ }
242
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
243
+ }
171
244
  }
@@ -1,4 +1,4 @@
1
- import type { ImageData } from "../types.js";
1
+ import type { ImageData, ImageMetadata } from "../types.js";
2
2
  import { TIFFFormat } from "./tiff.js";
3
3
  /**
4
4
  * DNG format handler
@@ -22,6 +22,18 @@ export declare class DNGFormat extends TIFFFormat {
22
22
  * @param imageData Image data to encode
23
23
  * @returns Encoded DNG image bytes
24
24
  */
25
- encode(imageData: ImageData): Promise<Uint8Array>;
25
+ encode(imageData: ImageData, _options?: unknown): Promise<Uint8Array>;
26
+ /**
27
+ * Get the list of metadata fields supported by DNG format
28
+ * DNG is based on TIFF, so it supports all TIFF metadata fields
29
+ */
30
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
31
+ /**
32
+ * Extract metadata from DNG data without fully decoding the pixel data
33
+ * DNG is TIFF-based, so we can use the parent extractMetadata and override format
34
+ * @param data Raw DNG data
35
+ * @returns Extracted metadata or undefined
36
+ */
37
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
26
38
  }
27
39
  //# sourceMappingURL=dng.d.ts.map
@@ -22,10 +22,6 @@ export class DNGFormat extends TIFFFormat {
22
22
  writable: true,
23
23
  value: "image/x-adobe-dng"
24
24
  });
25
- // Helper methods duplicated from TIFFFormat because they are protected/private there
26
- // and we can't easily access them if they are private.
27
- // Let's check TIFFFormat visibility.
28
- // The read/write helpers were not exported in the previous read_file output.
29
25
  }
30
26
  /**
31
27
  * Check if the given data is a DNG image
@@ -78,7 +74,7 @@ export class DNGFormat extends TIFFFormat {
78
74
  * @param imageData Image data to encode
79
75
  * @returns Encoded DNG image bytes
80
76
  */
81
- encode(imageData) {
77
+ encode(imageData, _options) {
82
78
  const { width, height, data } = imageData;
83
79
  // We'll create a Linear DNG (demosaiced RGB)
84
80
  // This is very similar to a standard TIFF but with specific tags.
@@ -188,4 +184,30 @@ export class DNGFormat extends TIFFFormat {
188
184
  }
189
185
  return Promise.resolve(new Uint8Array(result));
190
186
  }
187
+ // Helper methods duplicated from TIFFFormat because they are protected/private there
188
+ // and we can't easily access them if they are private.
189
+ // Let's check TIFFFormat visibility.
190
+ // The read/write helpers were not exported in the previous read_file output.
191
+ /**
192
+ * Get the list of metadata fields supported by DNG format
193
+ * DNG is based on TIFF, so it supports all TIFF metadata fields
194
+ */
195
+ getSupportedMetadata() {
196
+ return super.getSupportedMetadata();
197
+ }
198
+ /**
199
+ * Extract metadata from DNG data without fully decoding the pixel data
200
+ * DNG is TIFF-based, so we can use the parent extractMetadata and override format
201
+ * @param data Raw DNG data
202
+ * @returns Extracted metadata or undefined
203
+ */
204
+ async extractMetadata(data) {
205
+ // Use parent TIFF extractMetadata
206
+ const metadata = await super.extractMetadata(data);
207
+ if (metadata) {
208
+ // Override format to indicate this is a DNG
209
+ metadata.format = "dng";
210
+ }
211
+ return metadata;
212
+ }
191
213
  }
@@ -1,4 +1,4 @@
1
- import type { ImageData, ImageFormat, MultiFrameImageData } from "../types.js";
1
+ import type { ImageData, ImageDecoderOptions, ImageFormat, ImageMetadata, MultiFrameImageData } from "../types.js";
2
2
  /**
3
3
  * GIF format handler
4
4
  * Now includes pure-JS implementation with custom LZW compression/decompression
@@ -32,18 +32,18 @@ export declare class GIFFormat implements ImageFormat {
32
32
  * @param data Raw GIF image data
33
33
  * @returns Decoded image data with RGBA pixels of first frame
34
34
  */
35
- decode(data: Uint8Array): Promise<ImageData>;
36
- private extractMetadata;
35
+ decode(data: Uint8Array, settings?: ImageDecoderOptions): Promise<ImageData>;
36
+ private extractGIFMetadata;
37
37
  /**
38
38
  * Encode RGBA image data to GIF format (single frame)
39
39
  * @param imageData Image data to encode
40
40
  * @returns Encoded GIF image bytes
41
41
  */
42
- encode(imageData: ImageData): Promise<Uint8Array>;
42
+ encode(imageData: ImageData, _options?: unknown): Promise<Uint8Array>;
43
43
  /**
44
44
  * Decode all frames from an animated GIF
45
45
  */
46
- decodeFrames(data: Uint8Array): Promise<MultiFrameImageData>;
46
+ decodeFrames(data: Uint8Array, settings?: ImageDecoderOptions): Promise<MultiFrameImageData>;
47
47
  /**
48
48
  * Encode multi-frame image data to animated GIF
49
49
  */
@@ -55,5 +55,18 @@ export declare class GIFFormat implements ImageFormat {
55
55
  private parseXMP;
56
56
  private injectMetadata;
57
57
  private createCommentText;
58
+ /**
59
+ * Get the list of metadata fields supported by GIF format
60
+ */
61
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
62
+ /**
63
+ * Extract metadata from GIF data without fully decoding the pixel data
64
+ * This quickly parses GIF structure to extract metadata including frame count
65
+ * @param data Raw GIF data
66
+ * @returns Extracted metadata or undefined
67
+ */
68
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
69
+ private getSubBlocksSize;
70
+ private parseGIFCommentMetadata;
58
71
  }
59
72
  //# sourceMappingURL=gif.d.ts.map
@@ -56,18 +56,21 @@ export class GIFFormat {
56
56
  * @param data Raw GIF image data
57
57
  * @returns Decoded image data with RGBA pixels of first frame
58
58
  */
59
- async decode(data) {
59
+ async decode(data, settings) {
60
60
  if (!this.canDecode(data)) {
61
61
  throw new Error("Invalid GIF signature");
62
62
  }
63
- // Try pure-JS decoder first
63
+ // Try pure-JS decoder first with tolerant decoding enabled by default
64
64
  try {
65
- const decoder = new GIFDecoder(data);
65
+ const decoder = new GIFDecoder(data, {
66
+ tolerantDecoding: settings?.tolerantDecoding ?? true,
67
+ onWarning: settings?.onWarning,
68
+ });
66
69
  const result = decoder.decode();
67
70
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
68
71
  validateImageDimensions(result.width, result.height);
69
72
  // Extract metadata from comment extensions
70
- const metadata = this.extractMetadata(data);
73
+ const metadata = this.extractGIFMetadata(data);
71
74
  return {
72
75
  width: result.width,
73
76
  height: result.height,
@@ -75,9 +78,8 @@ export class GIFFormat {
75
78
  metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
76
79
  };
77
80
  }
78
- catch (error) {
81
+ catch (_error) {
79
82
  // Fall back to runtime decoder if pure-JS fails
80
- console.warn("Pure-JS GIF decoder failed, falling back to runtime:", error);
81
83
  let pos = 6; // Skip "GIF89a" or "GIF87a"
82
84
  const width = readUint16LE(data, pos);
83
85
  pos += 2;
@@ -85,7 +87,7 @@ export class GIFFormat {
85
87
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
86
88
  validateImageDimensions(width, height);
87
89
  const rgba = await this.decodeUsingRuntime(data, width, height);
88
- const metadata = this.extractMetadata(data);
90
+ const metadata = this.extractGIFMetadata(data);
89
91
  return {
90
92
  width,
91
93
  height,
@@ -94,7 +96,7 @@ export class GIFFormat {
94
96
  };
95
97
  }
96
98
  }
97
- extractMetadata(data) {
99
+ extractGIFMetadata(data) {
98
100
  const metadata = {};
99
101
  let pos = 6; // Skip "GIF89a" or "GIF87a"
100
102
  // Read logical screen descriptor
@@ -151,7 +153,7 @@ export class GIFFormat {
151
153
  * @param imageData Image data to encode
152
154
  * @returns Encoded GIF image bytes
153
155
  */
154
- async encode(imageData) {
156
+ async encode(imageData, _options) {
155
157
  const { width, height, data, metadata } = imageData;
156
158
  // Try pure-JS encoder first
157
159
  try {
@@ -164,9 +166,8 @@ export class GIFFormat {
164
166
  }
165
167
  return encoded;
166
168
  }
167
- catch (error) {
169
+ catch (_error) {
168
170
  // Fall back to runtime encoding if pure-JS fails
169
- console.warn("Pure-JS GIF encoder failed, falling back to runtime:", error);
170
171
  if (typeof OffscreenCanvas !== "undefined") {
171
172
  try {
172
173
  const canvas = new OffscreenCanvas(width, height);
@@ -199,15 +200,18 @@ export class GIFFormat {
199
200
  /**
200
201
  * Decode all frames from an animated GIF
201
202
  */
202
- decodeFrames(data) {
203
+ decodeFrames(data, settings) {
203
204
  if (!this.canDecode(data)) {
204
205
  throw new Error("Invalid GIF signature");
205
206
  }
206
207
  try {
207
- const decoder = new GIFDecoder(data);
208
+ const decoder = new GIFDecoder(data, {
209
+ tolerantDecoding: settings?.tolerantDecoding ?? true,
210
+ onWarning: settings?.onWarning,
211
+ });
208
212
  const result = decoder.decodeAllFrames();
209
213
  // Extract metadata from comment extensions
210
- const metadata = this.extractMetadata(data);
214
+ const metadata = this.extractGIFMetadata(data);
211
215
  return Promise.resolve({
212
216
  width: result.width,
213
217
  height: result.height,
@@ -402,4 +406,146 @@ export class GIFFormat {
402
406
  parts.push(`Copyright: ${metadata.copyright}`);
403
407
  return parts.length > 0 ? parts.join("\n") : null;
404
408
  }
409
+ /**
410
+ * Get the list of metadata fields supported by GIF format
411
+ */
412
+ getSupportedMetadata() {
413
+ return [
414
+ "title", // Comment extension
415
+ "description", // Comment extension
416
+ "author", // Comment extension
417
+ "copyright", // Comment extension
418
+ "frameCount", // Animation frames
419
+ ];
420
+ }
421
+ /**
422
+ * Extract metadata from GIF data without fully decoding the pixel data
423
+ * This quickly parses GIF structure to extract metadata including frame count
424
+ * @param data Raw GIF data
425
+ * @returns Extracted metadata or undefined
426
+ */
427
+ extractMetadata(data) {
428
+ if (!this.canDecode(data)) {
429
+ return Promise.resolve(undefined);
430
+ }
431
+ const metadata = {
432
+ format: "gif",
433
+ compression: "lzw",
434
+ frameCount: 0,
435
+ bitDepth: 8,
436
+ colorType: "indexed",
437
+ };
438
+ let pos = 6; // Skip "GIF89a" or "GIF87a"
439
+ // Read Logical Screen Descriptor
440
+ const _width = readUint16LE(data, pos);
441
+ pos += 2;
442
+ const _height = readUint16LE(data, pos);
443
+ pos += 2;
444
+ const packed = data[pos++];
445
+ const _backgroundColorIndex = data[pos++];
446
+ const _aspectRatio = data[pos++];
447
+ // Parse packed fields
448
+ const hasGlobalColorTable = (packed & 0x80) !== 0;
449
+ const globalColorTableSize = 2 << (packed & 0x07);
450
+ // Skip global color table if present
451
+ if (hasGlobalColorTable) {
452
+ pos += globalColorTableSize * 3;
453
+ }
454
+ // Parse data stream to count frames and extract metadata
455
+ while (pos < data.length) {
456
+ const separator = data[pos++];
457
+ if (separator === 0x21) {
458
+ // Extension
459
+ const label = data[pos++];
460
+ if (label === 0xf9) {
461
+ // Graphics Control Extension - indicates a frame
462
+ metadata.frameCount++;
463
+ // Skip block
464
+ const blockSize = data[pos++];
465
+ pos += blockSize;
466
+ pos++; // Block terminator
467
+ }
468
+ else if (label === 0xfe) {
469
+ // Comment Extension
470
+ const commentResult = this.readDataSubBlocks(data, pos);
471
+ pos = commentResult.endPos;
472
+ this.parseGIFCommentMetadata(commentResult.text, metadata);
473
+ }
474
+ else if (label === 0xff) {
475
+ // Application Extension
476
+ const blockSize = data[pos++];
477
+ pos += blockSize;
478
+ // Skip sub-blocks
479
+ pos += this.getSubBlocksSize(data, pos);
480
+ }
481
+ else {
482
+ // Other extension - skip sub-blocks
483
+ pos += this.getSubBlocksSize(data, pos);
484
+ }
485
+ }
486
+ else if (separator === 0x2c) {
487
+ // Image Descriptor - frame data
488
+ if (metadata.frameCount === 0) {
489
+ metadata.frameCount = 1;
490
+ }
491
+ pos += 8; // Skip left, top, width, height
492
+ const localPacked = data[pos++];
493
+ const hasLocalColorTable = (localPacked & 0x80) !== 0;
494
+ if (hasLocalColorTable) {
495
+ const localColorTableSize = 2 << (localPacked & 0x07);
496
+ pos += localColorTableSize * 3;
497
+ }
498
+ // Skip LZW minimum code size
499
+ pos++;
500
+ // Skip image data sub-blocks
501
+ pos += this.getSubBlocksSize(data, pos);
502
+ }
503
+ else if (separator === 0x3b) {
504
+ // Trailer - end of GIF
505
+ break;
506
+ }
507
+ else if (separator === 0x00) {
508
+ // Padding - skip
509
+ continue;
510
+ }
511
+ else {
512
+ // Unknown - try to continue
513
+ break;
514
+ }
515
+ }
516
+ // If no frames were counted, assume at least 1
517
+ if (metadata.frameCount === 0) {
518
+ metadata.frameCount = 1;
519
+ }
520
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
521
+ }
522
+ getSubBlocksSize(data, pos) {
523
+ let size = 0;
524
+ while (pos + size < data.length) {
525
+ const blockSize = data[pos + size];
526
+ size++; // For block size byte
527
+ if (blockSize === 0)
528
+ break;
529
+ size += blockSize;
530
+ }
531
+ return size;
532
+ }
533
+ parseGIFCommentMetadata(commentText, metadata) {
534
+ const lines = commentText.split("\n");
535
+ for (const line of lines) {
536
+ const colonIndex = line.indexOf(":");
537
+ if (colonIndex > 0) {
538
+ const key = line.substring(0, colonIndex).trim();
539
+ const value = line.substring(colonIndex + 1).trim();
540
+ if (key === "Title")
541
+ metadata.title = value;
542
+ else if (key === "Description")
543
+ metadata.description = value;
544
+ else if (key === "Author")
545
+ metadata.author = value;
546
+ else if (key === "Copyright")
547
+ metadata.copyright = value;
548
+ }
549
+ }
550
+ }
405
551
  }
@@ -0,0 +1,96 @@
1
+ import type { ImageData, ImageDecoderOptions, ImageFormat, ImageMetadata } from "../types.js";
2
+ /**
3
+ * HEIC format handler
4
+ * Supports HEIC/HEIF images using runtime APIs (ImageDecoder/OffscreenCanvas)
5
+ * Note: Pure JavaScript encode/decode is not supported due to complexity
6
+ */
7
+ export declare class HEICFormat implements ImageFormat {
8
+ /** Format name identifier */
9
+ readonly name = "heic";
10
+ /** MIME type for HEIC images */
11
+ readonly mimeType = "image/heic";
12
+ /**
13
+ * Check if the given data is a HEIC/HEIF image
14
+ * @param data Raw image data to check
15
+ * @returns true if data has HEIC/HEIF signature
16
+ */
17
+ canDecode(data: Uint8Array): boolean;
18
+ /**
19
+ * Decode HEIC image data to RGBA
20
+ * Uses runtime APIs (ImageDecoder) for decoding
21
+ * @param data Raw HEIC image data
22
+ * @returns Decoded image data with RGBA pixels
23
+ */
24
+ decode(data: Uint8Array, settings?: ImageDecoderOptions): Promise<ImageData>;
25
+ /**
26
+ * Encode RGBA image data to HEIC format
27
+ * Uses runtime APIs (OffscreenCanvas) for encoding
28
+ *
29
+ * Note: Metadata injection is not currently implemented. Metadata may be lost during encoding
30
+ * as it would require parsing and modifying the ISOBMFF container structure.
31
+ *
32
+ * @param imageData Image data to encode
33
+ * @returns Encoded HEIC image bytes
34
+ */
35
+ encode(imageData: ImageData, _options?: unknown): Promise<Uint8Array>;
36
+ /**
37
+ * Decode using runtime APIs
38
+ * @param data Raw HEIC data
39
+ * @returns Decoded image dimensions and pixel data
40
+ */
41
+ private decodeUsingRuntime;
42
+ /**
43
+ * Parse EXIF metadata from HEIC data
44
+ *
45
+ * Note: This is a simplified implementation that searches for EXIF headers linearly.
46
+ * A full implementation would require navigating the ISOBMFF box structure to find
47
+ * the 'meta' box and then the 'Exif' item. This simplified approach may not work
48
+ * in all cases but is suitable for basic metadata extraction when runtime APIs are
49
+ * not available or as a fallback.
50
+ *
51
+ * @param data Raw HEIC data
52
+ * @param metadata Metadata object to populate
53
+ */
54
+ private parseEXIF;
55
+ /**
56
+ * Parse TIFF-formatted EXIF data
57
+ * @param data EXIF data in TIFF format
58
+ * @param metadata Metadata object to populate
59
+ */
60
+ private parseTIFFExif;
61
+ /**
62
+ * Parse Exif Sub-IFD for camera settings
63
+ * @param data EXIF data
64
+ * @param exifIfdOffset Offset to Exif Sub-IFD
65
+ * @param littleEndian Byte order
66
+ * @param metadata Metadata object to populate
67
+ */
68
+ private parseExifSubIFD;
69
+ /**
70
+ * Parse GPS IFD for location data
71
+ * @param data EXIF data
72
+ * @param gpsIfdOffset Offset to GPS IFD
73
+ * @param littleEndian Byte order
74
+ * @param metadata Metadata object to populate
75
+ */
76
+ private parseGPSIFD;
77
+ /**
78
+ * Read a rational value (numerator/denominator)
79
+ * @param data Data buffer
80
+ * @param offset Offset to rational
81
+ * @param littleEndian Byte order
82
+ * @returns Decimal value
83
+ */
84
+ private readRational;
85
+ /**
86
+ * Get the list of metadata fields supported by HEIC format
87
+ */
88
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
89
+ /**
90
+ * Extract metadata from HEIC data without fully decoding the pixel data
91
+ * @param data Raw HEIC data
92
+ * @returns Extracted metadata or undefined
93
+ */
94
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
95
+ }
96
+ //# sourceMappingURL=heic.d.ts.map