cross-image 0.1.4 → 0.2.0
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 +155 -121
- package/esm/mod.d.ts +32 -8
- package/esm/mod.js +32 -8
- package/esm/src/formats/dng.d.ts +27 -0
- package/esm/src/formats/dng.js +191 -0
- package/esm/src/formats/pam.d.ts +43 -0
- package/esm/src/formats/pam.js +177 -0
- package/esm/src/formats/pcx.d.ts +13 -0
- package/esm/src/formats/pcx.js +204 -0
- package/esm/src/formats/tiff.d.ts +7 -7
- package/esm/src/image.d.ts +133 -1
- package/esm/src/image.js +250 -11
- package/esm/src/utils/image_processing.d.ts +91 -0
- package/esm/src/utils/image_processing.js +231 -0
- package/esm/src/utils/webp_decoder.js +47 -12
- package/esm/src/utils/webp_encoder.js +97 -39
- package/package.json +4 -1
- package/script/mod.d.ts +32 -8
- package/script/mod.js +36 -10
- package/script/src/formats/dng.d.ts +27 -0
- package/script/src/formats/dng.js +195 -0
- package/script/src/formats/pam.d.ts +43 -0
- package/script/src/formats/pam.js +181 -0
- package/script/src/formats/pcx.d.ts +13 -0
- package/script/src/formats/pcx.js +208 -0
- package/script/src/formats/tiff.d.ts +7 -7
- package/script/src/image.d.ts +133 -1
- package/script/src/image.js +250 -11
- package/script/src/utils/image_processing.d.ts +91 -0
- package/script/src/utils/image_processing.js +242 -0
- package/script/src/utils/webp_decoder.js +47 -12
- package/script/src/utils/webp_encoder.js +97 -39
- package/esm/src/formats/raw.d.ts +0 -40
- package/esm/src/formats/raw.js +0 -118
- package/script/src/formats/raw.d.ts +0 -40
- package/script/src/formats/raw.js +0 -122
package/esm/src/formats/raw.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { validateImageDimensions } from "../utils/security.js";
|
|
2
|
-
/**
|
|
3
|
-
* RAW format handler
|
|
4
|
-
* Implements a simple uncompressed RGBA format with a minimal header
|
|
5
|
-
*
|
|
6
|
-
* Format structure:
|
|
7
|
-
* - Magic bytes (4 bytes): "RGBA" (0x52 0x47 0x42 0x41)
|
|
8
|
-
* - Width (4 bytes, big-endian)
|
|
9
|
-
* - Height (4 bytes, big-endian)
|
|
10
|
-
* - RGBA pixel data (width * height * 4 bytes)
|
|
11
|
-
*/
|
|
12
|
-
export class RAWFormat {
|
|
13
|
-
constructor() {
|
|
14
|
-
/** Format name identifier */
|
|
15
|
-
Object.defineProperty(this, "name", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: "raw"
|
|
20
|
-
});
|
|
21
|
-
/** MIME type for RAW images */
|
|
22
|
-
Object.defineProperty(this, "mimeType", {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
writable: true,
|
|
26
|
-
value: "image/raw"
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(this, "MAGIC_BYTES", {
|
|
29
|
-
enumerable: true,
|
|
30
|
-
configurable: true,
|
|
31
|
-
writable: true,
|
|
32
|
-
value: new Uint8Array([0x52, 0x47, 0x42, 0x41])
|
|
33
|
-
}); // "RGBA"
|
|
34
|
-
Object.defineProperty(this, "HEADER_SIZE", {
|
|
35
|
-
enumerable: true,
|
|
36
|
-
configurable: true,
|
|
37
|
-
writable: true,
|
|
38
|
-
value: 12
|
|
39
|
-
}); // 4 bytes magic + 4 bytes width + 4 bytes height
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Check if the given data is a RAW image
|
|
43
|
-
* @param data Raw image data to check
|
|
44
|
-
* @returns true if data has RAW signature
|
|
45
|
-
*/
|
|
46
|
-
canDecode(data) {
|
|
47
|
-
// Check if data has at least header size and matches magic bytes
|
|
48
|
-
if (data.length < this.HEADER_SIZE) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return data[0] === this.MAGIC_BYTES[0] &&
|
|
52
|
-
data[1] === this.MAGIC_BYTES[1] &&
|
|
53
|
-
data[2] === this.MAGIC_BYTES[2] &&
|
|
54
|
-
data[3] === this.MAGIC_BYTES[3];
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Decode RAW image data to RGBA
|
|
58
|
-
* @param data Raw RAW image data
|
|
59
|
-
* @returns Decoded image data with RGBA pixels
|
|
60
|
-
*/
|
|
61
|
-
decode(data) {
|
|
62
|
-
if (!this.canDecode(data)) {
|
|
63
|
-
throw new Error("Invalid RAW signature");
|
|
64
|
-
}
|
|
65
|
-
// Read width and height from header (big-endian)
|
|
66
|
-
const width = this.readUint32(data, 4);
|
|
67
|
-
const height = this.readUint32(data, 8);
|
|
68
|
-
// Validate dimensions
|
|
69
|
-
if (width <= 0 || height <= 0) {
|
|
70
|
-
throw new Error(`Invalid RAW dimensions: ${width}x${height}`);
|
|
71
|
-
}
|
|
72
|
-
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
73
|
-
validateImageDimensions(width, height);
|
|
74
|
-
const expectedDataLength = width * height * 4;
|
|
75
|
-
const actualDataLength = data.length - this.HEADER_SIZE;
|
|
76
|
-
if (actualDataLength !== expectedDataLength) {
|
|
77
|
-
throw new Error(`Invalid RAW data length: expected ${expectedDataLength}, got ${actualDataLength}`);
|
|
78
|
-
}
|
|
79
|
-
// Extract pixel data
|
|
80
|
-
const pixelData = new Uint8Array(expectedDataLength);
|
|
81
|
-
pixelData.set(data.subarray(this.HEADER_SIZE));
|
|
82
|
-
return Promise.resolve({ width, height, data: pixelData });
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Encode RGBA image data to RAW format
|
|
86
|
-
* @param imageData Image data to encode
|
|
87
|
-
* @returns Encoded RAW image bytes
|
|
88
|
-
*/
|
|
89
|
-
encode(imageData) {
|
|
90
|
-
const { width, height, data } = imageData;
|
|
91
|
-
// Validate input
|
|
92
|
-
if (data.length !== width * height * 4) {
|
|
93
|
-
throw new Error(`Data length mismatch: expected ${width * height * 4}, got ${data.length}`);
|
|
94
|
-
}
|
|
95
|
-
// Create output buffer with header + pixel data
|
|
96
|
-
const output = new Uint8Array(this.HEADER_SIZE + data.length);
|
|
97
|
-
// Write magic bytes
|
|
98
|
-
output.set(this.MAGIC_BYTES, 0);
|
|
99
|
-
// Write width and height (big-endian)
|
|
100
|
-
this.writeUint32(output, 4, width);
|
|
101
|
-
this.writeUint32(output, 8, height);
|
|
102
|
-
// Write pixel data
|
|
103
|
-
output.set(data, this.HEADER_SIZE);
|
|
104
|
-
return Promise.resolve(output);
|
|
105
|
-
}
|
|
106
|
-
readUint32(data, offset) {
|
|
107
|
-
return ((data[offset] << 24) |
|
|
108
|
-
(data[offset + 1] << 16) |
|
|
109
|
-
(data[offset + 2] << 8) |
|
|
110
|
-
data[offset + 3]);
|
|
111
|
-
}
|
|
112
|
-
writeUint32(data, offset, value) {
|
|
113
|
-
data[offset] = (value >>> 24) & 0xff;
|
|
114
|
-
data[offset + 1] = (value >>> 16) & 0xff;
|
|
115
|
-
data[offset + 2] = (value >>> 8) & 0xff;
|
|
116
|
-
data[offset + 3] = value & 0xff;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { ImageData, ImageFormat } from "../types.js";
|
|
2
|
-
/**
|
|
3
|
-
* RAW format handler
|
|
4
|
-
* Implements a simple uncompressed RGBA format with a minimal header
|
|
5
|
-
*
|
|
6
|
-
* Format structure:
|
|
7
|
-
* - Magic bytes (4 bytes): "RGBA" (0x52 0x47 0x42 0x41)
|
|
8
|
-
* - Width (4 bytes, big-endian)
|
|
9
|
-
* - Height (4 bytes, big-endian)
|
|
10
|
-
* - RGBA pixel data (width * height * 4 bytes)
|
|
11
|
-
*/
|
|
12
|
-
export declare class RAWFormat implements ImageFormat {
|
|
13
|
-
/** Format name identifier */
|
|
14
|
-
readonly name = "raw";
|
|
15
|
-
/** MIME type for RAW images */
|
|
16
|
-
readonly mimeType = "image/raw";
|
|
17
|
-
private readonly MAGIC_BYTES;
|
|
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
|
-
*/
|
|
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
|
-
*/
|
|
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
|
-
*/
|
|
36
|
-
encode(imageData: ImageData): Promise<Uint8Array>;
|
|
37
|
-
private readUint32;
|
|
38
|
-
private writeUint32;
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=raw.d.ts.map
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RAWFormat = void 0;
|
|
4
|
-
const security_js_1 = require("../utils/security.js");
|
|
5
|
-
/**
|
|
6
|
-
* RAW format handler
|
|
7
|
-
* Implements a simple uncompressed RGBA format with a minimal header
|
|
8
|
-
*
|
|
9
|
-
* Format structure:
|
|
10
|
-
* - Magic bytes (4 bytes): "RGBA" (0x52 0x47 0x42 0x41)
|
|
11
|
-
* - Width (4 bytes, big-endian)
|
|
12
|
-
* - Height (4 bytes, big-endian)
|
|
13
|
-
* - RGBA pixel data (width * height * 4 bytes)
|
|
14
|
-
*/
|
|
15
|
-
class RAWFormat {
|
|
16
|
-
constructor() {
|
|
17
|
-
/** Format name identifier */
|
|
18
|
-
Object.defineProperty(this, "name", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: "raw"
|
|
23
|
-
});
|
|
24
|
-
/** MIME type for RAW images */
|
|
25
|
-
Object.defineProperty(this, "mimeType", {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true,
|
|
29
|
-
value: "image/raw"
|
|
30
|
-
});
|
|
31
|
-
Object.defineProperty(this, "MAGIC_BYTES", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: true,
|
|
34
|
-
writable: true,
|
|
35
|
-
value: new Uint8Array([0x52, 0x47, 0x42, 0x41])
|
|
36
|
-
}); // "RGBA"
|
|
37
|
-
Object.defineProperty(this, "HEADER_SIZE", {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
configurable: true,
|
|
40
|
-
writable: true,
|
|
41
|
-
value: 12
|
|
42
|
-
}); // 4 bytes magic + 4 bytes width + 4 bytes height
|
|
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
|
-
*/
|
|
49
|
-
canDecode(data) {
|
|
50
|
-
// Check if data has at least header size and matches magic bytes
|
|
51
|
-
if (data.length < this.HEADER_SIZE) {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
return data[0] === this.MAGIC_BYTES[0] &&
|
|
55
|
-
data[1] === this.MAGIC_BYTES[1] &&
|
|
56
|
-
data[2] === this.MAGIC_BYTES[2] &&
|
|
57
|
-
data[3] === this.MAGIC_BYTES[3];
|
|
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
|
-
*/
|
|
64
|
-
decode(data) {
|
|
65
|
-
if (!this.canDecode(data)) {
|
|
66
|
-
throw new Error("Invalid RAW signature");
|
|
67
|
-
}
|
|
68
|
-
// Read width and height from header (big-endian)
|
|
69
|
-
const width = this.readUint32(data, 4);
|
|
70
|
-
const height = this.readUint32(data, 8);
|
|
71
|
-
// Validate dimensions
|
|
72
|
-
if (width <= 0 || height <= 0) {
|
|
73
|
-
throw new Error(`Invalid RAW dimensions: ${width}x${height}`);
|
|
74
|
-
}
|
|
75
|
-
// Validate dimensions for security (prevent integer overflow and heap exhaustion)
|
|
76
|
-
(0, security_js_1.validateImageDimensions)(width, height);
|
|
77
|
-
const expectedDataLength = width * height * 4;
|
|
78
|
-
const actualDataLength = data.length - this.HEADER_SIZE;
|
|
79
|
-
if (actualDataLength !== expectedDataLength) {
|
|
80
|
-
throw new Error(`Invalid RAW data length: expected ${expectedDataLength}, got ${actualDataLength}`);
|
|
81
|
-
}
|
|
82
|
-
// Extract pixel data
|
|
83
|
-
const pixelData = new Uint8Array(expectedDataLength);
|
|
84
|
-
pixelData.set(data.subarray(this.HEADER_SIZE));
|
|
85
|
-
return Promise.resolve({ width, height, data: pixelData });
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Encode RGBA image data to RAW format
|
|
89
|
-
* @param imageData Image data to encode
|
|
90
|
-
* @returns Encoded RAW image bytes
|
|
91
|
-
*/
|
|
92
|
-
encode(imageData) {
|
|
93
|
-
const { width, height, data } = imageData;
|
|
94
|
-
// Validate input
|
|
95
|
-
if (data.length !== width * height * 4) {
|
|
96
|
-
throw new Error(`Data length mismatch: expected ${width * height * 4}, got ${data.length}`);
|
|
97
|
-
}
|
|
98
|
-
// Create output buffer with header + pixel data
|
|
99
|
-
const output = new Uint8Array(this.HEADER_SIZE + data.length);
|
|
100
|
-
// Write magic bytes
|
|
101
|
-
output.set(this.MAGIC_BYTES, 0);
|
|
102
|
-
// Write width and height (big-endian)
|
|
103
|
-
this.writeUint32(output, 4, width);
|
|
104
|
-
this.writeUint32(output, 8, height);
|
|
105
|
-
// Write pixel data
|
|
106
|
-
output.set(data, this.HEADER_SIZE);
|
|
107
|
-
return Promise.resolve(output);
|
|
108
|
-
}
|
|
109
|
-
readUint32(data, offset) {
|
|
110
|
-
return ((data[offset] << 24) |
|
|
111
|
-
(data[offset + 1] << 16) |
|
|
112
|
-
(data[offset + 2] << 8) |
|
|
113
|
-
data[offset + 3]);
|
|
114
|
-
}
|
|
115
|
-
writeUint32(data, offset, value) {
|
|
116
|
-
data[offset] = (value >>> 24) & 0xff;
|
|
117
|
-
data[offset + 1] = (value >>> 16) & 0xff;
|
|
118
|
-
data[offset + 2] = (value >>> 8) & 0xff;
|
|
119
|
-
data[offset + 3] = value & 0xff;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
exports.RAWFormat = RAWFormat;
|