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.
- package/README.md +160 -32
- package/esm/mod.d.ts +2 -1
- package/esm/mod.js +2 -1
- package/esm/src/formats/jpeg.d.ts +12 -1
- package/esm/src/formats/jpeg.js +633 -4
- package/esm/src/formats/png_base.d.ts +8 -0
- package/esm/src/formats/png_base.js +176 -3
- package/esm/src/formats/ppm.d.ts +50 -0
- package/esm/src/formats/ppm.js +242 -0
- package/esm/src/formats/tiff.d.ts +10 -1
- package/esm/src/formats/tiff.js +194 -44
- package/esm/src/formats/webp.d.ts +9 -2
- package/esm/src/formats/webp.js +211 -62
- package/esm/src/image.d.ts +81 -0
- package/esm/src/image.js +282 -5
- package/esm/src/types.d.ts +41 -1
- package/esm/src/utils/image_processing.d.ts +98 -0
- package/esm/src/utils/image_processing.js +440 -0
- package/esm/src/utils/metadata/xmp.d.ts +52 -0
- package/esm/src/utils/metadata/xmp.js +325 -0
- package/esm/src/utils/resize.d.ts +4 -0
- package/esm/src/utils/resize.js +74 -0
- package/package.json +1 -1
- package/script/mod.d.ts +2 -1
- package/script/mod.js +4 -2
- package/script/src/formats/jpeg.d.ts +12 -1
- package/script/src/formats/jpeg.js +633 -4
- package/script/src/formats/png_base.d.ts +8 -0
- package/script/src/formats/png_base.js +176 -3
- package/script/src/formats/ppm.d.ts +50 -0
- package/script/src/formats/ppm.js +246 -0
- package/script/src/formats/tiff.d.ts +10 -1
- package/script/src/formats/tiff.js +194 -44
- package/script/src/formats/webp.d.ts +9 -2
- package/script/src/formats/webp.js +211 -62
- package/script/src/image.d.ts +81 -0
- package/script/src/image.js +280 -3
- package/script/src/types.d.ts +41 -1
- package/script/src/utils/image_processing.d.ts +98 -0
- package/script/src/utils/image_processing.js +451 -0
- package/script/src/utils/metadata/xmp.d.ts +52 -0
- package/script/src/utils/metadata/xmp.js +333 -0
- package/script/src/utils/resize.d.ts +4 -0
- package/script/src/utils/resize.js +75 -0
|
@@ -9,10 +9,21 @@ exports.adjustBrightness = adjustBrightness;
|
|
|
9
9
|
exports.adjustContrast = adjustContrast;
|
|
10
10
|
exports.adjustExposure = adjustExposure;
|
|
11
11
|
exports.adjustSaturation = adjustSaturation;
|
|
12
|
+
exports.adjustHue = adjustHue;
|
|
12
13
|
exports.invert = invert;
|
|
13
14
|
exports.grayscale = grayscale;
|
|
14
15
|
exports.fillRect = fillRect;
|
|
15
16
|
exports.crop = crop;
|
|
17
|
+
exports.boxBlur = boxBlur;
|
|
18
|
+
exports.gaussianBlur = gaussianBlur;
|
|
19
|
+
exports.sharpen = sharpen;
|
|
20
|
+
exports.sepia = sepia;
|
|
21
|
+
exports.medianFilter = medianFilter;
|
|
22
|
+
exports.rotate90 = rotate90;
|
|
23
|
+
exports.rotate180 = rotate180;
|
|
24
|
+
exports.rotate270 = rotate270;
|
|
25
|
+
exports.flipHorizontal = flipHorizontal;
|
|
26
|
+
exports.flipVertical = flipVertical;
|
|
16
27
|
/**
|
|
17
28
|
* Composite one image on top of another at a specified position
|
|
18
29
|
* @param base Base image data (RGBA)
|
|
@@ -142,6 +153,99 @@ function adjustSaturation(data, amount) {
|
|
|
142
153
|
}
|
|
143
154
|
return result;
|
|
144
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Convert RGB to HSL color space
|
|
158
|
+
* @param r Red component (0-255)
|
|
159
|
+
* @param g Green component (0-255)
|
|
160
|
+
* @param b Blue component (0-255)
|
|
161
|
+
* @returns HSL values: [h (0-360), s (0-1), l (0-1)]
|
|
162
|
+
*/
|
|
163
|
+
function rgbToHsl(r, g, b) {
|
|
164
|
+
r /= 255;
|
|
165
|
+
g /= 255;
|
|
166
|
+
b /= 255;
|
|
167
|
+
const max = Math.max(r, g, b);
|
|
168
|
+
const min = Math.min(r, g, b);
|
|
169
|
+
const delta = max - min;
|
|
170
|
+
let h = 0;
|
|
171
|
+
let s = 0;
|
|
172
|
+
const l = (max + min) / 2;
|
|
173
|
+
if (delta !== 0) {
|
|
174
|
+
s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
|
|
175
|
+
if (max === r) {
|
|
176
|
+
h = ((g - b) / delta + (g < b ? 6 : 0)) / 6;
|
|
177
|
+
}
|
|
178
|
+
else if (max === g) {
|
|
179
|
+
h = ((b - r) / delta + 2) / 6;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
h = ((r - g) / delta + 4) / 6;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return [h * 360, s, l];
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Convert HSL to RGB color space
|
|
189
|
+
* @param h Hue (0-360)
|
|
190
|
+
* @param s Saturation (0-1)
|
|
191
|
+
* @param l Lightness (0-1)
|
|
192
|
+
* @returns RGB values: [r (0-255), g (0-255), b (0-255)]
|
|
193
|
+
*/
|
|
194
|
+
function hslToRgb(h, s, l) {
|
|
195
|
+
h = h / 360;
|
|
196
|
+
let r, g, b;
|
|
197
|
+
if (s === 0) {
|
|
198
|
+
r = g = b = l; // Achromatic
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const hue2rgb = (p, q, t) => {
|
|
202
|
+
if (t < 0)
|
|
203
|
+
t += 1;
|
|
204
|
+
if (t > 1)
|
|
205
|
+
t -= 1;
|
|
206
|
+
if (t < 1 / 6)
|
|
207
|
+
return p + (q - p) * 6 * t;
|
|
208
|
+
if (t < 1 / 2)
|
|
209
|
+
return q;
|
|
210
|
+
if (t < 2 / 3)
|
|
211
|
+
return p + (q - p) * (2 / 3 - t) * 6;
|
|
212
|
+
return p;
|
|
213
|
+
};
|
|
214
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
215
|
+
const p = 2 * l - q;
|
|
216
|
+
r = hue2rgb(p, q, h + 1 / 3);
|
|
217
|
+
g = hue2rgb(p, q, h);
|
|
218
|
+
b = hue2rgb(p, q, h - 1 / 3);
|
|
219
|
+
}
|
|
220
|
+
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Adjust hue of an image by rotating the hue wheel
|
|
224
|
+
* @param data Image data (RGBA)
|
|
225
|
+
* @param degrees Hue rotation in degrees (any value accepted, wraps at 360)
|
|
226
|
+
* @returns New image data with adjusted hue
|
|
227
|
+
*/
|
|
228
|
+
function adjustHue(data, degrees) {
|
|
229
|
+
const result = new Uint8Array(data.length);
|
|
230
|
+
// Normalize rotation to -180 to 180 range
|
|
231
|
+
const rotation = ((degrees % 360) + 360) % 360;
|
|
232
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
233
|
+
const r = data[i];
|
|
234
|
+
const g = data[i + 1];
|
|
235
|
+
const b = data[i + 2];
|
|
236
|
+
// Convert to HSL
|
|
237
|
+
const [h, s, l] = rgbToHsl(r, g, b);
|
|
238
|
+
// Rotate hue
|
|
239
|
+
const newH = (h + rotation) % 360;
|
|
240
|
+
// Convert back to RGB
|
|
241
|
+
const [newR, newG, newB] = hslToRgb(newH, s, l);
|
|
242
|
+
result[i] = newR;
|
|
243
|
+
result[i + 1] = newG;
|
|
244
|
+
result[i + 2] = newB;
|
|
245
|
+
result[i + 3] = data[i + 3]; // Preserve alpha
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
145
249
|
/**
|
|
146
250
|
* Invert colors of an image
|
|
147
251
|
* @param data Image data (RGBA)
|
|
@@ -240,3 +344,350 @@ function crop(data, width, height, x, y, cropWidth, cropHeight) {
|
|
|
240
344
|
}
|
|
241
345
|
return { data: result, width: actualWidth, height: actualHeight };
|
|
242
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* Apply a box blur filter to an image
|
|
349
|
+
* @param data Image data (RGBA)
|
|
350
|
+
* @param width Image width
|
|
351
|
+
* @param height Image height
|
|
352
|
+
* @param radius Blur radius (default: 1)
|
|
353
|
+
* @returns New image data with box blur applied
|
|
354
|
+
*/
|
|
355
|
+
function boxBlur(data, width, height, radius = 1) {
|
|
356
|
+
const result = new Uint8Array(data.length);
|
|
357
|
+
const clampedRadius = Math.max(1, Math.floor(radius));
|
|
358
|
+
for (let y = 0; y < height; y++) {
|
|
359
|
+
for (let x = 0; x < width; x++) {
|
|
360
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
361
|
+
let count = 0;
|
|
362
|
+
// Iterate over kernel
|
|
363
|
+
for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
|
|
364
|
+
for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
|
|
365
|
+
const px = x + kx;
|
|
366
|
+
const py = y + ky;
|
|
367
|
+
// Check bounds
|
|
368
|
+
if (px >= 0 && px < width && py >= 0 && py < height) {
|
|
369
|
+
const idx = (py * width + px) * 4;
|
|
370
|
+
r += data[idx];
|
|
371
|
+
g += data[idx + 1];
|
|
372
|
+
b += data[idx + 2];
|
|
373
|
+
a += data[idx + 3];
|
|
374
|
+
count++;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const outIdx = (y * width + x) * 4;
|
|
379
|
+
result[outIdx] = Math.round(r / count);
|
|
380
|
+
result[outIdx + 1] = Math.round(g / count);
|
|
381
|
+
result[outIdx + 2] = Math.round(b / count);
|
|
382
|
+
result[outIdx + 3] = Math.round(a / count);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Generate a Gaussian kernel for blur
|
|
389
|
+
* @param radius Kernel radius
|
|
390
|
+
* @param sigma Standard deviation (if not provided, calculated from radius)
|
|
391
|
+
* @returns Gaussian kernel as 1D array
|
|
392
|
+
*/
|
|
393
|
+
function generateGaussianKernel(radius, sigma) {
|
|
394
|
+
const size = radius * 2 + 1;
|
|
395
|
+
const kernel = new Array(size);
|
|
396
|
+
const s = sigma ?? radius / 3;
|
|
397
|
+
const s2 = 2 * s * s;
|
|
398
|
+
let sum = 0;
|
|
399
|
+
for (let i = 0; i < size; i++) {
|
|
400
|
+
const x = i - radius;
|
|
401
|
+
kernel[i] = Math.exp(-(x * x) / s2);
|
|
402
|
+
sum += kernel[i];
|
|
403
|
+
}
|
|
404
|
+
// Normalize
|
|
405
|
+
for (let i = 0; i < size; i++) {
|
|
406
|
+
kernel[i] /= sum;
|
|
407
|
+
}
|
|
408
|
+
return kernel;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Apply Gaussian blur to an image
|
|
412
|
+
* @param data Image data (RGBA)
|
|
413
|
+
* @param width Image width
|
|
414
|
+
* @param height Image height
|
|
415
|
+
* @param radius Blur radius (default: 1)
|
|
416
|
+
* @param sigma Optional standard deviation (if not provided, calculated from radius)
|
|
417
|
+
* @returns New image data with Gaussian blur applied
|
|
418
|
+
*/
|
|
419
|
+
function gaussianBlur(data, width, height, radius = 1, sigma) {
|
|
420
|
+
const clampedRadius = Math.max(1, Math.floor(radius));
|
|
421
|
+
const kernel = generateGaussianKernel(clampedRadius, sigma);
|
|
422
|
+
// Apply horizontal pass
|
|
423
|
+
const temp = new Uint8Array(data.length);
|
|
424
|
+
for (let y = 0; y < height; y++) {
|
|
425
|
+
for (let x = 0; x < width; x++) {
|
|
426
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
427
|
+
for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
|
|
428
|
+
const px = Math.max(0, Math.min(width - 1, x + kx));
|
|
429
|
+
const idx = (y * width + px) * 4;
|
|
430
|
+
const weight = kernel[kx + clampedRadius];
|
|
431
|
+
r += data[idx] * weight;
|
|
432
|
+
g += data[idx + 1] * weight;
|
|
433
|
+
b += data[idx + 2] * weight;
|
|
434
|
+
a += data[idx + 3] * weight;
|
|
435
|
+
}
|
|
436
|
+
const outIdx = (y * width + x) * 4;
|
|
437
|
+
temp[outIdx] = Math.round(r);
|
|
438
|
+
temp[outIdx + 1] = Math.round(g);
|
|
439
|
+
temp[outIdx + 2] = Math.round(b);
|
|
440
|
+
temp[outIdx + 3] = Math.round(a);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
// Apply vertical pass
|
|
444
|
+
const result = new Uint8Array(data.length);
|
|
445
|
+
for (let y = 0; y < height; y++) {
|
|
446
|
+
for (let x = 0; x < width; x++) {
|
|
447
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
448
|
+
for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
|
|
449
|
+
const py = Math.max(0, Math.min(height - 1, y + ky));
|
|
450
|
+
const idx = (py * width + x) * 4;
|
|
451
|
+
const weight = kernel[ky + clampedRadius];
|
|
452
|
+
r += temp[idx] * weight;
|
|
453
|
+
g += temp[idx + 1] * weight;
|
|
454
|
+
b += temp[idx + 2] * weight;
|
|
455
|
+
a += temp[idx + 3] * weight;
|
|
456
|
+
}
|
|
457
|
+
const outIdx = (y * width + x) * 4;
|
|
458
|
+
result[outIdx] = Math.round(r);
|
|
459
|
+
result[outIdx + 1] = Math.round(g);
|
|
460
|
+
result[outIdx + 2] = Math.round(b);
|
|
461
|
+
result[outIdx + 3] = Math.round(a);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Apply sharpen filter to an image
|
|
468
|
+
* @param data Image data (RGBA)
|
|
469
|
+
* @param width Image width
|
|
470
|
+
* @param height Image height
|
|
471
|
+
* @param amount Sharpening amount (0-1, default: 0.5)
|
|
472
|
+
* @returns New image data with sharpening applied
|
|
473
|
+
*/
|
|
474
|
+
function sharpen(data, width, height, amount = 0.5) {
|
|
475
|
+
const result = new Uint8Array(data.length);
|
|
476
|
+
const clampedAmount = Math.max(0, Math.min(1, amount));
|
|
477
|
+
// Sharpen kernel (Laplacian-based)
|
|
478
|
+
// Center weight is 1 + 4*amount, neighbors are -amount
|
|
479
|
+
const center = 1 + 4 * clampedAmount;
|
|
480
|
+
const neighbor = -clampedAmount;
|
|
481
|
+
for (let y = 0; y < height; y++) {
|
|
482
|
+
for (let x = 0; x < width; x++) {
|
|
483
|
+
const idx = (y * width + x) * 4;
|
|
484
|
+
let r = data[idx] * center;
|
|
485
|
+
let g = data[idx + 1] * center;
|
|
486
|
+
let b = data[idx + 2] * center;
|
|
487
|
+
// Apply kernel to neighbors (4-connected)
|
|
488
|
+
const neighbors = [
|
|
489
|
+
{ dx: 0, dy: -1 }, // top
|
|
490
|
+
{ dx: -1, dy: 0 }, // left
|
|
491
|
+
{ dx: 1, dy: 0 }, // right
|
|
492
|
+
{ dx: 0, dy: 1 }, // bottom
|
|
493
|
+
];
|
|
494
|
+
for (const { dx, dy } of neighbors) {
|
|
495
|
+
const nx = x + dx;
|
|
496
|
+
const ny = y + dy;
|
|
497
|
+
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
|
|
498
|
+
const nIdx = (ny * width + nx) * 4;
|
|
499
|
+
r += data[nIdx] * neighbor;
|
|
500
|
+
g += data[nIdx + 1] * neighbor;
|
|
501
|
+
b += data[nIdx + 2] * neighbor;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
result[idx] = Math.max(0, Math.min(255, Math.round(r)));
|
|
505
|
+
result[idx + 1] = Math.max(0, Math.min(255, Math.round(g)));
|
|
506
|
+
result[idx + 2] = Math.max(0, Math.min(255, Math.round(b)));
|
|
507
|
+
result[idx + 3] = data[idx + 3]; // Alpha unchanged
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return result;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Apply sepia tone effect to an image
|
|
514
|
+
* @param data Image data (RGBA)
|
|
515
|
+
* @returns New image data with sepia tone applied
|
|
516
|
+
*/
|
|
517
|
+
function sepia(data) {
|
|
518
|
+
const result = new Uint8Array(data.length);
|
|
519
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
520
|
+
const r = data[i];
|
|
521
|
+
const g = data[i + 1];
|
|
522
|
+
const b = data[i + 2];
|
|
523
|
+
// Sepia transformation matrix
|
|
524
|
+
result[i] = Math.min(255, Math.round(r * 0.393 + g * 0.769 + b * 0.189));
|
|
525
|
+
result[i + 1] = Math.min(255, Math.round(r * 0.349 + g * 0.686 + b * 0.168));
|
|
526
|
+
result[i + 2] = Math.min(255, Math.round(r * 0.272 + g * 0.534 + b * 0.131));
|
|
527
|
+
result[i + 3] = data[i + 3]; // Alpha unchanged
|
|
528
|
+
}
|
|
529
|
+
return result;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Apply median filter to reduce noise
|
|
533
|
+
* @param data Image data (RGBA)
|
|
534
|
+
* @param width Image width
|
|
535
|
+
* @param height Image height
|
|
536
|
+
* @param radius Filter radius (default: 1)
|
|
537
|
+
* @returns New image data with median filter applied
|
|
538
|
+
*/
|
|
539
|
+
function medianFilter(data, width, height, radius = 1) {
|
|
540
|
+
const result = new Uint8Array(data.length);
|
|
541
|
+
const clampedRadius = Math.max(1, Math.floor(radius));
|
|
542
|
+
for (let y = 0; y < height; y++) {
|
|
543
|
+
for (let x = 0; x < width; x++) {
|
|
544
|
+
const rValues = [];
|
|
545
|
+
const gValues = [];
|
|
546
|
+
const bValues = [];
|
|
547
|
+
const aValues = [];
|
|
548
|
+
// Collect values in kernel window
|
|
549
|
+
for (let ky = -clampedRadius; ky <= clampedRadius; ky++) {
|
|
550
|
+
for (let kx = -clampedRadius; kx <= clampedRadius; kx++) {
|
|
551
|
+
const px = x + kx;
|
|
552
|
+
const py = y + ky;
|
|
553
|
+
if (px >= 0 && px < width && py >= 0 && py < height) {
|
|
554
|
+
const idx = (py * width + px) * 4;
|
|
555
|
+
rValues.push(data[idx]);
|
|
556
|
+
gValues.push(data[idx + 1]);
|
|
557
|
+
bValues.push(data[idx + 2]);
|
|
558
|
+
aValues.push(data[idx + 3]);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
// Sort and get median
|
|
563
|
+
rValues.sort((a, b) => a - b);
|
|
564
|
+
gValues.sort((a, b) => a - b);
|
|
565
|
+
bValues.sort((a, b) => a - b);
|
|
566
|
+
aValues.sort((a, b) => a - b);
|
|
567
|
+
const mid = Math.floor(rValues.length / 2);
|
|
568
|
+
const outIdx = (y * width + x) * 4;
|
|
569
|
+
result[outIdx] = rValues[mid];
|
|
570
|
+
result[outIdx + 1] = gValues[mid];
|
|
571
|
+
result[outIdx + 2] = bValues[mid];
|
|
572
|
+
result[outIdx + 3] = aValues[mid];
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return result;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Rotate image 90 degrees clockwise
|
|
579
|
+
* @param data Image data (RGBA)
|
|
580
|
+
* @param width Image width
|
|
581
|
+
* @param height Image height
|
|
582
|
+
* @returns Rotated image data with swapped dimensions
|
|
583
|
+
*/
|
|
584
|
+
function rotate90(data, width, height) {
|
|
585
|
+
const result = new Uint8Array(data.length);
|
|
586
|
+
const newWidth = height;
|
|
587
|
+
const newHeight = width;
|
|
588
|
+
for (let y = 0; y < height; y++) {
|
|
589
|
+
for (let x = 0; x < width; x++) {
|
|
590
|
+
const srcIdx = (y * width + x) * 4;
|
|
591
|
+
const dstX = height - 1 - y;
|
|
592
|
+
const dstY = x;
|
|
593
|
+
const dstIdx = (dstY * newWidth + dstX) * 4;
|
|
594
|
+
result[dstIdx] = data[srcIdx];
|
|
595
|
+
result[dstIdx + 1] = data[srcIdx + 1];
|
|
596
|
+
result[dstIdx + 2] = data[srcIdx + 2];
|
|
597
|
+
result[dstIdx + 3] = data[srcIdx + 3];
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return { data: result, width: newWidth, height: newHeight };
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Rotate image 180 degrees
|
|
604
|
+
* @param data Image data (RGBA)
|
|
605
|
+
* @param width Image width
|
|
606
|
+
* @param height Image height
|
|
607
|
+
* @returns Rotated image data with same dimensions
|
|
608
|
+
*/
|
|
609
|
+
function rotate180(data, width, height) {
|
|
610
|
+
const result = new Uint8Array(data.length);
|
|
611
|
+
for (let y = 0; y < height; y++) {
|
|
612
|
+
for (let x = 0; x < width; x++) {
|
|
613
|
+
const srcIdx = (y * width + x) * 4;
|
|
614
|
+
const dstX = width - 1 - x;
|
|
615
|
+
const dstY = height - 1 - y;
|
|
616
|
+
const dstIdx = (dstY * width + dstX) * 4;
|
|
617
|
+
result[dstIdx] = data[srcIdx];
|
|
618
|
+
result[dstIdx + 1] = data[srcIdx + 1];
|
|
619
|
+
result[dstIdx + 2] = data[srcIdx + 2];
|
|
620
|
+
result[dstIdx + 3] = data[srcIdx + 3];
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return result;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Rotate image 270 degrees clockwise (or 90 degrees counter-clockwise)
|
|
627
|
+
* @param data Image data (RGBA)
|
|
628
|
+
* @param width Image width
|
|
629
|
+
* @param height Image height
|
|
630
|
+
* @returns Rotated image data with swapped dimensions
|
|
631
|
+
*/
|
|
632
|
+
function rotate270(data, width, height) {
|
|
633
|
+
const result = new Uint8Array(data.length);
|
|
634
|
+
const newWidth = height;
|
|
635
|
+
const newHeight = width;
|
|
636
|
+
for (let y = 0; y < height; y++) {
|
|
637
|
+
for (let x = 0; x < width; x++) {
|
|
638
|
+
const srcIdx = (y * width + x) * 4;
|
|
639
|
+
const dstX = y;
|
|
640
|
+
const dstY = width - 1 - x;
|
|
641
|
+
const dstIdx = (dstY * newWidth + dstX) * 4;
|
|
642
|
+
result[dstIdx] = data[srcIdx];
|
|
643
|
+
result[dstIdx + 1] = data[srcIdx + 1];
|
|
644
|
+
result[dstIdx + 2] = data[srcIdx + 2];
|
|
645
|
+
result[dstIdx + 3] = data[srcIdx + 3];
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return { data: result, width: newWidth, height: newHeight };
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Flip image horizontally (mirror)
|
|
652
|
+
* @param data Image data (RGBA)
|
|
653
|
+
* @param width Image width
|
|
654
|
+
* @param height Image height
|
|
655
|
+
* @returns Flipped image data
|
|
656
|
+
*/
|
|
657
|
+
function flipHorizontal(data, width, height) {
|
|
658
|
+
const result = new Uint8Array(data.length);
|
|
659
|
+
for (let y = 0; y < height; y++) {
|
|
660
|
+
for (let x = 0; x < width; x++) {
|
|
661
|
+
const srcIdx = (y * width + x) * 4;
|
|
662
|
+
const dstX = width - 1 - x;
|
|
663
|
+
const dstIdx = (y * width + dstX) * 4;
|
|
664
|
+
result[dstIdx] = data[srcIdx];
|
|
665
|
+
result[dstIdx + 1] = data[srcIdx + 1];
|
|
666
|
+
result[dstIdx + 2] = data[srcIdx + 2];
|
|
667
|
+
result[dstIdx + 3] = data[srcIdx + 3];
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return result;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Flip image vertically
|
|
674
|
+
* @param data Image data (RGBA)
|
|
675
|
+
* @param width Image width
|
|
676
|
+
* @param height Image height
|
|
677
|
+
* @returns Flipped image data
|
|
678
|
+
*/
|
|
679
|
+
function flipVertical(data, width, height) {
|
|
680
|
+
const result = new Uint8Array(data.length);
|
|
681
|
+
for (let y = 0; y < height; y++) {
|
|
682
|
+
for (let x = 0; x < width; x++) {
|
|
683
|
+
const srcIdx = (y * width + x) * 4;
|
|
684
|
+
const dstY = height - 1 - y;
|
|
685
|
+
const dstIdx = (dstY * width + x) * 4;
|
|
686
|
+
result[dstIdx] = data[srcIdx];
|
|
687
|
+
result[dstIdx + 1] = data[srcIdx + 1];
|
|
688
|
+
result[dstIdx + 2] = data[srcIdx + 2];
|
|
689
|
+
result[dstIdx + 3] = data[srcIdx + 3];
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return result;
|
|
693
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XMP (Extensible Metadata Platform) parsing and writing utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities for reading and writing XMP metadata in image files.
|
|
5
|
+
* It supports Dublin Core, EXIF, and Photoshop namespaces.
|
|
6
|
+
*/
|
|
7
|
+
import type { ImageMetadata } from "../../types.js";
|
|
8
|
+
/**
|
|
9
|
+
* XMP namespace URIs
|
|
10
|
+
*/
|
|
11
|
+
export declare const XMP_NAMESPACES: {
|
|
12
|
+
readonly RDF: "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
13
|
+
readonly DC: "http://purl.org/dc/elements/1.1/";
|
|
14
|
+
readonly XMP: "http://ns.adobe.com/xap/1.0/";
|
|
15
|
+
readonly EXIF: "http://ns.adobe.com/exif/1.0/";
|
|
16
|
+
readonly TIFF: "http://ns.adobe.com/tiff/1.0/";
|
|
17
|
+
readonly PHOTOSHOP: "http://ns.adobe.com/photoshop/1.0/";
|
|
18
|
+
readonly XMP_RIGHTS: "http://ns.adobe.com/xap/1.0/rights/";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* XMP Dublin Core field mapping to ImageMetadata
|
|
22
|
+
*/
|
|
23
|
+
export interface XMPFieldMapping {
|
|
24
|
+
xmpPath: string;
|
|
25
|
+
metadataKey: keyof ImageMetadata;
|
|
26
|
+
namespace: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Supported XMP fields and their mappings
|
|
30
|
+
*/
|
|
31
|
+
export declare const XMP_FIELD_MAPPINGS: XMPFieldMapping[];
|
|
32
|
+
/**
|
|
33
|
+
* Escape XML special characters
|
|
34
|
+
*/
|
|
35
|
+
export declare function escapeXML(str: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* Unescape XML special characters
|
|
38
|
+
*/
|
|
39
|
+
export declare function unescapeXML(str: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Parse XMP metadata from XML string
|
|
42
|
+
*/
|
|
43
|
+
export declare function parseXMP(xmpStr: string): Partial<ImageMetadata>;
|
|
44
|
+
/**
|
|
45
|
+
* Create XMP packet from metadata
|
|
46
|
+
*/
|
|
47
|
+
export declare function createXMP(metadata: Partial<ImageMetadata>): string;
|
|
48
|
+
/**
|
|
49
|
+
* Get list of supported XMP metadata fields
|
|
50
|
+
*/
|
|
51
|
+
export declare function getSupportedXMPFields(): Array<keyof ImageMetadata>;
|
|
52
|
+
//# sourceMappingURL=xmp.d.ts.map
|