File: tests.html

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (305 lines) | stat: -rw-r--r-- 8,837 bytes parent folder | download | duplicates (6)
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
296
297
298
299
300
301
302
303
304
305
<!--
Copyright 2019 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!doctype html>
<head>
 <title>piex wasm raw image preview / thumbnail test page</title>
 <link type="text/css" rel="stylesheet" href="/tests.css">
</head>

<body>
  <button onclick=runTest()>Run Test</button>
</body>

<script>
  class ImageBuffer {
    constructor(buffer) {
      this.source = new Uint8Array(buffer);
      this.length = buffer.byteLength;
    }

    process() {
      this.memory = Module._malloc(this.length);
      if (!this.memory)
        throw new Error('image malloc failure');

      Module.HEAP8.set(this.source, this.memory);
      this.result = Module.image(this.memory, this.length);

      return this.result;
    }

    preview() {
      let preview = this.result ? this.result.preview : null;
      if (!preview || this.result.error)
        return null;

      if (preview.format != 0)
        throw new Error('preview images should be JPEG format');

      const offset = preview.offset;
      const length = preview.length;
      if (offset > this.length || (this.length - offset) < length)
        throw new Error('failed to extract preview');

      const view = new Uint8Array(this.source.buffer, offset, length);
      preview.data = new Uint8Array(view);
      preview.type = 'preview';

      return preview;
    }

    thumbnail() {
      let thumbnail = this.result ? this.result.thumbnail : null;
      if (!thumbnail || this.result.error)
        return null;

      const offset = thumbnail.offset;
      const length = thumbnail.length;
      if (offset > this.length || (this.length - offset) < length)
        throw new Error('failed to extract thumbnail');

      const view = new Uint8Array(this.source.buffer, offset, length);
      thumbnail.data = new Uint8Array(view);
      if (thumbnail.format == 1)  // RGB
        thumbnail.size = thumbnail.width * thumbnail.height * 3;
      thumbnail.type = 'thumbnail';

      return thumbnail;
    }

    details() {
      const details = this.result ? this.result.details : null;
      if (!details || this.result.error)
        return null;

      let format = {};
      for (const [key, value] of Object.entries(details)) {
        if (typeof value === 'string') {
          format[key] = value.replace(/\0+$/, '').trim();
        } else if (typeof value === 'number') {
          if (!Number.isInteger(value)) {
            format[key] = Number(value.toFixed(3).replace(/0+$/, ''));
          } else {
            format[key] = value;
          }
        }
      }

      return JSON.stringify(format);
    }

    close() {
      Module._free(this.memory);
    }
  }

  function createFileSystem(images) {
    return new Promise((resolve, reject) => {
      document.title = 'createFileSystem';

      function failed(error) {
        reject(new Error('Creating file system: ' + error));
      }

      function createdFileSystem(fileSystem) {
        console.log('test: created file system', fileSystem.name);
        window.fileSystem = fileSystem;
        resolve();
      }

      const bytes = images * 30 * 1024 * 1024;  // 30M per image.
      window.webkitRequestFileSystem(
          window.TEMPORARY, bytes, createdFileSystem, failed);
    });
  }

  function writeToFileSystem(image) {
    return new Promise(async (resolve, reject) => {
      document.title = image;

      const buffer = await fetch(image).then((response) => {
        if (!response.ok)
          throw new Error('Failed to fetch image: ' + image);
        return response.arrayBuffer();
      }).catch(reject);

      function failure(error) {
        reject(new Error('Writing file system: ' + error));
      }

      function writeEntry(fileEntry) {
        fileEntry.createWriter((writer) => {
          writer.onerror = failure;
          writer.onwrite = resolve;
          writer.write(new Blob([buffer]));
        }, failure);
      }

      window.fileSystem.root.getFile(
          image.replace('images/', ''), {create: true}, writeEntry, failure);
    });
  }

  function readFromFileSystem(image) {
    return new Promise((resolve, reject) => {
      document.title = image;

      function failure(error) {
        reject(new Error('Reading file system: ' + error));
      }

      function invalid(size) {
        return size <= 0 || size >= Math.pow(2, 30);
      }

      function readEntry(fileEntry) {
        fileEntry.file((file) => {
          if (invalid(file.size))
            return failure('invalid file size');
          const reader = new FileReader();
          reader.onerror = failure;
          reader.onload = () => resolve(reader.result);
          reader.readAsArrayBuffer(file);
        }, failure);
      }

      window.fileSystem.root.getFile(
          image.replace('images/', ''), {}, readEntry, failure);
    });
  }

  function hashUint8Array(data, hash = ~0) {
    for (let i = 0; i < data.byteLength; ++i)
      hash = (hash << 5) - hash + data[i];
    return Math.abs(hash).toString(16);
  }

  function renderJPG(name, image) {
    const data = image.data;

    let renderer = new Image();
    renderer.onerror = renderer.onload = (event) => {
      if (renderer.width > (window.screen.availWidth / 2))
        renderer.classList.add('zoom');
      document.body.appendChild(renderer);
      URL.revokeObjectURL(renderer.src);
      if (--window.images_ <= 0)
        document.title = 'DONE';
    };

    renderer.src = URL.createObjectURL(new Blob([data]));
    ++window.images_;
  }

  function renderRGB(name, image) {
    const data = image.data;

    let canvas = document.createElement('canvas');
    canvas.width = image.width;
    canvas.height = image.height;
    if (image.width > (window.screen.availWidth / 2))
      canvas.classList.add('zoom');

    // Create imageData from the image RGB data.
    let context = canvas.getContext('2d');
    let imageData = context.createImageData(image.width, image.height);
    for (let i = 0, j = 0; i < image.size; i += 3, j += 4) {
      imageData.data[j + 0] = data[i + 0]; // R
      imageData.data[j + 1] = data[i + 1]; // G
      imageData.data[j + 2] = data[i + 2]; // B
      imageData.data[j + 3] = 255;         // A
    }

    // Render the imageData.
    context.putImageData(imageData, 0, 0);
    document.body.appendChild(canvas);
  }

  function renderResult(name, image) {
    if (!image)
      return;

    const hash = hashUint8Array(image.data);
    const data = image.data;
    image.data = undefined;
    const result = JSON.stringify(image);
    console.log('test:', name, image.type, 'hash', hash, result);
    image.data = data;

    if (image.format == 0)
      return renderJPG(name, image);
    if (image.format == 1)
      return renderRGB(name, image);
  }

  function renderDetails(name, details) {
    if (!details)
      return;

    const text = new TextEncoder('UTF-8').encode(details);
    const hash = hashUint8Array(text);

    console.log('test:', name, 'details hash', hash, details);
  }

  window.onload = function loadPiexModule() {
    let script = document.createElement('script');
    document.head.appendChild(script);
    script.src = '/piex.js.wasm';
    script.onload = () => {
      createPiexModule().then(module => {
        window.Module = module;
        document.title = 'READY';
      });
    };
  };

  window.onerror = (error) => {
    console.log('test: FAIL', error, '\n');
    document.title = 'DONE';
  };

  async function runTest(image = 'images/SONY_A500_01.ARW') {
    // Start the test of image.
    document.title = image;
    console.log('test:', image);
    document.body.innerHTML = `<pre>${image}</pre>`;

    // Fetch the image in an array buffer.
    let time = window.performance.now();
    const buffer = await readFromFileSystem(image).catch(onerror);
    if (!buffer)
      return;

    let imageBuffer = new ImageBuffer(buffer);
    const fetched = window.performance.now() - time;

    // Extract the preview|thumbnail images, render them.
    return new Promise((resolve) => {
      resolve(imageBuffer.process());
    }).then((result) => {
      let preview = imageBuffer.preview();
      let thumb = imageBuffer.thumbnail();
      let details = imageBuffer.details();
      imageBuffer.close();
      time = window.performance.now() - time;
      window.images_ = 0;
      renderDetails(image, details);
      renderResult(image, preview);
      renderResult(image, thumb);
      console.log('test: done',
        time.toFixed(3), fetched.toFixed(3), (time - fetched).toFixed(3));
      window.testTime += time;
      console.log('\n');
      if (!window.images_)
        document.title = 'DONE';
    }).catch((error) => {
      imageBuffer.close();
      console.log(error);
      document.title = 'DONE';
    });
  }
</script>