Skip to content

Commit 9a09cdc

Browse files
committed
Add CropConverter
Added CropConverter to allow percentage based cropping on output images
1 parent d16adcf commit 9a09cdc

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎src/converter/CropConverter.mjs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {AbstractConverter} from "./AbstractConverter.mjs";
2+
3+
/**
4+
* Class CropConverter
5+
*/
6+
class CropConverter extends AbstractConverter {
7+
/**
8+
* @inheritDoc
9+
*/
10+
async convert() {
11+
const [from, width, height] = this.data;
12+
13+
if (!await this.output.exists(from)) {
14+
return [];
15+
}
16+
17+
const newWidth = image.getWidth() * width
18+
const newHeight = image.getHeight() * height
19+
20+
this.log.log(`Crop ${from} to ${newWidth}x${newHeight}`);
21+
22+
const image = await this.readImage(from);
23+
24+
image.crop(0, 0, newWidth, newHeight)
25+
26+
await this.writeImage(from, image);
27+
28+
return [];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
static get DEFAULT_CONVERTER_DATA() {
35+
return [
36+
// Conduit
37+
["textures/blocks/conduit_base.png", 0.75, 0.75],
38+
["textures/blocks/conduit_closed.png", 0.5, 0.5],
39+
["textures/blocks/conduit_open.png", 0.5, 0.5],
40+
];
41+
}
42+
}
43+
44+
export {CropConverter};

‎src/converter/converters.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {ChestNormalConverter} from "./ChestNormalConverter.mjs";
1111
import {ChestSideConverter} from "./ChestSideConverter.mjs";
1212
import {ColorizeOverlayConverter} from "./ColorizeOverlayConverter.mjs";
1313
import {CopyConverter} from "./CopyConverter.mjs";
14+
import {CropConverter} from "./CropConverter.mjs";
1415
import {DeleteConverter} from "./DeleteConverter.mjs";
1516
import {DespriteConverter} from "./DespriteConverter.mjs";
1617
import {DespriteExperimentalConverter} from "./DespriteExperimentalConverter.mjs";
@@ -91,6 +92,7 @@ const converters = [
9192
EnchantedItemGlintConverter,
9293
PngToTgaConverter,
9394
CopyConverter,
95+
CropConverter,
9496
DeleteConverter
9597
].reduce((converters, converter) => {
9698
converters.push(...converter.getDefaultConverters());

0 commit comments

Comments
 (0)