summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-23 20:40:52 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-23 20:40:52 +0000
commit0819b02efb758f7b3f6e5661a18fa7908720cac5 (patch)
tree7e0cce071d63804f06451d08c4a26e747a28ecc2 /app
parentb5685797f80769c3db0c3c3a1c6bc117b2029b71 (diff)
downloadchromium_src-0819b02efb758f7b3f6e5661a18fa7908720cac5.zip
chromium_src-0819b02efb758f7b3f6e5661a18fa7908720cac5.tar.gz
chromium_src-0819b02efb758f7b3f6e5661a18fa7908720cac5.tar.bz2
Move functions from skia/ext to app/gfx where possible: most of skia_utils.* and image_operations.* can be moved because they are not used by WebKit code.
This also fixes the spelling of "Convolusion" to "Convolution" and updates some copyrights. BUG=none TEST=none Review URL: http://codereview.chromium.org/207059 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26975 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r--app/app.gyp4
-rw-r--r--app/gfx/color_utils.cc119
-rw-r--r--app/gfx/color_utils.h27
-rw-r--r--app/gfx/color_utils_unittest.cc38
-rw-r--r--app/gfx/skbitmap_operations.cc296
-rw-r--r--app/gfx/skbitmap_operations.h82
-rw-r--r--app/gfx/skbitmap_operations_unittest.cc348
7 files changed, 914 insertions, 0 deletions
diff --git a/app/app.gyp b/app/app.gyp
index 18edb94..7c61d17 100644
--- a/app/app.gyp
+++ b/app/app.gyp
@@ -86,6 +86,8 @@
'gfx/path_gtk.cc',
'gfx/path_win.cc',
'gfx/path.h',
+ 'gfx/skbitmap_operations.cc',
+ 'gfx/skbitmap_operations.h',
'gfx/text_elider.cc',
'gfx/text_elider.h',
'gtk_dnd_util.cc',
@@ -199,8 +201,10 @@
],
'sources': [
'animation_unittest.cc',
+ 'gfx/color_utils_unittest.cc',
'gfx/font_unittest.cc',
'gfx/icon_util_unittest.cc',
+ 'gfx/skbitmap_operations_unittest.cc',
'gfx/text_elider_unittest.cc',
'l10n_util_mac_unittest.mm',
'l10n_util_unittest.cc',
diff --git a/app/gfx/color_utils.cc b/app/gfx/color_utils.cc
index 48a13ca..1d6cfe4 100644
--- a/app/gfx/color_utils.cc
+++ b/app/gfx/color_utils.cc
@@ -23,6 +23,22 @@ namespace color_utils {
namespace {
+double calcHue(double temp1, double temp2, double hue) {
+ if (hue < 0.0)
+ ++hue;
+ else if (hue > 1.0)
+ --hue;
+
+ if (hue * 6.0 < 1.0)
+ return temp1 + (temp2 - temp1) * hue * 6.0;
+ if (hue * 2.0 < 1.0)
+ return temp2;
+ if (hue * 3.0 < 2.0)
+ return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hue) * 6.0;
+
+ return temp1;
+}
+
int GetLumaForColor(SkColor* color) {
int luma = static_cast<int>((0.3 * SkColorGetR(*color)) +
(0.59 * SkColorGetG(*color)) +
@@ -56,6 +72,109 @@ double ContrastRatio(SkColor color1, SkColor color2) {
// ----------------------------------------------------------------------------
+void SkColorToHSL(SkColor c, HSL* hsl) {
+ double r = static_cast<double>(SkColorGetR(c)) / 255.0;
+ double g = static_cast<double>(SkColorGetG(c)) / 255.0;
+ double b = static_cast<double>(SkColorGetB(c)) / 255.0;
+ double vmax = std::max(std::max(r, g), b);
+ double vmin = std::min(std::min(r, g), b);
+ double delta = vmax - vmin;
+ hsl->l = (vmax + vmin) / 2;
+ if (delta) {
+ double dr = (((vmax - r) / 6.0) + (delta / 2.0)) / delta;
+ double dg = (((vmax - g) / 6.0) + (delta / 2.0)) / delta;
+ double db = (((vmax - b) / 6.0) + (delta / 2.0)) / delta;
+ if (r == vmax)
+ hsl->h = db - dg;
+ else if (g == vmax)
+ hsl->h = (1.0 / 3.0) + dr - db;
+ else if (b == vmax)
+ hsl->h = (2.0 / 3.0) + dg - dr;
+
+ if (hsl->h < 0.0)
+ ++hsl->h;
+ else if (hsl->h > 1.0)
+ --hsl->h;
+
+ hsl->s = delta / ((hsl->l < 0.5) ? (vmax + vmin) : (2 - vmax - vmin));
+ } else {
+ hsl->h = hsl->s = 0;
+ }
+}
+
+SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha) {
+ double hue = hsl.h;
+ double saturation = hsl.s;
+ double lightness = hsl.l;
+
+ // If there's no color, we don't care about hue and can do everything based
+ // on brightness.
+ if (!saturation) {
+ uint8 light;
+
+ if (lightness < 0)
+ light = 0;
+ else if (lightness >= 1.0)
+ light = 255;
+ else
+ light = SkDoubleToFixed(lightness) >> 8;
+
+ return SkColorSetARGB(alpha, light, light, light);
+ }
+
+ double temp2 = (lightness < 0.5) ?
+ (lightness * (1.0 + saturation)) :
+ (lightness + saturation - (lightness * saturation));
+ double temp1 = 2.0 * lightness - temp2;
+ return SkColorSetARGB(alpha,
+ static_cast<int>(calcHue(temp1, temp2, hue + 1.0 / 3.0) * 255),
+ static_cast<int>(calcHue(temp1, temp2, hue) * 255),
+ static_cast<int>(calcHue(temp1, temp2, hue - 1.0 / 3.0) * 255));
+}
+
+SkColor HSLShift(SkColor color, const HSL& shift) {
+ HSL hsl;
+ int alpha = SkColorGetA(color);
+ SkColorToHSL(color, &hsl);
+
+ // Replace the hue with the tint's hue.
+ if (shift.h >= 0)
+ hsl.h = shift.h;
+
+ // Change the saturation.
+ if (shift.s >= 0) {
+ if (shift.s <= 0.5)
+ hsl.s *= shift.s * 2.0;
+ else
+ hsl.s += (1.0 - hsl.s) * ((shift.s - 0.5) * 2.0);
+ }
+
+ SkColor result = HSLToSkColor(hsl, alpha);
+
+ if (shift.l < 0)
+ return result;
+
+ // Lightness shifts in the style of popular image editors aren't
+ // actually represented in HSL - the L value does have some effect
+ // on saturation.
+ double r = static_cast<double>(SkColorGetR(result));
+ double g = static_cast<double>(SkColorGetG(result));
+ double b = static_cast<double>(SkColorGetB(result));
+ if (shift.l <= 0.5) {
+ r *= (shift.l * 2.0);
+ g *= (shift.l * 2.0);
+ b *= (shift.l * 2.0);
+ } else {
+ r += (255.0 - r) * ((shift.l - 0.5) * 2.0);
+ g += (255.0 - g) * ((shift.l - 0.5) * 2.0);
+ b += (255.0 - b) * ((shift.l - 0.5) * 2.0);
+ }
+ return SkColorSetARGB(alpha,
+ static_cast<int>(r),
+ static_cast<int>(g),
+ static_cast<int>(b));
+}
+
bool IsColorCloseToTransparent(SkAlpha alpha) {
const int kCloseToBoundary = 64;
return alpha < kCloseToBoundary;
diff --git a/app/gfx/color_utils.h b/app/gfx/color_utils.h
index 30e9e52..682062f 100644
--- a/app/gfx/color_utils.h
+++ b/app/gfx/color_utils.h
@@ -11,6 +11,33 @@ class SkBitmap;
namespace color_utils {
+// Represents an HSL color.
+struct HSL {
+ double h;
+ double s;
+ double l;
+};
+
+// Note: these transformations assume sRGB as the source color space
+void SkColorToHSL(SkColor c, HSL* hsl);
+SkColor HSLToSkColor(const HSL& hsl, SkAlpha alpha);
+
+// HSL-Shift an SkColor. The shift values are in the range of 0-1, with the
+// option to specify -1 for 'no change'. The shift values are defined as:
+// hsl_shift[0] (hue): The absolute hue value - 0 and 1 map
+// to 0 and 360 on the hue color wheel (red).
+// hsl_shift[1] (saturation): A saturation shift, with the
+// following key values:
+// 0 = remove all color.
+// 0.5 = leave unchanged.
+// 1 = fully saturate the image.
+// hsl_shift[2] (lightness): A lightness shift, with the
+// following key values:
+// 0 = remove all lightness (make all pixels black).
+// 0.5 = leave unchanged.
+// 1 = full lightness (make all pixels white).
+SkColor HSLShift(SkColor color, const HSL& shift);
+
// Determine if a given alpha value is nearly completely transparent.
bool IsColorCloseToTransparent(SkAlpha alpha);
diff --git a/app/gfx/color_utils_unittest.cc b/app/gfx/color_utils_unittest.cc
new file mode 100644
index 0000000..4e7f414
--- /dev/null
+++ b/app/gfx/color_utils_unittest.cc
@@ -0,0 +1,38 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdlib.h>
+
+#include "app/gfx/color_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+
+TEST(ColorUtils, SkColorToHSLRed) {
+ color_utils::HSL hsl = { 0, 0, 0 };
+ color_utils::SkColorToHSL(SK_ColorRED, &hsl);
+ EXPECT_EQ(hsl.h, 0);
+ EXPECT_EQ(hsl.s, 1);
+ EXPECT_EQ(hsl.l, 0.5);
+}
+
+TEST(ColorUtils, SkColorToHSLGrey) {
+ color_utils::HSL hsl = { 0, 0, 0 };
+ color_utils::SkColorToHSL(SkColorSetARGB(255, 128, 128, 128), &hsl);
+ EXPECT_EQ(hsl.h, 0);
+ EXPECT_EQ(hsl.s, 0);
+ EXPECT_EQ(static_cast<int>(hsl.l * 100),
+ static_cast<int>(0.5 * 100)); // Accurate to two decimal places.
+}
+
+TEST(ColorUtils, HSLToSkColorWithAlpha) {
+ SkColor red = SkColorSetARGB(128, 255, 0, 0);
+ color_utils::HSL hsl = { 0, 1, 0.5 };
+ SkColor result = color_utils::HSLToSkColor(hsl, 128);
+ EXPECT_EQ(SkColorGetA(red), SkColorGetA(result));
+ EXPECT_EQ(SkColorGetR(red), SkColorGetR(result));
+ EXPECT_EQ(SkColorGetG(red), SkColorGetG(result));
+ EXPECT_EQ(SkColorGetB(red), SkColorGetB(result));
+}
+
diff --git a/app/gfx/skbitmap_operations.cc b/app/gfx/skbitmap_operations.cc
new file mode 100644
index 0000000..174df68
--- /dev/null
+++ b/app/gfx/skbitmap_operations.cc
@@ -0,0 +1,296 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gfx/skbitmap_operations.h"
+
+#include "base/logging.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+
+// static
+SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first,
+ const SkBitmap& second,
+ double alpha) {
+ DCHECK((alpha >= 0) && (alpha <= 1));
+ DCHECK(first.width() == second.width());
+ DCHECK(first.height() == second.height());
+ DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
+ DCHECK(first.config() == SkBitmap::kARGB_8888_Config);
+
+ // Optimize for case where we won't need to blend anything.
+ static const double alpha_min = 1.0 / 255;
+ static const double alpha_max = 254.0 / 255;
+ if (alpha < alpha_min)
+ return first;
+ else if (alpha > alpha_max)
+ return second;
+
+ SkAutoLockPixels lock_first(first);
+ SkAutoLockPixels lock_second(second);
+
+ SkBitmap blended;
+ blended.setConfig(SkBitmap::kARGB_8888_Config, first.width(), first.height(),
+ 0);
+ blended.allocPixels();
+ blended.eraseARGB(0, 0, 0, 0);
+
+ double first_alpha = 1 - alpha;
+
+ for (int y = 0; y < first.height(); ++y) {
+ uint32* first_row = first.getAddr32(0, y);
+ uint32* second_row = second.getAddr32(0, y);
+ uint32* dst_row = blended.getAddr32(0, y);
+
+ for (int x = 0; x < first.width(); ++x) {
+ uint32 first_pixel = first_row[x];
+ uint32 second_pixel = second_row[x];
+
+ int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) +
+ (SkColorGetA(second_pixel) * alpha));
+ int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) +
+ (SkColorGetR(second_pixel) * alpha));
+ int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) +
+ (SkColorGetG(second_pixel) * alpha));
+ int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) +
+ (SkColorGetB(second_pixel) * alpha));
+
+ dst_row[x] = SkColorSetARGB(a, r, g, b);
+ }
+ }
+
+ return blended;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb,
+ const SkBitmap& alpha) {
+ DCHECK(rgb.width() == alpha.width());
+ DCHECK(rgb.height() == alpha.height());
+ DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel());
+ DCHECK(rgb.config() == SkBitmap::kARGB_8888_Config);
+ DCHECK(alpha.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap masked;
+ masked.setConfig(SkBitmap::kARGB_8888_Config, rgb.width(), rgb.height(), 0);
+ masked.allocPixels();
+ masked.eraseARGB(0, 0, 0, 0);
+
+ SkAutoLockPixels lock_rgb(rgb);
+ SkAutoLockPixels lock_alpha(alpha);
+ SkAutoLockPixels lock_masked(masked);
+
+ for (int y = 0; y < masked.height(); ++y) {
+ uint32* rgb_row = rgb.getAddr32(0, y);
+ uint32* alpha_row = alpha.getAddr32(0, y);
+ uint32* dst_row = masked.getAddr32(0, y);
+
+ for (int x = 0; x < masked.width(); ++x) {
+ SkColor rgb_pixel = SkUnPreMultiply::PMColorToColor(rgb_row[x]);
+ int alpha = SkAlphaMul(SkColorGetA(rgb_pixel), SkColorGetA(alpha_row[x]));
+ dst_row[x] = SkColorSetARGB(alpha,
+ SkAlphaMul(SkColorGetR(rgb_pixel), alpha),
+ SkAlphaMul(SkColorGetG(rgb_pixel), alpha),
+ SkAlphaMul(SkColorGetB(rgb_pixel), alpha));
+ }
+ }
+
+ return masked;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color,
+ const SkBitmap& image,
+ const SkBitmap& mask) {
+ DCHECK(image.config() == SkBitmap::kARGB_8888_Config);
+ DCHECK(mask.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap background;
+ background.setConfig(
+ SkBitmap::kARGB_8888_Config, mask.width(), mask.height(), 0);
+ background.allocPixels();
+
+ double bg_a = SkColorGetA(color);
+ double bg_r = SkColorGetR(color);
+ double bg_g = SkColorGetG(color);
+ double bg_b = SkColorGetB(color);
+
+ SkAutoLockPixels lock_mask(mask);
+ SkAutoLockPixels lock_image(image);
+ SkAutoLockPixels lock_background(background);
+
+ for (int y = 0; y < mask.height(); ++y) {
+ uint32* dst_row = background.getAddr32(0, y);
+ uint32* image_row = image.getAddr32(0, y % image.height());
+ uint32* mask_row = mask.getAddr32(0, y);
+
+ for (int x = 0; x < mask.width(); ++x) {
+ uint32 image_pixel = image_row[x % image.width()];
+
+ double img_a = SkColorGetA(image_pixel);
+ double img_r = SkColorGetR(image_pixel);
+ double img_g = SkColorGetG(image_pixel);
+ double img_b = SkColorGetB(image_pixel);
+
+ double img_alpha = static_cast<double>(img_a) / 255.0;
+ double img_inv = 1 - img_alpha;
+
+ double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0;
+
+ dst_row[x] = SkColorSetARGB(
+ static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a),
+ static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a),
+ static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a),
+ static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a));
+ }
+ }
+
+ return background;
+}
+
+
+// static
+SkBitmap SkBitmapOperations::CreateHSLShiftedBitmap(
+ const SkBitmap& bitmap,
+ color_utils::HSL hsl_shift) {
+ DCHECK(bitmap.empty() == false);
+ DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap shifted;
+ shifted.setConfig(SkBitmap::kARGB_8888_Config, bitmap.width(),
+ bitmap.height(), 0);
+ shifted.allocPixels();
+ shifted.eraseARGB(0, 0, 0, 0);
+ shifted.setIsOpaque(false);
+
+ SkAutoLockPixels lock_bitmap(bitmap);
+ SkAutoLockPixels lock_shifted(shifted);
+
+ // Loop through the pixels of the original bitmap.
+ for (int y = 0; y < bitmap.height(); ++y) {
+ SkPMColor* pixels = bitmap.getAddr32(0, y);
+ SkPMColor* tinted_pixels = shifted.getAddr32(0, y);
+
+ for (int x = 0; x < bitmap.width(); ++x) {
+ tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift(
+ SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift));
+ }
+ }
+
+ return shifted;
+}
+
+// static
+SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source,
+ int src_x, int src_y,
+ int dst_w, int dst_h) {
+ DCHECK(source.getConfig() == SkBitmap::kARGB_8888_Config);
+
+ SkBitmap cropped;
+ cropped.setConfig(SkBitmap::kARGB_8888_Config, dst_w, dst_h, 0);
+ cropped.allocPixels();
+ cropped.eraseARGB(0, 0, 0, 0);
+
+ SkAutoLockPixels lock_source(source);
+ SkAutoLockPixels lock_cropped(cropped);
+
+ // Loop through the pixels of the original bitmap.
+ for (int y = 0; y < dst_h; ++y) {
+ int y_pix = (src_y + y) % source.height();
+ while (y_pix < 0)
+ y_pix += source.height();
+
+ uint32* source_row = source.getAddr32(0, y_pix);
+ uint32* dst_row = cropped.getAddr32(0, y);
+
+ for (int x = 0; x < dst_w; ++x) {
+ int x_pix = (src_x + x) % source.width();
+ while (x_pix < 0)
+ x_pix += source.width();
+
+ dst_row[x] = source_row[x_pix];
+ }
+ }
+
+ return cropped;
+}
+
+// static
+SkBitmap SkBitmapOperations::DownsampleByTwoUntilSize(const SkBitmap& bitmap,
+ int min_w, int min_h) {
+ if ((bitmap.width() <= min_w) || (bitmap.height() <= min_h) ||
+ (min_w < 0) || (min_h < 0))
+ return bitmap;
+
+ // Since bitmaps are refcounted, this copy will be fast.
+ SkBitmap current = bitmap;
+ while ((current.width() >= min_w * 2) && (current.height() >= min_h * 2) &&
+ (current.width() > 1) && (current.height() > 1))
+ current = DownsampleByTwo(current);
+ return current;
+}
+
+// static
+SkBitmap SkBitmapOperations::DownsampleByTwo(const SkBitmap& bitmap) {
+ // Handle the nop case.
+ if ((bitmap.width() <= 1) || (bitmap.height() <= 1))
+ return bitmap;
+
+ SkBitmap result;
+ result.setConfig(SkBitmap::kARGB_8888_Config,
+ (bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2);
+ result.allocPixels();
+
+ SkAutoLockPixels lock(bitmap);
+ for (int dest_y = 0; dest_y < result.height(); ++dest_y) {
+ for (int dest_x = 0; dest_x < result.width(); ++dest_x) {
+ // This code is based on downsampleby2_proc32 in SkBitmap.cpp. It is very
+ // clever in that it does two channels at once: alpha and green ("ag")
+ // and red and blue ("rb"). Each channel gets averaged across 4 pixels
+ // to get the result.
+ int src_x = dest_x << 1;
+ int src_y = dest_y << 1;
+ const SkPMColor* cur_src = bitmap.getAddr32(src_x, src_y);
+ SkPMColor tmp, ag, rb;
+
+ // Top left pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag = (tmp >> 8) & 0xFF00FF;
+ rb = tmp & 0xFF00FF;
+ if (src_x < (bitmap.width() - 1))
+ ++cur_src;
+
+ // Top right pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+ if (src_y < (bitmap.height() - 1))
+ cur_src = bitmap.getAddr32(src_x, src_y + 1);
+ else
+ cur_src = bitmap.getAddr32(src_x, src_y); // Move back to the first.
+
+ // Bottom left pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+ if (src_x < (bitmap.width() - 1))
+ ++cur_src;
+
+ // Bottom right pixel of the 2x2 block.
+ tmp = *cur_src;
+ ag += (tmp >> 8) & 0xFF00FF;
+ rb += tmp & 0xFF00FF;
+
+ // Put the channels back together, dividing each by 4 to get the average.
+ // |ag| has the alpha and green channels shifted right by 8 bits from
+ // there they should end up, so shifting left by 6 gives them in the
+ // correct position divided by 4.
+ *result.getAddr32(dest_x, dest_y) =
+ ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
+ }
+ }
+
+ return result;
+}
+
diff --git a/app/gfx/skbitmap_operations.h b/app/gfx/skbitmap_operations.h
new file mode 100644
index 0000000..d6bb5d3
--- /dev/null
+++ b/app/gfx/skbitmap_operations.h
@@ -0,0 +1,82 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef APP_GFX_SKBITMAP_OPERATIONS_H_
+#define APP_GFX_SKBITMAP_OPERATIONS_H_
+
+#include "app/gfx/color_utils.h"
+#include "testing/gtest/include/gtest/gtest_prod.h"
+
+class SkBitmap;
+
+class SkBitmapOperations {
+ public:
+ // Create a bitmap that is a blend of two others. The alpha argument
+ // specifies the opacity of the second bitmap. The provided bitmaps must
+ // use have the kARGB_8888_Config config and be of equal dimensions.
+ static SkBitmap CreateBlendedBitmap(const SkBitmap& first,
+ const SkBitmap& second,
+ double alpha);
+
+ // Create a bitmap that is the original bitmap masked out by the mask defined
+ // in the alpha bitmap. The images must use the kARGB_8888_Config config and
+ // be of equal dimensions.
+ static SkBitmap CreateMaskedBitmap(const SkBitmap& first,
+ const SkBitmap& alpha);
+
+ // We create a button background image by compositing the color and image
+ // together, then applying the mask. This is a highly specialized composite
+ // operation that is the equivalent of drawing a background in |color|,
+ // tiling |image| over the top, and then masking the result out with |mask|.
+ // The images must use kARGB_8888_Config config.
+ static SkBitmap CreateButtonBackground(SkColor color,
+ const SkBitmap& image,
+ const SkBitmap& mask);
+
+ // Shift a bitmap's HSL values. The shift values are in the range of 0-1,
+ // with the option to specify -1 for 'no change'. The shift values are
+ // defined as:
+ // hsl_shift[0] (hue): The absolute hue value for the image - 0 and 1 map
+ // to 0 and 360 on the hue color wheel (red).
+ // hsl_shift[1] (saturation): A saturation shift for the image, with the
+ // following key values:
+ // 0 = remove all color.
+ // 0.5 = leave unchanged.
+ // 1 = fully saturate the image.
+ // hsl_shift[2] (lightness): A lightness shift for the image, with the
+ // following key values:
+ // 0 = remove all lightness (make all pixels black).
+ // 0.5 = leave unchanged.
+ // 1 = full lightness (make all pixels white).
+ static SkBitmap CreateHSLShiftedBitmap(const SkBitmap& bitmap,
+ color_utils::HSL hsl_shift);
+
+ // Create a bitmap that is cropped from another bitmap. This is special
+ // because it tiles the original bitmap, so your coordinates can extend
+ // outside the bounds of the original image.
+ static SkBitmap CreateTiledBitmap(const SkBitmap& bitmap,
+ int src_x, int src_y,
+ int dst_w, int dst_h);
+
+ // Iteratively downsamples by 2 until the bitmap is no smaller than the
+ // input size. The normal use of this is to downsample the bitmap "close" to
+ // the final size, and then use traditional resampling on the result.
+ // Because the bitmap will be closer to the final size, it will be faster,
+ // and linear interpolation will generally work well as a second step.
+ static SkBitmap DownsampleByTwoUntilSize(const SkBitmap& bitmap,
+ int min_w, int min_h);
+
+ private:
+ SkBitmapOperations(); // Class for scoping only.
+
+ // Makes a bitmap half has large in each direction by averaging groups of
+ // 4 pixels. This is one step in generating a mipmap.
+ static SkBitmap DownsampleByTwo(const SkBitmap& bitmap);
+
+ FRIEND_TEST(SkBitmapOperationsTest, DownsampleByTwo);
+ FRIEND_TEST(SkBitmapOperationsTest, DownsampleByTwoSmall);
+};
+
+#endif // APP_GFX_SKBITMAP_OPERATIONS_H_
+
diff --git a/app/gfx/skbitmap_operations_unittest.cc b/app/gfx/skbitmap_operations_unittest.cc
new file mode 100644
index 0000000..feb288c
--- /dev/null
+++ b/app/gfx/skbitmap_operations_unittest.cc
@@ -0,0 +1,348 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gfx/skbitmap_operations.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkColorPriv.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+
+namespace {
+
+// Returns true if each channel of the given two colors are "close." This is
+// used for comparing colors where rounding errors may cause off-by-one.
+bool ColorsClose(uint32_t a, uint32_t b) {
+ return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2;
+}
+
+void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
+ bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
+ bmp->allocPixels();
+
+ unsigned char* src_data =
+ reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
+ for (int i = 0; i < w * h; i++) {
+ src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
+ }
+}
+
+} // namespace
+
+// Blend two bitmaps together at 50% alpha and verify that the result
+// is the middle-blend of the two.
+TEST(SkBitmapOperationsTest, CreateBlendedBitmap) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src_a;
+ src_a.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src_a.allocPixels();
+
+ SkBitmap src_b;
+ src_b.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src_b.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255);
+ *src_b.getAddr32(x, y) =
+ SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0);
+ i++;
+ }
+ }
+
+ // Shift to red.
+ SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap(
+ src_a, src_b, 0.5);
+ SkAutoLockPixels srca_lock(src_a);
+ SkAutoLockPixels srcb_lock(src_b);
+ SkAutoLockPixels blended_lock(blended);
+
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ int i = y * src_w + x;
+ EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2),
+ SkColorGetA(*blended.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
+ SkColorGetR(*blended.getAddr32(x, y)));
+ EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2),
+ SkColorGetG(*blended.getAddr32(x, y)));
+ EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
+ SkColorGetB(*blended.getAddr32(x, y)));
+ }
+ }
+}
+
+// Test our masking functions.
+TEST(SkBitmapOperationsTest, CreateMaskedBitmap) {
+ int src_w = 16, src_h = 16;
+
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ // Generate alpha mask
+ SkBitmap alpha;
+ alpha.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ alpha.allocPixels();
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *alpha.getAddr32(x, y) = SkColorSetARGB((i + 128) % 255,
+ (i + 128) % 255,
+ (i + 64) % 255,
+ (i + 0) % 255);
+ i++;
+ }
+ }
+
+ SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels alpha_lock(alpha);
+ SkAutoLockPixels masked_lock(masked);
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ // Test that the alpha is equal.
+ SkColor src_pixel = SkUnPreMultiply::PMColorToColor(*src.getAddr32(x, y));
+ SkColor alpha_pixel =
+ SkUnPreMultiply::PMColorToColor(*alpha.getAddr32(x, y));
+ SkColor masked_pixel = *masked.getAddr32(x, y);
+
+ int alpha_value = SkAlphaMul(SkColorGetA(src_pixel),
+ SkColorGetA(alpha_pixel));
+ SkColor expected_pixel = SkColorSetARGB(
+ alpha_value,
+ SkAlphaMul(SkColorGetR(src_pixel), alpha_value),
+ SkAlphaMul(SkColorGetG(src_pixel), alpha_value),
+ SkAlphaMul(SkColorGetB(src_pixel), alpha_value));
+
+ EXPECT_TRUE(ColorsClose(expected_pixel, masked_pixel));
+ }
+ }
+}
+
+// Make sure that when shifting a bitmap without any shift parameters,
+// the end result is close enough to the original (rounding errors
+// notwithstanding).
+TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) {
+ int src_w = 4, src_h = 4;
+ SkBitmap src;
+ src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src.getAddr32(x, y) = SkColorSetARGB(i + 128 % 255,
+ i + 128 % 255, i + 64 % 255, i + 0 % 255);
+ i++;
+ }
+ }
+
+ color_utils::HSL hsl = { -1, -1, -1 };
+
+ SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels shifted_lock(shifted);
+
+ for (int y = 0; y < src_w; y++) {
+ for (int x = 0; x < src_h; x++) {
+ SkColor src_pixel = *src.getAddr32(x, y);
+ SkColor shifted_pixel = *shifted.getAddr32(x, y);
+ EXPECT_TRUE(ColorsClose(src_pixel, shifted_pixel));
+ }
+ }
+}
+
+// Shift a blue bitmap to red.
+TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h);
+ src.allocPixels();
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255);
+ i++;
+ }
+ }
+
+ // Shift to red.
+ color_utils::HSL hsl = { 0, -1, -1 };
+
+ SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels shifted_lock(shifted);
+
+ for (int y = 0, i = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ EXPECT_TRUE(ColorsClose(*shifted.getAddr32(x, y),
+ SkColorSetARGB(255, i % 255, 0, 0)));
+ i++;
+ }
+ }
+}
+
+// Test our cropping.
+TEST(SkBitmapOperationsTest, CreateCroppedBitmap) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4,
+ 8, 8);
+ ASSERT_EQ(8, cropped.width());
+ ASSERT_EQ(8, cropped.height());
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels cropped_lock(cropped);
+ for (int y = 4; y < 12; y++) {
+ for (int x = 4; x < 12; x++) {
+ EXPECT_EQ(*src.getAddr32(x, y),
+ *cropped.getAddr32(x - 4, y - 4));
+ }
+ }
+}
+
+// Test whether our cropping correctly wraps across image boundaries.
+TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) {
+ int src_w = 16, src_h = 16;
+ SkBitmap src;
+ FillDataToBitmap(src_w, src_h, &src);
+
+ SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(
+ src, src_w / 2, src_h / 2, src_w, src_h);
+ ASSERT_EQ(src_w, cropped.width());
+ ASSERT_EQ(src_h, cropped.height());
+
+ SkAutoLockPixels src_lock(src);
+ SkAutoLockPixels cropped_lock(cropped);
+ for (int y = 0; y < src_h; y++) {
+ for (int x = 0; x < src_w; x++) {
+ EXPECT_EQ(*src.getAddr32(x, y),
+ *cropped.getAddr32((x + src_w / 2) % src_w,
+ (y + src_h / 2) % src_h));
+ }
+ }
+}
+
+TEST(SkBitmapOperationsTest, DownsampleByTwo) {
+ // Use an odd-sized bitmap to make sure the edge cases where there isn't a
+ // 2x2 block of pixels is handled correctly.
+ // Here's the ARGB example
+ //
+ // 50% transparent green opaque 50% blue white
+ // 80008000 FF000080 FFFFFFFF
+ //
+ // 50% transparent red opaque 50% gray black
+ // 80800000 80808080 FF000000
+ //
+ // black white 50% gray
+ // FF000000 FFFFFFFF FF808080
+ //
+ // The result of this computation should be:
+ // A0404040 FF808080
+ // FF808080 FF808080
+ SkBitmap input;
+ input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3);
+ input.allocPixels();
+
+ // The color order may be different, but we don't care (the channels are
+ // trated the same).
+ *input.getAddr32(0, 0) = 0x80008000;
+ *input.getAddr32(1, 0) = 0xFF000080;
+ *input.getAddr32(2, 0) = 0xFFFFFFFF;
+ *input.getAddr32(0, 1) = 0x80800000;
+ *input.getAddr32(1, 1) = 0x80808080;
+ *input.getAddr32(2, 1) = 0xFF000000;
+ *input.getAddr32(0, 2) = 0xFF000000;
+ *input.getAddr32(1, 2) = 0xFFFFFFFF;
+ *input.getAddr32(2, 2) = 0xFF808080;
+
+ SkBitmap result = SkBitmapOperations::DownsampleByTwo(input);
+ EXPECT_EQ(2, result.width());
+ EXPECT_EQ(2, result.height());
+
+ // Some of the values are off-by-one due to rounding.
+ SkAutoLockPixels lock(result);
+ EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1));
+ EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1));
+}
+
+// Test edge cases for DownsampleByTwo.
+TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) {
+ SkPMColor reference = 0xFF4080FF;
+
+ // Test a 1x1 bitmap.
+ SkBitmap one_by_one;
+ one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
+ one_by_one.allocPixels();
+ *one_by_one.getAddr32(0, 0) = reference;
+ SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one);
+ SkAutoLockPixels lock1(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+ EXPECT_EQ(reference, *result.getAddr32(0, 0));
+
+ // Test an n by 1 bitmap.
+ SkBitmap one_by_n;
+ one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1);
+ one_by_n.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwo(one_by_n);
+ SkAutoLockPixels lock2(result);
+ EXPECT_EQ(300, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test a 1 by n bitmap.
+ SkBitmap n_by_one;
+ n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300);
+ n_by_one.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwo(n_by_one);
+ SkAutoLockPixels lock3(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(300, result.height());
+
+ // Test an empty bitmap
+ SkBitmap empty;
+ result = SkBitmapOperations::DownsampleByTwo(empty);
+ EXPECT_TRUE(result.isNull());
+ EXPECT_EQ(0, result.width());
+ EXPECT_EQ(0, result.height());
+}
+
+// Here we assume DownsampleByTwo works correctly (it's tested above) and
+// just make sure that the wrapper function does the right thing.
+TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) {
+ // First make sure a "too small" bitmap doesn't get modified at all.
+ SkBitmap too_small;
+ too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
+ too_small.allocPixels();
+ SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize(
+ too_small, 16, 16);
+ EXPECT_EQ(10, result.width());
+ EXPECT_EQ(10, result.height());
+
+ // Now make sure giving it a 0x0 target returns something reasonable.
+ result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test multiple steps of downsampling.
+ SkBitmap large;
+ large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43);
+ large.allocPixels();
+ result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6);
+
+ // The result should be divided in half 100x43 -> 50x22 -> 25x11
+ EXPECT_EQ(25, result.width());
+ EXPECT_EQ(11, result.height());
+}