-
-
Save iTonyYo/e6268df407fc1c3c16494d021b5560b9 to your computer and use it in GitHub Desktop.
webp support using libwebpjs (webp->png in canvas)
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
WebPDecodeAndDraw = function (data) { | |
var decoder = new WebPDecoder(); | |
var bitmap = decoder.WebPDecode(data, data.length); | |
if (bitmap) { | |
//Draw Image | |
var output = ctx.createImageData(canvas.width, canvas.height); | |
var biWidth = canvas.width; | |
var outputData = output.data; | |
for (var h=0;h<canvas.height;h++) { | |
for (var w=0;w<canvas.width;w++) { | |
outputData[0+w*4+(biWidth*4)*h] = bitmap[0+w*4+(biWidth*4)*h]; | |
outputData[1+w*4+(biWidth*4)*h] = bitmap[1+w*4+(biWidth*4)*h]; | |
outputData[2+w*4+(biWidth*4)*h] = bitmap[2+w*4+(biWidth*4)*h]; | |
outputData[3+w*4+(biWidth*4)*h] = bitmap[3+w*4+(biWidth*4)*h]; | |
}; | |
} | |
ctx.putImageData(output, 0, 0); | |
var dataURL = canvas.toDataURL("image/png"); | |
document.getElementById("dec").src=dataURL; | |
} | |
}; | |
function getImage(img) { | |
// Create an empty canvas element | |
canvas = document.createElement("canvas"); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
// Copy the image contents to the canvas | |
ctx = canvas.getContext("2d"); | |
ctx.drawImage(img, 0, 0); | |
WebPDecodeAndDraw(ctx.getImageData(0,0,ctx.canvas.width,ctx.canvas.height)['data']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment