cross-image 0.2.0 → 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.
Files changed (62) hide show
  1. package/README.md +41 -28
  2. package/esm/mod.d.ts +4 -1
  3. package/esm/mod.js +4 -1
  4. package/esm/src/formats/apng.d.ts +50 -0
  5. package/esm/src/formats/apng.js +364 -0
  6. package/esm/src/formats/bmp.d.ts +0 -6
  7. package/esm/src/formats/bmp.js +24 -47
  8. package/esm/src/formats/dng.js +4 -4
  9. package/esm/src/formats/gif.d.ts +0 -2
  10. package/esm/src/formats/gif.js +10 -16
  11. package/esm/src/formats/ico.d.ts +41 -0
  12. package/esm/src/formats/ico.js +214 -0
  13. package/esm/src/formats/pcx.js +1 -1
  14. package/esm/src/formats/png.d.ts +2 -21
  15. package/esm/src/formats/png.js +5 -429
  16. package/esm/src/formats/png_base.d.ts +108 -0
  17. package/esm/src/formats/png_base.js +487 -0
  18. package/esm/src/formats/ppm.d.ts +50 -0
  19. package/esm/src/formats/ppm.js +242 -0
  20. package/esm/src/formats/tiff.d.ts +4 -0
  21. package/esm/src/formats/tiff.js +163 -44
  22. package/esm/src/formats/webp.d.ts +0 -1
  23. package/esm/src/formats/webp.js +4 -7
  24. package/esm/src/image.d.ts +30 -0
  25. package/esm/src/image.js +62 -1
  26. package/esm/src/utils/byte_utils.d.ts +30 -0
  27. package/esm/src/utils/byte_utils.js +50 -0
  28. package/esm/src/utils/gif_encoder.d.ts +3 -2
  29. package/esm/src/utils/gif_encoder.js +115 -48
  30. package/esm/src/utils/image_processing.d.ts +43 -0
  31. package/esm/src/utils/image_processing.js +230 -0
  32. package/package.json +1 -1
  33. package/script/mod.d.ts +4 -1
  34. package/script/mod.js +8 -2
  35. package/script/src/formats/apng.d.ts +50 -0
  36. package/script/src/formats/apng.js +368 -0
  37. package/script/src/formats/bmp.d.ts +0 -6
  38. package/script/src/formats/bmp.js +24 -47
  39. package/script/src/formats/dng.js +4 -4
  40. package/script/src/formats/gif.d.ts +0 -2
  41. package/script/src/formats/gif.js +10 -16
  42. package/script/src/formats/ico.d.ts +41 -0
  43. package/script/src/formats/ico.js +218 -0
  44. package/script/src/formats/pcx.js +1 -1
  45. package/script/src/formats/png.d.ts +2 -21
  46. package/script/src/formats/png.js +5 -429
  47. package/script/src/formats/png_base.d.ts +108 -0
  48. package/script/src/formats/png_base.js +491 -0
  49. package/script/src/formats/ppm.d.ts +50 -0
  50. package/script/src/formats/ppm.js +246 -0
  51. package/script/src/formats/tiff.d.ts +4 -0
  52. package/script/src/formats/tiff.js +163 -44
  53. package/script/src/formats/webp.d.ts +0 -1
  54. package/script/src/formats/webp.js +4 -7
  55. package/script/src/image.d.ts +30 -0
  56. package/script/src/image.js +61 -0
  57. package/script/src/utils/byte_utils.d.ts +30 -0
  58. package/script/src/utils/byte_utils.js +58 -0
  59. package/script/src/utils/gif_encoder.d.ts +3 -2
  60. package/script/src/utils/gif_encoder.js +115 -48
  61. package/script/src/utils/image_processing.d.ts +43 -0
  62. package/script/src/utils/image_processing.js +235 -0
@@ -229,3 +229,233 @@ export function crop(data, width, height, x, y, cropWidth, cropHeight) {
229
229
  }
230
230
  return { data: result, width: actualWidth, height: actualHeight };
231
231
  }
