cross-image 0.2.2 → 0.2.3

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 (36) hide show
  1. package/README.md +128 -7
  2. package/esm/src/formats/jpeg.d.ts +12 -1
  3. package/esm/src/formats/jpeg.js +633 -4
  4. package/esm/src/formats/png_base.d.ts +8 -0
  5. package/esm/src/formats/png_base.js +176 -3
  6. package/esm/src/formats/tiff.d.ts +6 -1
  7. package/esm/src/formats/tiff.js +31 -0
  8. package/esm/src/formats/webp.d.ts +9 -2
  9. package/esm/src/formats/webp.js +211 -62
  10. package/esm/src/image.d.ts +51 -0
  11. package/esm/src/image.js +225 -5
  12. package/esm/src/types.d.ts +41 -1
  13. package/esm/src/utils/image_processing.d.ts +55 -0
  14. package/esm/src/utils/image_processing.js +210 -0
  15. package/esm/src/utils/metadata/xmp.d.ts +52 -0
  16. package/esm/src/utils/metadata/xmp.js +325 -0
  17. package/esm/src/utils/resize.d.ts +4 -0
  18. package/esm/src/utils/resize.js +74 -0
  19. package/package.json +1 -1
  20. package/script/src/formats/jpeg.d.ts +12 -1
  21. package/script/src/formats/jpeg.js +633 -4
  22. package/script/src/formats/png_base.d.ts +8 -0
  23. package/script/src/formats/png_base.js +176 -3
  24. package/script/src/formats/tiff.d.ts +6 -1
  25. package/script/src/formats/tiff.js +31 -0
  26. package/script/src/formats/webp.d.ts +9 -2
  27. package/script/src/formats/webp.js +211 -62
  28. package/script/src/image.d.ts +51 -0
  29. package/script/src/image.js +223 -3
  30. package/script/src/types.d.ts +41 -1
  31. package/script/src/utils/image_processing.d.ts +55 -0
  32. package/script/src/utils/image_processing.js +216 -0
  33. package/script/src/utils/metadata/xmp.d.ts +52 -0
  34. package/script/src/utils/metadata/xmp.js +333 -0
  35. package/script/src/utils/resize.d.ts +4 -0
  36. package/script/src/utils/resize.js +75 -0
@@ -24,6 +24,32 @@ export interface ImageMetadata {
24
24
  copyright?: string;
25
25
  /** Creation date */
26
26
  creationDate?: Date;
27
+ /** Camera make/manufacturer (e.g., "Canon", "Nikon") */
28
+ cameraMake?: string;
29
+ /** Camera model (e.g., "Canon EOS 5D Mark IV") */
30
+ cameraModel?: string;
31
+ /** Lens make/manufacturer */
32
+ lensMake?: string;
33
+ /** Lens model */
34
+ lensModel?: string;
35
+ /** ISO speed rating (e.g., 100, 400, 3200) */
36
+ iso?: number;
37
+ /** Exposure time / Shutter speed in seconds (e.g., 0.0125 = 1/80s) */
38
+ exposureTime?: number;
39
+ /** F-number / Aperture (e.g., 2.8, 5.6, 16) */
40
+ fNumber?: number;
41
+ /** Focal length in millimeters (e.g., 50, 85, 200) */
42
+ focalLength?: number;
43
+ /** Flash mode (0 = no flash, 1 = flash fired) */
44
+ flash?: number;
45
+ /** White balance mode (0 = auto, 1 = manual) */
46
+ whiteBalance?: number;
47
+ /** Orientation (1 = normal, 3 = 180°, 6 = 90° CW, 8 = 90° CCW) */
48
+ orientation?: number;
49
+ /** Software used to create/edit the image */
50
+ software?: string;
51
+ /** User comment / notes */
52
+ userComment?: string;
27
53
  /** Custom metadata fields */
28
54
  custom?: Record<string, string | number | boolean>;
29
55
  }
@@ -88,7 +114,16 @@ export interface ResizeOptions {
88
114
  /** Target height in pixels */
89
115
  height: number;
90
116
  /** Resize method (default: "bilinear") */
91
- method?: "nearest" | "bilinear";
117
+ method?: "nearest" | "bilinear" | "bicubic";
118
+ /**
119
+ * Fitting mode (default: "stretch")
120
+ * - "stretch": Stretch image to fill dimensions (may distort)
121
+ * - "fit": Fit image within dimensions maintaining aspect ratio (may have letterboxing)
122
+ * - "fill": Fill dimensions maintaining aspect ratio (may crop)
123
+ * - "cover": Alias for "fill"
124
+ * - "contain": Alias for "fit"
125
+ */
126
+ fit?: "stretch" | "fit" | "fill" | "cover" | "contain";
92
127
  }
