Skip to content

Commit e551ae6

Browse files
committed
separating format conversions into separate file, provided to lib as a parameter
1 parent 9463113 commit e551ae6

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

formats-node.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const jpegJs = require('jpeg-js');
2+
const { PNG } = require('pngjs');
3+
4+
module.exports = {};
5+
6+
module.exports.JPEG = ({ data, width, height, quality }) => jpegJs.encode({ data, width, height }, quality).data;
7+
8+
module.exports.PNG = ({ data, width, height }) => {
9+
const png = new PNG({ width, height });
10+
png.data = data;
11+
12+
return PNG.sync.write(png, {
13+
width: width,
14+
height: height,
15+
deflateLevel: 9,
16+
deflateStrategy: 3,
17+
filterType: -1,
18+
colorType: 6,
19+
inputHasAlpha: true
20+
});
21+
};

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const decode = require('heic-decode');
2-
const { one, all } = require('./lib.js')(decode);
2+
const formats = require('./formats-node.js');
3+
const { one, all } = require('./lib.js')(decode, formats);
34

45
module.exports = one;
56
module.exports.all = all;

lib.js

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
1-
const jpegJs = require('jpeg-js');
2-
const { PNG } = require('pngjs');
3-
4-
module.exports = decode => {
5-
const to = {
6-
JPEG: ({ data, width, height, quality }) => jpegJs.encode({ data, width, height }, quality).data,
7-
PNG: ({ data, width, height }) => {
8-
const png = new PNG({ width, height });
9-
png.data = data;
10-
11-
return PNG.sync.write(png, {
12-
width: width,
13-
height: height,
14-
deflateLevel: 9,
15-
deflateStrategy: 3,
16-
filterType: -1,
17-
colorType: 6,
18-
inputHasAlpha: true
19-
});
20-
}
21-
};
22-
1+
module.exports = (decode, encode) => {
232
const convertImage = async ({ image, format, quality }) => {
24-
return await to[format]({
3+
return await encode[format]({
254
width: image.width,
265
height: image.height,
276
data: image.data,
@@ -30,8 +9,8 @@ module.exports = decode => {
309
};
3110

3211
const convert = async ({ buffer, format, quality, all }) => {
33-
if (!to[format]) {
34-
throw new Error(`output format needs to be one of [${Object.keys(to)}]`);
12+
if (!encode[format]) {
13+
throw new Error(`output format needs to be one of [${Object.keys(encode)}]`);
3514
}
3615

3716
if (!all) {

0 commit comments

Comments
 (0)