cross-image 0.2.3 → 0.2.4

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 (72) hide show
  1. package/README.md +333 -289
  2. package/esm/mod.d.ts +2 -0
  3. package/esm/mod.js +2 -0
  4. package/esm/src/formats/apng.d.ts +13 -1
  5. package/esm/src/formats/apng.js +97 -0
  6. package/esm/src/formats/ascii.d.ts +11 -1
  7. package/esm/src/formats/ascii.js +24 -0
  8. package/esm/src/formats/avif.d.ts +96 -0
  9. package/esm/src/formats/avif.js +607 -0
  10. package/esm/src/formats/bmp.d.ts +11 -1
  11. package/esm/src/formats/bmp.js +73 -0
  12. package/esm/src/formats/dng.d.ts +13 -1
  13. package/esm/src/formats/dng.js +26 -4
  14. package/esm/src/formats/gif.d.ts +15 -2
  15. package/esm/src/formats/gif.js +146 -4
  16. package/esm/src/formats/heic.d.ts +96 -0
  17. package/esm/src/formats/heic.js +608 -0
  18. package/esm/src/formats/ico.d.ts +11 -1
  19. package/esm/src/formats/ico.js +28 -0
  20. package/esm/src/formats/jpeg.d.ts +7 -0
  21. package/esm/src/formats/jpeg.js +76 -0
  22. package/esm/src/formats/pam.d.ts +11 -1
  23. package/esm/src/formats/pam.js +66 -0
  24. package/esm/src/formats/pcx.d.ts +11 -1
  25. package/esm/src/formats/pcx.js +45 -0
  26. package/esm/src/formats/png.d.ts +13 -1
  27. package/esm/src/formats/png.js +87 -0
  28. package/esm/src/formats/ppm.d.ts +11 -1
  29. package/esm/src/formats/ppm.js +34 -0
  30. package/esm/src/formats/tiff.d.ts +7 -0
  31. package/esm/src/formats/tiff.js +134 -0
  32. package/esm/src/formats/webp.d.ts +7 -0
  33. package/esm/src/formats/webp.js +92 -0
  34. package/esm/src/image.d.ts +9 -0
  35. package/esm/src/image.js +28 -0
  36. package/esm/src/types.d.ts +18 -0
  37. package/package.json +18 -1
  38. package/script/mod.d.ts +2 -0
  39. package/script/mod.js +5 -1
  40. package/script/src/formats/apng.d.ts +13 -1
  41. package/script/src/formats/apng.js +97 -0
  42. package/script/src/formats/ascii.d.ts +11 -1
  43. package/script/src/formats/ascii.js +24 -0
  44. package/script/src/formats/avif.d.ts +96 -0
  45. package/script/src/formats/avif.js +611 -0
  46. package/script/src/formats/bmp.d.ts +11 -1
  47. package/script/src/formats/bmp.js +73 -0
  48. package/script/src/formats/dng.d.ts +13 -1
  49. package/script/src/formats/dng.js +26 -4
  50. package/script/src/formats/gif.d.ts +15 -2
  51. package/script/src/formats/gif.js +146 -4
  52. package/script/src/formats/heic.d.ts +96 -0
  53. package/script/src/formats/heic.js +612 -0
  54. package/script/src/formats/ico.d.ts +11 -1
  55. package/script/src/formats/ico.js +28 -0
  56. package/script/src/formats/jpeg.d.ts +7 -0
  57. package/script/src/formats/jpeg.js +76 -0
  58. package/script/src/formats/pam.d.ts +11 -1
  59. package/script/src/formats/pam.js +66 -0
  60. package/script/src/formats/pcx.d.ts +11 -1
  61. package/script/src/formats/pcx.js +45 -0
  62. package/script/src/formats/png.d.ts +13 -1
  63. package/script/src/formats/png.js +87 -0
  64. package/script/src/formats/ppm.d.ts +11 -1
  65. package/script/src/formats/ppm.js +34 -0
  66. package/script/src/formats/tiff.d.ts +7 -0
  67. package/script/src/formats/tiff.js +134 -0
  68. package/script/src/formats/webp.d.ts +7 -0
  69. package/script/src/formats/webp.js +92 -0
  70. package/script/src/image.d.ts +9 -0
  71. package/script/src/image.js +28 -0
  72. package/script/src/types.d.ts +18 -0
@@ -171,5 +171,78 @@ class BMPFormat {
171
171
  }
