Skip to content

Commit

Permalink
Merge pull request #32 from catdad-experiments/heic-decode-wasm
Browse files Browse the repository at this point in the history
[BREAKING] use heic-decode version with a wasm implementation of libheif-js
  • Loading branch information
catdad authored Nov 11, 2023
2 parents a9f7e96 + 7ba2a75 commit 4b6ca2d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 56 deletions.
58 changes: 3 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
@@ -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: Buffer.from(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;
59 changes: 59 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
@@ -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 })
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 4b6ca2d

Please sign in to comment.