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
@@ -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
  import { PNGBase } from "./png_base.js";
3
3
  /**
4
4
  * APNG (Animated PNG) format handler
@@ -46,5 +46,17 @@ export declare class APNGFormat extends PNGBase implements ImageFormat {
46
46
  */
47
47
  encodeFrames(imageData: MultiFrameImageData): Promise<Uint8Array>;
48
48
  private decodeFrameData;
49
+ /**
50
+ * Get the list of metadata fields supported by APNG format
51
+ * Includes all PNG fields plus frame count
52
+ */
53
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
54
+ /**
55
+ * Extract metadata from APNG data without fully decoding the pixel data
56
+ * This quickly parses PNG chunks to extract metadata including frame count
57
+ * @param data Raw APNG data
58
+ * @returns Extracted metadata or undefined
59
+ */
60
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
49
61
  }
50
62
  //# sourceMappingURL=apng.d.ts.map
@@ -361,4 +361,101 @@ export class APNGFormat extends PNGBase {
361
361
  const rgba = this.unfilterAndConvert(decompressed, width, height, bitDepth, colorType);
362
362
  return rgba;
363
363
  }
364
+ /**
365
+ * Get the list of metadata fields supported by APNG format
366
+ * Includes all PNG fields plus frame count
367
+ */
368
+ getSupportedMetadata() {
369
+ return [
370
+ ...super.getSupportedMetadata(),
371
+ "frameCount", // acTL chunk
372
+ ];
373
+ }
374
+ /**
375
+ * Extract metadata from APNG data without fully decoding the pixel data
376
+ * This quickly parses PNG chunks to extract metadata including frame count
377
+ * @param data Raw APNG data
378
+ * @returns Extracted metadata or undefined
379
+ */
380
+ extractMetadata(data) {
381
+ if (!this.canDecode(data)) {
382
+ return Promise.resolve(undefined);
383
+ }
384
+ let pos = 8; // Skip PNG signature
385
+ let width = 0;
386
+ let height = 0;
387
+ let frameCount = 0;
388
+ const metadata = {
389
+ format: "apng",
390
+ compression: "deflate",
391
+ };
392
+ // Parse chunks for metadata only
393
+ while (pos < data.length) {
394
+ if (pos + 8 > data.length)
395
+ break;
396
+ const length = this.readUint32(data, pos);
397
+ pos += 4;
398
+ const type = String.fromCharCode(data[pos], data[pos + 1], data[pos + 2], data[pos + 3]);
399
+ pos += 4;
400
+ if (pos + length + 4 > data.length)
401
+ break;
402
+ const chunkData = data.slice(pos, pos + length);
403
+ pos += length;
404
+ pos += 4; // Skip CRC
405
+ if (type === "IHDR") {
406
+ width = this.readUint32(chunkData, 0);
407
+ height = this.readUint32(chunkData, 4);
408
+ // Parse bit depth and color type from IHDR
409
+ if (chunkData.length >= 9) {
410
+ metadata.bitDepth = chunkData[8];
411
+ const colorTypeCode = chunkData[9];
412
+ // PNG color types: 0=grayscale, 2=rgb, 3=indexed, 4=grayscale+alpha, 6=rgba
413
+ switch (colorTypeCode) {
414
+ case 0:
415
+ metadata.colorType = "grayscale";
416
+ break;
417
+ case 2:
418
+ metadata.colorType = "rgb";
419
+ break;
420
+ case 3:
421
+ metadata.colorType = "indexed";
422
+ break;
423
+ case 4:
424
+ metadata.colorType = "grayscale-alpha";
425
+ break;
426
+ case 6:
427
+ metadata.colorType = "rgba";
428
+ break;
429
+ }
430
+ }
431
+ }
432
+ else if (type === "acTL") {
433
+ // Animation control chunk - contains frame count
434
+ if (chunkData.length >= 4) {
435
+ frameCount = this.readUint32(chunkData, 0);
436
+ metadata.frameCount = frameCount;
437
+ }
438
+ }
439
+ else if (type === "pHYs") {
440
+ // Physical pixel dimensions
441
+ this.parsePhysChunk(chunkData, metadata, width, height);
442
+ }
443
+ else if (type === "tEXt") {
444
+ // Text chunk
445
+ this.parseTextChunk(chunkData, metadata);
446
+ }
447
+ else if (type === "iTXt") {
448
+ // International text chunk
449
+ this.parseITxtChunk(chunkData, metadata);
450
+ }
451
+ else if (type === "eXIf") {
452
+ // EXIF chunk
453
+ this.parseExifChunk(chunkData, metadata);
454
+ }
455
+ else if (type === "IEND") {
456
+ break;
457
+ }
458
+ }
459
+ return Promise.resolve(Object.keys(metadata).length > 0 ? metadata : undefined);
460
+ }
364
461
  }
