summaryrefslogtreecommitdiff
path: root/scripts/qpa_image_viewer.html
blob: f666d774fd18214f36edd82550532ad5d389de8a (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!--
--------------------------------------
HTML QPA Image Viewer
--------------------------------------

Copyright (c) 2020 The Khronos Group Inc.
Copyright (c) 2020 Valve Corporation.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
    <head>
        <meta charset="utf-8"/>
        <title>Load PNGs from QPA output</title>
        <style>
            body {
                background: white;
                text-align: left;
                font-family: sans-serif;
            }
            h1 {
                margin-top: 2ex;
            }
            h2 {
                font-size: large;
            }
            figure {
                display: flex;
                flex-direction: column;
            }
            img {
                margin-right: 1ex;
                margin-bottom: 1ex;
                /* Attempt to zoom images using the nearest-neighbor scaling
                algorithm. */
                image-rendering: pixelated;
                image-rendering: crisp-edges;
                /* Use a black background color for images in case some pixels
                are transparent to some degree. In the worst case, the image
                could appear to be missing. */
                background: black;
            }
            button {
                margin: 1ex;
                border: none;
                border-radius: .5ex;
                padding: 1ex;
                background-color: steelblue;
                color: white;
                font-size: large;
            }
            button:hover {
                opacity: .8;
            }
            #clearimagesbutton,#cleartextbutton {
                background-color: seagreen;
            }
            select {
                font-size: large;
                padding: 1ex;
                border-radius: .5ex;
                border: 1px solid darkgrey;
            }
            select:hover {
                opacity: .8;
            }
            .loadoption {
                text-align: center;
                margin: 1ex;
                padding: 2ex;
                border: 1px solid darkgrey;
                border-radius: 1ex;
            }
            #options {
                display: flex;
                flex-wrap: wrap;
            }
            #qpatext {
                display: block;
                min-width: 80ex;
                max-width: 132ex;
                min-height: 25ex;
                max-height: 25ex;
                margin: 1ex auto;
            }
            #fileselector {
                display: none;
            }
            #zoomandclear {
                margin: 2ex;
            }
            #images {
                margin: 2ex;
                display: flex;
                flex-direction: column;
            }
            .imagesblock {
                display: flex;
                flex-wrap: wrap;
            }
        </style>
    </head>
    <body>
        <h1>Load PNGs from QPA output</h1>

        <div id="options">
            <div class="loadoption">
                <h2>Option 1: Load local QPA files</h2>
                <!-- The file selector text cannot be changed, so we use a hidden selector trick. -->
                <button id="fileselectorbutton">&#x1F4C2; Load files</button>
                <input id="fileselector" type="file" multiple>
            </div>

            <div class="loadoption">
                <h2>Option 2: Paste QPA text or text extract containing &lt;Image&gt; elements below and click "Load images"</h2>
                <textarea id="qpatext"></textarea>
                <button id="loadimagesbutton">&#x1F4C3; Load images</button>
                <button id="cleartextbutton">&#x267B; Clear text</button>
            </div>
        </div>

        <div id="zoomandclear">
            &#x1F50E; Image zoom
            <select id="zoomselect">
                <option value="1" selected>1x</option>
                <option value="2">2x</option>
                <option value="4">4x</option>
                <option value="8">8x</option>
                <option value="16">16x</option>
                <option value="32">32x</option>
            </select>
            <button id="clearimagesbutton">&#x267B; Clear images</button>
        </div>

        <div id="images"></div>

        <script>
            // Returns zoom factor as a number.
            var getSelectedZoom = function () {
                return new Number(document.getElementById("zoomselect").value);
            }

            // Scales a given image with the selected zoom factor.
            var scaleSingleImage = function (img) {
                var factor = getSelectedZoom();
                img.style.width = (img.naturalWidth * factor) + "px";
                img.style.height = (img.naturalHeight * factor) + "px";
            }

            // Rescales all <img> elements in the page. Used after changing the selected zoom.
            var rescaleImages = function () {
                var imageList = document.getElementsByTagName("img");
                for (var i = 0; i < imageList.length; i++) {
                    scaleSingleImage(imageList[i])
                }
            }

            // Removes everything contained in the images <div>.
            var clearImages = function () {
                var imagesNode = document.getElementById("images");
                while (imagesNode.hasChildNodes()) {
                    imagesNode.removeChild(imagesNode.lastChild);
                }
            }

            // Clears textarea text.
            var clearText = function() {
                document.getElementById("qpatext").value = "";
            }

            // Returns a properly sized image with the given base64-encoded PNG data.
            var createImage = function (pngData, imageName) {
                var imageContainer = document.createElement("figure");
                if (imageName.length > 0) {
                    var newFileNameHeader = document.createElement("figcaption");
                    newFileNameHeader.textContent = escape(imageName);
                    imageContainer.appendChild(newFileNameHeader);
                }
                var newImage = document.createElement("img");
                newImage.src = "data:image/png;base64," + pngData;
                newImage.onload = (function () {
                    // Grab the image for the callback. We need to wait until
                    // the image has been properly loaded to access its
                    // naturalWidth and naturalHeight properties, needed for
                    // scaling.
                    var cbImage = newImage;
                    return function () {
                        scaleSingleImage(cbImage);
                    };
                })();
                imageContainer.appendChild(newImage);
                return imageContainer;
            }

            // Returns a new h3 header with the given file name.
            var createFileNameHeader = function (fileName) {
                var newHeader = document.createElement("h3");
                newHeader.textContent = fileName;
                return newHeader;
            }

            // Returns a new image block to contain images from a file.
            var createImagesBlock = function () {
                var imagesBlock = document.createElement("div");
                imagesBlock.className = "imagesblock";
                return imagesBlock;
            }

            // Processes a chunk of QPA text from the given file name. Creates
            // the file name header and a list of images in the images <div>, as
            // found in the text.
            var processText = function(textString, fileName) {
                var imagesNode = document.getElementById("images");
                var newHeader = createFileNameHeader(fileName);
                imagesNode.appendChild(newHeader);
                var imagesBlock = createImagesBlock();
                // [\s\S] is a match-anything regexp like the dot, except it
                // also matches newlines. Ideally, browsers would need to widely
                // support the "dotall" regexp modifier, but that's not the case
                // yet and this does the trick.
                // Group 1 are the image element properties, if any.
                // Group 2 is the base64 PNG data.
                var imageRegexp = /<Image\b(.*?)>([\s\S]*?)<\/Image>/g;
                var imageNameRegexp = /\bName="(.*?)"/;
                var result;
                var innerResult;
                var imageName;
                while ((result = imageRegexp.exec(textString)) !== null) {
                    innerResult = result[1].match(imageNameRegexp);
                    imageName = ((innerResult !== null) ? innerResult[1] : "");
                    // Blanks need to be removed from the base64 string.
                    var pngData = result[2].replace(/\s+/g, "");
                    imagesBlock.appendChild(createImage(pngData, imageName));
                }
                imagesNode.appendChild(imagesBlock);
            }

            // Loads images from the text in the text area.
            var loadImages = function () {
                processText(document.getElementById("qpatext").value, "<Pasted Text>");
            }

            // Loads images from the files in the file selector.
            var handleFileSelect = function (evt) {
                var files = evt.target.files;
                for (var i = 0; i < files.length; i++) {
                    // Creates a reader per file.
                    var reader = new FileReader();
                    // Grab the needed objects to use them after the file has
                    // been read, in order to process its contents and add
                    // images, if found, in the images <div>.
                    reader.onload = (function () {
                        var cbFileName = files[i].name;
                        var cbReader = reader;
                        return function () {
                            processText(cbReader.result, cbFileName);
                        };
                    })();
                    // Reads file contents. This will trigger the event above.
                    reader.readAsText(files[i]);
                }
            }

            // File selector trick: click on the selector when clicking on the
            // custom button.
            var clickFileSelector = function () {
                document.getElementById("fileselector").click();
            }

            // Clears selected files to be able to select them again if needed.
            var clearSelectedFiles = function() {
                document.getElementById("fileselector").value = "";
            }

            // Set event handlers for interactive elements in the page.
            document.getElementById("fileselector").onclick = clearSelectedFiles;
            document.getElementById("fileselector").addEventListener("change", handleFileSelect, false);
            document.getElementById("fileselectorbutton").onclick = clickFileSelector;
            document.getElementById("loadimagesbutton").onclick = loadImages;
            document.getElementById("cleartextbutton").onclick = clearText;
            document.getElementById("zoomselect").onchange = rescaleImages;
            document.getElementById("clearimagesbutton").onclick = clearImages;
        </script>
    </body>
</html>