Fix bug where GIF input would error on output.

Also minor corrections to jsdocs in ImageManipulation
This commit is contained in:
j433866 2019-03-20 11:20:34 +00:00
parent b312e17904
commit 6a01e40394
17 changed files with 118 additions and 26 deletions

View file

@ -12,7 +12,7 @@ import OperationError from "../errors/OperationError";
* Gaussian blurs an image.
*
* @param {jimp} input
* @param {int} radius
* @param {number} radius
* @param {boolean} fast
* @returns {jimp}
*/
@ -24,7 +24,6 @@ export function gaussianBlur (input, radius) {
input = boxBlur(input, (boxes[i] - 1) / 2);
}
} catch (err) {
log.error(err);
throw new OperationError(`Error blurring image. (${err})`);
}
@ -33,8 +32,8 @@ export function gaussianBlur (input, radius) {
/**
*
* @param {int} radius
* @param {int} numBoxes
* @param {number} radius
* @param {number} numBoxes
* @returns {Array}
*/
function boxesForGauss(radius, numBoxes) {
@ -167,9 +166,9 @@ function boxBlurH (source, output, width, height, radius) {
*
* @param {jimp} source
* @param {jimp} output
* @param {int} width
* @param {int} height
* @param {int} radius
* @param {number} width
* @param {number} height
* @param {number} radius
* @returns {jimp}
*/
function boxBlurV (source, output, width, height, radius) {

View file

@ -228,7 +228,12 @@ class AddTextToImage extends Operation {
}
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adding text to image. (${err})`);

View file

@ -76,7 +76,12 @@ class BlurImage extends Operation {
break;
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error blurring image. (${err})`);

View file

@ -127,7 +127,12 @@ class ContainImage extends Operation {
image = newImage;
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error containing image. (${err})`);

View file

@ -115,7 +115,12 @@ class CoverImage extends Operation {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Covering image...");
image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error covering image. (${err})`);

View file

@ -116,7 +116,12 @@ class CropImage extends Operation {
image.crop(xPos, yPos, width, height);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error cropping image. (${err})`);

View file

@ -51,7 +51,13 @@ class DitherImage extends Operation {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Applying dither to image...");
image.dither565();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error applying dither to image. (${err})`);

View file

@ -66,7 +66,12 @@ class FlipImage extends Operation {
break;
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error flipping image. (${err})`);

View file

@ -75,7 +75,12 @@ class ImageBrightnessContrast extends Operation {
image.contrast(contrast / 100);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image brightness or contrast. (${err})`);

View file

@ -66,7 +66,12 @@ class ImageFilter extends Operation {
image.sepia();
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error applying filter to image. (${err})`);

View file

@ -102,7 +102,13 @@ class ImageHueSaturationLightness extends Operation {
}
]);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`);

View file

@ -61,7 +61,12 @@ class ImageOpacity extends Operation {
self.sendStatusMessage("Changing image opacity...");
image.opacity(opacity / 100);
const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error changing image opacity. (${err})`);

View file

@ -51,7 +51,13 @@ class InvertImage extends Operation {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Inverting image...");
image.invert();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error inverting image. (${err})`);

View file

@ -41,12 +41,26 @@ class NormaliseImage extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error opening image file. (${err})`);
}
image.normalize();
try {
image.normalize();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error normalising image. (${err})`);
}
}
/**

View file

@ -110,7 +110,12 @@ class ResizeImage extends Operation {
image.resize(width, height, resizeMap[resizeAlg]);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error resizing image. (${err})`);

View file

@ -59,7 +59,13 @@ class RotateImage extends Operation {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Rotating image...");
image.rotate(degrees);
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error rotating image. (${err})`);

View file

@ -134,7 +134,12 @@ class SharpenImage extends Operation {
}
});
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
let imageBuffer;
if (image.getMIME() === "image/gif") {
imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
} else {
imageBuffer = await image.getBufferAsync(jimp.AUTO);
}
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error sharpening image. (${err})`);