summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/element/manual/imagebitmap/common.sub.js
blob: 1889035202287d8ee8e6e192647826c702b60be5 (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
function makeCanvas() {
    return new Promise(resolve => {
        var testCanvas = document.createElement("canvas");
        testCanvas.width = 20;
        testCanvas.height = 20;
        var testCtx = testCanvas.getContext("2d");
        testCtx.fillStyle = "rgb(255, 0, 0)";
        testCtx.fillRect(0, 0, 10, 10);
        testCtx.fillStyle = "rgb(0, 255, 0)";
        testCtx.fillRect(10, 0, 10, 10);
        testCtx.fillStyle = "rgb(0, 0, 255)";
        testCtx.fillRect(0, 10, 10, 10);
        testCtx.fillStyle = "rgb(0, 0, 0)";
        testCtx.fillRect(10, 10, 10, 10);
        resolve(testCanvas);
    });
}

function makeOffscreenCanvas() {
    return new Promise(resolve => {
        let canvas = new OffscreenCanvas(20, 20);
        var testCtx = canvas.getContext("2d");
        testCtx.fillStyle = "rgb(255, 0, 0)";
        testCtx.fillRect(0, 0, 10, 10);
        testCtx.fillStyle = "rgb(0, 255, 0)";
        testCtx.fillRect(10, 0, 10, 10);
        testCtx.fillStyle = "rgb(0, 0, 255)";
        testCtx.fillRect(0, 10, 10, 10);
        testCtx.fillStyle = "rgb(0, 0, 0)";
        testCtx.fillRect(10, 10, 10, 10);
        resolve(canvas);
    });
}

var imageBitmapVideoPromise = new Promise(function(resolve, reject) {
    var video = document.createElement("video");
    video.oncanplaythrough = function() {
        resolve(video);
    };
    video.onerror = reject;

    // preload=auto is required to ensure a frame is available once
    // canplaythrough is fired. The default of preload=metadata does not
    // gaurantee this.
    video.preload = "auto";
    video.src = getVideoURI("/images/pattern");

    // Prevent WebKit from garbage collecting event handlers.
    window._video = video;
});

function makeVideo() {
    return imageBitmapVideoPromise;
}

var imageBitmapDataUrlVideoPromise = fetch(getVideoURI("/images/pattern"))
    .then(response => Promise.all([response.headers.get("Content-Type"), response.arrayBuffer()]))
    .then(([type, data]) => {
        return new Promise(function(resolve, reject) {
            var video = document.createElement("video");
            video.oncanplaythrough = function() {
                resolve(video);
            };
            video.onerror = reject;

            var encoded = btoa(String.fromCodePoint(...new Uint8Array(data)));
            var dataUrl = `data:${type};base64,${encoded}`;

            // preload=auto is required to ensure a frame is available once
            // canplaythrough is fired. The default of preload=metadata does not
            // gaurantee this.
            video.preload = "auto";
            video.src = dataUrl;

            // Prevent WebKit from garbage collecting event handlers.
            window._dataVideo = video;
        });
    });

function makeDataUrlVideo() {
    return imageBitmapDataUrlVideoPromise;
}

function makeMakeHTMLImage(src) {
    return function() {
        return new Promise((resolve, reject) => {
            var img = new Image();
            img.onload = function() {
                resolve(img);
            };
            img.onerror = reject;
            img.src = src;
        });
    }
}

function makeMakeSVGImage(src) {
    return function() {
        return new Promise((resolve, reject) => {
            var image = document.createElementNS("http://www.w3.org/2000/svg", "image");
            image.onload = () => resolve(image);
            image.onerror = reject;
            image.setAttribute("externalResourcesRequired", "true");
            image.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:href', src);
            document.body.appendChild(image);
        });
    }
}

function makeImageData() {
    return new Promise(function(resolve, reject) {
        var width = 20, height = 20;
        var imgData = new ImageData(width, height);
        for (var i = 0; i < width * height * 4; i += 4) {
            imgData.data[i] = 0;
            imgData.data[i + 1] = 0;
            imgData.data[i + 2] = 0;
            imgData.data[i + 3] = 255; //alpha channel: 255
        }
        var halfWidth = width / 2;
        var halfHeight = height / 2;
        // initialize to R, G, B, Black, with each one 10*10 pixels
        for (var i = 0; i < halfHeight; i++)
            for (var j = 0; j < halfWidth; j++)
                imgData.data[i * width * 4 + j * 4] = 255;
        for (var i = 0; i < halfHeight; i++)
            for (var j = halfWidth; j < width; j++)
                imgData.data[i * width * 4 + j * 4 + 1] = 255;
        for (var i = halfHeight; i < height; i++)
            for (var j = 0; j < halfWidth; j++)
                imgData.data[i * width * 4 + j * 4 + 2] = 255;
        resolve(imgData);
    });
}

function makeImageBitmap() {
    return makeCanvas().then(canvas => {
        return createImageBitmap(canvas);
    });
}

function makeBlob(src) {
    return function () {
        return new Promise(function(resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", src);
            xhr.responseType = 'blob';
            xhr.send();
            xhr.onload = function() {
                resolve(xhr.response);
            };
        });
    }
}

var imageSourceTypes = [
    { name: 'an HTMLCanvasElement', factory: makeCanvas },
    { name: 'an HTMLVideoElement',  factory: makeVideo },
    { name: 'an HTMLVideoElement from a data URL', factory: makeDataUrlVideo },
    { name: 'a bitmap HTMLImageElement', factory: makeMakeHTMLImage("/images/pattern.png") },
    { name: 'a vector HTMLImageElement', factory: makeMakeHTMLImage("/images/pattern.svg") },
    { name: 'a bitmap SVGImageElement', factory: makeMakeSVGImage("/images/pattern.png") },
    { name: 'a vector SVGImageElement', factory: makeMakeSVGImage("/images/pattern.svg") },
    { name: 'an OffscreenCanvas',   factory: makeOffscreenCanvas },
    { name: 'an ImageData',         factory: makeImageData },
    { name: 'an ImageBitmap',       factory: makeImageBitmap },
    { name: 'a Blob',               factory: makeBlob("/images/pattern.png") },
];