gecko/content/canvas/test/test_2d.imagedata_coercion.html

111 lines
3.6 KiB
HTML

<!DOCTYPE HTML>
<title>Canvas test: 2d.imagedata_coercion</title>
<!-- Testing: imagedata coerced correctly on set -->
<script src="/MochiKit/packed.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>
<canvas id="c" width="100" height="1"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
/* NOTE: Due to round-tripping through premultiplied values and the rounding
that ensues, values of alpha < 255 will tend to do weird things. In
particular, the premultiplied color values are computed by multiplying by a,
dividing by 255, then always rounding up. The conversion the other way is done
by multiplying by 255/a and rounding down. So if
255/a * (amount added when rounding) > 1
we will get a change in value when we go through a putImageData/getImageData cycle. Therefore, to make sure we don't have to worry about our color
channels, our alpha channel should never be < 250, unless it's 0. And when it's 0, all our color channels will come back as 0 too. */
/* Our tests. Each test has two arrays: the array of values to set and the
array of values that should read back as a result. */
var tests = [
[
[ 0, 1, 3, 250 ], [ 0, 1, 3, 250 ]
],
[
[ 0, 1, 2, 250, 4, 5, 6, 250 ], [ 0, 1, 2, 250, 4, 5, 6, 250 ]
],
[
[ 0, 1000, 2, 300, 400, 5, 600, 250 ], [ 0, 255, 2, 255, 255, 5, 255, 250 ]
],
[
[ -10, -5, NaN, 250, 4, 5, 6, -250 ], [ 0, 0, 0, 250, 0, 0, 0, 0 ]
],
[
[ 0.5, 12.2, 12.8, 251.5, 12.5, 13.5, 13.2, 250.5 ],
[ 0, 12, 13, 252, 12, 14, 13, 250 ]
],
];
function doTest(type, idx) {
var testPair = tests[idx];
var test = testPair[0];
var ref = testPair[1];
var descSuffix = " for " + type + " test #" + (idx+1);
function myIs(a, b, str) {
is(a, b, str + descSuffix);
}
myIs(test.length, ref.length, "Length mismatch");
myIs(test.length % 4, 0, "Length not a multiple of 4");
var pixels = test.length / 4;
var imageData = ctx.createImageData(pixels, 1);
myIs(imageData.width, pixels, "Incorrect created data width");
myIs(imageData.height, 1, "Incorrect created data height");
myIs(imageData.data.length, test.length,
"Incorrect length in created image data");
ctx.putImageData(imageData, 0, 0);
var testImageData = ctx.getImageData(0, 0, pixels, 1);
myIs(testImageData.data.length, test.length,
"Incorrect length in test image data after clearing pixels");
var j;
for (j = 0; j < testImageData.data.length; ++j) {
myIs(testImageData.data[j], 0,
"Nonzero value at position " + j + " in test image data " +
"after clearing pixels");
}
for (j = 0; j < imageData.data.length; ++j) {
imageData.data[j] = test[j];
}
if (type == "slow") {
// convert to a non-dense array so we can test that codepath
imageData.data.makeMeSlow = 1;
}
ctx.putImageData(imageData, 0, 0);
testImageData = ctx.getImageData(0, 0, pixels, 1);
myIs(testImageData.data.length, test.length,
"Incorrect length in test image data after putting our imagedata");
for (j = 0; j < testImageData.data.length; ++j) {
myIs(testImageData.data[j], ref[j],
"Incorrect value at position " + j + " in test image data " +
"after putting our imagedata");
}
}
function doTests(type) {
for (var i = 0; i < tests.length; ++i) {
doTest(type, i);
}
}
var canvas;
var ctx;
SimpleTest.waitForExplicitFinish();
addLoadEvent(function () {
canvas = document.getElementById('c');
ctx = canvas.getContext('2d');
doTests("fast");
doTests("slow")
SimpleTest.finish();
});
</script>