summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/textures/misc/exif-orientation.html
blob: 5a4b88e5b8ed601246b9ac4ed1ad3c3ef4bd6dad (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
<!--
Copyright (c) 2020 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Verifies EXIF orientation is respected when uploading images to WebGL textures</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"></script>
<script src="../../../js/tests/tex-image-and-sub-image-utils.js"></script>
</head>
<body onload="run()">
<canvas id="c" width="256" height="256"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description();
let wtu = WebGLTestUtils;
let tiu = TexImageUtils;
let canvas = document.getElementById("c");
let gl = wtu.create3DContext(canvas);
let program = tiu.setupTexturedQuad(gl, gl.RGBA);
const resourcePath = "../../../resources/";
const tolerance = 5;

// The locations are written assuming flipY = false. For flipY = true, y = 1.0-y.
const expectedColors = {
    top:    { location: [ 0.5, 0.25 ], color: [ 255,   0,   0 ] },
    left:   { location: [ 0.4, 0.5  ], color: [   0,   0, 255 ] },
    right:  { location: [ 0.6, 0.5  ], color: [ 255, 255,   0 ] },
    bottom: { location: [ 0.5, 0.75 ], color: [   0, 255,   0 ] },
}

function output(str)
{
    debug(str);
    bufferedLogToConsole(str);
}

function checkPixels(flipY)
{
    for (let place in expectedColors) {
        let color = expectedColors[place];
        let loc = color.location;
        let x = loc[0];
        let y = (flipY ? 1.0 - loc[1] : loc[1]);
        output("  Checking " + place);
        wtu.checkCanvasRect(gl, Math.floor(canvas.width * x), Math.floor(canvas.height * y), 1, 1,
                            color.color, "shouldBe " + color.color + " +/-" + tolerance, tolerance);
    }
}

async function testImageBitmapWithFlipY(source, flipY)
{
    const bitmap = await createImageBitmap(source, flipY ? {imageOrientation: flipY} : undefined);

    output("  Testing texImage2D, flipY = " + flipY);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
    wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
    checkPixels(flipY == "flipY");

    output("  Testing texSubImage2D, flipY = " + flipY);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, bitmap.width, bitmap.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
    wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
    checkPixels(flipY == "flipY");
}

async function testImageBitmapFromBlob(filename)
{
    let response = await fetch(resourcePath + filename);
    let blob = await response.blob();
    output("----------------------------------------------------------------");
    output("Testing " + filename + " via ImageBitmap from Blob");
    await testImageBitmapWithFlipY(blob, "flipY");
    await testImageBitmapWithFlipY(blob, "none");
    await testImageBitmapWithFlipY(blob, undefined);
}

async function testImageBitmapFromImage(image)
{
    await testImageBitmapWithFlipY(image, "flipY");
    await testImageBitmapWithFlipY(image, "none");
    await testImageBitmapWithFlipY(image, undefined);
}

async function testImageElementWithFlipY(image, flipY)
{
    output("  Testing texImage2D, flipY = " + flipY);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
    wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
    checkPixels(flipY);

    output("  Testing texSubImage2D, flipY = " + flipY);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
    wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
    checkPixels(flipY);
}

async function testImageElement(filename)
{
    let image = new Image();
    image.src = resourcePath + filename;
    await image.decode();

    output("----------------------------------------------------------------");
    output("Testing " + filename + " via HTMLImageElement");

    await testImageElementWithFlipY(image, true);
    await testImageElementWithFlipY(image, false);

    output("----------------------------------------------------------------");
    output("Testing " + filename + " via ImageBitmap from HTMLImageElement");

    await testImageBitmapFromImage(image);
}

async function testSingleImage(filename)
{
    await testImageBitmapFromBlob(filename);
    await testImageElement(filename);
}

async function run()
{
    let tex = gl.createTexture();
    // Bind the texture to the default texture unit 0
    gl.bindTexture(gl.TEXTURE_2D, tex);
    // Set up texture parameters
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

    const filenames = [
        "exif-orientation-test-1-normal.jpg",
        "exif-orientation-test-2-mirror-horizontal.jpg",
        "exif-orientation-test-3-rotate-180.jpg",
        "exif-orientation-test-4-mirror-vertical.jpg",
        "exif-orientation-test-5-mirror-horizontal-90-ccw.jpg",
        "exif-orientation-test-6-90-cw.jpg",
        "exif-orientation-test-7-mirror-horizontal-90-cw.jpg",
        "exif-orientation-test-8-90-ccw.jpg",
    ];

    for (let fn of filenames) {
        await testSingleImage(fn);
    }

    finishTest();
}

var successfullyParsed = true;
</script>
</body>
</html>