tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

canvas-display-p3-drawImage-ImageBitmap-ImageData.html (2250B)


      1 <!DOCTYPE HTML>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="canvas-display-p3.js"></script>
      5 <script>
      6 // Test that ImageBitmaps created from ImageData sources with different color
      7 // spaces can be drawn into sRGB and Display P3 canvases, by reading pixels with
      8 // getImageData() as sRGB and Display P3 values.
      9 function runTest(sourceColorSpace, destinationColorSpace, colors) {
     10    for (let [sourceColorString, expectedColor] of Object.entries(colors)) {
     11        for (let cropSource of [false, true]) {
     12            async_test(function(t) {
     13                let sourceColor = sourceColorString.split(",").map(x => +x);
     14 
     15                let sourceImageData = new ImageData(4, 4, { colorSpace: sourceColorSpace });
     16                for (let i = 0; i < 4 * 4 * 4; i += 4) {
     17                    for (let c = 0; c < 4; ++c)
     18                        sourceImageData.data[i + c] = sourceColor[c];
     19                }
     20 
     21                let imageBitmapPromise;
     22                if (cropSource)
     23                    imageBitmapPromise = createImageBitmap(sourceImageData, 2, 2, 2, 2);
     24                else
     25                    imageBitmapPromise = createImageBitmap(sourceImageData);
     26 
     27                imageBitmapPromise.then(t.step_func_done(function(imageBitmap) {
     28                    let destination = document.createElement("canvas");
     29                    destination.width = 2;
     30                    destination.height = 2;
     31 
     32                    let destinationContext = destination.getContext("2d", { colorSpace: destinationColorSpace });
     33                    destinationContext.drawImage(imageBitmap, 0, 0);
     34 
     35                    let destinationImageData = destinationContext.getImageData(1, 1, 1, 1);
     36 
     37                    assert_true(pixelsApproximatelyEqual(destinationImageData.data, expectedColor), `Actual pixel value ${[...destinationImageData.data]} is approximately equal to ${expectedColor}.`);
     38                }));
     39            }, `Source ${sourceColorSpace}, destination ${destinationColorSpace}, color ${sourceColorString}, cropSource=${cropSource}`);
     40        }
     41    }
     42 }
     43 
     44 runTest("srgb", "display-p3", fromSRGBToDisplayP3);
     45 runTest("display-p3", "srgb", fromDisplayP3ToSRGB);
     46 </script>