-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppmReader.html
143 lines (123 loc) · 4.66 KB
/
ppmReader.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html>
<html>
<head>
<title>PPM Viewer</title>
<meta charset="UTF-8">
<style type="text/css">
body {
background-color: black;
}
form, div, p {
text-align:center;
color: white;
}
div {
margin-top: 5px;
margin-bottom: 5px;
}
form,div {
margin-left:auto;
margin-right:auto;
}
#instruct {
}
#name {
font-size: 1.4em;
font-weight: bold;
}
#errorDiv {
text-align:center;
}
.error {
display:inline-block;
text-align:left;
margin-left:auto;
margin-right:auto;
font-weight:bold;
}
</style>
<script type="text/javascript">
var reloadButton;
var canvas;
var ctx;
function showError(msg) {
var errorDiv = document.getElementById("errorDiv");
errorDiv.innerHTML = '<div class="error">Error: ' + msg + '</div>';
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function processPPM(fileContents) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
fileContents = fileContents.replace(/^\s+/, '').replace(/\s+$/, '');
var data = fileContents.split(/\s+/);
if (fileContents.substr(0, 2) != 'P3' || data[0] != 'P3') {
showError('File is not a PPM');
return;
}
var width = data[1];
var height = data[2];
var maxColors = data[3];
if (data[3] != 255) {
showError('MaxColors is not 255');
return;
}
if (data.length != 3 * width * height + 4) {
showError('Not enough pixel data.<br>'
+ 'Found: ' + (data.length - 4) + '<br>'
+ 'Expecting: ' + (3 * width * height) + '<br>'
+ 'Based on width = ' + width
+ ' and height = ' + height);
return;
}
errorDiv.innerHTML = '';
canvas.width=width;
canvas.height=height;
var img = ctx.getImageData(0, 0, width, height);
var pixels = img.data;
var imageIndex = 0;
for (var i = 4; i < data.length; i += 3) {
pixels[imageIndex++] = data[i]; // r
pixels[imageIndex++] = data[i+1]; // g
pixels[imageIndex++] = data[i+2]; // b
pixels[imageIndex++] = 255; // a
}
ctx.putImageData(img, 0, 0);
reloadButton.disabled = false;
}
function processFiles(files) {
if (! reloadButton) {
reloadButton = document.getElementById("reloadBtn");
}
if (! canvas) {
canvas = document.getElementById("imageCanvas");
ctx = canvas.getContext("2d");
}
reloadButton.disabled = true;
var file = files[0];
var filenameDiv = document.getElementById("filenameDiv");
filenameDiv.innerHTML = "File: " + file.name;
if (file.name.substr(file.name.length-4) != ".ppm") {
showError('file name does not end with ".ppm"');
return
}
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
processPPM(contents);
}
r.readAsText(file);
}
</script>
</head>
<body>
<div id="name">PPM Viewer</div>
<div id="instruct">Choose a PPM image to view</div>
<form name="fileForm" id="fileForm">
<input type="file" name="filedata" id="filedata" onchange="processFiles(this.files);">
<br>
<button id="reloadBtn" onclick="processFiles(this.form.filedata.files); return false;" disabled>Reload image</button>
</form>
<div id="filenameDiv"></div>
<div id="errorDiv"></div>
<div><canvas id="imageCanvas" width="100" height="100"></canvas></div>
</body>
</html>