172
172
  return Promise.resolve(result);
173
173
  }
174
+ /**
175
+ * Get the list of metadata fields supported by BMP format
176
+ */
177
+ getSupportedMetadata() {
178
+ return [
179
+ "dpiX", // Pixels per meter in header
180
+ "dpiY", // Pixels per meter in header
181
+ ];
182
+ }
183
+ /**
184
+ * Extract metadata from BMP data without fully decoding the pixel data
185
+ * @param data Raw BMP data
186
+ * @returns Extracted metadata or undefined
187
+ */
188
+ extractMetadata(data) {
189
+ if (!this.canDecode(data)) {
190
+ return Promise.resolve(undefined);
191
+ }
192
+ const metadata = {
193
+ format: "bmp",
194
+ frameCount: 1,
195
+ bitDepth: 24,
196
+ colorType: "rgb",
197
+ };
198
+ // Read BMP header to determine version
199
+ const headerSize = (0, byte_utils_js_1.readUint32LE)(data, 14);
200
+ if (headerSize >= 40) {
201
+ // BITMAPINFOHEADER or later
202
+ const bitsPerPixel = (0, byte_utils_js_1.readUint16LE)(data, 28);
203
+ metadata.bitDepth = bitsPerPixel;
204
+ if (bitsPerPixel === 1 || bitsPerPixel === 8) {
205
+ metadata.colorType = "indexed";
206
+ }
207
+ else if (bitsPerPixel === 24) {
208
+ metadata.colorType = "rgb";
209
+ }
210
+ else if (bitsPerPixel === 32) {
211
+ metadata.colorType = "rgba";
212
+ }
213
+ const compression = (0, byte_utils_js_1.readUint32LE)(data, 30);
214
+ if (compression === 0) {
215
+ metadata.compression = "none";
216
+ }
217
+ else if (compression === 1) {
218
+ metadata.compression = "rle8";
219
+ }
220
+ else if (compression === 2) {
221
+ metadata.compression = "rle4";
222
+ }
223
+ else if (compression === 3) {
224
+ metadata.compression = "bitfields";
225
+ }
226
+ else {
227
+ metadata.compression = `unknown-${compression}`;
228
+ }
229
+ // DPI information (pixels per meter)
230
+ if (headerSize >= 40) {
231
+ const xPelsPerMeter = (0, byte_utils_js_1.readUint32LE)(data, 38);
232
+ const yPelsPerMeter = (0, byte_utils_js_1.readUint32LE)(data, 42);
233
+ if (xPelsPerMeter > 0) {
234
+ metadata.dpiX = Math.round(xPelsPerMeter * 0.0254);
235
+ }
236
+ if (yPelsPerMeter > 0) {
237
+ metadata.dpiY = Math.round(yPelsPerMeter * 0.0254);
238
+ }
239
+ }
240
+ }
241
+ else {
242
+ // BITMAPCOREHEADER
243
+ metadata.compression = "none";
244
+ }
245
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
246
+ }
174
247
  }
175
248
  exports.BMPFormat = BMPFormat;
@@ -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
@@ -23,5 +23,17 @@ export declare class DNGFormat extends TIFFFormat {
23
23
  * @returns Encoded DNG image bytes
24
24
  */
25
25
  encode(imageData: ImageData): 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
@@ -25,10 +25,6 @@ class DNGFormat extends tiff_js_1.TIFFFormat {
25
25
  writable: true,
26
26
  value: "image/x-adobe-dng"
27
27
  });
28
- // Helper methods duplicated from TIFFFormat because they are protected/private there
29
- // and we can't easily access them if they are private.
30
- // Let's check TIFFFormat visibility.
31
- // The read/write helpers were not exported in the previous read_file output.
32
28
  }
