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
@@ -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
@@ -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
@@ -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
@@ -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, 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
@@ -67,7 +67,7 @@ export class GIFFormat {
67
67
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
68
68
  validateImageDimensions(result.width, result.height);
69
69
  // Extract metadata from comment extensions
70
- const metadata = this.extractMetadata(data);
70
+ const metadata = this.extractGIFMetadata(data);
71
71
  return {
72
72
  width: result.width,
73
73
  height: result.height,
@@ -85,7 +85,7 @@ export class GIFFormat {
85
85
  // Validate dimensions for security (prevent integer overflow and heap exhaustion)
86
86
  validateImageDimensions(width, height);
87
87
  const rgba = await this.decodeUsingRuntime(data, width, height);
88
- const metadata = this.extractMetadata(data);
88
+ const metadata = this.extractGIFMetadata(data);
89
89
  return {
90
90
  width,
91
91
  height,
@@ -94,7 +94,7 @@ export class GIFFormat {
94
94
  };
95
95
  }
96
96
  }
97
- extractMetadata(data) {
97
+ extractGIFMetadata(data) {
98
98
  const metadata = {};
99
99
  let pos = 6; // Skip "GIF89a" or "GIF87a"
100
100
  // Read logical screen descriptor
@@ -207,7 +207,7 @@ export class GIFFormat {
207
207
  const decoder = new GIFDecoder(data);
208
208
  const result = decoder.decodeAllFrames();
209
209
  // Extract metadata from comment extensions
210
- const metadata = this.extractMetadata(data);
210
+ const metadata = this.extractGIFMetadata(data);
211
211
  return Promise.resolve({
212
212
  width: result.width,
213
213
  height: result.height,
@@ -402,4 +402,146 @@ export class GIFFormat {
402
402
  parts.push(`Copyright: ${metadata.copyright}`);
403
403
  return parts.length > 0 ? parts.join("\n") : null;
404
404
  }
405
+ /**
406
+ * Get the list of metadata fields supported by GIF format
407
+ */
408
+ getSupportedMetadata() {
409
+ return [
410
+ "title", // Comment extension
411
+ "description", // Comment extension
412
+ "author", // Comment extension
413
+ "copyright", // Comment extension
414
+ "frameCount", // Animation frames
415
+ ];
416
+ }
417
+ /**
418
+ * Extract metadata from GIF data without fully decoding the pixel data
419
+ * This quickly parses GIF structure to extract metadata including frame count
420
+ * @param data Raw GIF data
421
+ * @returns Extracted metadata or undefined
422
+ */
423
+ extractMetadata(data) {
424
+ if (!this.canDecode(data)) {
425
+ return Promise.resolve(undefined);
426
+ }
427
+ const metadata = {
428
+ format: "gif",
429
+ compression: "lzw",
430
+ frameCount: 0,
431
+ bitDepth: 8,
432
+ colorType: "indexed",
433
+ };
434
+ let pos = 6; // Skip "GIF89a" or "GIF87a"
435
+ // Read Logical Screen Descriptor
436
+ const _width = readUint16LE(data, pos);
437
+ pos += 2;
438
+ const _height = readUint16LE(data, pos);
439
+ pos += 2;
440
+ const packed = data[pos++];
441
+ const _backgroundColorIndex = data[pos++];
442
+ const _aspectRatio = data[pos++];
443
+ // Parse packed fields
444
+ const hasGlobalColorTable = (packed & 0x80) !== 0;
445
+ const globalColorTableSize = 2 << (packed & 0x07);
446
+ // Skip global color table if present
447
+ if (hasGlobalColorTable) {
448
+ pos += globalColorTableSize * 3;
449
+ }
450
+ // Parse data stream to count frames and extract metadata
451
+ while (pos < data.length) {
452
+ const separator = data[pos++];
453
+ if (separator === 0x21) {
454
+ // Extension
455
+ const label = data[pos++];
456
+ if (label === 0xf9) {
457
+ // Graphics Control Extension - indicates a frame
458
+ metadata.frameCount++;
459
+ // Skip block
460
+ const blockSize = data[pos++];
461
+ pos += blockSize;
462
+ pos++; // Block terminator
463
+ }
464
+ else if (label === 0xfe) {
465
+ // Comment Extension
466
+ const commentResult = this.readDataSubBlocks(data, pos);
467
+ pos = commentResult.endPos;
468
+ this.parseGIFCommentMetadata(commentResult.text, metadata);
469
+ }
470
+ else if (label === 0xff) {
471
+ // Application Extension
472
+ const blockSize = data[pos++];
473
+ pos += blockSize;
474
+ // Skip sub-blocks
475
+ pos += this.getSubBlocksSize(data, pos);
476
+ }
477
+ else {
478
+ // Other extension - skip sub-blocks
479
+ pos += this.getSubBlocksSize(data, pos);
480
+ }
481
+ }
482
+ else if (separator === 0x2c) {
483
+ // Image Descriptor - frame data
484
+ if (metadata.frameCount === 0) {
485
+ metadata.frameCount = 1;
486
+ }
487
+ pos += 8; // Skip left, top, width, height
488
+ const localPacked = data[pos++];
489
+ const hasLocalColorTable = (localPacked & 0x80) !== 0;
490
+ if (hasLocalColorTable) {
491
+ const localColorTableSize = 2 << (localPacked & 0x07);
492
+ pos += localColorTableSize * 3;
493
+ }
494
+ // Skip LZW minimum code size
495
+ pos++;
496
+ // Skip image data sub-blocks
497
+ pos += this.getSubBlocksSize(data, pos);
498
+ }
499
+ else if (separator === 0x3b) {
500
+ // Trailer - end of GIF
501
+ break;
502
+ }
503
+ else if (separator === 0x00) {
504
+ // Padding - skip
505
+ continue;
506
+ }
507
+ else {
508
+ // Unknown - try to continue
509
+ break;
510
+ }
511
+ }
512
+ // If no frames were counted, assume at least 1
513
+ if (metadata.frameCount === 0) {
514
+ metadata.frameCount = 1;
515
+ }
516
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
517
+ }
518
+ getSubBlocksSize(data, pos) {
519
+ let size = 0;
520
+ while (pos + size < data.length) {
521
+ const blockSize = data[pos + size];
522
+ size++; // For block size byte
523
+ if (blockSize === 0)
524
+ break;
525
+ size += blockSize;
526
+ }
527
+ return size;
528
+ }
529
+ parseGIFCommentMetadata(commentText, metadata) {
530
+ const lines = commentText.split("\n");
531
+ for (const line of lines) {
532
+ const colonIndex = line.indexOf(":");
533
+ if (colonIndex > 0) {
534
+ const key = line.substring(0, colonIndex).trim();
535
+ const value = line.substring(colonIndex + 1).trim();
536
+ if (key === "Title")
537
+ metadata.title = value;
538
+ else if (key === "Description")
539
+ metadata.description = value;
540
+ else if (key === "Author")
541
+ metadata.author = value;
542
+ else if (key === "Copyright")
543
+ metadata.copyright = value;
544
+ }
545
+ }
546
+ }
405
547
  }
@@ -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