cross-image 0.1.2 → 0.1.5
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.
- package/README.md +143 -606
- package/esm/mod.d.ts +5 -5
- package/esm/mod.js +5 -5
- package/esm/src/formats/ascii.d.ts +11 -0
- package/esm/src/formats/ascii.js +14 -0
- package/esm/src/formats/bmp.d.ts +17 -0
- package/esm/src/formats/bmp.js +20 -0
- package/esm/src/formats/gif.d.ts +21 -0
- package/esm/src/formats/gif.js +26 -0
- package/esm/src/formats/jpeg.d.ts +17 -0
- package/esm/src/formats/jpeg.js +20 -0
- package/esm/src/formats/png.d.ts +17 -0
- package/esm/src/formats/png.js +20 -0
- package/esm/src/formats/raw.d.ts +17 -0
- package/esm/src/formats/raw.js +20 -0
- package/esm/src/formats/tiff.d.ts +16 -0
- package/esm/src/formats/tiff.js +37 -12
- package/esm/src/formats/webp.d.ts +18 -0
- package/esm/src/formats/webp.js +21 -0
- package/esm/src/image.d.ts +34 -1
- package/esm/src/image.js +55 -9
- package/esm/src/utils/security.d.ts +28 -0
- package/esm/src/utils/security.js +48 -0
- package/esm/src/utils/webp_decoder.js +3 -0
- package/package.json +1 -4
- package/script/mod.d.ts +5 -5
- package/script/mod.js +5 -5
- package/script/src/formats/ascii.d.ts +11 -0
- package/script/src/formats/ascii.js +14 -0
- package/script/src/formats/bmp.d.ts +17 -0
- package/script/src/formats/bmp.js +20 -0
- package/script/src/formats/gif.d.ts +21 -0
- package/script/src/formats/gif.js +26 -0
- package/script/src/formats/jpeg.d.ts +17 -0
- package/script/src/formats/jpeg.js +20 -0
- package/script/src/formats/png.d.ts +17 -0
- package/script/src/formats/png.js +20 -0
- package/script/src/formats/raw.d.ts +17 -0
- package/script/src/formats/raw.js +20 -0
- package/script/src/formats/tiff.d.ts +16 -0
- package/script/src/formats/tiff.js +37 -12
- package/script/src/formats/webp.d.ts +18 -0
- package/script/src/formats/webp.js +21 -0
- package/script/src/image.d.ts +34 -1
- package/script/src/image.js +55 -9
- package/script/src/utils/security.d.ts +28 -0
- package/script/src/utils/security.js +53 -0
- package/script/src/utils/webp_decoder.js +3 -0
- package/esm/mod.d.ts.map +0 -1
- package/esm/src/formats/ascii.d.ts.map +0 -1
- package/esm/src/formats/bmp.d.ts.map +0 -1
- package/esm/src/formats/gif.d.ts.map +0 -1
- package/esm/src/formats/jpeg.d.ts.map +0 -1
- package/esm/src/formats/png.d.ts.map +0 -1
- package/esm/src/formats/raw.d.ts.map +0 -1
- package/esm/src/formats/tiff.d.ts.map +0 -1
- package/esm/src/formats/webp.d.ts.map +0 -1
- package/esm/src/image.d.ts.map +0 -1
- package/esm/src/types.d.ts.map +0 -1
- package/esm/src/utils/gif_decoder.d.ts.map +0 -1
- package/esm/src/utils/gif_encoder.d.ts.map +0 -1
- package/esm/src/utils/jpeg_decoder.d.ts.map +0 -1
- package/esm/src/utils/jpeg_encoder.d.ts.map +0 -1
- package/esm/src/utils/lzw.d.ts.map +0 -1
- package/esm/src/utils/resize.d.ts.map +0 -1
- package/esm/src/utils/tiff_lzw.d.ts.map +0 -1
- package/esm/src/utils/webp_decoder.d.ts.map +0 -1
- package/esm/src/utils/webp_encoder.d.ts.map +0 -1
- package/script/mod.d.ts.map +0 -1
- package/script/src/formats/ascii.d.ts.map +0 -1
- package/script/src/formats/bmp.d.ts.map +0 -1
- package/script/src/formats/gif.d.ts.map +0 -1
- package/script/src/formats/jpeg.d.ts.map +0 -1
- package/script/src/formats/png.d.ts.map +0 -1
- package/script/src/formats/raw.d.ts.map +0 -1
- package/script/src/formats/tiff.d.ts.map +0 -1
- package/script/src/formats/webp.d.ts.map +0 -1
- package/script/src/image.d.ts.map +0 -1
- package/script/src/types.d.ts.map +0 -1
- package/script/src/utils/gif_decoder.d.ts.map +0 -1
- package/script/src/utils/gif_encoder.d.ts.map +0 -1
- package/script/src/utils/jpeg_decoder.d.ts.map +0 -1
- package/script/src/utils/jpeg_encoder.d.ts.map +0 -1
- package/script/src/utils/lzw.d.ts.map +0 -1
- package/script/src/utils/resize.d.ts.map +0 -1
- package/script/src/utils/tiff_lzw.d.ts.map +0 -1
- package/script/src/utils/webp_decoder.d.ts.map +0 -1
- package/script/src/utils/webp_encoder.d.ts.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ASCIIFormat = void 0;
|
|
4
|
+
const security_js_1 = require("../utils/security.js");
|
|
4
5
|
/**
|
|
5
6
|
* ASCII format handler
|
|
6
7
|
* Converts images to ASCII art text representation
|
|
@@ -65,6 +66,11 @@ class ASCIIFormat {
|
|
|
65
66
|
data[4] === this.MAGIC_BYTES[4] &&
|
|
66
67
|
data[5] === this.MAGIC_BYTES[5];
|
|
67
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Decode ASCII art to a basic grayscale RGBA image
|
|
71
|
+
* @param data Raw ASCII art data
|
|
72
|
+
* @returns Decoded image data with grayscale RGBA pixels
|
|
73
|
+
*/
|
|
68
74
|
decode(data) {
|
|
69
75
|
if (!this.canDecode(data)) {
|
|
70
76
|
throw new Error("Invalid ASCII art signature");
|
|
@@ -86,6 +92,8 @@ class ASCIIFormat {
|
|
|
86
92
|
// Calculate dimensions
|
|
87
93
|
const height = artLines.length;
|
|
88
94
|
const width = Math.max(...artLines.map((line) => line.length));
|
|
95
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
96
|
+
(0, security_js_1.validateImageDimensions)(width, height);
|
|
89
97
|
// Convert ASCII art back to image data
|
|
90
98
|
const imageData = new Uint8Array(width * height * 4);
|
|
91
99
|
const charset = this.CHARSETS[options.charset] || this.CHARSETS.simple;
|
|
@@ -114,6 +122,12 @@ class ASCIIFormat {
|
|
|
114
122
|
}
|
|
115
123
|
return Promise.resolve({ width, height, data: imageData });
|
|
116
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Encode RGBA image data to ASCII art
|
|
127
|
+
* @param imageData Image data to encode
|
|
128
|
+
* @param options Optional ASCII encoding options
|
|
129
|
+
* @returns Encoded ASCII art as UTF-8 bytes
|
|
130
|
+
*/
|
|
117
131
|
encode(imageData, options = {}) {
|
|
118
132
|
const { width: targetWidth = 80, charset = "simple", aspectRatio = 0.5, invert = false, } = options;
|
|
119
133
|
// Get character set
|
|
@@ -4,10 +4,27 @@ import type { ImageData, ImageFormat } from "../types.js";
|
|
|
4
4
|
* Implements a pure JavaScript BMP decoder and encoder
|
|
5
5
|
*/
|
|
6
6
|
export declare class BMPFormat implements ImageFormat {
|
|
7
|
+
/** Format name identifier */
|
|
7
8
|
readonly name = "bmp";
|
|
9
|
+
/** MIME type for BMP images */
|
|
8
10
|
readonly mimeType = "image/bmp";
|
|
11
|
+
/**
|
|
12
|
+
* Check if the given data is a BMP image
|
|
13
|
+
* @param data Raw image data to check
|
|
14
|
+
* @returns true if data has BMP signature
|
|
15
|
+
*/
|
|
9
16
|
canDecode(data: Uint8Array): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Decode BMP image data to RGBA
|
|
19
|
+
* @param data Raw BMP image data
|
|
20
|
+
* @returns Decoded image data with RGBA pixels
|
|
21
|
+
*/
|
|
10
22
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
23
|
+
/**
|
|
24
|
+
* Encode RGBA image data to BMP format
|
|
25
|
+
* @param imageData Image data to encode
|
|
26
|
+
* @returns Encoded BMP image bytes
|
|
27
|
+
*/
|
|
11
28
|
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
12
29
|
private readUint16LE;
|
|
13
30
|
private readUint32LE;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BMPFormat = void 0;
|
|
4
|
+
const security_js_1 = require("../utils/security.js");
|
|
4
5
|
// Constants for unit conversions
|
|
5
6
|
const INCHES_PER_METER = 39.3701;
|
|
6
7
|
/**
|
|
@@ -9,12 +10,14 @@ const INCHES_PER_METER = 39.3701;
|
|
|
9
10
|
*/
|
|
10
11
|
class BMPFormat {
|
|
11
12
|
constructor() {
|
|
13
|
+
/** Format name identifier */
|
|
12
14
|
Object.defineProperty(this, "name", {
|
|
13
15
|
enumerable: true,
|
|
14
16
|
configurable: true,
|
|
15
17
|
writable: true,
|
|
16
18
|
value: "bmp"
|
|
17
19
|
});
|
|
20
|
+
/** MIME type for BMP images */
|
|
18
21
|
Object.defineProperty(this, "mimeType", {
|
|
19
22
|
enumerable: true,
|
|
20
23
|
configurable: true,
|
|
@@ -22,11 +25,21 @@ class BMPFormat {
|
|
|
22
25
|
value: "image/bmp"
|
|
23
26
|
});
|
|
24
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if the given data is a BMP image
|
|
30
|
+
* @param data Raw image data to check
|
|
31
|
+
* @returns true if data has BMP signature
|
|
32
|
+
*/
|
|
25
33
|
canDecode(data) {
|
|
26
34
|
// BMP signature: 'BM' (0x42 0x4D)
|
|
27
35
|
return data.length >= 2 &&
|
|
28
36
|
data[0] === 0x42 && data[1] === 0x4d;
|
|
29
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Decode BMP image data to RGBA
|
|
40
|
+
* @param data Raw BMP image data
|
|
41
|
+
* @returns Decoded image data with RGBA pixels
|
|
42
|
+
*/
|
|
30
43
|
decode(data) {
|
|
31
44
|
if (!this.canDecode(data)) {
|
|
32
45
|
throw new Error("Invalid BMP signature");
|
|
@@ -64,6 +77,8 @@ class BMPFormat {
|
|
|
64
77
|
// Handle negative height (top-down bitmap)
|
|
65
78
|
const isTopDown = height < 0;
|
|
66
79
|
const absHeight = Math.abs(height);
|
|
80
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
81
|
+
(0, security_js_1.validateImageDimensions)(width, absHeight);
|
|
67
82
|
// Only support uncompressed BMPs for now
|
|
68
83
|
if (compression !== 0) {
|
|
69
84
|
throw new Error(`Compressed BMP not supported (compression type: ${compression})`);
|
|
@@ -97,6 +112,11 @@ class BMPFormat {
|
|
|
97
112
|
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
|
98
113
|
});
|
|
99
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Encode RGBA image data to BMP format
|
|
117
|
+
* @param imageData Image data to encode
|
|
118
|
+
* @returns Encoded BMP image bytes
|
|
119
|
+
*/
|
|
100
120
|
encode(imageData) {
|
|
101
121
|
const { width, height, data, metadata } = imageData;
|
|
102
122
|
// Calculate sizes
|
|
@@ -12,12 +12,33 @@ import type { ImageData, ImageFormat, MultiFrameImageData } from "../types.js";
|
|
|
12
12
|
* - Falls back to runtime APIs when pure-JS fails
|
|
13
13
|
*/
|
|
14
14
|
export declare class GIFFormat implements ImageFormat {
|
|
15
|
+
/** Format name identifier */
|
|
15
16
|
readonly name = "gif";
|
|
17
|
+
/** MIME type for GIF images */
|
|
16
18
|
readonly mimeType = "image/gif";
|
|
19
|
+
/**
|
|
20
|
+
* Check if this format supports multiple frames (animations)
|
|
21
|
+
* @returns true for GIF format
|
|
22
|
+
*/
|
|
17
23
|
supportsMultipleFrames(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Check if the given data is a GIF image
|
|
26
|
+
* @param data Raw image data to check
|
|
27
|
+
* @returns true if data has GIF signature
|
|
28
|
+
*/
|
|
18
29
|
canDecode(data: Uint8Array): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Decode GIF image data to RGBA (first frame only)
|
|
32
|
+
* @param data Raw GIF image data
|
|
33
|
+
* @returns Decoded image data with RGBA pixels of first frame
|
|
34
|
+
*/
|
|
19
35
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
20
36
|
private extractMetadata;
|
|
37
|
+
/**
|
|
38
|
+
* Encode RGBA image data to GIF format (single frame)
|
|
39
|
+
* @param imageData Image data to encode
|
|
40
|
+
* @returns Encoded GIF image bytes
|
|
41
|
+
*/
|
|
21
42
|
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
22
43
|
/**
|
|
23
44
|
* Decode all frames from an animated GIF
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GIFFormat = void 0;
|
|
4
4
|
const gif_decoder_js_1 = require("../utils/gif_decoder.js");
|
|
5
5
|
const gif_encoder_js_1 = require("../utils/gif_encoder.js");
|
|
6
|
+
const security_js_1 = require("../utils/security.js");
|
|
6
7
|
/**
|
|
7
8
|
* GIF format handler
|
|
8
9
|
* Now includes pure-JS implementation with custom LZW compression/decompression
|
|
@@ -17,12 +18,14 @@ const gif_encoder_js_1 = require("../utils/gif_encoder.js");
|
|
|
17
18
|
*/
|
|
18
19
|
class GIFFormat {
|
|
19
20
|
constructor() {
|
|
21
|
+
/** Format name identifier */
|
|
20
22
|
Object.defineProperty(this, "name", {
|
|
21
23
|
enumerable: true,
|
|
22
24
|
configurable: true,
|
|
23
25
|
writable: true,
|
|
24
26
|
value: "gif"
|
|
25
27
|
});
|
|
28
|
+
/** MIME type for GIF images */
|
|
26
29
|
Object.defineProperty(this, "mimeType", {
|
|
27
30
|
enumerable: true,
|
|
28
31
|
configurable: true,
|
|
@@ -30,9 +33,18 @@ class GIFFormat {
|
|
|
30
33
|
value: "image/gif"
|
|
31
34
|
});
|
|
32
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if this format supports multiple frames (animations)
|
|
38
|
+
* @returns true for GIF format
|
|
39
|
+
*/
|
|
33
40
|
supportsMultipleFrames() {
|
|
34
41
|
return true;
|
|
35
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if the given data is a GIF image
|
|
45
|
+
* @param data Raw image data to check
|
|
46
|
+
* @returns true if data has GIF signature
|
|
47
|
+
*/
|
|
36
48
|
canDecode(data) {
|
|
37
49
|
// GIF signature: "GIF87a" or "GIF89a"
|
|
38
50
|
return data.length >= 6 &&
|
|
@@ -41,6 +53,11 @@ class GIFFormat {
|
|
|
41
53
|
(data[4] === 0x37 || data[4] === 0x39) && // "7" or "9"
|
|
42
54
|
data[5] === 0x61; // "a"
|
|
43
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Decode GIF image data to RGBA (first frame only)
|
|
58
|
+
* @param data Raw GIF image data
|
|
59
|
+
* @returns Decoded image data with RGBA pixels of first frame
|
|
60
|
+
*/
|
|
44
61
|
async decode(data) {
|
|
45
62
|
if (!this.canDecode(data)) {
|
|
46
63
|
throw new Error("Invalid GIF signature");
|
|
@@ -49,6 +66,8 @@ class GIFFormat {
|
|
|
49
66
|
try {
|
|
50
67
|
const decoder = new gif_decoder_js_1.GIFDecoder(data);
|
|
51
68
|
const result = decoder.decode();
|
|
69
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
70
|
+
(0, security_js_1.validateImageDimensions)(result.width, result.height);
|
|
52
71
|
// Extract metadata from comment extensions
|
|
53
72
|
const metadata = this.extractMetadata(data);
|
|
54
73
|
return {
|
|
@@ -65,6 +84,8 @@ class GIFFormat {
|
|
|
65
84
|
const width = this.readUint16LE(data, pos);
|
|
66
85
|
pos += 2;
|
|
67
86
|
const height = this.readUint16LE(data, pos);
|
|
87
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
88
|
+
(0, security_js_1.validateImageDimensions)(width, height);
|
|
68
89
|
const rgba = await this.decodeUsingRuntime(data, width, height);
|
|
69
90
|
const metadata = this.extractMetadata(data);
|
|
70
91
|
return {
|
|
@@ -127,6 +148,11 @@ class GIFFormat {
|
|
|
127
148
|
}
|
|
128
149
|
return metadata;
|
|
129
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Encode RGBA image data to GIF format (single frame)
|
|
153
|
+
* @param imageData Image data to encode
|
|
154
|
+
* @returns Encoded GIF image bytes
|
|
155
|
+
*/
|
|
130
156
|
async encode(imageData) {
|
|
131
157
|
const { width, height, data, metadata } = imageData;
|
|
132
158
|
// Try pure-JS encoder first
|
|
@@ -4,10 +4,27 @@ import type { ImageData, ImageFormat } from "../types.js";
|
|
|
4
4
|
* Implements a basic JPEG decoder and encoder
|
|
5
5
|
*/
|
|
6
6
|
export declare class JPEGFormat implements ImageFormat {
|
|
7
|
+
/** Format name identifier */
|
|
7
8
|
readonly name = "jpeg";
|
|
9
|
+
/** MIME type for JPEG images */
|
|
8
10
|
readonly mimeType = "image/jpeg";
|
|
11
|
+
/**
|
|
12
|
+
* Check if the given data is a JPEG image
|
|
13
|
+
* @param data Raw image data to check
|
|
14
|
+
* @returns true if data has JPEG signature
|
|
15
|
+
*/
|
|
9
16
|
canDecode(data: Uint8Array): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Decode JPEG image data to RGBA
|
|
19
|
+
* @param data Raw JPEG image data
|
|
20
|
+
* @returns Decoded image data with RGBA pixels
|
|
21
|
+
*/
|
|
10
22
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
23
|
+
/**
|
|
24
|
+
* Encode RGBA image data to JPEG format
|
|
25
|
+
* @param imageData Image data to encode
|
|
26
|
+
* @returns Encoded JPEG image bytes
|
|
27
|
+
*/
|
|
11
28
|
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
12
29
|
private injectMetadata;
|
|
13
30
|
private decodeUsingRuntime;
|
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.JPEGFormat = void 0;
|
|
37
|
+
const security_js_1 = require("../utils/security.js");
|
|
37
38
|
// Constants for unit conversions
|
|
38
39
|
const CM_PER_INCH = 2.54;
|
|
39
40
|
/**
|
|
@@ -42,12 +43,14 @@ const CM_PER_INCH = 2.54;
|
|
|
42
43
|
*/
|
|
43
44
|
class JPEGFormat {
|
|
44
45
|
constructor() {
|
|
46
|
+
/** Format name identifier */
|
|
45
47
|
Object.defineProperty(this, "name", {
|
|
46
48
|
enumerable: true,
|
|
47
49
|
configurable: true,
|
|
48
50
|
writable: true,
|
|
49
51
|
value: "jpeg"
|
|
50
52
|
});
|
|
53
|
+
/** MIME type for JPEG images */
|
|
51
54
|
Object.defineProperty(this, "mimeType", {
|
|
52
55
|
enumerable: true,
|
|
53
56
|
configurable: true,
|
|
@@ -55,11 +58,21 @@ class JPEGFormat {
|
|
|
55
58
|
value: "image/jpeg"
|
|
56
59
|
});
|
|
57
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Check if the given data is a JPEG image
|
|
63
|
+
* @param data Raw image data to check
|
|
64
|
+
* @returns true if data has JPEG signature
|
|
65
|
+
*/
|
|
58
66
|
canDecode(data) {
|
|
59
67
|
// JPEG signature: FF D8 FF
|
|
60
68
|
return data.length >= 3 &&
|
|
61
69
|
data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff;
|
|
62
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Decode JPEG image data to RGBA
|
|
73
|
+
* @param data Raw JPEG image data
|
|
74
|
+
* @returns Decoded image data with RGBA pixels
|
|
75
|
+
*/
|
|
63
76
|
async decode(data) {
|
|
64
77
|
if (!this.canDecode(data)) {
|
|
65
78
|
throw new Error("Invalid JPEG signature");
|
|
@@ -114,6 +127,8 @@ class JPEGFormat {
|
|
|
114
127
|
if (width === 0 || height === 0) {
|
|
115
128
|
throw new Error("Could not determine JPEG dimensions");
|
|
116
129
|
}
|
|
130
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
131
|
+
(0, security_js_1.validateImageDimensions)(width, height);
|
|
117
132
|
// For a pure JS implementation, we'd need to implement full JPEG decoding
|
|
118
133
|
// which is very complex. Instead, we'll use the browser/runtime's decoder.
|
|
119
134
|
const rgba = await this.decodeUsingRuntime(data, width, height);
|
|
@@ -124,6 +139,11 @@ class JPEGFormat {
|
|
|
124
139
|
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
|
125
140
|
};
|
|
126
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Encode RGBA image data to JPEG format
|
|
144
|
+
* @param imageData Image data to encode
|
|
145
|
+
* @returns Encoded JPEG image bytes
|
|
146
|
+
*/
|
|
127
147
|
async encode(imageData) {
|
|
128
148
|
const { width, height, data, metadata } = imageData;
|
|
129
149
|
// Try to use runtime encoding if available (better quality)
|
|
@@ -4,10 +4,27 @@ import type { ImageData, ImageFormat } from "../types.js";
|
|
|
4
4
|
* Implements a pure JavaScript PNG decoder and encoder
|
|
5
5
|
*/
|
|
6
6
|
export declare class PNGFormat implements ImageFormat {
|
|
7
|
+
/** Format name identifier */
|
|
7
8
|
readonly name = "png";
|
|
9
|
+
/** MIME type for PNG images */
|
|
8
10
|
readonly mimeType = "image/png";
|
|
11
|
+
/**
|
|
12
|
+
* Check if the given data is a PNG image
|
|
13
|
+
* @param data Raw image data to check
|
|
14
|
+
* @returns true if data has PNG signature
|
|
15
|
+
*/
|
|
9
16
|
canDecode(data: Uint8Array): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Decode PNG image data to RGBA
|
|
19
|
+
* @param data Raw PNG image data
|
|
20
|
+
* @returns Decoded image data with RGBA pixels
|
|
21
|
+
*/
|
|
10
22
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
23
|
+
/**
|
|
24
|
+
* Encode RGBA image data to PNG format
|
|
25
|
+
* @param imageData Image data to encode
|
|
26
|
+
* @returns Encoded PNG image bytes
|
|
27
|
+
*/
|
|
11
28
|
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
12
29
|
private readUint32;
|
|
13
30
|
private writeUint32;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PNGFormat = void 0;
|
|
4
|
+
const security_js_1 = require("../utils/security.js");
|
|
4
5
|
// Constants for unit conversions
|
|
5
6
|
const INCHES_PER_METER = 39.3701;
|
|
6
7
|
/**
|
|
@@ -9,12 +10,14 @@ const INCHES_PER_METER = 39.3701;
|
|
|
9
10
|
*/
|
|
10
11
|
class PNGFormat {
|
|
11
12
|
constructor() {
|
|
13
|
+
/** Format name identifier */
|
|
12
14
|
Object.defineProperty(this, "name", {
|
|
13
15
|
enumerable: true,
|
|
14
16
|
configurable: true,
|
|
15
17
|
writable: true,
|
|
16
18
|
value: "png"
|
|
17
19
|
});
|
|
20
|
+
/** MIME type for PNG images */
|
|
18
21
|
Object.defineProperty(this, "mimeType", {
|
|
19
22
|
enumerable: true,
|
|
20
23
|
configurable: true,
|
|
@@ -22,6 +25,11 @@ class PNGFormat {
|
|
|
22
25
|
value: "image/png"
|
|
23
26
|
});
|
|
24
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if the given data is a PNG image
|
|
30
|
+
* @param data Raw image data to check
|
|
31
|
+
* @returns true if data has PNG signature
|
|
32
|
+
*/
|
|
25
33
|
canDecode(data) {
|
|
26
34
|
// PNG signature: 137 80 78 71 13 10 26 10
|
|
27
35
|
return data.length >= 8 &&
|
|
@@ -30,6 +38,11 @@ class PNGFormat {
|
|
|
30
38
|
data[4] === 13 && data[5] === 10 &&
|
|
31
39
|
data[6] === 26 && data[7] === 10;
|
|
32
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Decode PNG image data to RGBA
|
|
43
|
+
* @param data Raw PNG image data
|
|
44
|
+
* @returns Decoded image data with RGBA pixels
|
|
45
|
+
*/
|
|
33
46
|
async decode(data) {
|
|
34
47
|
if (!this.canDecode(data)) {
|
|
35
48
|
throw new Error("Invalid PNG signature");
|
|
@@ -82,6 +95,8 @@ class PNGFormat {
|
|
|
82
95
|
if (width === 0 || height === 0) {
|
|
83
96
|
throw new Error("Invalid PNG: missing IHDR chunk");
|
|
84
97
|
}
|
|
98
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
99
|
+
(0, security_js_1.validateImageDimensions)(width, height);
|
|
85
100
|
// Concatenate IDAT chunks
|
|
86
101
|
const idatData = this.concatenateChunks(chunks);
|
|
87
102
|
// Decompress data
|
|
@@ -95,6 +110,11 @@ class PNGFormat {
|
|
|
95
110
|
metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
|
|
96
111
|
};
|
|
97
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Encode RGBA image data to PNG format
|
|
115
|
+
* @param imageData Image data to encode
|
|
116
|
+
* @returns Encoded PNG image bytes
|
|
117
|
+
*/
|
|
98
118
|
async encode(imageData) {
|
|
99
119
|
const { width, height, data, metadata } = imageData;
|
|
100
120
|
// Prepare IHDR chunk
|
|
@@ -10,12 +10,29 @@ import type { ImageData, ImageFormat } from "../types.js";
|
|
|
10
10
|
* - RGBA pixel data (width * height * 4 bytes)
|
|
11
11
|
*/
|
|
12
12
|
export declare class RAWFormat implements ImageFormat {
|
|
13
|
+
/** Format name identifier */
|
|
13
14
|
readonly name = "raw";
|
|
15
|
+
/** MIME type for RAW images */
|
|
14
16
|
readonly mimeType = "image/raw";
|
|
15
17
|
private readonly MAGIC_BYTES;
|
|
16
18
|
private readonly HEADER_SIZE;
|
|
19
|
+
/**
|
|
20
|
+
* Check if the given data is a RAW image
|
|
21
|
+
* @param data Raw image data to check
|
|
22
|
+
* @returns true if data has RAW signature
|
|
23
|
+
*/
|
|
17
24
|
canDecode(data: Uint8Array): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Decode RAW image data to RGBA
|
|
27
|
+
* @param data Raw RAW image data
|
|
28
|
+
* @returns Decoded image data with RGBA pixels
|
|
29
|
+
*/
|
|
18
30
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
31
|
+
/**
|
|
32
|
+
* Encode RGBA image data to RAW format
|
|
33
|
+
* @param imageData Image data to encode
|
|
34
|
+
* @returns Encoded RAW image bytes
|
|
35
|
+
*/
|
|
19
36
|
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
20
37
|
private readUint32;
|
|
21
38
|
private writeUint32;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RAWFormat = void 0;
|
|
4
|
+
const security_js_1 = require("../utils/security.js");
|
|
4
5
|
/**
|
|
5
6
|
* RAW format handler
|
|
6
7
|
* Implements a simple uncompressed RGBA format with a minimal header
|
|
@@ -13,12 +14,14 @@ exports.RAWFormat = void 0;
|
|
|
13
14
|
*/
|
|
14
15
|
class RAWFormat {
|
|
15
16
|
constructor() {
|
|
17
|
+
/** Format name identifier */
|
|
16
18
|
Object.defineProperty(this, "name", {
|
|
17
19
|
enumerable: true,
|
|
18
20
|
configurable: true,
|
|
19
21
|
writable: true,
|
|
20
22
|
value: "raw"
|
|
21
23
|
});
|
|
24
|
+
/** MIME type for RAW images */
|
|
22
25
|
Object.defineProperty(this, "mimeType", {
|
|
23
26
|
enumerable: true,
|
|
24
27
|
configurable: true,
|
|
@@ -38,6 +41,11 @@ class RAWFormat {
|
|
|
38
41
|
value: 12
|
|
39
42
|
}); // 4 bytes magic + 4 bytes width + 4 bytes height
|
|
40
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Check if the given data is a RAW image
|
|
46
|
+
* @param data Raw image data to check
|
|
47
|
+
* @returns true if data has RAW signature
|
|
48
|
+
*/
|
|
41
49
|
canDecode(data) {
|
|
42
50
|
// Check if data has at least header size and matches magic bytes
|
|
43
51
|
if (data.length < this.HEADER_SIZE) {
|
|
@@ -48,6 +56,11 @@ class RAWFormat {
|
|
|
48
56
|
data[2] === this.MAGIC_BYTES[2] &&
|
|
49
57
|
data[3] === this.MAGIC_BYTES[3];
|
|
50
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Decode RAW image data to RGBA
|
|
61
|
+
* @param data Raw RAW image data
|
|
62
|
+
* @returns Decoded image data with RGBA pixels
|
|
63
|
+
*/
|
|
51
64
|
decode(data) {
|
|
52
65
|
if (!this.canDecode(data)) {
|
|
53
66
|
throw new Error("Invalid RAW signature");
|
|
@@ -59,6 +72,8 @@ class RAWFormat {
|
|
|
59
72
|
if (width <= 0 || height <= 0) {
|
|
60
73
|
throw new Error(`Invalid RAW dimensions: ${width}x${height}`);
|
|
61
74
|
}
|
|
75
|
+
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
76
|
+
(0, security_js_1.validateImageDimensions)(width, height);
|
|
62
77
|
const expectedDataLength = width * height * 4;
|
|
63
78
|
const actualDataLength = data.length - this.HEADER_SIZE;
|
|
64
79
|
if (actualDataLength !== expectedDataLength) {
|
|
@@ -69,6 +84,11 @@ class RAWFormat {
|
|
|
69
84
|
pixelData.set(data.subarray(this.HEADER_SIZE));
|
|
70
85
|
return Promise.resolve({ width, height, data: pixelData });
|
|
71
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Encode RGBA image data to RAW format
|
|
89
|
+
* @param imageData Image data to encode
|
|
90
|
+
* @returns Encoded RAW image bytes
|
|
91
|
+
*/
|
|
72
92
|
encode(imageData) {
|
|
73
93
|
const { width, height, data } = imageData;
|
|
74
94
|
// Validate input
|
|
@@ -14,10 +14,26 @@ export interface TIFFEncodeOptions {
|
|
|
14
14
|
* Supports multi-page TIFF files.
|
|
15
15
|
*/
|
|
16
16
|
export declare class TIFFFormat implements ImageFormat {
|
|
17
|
+
/** Format name identifier */
|
|
17
18
|
readonly name = "tiff";
|
|
19
|
+
/** MIME type for TIFF images */
|
|
18
20
|
readonly mimeType = "image/tiff";
|
|
21
|
+
/**
|
|
22
|
+
* Check if this format supports multiple frames (pages)
|
|
23
|
+
* @returns true for TIFF format
|
|
24
|
+
*/
|
|
19
25
|
supportsMultipleFrames(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Check if the given data is a TIFF image
|
|
28
|
+
* @param data Raw image data to check
|
|
29
|
+
* @returns true if data has TIFF signature
|
|
30
|
+
*/
|
|
20
31
|
canDecode(data: Uint8Array): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Decode TIFF image data to RGBA (first page only)
|
|
34
|
+
* @param data Raw TIFF image data
|
|
35
|
+
* @returns Decoded image data with RGBA pixels of first page
|
|
36
|
+
*/
|
|
21
37
|
decode(data: Uint8Array): Promise<ImageData>;
|
|
22
38
|
encode(imageData: ImageData, options?: unknown): Promise<Uint8Array>;
|
|
23
39
|
/**
|