-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a browser implementation using canvas to encode the resulting …
…image
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const decode = require('heic-decode'); | ||
const formats = require('./formats-browser.js'); | ||
const { one, all } = require('./lib.js')(decode, formats); | ||
|
||
module.exports = one; | ||
module.exports.all = all; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module.exports = {}; | ||
|
||
const initializeCanvas = ({ data, width, height }) => { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = width; | ||
canvas.height = height; | ||
const ctx = canvas.getContext('2d'); | ||
|
||
const imageData = new ImageData(data, width, height); | ||
|
||
ctx.putImageData(imageData, 0, 0); | ||
|
||
return canvas; | ||
}; | ||
|
||
const convert = async ({ data, width, height }, ...blobArgs) => { | ||
const canvas = initializeCanvas({ data, width, height }); | ||
|
||
const blob = await new Promise((resolve, reject) => { | ||
canvas.toBlob(blob => { | ||
if (blob) { | ||
return resolve(blob); | ||
} | ||
|
||
return reject(new Error('failed to convert the image')); | ||
}, ...blobArgs); | ||
}); | ||
|
||
const arrayBuffer = await blob.arrayBuffer(); | ||
|
||
return new Uint8Array(arrayBuffer); | ||
}; | ||
|
||
module.exports.JPEG = async ({ data, width, height, quality }) => { | ||
return convert({ data, width, height }, 'image/jpeg', quality); | ||
}; | ||
|
||
module.exports.PNG = async ({ data, width, height }) => { | ||
return convert({ data, width, height }, 'image/png'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const JSDOM = require('jsdom').JSDOM; | ||
const canvas = require('canvas'); | ||
const runTests = require('./run-tests.js'); | ||
|
||
describe('heic-convert (browser)', () => { | ||
before(() => { | ||
const { window } = new JSDOM(``, { | ||
pretendToBeVisual: true | ||
}); | ||
|
||
global.window = window; | ||
global.document = window.document; | ||
global.ImageData = canvas.ImageData; | ||
|
||
// okay, now we are getting hacky... jsdom folks got into a | ||
// fight when talking about implementing this spec, which | ||
// is now broadly supported across all evergreen browsers | ||
// https://github.com/jsdom/jsdom/issues/2555 | ||
global.window.Blob.prototype.arrayBuffer = async function() { | ||
const blob = this; | ||
const fileReader = new window.FileReader(); | ||
|
||
var arrayBuffer = new Promise(r => { | ||
fileReader.onload = function(event) { | ||
r(event.target.result); | ||
}; | ||
|
||
fileReader.readAsArrayBuffer(blob); | ||
}); | ||
|
||
return arrayBuffer; | ||
}; | ||
}); | ||
|
||
after(() => { | ||
global.window.close(); | ||
delete global.window; | ||
}); | ||
|
||
runTests(require('../browser')); | ||
}); |