33
29
  /**
34
30
  * Check if the given data is a DNG image
@@ -191,5 +187,31 @@ class DNGFormat extends tiff_js_1.TIFFFormat {
191
187
  }
192
188
  return Promise.resolve(new Uint8Array(result));
193
189
  }
190
+ // Helper methods duplicated from TIFFFormat because they are protected/private there
191
+ // and we can't easily access them if they are private.
192
+ // Let's check TIFFFormat visibility.
193
+ // The read/write helpers were not exported in the previous read_file output.
194
+ /**
195
+ * Get the list of metadata fields supported by DNG format
196
+ * DNG is based on TIFF, so it supports all TIFF metadata fields
197
+ */
198
+ getSupportedMetadata() {
199
+ return super.getSupportedMetadata();
200
+ }
201
+ /**
202
+ * Extract metadata from DNG data without fully decoding the pixel data
203
+ * DNG is TIFF-based, so we can use the parent extractMetadata and override format
204
+ * @param data Raw DNG data
205
+ * @returns Extracted metadata or undefined
206
+ */
207
+ async extractMetadata(data) {
208
+ // Use parent TIFF extractMetadata
209
+ const metadata = await super.extractMetadata(data);
210
+ if (metadata) {
211
+ // Override format to indicate this is a DNG
212
+ metadata.format = "dng";
213
+ }
214
+ return metadata;
215
+ }
194
216
  }
195
217
  exports.DNGFormat = DNGFormat;
