image/test/reftest/img2html.html

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <html>
michael@0 2 <head>
michael@0 3 <title>Image-to-html converter</title>
michael@0 4 <style>
michael@0 5 #img, #canvas, #span {
michael@0 6 display: none;
michael@0 7 background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAL0lEQVQ4jWP09vZ+xoAHuLi44JNmYMIrSwQYNWAwGMBCKJ737NlDWxeMGjAYDAAAak4FtfgpsBoAAAAASUVORK5CYII=);
michael@0 8 }
michael@0 9 </style>
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <h2>Image-to-html converter</h2>
michael@0 13 <p>Enter the relative path to an image file, and this will convert it
michael@0 14 to a pure HTML representation (no images).</p>
michael@0 15
michael@0 16
michael@0 17 <form onsubmit="start_convert(); return false;">
michael@0 18 Path to image: <input type="text" id="filepath" size="60"><br>
michael@0 19 <input id="fill" type="checkbox">
michael@0 20 Fill canvas with <input id="fillRGB" value="rgb(10,100,250)"> (instead of transparency).<br>
michael@0 21 <button type='submit'>Convert!</button>
michael@0 22 <br><br>
michael@0 23 <img id="img" onload="run_convert();"><canvas id="canvas"></canvas><span id="span"></span><br>
michael@0 24 (img / canvas/ imghtml)
michael@0 25 <br><br>
michael@0 26 Result:<br>
michael@0 27 <textarea id="textarea" rows="10" cols="80"></textarea>
michael@0 28 </form>
michael@0 29
michael@0 30
michael@0 31 <script>
michael@0 32 var img = document.getElementById("img");
michael@0 33 var canvas = document.getElementById("canvas");
michael@0 34 var span = document.getElementById("span");
michael@0 35 var textarea = document.getElementById("textarea");
michael@0 36 var fill = document.getElementById("fill");
michael@0 37 var fillRGB = document.getElementById("fillRGB");
michael@0 38
michael@0 39 function start_convert() {
michael@0 40 try {
michael@0 41
michael@0 42 // Unhide stuff. They're initially hidden because the image shows a
michael@0 43 // broken-image icon on first page load, and the canvas defaults to a
michael@0 44 // large empty area.
michael@0 45 img.style.display = "inline";
michael@0 46 canvas.style.display = "inline";
michael@0 47 span.style.display = "inline-block";
michael@0 48
michael@0 49 // Clear out any previous values.
michael@0 50 textarea.value = "(loading image)"
michael@0 51 span.innerHTML = "";
michael@0 52
michael@0 53 // Get the image filename
michael@0 54 var input = document.getElementById("filepath");
michael@0 55 img.src = input.value;
michael@0 56
michael@0 57 // We're done, let the onload handler do the real work.
michael@0 58 } catch (e) {
michael@0 59 alert("Crap, start_convert failed: " + e);
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 function run_convert() {
michael@0 64 try {
michael@0 65 textarea.value = "(rendering canvas)";
michael@0 66
michael@0 67 canvas.width = img.width;
michael@0 68 canvas.height = img.height;
michael@0 69 var ctx = canvas.getContext("2d");
michael@0 70 ctx.clearRect(0, 0, img.width, img.height);
michael@0 71 if (fill.checked) {
michael@0 72 ctx.fillStyle = fillRGB.value;
michael@0 73 ctx.fillRect (0, 0, img.width, img.height);
michael@0 74 }
michael@0 75 ctx.drawImage(img, 0, 0);
michael@0 76
michael@0 77 // [r, g, b, a, r, g, b, a, ...]
michael@0 78 var pixels = ctx.getImageData(0, 0, img.width, img.height).data;
michael@0 79
michael@0 80 var imghtml = "<table cellpadding='0' cellspacing='0' width='" +
michael@0 81 img.width + "' height='" + img.height + "'>\n";
michael@0 82
michael@0 83 for (var y = 0; y < img.height; y++) {
michael@0 84 imghtml += "<tr height='1'>\n";
michael@0 85
michael@0 86 textarea.value = "(converting row " + y + ")";
michael@0 87
michael@0 88 for (var x = 0; x < img.width; x++) {
michael@0 89 var p = img.width * y * 4 + x * 4;
michael@0 90
michael@0 91 var r = pixels[p + 0];
michael@0 92 var g = pixels[p + 1];
michael@0 93 var b = pixels[p + 2];
michael@0 94 var a = pixels[p + 3];
michael@0 95
michael@0 96 var alpha = (a / 255).toString();
michael@0 97 alpha = alpha.substring(0, 6); // "0.12345678 --> 0.1234"
michael@0 98 imghtml += " <td width='1' style='background-color: " +
michael@0 99 "rgba(" +
michael@0 100 r + "," +
michael@0 101 g + "," +
michael@0 102 b + "," +
michael@0 103 alpha +
michael@0 104 ")'></td>\n";
michael@0 105 }
michael@0 106
michael@0 107 imghtml += "</tr>\n";
michael@0 108 }
michael@0 109
michael@0 110 imghtml += "</table>\n";
michael@0 111
michael@0 112 span.innerHTML = imghtml;
michael@0 113 textarea.value = "<html><body>\n" + imghtml + "</body></html>";
michael@0 114
michael@0 115 } catch (e) {
michael@0 116 alert("Crap, run_convert failed: " + e);
michael@0 117 }
michael@0 118 }
michael@0 119 </script>
michael@0 120
michael@0 121 </body>
michael@0 122 </html>

mercurial