| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>Image Plane viewer</title> |
| 4 |
<script> |
| 5 |
|
| 6 |
// Check that we have a modern browser |
| 7 |
if (window.File && window.FileReader && window.FileList && window.Blob) { |
| 8 |
} else { |
| 9 |
alert("Get a better browser!"); |
| 10 |
} |
| 11 |
|
| 12 |
var imageDataBlock = function(block) |
| 13 |
{ |
| 14 |
this.block = block; |
| 15 |
this.pos = 0; |
| 16 |
this.width = 100; |
| 17 |
this.height = 100; |
| 18 |
this.depth = 1; |
| 19 |
this.maxval = 255; |
| 20 |
this.planar = false; |
| 21 |
} |
| 22 |
|
| 23 |
function error(message) |
| 24 |
{ |
| 25 |
alert(message); |
| 26 |
} |
| 27 |
|
| 28 |
imageDataBlock.prototype.getLine = function () |
| 29 |
{ |
| 30 |
var line=""; |
| 31 |
var c; |
| 32 |
|
| 33 |
while ((c = this.block[this.pos++]).charCodeAt(0) >= 32) |
| 34 |
line += c; |
| 35 |
return line; |
| 36 |
} |
| 37 |
|
| 38 |
imageDataBlock.prototype.updateImage = function() |
| 39 |
{ |
| 40 |
var w = this.width; |
| 41 |
var h = this.height; |
| 42 |
var d = this.depth; |
| 43 |
var x, y, z; |
| 44 |
var R, G, B; |
| 45 |
var p = this.pos; |
| 46 |
var canvas = this.canvas.getContext("2d"); |
| 47 |
var id = canvas.getImageData(0, 0, w, h); |
| 48 |
var q = 0; |
| 49 |
|
| 50 |
if (!this.planar) |
| 51 |
{ |
| 52 |
for (y = h; y > 0; y--) |
| 53 |
{ |
| 54 |
for (x = w; x > 0; x--) |
| 55 |
{ |
| 56 |
var C = 0; |
| 57 |
var M = 0; |
| 58 |
var Y = 0; |
| 59 |
var K = 0; |
| 60 |
for (z = 0; z < d; z++) |
| 61 |
{ |
| 62 |
var a = this.block.charCodeAt(p++); |
| 63 |
var pl = this.planes[z]; |
| 64 |
if (pl.enabled) |
| 65 |
{ |
| 66 |
C += a * pl.c; |
| 67 |
M += a * pl.m; |
| 68 |
Y += a * pl.y; |
| 69 |
K += a * pl.k; |
| 70 |
} |
| 71 |
} |
| 72 |
R = 255 - C - K; |
| 73 |
if (R < 0) R = 0; |
| 74 |
G = 255 - M - K; |
| 75 |
if (G < 0) G = 0; |
| 76 |
B = 255 - Y - K; |
| 77 |
if (B < 0) B = 0; |
| 78 |
id.data[q++] = R; |
| 79 |
id.data[q++] = G; |
| 80 |
id.data[q++] = B; |
| 81 |
id.data[q++] = 255; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
var fwd = w*h; |
| 88 |
var back = w*h*d-1; |
| 89 |
for (y = h; y > 0; y--) |
| 90 |
{ |
| 91 |
for (x = w; x > 0; x--) |
| 92 |
{ |
| 93 |
R = 0; |
| 94 |
G = 0; |
| 95 |
B = 0; |
| 96 |
for (z = 0; z < d; z++) |
| 97 |
{ |
| 98 |
var a = this.block.charCodeAt(p); |
| 99 |
p += fwd; |
| 100 |
var pl = this.planes[z]; |
| 101 |
if (pl.enabled) |
| 102 |
{ |
| 103 |
R += a * pl.c; |
| 104 |
G += a * pl.m; |
| 105 |
B += a * pl.y; |
| 106 |
} |
| 107 |
} |
| 108 |
p -= back; |
| 109 |
if (R > 255) R = 255; |
| 110 |
if (G > 255) G = 255; |
| 111 |
if (B > 255) B = 255; |
| 112 |
id.data[q++] = R; |
| 113 |
id.data[q++] = G; |
| 114 |
id.data[q++] = B; |
| 115 |
id.data[q++] = 255; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
canvas.putImageData(id, 0, 0); |
| 120 |
} |
| 121 |
|
| 122 |
imageDataBlock.prototype.setDepth = function(depth_) |
| 123 |
{ |
| 124 |
var i; |
| 125 |
this.depth = depth_; |
| 126 |
this.planes = new Array(this.depth); |
| 127 |
for (i=0; i < this.depth; i++) |
| 128 |
{ |
| 129 |
this.planes[i] = new Object(); |
| 130 |
this.planes[i].c = 0; |
| 131 |
this.planes[i].m = 0; |
| 132 |
this.planes[i].y = 0; |
| 133 |
this.planes[i].k = 1; |
| 134 |
this.planes[i].name = "Spot"; |
| 135 |
this.planes[i].enabled = true; |
| 136 |
} |
| 137 |
if (this.depth == 1) |
| 138 |
{ |
| 139 |
this.planes[0].name = "K"; |
| 140 |
} |
| 141 |
else if (this.planar) |
| 142 |
{ |
| 143 |
if (this.depth >= 3) |
| 144 |
{ |
| 145 |
this.planes[0].c = 1; |
| 146 |
this.planes[0].k = 0; |
| 147 |
this.planes[0].name = "R"; |
| 148 |
this.planes[1].m = 1; |
| 149 |
this.planes[1].k = 0; |
| 150 |
this.planes[1].name = "G"; |
| 151 |
this.planes[2].y = 1; |
| 152 |
this.planes[2].k = 0; |
| 153 |
this.planes[2].name = "B"; |
| 154 |
i = 3; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
this.planes[0].c = 1; |
| 159 |
this.planes[0].m = 1; |
| 160 |
this.planes[0].y = 1; |
| 161 |
this.planes[0].k = 0; |
| 162 |
this.planes[0].name = "Gray"; |
| 163 |
i = 1; |
| 164 |
} |
| 165 |
for (; i < this.depth; i++) |
| 166 |
{ |
| 167 |
this.planes[i].c = 1; |
| 168 |
this.planes[i].m = 1; |
| 169 |
this.planes[i].y = 1; |
| 170 |
this.planes[i].k = 0; |
| 171 |
this.planes[i].name = "Plane"; |
| 172 |
this.planes[i].enabled = false; |
| 173 |
} |
| 174 |
} |
| 175 |
else if (this.depth >= 4) |
| 176 |
{ |
| 177 |
this.planes[0].c = 1; |
| 178 |
this.planes[0].k = 0; |
| 179 |
this.planes[0].name = "C"; |
| 180 |
this.planes[1].m = 1; |
| 181 |
this.planes[1].k = 0; |
| 182 |
this.planes[1].name = "M"; |
| 183 |
this.planes[2].y = 1; |
| 184 |
this.planes[2].k = 0; |
| 185 |
this.planes[2].name = "Y"; |
| 186 |
this.planes[3].name = "K"; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
imageDataBlock.prototype.planeChange = function(plane,checked) |
| 191 |
{ |
| 192 |
this.planes[plane].enabled = checked; |
| 193 |
this.updateImage(); |
| 194 |
} |
| 195 |
|
| 196 |
function strip(line) |
| 197 |
{ |
| 198 |
while (line.substring(0,1) == " ") |
| 199 |
line = line.substring(1); |
| 200 |
return line; |
| 201 |
} |
| 202 |
|
| 203 |
imageDataBlock.prototype.readPAM = function () |
| 204 |
{ |
| 205 |
while (1) |
| 206 |
{ |
| 207 |
var line = this.getLine(); |
| 208 |
if (line.substring(0, 5) == "WIDTH") |
| 209 |
this.width = parseInt(line.substring(6), 10); |
| 210 |
else if (line.substring(0, 6) == "HEIGHT") |
| 211 |
this.height = parseInt(line.substring(7), 10); |
| 212 |
else if (line.substring(0, 5) == "DEPTH") |
| 213 |
this.setDepth(parseInt(line.substring(6), 10)); |
| 214 |
else if (line.substring(0, 6) == "MAXVAL") |
| 215 |
this.maxval = parseInt(line.substring(7), 10); |
| 216 |
else if (line.substring(0, 8) == "TUPLTYPE") |
| 217 |
{ |
| 218 |
if (line.substring(9,13) == "CMYK") |
| 219 |
{} |
| 220 |
else if (line.substring(9,13) == "DEVN") |
| 221 |
{} |
| 222 |
else |
| 223 |
return error("Not a CMYK or DEVN file!"); |
| 224 |
} |
| 225 |
else if (line.substring(0, 1) == "#") |
| 226 |
{ |
| 227 |
line = strip(line.substring(1)); |
| 228 |
if (line.split(" ")[0] == "PLANE") |
| 229 |
{ |
| 230 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 231 |
var p = parseInt(line, 10); |
| 232 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 233 |
this.planes[p].name = line.split(" ")[0]; |
| 234 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 235 |
this.planes[p].c = parseFloat(line); |
| 236 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 237 |
this.planes[p].m = parseFloat(line); |
| 238 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 239 |
this.planes[p].y = parseFloat(line); |
| 240 |
line = strip(line.substring((line+" ").indexOf(" "))); |
| 241 |
this.planes[p].k = parseFloat(line); |
| 242 |
} |
| 243 |
} |
| 244 |
else if (line.substring(0, 6) == "ENDHDR") |
| 245 |
break; |
| 246 |
else |
| 247 |
return error("Unknown header field: "+line); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
imageDataBlock.prototype.readPGM = function () |
| 252 |
{ |
| 253 |
var i = 0; |
| 254 |
var line = ""; |
| 255 |
while (i < 3) |
| 256 |
{ |
| 257 |
if (line == "") |
| 258 |
line = this.getLine(); |
| 259 |
else if (line.substring(0, 1) == "#") |
| 260 |
line = ""; |
| 261 |
else if (line.substring(0, 1) == " ") |
| 262 |
line = line.substring(1); |
| 263 |
else if (line.substring(0, 1) == "\t") |
| 264 |
line = line.substring(1); |
| 265 |
else |
| 266 |
{ |
| 267 |
if (i == 0) |
| 268 |
this.width = parseInt(line, 10); |
| 269 |
else if (i == 1) |
| 270 |
this.height = parseInt(line, 10); |
| 271 |
else if (i == 2) |
| 272 |
this.maxval = parseInt(line, 10); |
| 273 |
i++; |
| 274 |
var idx = line.indexOf(" "); |
| 275 |
if (idx >= 0) |
| 276 |
line = line.substring(idx+1); |
| 277 |
else |
| 278 |
line = ""; |
| 279 |
} |
| 280 |
} |
| 281 |
this.setDepth(1); |
| 282 |
|
| 283 |
var m = this.pos; |
| 284 |
var n = ""; |
| 285 |
for (i = this.width * this.height; i > 0; i--) |
| 286 |
{ |
| 287 |
n += String.fromCharCode(this.block.charCodeAt(m++)^255); |
| 288 |
} |
| 289 |
this.block = n; |
| 290 |
this.pos = 0; |
| 291 |
} |
| 292 |
|
| 293 |
imageDataBlock.prototype.readRAW = function (name) |
| 294 |
{ |
| 295 |
this.planar = true; |
| 296 |
name = name.substring(name.indexOf(name, "_")+1); |
| 297 |
this.width = parseInt(name,10); |
| 298 |
name = name.substring(name.indexOf(name, "x")+1); |
| 299 |
this.height = parseInt(name,10); |
| 300 |
name = name.substring(name.indexOf(name, "x")+1); |
| 301 |
this.maxval = 255; |
| 302 |
this.setDepth = this.setDepth(parseInt(name, 10)); |
| 303 |
} |
| 304 |
|
| 305 |
function imageView(file, event) |
| 306 |
{ |
| 307 |
var viewsdiv = document.getElementById('views'); |
| 308 |
var viewdiv = document.createElement('div'); |
| 309 |
viewsdiv.appendChild(viewdiv); |
| 310 |
|
| 311 |
var data = new imageDataBlock(event.target.result); |
| 312 |
var line = ""; |
| 313 |
if (file.name.indexOf("raw") >= 0) |
| 314 |
{ |
| 315 |
data.readRAW(file.name); |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
line = data.getLine(); |
| 320 |
if (line == "P7") |
| 321 |
data.readPAM(); |
| 322 |
else if (line == "P5") |
| 323 |
data.readPGM(); |
| 324 |
else |
| 325 |
return error("Not a PAM/PGM file!"); |
| 326 |
} |
| 327 |
|
| 328 |
var fixdiv = document.createElement('div'); |
| 329 |
fixdiv.setAttribute("style", "display:block; position:fixed; top:0px;right:0px;width:auto;height:auto;"); |
| 330 |
var text = file.name + " W="+data.width+" H="+data.height+" D="+data.depth; |
| 331 |
fixdiv.appendChild(document.createTextNode(text)); |
| 332 |
fixdiv.appendChild(document.createElement('br')); |
| 333 |
var form = document.createElement('form'); |
| 334 |
fixdiv.appendChild(form); |
| 335 |
var i; |
| 336 |
for (i=0; i < data.depth; i++) |
| 337 |
{ |
| 338 |
var span = document.createElement('span'); |
| 339 |
var button = document.createElement('input'); |
| 340 |
var label = document.createElement('label'); |
| 341 |
button.setAttribute("type", "checkbox"); |
| 342 |
button.setAttribute("name", "plane"+i); |
| 343 |
button.setAttribute("value", "yes"); |
| 344 |
button.setAttribute("onchange", "this.data.planeChange("+i+", this.checked)"); |
| 345 |
button.checked = 1; |
| 346 |
button.data = data; |
| 347 |
label.innerHTML=data.planes[i].name; |
| 348 |
span.appendChild(button); |
| 349 |
span.appendChild(label); |
| 350 |
form.appendChild(span); |
| 351 |
} |
| 352 |
viewdiv.appendChild(fixdiv); |
| 353 |
|
| 354 |
data.canvas = document.createElement('canvas'); |
| 355 |
data.canvas.setAttribute("width", data.width); |
| 356 |
data.canvas.setAttribute("height", data.height); |
| 357 |
viewdiv.appendChild(data.canvas); |
| 358 |
|
| 359 |
data.updateImage(); |
| 360 |
} |
| 361 |
|
| 362 |
function handleFileSelect(event) |
| 363 |
{ |
| 364 |
var files = event.target.files; |
| 365 |
var f, i; |
| 366 |
|
| 367 |
for (i=0; f=files[i]; i++) |
| 368 |
{ |
| 369 |
var reader = new FileReader(); |
| 370 |
reader.onload = (function (theFile) { return function (e) { imageView(theFile, e); } })(f); |
| 371 |
reader.readAsBinaryString(f); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
</script> |
| 376 |
</head> |
| 377 |
<body> |
| 378 |
<input type="file" id="files" name="files[]">Select a .pam or .pgm</input> |
| 379 |
<div id=views> |
| 380 |
<script> |
| 381 |
document.getElementById('files').addEventListener('change', handleFileSelect, false); |
| 382 |
</script> |
| 383 |
</body> |
| 384 |
</html> |