cross-image 0.2.1 → 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 (44) hide show
  1. package/README.md +160 -32
  2. package/esm/mod.d.ts +2 -1
  3. package/esm/mod.js +2 -1
  4. package/esm/src/formats/jpeg.d.ts +12 -1
  5. package/esm/src/formats/jpeg.js +633 -4
  6. package/esm/src/formats/png_base.d.ts +8 -0
  7. package/esm/src/formats/png_base.js +176 -3
  8. package/esm/src/formats/ppm.d.ts +50 -0
  9. package/esm/src/formats/ppm.js +242 -0
  10. package/esm/src/formats/tiff.d.ts +10 -1
  11. package/esm/src/formats/tiff.js +194 -44
  12. package/esm/src/formats/webp.d.ts +9 -2
  13. package/esm/src/formats/webp.js +211 -62
  14. package/esm/src/image.d.ts +81 -0
  15. package/esm/src/image.js +282 -5
  16. package/esm/src/types.d.ts +41 -1
  17. package/esm/src/utils/image_processing.d.ts +98 -0
  18. package/esm/src/utils/image_processing.js +440 -0
  19. package/esm/src/utils/metadata/xmp.d.ts +52 -0
  20. package/esm/src/utils/metadata/xmp.js +325 -0
  21. package/esm/src/utils/resize.d.ts +4 -0
  22. package/esm/src/utils/resize.js +74 -0
  23. package/package.json +1 -1
  24. package/script/mod.d.ts +2 -1
  25. package/script/mod.js +4 -2
  26. package/script/src/formats/jpeg.d.ts +12 -1
  27. package/script/src/formats/jpeg.js +633 -4
  28. package/script/src/formats/png_base.d.ts +8 -0
  29. package/script/src/formats/png_base.js +176 -3
  30. package/script/src/formats/ppm.d.ts +50 -0
  31. package/script/src/formats/ppm.js +246 -0
  32. package/script/src/formats/tiff.d.ts +10 -1
  33. package/script/src/formats/tiff.js +194 -44
  34. package/script/src/formats/webp.d.ts +9 -2
  35. package/script/src/formats/webp.js +211 -62
  36. package/script/src/image.d.ts +81 -0
  37. package/script/src/image.js +280 -3
  38. package/script/src/types.d.ts +41 -1
  39. package/script/src/utils/image_processing.d.ts +98 -0
  40. package/script/src/utils/image_processing.js +451 -0
  41. package/script/src/utils/metadata/xmp.d.ts +52 -0
  42. package/script/src/utils/metadata/xmp.js +333 -0
  43. package/script/src/utils/resize.d.ts +4 -0
  44. package/script/src/utils/resize.js +75 -0
