Skip to content

Commit

Permalink
Plumb-thru color space in skia/ext/image_operations.
Browse files Browse the repository at this point in the history
Copies all the source ImageInfo to the SkBitmap result. Previously,
the color space info was being dropped; probably because this code was
written before Skia had a concept of color space. The new code uses a
simpler API that should be more future-proof. Unit tests were modified
to check that the color space is being plumbed-through.

Bug: 810131
Change-Id: Ibf6dd685731c6fa1f98b1812a70110cf07405e2f
Reviewed-on: https://chromium-review.googlesource.com/c/1352487
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Yuri Wiitala <miu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611720}
  • Loading branch information
miu-chromium authored and Commit Bot committed Nov 28, 2018
1 parent 220028e commit 40d67e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion skia/ext/image_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ SkBitmap ImageOperations::Resize(const SkPixmap& source,

// Convolve into the result.
SkBitmap result;
result.setInfo(SkImageInfo::MakeN32(dest_subset.width(), dest_subset.height(), source.alphaType()));
result.setInfo(
source.info().makeWH(dest_subset.width(), dest_subset.height()));
result.allocPixels(allocator);
if (!result.readyToDraw())
return SkBitmap();
Expand Down
17 changes: 14 additions & 3 deletions skia/ext/image_operations_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "skia/ext/image_operations.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
Expand Down Expand Up @@ -115,7 +117,7 @@ bool ColorsClose(uint32_t a, uint32_t b) {
}

void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
bmp->allocN32Pixels(w, h);
bmp->allocPixels(SkImageInfo::MakeN32Premul(w, h, SkColorSpace::MakeSRGB()));

for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
Expand All @@ -139,7 +141,7 @@ void DrawCheckerToBitmap(int w, int h,
ASSERT_GT(rect_h, 0);
ASSERT_NE(color1, color2);

bmp->allocN32Pixels(w, h);
bmp->allocPixels(SkImageInfo::MakeN32Premul(w, h, SkColorSpace::MakeSRGB()));

for (int y = 0; y < h; ++y) {
bool y_bit = (((y / rect_h) & 0x1) == 0);
Expand Down Expand Up @@ -194,6 +196,7 @@ void CheckResampleToSame(skia::ImageOperations::ResizeMethod method) {
SkBitmap results = skia::ImageOperations::Resize(src, method, src_w, src_h);
ASSERT_EQ(src_w, results.width());
ASSERT_EQ(src_h, results.height());
EXPECT_TRUE(results.colorSpace() && results.colorSpace()->isSRGB());

for (int y = 0; y < src_h; y++) {
for (int x = 0; x < src_w; x++) {
Expand Down Expand Up @@ -254,6 +257,7 @@ void CheckResizeMethodShouldAverageGrid(
SkBitmap dest = skia::ImageOperations::Resize(src, method, dest_w, dest_h);
ASSERT_EQ(dest_w, dest.width());
ASSERT_EQ(dest_h, dest.height());
EXPECT_TRUE(dest.colorSpace() && dest.colorSpace()->isSRGB());

// Check that pixels match the expected average.
float max_observed_distance = 0.0f;
Expand Down Expand Up @@ -361,6 +365,8 @@ TEST(ImageOperations, Halve) {
src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2);
ASSERT_EQ(src_w / 2, actual_results.width());
ASSERT_EQ(src_h / 2, actual_results.height());
EXPECT_TRUE(actual_results.colorSpace() &&
actual_results.colorSpace()->isSRGB());

// Compute the expected values & compare.
for (int y = 0; y < actual_results.height(); y++) {
Expand Down Expand Up @@ -404,6 +410,7 @@ TEST(ImageOperations, HalveSubset) {
src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2);
ASSERT_EQ(src_w / 2, full_results.width());
ASSERT_EQ(src_h / 2, full_results.height());
EXPECT_TRUE(full_results.colorSpace() && full_results.colorSpace()->isSRGB());

// Now do a halving of a a subset, recall the destination subset is in the
// destination coordinate system (max = half of the original image size).
Expand All @@ -413,6 +420,8 @@ TEST(ImageOperations, HalveSubset) {
src_w / 2, src_h / 2, subset_rect);
ASSERT_EQ(subset_rect.width(), subset_results.width());
ASSERT_EQ(subset_rect.height(), subset_results.height());
EXPECT_TRUE(subset_results.colorSpace() &&
subset_results.colorSpace()->isSRGB());

// The computed subset and the corresponding subset of the original image
// should be the same.
Expand Down Expand Up @@ -515,7 +524,8 @@ TEST(ImageOperations, ScaleUp) {
const int dst_w = 9;
const int dst_h = 9;
SkBitmap src;
src.allocN32Pixels(src_w, src_h);
src.allocPixels(
SkImageInfo::MakeN32Premul(src_w, src_h, SkColorSpace::MakeSRGB()));

for (int src_y = 0; src_y < src_h; ++src_y) {
for (int src_x = 0; src_x < src_w; ++src_x) {
Expand All @@ -528,6 +538,7 @@ TEST(ImageOperations, ScaleUp) {
src,
skia::ImageOperations::RESIZE_LANCZOS3,
dst_w, dst_h);
EXPECT_TRUE(dst.colorSpace() && dst.colorSpace()->isSRGB());
for (int dst_y = 0; dst_y < dst_h; ++dst_y) {
for (int dst_x = 0; dst_x < dst_w; ++dst_x) {
float dst_x_in_src = (dst_x + 0.5) * src_w / dst_w;
Expand Down

0 comments on commit 40d67e0

Please sign in to comment.