File: createImageBitmap-premultiplyAlpha.html

package info (click to toggle)
firefox 145.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,528 kB
  • sloc: cpp: 7,594,999; javascript: 6,459,658; ansic: 3,752,909; python: 1,403,455; xml: 629,809; asm: 438,679; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (92 lines) | stat: -rw-r--r-- 3,468 bytes parent folder | download | duplicates (12)
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
<!DOCTYPE html>
<html>
<title>createImageBitmap + premultiplyAlpha test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<script src="/common/media.js"></script>
<script src="common.sub.js"></script>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<body>
<canvas id="TWOD"></canvas>
<script>

const kWidth = 100;
const kHeight = 100;
const kTolerance = 2;
// The test pixel value (as unpremultiplied 8-bit sRGB).
const kTestPixel = [64, 128, 255, 128];

// Create an ImageData object with the test pixel value.
let createFromImageData = function(imageDataSettings, imageBitmapOptions) {
  let imageData = new ImageData(kWidth, kHeight);
  for (let i = 0; i < kWidth*kHeight; ++i) {
    imageData.data[4*i + 0] = kTestPixel[0];
    imageData.data[4*i + 1] = kTestPixel[1];
    imageData.data[4*i + 2] = kTestPixel[2];
    imageData.data[4*i + 3] = kTestPixel[3];
  }
  return createImageBitmap(imageData, imageBitmapOptions);
}

// Create a canvas that is cleared with the test pixel value.
let createFromCanvas = function(canvasSettings, imageBitmapOptions) {
  let canvas = document.createElement("canvas");
  canvas.width = kWidth;
  canvas.height = kHeight;
  let ctx = canvas.getContext("2d", canvasSettings);
  ctx.fillStyle = 'rgba(' + kTestPixel[0] + ', ' +
                            kTestPixel[1] + ', ' +
                            kTestPixel[2] + ', ' +
                            kTestPixel[3]/255 + ')'
  ctx.fillRect(0, 0, kWidth, kHeight);
  return createImageBitmap(canvas, imageBitmapOptions);
}

// Ensure that the bitmap has the pixel value common to all tests.
let testImageBitmap = function(imageBitmap) {
  let canvas = document.createElement("canvas");
  canvas.width = kWidth;
  canvas.height = kHeight;
  let ctx = canvas.getContext("2d");
  ctx.drawImage(imageBitmap, 0, 0);
  _assertPixelApprox(canvas, 0, 0, kTestPixel[0], kTestPixel[1], kTestPixel[2], kTestPixel[3], kTolerance);
}

// The test will use the `factory` methods in `factories` to create promises
// that will create an ImageBitmap (e.g, from ImageData, variously-configured
// Canvas2D, etc). The `settings` method sets parameters for the factory.
let factories = [
  { factory:createFromImageData, settings:{}, desc:'ImageData' },
  { factory:createFromCanvas,    settings:{}, desc:'Canvas2D'  },
  { factory:createFromCanvas,    settings:{willReadFrequently:true},
    desc:'Canvas2D willReadFrequently:true'  },
  { factory:createFromCanvas,    settings:{willReadFrequently:false},
    desc:'Canvas2D willReadFrequently:false'  }
];

// For all of the above configurations, create an ImageBitmap with the
// indicated ImageBitmapOptions.
let imageBitmapOptions = [
  { options:{premultiplyAlpha:"none"},        desc:'unpremultiplied' },
  { options:{premultiplyAlpha:"premultiply"}, desc:'premultiplied'   },
  { options:{premultiplyAlpha:"default"},     desc:'default'         }
];

for (f of factories) {
  for (o of imageBitmapOptions) {
    let factory = f.factory;
    let settings = f.settings;
    let options = o.options;
    promise_test(
        function() {
          return factory(settings, options).then(imageBitmap => {
            testImageBitmap(imageBitmap);
          });
        },
        'createImageBitmap: from ' + f.desc + ', ' + o.desc + ', drawn to canvas');
  }
}
</script>
</body>
</html>