heic-decode
Advanced tools
Comparing version 1.1.2 to 2.0.0
72
index.js
@@ -1,70 +0,6 @@ | ||
const libheif = require('libheif-js'); | ||
const libheif = require('libheif-js/wasm-bundle'); | ||
const uint8ArrayUtf8ByteString = (array, start, end) => { | ||
return String.fromCharCode(...array.slice(start, end)); | ||
}; | ||
const { one, all } = require('./lib.js')(libheif); | ||
// brands explained: https://github.com/strukturag/libheif/issues/83 | ||
// code adapted from: https://github.com/sindresorhus/file-type/blob/6f901bd82b849a85ca4ddba9c9a4baacece63d31/core.js#L428-L438 | ||
const isHeic = (buffer) => { | ||
const brandMajor = uint8ArrayUtf8ByteString(buffer, 8, 12).replace('\0', ' ').trim(); | ||
switch (brandMajor) { | ||
case 'mif1': | ||
return true; // {ext: 'heic', mime: 'image/heif'}; | ||
case 'msf1': | ||
return true; // {ext: 'heic', mime: 'image/heif-sequence'}; | ||
case 'heic': | ||
case 'heix': | ||
return true; // {ext: 'heic', mime: 'image/heic'}; | ||
case 'hevc': | ||
case 'hevx': | ||
return true; // {ext: 'heic', mime: 'image/heic-sequence'}; | ||
} | ||
return false; | ||
}; | ||
const decodeImage = async (image) => { | ||
const width = image.get_width(); | ||
const height = image.get_height(); | ||
const arrayBuffer = await new Promise((resolve, reject) => { | ||
image.display({ data: new Uint8ClampedArray(width*height*4), width, height }, (displayData) => { | ||
if (!displayData) { | ||
return reject(new Error('HEIF processing error')); | ||
} | ||
// get the ArrayBuffer from the Uint8Array | ||
resolve(displayData.data.buffer); | ||
}); | ||
}); | ||
return { width, height, data: arrayBuffer }; | ||
}; | ||
const decodeBuffer = async ({ buffer, all }) => { | ||
if (!isHeic(buffer)) { | ||
throw new TypeError('input buffer is not a HEIC image'); | ||
} | ||
const decoder = new libheif.HeifDecoder(); | ||
const data = decoder.decode(buffer); | ||
if (!data.length) { | ||
throw new Error('HEIF image not found'); | ||
} | ||
if (!all) { | ||
return await decodeImage(data[0]); | ||
} | ||
return data.map(image => { | ||
return { | ||
decode: async () => await decodeImage(image) | ||
}; | ||
}); | ||
}; | ||
module.exports = async ({ buffer }) => await decodeBuffer({ buffer, all: false }); | ||
module.exports.all = async ({ buffer }) => await decodeBuffer({ buffer, all: true }); | ||
module.exports = one; | ||
module.exports.all = all; |
{ | ||
"name": "heic-decode", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Decode HEIC images to raw data", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"libheif-js": "^1.10.0" | ||
"libheif-js": "^1.17.1" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -35,3 +35,3 @@ # heic-decode | ||
height, // integer height of the image | ||
data // ArrayBuffer containing decoded raw image data | ||
data // Uint8ClampedArray containing pixel data | ||
} = await decode({ buffer }); | ||
@@ -58,3 +58,3 @@ })(); | ||
height, // integer height of the image | ||
data // ArrayBuffer containing decoded raw image data | ||
data // Uint8ClampedArray containing pixel data | ||
} = await image.decode(); | ||
@@ -65,3 +65,3 @@ } | ||
You can use this data to integrate with other imaging libraries for processing. | ||
When the images are decoded, the return value is a plain object in the format of [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData). You can use this object to integrate with other imaging libraries for processing. | ||
@@ -68,0 +68,0 @@ _Note that while the decoder returns a Promise, it does the majority of the work synchronously, so you should consider using a worker thread in order to not block the main thread in highly concurrent production environments._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5856
4
67
Updatedlibheif-js@^1.17.1