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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/platform/image-decoders/image_frame.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/private/chromium/SkPMColor.h"
#include "third_party/skia/modules/skcms/skcms.h"
namespace blink {
namespace {
// Needed for ImageFrame::SetMemoryAllocator, but still does the default
// allocation.
class TestAllocator final : public SkBitmap::Allocator {
bool allocPixelRef(SkBitmap* dst) override { return dst->tryAllocPixels(); }
};
class ImageFrameTest : public testing::Test {
public:
void SetUp() override {
src_8888_a = 0x80;
src_8888_r = 0x40;
src_8888_g = 0x50;
src_8888_b = 0x60;
src_8888 = SkPMColorSetARGB(src_8888_a, src_8888_r, src_8888_g, src_8888_b);
dst_8888 = SkPMColorSetARGB(0xA0, 0x60, 0x70, 0x80);
#if SK_PMCOLOR_BYTE_ORDER(B, G, R, A)
pixel_format_n32 = skcms_PixelFormat_BGRA_8888;
#else
pixel_format_n32 = skcms_PixelFormat_RGBA_8888;
#endif
skcms_Transform(&src_8888, pixel_format_n32, skcms_AlphaFormat_Unpremul,
nullptr, &src_f16, skcms_PixelFormat_RGBA_hhhh,
skcms_AlphaFormat_Unpremul, nullptr, 1);
skcms_Transform(&dst_8888, pixel_format_n32, skcms_AlphaFormat_Unpremul,
nullptr, &dst_f16, skcms_PixelFormat_RGBA_hhhh,
skcms_AlphaFormat_Unpremul, nullptr, 1);
}
protected:
const float color_compoenent_tolerance = 0.01;
unsigned src_8888_a, src_8888_r, src_8888_g, src_8888_b;
ImageFrame::PixelData src_8888, dst_8888;
ImageFrame::PixelDataF16 src_f16, dst_f16;
skcms_PixelFormat pixel_format_n32;
void ConvertN32ToF32(float* dst, ImageFrame::PixelData src) {
skcms_Transform(&src, pixel_format_n32, skcms_AlphaFormat_Unpremul, nullptr,
dst, skcms_PixelFormat_RGBA_ffff,
skcms_AlphaFormat_Unpremul, nullptr, 1);
}
void ConvertF16ToF32(float* dst, ImageFrame::PixelDataF16 src) {
skcms_Transform(&src, skcms_PixelFormat_RGBA_hhhh,
skcms_AlphaFormat_Unpremul, nullptr, dst,
skcms_PixelFormat_RGBA_ffff, skcms_AlphaFormat_Unpremul,
nullptr, 1);
}
};
TEST_F(ImageFrameTest, BlendRGBARawF16Buffer) {
ImageFrame::PixelData blended_8888(dst_8888);
ImageFrame::BlendRGBARaw(&blended_8888, src_8888_r, src_8888_g, src_8888_b,
src_8888_a);
ImageFrame::PixelDataF16 blended_f16 = dst_f16;
ImageFrame::BlendRGBARawF16Buffer(&blended_f16, &src_f16, 1);
float f32_from_blended_8888[4];
ConvertN32ToF32(f32_from_blended_8888, blended_8888);
float f32_from_blended_f16[4];
ConvertF16ToF32(f32_from_blended_f16, blended_f16);
for (int i = 0; i < 4; i++) {
ASSERT_TRUE(fabs(f32_from_blended_8888[i] - f32_from_blended_f16[i]) <
color_compoenent_tolerance);
}
}
TEST_F(ImageFrameTest, BlendRGBAPremultipliedF16Buffer) {
ImageFrame::PixelData blended_8888(dst_8888);
ImageFrame::BlendRGBAPremultiplied(&blended_8888, src_8888_r, src_8888_g,
src_8888_b, src_8888_a);
ImageFrame::PixelDataF16 blended_f16 = dst_f16;
ImageFrame::BlendRGBAPremultipliedF16Buffer(&blended_f16, &src_f16, 1);
float f32_from_blended_8888[4];
ConvertN32ToF32(f32_from_blended_8888, blended_8888);
float f32_from_blended_f16[4];
ConvertF16ToF32(f32_from_blended_f16, blended_f16);
for (int i = 0; i < 4; i++) {
ASSERT_TRUE(fabs(f32_from_blended_8888[i] - f32_from_blended_f16[i]) <
color_compoenent_tolerance);
}
}
} // namespace
} // namespace blink
|