93
128
  /**
94
129
  * Options for ASCII art encoding
@@ -163,5 +198,10 @@ export interface ImageFormat {
163
198
  * Check if the format supports multiple frames
164
199
  */
165
200
  supportsMultipleFrames?(): boolean;
201
+ /**
202
+ * Get the list of metadata fields supported by this format
203
+ * @returns Array of metadata field names that can be persisted
204
+ */
205
+ getSupportedMetadata?(): Array<keyof ImageMetadata>;
166
206
  }
167
207
  //# sourceMappingURL=types.d.ts.map
@@ -44,6 +44,13 @@ export declare function adjustExposure(data: Uint8Array, amount: number): Uint8A
44
44
  * @returns New image data with adjusted saturation
45
45
  */
46
46
  export declare function adjustSaturation(data: Uint8Array, amount: number): Uint8Array;
47
+ /**
48
+ * Adjust hue of an image by rotating the hue wheel
49
+ * @param data Image data (RGBA)
50
+ * @param degrees Hue rotation in degrees (any value accepted, wraps at 360)
51
+ * @returns New image data with adjusted hue
52
+ */
53
+ export declare function adjustHue(data: Uint8Array, degrees: number): Uint8Array;
47
54
  /**
48
55
  * Invert colors of an image
49
56
  * @param data Image data (RGBA)
@@ -131,4 +138,52 @@ export declare function sepia(data: Uint8Array): Uint8Array;
131
138
  * @returns New image data with median filter applied
132
139
  */
133
140
  export declare function medianFilter(data: Uint8Array, width: number, height: number, radius?: number): Uint8Array;
141
+ /**
142
+ * Rotate image 90 degrees clockwise
143
+ * @param data Image data (RGBA)
144
+ * @param width Image width
145
+ * @param height Image height
146
+ * @returns Rotated image data with swapped dimensions
147
+ */
148
+ export declare function rotate90(data: Uint8Array, width: number, height: number): {
149
+ data: Uint8Array;
150
+ width: number;
151
+ height: number;
152
+ };
153
+ /**
154
+ * Rotate image 180 degrees
155
+ * @param data Image data (RGBA)
156
+ * @param width Image width
157
+ * @param height Image height
158
+ * @returns Rotated image data with same dimensions
159
+ */
160
+ export declare function rotate180(data: Uint8Array, width: number, height: number): Uint8Array;
161
+ /**
162
+ * Rotate image 270 degrees clockwise (or 90 degrees counter-clockwise)
163
+ * @param data Image data (RGBA)
164
+ * @param width Image width
165
+ * @param height Image height
166
+ * @returns Rotated image data with swapped dimensions
167
+ */
168
+ export declare function rotate270(data: Uint8Array, width: number, height: number): {
169
+ data: Uint8Array;
170
+ width: number;
171
+ height: number;
172
+ };
173
+ /**
174
+ * Flip image horizontally (mirror)
175
+ * @param data Image data (RGBA)
176
+ * @param width Image width
177
+ * @param height Image height
178
+ * @returns Flipped image data
179
+ */
180
+ export declare function flipHorizontal(data: Uint8Array, width: number, height: number): Uint8Array;
181
+ /**
182
+ * Flip image vertically
183
+ * @param data Image data (RGBA)
184
+ * @param width Image width
185
+ * @param height Image height
186
+ * @returns Flipped image data
187
+ */
188
+ export declare function flipVertical(data: Uint8Array, width: number, height: number): Uint8Array;
134
189
  //# sourceMappingURL=image_processing.d.ts.map
@@ -131,6 +131,99 @@ export function adjustSaturation(data, amount) {
131
131
  }
132
132
  return result;
133
133
  }
