Fix operations

This commit is contained in:
C85297 2026-02-05 12:23:11 +00:00
parent b2a0e7960f
commit fd0d40c18e
No known key found for this signature in database
10 changed files with 68 additions and 26 deletions

View File

@ -17,6 +17,7 @@ import {
measureTextHeight, measureTextHeight,
loadFont, loadFont,
} from "jimp"; } from "jimp";
import log from "loglevel";
/** /**
* Add Text To Image operation * Add Text To Image operation
@ -211,6 +212,10 @@ class AddTextToImage extends Operation {
} }
}); });
let outlog = "";
outlog += `Adding text: "${text}" at (${xPos}, ${yPos}) with font: ${fontFace}, size: ${size}, colour: rgba(${red}, ${green}, ${blue}, ${alpha})\n`;
outlog += `Adding to image ${image.width}w, ${image.height}h\n`;
// Create a temporary image to hold the rendered text // Create a temporary image to hold the rendered text
const textImage = new Jimp({ const textImage = new Jimp({
width: measureText(jimpFont, text), width: measureText(jimpFont, text),
@ -222,17 +227,25 @@ class AddTextToImage extends Operation {
y: 0, y: 0,
text, text,
}); });
outlog += `Rendered text image size: ${textImage.width}w, ${textImage.height}h\n`;
// Scale the rendered text image to the correct size // Scale the rendered text image to the correct size
const scaleFactor = size / 72; const scaleFactor = size / 72;
if (size !== 1) { if (size !== 1) {
// Use bicubic for decreasing size // Use bicubic for decreasing size
if (size > 1) { if (size > 1) {
textImage.scale(scaleFactor, ResizeStrategy.BICUBIC); textImage.scale({
f: scaleFactor,
mode: ResizeStrategy.BICUBIC,
});
} else { } else {
textImage.scale(scaleFactor, ResizeStrategy.BILINEAR); textImage.scale({
f: scaleFactor,
mode: ResizeStrategy.BILINEAR,
});
} }
} }
outlog += `Scaled text image size: ${textImage.width}w, ${textImage.height}h\n`;
// If using the alignment options, calculate the pixel values AFTER the image has been scaled // If using the alignment options, calculate the pixel values AFTER the image has been scaled
switch (hAlign) { switch (hAlign) {
@ -246,6 +259,7 @@ class AddTextToImage extends Operation {
xPos = image.width - textImage.width; xPos = image.width - textImage.width;
break; break;
} }
outlog += `Calculated xPos: ${xPos}\n`;
switch (vAlign) { switch (vAlign) {
case "Top": case "Top":
@ -258,9 +272,15 @@ class AddTextToImage extends Operation {
yPos = image.height - textImage.height; yPos = image.height - textImage.height;
break; break;
} }
outlog += `Calculated yPos: ${yPos}\n`;
// throw new OperationError(outlog);
// Blit the rendered text image onto the original source image // Blit the rendered text image onto the original source image
image.blit(textImage, xPos, yPos); image.blit({
src: textImage,
x: xPos,
y: yPos,
});
let imageBuffer; let imageBuffer;
if (image.mime === "image/gif") { if (image.mime === "image/gif") {

View File

@ -118,16 +118,20 @@ class ContainImage extends Operation {
try { try {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Containing image..."); self.sendStatusMessage("Containing image...");
image.contain( image.contain({
width, width,
height, height,
alignMap[hAlign] | alignMap[vAlign], align: alignMap[hAlign] | alignMap[vAlign],
resizeMap[alg], mode: resizeMap[alg],
); });
if (opaqueBg) { if (opaqueBg) {
const newImage = await Jimp.read(width, height, 0x000000ff); const newImage = await Jimp.read(width, height, 0x000000ff);
newImage.blit(image, 0, 0); newImage.blit({
image,
x: 0,
y: 0,
});
image = newImage; image = newImage;
} }

View File

@ -113,12 +113,12 @@ class CoverImage extends Operation {
try { try {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Covering image..."); self.sendStatusMessage("Covering image...");
image.cover( image.cover({
width, width,
height, height,
alignMap[hAlign] | alignMap[vAlign], align: alignMap[hAlign] | alignMap[vAlign],
resizeMap[alg], mode: resizeMap[alg],
); });
let imageBuffer; let imageBuffer;
if (image.mime === "image/gif") { if (image.mime === "image/gif") {
imageBuffer = await image.getBuffer(JimpMime.png); imageBuffer = await image.getBuffer(JimpMime.png);

View File

@ -124,7 +124,7 @@ class CropImage extends Operation {
leaveBorder: autoBorder, leaveBorder: autoBorder,
}); });
} else { } else {
image.crop(xPos, yPos, width, height); image.crop({ xPos, yPos, width, height });
} }
let imageBuffer; let imageBuffer;

View File

@ -50,7 +50,7 @@ class DitherImage extends Operation {
try { try {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Applying dither to image..."); self.sendStatusMessage("Applying dither to image...");
image.dither565(); image.dither();
let imageBuffer; let imageBuffer;
if (image.mime === "image/gif") { if (image.mime === "image/gif") {

View File

@ -59,10 +59,16 @@ class FlipImage extends Operation {
self.sendStatusMessage("Flipping image..."); self.sendStatusMessage("Flipping image...");
switch (flipAxis) { switch (flipAxis) {
case "Horizontal": case "Horizontal":
image.flip(true, false); image.flip({
horizontal: true,
vertical: false,
});
break; break;
case "Vertical": case "Vertical":
image.flip(false, true); image.flip({
horizontal: false,
vertical: true,
});
break; break;
} }

View File

@ -155,11 +155,11 @@ class GenerateImage extends Operation {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Scaling image..."); self.sendStatusMessage("Scaling image...");
image.scaleToFit( image.scaleToFit({
width * scale, w: width * scale,
height * scale, h: height * scale,
ResizeStrategy.NEAREST_NEIGHBOR, mode: ResizeStrategy.NEAREST_NEIGHBOR,
); });
} }
try { try {

View File

@ -76,7 +76,7 @@ class ImageHueSaturationLightness extends Operation {
if (hue !== 0) { if (hue !== 0) {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Changing image hue..."); self.sendStatusMessage("Changing image hue...");
image.colour([ image.color([
{ {
apply: "hue", apply: "hue",
params: [hue], params: [hue],
@ -86,7 +86,7 @@ class ImageHueSaturationLightness extends Operation {
if (saturation !== 0) { if (saturation !== 0) {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Changing image saturation..."); self.sendStatusMessage("Changing image saturation...");
image.colour([ image.color([
{ {
apply: "saturate", apply: "saturate",
params: [saturation], params: [saturation],
@ -96,7 +96,7 @@ class ImageHueSaturationLightness extends Operation {
if (lightness !== 0) { if (lightness !== 0) {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Changing image lightness..."); self.sendStatusMessage("Changing image lightness...");
image.colour([ image.color([
{ {
apply: "lighten", apply: "lighten",
params: [lightness], params: [lightness],

View File

@ -106,9 +106,17 @@ class ResizeImage extends Operation {
if (isWorkerEnvironment()) if (isWorkerEnvironment())
self.sendStatusMessage("Resizing image..."); self.sendStatusMessage("Resizing image...");
if (aspect) { if (aspect) {
image.scaleToFit(width, height, resizeMap[resizeAlg]); image.scaleToFit({
w: width,
h: height,
mode: resizeMap[resizeAlg],
});
} else { } else {
image.resize(width, height, resizeMap[resizeAlg]); image.resize({
w: width,
h: height,
mode: resizeMap[resizeAlg],
});
} }
let imageBuffer; let imageBuffer;

View File

@ -35,6 +35,10 @@ module.exports = {
testOp(browser, "AND", "test input", "4$04 $044", [{ "option": "Hex", "string": "34" }]); testOp(browser, "AND", "test input", "4$04 $044", [{ "option": "Hex", "string": "34" }]);
testOp(browser, "Add line numbers", "test input", "1 test input"); testOp(browser, "Add line numbers", "test input", "1 test input");
testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16], []]); testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16], []]);
testOp(browser, ["From Hex", "Dither Image", "SHA2"], Images.PNG_HEX, "cbf587a78915cfb14546ba83080b13e5054800802488dd0cb786b8951e7dc0b48f055260917bd0ccfc075e422b9d6aff112948562653995d74e70f0b66367ac3", [[], [], []]);
testOp(browser, ["From Hex", "Generate Image", "SHA2"], Images.PNG_HEX, "2c451762a6c9192fd31dc80765eab3f447be70ea51f6fdb6911ade4d89d4a98bd0a1ff00b08d76aac472faeceb54b66092e3f3be7bbf899bf3e55ca9c96a56aa", [[], [], []]);
testOp(browser, ["From Hex", "Image Hue/Saturation/Lightness", "SHA2"], Images.PNG_HEX, "522dfc0bbef00e05c5d6861a002039fa2952e4bbb7fe8d21d0d538ef6f9d65da82065929b4150dc5b8b49460ee6c9bef7f660b86f8d4e7442a07c61c0a152a4b", [[], [], []]);
testOp(browser, ["From Hex", "Resize Image", "SHA2"], Images.PNG_HEX, "654bfbf0a0537c901459c4bc22c5fb0bacbf01af775a0733e3a1c46cda5b699bcc4ed85322d813c7bb9b245d62d64425c0766fe03d3d20bc63634e2a4df17626", [[], [64, 64], []]);
testOp(browser, "Adler-32 Checksum", "test input", "16160411"); testOp(browser, "Adler-32 Checksum", "test input", "16160411");
testOp(browser, "Affine Cipher Decode", "test input", "rcqr glnsr", [1, 2]); testOp(browser, "Affine Cipher Decode", "test input", "rcqr glnsr", [1, 2]);
testOp(browser, "Affine Cipher Encode", "test input", "gndg zoujg", [3, 1]); testOp(browser, "Affine Cipher Encode", "test input", "gndg zoujg", [3, 1]);