@@ -1,4 +1,4 @@
1
- import type { ASCIIOptions, ImageData, ImageFormat } from "../types.js";
1
+ import type { ASCIIOptions, ImageData, ImageFormat, ImageMetadata } from "../types.js";
2
2
  /**
3
3
  * ASCII format handler
4
4
  * Converts images to ASCII art text representation
@@ -34,5 +34,15 @@ export declare class ASCIIFormat implements ImageFormat {
34
34
  * Parse options from the options line
35
35
  */
36
36
  private parseOptions;
37
+ /**
38
+ * Get the list of metadata fields supported by ASCII format
39
+ */
40
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
41
+ /**
42
+ * Extract metadata from ASCII art data without fully decoding
43
+ * @param data Raw ASCII art data
44
+ * @returns Extracted metadata or undefined
45
+ */
46
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
37
47
  }
38
48
  //# sourceMappingURL=ascii.d.ts.map
@@ -183,4 +183,28 @@ export class ASCIIFormat {
183
183
  }
184
184
  return options;
185
185
  }
186
+ /**
187
+ * Get the list of metadata fields supported by ASCII format
188
+ */
189
+ getSupportedMetadata() {
190
+ return []; // ASCII art doesn't support metadata preservation
191
+ }
192
+ /**
193
+ * Extract metadata from ASCII art data without fully decoding
194
+ * @param data Raw ASCII art data
195
+ * @returns Extracted metadata or undefined
196
+ */
197
+ extractMetadata(data) {
198
+ if (!this.canDecode(data)) {
199
+ return Promise.resolve(undefined);
200
+ }
201
+ const metadata = {
202
+ format: "ascii",
203
+ compression: "none",
204
+ frameCount: 1,
205
+ bitDepth: 1, // ASCII is 1-bit (character or no character)
206
+ colorType: "grayscale",
207
+ };
208
+ return Promise.resolve(metadata);
209
+ }
186
210
  }
@@ -0,0 +1,96 @@
1
+ import type { ImageData, ImageFormat, ImageMetadata } from "../types.js";
2
+ /**
3
+ * AVIF format handler
4
+ * Supports AVIF images using runtime APIs (ImageDecoder/OffscreenCanvas)
5
+ * Note: Pure JavaScript encode/decode is not supported due to complexity
6
+ */
7
+ export declare class AVIFFormat implements ImageFormat {
8
+ /** Format name identifier */
9
+ readonly name = "avif";
10
+ /** MIME type for AVIF images */
11
+ readonly mimeType = "image/avif";
12
+ /**
13
+ * Check if the given data is an AVIF image
14
+ * @param data Raw image data to check
15
+ * @returns true if data has AVIF signature
16
+ */
17
+ canDecode(data: Uint8Array): boolean;
18
+ /**
19
+ * Decode AVIF image data to RGBA
20
+ * Uses runtime APIs (ImageDecoder) for decoding
21
+ * @param data Raw AVIF image data
22
+ * @returns Decoded image data with RGBA pixels
23
+ */
24
+ decode(data: Uint8Array): Promise<ImageData>;
25
+ /**
26
+ * Encode RGBA image data to AVIF 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 AVIF image bytes
34
+ */
35
+ encode(imageData: ImageData): Promise<Uint8Array>;
36
+ /**
37
+ * Decode using runtime APIs
38
+ * @param data Raw AVIF data
39
+ * @returns Decoded image dimensions and pixel data
40
+ */
41
+ private decodeUsingRuntime;
42
+ /**
43
+ * Parse EXIF metadata from AVIF 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 AVIF 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 AVIF format
87
+ */
88
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
89
+ /**
90
+ * Extract metadata from AVIF data without fully decoding the pixel data
91
+ * @param data Raw AVIF data
92
+ * @returns Extracted metadata or undefined
93
+ */
94
+ extractMetadata(data: Uint8Array): Promise<ImageMetadata | undefined>;
95
+ }
96
+ //# sourceMappingURL=avif.d.ts.map