134
+ /**
135
+ * Convert RGB to HSL color space
136
+ * @param r Red component (0-255)
137
+ * @param g Green component (0-255)
138
+ * @param b Blue component (0-255)
139
+ * @returns HSL values: [h (0-360), s (0-1), l (0-1)]
140
+ */
141
+ function rgbToHsl(r, g, b) {
142
+ r /= 255;
143
+ g /= 255;
144
+ b /= 255;
145
+ const max = Math.max(r, g, b);
146
+ const min = Math.min(r, g, b);
147
+ const delta = max - min;
148
+ let h = 0;
149
+ let s = 0;
150
+ const l = (max + min) / 2;
151
+ if (delta !== 0) {
152
+ s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
153
+ if (max === r) {
154
+ h = ((g - b) / delta + (g < b ? 6 : 0)) / 6;
155
+ }
156
+ else if (max === g) {
157
+ h = ((b - r) / delta + 2) / 6;
158
+ }
159
+ else {
160
+ h = ((r - g) / delta + 4) / 6;
161
+ }
162
+ }
163
+ return [h * 360, s, l];
164
+ }
165
+ /**
166
+ * Convert HSL to RGB color space
167
+ * @param h Hue (0-360)
168
+ * @param s Saturation (0-1)
169
+ * @param l Lightness (0-1)
170
+ * @returns RGB values: [r (0-255), g (0-255), b (0-255)]
171
+ */
172
+ function hslToRgb(h, s, l) {
173
+ h = h / 360;
174
+ let r, g, b;
175
+ if (s === 0) {
176
+ r = g = b = l; // Achromatic
177
+ }
178
+ else {
179
+ const hue2rgb = (p, q, t) => {
180
+ if (t < 0)
181
+ t += 1;
182
+ if (t > 1)
183
+ t -= 1;
184
+ if (t < 1 / 6)
185
+ return p + (q - p) * 6 * t;
186
+ if (t < 1 / 2)
187
+ return q;
188
+ if (t < 2 / 3)
189
+ return p + (q - p) * (2 / 3 - t) * 6;
190
+ return p;
191
+ };
192
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
193
+ const p = 2 * l - q;
194
+ r = hue2rgb(p, q, h + 1 / 3);
195
+ g = hue2rgb(p, q, h);
196
+ b = hue2rgb(p, q, h - 1 / 3);
197
+ }
198
+ return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
199
+ }
200
+ /**
201
+ * Adjust hue of an image by rotating the hue wheel
202
+ * @param data Image data (RGBA)
203
+ * @param degrees Hue rotation in degrees (any value accepted, wraps at 360)
204
+ * @returns New image data with adjusted hue
205
+ */
206
+ export function adjustHue(data, degrees) {
207
+ const result = new Uint8Array(data.length);
208
+ // Normalize rotation to -180 to 180 range
209
+ const rotation = ((degrees % 360) + 360) % 360;
210
+ for (let i = 0; i < data.length; i += 4) {
211
+ const r = data[i];
212
+ const g = data[i + 1];
213
+ const b = data[i + 2];
214
+ // Convert to HSL
215
+ const [h, s, l] = rgbToHsl(r, g, b);
216
+ // Rotate hue
217
+ const newH = (h + rotation) % 360;
218
+ // Convert back to RGB
219
+ const [newR, newG, newB] = hslToRgb(newH, s, l);
220
+ result[i] = newR;
221
+ result[i + 1] = newG;
222
+ result[i + 2] = newB;
223
+ result[i + 3] = data[i + 3]; // Preserve alpha
224
+ }
225
+ return result;
226
+ }
134
227
  /**
135
228
  * Invert colors of an image
136
229
  * @param data Image data (RGBA)
@@ -459,3 +552,120 @@ export function medianFilter(data, width, height, radius = 1) {
459
552
  }
460
553
  return result;
461
554
  }
555
+ /**
556
+ * Rotate image 90 degrees clockwise
557
+ * @param data Image data (RGBA)
558
+ * @param width Image width
559
+ * @param height Image height
560
+ * @returns Rotated image data with swapped dimensions
561
+ */
562
+ export function rotate90(data, width, height) {
563
+ const result = new Uint8Array(data.length);
564
+ const newWidth = height;
565
+ const newHeight = width;
566
+ for (let y = 0; y < height; y++) {
567
+ for (let x = 0; x < width; x++) {
568
+ const srcIdx = (y * width + x) * 4;
569
+ const dstX = height - 1 - y;
570
+ const dstY = x;
571
+ const dstIdx = (dstY * newWidth + dstX) * 4;
572
+ result[dstIdx] = data[srcIdx];
573
+ result[dstIdx + 1] = data[srcIdx + 1];
574
+ result[dstIdx + 2] = data[srcIdx + 2];
575
+ result[dstIdx + 3] = data[srcIdx + 3];
576
+ }
577
+ }
578
+ return { data: result, width: newWidth, height: newHeight };
579
+ }
580
+ /**
581
+ * Rotate image 180 degrees
582
+ * @param data Image data (RGBA)
583
+ * @param width Image width
584
+ * @param height Image height
585
+ * @returns Rotated image data with same dimensions
586
+ */
587
+ export function rotate180(data, width, height) {
588
+ 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];
599
+ }
600
+ }
601
+ return result;
602
+ }
603
+ /**
604
+ * Rotate image 270 degrees clockwise (or 90 degrees counter-clockwise)
605
+ * @param data Image data (RGBA)
606
+ * @param width Image width
607
+ * @param height Image height
608
+ * @returns Rotated image data with swapped dimensions
609
+ */
610
+ export function rotate270(data, width, height) {
611
+ const result = new Uint8Array(data.length);
612
+ const newWidth = height;
613
+ const newHeight = width;
614
+ for (let y = 0; y < height; y++) {
615
+ for (let x = 0; x < width; x++) {
616
+ const srcIdx = (y * width + x) * 4;
617
+ const dstX = y;
618
+ const dstY = width - 1 - x;
619
+ const dstIdx = (dstY * newWidth + dstX) * 4;
620
+ result[dstIdx] = data[srcIdx];
621
+ result[dstIdx + 1] = data[srcIdx + 1];
622
+ result[dstIdx + 2] = data[srcIdx + 2];
623
+ result[dstIdx + 3] = data[srcIdx + 3];
624
+ }
625
+ }
626
+ return { data: result, width: newWidth, height: newHeight };
627
+ }
628
+ /**
629
+ * Flip image horizontally (mirror)
630
+ * @param data Image data (RGBA)
631
+ * @param width Image width
632
+ * @param height Image height
633
+ * @returns Flipped image data
634
+ */
635
+ export function flipHorizontal(data, width, height) {
636
+ 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];
646
+ }
647
+ }
648
+ return result;
649
+ }
650
+ /**
651
+ * Flip image vertically
652
+ * @param data Image data (RGBA)
653
+ * @param width Image width
654
+ * @param height Image height
655
+ * @returns Flipped image data
656
+ */
657
+ export function flipVertical(data, width, height) {
658
+ const result = new Uint8Array(data.length);
659
+ 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
+ }
669
+ }
670
+ return result;
671
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * XMP (Extensible Metadata Platform) parsing and writing utilities
3
+ *
4
+ * This module provides utilities for reading and writing XMP metadata in image files.
5
+ * It supports Dublin Core, EXIF, and Photoshop namespaces.
6
+ */
7
+ import type { ImageMetadata } from "../../types.js";
8
+ /**
9
+ * XMP namespace URIs
10
+ */
11
+ export declare const XMP_NAMESPACES: {
12
+ readonly RDF: "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
13
+ readonly DC: "http://purl.org/dc/elements/1.1/";
14
+ readonly XMP: "http://ns.adobe.com/xap/1.0/";
15
+ readonly EXIF: "http://ns.adobe.com/exif/1.0/";
16
+ readonly TIFF: "http://ns.adobe.com/tiff/1.0/";
17
+ readonly PHOTOSHOP: "http://ns.adobe.com/photoshop/1.0/";
18
+ readonly XMP_RIGHTS: "http://ns.adobe.com/xap/1.0/rights/";
19
+ };
20
+ /**
21
+ * XMP Dublin Core field mapping to ImageMetadata
22
+ */
23
+ export interface XMPFieldMapping {
24
+ xmpPath: string;
25
+ metadataKey: keyof ImageMetadata;
26
+ namespace: string;
27
+ }
28
+ /**
29
+ * Supported XMP fields and their mappings
30
+ */
31
+ export declare const XMP_FIELD_MAPPINGS: XMPFieldMapping[];
32
+ /**
33
+ * Escape XML special characters
34
+ */
35
+ export declare function escapeXML(str: string): string;
36
+ /**
37
+ * Unescape XML special characters
38
+ */
39
+ export declare function unescapeXML(str: string): string;
40
+ /**
41
+ * Parse XMP metadata from XML string
42
+ */
43
+ export declare function parseXMP(xmpStr: string): Partial<ImageMetadata>;
44
+ /**
45
+ * Create XMP packet from metadata
46
+ */
47
+ export declare function createXMP(metadata: Partial<ImageMetadata>): string;
48
+ /**
49
+ * Get list of supported XMP metadata fields
50
+ */
51
+ export declare function getSupportedXMPFields(): Array<keyof ImageMetadata>;
52
+ //# sourceMappingURL=xmp.d.ts.map