Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async (input: RendererInput): Promise => {
// generate thumbnail
Logger.silly('[JimpThRenderer] rendering thumbnail:' + input.mediaPath);
const image = await Jimp.read(input.mediaPath);
/**
* newWidth * newHeight = size*size
* newHeight/newWidth = height/width
*
* newHeight = (height/width)*newWidth
* newWidth * newWidth = (size*size) / (height/width)
*
* @type {number}
*/
const ratio = image.bitmap.height / image.bitmap.width;
const algo = input.qualityPriority === true ? Jimp.RESIZE_BEZIER : Jimp.RESIZE_NEAREST_NEIGHBOR;
if (input.cut) {
image.crop(
input.cut.left,
input.cut.top,
input.cut.width,
input.cut.height
);
}
if (input.makeSquare === false) {
if (image.bitmap.width < image.bitmap.height) {
image.resize(Math.min(input.size, image.bitmap.width), Jimp.AUTO, algo);
} else {
image.resize(Jimp.AUTO, Math.min(image.size, image.bitmap.height), algo);
}
new Jimp(BOUNDS.x2-BOUNDS.x1, BOUNDS.y2-BOUNDS.y1, function (err, image) {
if (!err) {
//Step 1: Draw Map + calculate viewport
Object.keys(options.parsedMapData.image.pixels).forEach(key => {
const color = COLORS[key];
options.parsedMapData.image.pixels[key].forEach(function drawPixel(px) {
if(px[0]>= BOUNDS.x1 && px[0]<= BOUNDS.x2 && px[1]>= BOUNDS.y1 && px[1]<= BOUNDS.y2 ){
image.setPixelColor(color, px[0]-BOUNDS.x1, px[1]-BOUNDS.y1);
}
})
});
//Step 2: Scale
image.scale(settings.scale, Jimp.RESIZE_NEAREST_NEIGHBOR);
//Step 3: Draw Path
const coords = options.parsedMapData.path.points.map(point => {
return [
Math.floor((point[0]/50 - options.parsedMapData.image.position.left - BOUNDS.x1) * settings.scale),
Math.floor((point[1]/50 - options.parsedMapData.image.position.top - BOUNDS.y1) * settings.scale)
]});
let first = true;
let oldPathX, oldPathY; // old Coordinates
let dx, dy; //delta x and y
let step, x, y, i;
coords.forEach(function (coord) {
if (!first && settings.drawPath) {
dx = (coord[0] - oldPathX);
dy = (coord[1] - oldPathY);
if (Math.abs(dx) >= Math.abs(dy)) {
new Jimp(width, height, 0x0, (err, image) => {
this.buffer = image;
for(let item of data) {
this.renderItem(item, options);
}
let filter = new options.filter();
filter.apply(image);
if(options.scale && options.scale !== 1) {
let scaleMethod = Jimp.RESIZE_BILINEAR;
if(options.scaleMethod === "NEAREST_NEIGHBOR") scaleMethod = Jimp.RESIZE_NEAREST_NEIGHBOR;
if(options.scaleMethod === "BICUBIC") scaleMethod = Jimp.RESIZE_BICUBIC;
if(options.scaleMethod === "HERMITE") scaleMethod = Jimp.RESIZE_HERMITE;
if(options.scaleMethod === "BEZIER") scaleMethod = Jimp.RESIZE_BEZIER;
image.resize(Math.round(width * options.scale) || 1, Math.round(height * options.scale) || 1, scaleMethod);
}
if(this.callback) this.callback(this);
});
}
promise = Jimp.read(nearestIcon.file).then(image =>
image.contain(
width,
height,
Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE,
options.pixel_art &&
width >= image.bitmap.width &&
height >= image.bitmap.height
? Jimp.RESIZE_NEAREST_NEIGHBOR
: null
)
);
async convert(src: string): Promise> {
let img: Jimp.Jimp;
try {
img = await Jimp.read(src);
} catch (e) {
return left(ConversionError.failedToLoad(e));
}
if (img.bitmap.height > MAX_HEIGHT || img.bitmap.width > MAX_WIDTH) {
return left(ConversionError.tooLarge({ width: MAX_WIDTH, height: MAX_HEIGHT }))
}
transparentBackgroundColor(img);
adjustSizeToPowerOf2(img);
img.resize(img.bitmap.width * 2, img.bitmap.height * 2, Jimp.RESIZE_NEAREST_NEIGHBOR);
const x = imageToChwFloat32Array(img, 4).map((v) => {
return v / 127.5 - 1.0;
});
this._inputImage.set(x);
try {
await this._runner.run();
} catch (e) {
return left(ConversionError.failedToConvert(e));
}
const y = this._outputImage.toActual().map((v) => Math.min(Math.max((v + 1.0) * 127.5, 0.0), 255.0));
const convertedImage = chwFloat32ArrayToImage(y, 4, img.bitmap.width, img.bitmap.height);
return new Promise>((resolve, reject) => {
(convertedImage as any).getBase64(Jimp.MIME_PNG, (error: any, dst: string) => {
if (error) {
const Jimp = require('jimp');
const RESIZE_MODES = [
Jimp.RESIZE_NEAREST_NEIGHBOR,
Jimp.RESIZE_BILINEAR,
Jimp.RESIZE_BICUBIC,
Jimp.RESIZE_HERMITE,
Jimp.RESIZE_BEZIER
];
const FIELD_CONFIGS = {
width: {
type: 'integer',
min: 1,
max: 4096,
required: true,
default: 150
},
height: {
type: 'integer',