| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>libheif decoder demo</title> |
| 5 |
<style type="text/css"> |
| 6 |
body { |
| 7 |
font-family: sans-serif; |
| 8 |
color: white; |
| 9 |
background-color: black; |
| 10 |
} |
| 11 |
a { |
| 12 |
color: #00c1c3; |
| 13 |
} |
| 14 |
#form, #save-container, select { |
| 15 |
margin: 1em 0; |
| 16 |
} |
| 17 |
.hidden { |
| 18 |
display: none; |
| 19 |
} |
| 20 |
.flexible-size { |
| 21 |
position: relative; |
| 22 |
height: 0; |
| 23 |
} |
| 24 |
.flexible-size canvas { |
| 25 |
position: absolute; |
| 26 |
top: 0; |
| 27 |
left: 0; |
| 28 |
width: 100%; |
| 29 |
height: 100%; |
| 30 |
} |
| 31 |
</style> |
| 32 |
</head> |
| 33 |
<body> |
| 34 |
<h1>libheif decoder demo</h1> |
| 35 |
<a href="https://github.com/strukturag/libheif"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/52760788cde945287fbb584134c4cbc2bc36f904/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f77686974655f6666666666662e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png"></a> |
| 36 |
<div><small>Copyright © 2017-2018 by <a href="http://www.struktur.de">struktur AG</a></small></div> |
| 37 |
<div id="form" class="hidden"> |
| 38 |
<div> |
| 39 |
Select HEIF image file to display: |
| 40 |
</div> |
| 41 |
<input type="file" id="file"> |
| 42 |
</div> |
| 43 |
<select id="images" class="hidden"></select> |
| 44 |
<div id="loading"> |
| 45 |
Loading, please wait... |
| 46 |
</div> |
| 47 |
<div id="decoding" class="hidden"> |
| 48 |
Decoding, please wait... |
| 49 |
</div> |
| 50 |
<div id="save-container" class="hidden"> |
| 51 |
<button id="save">Save image...</button> |
| 52 |
<div id="downloads" class="hidden"> |
| 53 |
</div> |
| 54 |
</div> |
| 55 |
<div id="container" class="hidden"> |
| 56 |
<canvas id="canvas"></canvas> |
| 57 |
</div> |
| 58 |
<div id="error-format" class="hidden"> |
| 59 |
The selected file is not a valid HEIF file or the specific format is not |
| 60 |
supported yet.<br> |
| 61 |
If you selected is a valid HEIF file and still see this error, please send |
| 62 |
the file to |
| 63 |
<a target="_blank" href="mailto:[email protected]">[email protected]</a> |
| 64 |
so we can improve the library. |
| 65 |
</div> |
| 66 |
<script> |
| 67 |
(function() { |
| 68 |
|
| 69 |
var saveSupported = false; |
| 70 |
|
| 71 |
// IE polyfill for Canvas.toBlob |
| 72 |
if (!HTMLCanvasElement.prototype.toBlob) { |
| 73 |
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', { |
| 74 |
value: function (callback, type, quality) { |
| 75 |
var canvas = this; |
| 76 |
setTimeout(function() { |
| 77 |
|
| 78 |
var binStr = atob(canvas.toDataURL(type, quality).split(',')[1]), |
| 79 |
len = binStr.length, |
| 80 |
arr = new Uint8Array(len); |
| 81 |
|
| 82 |
for (var i = 0; i < len; i++ ) { |
| 83 |
arr[i] = binStr.charCodeAt(i); |
| 84 |
} |
| 85 |
|
| 86 |
callback(new Blob([arr], {type: type || 'image/png'})); |
| 87 |
}); |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
function show(id) { |
| 93 |
document.getElementById(id).style.display = "block"; |
| 94 |
} |
| 95 |
|
| 96 |
function hide(id) { |
| 97 |
document.getElementById(id).style.display = "none"; |
| 98 |
} |
| 99 |
|
| 100 |
function addClass(elem, classname) { |
| 101 |
if (elem.classList) { |
| 102 |
elem.classList.add(classname); |
| 103 |
} else { |
| 104 |
var arr = elem.className.split(" "); |
| 105 |
if (arr.indexOf(classname) === -1) { |
| 106 |
elem.className += " " + classname; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
function removeClass(elem, classname) { |
| 112 |
if (elem.classList) { |
| 113 |
elem.classList.remove(classname); |
| 114 |
} else { |
| 115 |
var arr = elem.className.split(" "); |
| 116 |
var i = arr.indexOf(classname); |
| 117 |
if (i >= 0) { |
| 118 |
arr.splice(i, 1); |
| 119 |
elem.className = arr.join(" "); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
var CanvasDrawer = function(canvas) { |
| 125 |
this.canvas = canvas; |
| 126 |
this.ctx = canvas.getContext("2d"); |
| 127 |
this.image_data = null; |
| 128 |
this.container = document.getElementById("container"); |
| 129 |
}; |
| 130 |
|
| 131 |
CanvasDrawer.prototype.draw = function(image) { |
| 132 |
var w = image.get_width(); |
| 133 |
var h = image.get_height(); |
| 134 |
if (w != this.canvas.width || h != this.canvas.height || |
| 135 |
!this.image_data) { |
| 136 |
this.canvas.width = w; |
| 137 |
this.canvas.height = h; |
| 138 |
if (w > document.body.clientWidth) { |
| 139 |
addClass(this.container, "flexible-size"); |
| 140 |
this.container.style["padding-bottom"] = String(100 * (h / w)) + "%"; |
| 141 |
} else { |
| 142 |
removeClass(this.container, "flexible-size"); |
| 143 |
this.container.style["padding-bottom"] = ""; |
| 144 |
} |
| 145 |
this.image_data = this.ctx.createImageData(w, h); |
| 146 |
var image_data = this.image_data.data; |
| 147 |
// Start with a white image. |
| 148 |
for (var i=0; i<w*h; i++) { |
| 149 |
image_data[i*4+3] = 255; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
image.display(this.image_data, function(display_image_data) { |
| 154 |
hide("decoding"); |
| 155 |
if (!display_image_data) { |
| 156 |
show("error-format"); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
show("container"); |
| 161 |
if (saveSupported) { |
| 162 |
show("save-container"); |
| 163 |
} else { |
| 164 |
hide("save-container"); |
| 165 |
} |
| 166 |
if (window.requestAnimationFrame) { |
| 167 |
this.pending_image_data = display_image_data; |
| 168 |
window.requestAnimationFrame(function() { |
| 169 |
if (this.pending_image_data) { |
| 170 |
this.ctx.putImageData(this.pending_image_data, 0, 0); |
| 171 |
this.pending_image_data = null; |
| 172 |
} |
| 173 |
}.bind(this)); |
| 174 |
} else { |
| 175 |
this.ctx.putImageData(display_image_data, 0, 0); |
| 176 |
} |
| 177 |
}.bind(this)); |
| 178 |
}; |
| 179 |
|
| 180 |
function HeifDemo(libheif) { |
| 181 |
this.libheif = libheif; |
| 182 |
this.canvas = document.getElementById("canvas"); |
| 183 |
this.downloads = document.getElementById("downloads"); |
| 184 |
this.image_data = []; |
| 185 |
this.images_select = document.getElementById("images"); |
| 186 |
this.images_select.addEventListener("change", function(event) { |
| 187 |
var index = parseInt(event.target.value, 10); |
| 188 |
if (index >= 0 && index < this.image_data.length) { |
| 189 |
this._showImage(this.image_data[index]); |
| 190 |
} |
| 191 |
}.bind(this)); |
| 192 |
this.drawer = new CanvasDrawer(canvas); |
| 193 |
this.decoder = new libheif.HeifDecoder(); |
| 194 |
|
| 195 |
console.log("Using libheif", libheif.heif_get_version()); |
| 196 |
saveSupported = this.canvas.toBlob && |
| 197 |
((URL && URL.createObjectURL) || navigator.msSaveOrOpenBlob); |
| 198 |
if (saveSupported) { |
| 199 |
console.log("Saving images is supported in this browser."); |
| 200 |
} else { |
| 201 |
console.warn("Saving images is not supported in this browser."); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
HeifDemo.prototype.reset = function() { |
| 206 |
this.canvas.width = 0; |
| 207 |
this.canvas.height = 0; |
| 208 |
}; |
| 209 |
|
| 210 |
HeifDemo.prototype.loadUrl = function(url) { |
| 211 |
var request = new XMLHttpRequest(); |
| 212 |
console.log("Downloading", url); |
| 213 |
request.open("get", url, true); |
| 214 |
request.responseType = "arraybuffer"; |
| 215 |
hide("container"); |
| 216 |
show("loading"); |
| 217 |
request.onload = function(event) { |
| 218 |
hide("loading"); |
| 219 |
if (request.status !== 200) { |
| 220 |
console.log("Downloading failed", request); |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
var data = request.response; |
| 225 |
var remaining = data.byteLength; |
| 226 |
console.log("Received", remaining, "bytes"); |
| 227 |
var buffer = new Uint8Array(data, 0, remaining); |
| 228 |
var filename = url.substring(url.lastIndexOf('/')+1); |
| 229 |
this.loadBuffer(buffer, filename); |
| 230 |
}.bind(this); |
| 231 |
request.send(); |
| 232 |
}; |
| 233 |
|
| 234 |
HeifDemo.prototype.loadBuffer = function(buffer, filename) { |
| 235 |
this.filename = filename; |
| 236 |
hide("error-format"); |
| 237 |
hide("container"); |
| 238 |
hide("save-container"); |
| 239 |
show("loading"); |
| 240 |
for (var i = 0; i < this.image_data.length; i++) { |
| 241 |
this.image_data[i].free(); |
| 242 |
} |
| 243 |
this.image_data = this.decoder.decode(buffer); |
| 244 |
hide("loading"); |
| 245 |
if (!this.image_data || !this.image_data.length) { |
| 246 |
show("error-format"); |
| 247 |
return false; |
| 248 |
} |
| 249 |
this._showImage(this.image_data[0]); |
| 250 |
if (this.image_data.length > 1) { |
| 251 |
show("images"); |
| 252 |
while (this.images_select.firstChild) { |
| 253 |
this.images_select.removeChild(this.images_select.firstChild); |
| 254 |
} |
| 255 |
for (var i = 0; i < this.image_data.length; i++) { |
| 256 |
var option = document.createElement("option"); |
| 257 |
option.setAttribute("value", i); |
| 258 |
var label = document.createTextNode("Image " + (i+1)); |
| 259 |
option.appendChild(label); |
| 260 |
this.images_select.appendChild(option); |
| 261 |
} |
| 262 |
} else { |
| 263 |
hide("images"); |
| 264 |
} |
| 265 |
return true; |
| 266 |
}; |
| 267 |
|
| 268 |
HeifDemo.prototype._showImage = function(image) { |
| 269 |
this.reset(); |
| 270 |
hide("container"); |
| 271 |
hide("save-container"); |
| 272 |
show("decoding"); |
| 273 |
this.drawer.draw(image); |
| 274 |
}; |
| 275 |
|
| 276 |
HeifDemo.prototype.saveImage = function(format) { |
| 277 |
if (!saveSupported) { |
| 278 |
alert("Saving images is not supported with your browser."); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
if (!format) { |
| 283 |
format = "image/jpeg"; |
| 284 |
} |
| 285 |
while (this.downloads.firstChild) { |
| 286 |
this.downloads.removeChild(this.downloads.firstChild); |
| 287 |
} |
| 288 |
|
| 289 |
var FILE_EXTENSIONS = { |
| 290 |
"image/jpeg": ".jpeg", |
| 291 |
"image/png": ".png" |
| 292 |
}; |
| 293 |
this.canvas.toBlob(function(blob) { |
| 294 |
var extension = FILE_EXTENSIONS[blob.type] || ".bin"; |
| 295 |
var basename = this.filename.replace(/\.[^/.]+$/, ""); |
| 296 |
if (navigator.msSaveOrOpenBlob) { |
| 297 |
navigator.msSaveOrOpenBlob(blob, basename + extension); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
var url = URL.createObjectURL(blob); |
| 302 |
var dlink = document.createElement("a"); |
| 303 |
dlink.download = basename + extension; |
| 304 |
dlink.href = url; |
| 305 |
this.downloads.appendChild(dlink); |
| 306 |
dlink.click(); |
| 307 |
URL.revokeObjectURL(url); |
| 308 |
}.bind(this), format); |
| 309 |
}; |
| 310 |
|
| 311 |
window.onload = function() { |
| 312 |
hide("loading"); |
| 313 |
if (typeof libheif === "undefined") { |
| 314 |
alert("libheif could not be loaded, please check your browser console for details."); |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
var demo = new HeifDemo(libheif()); |
| 319 |
|
| 320 |
show("form"); |
| 321 |
|
| 322 |
document.getElementById("file").addEventListener("change", function(event) { |
| 323 |
var files = event.target.files; |
| 324 |
if (!files || !files.length) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
hide("container"); |
| 329 |
hide("error-format"); |
| 330 |
hide("decoding"); |
| 331 |
show("loading"); |
| 332 |
demo.reset(); |
| 333 |
var file = files[0]; |
| 334 |
var reader = new FileReader(); |
| 335 |
reader.onload = (function(theFile) { |
| 336 |
return function(e) { |
| 337 |
hide("container"); |
| 338 |
hide("save-container"); |
| 339 |
setTimeout(function() { |
| 340 |
var buffer = e.target.result; |
| 341 |
if (!demo.loadBuffer(buffer, theFile.name)) { |
| 342 |
hide("container"); |
| 343 |
show("error-format"); |
| 344 |
} |
| 345 |
}, 1); |
| 346 |
}; |
| 347 |
})(file); |
| 348 |
reader.readAsArrayBuffer(file); |
| 349 |
}); |
| 350 |
|
| 351 |
document.getElementById("save").addEventListener("click", function(event) { |
| 352 |
demo.saveImage(); |
| 353 |
}); |
| 354 |
|
| 355 |
// Start with example image. |
| 356 |
var url = "example.heic"; |
| 357 |
demo.loadUrl(url); |
| 358 |
}; |
| 359 |
|
| 360 |
}).call(); |
| 361 |
</script> |
| 362 |
<script src="libheif.js"></script> |
| 363 |
</body> |
| 364 |
</html> |