@@ -1,4 +1,4 @@
1
- import type { ImageData, ImageFormat, MultiFrameImageData } from "../types.js";
1
+ import type { ImageData, 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
@@ -33,7 +33,7 @@ export declare class GIFFormat implements ImageFormat {
33
33
  * @returns Decoded image data with RGBA pixels of first frame
34
34
  */
35
35
  decode(data: Uint8Array): Promise<ImageData>;
36
- private extractMetadata;
36
+ private extractGIFMetadata;
37
37
  /**
38
38
  * Encode RGBA image data to GIF format (single frame)
39
39
  * @param imageData Image data to encode
@@ -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
@@ -70,7 +70,7 @@ class GIFFormat {
70
70
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
71
71
  (0, security_js_1.validateImageDimensions)(result.width, result.height);
72
72
  // Extract metadata from comment extensions
73
- const metadata = this.extractMetadata(data);
73
+ const metadata = this.extractGIFMetadata(data);
74
74
  return {
75
75
  width: result.width,
76
76
  height: result.height,
@@ -88,7 +88,7 @@ class GIFFormat {
88
88
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
89
89
  (0, security_js_1.validateImageDimensions)(width, height);
90
90
  const rgba = await this.decodeUsingRuntime(data, width, height);
91
- const metadata = this.extractMetadata(data);
91
+ const metadata = this.extractGIFMetadata(data);
92
92
  return {
93
93
  width,
94
94
  height,
@@ -97,7 +97,7 @@ class GIFFormat {
97
97
  };
98
98
  }
99
99
  }
100
- extractMetadata(data) {
100
+ extractGIFMetadata(data) {
101
101
  const metadata = {};
102
102
  let pos = 6; // Skip "GIF89a" or "GIF87a"
103
103
  // Read logical screen descriptor
@@ -210,7 +210,7 @@ class GIFFormat {
210
210
  const decoder = new gif_decoder_js_1.GIFDecoder(data);
211
211
  const result = decoder.decodeAllFrames();
212
212
  // Extract metadata from comment extensions
213
- const metadata = this.extractMetadata(data);
213
+ const metadata = this.extractGIFMetadata(data);
214
214
  return Promise.resolve({
215
215
  width: result.width,
216
216
  height: result.height,
@@ -405,5 +405,147 @@ class GIFFormat {
405
405
  parts.push(`Copyright: ${metadata.copyright}`);
406
406
  return parts.length > 0 ? parts.join("\n") : null;
407
407
  }
408
+ /**
409
+ * Get the list of metadata fields supported by GIF format
410
+ */
411
+ getSupportedMetadata() {
412
+ return [
413
+ "title", // Comment extension
414
+ "description", // Comment extension
415
+ "author", // Comment extension
416
+ "copyright", // Comment extension
417
+ "frameCount", // Animation frames
418
+ ];
419
+ }
420
+ /**
421
+ * Extract metadata from GIF data without fully decoding the pixel data
422
+ * This quickly parses GIF structure to extract metadata including frame count
423
+ * @param data Raw GIF data
424
+ * @returns Extracted metadata or undefined
425
+ */
426
+ extractMetadata(data) {
427
+ if (!this.canDecode(data)) {
428
+ return Promise.resolve(undefined);
429
+ }
430
+ const metadata = {
431
+ format: "gif",
432
+ compression: "lzw",
433
+ frameCount: 0,
434
+ bitDepth: 8,
435
+ colorType: "indexed",
436
+ };
437
+ let pos = 6; // Skip "GIF89a" or "GIF87a"
438
+ // Read Logical Screen Descriptor
439
+ const _width = (0, byte_utils_js_1.readUint16LE)(data, pos);
440
+ pos += 2;
441
+ const _height = (0, byte_utils_js_1.readUint16LE)(data, pos);
442
+ pos += 2;
443
+ const packed = data[pos++];
444
+ const _backgroundColorIndex = data[pos++];
445
+ const _aspectRatio = data[pos++];
446
+ // Parse packed fields
447
+ const hasGlobalColorTable = (packed & 0x80) !== 0;
448
+ const globalColorTableSize = 2 << (packed & 0x07);
449
+ // Skip global color table if present
450
+ if (hasGlobalColorTable) {
451
+ pos += globalColorTableSize * 3;
452
+ }
453
+ // Parse data stream to count frames and extract metadata
454
+ while (pos < data.length) {
455
+ const separator = data[pos++];
456
+ if (separator === 0x21) {
457
+ // Extension
458
+ const label = data[pos++];
459
+ if (label === 0xf9) {
460
+ // Graphics Control Extension - indicates a frame
461
+ metadata.frameCount++;
462
+ // Skip block
463
+ const blockSize = data[pos++];
464
+ pos += blockSize;
465
+ pos++; // Block terminator
466
+ }
467
+ else if (label === 0xfe) {
468
+ // Comment Extension
469
+ const commentResult = this.readDataSubBlocks(data, pos);
470
+ pos = commentResult.endPos;
471
+ this.parseGIFCommentMetadata(commentResult.text, metadata);
472
+ }
473
+ else if (label === 0xff) {
474
+ // Application Extension
475
+ const blockSize = data[pos++];
476
+ pos += blockSize;
477
+ // Skip sub-blocks
478
+ pos += this.getSubBlocksSize(data, pos);
479
+ }
480
+ else {
481
+ // Other extension - skip sub-blocks
482
+ pos += this.getSubBlocksSize(data, pos);
483
+ }
484
+ }
485
+ else if (separator === 0x2c) {
486
+ // Image Descriptor - frame data
487
+ if (metadata.frameCount === 0) {
488
+ metadata.frameCount = 1;
489
+ }
490
+ pos += 8; // Skip left, top, width, height
491
+ const localPacked = data[pos++];
492
+ const hasLocalColorTable = (localPacked & 0x80) !== 0;
493
+ if (hasLocalColorTable) {
494
+ const localColorTableSize = 2 << (localPacked & 0x07);
495
+ pos += localColorTableSize * 3;
496
+ }
497
+ // Skip LZW minimum code size
498
+ pos++;
499
+ // Skip image data sub-blocks
500
+ pos += this.getSubBlocksSize(data, pos);
501
+ }
502
+ else if (separator === 0x3b) {
503
+ // Trailer - end of GIF
504
+ break;
505
+ }
506
+ else if (separator === 0x00) {
507
+ // Padding - skip
508
+ continue;
509
+ }
510
+ else {
511
+ // Unknown - try to continue
512
+ break;
513
+ }
514
+ }
515
+ // If no frames were counted, assume at least 1
516
+ if (metadata.frameCount === 0) {
517
+ metadata.frameCount = 1;
518
+ }
519
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
520
+ }
521
+ getSubBlocksSize(data, pos) {
522
+ let size = 0;
523
+ while (pos + size < data.length) {
524
+ const blockSize = data[pos + size];
525
+ size++; // For block size byte
526
+ if (blockSize === 0)
527
+ break;
528
+ size += blockSize;
529
+ }
530
+ return size;
531
+ }
532
+ parseGIFCommentMetadata(commentText, metadata) {
533
+ const lines = commentText.split("\n");
534
+ for (const line of lines) {
535
+ const colonIndex = line.indexOf(":");
536
+ if (colonIndex > 0) {
537
+ const key = line.substring(0, colonIndex).trim();
538
+ const value = line.substring(colonIndex + 1).trim();
539
+ if (key === "Title")
540
+ metadata.title = value;
541
+ else if (key === "Description")
542
+ metadata.description = value;
543
+ else if (key === "Author")
544
+ metadata.author = value;
545
+ else if (key === "Copyright")
546
+ metadata.copyright = value;
547
+ }
548
+ }
549
+ }
408
550
  }
409
551
  exports.GIFFormat = GIFFormat;
@@ -0,0 +1,96 @@
1
+ import type { ImageData, 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): 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): 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