Skip to content

Commit e788665

Browse files
Merge pull request #14167 from stbensonimoh/convert-pdf-viewer-to-async-await
Convert examples/image_decoders/jpeg_viewer.js to await/async #14123
2 parents e4e4412 + f263c86 commit e788665

File tree

1 file changed

+35
-54
lines changed

1 file changed

+35
-54
lines changed

examples/image_decoders/jpeg_viewer.js

+35-54
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,39 @@ const JPEG_IMAGE = "fish.jpg";
2525
const jpegCanvas = document.getElementById("jpegCanvas");
2626
const jpegCtx = jpegCanvas.getContext("2d");
2727

28-
// Load the image data, and convert it to a Uint8Array.
29-
//
30-
let nonBinaryRequest = false;
31-
const request = new XMLHttpRequest();
32-
request.open("GET", JPEG_IMAGE, false);
33-
try {
34-
request.responseType = "arraybuffer";
35-
nonBinaryRequest = request.responseType !== "arraybuffer";
36-
} catch (e) {
37-
nonBinaryRequest = true;
38-
}
39-
if (nonBinaryRequest && request.overrideMimeType) {
40-
request.overrideMimeType("text/plain; charset=x-user-defined");
41-
}
42-
request.send(null);
43-
44-
let typedArrayImage;
45-
if (nonBinaryRequest) {
46-
const str = request.responseText,
47-
length = str.length;
48-
const bytes = new Uint8Array(length);
49-
for (let i = 0; i < length; ++i) {
50-
bytes[i] = str.charCodeAt(i) & 0xff;
28+
(async function () {
29+
// Load the image data, and convert it to a Uint8Array.
30+
//
31+
const response = await fetch(JPEG_IMAGE);
32+
if (!response.ok) {
33+
throw new Error(response.statusText);
5134
}
52-
typedArrayImage = bytes;
53-
} else {
54-
typedArrayImage = new Uint8Array(request.response);
55-
}
56-
57-
// Parse the image data using `JpegImage`.
58-
//
59-
const jpegImage = new pdfjsImageDecoders.JpegImage();
60-
jpegImage.parse(typedArrayImage);
61-
62-
const width = jpegImage.width,
63-
height = jpegImage.height;
64-
const jpegData = jpegImage.getData({
65-
width,
66-
height,
67-
forceRGB: true,
68-
});
69-
70-
// Render the JPEG image on a <canvas>.
71-
//
72-
const imageData = jpegCtx.createImageData(width, height);
73-
const imageBytes = imageData.data;
74-
for (let j = 0, k = 0, jj = width * height * 4; j < jj; ) {
75-
imageBytes[j++] = jpegData[k++];
76-
imageBytes[j++] = jpegData[k++];
77-
imageBytes[j++] = jpegData[k++];
78-
imageBytes[j++] = 255;
79-
}
80-
jpegCanvas.width = width;
81-
jpegCanvas.height = height;
82-
jpegCtx.putImageData(imageData, 0, 0);
35+
const typedArrayImage = new Uint8Array(await response.arrayBuffer());
36+
37+
// Parse the image data using `JpegImage`.
38+
//
39+
const jpegImage = new pdfjsImageDecoders.JpegImage();
40+
jpegImage.parse(typedArrayImage);
41+
42+
const width = jpegImage.width,
43+
height = jpegImage.height;
44+
const jpegData = jpegImage.getData({
45+
width,
46+
height,
47+
forceRGB: true,
48+
});
49+
50+
// Render the JPEG image on a <canvas>.
51+
//
52+
const imageData = jpegCtx.createImageData(width, height);
53+
const imageBytes = imageData.data;
54+
for (let j = 0, k = 0, jj = width * height * 4; j < jj; ) {
55+
imageBytes[j++] = jpegData[k++];
56+
imageBytes[j++] = jpegData[k++];
57+
imageBytes[j++] = jpegData[k++];
58+
imageBytes[j++] = 255;
59+
}
60+
jpegCanvas.width = width;
61+
jpegCanvas.height = height;
62+
jpegCtx.putImageData(imageData, 0, 0);
63+
})();

0 commit comments

Comments
 (0)