232
+ /**
233
+ * Apply a box blur filter to an image
234
+ * @param data Image data (RGBA)
235
+ * @param width Image width
236
+ * @param height Image height
237
+ * @param radius Blur radius (default: 1)
238
+ * @returns New image data with box blur applied
239
+ */
240
+ export function boxBlur(data, width, height, radius = 1) {
241
+ const result = new Uint8Array(data.length);
242
+ const clampedRadius = Math.max(1, Math.floor(radius));
243
+ for (let y = 0; y < height; y++) {
244
+ for (let x = 0; x < width; x++) {
245
+ let r = 0, g = 0, b = 0, a = 0;
246
+ let count = 0;
247
+ // Iterate over kernel
248
+ for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
249
+ for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
250
+ const px = x + kx;
251
+ const py = y + ky;
252
+ // Check bounds
253
+ if (px >= 0 && px < width && py >= 0 && py < height) {
254
+ const idx = (py * width + px) * 4;
255
+ r += data[idx];
256
+ g += data[idx + 1];
257
+ b += data[idx + 2];
258
+ a += data[idx + 3];
259
+ count++;
260
+ }
261
+ }
262
+ }
263
+ const outIdx = (y * width + x) * 4;
264
+ result[outIdx] = Math.round(r / count);
265
+ result[outIdx + 1] = Math.round(g / count);
266
+ result[outIdx + 2] = Math.round(b / count);
267
+ result[outIdx + 3] = Math.round(a / count);
268
+ }
269
+ }
270
+ return result;
271
+ }
272
+ /**
273
+ * Generate a Gaussian kernel for blur
274
+ * @param radius Kernel radius
275
+ * @param sigma Standard deviation (if not provided, calculated from radius)
276
+ * @returns Gaussian kernel as 1D array
277
+ */
278
+ function generateGaussianKernel(radius, sigma) {
279
+ const size = radius * 2 + 1;
280
+ const kernel = new Array(size);
281
+ const s = sigma ?? radius / 3;
282
+ const s2 = 2 * s * s;
283
+ let sum = 0;
284
+ for (let i = 0; i < size; i++) {
285
+ const x = i - radius;
286
+ kernel[i] = Math.exp(-(x * x) / s2);
287
+ sum += kernel[i];
288
+ }
289
+ // Normalize
290
+ for (let i = 0; i < size; i++) {
291
+ kernel[i] /= sum;
292
+ }
293
+ return kernel;
294
+ }
295
+ /**
296
+ * Apply Gaussian blur to an image
297
+ * @param data Image data (RGBA)
298
+ * @param width Image width
299
+ * @param height Image height
300
+ * @param radius Blur radius (default: 1)
301
+ * @param sigma Optional standard deviation (if not provided, calculated from radius)
302
+ * @returns New image data with Gaussian blur applied
303
+ */
304
+ export function gaussianBlur(data, width, height, radius = 1, sigma) {
305
+ const clampedRadius = Math.max(1, Math.floor(radius));
306
+ const kernel = generateGaussianKernel(clampedRadius, sigma);
307
+ // Apply horizontal pass
308
+ const temp = new Uint8Array(data.length);
309
+ for (let y = 0; y < height; y++) {
310
+ for (let x = 0; x < width; x++) {
311
+ let r = 0, g = 0, b = 0, a = 0;
312
+ for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
313
+ const px = Math.max(0, Math.min(width - 1, x + kx));
314
+ const idx = (y * width + px) * 4;
315
+ const weight = kernel[kx + clampedRadius];
316
+ r += data[idx] * weight;
317
+ g += data[idx + 1] * weight;
318
+ b += data[idx + 2] * weight;
319
+ a += data[idx + 3] * weight;
320
+ }
321
+ const outIdx = (y * width + x) * 4;
322
+ temp[outIdx] = Math.round(r);
323
+ temp[outIdx + 1] = Math.round(g);
324
+ temp[outIdx + 2] = Math.round(b);
325
+ temp[outIdx + 3] = Math.round(a);
326
+ }
327
+ }
328
+ // Apply vertical pass
329
+ const result = new Uint8Array(data.length);
330
+ for (let y = 0; y < height; y++) {
331
+ for (let x = 0; x < width; x++) {
332
+ let r = 0, g = 0, b = 0, a = 0;
333
+ for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
334
+ const py = Math.max(0, Math.min(height - 1, y + ky));
335
+ const idx = (py * width + x) * 4;
336
+ const weight = kernel[ky + clampedRadius];
337
+ r += temp[idx] * weight;
338
+ g += temp[idx + 1] * weight;
339
+ b += temp[idx + 2] * weight;
340
+ a += temp[idx + 3] * weight;
341
+ }
342
+ const outIdx = (y * width + x) * 4;
343
+ result[outIdx] = Math.round(r);
344
+ result[outIdx + 1] = Math.round(g);
345
+ result[outIdx + 2] = Math.round(b);
346
+ result[outIdx + 3] = Math.round(a);
347
+ }
348
+ }
349
+ return result;
350
+ }
351
+ /**
352
+ * Apply sharpen filter to an image
353
+ * @param data Image data (RGBA)
354
+ * @param width Image width
355
+ * @param height Image height
356
+ * @param amount Sharpening amount (0-1, default: 0.5)
357
+ * @returns New image data with sharpening applied
358
+ */
359
+ export function sharpen(data, width, height, amount = 0.5) {
360
+ const result = new Uint8Array(data.length);
361
+ const clampedAmount = Math.max(0, Math.min(1, amount));
362
+ // Sharpen kernel (Laplacian-based)
363
+ // Center weight is 1 + 4*amount, neighbors are -amount
364
+ const center = 1 + 4 * clampedAmount;
365
+ const neighbor = -clampedAmount;
366
+ for (let y = 0; y < height; y++) {
367
+ for (let x = 0; x < width; x++) {
368
+ const idx = (y * width + x) * 4;
369
+ let r = data[idx] * center;
370
+ let g = data[idx + 1] * center;
371
+ let b = data[idx + 2] * center;
372
+ // Apply kernel to neighbors (4-connected)
373
+ const neighbors = [
374
+ { dx: 0, dy: -1 }, // top
375
+ { dx: -1, dy: 0 }, // left
376
+ { dx: 1, dy: 0 }, // right
377
+ { dx: 0, dy: 1 }, // bottom
378
+ ];
379
+ for (const { dx, dy } of neighbors) {
380
+ const nx = x + dx;
381
+ const ny = y + dy;
382
+ if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
383
+ const nIdx = (ny * width + nx) * 4;
384
+ r += data[nIdx] * neighbor;
385
+ g += data[nIdx + 1] * neighbor;
386
+ b += data[nIdx + 2] * neighbor;
387
+ }
388
+ }
389
+ result[idx] = Math.max(0, Math.min(255, Math.round(r)));
390
+ result[idx + 1] = Math.max(0, Math.min(255, Math.round(g)));
391
+ result[idx + 2] = Math.max(0, Math.min(255, Math.round(b)));
392
+ result[idx + 3] = data[idx + 3]; // Alpha unchanged
393
+ }
394
+ }
395
+ return result;
396
+ }
397
+ /**
398
+ * Apply sepia tone effect to an image
399
+ * @param data Image data (RGBA)
400
+ * @returns New image data with sepia tone applied
401
+ */
402
+ export function sepia(data) {
403
+ const result = new Uint8Array(data.length);
404
+ for (let i = 0; i < data.length; i += 4) {
405
+ const r = data[i];
406
+ const g = data[i + 1];
407
+ const b = data[i + 2];
408
+ // Sepia transformation matrix
409
+ result[i] = Math.min(255, Math.round(r * 0.393 + g * 0.769 + b * 0.189));
410
+ result[i + 1] = Math.min(255, Math.round(r * 0.349 + g * 0.686 + b * 0.168));
411
+ result[i + 2] = Math.min(255, Math.round(r * 0.272 + g * 0.534 + b * 0.131));
412
+ result[i + 3] = data[i + 3]; // Alpha unchanged
413
+ }
414
+ return result;
415
+ }
416
+ /**
417
+ * Apply median filter to reduce noise
418
+ * @param data Image data (RGBA)
419
+ * @param width Image width
420
+ * @param height Image height
421
+ * @param radius Filter radius (default: 1)
422
+ * @returns New image data with median filter applied
423
+ */
424
+ export function medianFilter(data, width, height, radius = 1) {
425
+ const result = new Uint8Array(data.length);
426
+ const clampedRadius = Math.max(1, Math.floor(radius));
427
+ for (let y = 0; y < height; y++) {
428
+ for (let x = 0; x < width; x++) {
429
+ const rValues = [];
430
+ const gValues = [];
431
+ const bValues = [];
432
+ const aValues = [];
433
+ // Collect values in kernel window
434
+ for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
435
+ for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
436
+ const px = x + kx;
437
+ const py = y + ky;
438
+ if (px >= 0 && px < width && py >= 0 && py < height) {
439
+ const idx = (py * width + px) * 4;
440
+ rValues.push(data[idx]);
441
+ gValues.push(data[idx + 1]);
442
+ bValues.push(data[idx + 2]);
443
+ aValues.push(data[idx + 3]);
444
+ }
445
+ }
446
+ }
447
+ // Sort and get median
448
+ rValues.sort((a, b) => a - b);
449
+ gValues.sort((a, b) => a - b);
450
+ bValues.sort((a, b) => a - b);
451
+ aValues.sort((a, b) => a - b);
452
+ const mid = Math.floor(rValues.length / 2);
453
+ const outIdx = (y * width + x) * 4;
454
+ result[outIdx] = rValues[mid];
455
+ result[outIdx + 1] = gValues[mid];
456
+ result[outIdx + 2] = bValues[mid];
457
+ result[outIdx + 3] = aValues[mid];
458
+ }
459
+ }
460
+ return result;
461
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cross-image",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A pure JavaScript, dependency-free, cross-runtime image processing library for Deno, Node.js, and Bun.",
5
5
  "keywords": [
6
6
  "image",
package/script/mod.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * @module @cross/image
3
3
  *
4
4
  * A pure JavaScript, dependency-free, cross-runtime image processing library.
5
- * Supports decoding, resizing, and encoding common image formats (PNG, JPEG, WebP, GIF, TIFF, BMP, DNG, PAM, PCX).
5
+ * Supports decoding, resizing, and encoding common image formats (PNG, APNG, JPEG, WebP, GIF, TIFF, BMP, ICO, DNG, PAM, PPM, PCX).
6
6
  * Includes image processing capabilities like compositing, level adjustments, and pixel manipulation.
7
7
  *
8
8
  * @example
@@ -45,13 +45,16 @@
45
45
  export { Image } from "./src/image.js";
46
46
  export type { ASCIIOptions, FrameMetadata, ImageData, ImageFormat, ImageFrame, ImageMetadata, MultiFrameImageData, ResizeOptions, WebPEncodeOptions, } from "./src/types.js";
47
47
  export { PNGFormat } from "./src/formats/png.js";
48
+ export { APNGFormat } from "./src/formats/apng.js";
48
49
  export { JPEGFormat } from "./src/formats/jpeg.js";
49
50
  export { WebPFormat } from "./src/formats/webp.js";
50
51
  export { GIFFormat } from "./src/formats/gif.js";
51
52
  export { type TIFFEncodeOptions, TIFFFormat } from "./src/formats/tiff.js";
52
53
  export { BMPFormat } from "./src/formats/bmp.js";
54
+ export { ICOFormat } from "./src/formats/ico.js";
53
55
  export { DNGFormat } from "./src/formats/dng.js";
54
56
  export { PAMFormat } from "./src/formats/pam.js";
55
57
  export { PCXFormat } from "./src/formats/pcx.js";
58
+ export { PPMFormat } from "./src/formats/ppm.js";
56
59
  export { ASCIIFormat } from "./src/formats/ascii.js";
57
60
  //# sourceMappingURL=mod.d.ts.map
package/script/mod.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @module @cross/image
4
4
  *
5
5
  * A pure JavaScript, dependency-free, cross-runtime image processing library.
6
- * Supports decoding, resizing, and encoding common image formats (PNG, JPEG, WebP, GIF, TIFF, BMP, DNG, PAM, PCX).
6
+ * Supports decoding, resizing, and encoding common image formats (PNG, APNG, JPEG, WebP, GIF, TIFF, BMP, ICO, DNG, PAM, PPM, PCX).
7
7
  * Includes image processing capabilities like compositing, level adjustments, and pixel manipulation.
8
8
  *
9
9
  * @example
@@ -44,11 +44,13 @@
44
44
  * ```
45
45
  */
46
46
  Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.ASCIIFormat = exports.PCXFormat = exports.PAMFormat = exports.DNGFormat = exports.BMPFormat = exports.TIFFFormat = exports.GIFFormat = exports.WebPFormat = exports.JPEGFormat = exports.PNGFormat = exports.Image = void 0;
47
+ exports.ASCIIFormat = exports.PPMFormat = exports.PCXFormat = exports.PAMFormat = exports.DNGFormat = exports.ICOFormat = exports.BMPFormat = exports.TIFFFormat = exports.GIFFormat = exports.WebPFormat = exports.JPEGFormat = exports.APNGFormat = exports.PNGFormat = exports.Image = void 0;
48
48
  var image_js_1 = require("./src/image.js");
49
49
  Object.defineProperty(exports, "Image", { enumerable: true, get: function () { return image_js_1.Image; } });
50
50
  var png_js_1 = require("./src/formats/png.js");
51
51
  Object.defineProperty(exports, "PNGFormat", { enumerable: true, get: function () { return png_js_1.PNGFormat; } });
52
+ var apng_js_1 = require("./src/formats/apng.js");
53
+ Object.defineProperty(exports, "APNGFormat", { enumerable: true, get: function () { return apng_js_1.APNGFormat; } });
52
54
  var jpeg_js_1 = require("./src/formats/jpeg.js");
53
55
  Object.defineProperty(exports, "JPEGFormat", { enumerable: true, get: function () { return jpeg_js_1.JPEGFormat; } });
54
56
  var webp_js_1 = require("./src/formats/webp.js");
@@ -59,11 +61,15 @@ var tiff_js_1 = require("./src/formats/tiff.js");
59
61
  Object.defineProperty(exports, "TIFFFormat", { enumerable: true, get: function () { return tiff_js_1.TIFFFormat; } });
60
62
  var bmp_js_1 = require("./src/formats/bmp.js");
61
63
  Object.defineProperty(exports, "BMPFormat", { enumerable: true, get: function () { return bmp_js_1.BMPFormat; } });
64
+ var ico_js_1 = require("./src/formats/ico.js");
65
+ Object.defineProperty(exports, "ICOFormat", { enumerable: true, get: function () { return ico_js_1.ICOFormat; } });
62
66
  var dng_js_1 = require("./src/formats/dng.js");
63
67
  Object.defineProperty(exports, "DNGFormat", { enumerable: true, get: function () { return dng_js_1.DNGFormat; } });
64
68
  var pam_js_1 = require("./src/formats/pam.js");
65
69
  Object.defineProperty(exports, "PAMFormat", { enumerable: true, get: function () { return pam_js_1.PAMFormat; } });
66
70
  var pcx_js_1 = require("./src/formats/pcx.js");
67
71
  Object.defineProperty(exports, "PCXFormat", { enumerable: true, get: function () { return pcx_js_1.PCXFormat; } });
72
+ var ppm_js_1 = require("./src/formats/ppm.js");
73
+ Object.defineProperty(exports, "PPMFormat", { enumerable: true, get: function () { return ppm_js_1.PPMFormat; } });
68
74
  var ascii_js_1 = require("./src/formats/ascii.js");
69
75
  Object.defineProperty(exports, "ASCIIFormat", { enumerable: true, get: function () { return ascii_js_1.ASCIIFormat; } });
@@ -0,0 +1,50 @@
1
+ import type { ImageData, ImageFormat, MultiFrameImageData } from "../types.js";
2
+ import { PNGBase } from "./png_base.js";
3
+ /**
4
+ * APNG (Animated PNG) format handler
5
+ * Implements support for animated PNG images with multiple frames
6
+ * APNG extends PNG with animation control chunks (acTL, fcTL, fdAT)
7
+ */
8
+ export declare class APNGFormat extends PNGBase implements ImageFormat {
9
+ /** Format name identifier */
10
+ readonly name = "apng";
11
+ /** MIME type for APNG images */
12
+ readonly mimeType = "image/apng";
13
+ /**
14
+ * Check if this format supports multiple frames (animations)
15
+ * @returns true for APNG format
16
+ */
17
+ supportsMultipleFrames(): boolean;
18
+ /**
19
+ * Check if the given data is an APNG image
20
+ * @param data Raw image data to check
21
+ * @returns true if data has PNG signature and contains acTL chunk
22
+ */
23
+ canDecode(data: Uint8Array): boolean;
24
+ /**
25
+ * Decode APNG image data to RGBA (first frame only)
26
+ * @param data Raw APNG image data
27
+ * @returns Decoded image data with RGBA pixels of first frame
28
+ */
29
+ decode(data: Uint8Array): Promise<ImageData>;
30
+ /**
31
+ * Decode all frames from APNG image
32
+ * @param data Raw APNG image data
33
+ * @returns Decoded multi-frame image data
34
+ */
35
+ decodeFrames(data: Uint8Array): Promise<MultiFrameImageData>;
36
+ /**
37
+ * Encode RGBA image data to APNG format (single frame)
38
+ * @param imageData Image data to encode
39
+ * @returns Encoded APNG image bytes
40
+ */
41
+ encode(imageData: ImageData): Promise<Uint8Array>;
42
+ /**
43
+ * Encode multi-frame image data to APNG format
44
+ * @param imageData Multi-frame image data to encode
45
+ * @returns Encoded APNG image bytes
46
+ */
47
+ encodeFrames(imageData: MultiFrameImageData): Promise<Uint8Array>;
48
+ private decodeFrameData;
49
+ }
50
+ //# sourceMappingURL=apng.d.ts.map