@@ -132,18 +132,49 @@ export class TIFFFormat {
132
132
  const { width, height, data, metadata } = imageData;
133
133
  const opts = options;
134
134
  const compression = opts?.compression ?? "none";
135
+ const grayscale = opts?.grayscale ?? false;
136
+ const rgb = opts?.rgb ?? false;
137
+ // Convert RGBA to grayscale if requested
138
+ let sourceData;
139
+ let samplesPerPixel;
140
+ if (grayscale) {
141
+ sourceData = new Uint8Array(width * height);
142
+ samplesPerPixel = 1;
143
+ for (let i = 0; i < width * height; i++) {
144
+ const r = data[i * 4];
145
+ const g = data[i * 4 + 1];
146
+ const b = data[i * 4 + 2];
147
+ // Use standard luminance formula
148
+ sourceData[i] = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
149
+ }
150
+ }
151
+ else if (rgb) {
152
+ // Convert RGBA to RGB (strip alpha channel)
153
+ sourceData = new Uint8Array(width * height * 3);
154
+ samplesPerPixel = 3;
155
+ for (let i = 0; i < width * height; i++) {
156
+ sourceData[i * 3] = data[i * 4]; // R
157
+ sourceData[i * 3 + 1] = data[i * 4 + 1]; // G
158
+ sourceData[i * 3 + 2] = data[i * 4 + 2]; // B
159
+ }
160
+ }
161
+ else {
162
+ // Keep as RGBA
163
+ sourceData = data;
164
+ samplesPerPixel = 4;
165
+ }
135
166
  // Prepare pixel data (compress if needed)
136
167
  let pixelData;
137
168
  let compressionCode;
138
169
  if (compression === "lzw") {
139
170
  // LZW compress the pixel data
140
171
  const encoder = new TIFFLZWEncoder();
141
- pixelData = encoder.compress(data);
172
+ pixelData = encoder.compress(sourceData);
142
173
  compressionCode = 5;
143
174
  }
144
175
  else {
145
176
  // Uncompressed
146
- pixelData = data;
177
+ pixelData = sourceData;
147
178
  compressionCode = 1;
148
179
  }
149
180
  const result = [];
@@ -161,7 +192,10 @@ export class TIFFFormat {
161
192
  // IFD (Image File Directory)
162
193
  const ifdStart = result.length;
163
194
  // Count number of entries (including metadata)
164
- let numEntries = 12; // Base entries (including ExtraSamples)
195
+ // Grayscale: 10 entries (no ExtraSamples)
196
+ // RGB: 11 entries (no ExtraSamples)
197
+ // RGBA: 12 entries (includes ExtraSamples)
198
+ let numEntries = grayscale ? 10 : (rgb ? 11 : 12);
165
199
  if (metadata?.description)
166
200
  numEntries++;
167
201
  if (metadata?.author)
@@ -179,16 +213,28 @@ export class TIFFFormat {
179
213
  // ImageHeight (0x0101)
180
214
  this.writeIFDEntry(result, 0x0101, 4, 1, height);
181
215
  // BitsPerSample (0x0102) - 8 bits per channel
182
- this.writeIFDEntry(result, 0x0102, 3, 4, dataOffset);
183
- dataOffset += 8; // 4 x 2-byte values
216
+ if (grayscale) {
217
+ // Single value for grayscale
218
+ this.writeIFDEntry(result, 0x0102, 3, 1, 8);
219
+ }
220
+ else if (rgb) {
221
+ // 3 values for RGB
222
+ this.writeIFDEntry(result, 0x0102, 3, 3, dataOffset);
223
+ dataOffset += 6; // 3 x 2-byte values
224
+ }
225
+ else {
226
+ // 4 values for RGBA
227
+ this.writeIFDEntry(result, 0x0102, 3, 4, dataOffset);
228
+ dataOffset += 8; // 4 x 2-byte values
229
+ }
184
230
  // Compression (0x0103) - 1 = uncompressed, 5 = LZW
185
231
  this.writeIFDEntry(result, 0x0103, 3, 1, compressionCode);
186
- // PhotometricInterpretation (0x0106) - 2 = RGB
187
- this.writeIFDEntry(result, 0x0106, 3, 1, 2);
232
+ // PhotometricInterpretation (0x0106) - 1 = BlackIsZero (grayscale), 2 = RGB
233
+ this.writeIFDEntry(result, 0x0106, 3, 1, grayscale ? 1 : 2);
188
234
  // StripOffsets (0x0111)
189
235
  this.writeIFDEntry(result, 0x0111, 4, 1, 8);
190
- // SamplesPerPixel (0x0115) - 4 (RGBA)
191
- this.writeIFDEntry(result, 0x0115, 3, 1, 4);
236
+ // SamplesPerPixel (0x0115) - 1 for grayscale, 3 for RGB, 4 for RGBA
237
+ this.writeIFDEntry(result, 0x0115, 3, 1, samplesPerPixel);
192
238
  // RowsPerStrip (0x0116)
193
239
  this.writeIFDEntry(result, 0x0116, 4, 1, height);
194
240
  // StripByteCounts (0x0117)
@@ -201,8 +247,10 @@ export class TIFFFormat {
201
247
  const yResOffset = dataOffset;
202
248
  this.writeIFDEntry(result, 0x011b, 5, 1, yResOffset);
203
249
  dataOffset += 8;
204
- // ExtraSamples (0x0152) - 2 = unassociated alpha
205
- this.writeIFDEntry(result, 0x0152, 3, 1, 2);
250
+ // ExtraSamples (0x0152) - 2 = unassociated alpha (only for RGBA)
251
+ if (!grayscale && !rgb) {
252
+ this.writeIFDEntry(result, 0x0152, 3, 1, 2);
253
+ }
206
254
  // Optional metadata entries
207
255
  if (metadata?.description) {
208
256
  const descBytes = new TextEncoder().encode(metadata.description + "\0");
@@ -229,11 +277,18 @@ export class TIFFFormat {
229
277
  // Next IFD offset (0 = no more IFDs)
230
278
  this.writeUint32LE(result, 0);
231
279
  // Write variable-length data
232
- // BitsPerSample values (must be written first to match offset calculation)
233
- this.writeUint16LE(result, 8);
234
- this.writeUint16LE(result, 8);
235
- this.writeUint16LE(result, 8);
236
- this.writeUint16LE(result, 8);
280
+ // BitsPerSample values (only for RGB and RGBA, not for grayscale)
281
+ if (rgb) {
282
+ this.writeUint16LE(result, 8);
283
+ this.writeUint16LE(result, 8);
284
+ this.writeUint16LE(result, 8);
285
+ }
286
+ else if (!grayscale) {
287
+ this.writeUint16LE(result, 8);
288
+ this.writeUint16LE(result, 8);
289
+ this.writeUint16LE(result, 8);
290
+ this.writeUint16LE(result, 8);
291
+ }
237
292
  // XResolution value (rational)
238
293
  const dpiX = metadata?.dpiX ?? DEFAULT_DPI;
239
294
  this.writeUint32LE(result, dpiX);
@@ -696,14 +751,23 @@ export class TIFFFormat {
696
751
  }
697
752
  // Check photometric interpretation
698
753
  const photometric = this.getIFDValue(data, ifdOffset, 0x0106, isLittleEndian);
699
- if (photometric !== 2) {
700
- // Only support RGB (photometric = 2)
754
+ if (photometric !== 0 && photometric !== 1 && photometric !== 2) {
755
+ // Support: 0 = WhiteIsZero, 1 = BlackIsZero, 2 = RGB
701
756
  return null;
702
757
  }
703
758
  // Get samples per pixel
704
759
  const samplesPerPixel = this.getIFDValue(data, ifdOffset, 0x0115, isLittleEndian);
705
- if (!samplesPerPixel || (samplesPerPixel !== 3 && samplesPerPixel !== 4)) {
706
- // Only support RGB (3) or RGBA (4)
760
+ // For grayscale (photometric 0 or 1), expect 1 sample per pixel
761
+ // For RGB, expect 3 or 4 samples per pixel
762
+ if (!samplesPerPixel) {
763
+ return null;
764
+ }
765
+ if ((photometric === 0 || photometric === 1) && samplesPerPixel !== 1) {
766
+ // Grayscale requires 1 sample per pixel
767
+ return null;
768
+ }
769
+ if (photometric === 2 && samplesPerPixel !== 3 && samplesPerPixel !== 4) {
770
+ // RGB requires 3 or 4 samples per pixel
707
771
  return null;
708
772
  }
709
773
  // Get strip offset
@@ -733,17 +797,40 @@ export class TIFFFormat {
733
797
  // Convert to RGBA
734
798
  const rgba = new Uint8Array(width * height * 4);
735
799
  let srcPos = 0;
736
- for (let y = 0; y < height; y++) {
737
- for (let x = 0; x < width; x++) {
738
- const dstIdx = (y * width + x) * 4;
739
- if (srcPos + samplesPerPixel > pixelData.length) {
740
- return null; // Not enough data
800
+ if (photometric === 0 || photometric === 1) {
801
+ // Grayscale image
802
+ for (let y = 0; y < height; y++) {
803
+ for (let x = 0; x < width; x++) {
804
+ const dstIdx = (y * width + x) * 4;
805
+ if (srcPos >= pixelData.length) {
806
+ return null; // Not enough data
807
+ }
808
+ let gray = pixelData[srcPos++];
809
+ // WhiteIsZero (0) means 0=white, 255=black, so invert
810
+ if (photometric === 0) {
811
+ gray = 255 - gray;
812
+ }
813
+ rgba[dstIdx] = gray; // R
814
+ rgba[dstIdx + 1] = gray; // G
815
+ rgba[dstIdx + 2] = gray; // B
816
+ rgba[dstIdx + 3] = 255; // A (opaque)
817
+ }
818
+ }
819
+ }
820
+ else {
821
+ // RGB/RGBA image
822
+ for (let y = 0; y < height; y++) {
823
+ for (let x = 0; x < width; x++) {
824
+ const dstIdx = (y * width + x) * 4;
825
+ if (srcPos + samplesPerPixel > pixelData.length) {
826
+ return null; // Not enough data
827
+ }
828
+ // TIFF stores RGB(A) in order
829
+ rgba[dstIdx] = pixelData[srcPos++]; // R
830
+ rgba[dstIdx + 1] = pixelData[srcPos++]; // G
831
+ rgba[dstIdx + 2] = pixelData[srcPos++]; // B
832
+ rgba[dstIdx + 3] = samplesPerPixel === 4 ? pixelData[srcPos++] : 255; // A
741
833
  }
742
- // TIFF stores RGB(A) in order
743
- rgba[dstIdx] = pixelData[srcPos++]; // R
744
- rgba[dstIdx + 1] = pixelData[srcPos++]; // G
745
- rgba[dstIdx + 2] = pixelData[srcPos++]; // B
746
- rgba[dstIdx + 3] = samplesPerPixel === 4 ? pixelData[srcPos++] : 255; // A
747
834
  }
748
835
  }
749
836
  return rgba;
@@ -761,14 +848,23 @@ export class TIFFFormat {
761
848
  }
762
849
  // Check photometric interpretation
763
850
  const photometric = this.getIFDValue(data, ifdOffset, 0x0106, isLittleEndian);
764
- if (photometric !== 2) {
765
- // Only support RGB (photometric = 2)
851
+ if (photometric !== 0 && photometric !== 1 && photometric !== 2) {
852
+ // Support: 0 = WhiteIsZero, 1 = BlackIsZero, 2 = RGB
766
853
  return null;
767
854
  }
768
855
  // Get samples per pixel
769
856
  const samplesPerPixel = this.getIFDValue(data, ifdOffset, 0x0115, isLittleEndian);
770
- if (!samplesPerPixel || (samplesPerPixel !== 3 && samplesPerPixel !== 4)) {
771
- // Only support RGB (3) or RGBA (4)
857
+ // For grayscale (photometric 0 or 1), expect 1 sample per pixel
858
+ // For RGB, expect 3 or 4 samples per pixel
859
+ if (!samplesPerPixel) {
860
+ return null;
861
+ }
862
+ if ((photometric === 0 || photometric === 1) && samplesPerPixel !== 1) {
863
+ // Grayscale requires 1 sample per pixel
864
+ return null;
865
+ }
866
+ if (photometric === 2 && samplesPerPixel !== 3 && samplesPerPixel !== 4) {
867
+ // RGB requires 3 or 4 samples per pixel
772
868
  return null;
773
869
  }
774
870
  // Get strip offset
@@ -798,19 +894,73 @@ export class TIFFFormat {
798
894
  // Convert to RGBA
799
895
  const rgba = new Uint8Array(width * height * 4);
800
896
  let srcPos = 0;
801
- for (let y = 0; y < height; y++) {
802
- for (let x = 0; x < width; x++) {
803
- const dstIdx = (y * width + x) * 4;
804
- if (srcPos + samplesPerPixel > pixelData.length) {
805
- return null; // Not enough data
897
+ if (photometric === 0 || photometric === 1) {
898
+ // Grayscale image
899
+ for (let y = 0; y < height; y++) {
900
+ for (let x = 0; x < width; x++) {
901
+ const dstIdx = (y * width + x) * 4;
902
+ if (srcPos >= pixelData.length) {
903
+ return null; // Not enough data
904
+ }
905
+ let gray = pixelData[srcPos++];
906
+ // WhiteIsZero (0) means 0=white, 255=black, so invert
907
+ if (photometric === 0) {
908
+ gray = 255 - gray;
909
+ }
910
+ rgba[dstIdx] = gray; // R
911
+ rgba[dstIdx + 1] = gray; // G
912
+ rgba[dstIdx + 2] = gray; // B
913
+ rgba[dstIdx + 3] = 255; // A (opaque)
914
+ }
915
+ }
916
+ }
917
+ else {
918
+ // RGB/RGBA image
919
+ for (let y = 0; y < height; y++) {
920
+ for (let x = 0; x < width; x++) {
921
+ const dstIdx = (y * width + x) * 4;
922
+ if (srcPos + samplesPerPixel > pixelData.length) {
923
+ return null; // Not enough data
924
+ }
925
+ // TIFF stores RGB(A) in order
926
+ rgba[dstIdx] = pixelData[srcPos++]; // R
927
+ rgba[dstIdx + 1] = pixelData[srcPos++]; // G
928
+ rgba[dstIdx + 2] = pixelData[srcPos++]; // B
929
+ rgba[dstIdx + 3] = samplesPerPixel === 4 ? pixelData[srcPos++] : 255; // A
806
930
  }
807
- // TIFF stores RGB(A) in order
808
- rgba[dstIdx] = pixelData[srcPos++]; // R
809
- rgba[dstIdx + 1] = pixelData[srcPos++]; // G
810
- rgba[dstIdx + 2] = pixelData[srcPos++]; // B
811
- rgba[dstIdx + 3] = samplesPerPixel === 4 ? pixelData[srcPos++] : 255; // A
812
931
  }
813
932
  }
814
933
  return rgba;
815
934
  }
935
+ /**
936
+ * Get list of metadata fields supported by TIFF format
937
+ * TIFF supports extensive EXIF metadata including GPS and InteropIFD
938
+ */
939
+ getSupportedMetadata() {
940
+ return [
941
+ "creationDate",
942
+ "description",
943
+ "author",
944
+ "copyright",
945
+ "cameraMake",
946
+ "cameraModel",
947
+ "orientation",
948
+ "software",
949
+ "iso",
950
+ "exposureTime",
951
+ "fNumber",
952
+ "focalLength",
953
+ "flash",
954
+ "whiteBalance",
955
+ "lensMake",
956
+ "lensModel",
957
+ "userComment",
958
+ "latitude",
959
+ "longitude",
960
+ "dpiX",
961
+ "dpiY",
962
+ "physicalWidth",
963
+ "physicalHeight",
964
+ ];
965
+ }
816
966
  }
@@ -1,4 +1,4 @@
1
- import type { ImageData, ImageFormat, WebPEncodeOptions } from "../types.js";
1
+ import type { ImageData, ImageFormat, ImageMetadata, WebPEncodeOptions } from "../types.js";
2
2
  /**
3
3
  * WebP format handler
4
4
  * Implements a basic WebP decoder and encoder
@@ -30,10 +30,17 @@ export declare class WebPFormat implements ImageFormat {
30
30
  private readUint24LE;
31
31
  private decodeUsingRuntime;
32
32
  private parseEXIF;
33
+ private parseGPSIFD;
34
+ private readRational;
33
35
  private parseXMP;
34
36
  private injectMetadata;
35
37
  private createEXIFChunk;
38
+ private createGPSIFD;
39
+ private writeRational;
36
40
  private createXMPChunk;
37
- private escapeXML;
41
+ /**
42
+ * Get the list of metadata fields supported by WebP format
43
+ */
44
+ getSupportedMetadata(): Array<keyof ImageMetadata>;
38
45
  }
39
46
  //# sourceMappingURL=webp.d.ts.map