From 97d76f8cee6ef4166c9b511113e655253a42c431 Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Thu, 9 Nov 2023 18:23:41 -0500 Subject: [PATCH 1/2] updating to heic-decode@2 (uses libheif wasm version) --- index.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b04872f..9c33d5e 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ const convertImage = async ({ image, format, quality }) => { return await to[format]({ width: image.width, height: image.height, - data: Buffer.from(image.data), + data: image.data, quality: Math.floor(quality * 100) }); }; diff --git a/package.json b/package.json index 7f31a4f..e6bc4e1 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "rootrequire": "^1.0.0" }, "dependencies": { - "heic-decode": "^1.1.2", + "heic-decode": "^2.0.0", "jpeg-js": "^0.4.4", "pngjs": "^6.0.0" }, From 7ba2a75c34ad9605c2d13b3afe765dd5533be727 Mon Sep 17 00:00:00 2001 From: Kiril Vatev Date: Thu, 9 Nov 2023 18:36:08 -0500 Subject: [PATCH 2/2] allow using heic-convert with a custom heic-decode instance --- index.js | 58 +++---------------------------------------------------- lib.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 55 deletions(-) create mode 100644 lib.js diff --git a/index.js b/index.js index 9c33d5e..4ac14c9 100644 --- a/index.js +++ b/index.js @@ -1,57 +1,5 @@ -const jpegJs = require('jpeg-js'); -const { PNG } = require('pngjs'); - const decode = require('heic-decode'); +const { one, all } = require('./lib.js')(decode); -const to = { - JPEG: ({ data, width, height, quality }) => jpegJs.encode({ data, width, height }, quality).data, - PNG: ({ data, width, height }) => { - const png = new PNG({ width, height }); - png.data = data; - - return PNG.sync.write(png, { - width: width, - height: height, - deflateLevel: 9, - deflateStrategy: 3, - filterType: -1, - colorType: 6, - inputHasAlpha: true - }); - } -}; - -const convertImage = async ({ image, format, quality }) => { - return await to[format]({ - width: image.width, - height: image.height, - data: image.data, - quality: Math.floor(quality * 100) - }); -}; - -const convert = async ({ buffer, format, quality, all }) => { - if (!to[format]) { - throw new Error(`output format needs to be one of [${Object.keys(to)}]`); - } - - if (!all) { - const image = await decode({ buffer }); - return await convertImage({ image, format, quality }); - } - - const images = await decode.all({ buffer }); - - return images.map(image => { - return { - convert: async () => await convertImage({ - image: await image.decode(), - format, - quality - }) - }; - }); -}; - -module.exports = async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: false }); -module.exports.all = async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: true }); +module.exports = one; +module.exports.all = all; diff --git a/lib.js b/lib.js new file mode 100644 index 0000000..678df09 --- /dev/null +++ b/lib.js @@ -0,0 +1,59 @@ +const jpegJs = require('jpeg-js'); +const { PNG } = require('pngjs'); + +module.exports = decode => { + const to = { + JPEG: ({ data, width, height, quality }) => jpegJs.encode({ data, width, height }, quality).data, + PNG: ({ data, width, height }) => { + const png = new PNG({ width, height }); + png.data = data; + + return PNG.sync.write(png, { + width: width, + height: height, + deflateLevel: 9, + deflateStrategy: 3, + filterType: -1, + colorType: 6, + inputHasAlpha: true + }); + } + }; + + const convertImage = async ({ image, format, quality }) => { + return await to[format]({ + width: image.width, + height: image.height, + data: image.data, + quality: Math.floor(quality * 100) + }); + }; + + const convert = async ({ buffer, format, quality, all }) => { + if (!to[format]) { + throw new Error(`output format needs to be one of [${Object.keys(to)}]`); + } + + if (!all) { + const image = await decode({ buffer }); + return await convertImage({ image, format, quality }); + } + + const images = await decode.all({ buffer }); + + return images.map(image => { + return { + convert: async () => await convertImage({ + image: await image.decode(), + format, + quality + }) + }; + }); + }; + + return { + one: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: false }), + all: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: true }) + }; +};