cross-image 0.2.1 → 0.2.2

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.
@@ -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,17 +894,40 @@ 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;
@@ -207,6 +207,36 @@ export declare class Image {
207
207
  * @returns This image instance for chaining
208
208
  */
209
209
  grayscale(): this;
210
+ /**
211
+ * Apply sepia tone effect to the image
212
+ * @returns This image instance for chaining
213
+ */
214
+ sepia(): this;
215
+ /**
216
+ * Apply box blur filter to the image
217
+ * @param radius Blur radius (default: 1)
218
+ * @returns This image instance for chaining
219
+ */
220
+ blur(radius?: number): this;
221
+ /**
222
+ * Apply Gaussian blur filter to the image
223
+ * @param radius Blur radius (default: 1)
224
+ * @param sigma Optional standard deviation (if not provided, calculated from radius)
225
+ * @returns This image instance for chaining
226
+ */
227
+ gaussianBlur(radius?: number, sigma?: number): this;
228
+ /**
229
+ * Apply sharpen filter to the image
230
+ * @param amount Sharpening amount (0-1, default: 0.5)
231
+ * @returns This image instance for chaining
232
+ */
233
+ sharpen(amount?: number): this;
234
+ /**
235
+ * Apply median filter to reduce noise
236
+ * @param radius Filter radius (default: 1)
237
+ * @returns This image instance for chaining
238
+ */
239
+ medianFilter(radius?: number): this;
210
240
  /**
211
241
  * Fill a rectangular region with a color
212
242
  * @param x Starting X position
package/esm/src/image.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { resizeBilinear, resizeNearest } from "./utils/resize.js";
2
- import { adjustBrightness, adjustContrast, adjustExposure, adjustSaturation, composite, crop, fillRect, grayscale, invert, } from "./utils/image_processing.js";
2
+ import { adjustBrightness, adjustContrast, adjustExposure, adjustSaturation, boxBlur, composite, crop, fillRect, gaussianBlur, grayscale, invert, medianFilter, sepia, sharpen, } from "./utils/image_processing.js";
3
3
  import { PNGFormat } from "./formats/png.js";
4
4
  import { APNGFormat } from "./formats/apng.js";
5
5
  import { JPEGFormat } from "./formats/jpeg.js";
@@ -11,6 +11,7 @@ import { ICOFormat } from "./formats/ico.js";
11
11
  import { DNGFormat } from "./formats/dng.js";
12
12
  import { PAMFormat } from "./formats/pam.js";
13
13
  import { PCXFormat } from "./formats/pcx.js";
14
+ import { PPMFormat } from "./formats/ppm.js";
14
15
  import { ASCIIFormat } from "./formats/ascii.js";
15
16
  import { validateImageDimensions } from "./utils/security.js";
16
17
  /**
@@ -458,6 +459,61 @@ export class Image {
458
459
  this.imageData.data = grayscale(this.imageData.data);
459
460
  return this;
460
461
  }
462
+ /**
463
+ * Apply sepia tone effect to the image
464
+ * @returns This image instance for chaining
465
+ */
466
+ sepia() {
467
+ if (!this.imageData)
468
+ throw new Error("No image loaded");
469
+ this.imageData.data = sepia(this.imageData.data);
470
+ return this;
471
+ }
472
+ /**
473
+ * Apply box blur filter to the image
474
+ * @param radius Blur radius (default: 1)
475
+ * @returns This image instance for chaining
476
+ */
477
+ blur(radius = 1) {
478
+ if (!this.imageData)
479
+ throw new Error("No image loaded");
480
+ this.imageData.data = boxBlur(this.imageData.data, this.imageData.width, this.imageData.height, radius);
481
+ return this;
482
+ }
483
+ /**
484
+ * Apply Gaussian blur filter to the image
485
+ * @param radius Blur radius (default: 1)
486
+ * @param sigma Optional standard deviation (if not provided, calculated from radius)
487
+ * @returns This image instance for chaining
488
+ */
489
+ gaussianBlur(radius = 1, sigma) {
490
+ if (!this.imageData)
491
+ throw new Error("No image loaded");
492
+ this.imageData.data = gaussianBlur(this.imageData.data, this.imageData.width, this.imageData.height, radius, sigma);
493
+ return this;
494
+ }
495
+ /**
496
+ * Apply sharpen filter to the image
497
+ * @param amount Sharpening amount (0-1, default: 0.5)
498
+ * @returns This image instance for chaining
499
+ */
500
+ sharpen(amount = 0.5) {
501
+ if (!this.imageData)
502
+ throw new Error("No image loaded");
503
+ this.imageData.data = sharpen(this.imageData.data, this.imageData.width, this.imageData.height, amount);
504
+ return this;
505
+ }
506
+ /**
507
+ * Apply median filter to reduce noise
508
+ * @param radius Filter radius (default: 1)
509
+ * @returns This image instance for chaining
510
+ */
511
+ medianFilter(radius = 1) {
512
+ if (!this.imageData)
513
+ throw new Error("No image loaded");
514
+ this.imageData.data = medianFilter(this.imageData.data, this.imageData.width, this.imageData.height, radius);
515
+ return this;
516
+ }
461
517
  /**
462
518
  * Fill a rectangular region with a color
463
519
  * @param x Starting X position
@@ -563,6 +619,7 @@ Object.defineProperty(Image, "formats", {
563
619
  new DNGFormat(),
564
620
  new PAMFormat(),
565
621
  new PCXFormat(),
622
+ new PPMFormat(),
566
623
  new ASCIIFormat(),
567
624
  ]
568
625
  });
@@ -88,4 +88,47 @@ export declare function crop(data: Uint8Array, width: number, height: number, x:
88
88
  width: number;
89
89
  height: number;
90
90
  };
91
+ /**
92
+ * Apply a box blur filter to an image
93
+ * @param data Image data (RGBA)
94
+ * @param width Image width
95
+ * @param height Image height
96
+ * @param radius Blur radius (default: 1)
97
+ * @returns New image data with box blur applied
98
+ */
99
+ export declare function boxBlur(data: Uint8Array, width: number, height: number, radius?: number): Uint8Array;
100
+ /**
101
+ * Apply Gaussian blur to an image
102
+ * @param data Image data (RGBA)
103
+ * @param width Image width
104
+ * @param height Image height
105
+ * @param radius Blur radius (default: 1)
106
+ * @param sigma Optional standard deviation (if not provided, calculated from radius)
107
+ * @returns New image data with Gaussian blur applied
108
+ */
109
+ export declare function gaussianBlur(data: Uint8Array, width: number, height: number, radius?: number, sigma?: number): Uint8Array;
110
+ /**
111
+ * Apply sharpen filter to an image
112
+ * @param data Image data (RGBA)
113
+ * @param width Image width
114
+ * @param height Image height
115
+ * @param amount Sharpening amount (0-1, default: 0.5)
116
+ * @returns New image data with sharpening applied
117
+ */
118
+ export declare function sharpen(data: Uint8Array, width: number, height: number, amount?: number): Uint8Array;
119
+ /**
120
+ * Apply sepia tone effect to an image
121
+ * @param data Image data (RGBA)
122
+ * @returns New image data with sepia tone applied
123
+ */
124
+ export declare function sepia(data: Uint8Array): Uint8Array;
125
+ /**
126
+ * Apply median filter to reduce noise
127
+ * @param data Image data (RGBA)
128
+ * @param width Image width
129
+ * @param height Image height
130
+ * @param radius Filter radius (default: 1)
131
+ * @returns New image data with median filter applied
132
+ */
133
+ export declare function medianFilter(data: Uint8Array, width: number, height: number, radius?: number): Uint8Array;
91
134
  //# sourceMappingURL=image_processing.d.ts.map