From cab34d6a13d3044c59cd5dccdf4b04725a395aac Mon Sep 17 00:00:00 2001 From: "pkasting@chromium.org" Date: Thu, 24 Sep 2009 01:14:52 +0000 Subject: [PATCH] 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. This is a re-do of r26975, this time with WebKit update and some fixes to compile on Mac and Linux. BUG=none TEST=none git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27031 0039d316-1c4b-4281-b951-d872f2087c98 --- DEPS | 2 +- app/app.gyp | 4 + app/gfx/color_utils.cc | 119 +++++ app/gfx/color_utils.h | 27 ++ .../gfx/color_utils_unittest.cc | 24 +- app/gfx/skbitmap_operations.cc | 296 +++++++++++++ app/gfx/skbitmap_operations.h | 82 ++++ app/gfx/skbitmap_operations_unittest.cc | 348 +++++++++++++++ chrome/browser/browser_theme_provider.cc | 43 +- chrome/browser/browser_theme_provider.h | 22 +- chrome/browser/browser_theme_provider_gtk.cc | 1 + chrome/browser/browser_theme_provider_mac.mm | 8 +- chrome/browser/dom_ui/dom_ui_theme_source.cc | 6 +- chrome/browser/download/download_util.cc | 1 + chrome/browser/gtk/browser_titlebar.cc | 24 +- chrome/browser/gtk/browser_window_gtk.cc | 6 +- chrome/browser/gtk/custom_button.cc | 6 +- chrome/browser/gtk/gtk_theme_provider.cc | 35 +- chrome/browser/gtk/gtk_theme_provider.h | 5 +- chrome/browser/gtk/tabs/tab_renderer_gtk.cc | 6 +- .../tab_contents/thumbnail_generator.cc | 7 +- chrome/browser/views/bookmark_bar_view.cc | 1 - chrome/browser/views/bookmark_manager_view.cc | 1 + .../browser/views/detachable_toolbar_view.cc | 2 + .../views/extensions/extension_shelf.cc | 1 - chrome/browser/views/tabs/tab_2.cc | 46 +- .../browser/views/tabs/tab_overview_cell.cc | 4 +- chrome/browser/views/tabs/tab_renderer.cc | 48 +-- chrome/renderer/render_view.cc | 3 +- skia/ext/convolver.cc | 52 +-- skia/ext/convolver.h | 16 +- skia/ext/convolver_unittest.cc | 18 +- skia/ext/image_operations.cc | 407 ++---------------- skia/ext/image_operations.h | 73 +--- skia/ext/image_operations_unittest.cc | 380 +--------------- skia/ext/skia_utils.cc | 148 +------ skia/ext/skia_utils.h | 34 +- views/controls/button/image_button.cc | 13 +- webkit/tools/test_shell/test_shell.gyp | 1 - 39 files changed, 1109 insertions(+), 1211 deletions(-) rename skia/ext/skia_utils_unittest.cc => app/gfx/color_utils_unittest.cc (65%) create mode 100644 app/gfx/skbitmap_operations.cc create mode 100644 app/gfx/skbitmap_operations.h create mode 100644 app/gfx/skbitmap_operations_unittest.cc diff --git a/DEPS b/DEPS index bc5c432c810204..9284523e1677db 100644 --- a/DEPS +++ b/DEPS @@ -1,7 +1,7 @@ vars = { "webkit_trunk": "http://svn.webkit.org/repository/webkit/trunk", - "webkit_revision": "48679", + "webkit_revision": "48684", "ffmpeg_revision": "26428", } diff --git a/app/app.gyp b/app/app.gyp index 18edb94ded0ebf..7c61d177c88a1c 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 48a13cabdbd887..1d6cfe42f28135 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((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(SkColorGetR(c)) / 255.0; + double g = static_cast(SkColorGetG(c)) / 255.0; + double b = static_cast(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(calcHue(temp1, temp2, hue + 1.0 / 3.0) * 255), + static_cast(calcHue(temp1, temp2, hue) * 255), + static_cast(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(SkColorGetR(result)); + double g = static_cast(SkColorGetG(result)); + double b = static_cast(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(r), + static_cast(g), + static_cast(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 30e9e520650b0b..682062fafcb63f 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/skia/ext/skia_utils_unittest.cc b/app/gfx/color_utils_unittest.cc similarity index 65% rename from skia/ext/skia_utils_unittest.cc rename to app/gfx/color_utils_unittest.cc index 3b590b4d75e48a..4e7f414e6c6b3a 100644 --- a/skia/ext/skia_utils_unittest.cc +++ b/app/gfx/color_utils_unittest.cc @@ -4,36 +4,32 @@ #include -#include "skia/ext/skia_utils.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(SkiaUtils, SkColorToHSLRed) { - SkColor red = SkColorSetARGB(255, 255, 0, 0); - skia::HSL hsl = { 0, 0, 0 }; - skia::SkColorToHSL(red, hsl); +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(SkiaUtils, SkColorToHSLGrey) { - SkColor red = SkColorSetARGB(255, 128, 128, 128); - skia::HSL hsl = { 0, 0, 0 }; - skia::SkColorToHSL(red, hsl); +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(hsl.l * 100), static_cast(0.5 * 100)); // Accurate to two decimal places. } -TEST(SkiaUtils, HSLToSkColorWithAlpha) { +TEST(ColorUtils, HSLToSkColorWithAlpha) { SkColor red = SkColorSetARGB(128, 255, 0, 0); - - skia::HSL hsl = { 0, 1, 0.5 }; - - SkColor result = skia::HSLToSkColor(128, hsl); + 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)); diff --git a/app/gfx/skbitmap_operations.cc b/app/gfx/skbitmap_operations.cc new file mode 100644 index 00000000000000..174df68eec2b33 --- /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((SkColorGetA(first_pixel) * first_alpha) + + (SkColorGetA(second_pixel) * alpha)); + int r = static_cast((SkColorGetR(first_pixel) * first_alpha) + + (SkColorGetR(second_pixel) * alpha)); + int g = static_cast((SkColorGetG(first_pixel) * first_alpha) + + (SkColorGetG(second_pixel) * alpha)); + int b = static_cast((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(img_a) / 255.0; + double img_inv = 1 - img_alpha; + + double mask_a = static_cast(SkColorGetA(mask_row[x])) / 255.0; + + dst_row[x] = SkColorSetARGB( + static_cast(std::min(255.0, bg_a + img_a) * mask_a), + static_cast(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a), + static_cast(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a), + static_cast(((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 00000000000000..d6bb5d3d69d393 --- /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 00000000000000..feb288cfe6f70d --- /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(SkColorGetB(a) - SkColorGetB(b))) < 2 && + abs(static_cast(SkColorGetG(a) - SkColorGetG(b))) < 2 && + abs(static_cast(SkColorGetR(a) - SkColorGetR(b))) < 2 && + abs(static_cast(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(bmp->getAddr32(0, 0)); + for (int i = 0; i < w * h; i++) { + src_data[i * 4 + 0] = static_cast(i % 255); + src_data[i * 4 + 1] = static_cast(i % 255); + src_data[i * 4 + 2] = static_cast(i % 255); + src_data[i * 4 + 3] = static_cast(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((255 + ((255 - i) % 255)) / 2), + SkColorGetA(*blended.getAddr32(x, y))); + EXPECT_EQ(static_cast(i % 255 / 2), + SkColorGetR(*blended.getAddr32(x, y))); + EXPECT_EQ((static_cast((i * 2) % 255 + (i * 4) % 255) / 2), + SkColorGetG(*blended.getAddr32(x, y))); + EXPECT_EQ(static_cast(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()); +} diff --git a/chrome/browser/browser_theme_provider.cc b/chrome/browser/browser_theme_provider.cc index 3b1326ce13ac17..fa6aa89e4c25e0 100644 --- a/chrome/browser/browser_theme_provider.cc +++ b/chrome/browser/browser_theme_provider.cc @@ -4,6 +4,7 @@ #include "chrome/browser/browser_theme_provider.h" +#include "app/gfx/skbitmap_operations.h" #include "base/file_util.h" #include "base/string_util.h" #include "base/gfx/png_decoder.h" @@ -25,10 +26,9 @@ #include "grit/theme_resources.h" #include "net/base/file_stream.h" #include "net/base/net_errors.h" -#include "skia/ext/image_operations.h" -#include "skia/ext/skia_utils.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkUnPreMultiply.h" #if defined(OS_WIN) #include "app/win_util.h" @@ -125,15 +125,17 @@ const SkColor BrowserThemeProvider::kDefaultColorControlBackground = NULL; const SkColor BrowserThemeProvider::kDefaultColorButtonBackground = NULL; // Default tints. -const skia::HSL BrowserThemeProvider::kDefaultTintButtons = { -1, -1, -1 }; -const skia::HSL BrowserThemeProvider::kDefaultTintFrame = { -1, -1, -1 }; -const skia::HSL BrowserThemeProvider::kDefaultTintFrameInactive = +const color_utils::HSL BrowserThemeProvider::kDefaultTintButtons = + { -1, -1, -1 }; +const color_utils::HSL BrowserThemeProvider::kDefaultTintFrame = { -1, -1, -1 }; +const color_utils::HSL BrowserThemeProvider::kDefaultTintFrameInactive = { -1, -1, 0.75f }; -const skia::HSL BrowserThemeProvider::kDefaultTintFrameIncognito = +const color_utils::HSL BrowserThemeProvider::kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; -const skia::HSL BrowserThemeProvider::kDefaultTintFrameIncognitoInactive = +const color_utils::HSL + BrowserThemeProvider::kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; -const skia::HSL BrowserThemeProvider::kDefaultTintBackgroundTab = +const color_utils::HSL BrowserThemeProvider::kDefaultTintBackgroundTab = { -1, 0.5, 0.75 }; // Saved default values. @@ -680,7 +682,7 @@ const std::string BrowserThemeProvider::GetTintKey(int id) { } } -skia::HSL BrowserThemeProvider::GetDefaultTint(int id) { +color_utils::HSL BrowserThemeProvider::GetDefaultTint(int id) { switch (id) { case TINT_FRAME: return kDefaultTintFrame; @@ -695,23 +697,20 @@ skia::HSL BrowserThemeProvider::GetDefaultTint(int id) { case TINT_BACKGROUND_TAB: return kDefaultTintBackgroundTab; default: - skia::HSL result = {-1, -1, -1}; + color_utils::HSL result = {-1, -1, -1}; return result; } } -skia::HSL BrowserThemeProvider::GetTint(int id) { +color_utils::HSL BrowserThemeProvider::GetTint(int id) { DCHECK(CalledOnValidThread()); TintMap::iterator tint_iter = tints_.find(GetTintKey(id)); - if (tint_iter != tints_.end()) - return tint_iter->second; - else - return GetDefaultTint(id); + return (tint_iter == tints_.end()) ? GetDefaultTint(id) : tint_iter->second; } SkBitmap BrowserThemeProvider::TintBitmap(const SkBitmap& bitmap, int hsl_id) { - return skia::ImageOperations::CreateHSLShiftedBitmap(bitmap, GetTint(hsl_id)); + return SkBitmapOperations::CreateHSLShiftedBitmap(bitmap, GetTint(hsl_id)); } void BrowserThemeProvider::SetImageData(DictionaryValue* images_value, @@ -784,7 +783,7 @@ void BrowserThemeProvider::SetTintData(DictionaryValue* tints_value) { ListValue* tint_list; if (tints_value->GetList(*iter, &tint_list) && tint_list->GetSize() == 3) { - skia::HSL hsl = { -1, -1, -1 }; + color_utils::HSL hsl = { -1, -1, -1 }; int value = 0; if (!tint_list->GetReal(0, &hsl.h) && tint_list->GetInteger(0, &value)) hsl.h = value; @@ -912,7 +911,8 @@ void BrowserThemeProvider::SetColor(const char* key, const SkColor& color) { colors_[key] = color; } -void BrowserThemeProvider::SetTint(const char* key, const skia::HSL& tint) { +void BrowserThemeProvider::SetTint(const char* key, + const color_utils::HSL& tint) { tints_[key] = tint; } @@ -1057,9 +1057,8 @@ SkBitmap* BrowserThemeProvider::GenerateBitmap(int id) { SkBitmap bg_tint = TintBitmap(*(it->second), TINT_BACKGROUND_TAB); int vertical_offset = HasCustomImage(id) ? kRestoredTabVerticalOffset : 0; - SkBitmap* bg_tab = new SkBitmap( - skia::ImageOperations::CreateTiledBitmap(bg_tint, 0, - vertical_offset, bg_tint.width(), bg_tint.height())); + SkBitmap* bg_tab = new SkBitmap(SkBitmapOperations::CreateTiledBitmap( + bg_tint, 0, vertical_offset, bg_tint.width(), bg_tint.height())); // If they've provided a custom image, overlay it. if (HasCustomImage(id)) { @@ -1143,7 +1142,7 @@ void BrowserThemeProvider::SaveTintData() { if (tints_.size()) { TintMap::iterator iter = tints_.begin(); while (iter != tints_.end()) { - skia::HSL hsl = (*iter).second; + color_utils::HSL hsl = (*iter).second; ListValue* hsl_list = new ListValue(); hsl_list->Set(0, Value::CreateRealValue(hsl.h)); hsl_list->Set(1, Value::CreateRealValue(hsl.s)); diff --git a/chrome/browser/browser_theme_provider.h b/chrome/browser/browser_theme_provider.h index 5313cec4c166ee..0d11a6a969ece0 100644 --- a/chrome/browser/browser_theme_provider.h +++ b/chrome/browser/browser_theme_provider.h @@ -9,12 +9,12 @@ #include #include +#include "app/gfx/color_utils.h" #include "app/resource_bundle.h" #include "app/theme_provider.h" #include "base/basictypes.h" #include "base/non_thread_safe.h" #include "base/ref_counted.h" -#include "skia/ext/skia_utils.h" class Extension; class Profile; @@ -93,12 +93,12 @@ class BrowserThemeProvider : public base::RefCounted, static const SkColor kDefaultColorControlBackground; static const SkColor kDefaultColorButtonBackground; - static const skia::HSL kDefaultTintButtons; - static const skia::HSL kDefaultTintFrame; - static const skia::HSL kDefaultTintFrameInactive; - static const skia::HSL kDefaultTintFrameIncognito; - static const skia::HSL kDefaultTintFrameIncognitoInactive; - static const skia::HSL kDefaultTintBackgroundTab; + static const color_utils::HSL kDefaultTintButtons; + static const color_utils::HSL kDefaultTintFrame; + static const color_utils::HSL kDefaultTintFrameInactive; + static const color_utils::HSL kDefaultTintFrameIncognito; + static const color_utils::HSL kDefaultTintFrameIncognitoInactive; + static const color_utils::HSL kDefaultTintBackgroundTab; static const char* kDefaultThemeID; @@ -209,10 +209,10 @@ class BrowserThemeProvider : public base::RefCounted, void SetColor(const char* id, const SkColor& color); // Sets an individual tint value. - void SetTint(const char* id, const skia::HSL& tint); + void SetTint(const char* id, const color_utils::HSL& tint); // Get the specified tint - |id| is one of the TINT_* enum values. - skia::HSL GetTint(int id); + color_utils::HSL GetTint(int id); // Generate any frame colors that weren't specified. void GenerateFrameColors(); @@ -255,7 +255,7 @@ class BrowserThemeProvider : public base::RefCounted, private: typedef std::map ImageMap; typedef std::map ColorMap; - typedef std::map TintMap; + typedef std::map TintMap; typedef std::map DisplayPropertyMap; typedef std::map > RawDataMap; typedef std::map ResourceNameMap; @@ -264,7 +264,7 @@ class BrowserThemeProvider : public base::RefCounted, const std::string GetTintKey(int id); // Returns the default tint for the given tint |id| TINT_* enum value. - skia::HSL GetDefaultTint(int id); + color_utils::HSL GetDefaultTint(int id); // Returns the string key for the given color |id| COLOR_* enum value. const std::string GetColorKey(int id); diff --git a/chrome/browser/browser_theme_provider_gtk.cc b/chrome/browser/browser_theme_provider_gtk.cc index 2787dcee4eb179..ac04e6544e1631 100644 --- a/chrome/browser/browser_theme_provider_gtk.cc +++ b/chrome/browser/browser_theme_provider_gtk.cc @@ -7,6 +7,7 @@ #include "app/l10n_util.h" #include "base/gfx/gtk_util.h" #include "base/logging.h" +#include "third_party/skia/include/core/SkBitmap.h" GdkPixbuf* BrowserThemeProvider::GetPixbufNamed(int id) { return GetPixbufImpl(id, false); diff --git a/chrome/browser/browser_theme_provider_mac.mm b/chrome/browser/browser_theme_provider_mac.mm index 063ec25875f465..6df03367bab470 100644 --- a/chrome/browser/browser_theme_provider_mac.mm +++ b/chrome/browser/browser_theme_provider_mac.mm @@ -6,14 +6,14 @@ #import +#include "app/gfx/color_utils.h" #include "base/logging.h" -#include "skia/ext/skia_utils.h" #include "skia/ext/skia_utils_mac.h" namespace { -void HSLToHSB(const skia::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { - SkColor color = skia::HSLToSkColor(1.0, hsl); // alpha value doesn't matter +void HSLToHSB(const color_utils::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { + SkColor color = color_utils::HSLToSkColor(hsl, 255); // alpha doesn't matter SkScalar hsv[3]; SkColorToHSV(color, hsv); @@ -106,7 +106,7 @@ void HSLToHSB(const skia::HSL& hsl, CGFloat* h, CGFloat* s, CGFloat* b) { TintMap::iterator tint_iter = tints_.find(GetTintKey(id)); if (tint_iter != tints_.end()) { - skia::HSL tint = tint_iter->second; + color_utils::HSL tint = tint_iter->second; CGFloat hue, saturation, brightness; HSLToHSB(tint, &hue, &saturation, &brightness); diff --git a/chrome/browser/dom_ui/dom_ui_theme_source.cc b/chrome/browser/dom_ui/dom_ui_theme_source.cc index b956c3e5780bb9..d2607be828e93e 100644 --- a/chrome/browser/dom_ui/dom_ui_theme_source.cc +++ b/chrome/browser/dom_ui/dom_ui_theme_source.cc @@ -135,11 +135,11 @@ void DOMUIThemeSource::InitNewTabCSS() { SkColor color_header = tp->GetColor(BrowserThemeProvider::COLOR_NTP_HEADER); // Generate a lighter color for the header gradients. - skia::HSL header_lighter; - skia::SkColorToHSL(color_header, header_lighter); + color_utils::HSL header_lighter; + color_utils::SkColorToHSL(color_header, &header_lighter); header_lighter.l += (1 - header_lighter.l) * 0.33; SkColor color_header_gradient_light = - skia::HSLToSkColor(SkColorGetA(color_header), header_lighter); + color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header)); // Generate section border color from the header color. See // BookmarkBarView::Paint for how we do this for the bookmark bar diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc index 6b3b2f1e46296c..c52780b926d176 100644 --- a/chrome/browser/download/download_util.cc +++ b/chrome/browser/download/download_util.cc @@ -12,6 +12,7 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "base/file_util.h" +#include "base/gfx/rect.h" #include "base/string_util.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/download/download_item_model.h" diff --git a/chrome/browser/gtk/browser_titlebar.cc b/chrome/browser/gtk/browser_titlebar.cc index fe85dbb8cf4a90..248b945b85cc1d 100644 --- a/chrome/browser/gtk/browser_titlebar.cc +++ b/chrome/browser/gtk/browser_titlebar.cc @@ -10,6 +10,7 @@ #include #include +#include "app/gfx/skbitmap_operations.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "base/gfx/gtk_util.h" @@ -31,7 +32,6 @@ #include "grit/app_resources.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "skia/ext/image_operations.h" namespace { @@ -93,12 +93,12 @@ GdkPixbuf* GetOTRAvatar() { return otr_avatar; } -// Converts a GdkColor to a skia::HSL. -skia::HSL GdkColorToHSL(const GdkColor* color) { - skia::HSL hsl; - skia::SkColorToHSL(SkColorSetRGB(color->red >> 8, - color->green >> 8, - color->blue >> 8), hsl); +// Converts a GdkColor to a color_utils::HSL. +color_utils::HSL GdkColorToHSL(const GdkColor* color) { + color_utils::HSL hsl; + color_utils::SkColorToHSL(SkColorSetRGB(color->red >> 8, + color->green >> 8, + color->blue >> 8), &hsl); return hsl; } @@ -107,10 +107,10 @@ skia::HSL GdkColorToHSL(const GdkColor* color) { GdkColor PickLuminosityContrastingColor(const GdkColor* base, const GdkColor* one, const GdkColor* two) { - // Convert all GdkColors to skia::HSLs. - skia::HSL baseHSL = GdkColorToHSL(base); - skia::HSL oneHSL = GdkColorToHSL(one); - skia::HSL twoHSL = GdkColorToHSL(two); + // Convert all GdkColors to color_utils::HSLs. + color_utils::HSL baseHSL = GdkColorToHSL(base); + color_utils::HSL oneHSL = GdkColorToHSL(one); + color_utils::HSL twoHSL = GdkColorToHSL(two); double one_difference = fabs(baseHSL.l - oneHSL.l); double two_difference = fabs(baseHSL.l - twoHSL.l); @@ -591,7 +591,7 @@ static void MakeThrobberFrames(int resource_id, // Make a separate GdkPixbuf for each frame of the animation. for (size_t i = 0; i < num_frames; ++i) { - SkBitmap frame = skia::ImageOperations::CreateTiledBitmap(*frame_strip, + SkBitmap frame = SkBitmapOperations::CreateTiledBitmap(*frame_strip, i * frame_size, 0, frame_size, frame_size); frames->push_back(gfx::GdkPixbufFromSkBitmap(&frame)); } diff --git a/chrome/browser/gtk/browser_window_gtk.cc b/chrome/browser/gtk/browser_window_gtk.cc index 8b2dae50c5004d..cadfb08ca37266 100644 --- a/chrome/browser/gtk/browser_window_gtk.cc +++ b/chrome/browser/gtk/browser_window_gtk.cc @@ -9,6 +9,7 @@ #include +#include "app/gfx/color_utils.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "app/theme_provider.h" @@ -75,7 +76,6 @@ #include "grit/generated_resources.h" #include "grit/google_chrome_strings.h" #include "grit/theme_resources.h" -#include "skia/ext/skia_utils.h" #include "skia/ext/skia_utils_gtk.h" #if defined(OS_CHROMEOS) @@ -1767,8 +1767,8 @@ void BrowserWindowGtk::SetBackgroundColor() { // When the cursor is over the divider, GTK+ normally lightens the background // color by 1.3 (see LIGHTNESS_MULT in gtkstyle.c). Since we're setting the // color, override the prelight also. - skia::HSL hsl = { -1, 0.5, 0.65 }; - SkColor frame_prelight_color = skia::HSLShift(frame_color, hsl); + color_utils::HSL hsl = { -1, 0.5, 0.65 }; + SkColor frame_prelight_color = color_utils::HSLShift(frame_color, hsl); GdkColor frame_prelight_color_gdk = SkColorToGdkColor(frame_prelight_color); gtk_widget_modify_bg(contents_split_, GTK_STATE_PRELIGHT, &frame_prelight_color_gdk); diff --git a/chrome/browser/gtk/custom_button.cc b/chrome/browser/gtk/custom_button.cc index 745f49543cadaa..3e4e1cfc38784e 100644 --- a/chrome/browser/gtk/custom_button.cc +++ b/chrome/browser/gtk/custom_button.cc @@ -4,6 +4,7 @@ #include "chrome/browser/gtk/custom_button.h" +#include "app/gfx/skbitmap_operations.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "app/theme_provider.h" @@ -15,7 +16,6 @@ #include "chrome/common/gtk_util.h" #include "chrome/common/notification_service.h" #include "grit/theme_resources.h" -#include "skia/ext/image_operations.h" CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeProvider* theme_provider, int normal_id, int active_id, int highlight_id, int depressed_id) @@ -104,8 +104,8 @@ void CustomDrawButtonBase::SetBackground(SkColor color, background_image_->UsePixbuf(NULL); } } else { - SkBitmap img = skia::ImageOperations::CreateButtonBackground(color, - *image, *mask); + SkBitmap img = + SkBitmapOperations::CreateButtonBackground(color, *image, *mask); GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&img); background_image_->UsePixbuf(pixbuf); diff --git a/chrome/browser/gtk/gtk_theme_provider.cc b/chrome/browser/gtk/gtk_theme_provider.cc index 428e58b10d37fc..15d0b7758ac39f 100644 --- a/chrome/browser/gtk/gtk_theme_provider.cc +++ b/chrome/browser/gtk/gtk_theme_provider.cc @@ -6,6 +6,7 @@ #include +#include "app/gfx/color_utils.h" #include "base/gfx/gtk_util.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/profile.h" @@ -31,9 +32,9 @@ namespace { const int kToolbarImageWidth = 64; const int kToolbarImageHeight = 128; -const skia::HSL kExactColor = { -1, -1, -1 }; +const color_utils::HSL kExactColor = { -1, -1, -1 }; -const skia::HSL kDefaultFrameShift = { -1, -1, 0.4 }; +const color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 }; // Values used as the new luminance and saturation values in the inactive tab // text color. @@ -352,8 +353,8 @@ void GtkThemeProvider::LoadGtkValues() { // color, change the luminosity of the frame color downwards to 80% of what // it currently is. This is in a futile attempt to match the default // metacity and xfwm themes. - SkColor shifted = skia::HSLShift(GdkToSkColor(&frame_color), - kDefaultFrameShift); + SkColor shifted = color_utils::HSLShift(GdkToSkColor(&frame_color), + kDefaultFrameShift); frame_color.pixel = 0; frame_color.red = SkColorGetR(shifted) * kSkiaToGDKMultiplier; frame_color.green = SkColorGetG(shifted) * kSkiaToGDKMultiplier; @@ -363,15 +364,15 @@ void GtkThemeProvider::LoadGtkValues() { // By default, the button tint color is the background selection color. But // this can be unreadable in some dark themes, so we set a minimum contrast // between the button color and the toolbar color. - skia::HSL button_hsl; - skia::SkColorToHSL(GdkToSkColor(&button_color), button_hsl); - skia::HSL toolbar_hsl; - skia::SkColorToHSL(GdkToSkColor(&toolbar_color), toolbar_hsl); + color_utils::HSL button_hsl; + color_utils::SkColorToHSL(GdkToSkColor(&button_color), &button_hsl); + color_utils::HSL toolbar_hsl; + color_utils::SkColorToHSL(GdkToSkColor(&toolbar_color), &toolbar_hsl); double hsl_difference = fabs(button_hsl.l - toolbar_hsl.l); if (hsl_difference <= kMinimumLuminanceDifference) { // Not enough contrast. Try the text color instead. - skia::HSL label_hsl; - skia::SkColorToHSL(GdkToSkColor(&label_color), label_hsl); + color_utils::HSL label_hsl; + color_utils::SkColorToHSL(GdkToSkColor(&label_color), &label_hsl); double label_difference = fabs(label_hsl.l - toolbar_hsl.l); if (label_difference >= kMinimumLuminanceDifference) { button_color = label_color; @@ -405,7 +406,7 @@ void GtkThemeProvider::LoadGtkValues() { // background tab color, with the lightness and saturation moved in the // opposite direction. (We don't touch the hue, since there should be subtle // hints of the color in the text.) - skia::HSL inactive_tab_text_hsl = GetTint(TINT_BACKGROUND_TAB); + color_utils::HSL inactive_tab_text_hsl = GetTint(TINT_BACKGROUND_TAB); if (inactive_tab_text_hsl.l < 0.5) inactive_tab_text_hsl.l = kDarkInactiveLuminance; else @@ -417,7 +418,7 @@ void GtkThemeProvider::LoadGtkValues() { inactive_tab_text_hsl.s = kLightInactiveSaturation; SetColor(kColorBackgroundTabText, - skia::HSLToSkColor(255, inactive_tab_text_hsl)); + color_utils::HSLToSkColor(inactive_tab_text_hsl, 255)); // The inactive color/tint is special: We *must* use the exact insensitive // color for all inactive windows, otherwise we end up neon pink half the @@ -437,10 +438,12 @@ void GtkThemeProvider::SetThemeColorFromGtk(const char* id, GdkColor* color) { SetColor(id, GdkToSkColor(color)); } -void GtkThemeProvider::SetThemeTintFromGtk(const char* id, GdkColor* color, - const skia::HSL& default_tint) { - skia::HSL hsl; - skia::SkColorToHSL(GdkToSkColor(color), hsl); +void GtkThemeProvider::SetThemeTintFromGtk( + const char* id, + GdkColor* color, + const color_utils::HSL& default_tint) { + color_utils::HSL hsl; + color_utils::SkColorToHSL(GdkToSkColor(color), &hsl); if (default_tint.s != -1) hsl.s = default_tint.s; diff --git a/chrome/browser/gtk/gtk_theme_provider.h b/chrome/browser/gtk/gtk_theme_provider.h index 14b6e73256a833..a7b7ab38589648 100644 --- a/chrome/browser/gtk/gtk_theme_provider.h +++ b/chrome/browser/gtk/gtk_theme_provider.h @@ -9,12 +9,11 @@ #include #include +#include "app/gfx/color_utils.h" #include "chrome/browser/browser_theme_provider.h" #include "chrome/common/notification_observer.h" #include "chrome/common/owned_widget_gtk.h" -#include "skia/ext/skia_utils.h" - class CairoCachedSurface; class Profile; @@ -112,7 +111,7 @@ class GtkThemeProvider : public BrowserThemeProvider, // Sets the underlying theme colors/tints from a GTK color. void SetThemeColorFromGtk(const char* id, GdkColor* color); void SetThemeTintFromGtk(const char* id, GdkColor* color, - const skia::HSL& default_tint); + const color_utils::HSL& default_tint); // Split out from FreePlatformCaches so it can be called in our destructor; // FreePlatformCaches() is called from the BrowserThemeProvider's destructor, diff --git a/chrome/browser/gtk/tabs/tab_renderer_gtk.cc b/chrome/browser/gtk/tabs/tab_renderer_gtk.cc index 213475a517ebbe..658d01fad36642 100644 --- a/chrome/browser/gtk/tabs/tab_renderer_gtk.cc +++ b/chrome/browser/gtk/tabs/tab_renderer_gtk.cc @@ -9,6 +9,7 @@ #include "app/gfx/canvas_paint.h" #include "app/gfx/favicon_size.h" +#include "app/gfx/skbitmap_operations.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "chrome/browser/browser.h" @@ -23,7 +24,6 @@ #include "grit/app_resources.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "skia/ext/image_operations.h" namespace { @@ -697,13 +697,13 @@ SkBitmap* TabRendererGtk::GetMaskedBitmap(const SkBitmap* mask, // offsets. delete it->second.bitmap; } - SkBitmap image = skia::ImageOperations::CreateTiledBitmap( + SkBitmap image = SkBitmapOperations::CreateTiledBitmap( *background, bg_offset_x, bg_offset_y, mask->width(), height() + kToolbarOverlap); CachedBitmap bitmap = { bg_offset_x, bg_offset_y, - new SkBitmap(skia::ImageOperations::CreateMaskedBitmap(image, *mask)) + new SkBitmap(SkBitmapOperations::CreateMaskedBitmap(image, *mask)) }; cached_bitmaps_[std::make_pair(mask, background)] = bitmap; return bitmap.bitmap; diff --git a/chrome/browser/tab_contents/thumbnail_generator.cc b/chrome/browser/tab_contents/thumbnail_generator.cc index 7d20381074c6d9..1a5c5c4580a9a8 100644 --- a/chrome/browser/tab_contents/thumbnail_generator.cc +++ b/chrome/browser/tab_contents/thumbnail_generator.cc @@ -6,13 +6,13 @@ #include +#include "app/gfx/skbitmap_operations.h" #include "base/histogram.h" #include "base/time.h" #include "chrome/browser/renderer_host/backing_store.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/common/notification_service.h" #include "chrome/common/property_bag.h" -#include "skia/ext/image_operations.h" #include "skia/ext/platform_canvas.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -139,9 +139,8 @@ SkBitmap GetThumbnailForBackingStore(BackingStore* backing_store) { NOTIMPLEMENTED(); #endif - result = skia::ImageOperations::DownsampleByTwoUntilSize( - bmp, - kThumbnailWidth, kThumbnailHeight); + result = SkBitmapOperations::DownsampleByTwoUntilSize(bmp, kThumbnailWidth, + kThumbnailHeight); #if defined(OS_WIN) // This is a bit subtle. SkBitmaps are refcounted, but the magic ones in diff --git a/chrome/browser/views/bookmark_bar_view.cc b/chrome/browser/views/bookmark_bar_view.cc index bf0ec9afa0e272..586422abaea568 100644 --- a/chrome/browser/views/bookmark_bar_view.cc +++ b/chrome/browser/views/bookmark_bar_view.cc @@ -37,7 +37,6 @@ #include "grit/app_resources.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "skia/ext/skia_utils.h" #include "views/controls/button/menu_button.h" #include "views/controls/menu/menu_item_view.h" #include "views/drag_utils.h" diff --git a/chrome/browser/views/bookmark_manager_view.cc b/chrome/browser/views/bookmark_manager_view.cc index 384a2ceb68919a..520334653dc152 100644 --- a/chrome/browser/views/bookmark_manager_view.cc +++ b/chrome/browser/views/bookmark_manager_view.cc @@ -29,6 +29,7 @@ #include "grit/generated_resources.h" #include "grit/locale_settings.h" #include "skia/ext/skia_utils.h" +#include "third_party/skia/include/core/SkShader.h" #include "views/grid_layout.h" #include "views/controls/button/menu_button.h" #include "views/controls/menu/menu_item_view.h" diff --git a/chrome/browser/views/detachable_toolbar_view.cc b/chrome/browser/views/detachable_toolbar_view.cc index 8363620cd83e51..10613bbcb2130c 100644 --- a/chrome/browser/views/detachable_toolbar_view.cc +++ b/chrome/browser/views/detachable_toolbar_view.cc @@ -7,7 +7,9 @@ #include "app/gfx/canvas.h" #include "chrome/browser/browser_theme_provider.h" #include "grit/theme_resources.h" +#include "skia/ext/skia_utils.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkShader.h" // How round the 'new tab' style bookmarks bar is. static const int kNewtabBarRoundness = 5; diff --git a/chrome/browser/views/extensions/extension_shelf.cc b/chrome/browser/views/extensions/extension_shelf.cc index 0b9ae7c43d47c5..efab520ed99240 100644 --- a/chrome/browser/views/extensions/extension_shelf.cc +++ b/chrome/browser/views/extensions/extension_shelf.cc @@ -24,7 +24,6 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/notification_service.h" #include "chrome/common/pref_names.h" -#include "skia/ext/skia_utils.h" #include "views/controls/label.h" #include "views/screen.h" #include "views/widget/root_view.h" diff --git a/chrome/browser/views/tabs/tab_2.cc b/chrome/browser/views/tabs/tab_2.cc index 806506092d759a..8cfe8a517a3e17 100644 --- a/chrome/browser/views/tabs/tab_2.cc +++ b/chrome/browser/views/tabs/tab_2.cc @@ -7,6 +7,7 @@ #include "app/gfx/canvas.h" #include "app/gfx/font.h" #include "app/gfx/path.h" +#include "app/gfx/skbitmap_operations.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "app/slide_animation.h" @@ -17,7 +18,6 @@ #include "grit/app_resources.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "skia/ext/image_operations.h" #include "views/animator.h" #include "views/controls/button/image_button.h" #include "views/widget/widget.h" @@ -525,23 +525,21 @@ void Tab2::PaintInactiveTabBackground(gfx::Canvas* canvas) { // Draw left edge. Don't draw over the toolbar, as we're not the foreground // tab. - SkBitmap tab_l = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, offset, background_offset_.y(), - tab_active_.l_width, height()); - SkBitmap theme_l = skia::ImageOperations::CreateMaskedBitmap( - tab_l, *tab_alpha_.image_l); + SkBitmap tab_l = SkBitmapOperations::CreateTiledBitmap( + *tab_bg, offset, background_offset_.y(), tab_active_.l_width, height()); + SkBitmap theme_l = + SkBitmapOperations::CreateMaskedBitmap(tab_l, *tab_alpha_.image_l); canvas->DrawBitmapInt(theme_l, 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, false); // Draw right edge. Again, don't draw over the toolbar. - SkBitmap tab_r = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, + SkBitmap tab_r = SkBitmapOperations::CreateTiledBitmap(*tab_bg, offset + width() - tab_active_.r_width, background_offset_.y(), tab_active_.r_width, height()); - SkBitmap theme_r = skia::ImageOperations::CreateMaskedBitmap( - tab_r, *tab_alpha_.image_r); + SkBitmap theme_r = + SkBitmapOperations::CreateMaskedBitmap(tab_r, *tab_alpha_.image_r); canvas->DrawBitmapInt(theme_r, 0, 0, theme_r.width(), theme_r.height() - kToolbarOverlap, width() - theme_r.width(), 0, theme_r.width(), @@ -576,19 +574,17 @@ void Tab2::PaintActiveTabBackground(gfx::Canvas* canvas) { SkBitmap* tab_bg = GetThemeProvider()->GetBitmapNamed(IDR_THEME_TOOLBAR); // Draw left edge. - SkBitmap tab_l = skia::ImageOperations::CreateTiledBitmap( + SkBitmap tab_l = SkBitmapOperations::CreateTiledBitmap( *tab_bg, offset, 0, tab_active_.l_width, height()); - SkBitmap theme_l = skia::ImageOperations::CreateMaskedBitmap( - tab_l, *tab_alpha_.image_l); + SkBitmap theme_l = + SkBitmapOperations::CreateMaskedBitmap(tab_l, *tab_alpha_.image_l); canvas->DrawBitmapInt(theme_l, 0, 0); // Draw right edge. - SkBitmap tab_r = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, - offset + width() - tab_active_.r_width, 0, - tab_active_.r_width, height()); - SkBitmap theme_r = skia::ImageOperations::CreateMaskedBitmap( - tab_r, *tab_alpha_.image_r); + SkBitmap tab_r = SkBitmapOperations::CreateTiledBitmap(*tab_bg, + offset + width() - tab_active_.r_width, 0, tab_active_.r_width, height()); + SkBitmap theme_r = + SkBitmapOperations::CreateMaskedBitmap(tab_r, *tab_alpha_.image_r); canvas->DrawBitmapInt(theme_r, width() - tab_active_.r_width, 0); // Draw center. Instead of masking out the top portion we simply skip over it @@ -607,12 +603,12 @@ void Tab2::PaintActiveTabBackground(gfx::Canvas* canvas) { } void Tab2::PaintHoverTabBackground(gfx::Canvas* canvas, double opacity) { - SkBitmap left = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive_.image_l, *tab_active_.image_l, opacity); - SkBitmap center = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive_.image_c, *tab_active_.image_c, opacity); - SkBitmap right = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive_.image_r, *tab_active_.image_r, opacity); + SkBitmap left = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive_.image_l, *tab_active_.image_l, opacity); + SkBitmap center = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive_.image_c, *tab_active_.image_c, opacity); + SkBitmap right = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive_.image_r, *tab_active_.image_r, opacity); canvas->DrawBitmapInt(left, 0, 0); canvas->TileImageInt(center, tab_active_.l_width, 0, diff --git a/chrome/browser/views/tabs/tab_overview_cell.cc b/chrome/browser/views/tabs/tab_overview_cell.cc index 0dc46e514fed37..4bebd42052cd0f 100644 --- a/chrome/browser/views/tabs/tab_overview_cell.cc +++ b/chrome/browser/views/tabs/tab_overview_cell.cc @@ -5,8 +5,8 @@ #include "chrome/browser/views/tabs/tab_overview_cell.h" #include "app/gfx/favicon_size.h" +#include "app/gfx/skbitmap_operations.h" #include "base/string_util.h" -#include "skia/ext/image_operations.h" #include "views/border.h" #include "views/controls/image_view.h" #include "views/controls/label.h" @@ -67,7 +67,7 @@ TabOverviewCell::TabOverviewCell() : configured_thumbnail_(false) { void TabOverviewCell::SetThumbnail(const SkBitmap& thumbnail) { // Do mipmapped-based resampling to get closer to the correct size. The // input bitmap isn't guaranteed to have any specific resolution. - thumbnail_view_->SetImage(skia::ImageOperations::DownsampleByTwoUntilSize( + thumbnail_view_->SetImage(SkBitmapOperations::DownsampleByTwoUntilSize( thumbnail, kThumbnailWidth, kThumbnailHeight)); configured_thumbnail_ = true; } diff --git a/chrome/browser/views/tabs/tab_renderer.cc b/chrome/browser/views/tabs/tab_renderer.cc index 8e051c3a2e83e7..79664fd150b414 100644 --- a/chrome/browser/views/tabs/tab_renderer.cc +++ b/chrome/browser/views/tabs/tab_renderer.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. @@ -9,6 +9,7 @@ #include "app/gfx/canvas.h" #include "app/gfx/favicon_size.h" #include "app/gfx/font.h" +#include "app/gfx/skbitmap_operations.h" #include "app/l10n_util.h" #include "app/resource_bundle.h" #include "chrome/browser/browser.h" @@ -20,7 +21,6 @@ #include "grit/app_resources.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "skia/ext/image_operations.h" #include "views/widget/widget.h" #include "views/window/non_client_view.h" #include "views/window/window.h" @@ -657,23 +657,21 @@ void TabRenderer::PaintInactiveTabBackground(gfx::Canvas* canvas) { // Draw left edge. Don't draw over the toolbar, as we're not the foreground // tab. - SkBitmap tab_l = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, offset, bg_offset_y, - tab_active.l_width, height()); - SkBitmap theme_l = skia::ImageOperations::CreateMaskedBitmap( - tab_l, *tab_alpha.image_l); + SkBitmap tab_l = SkBitmapOperations::CreateTiledBitmap( + *tab_bg, offset, bg_offset_y, tab_active.l_width, height()); + SkBitmap theme_l = + SkBitmapOperations::CreateMaskedBitmap(tab_l, *tab_alpha.image_l); canvas->DrawBitmapInt(theme_l, 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, false); // Draw right edge. Again, don't draw over the toolbar. - SkBitmap tab_r = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, + SkBitmap tab_r = SkBitmapOperations::CreateTiledBitmap(*tab_bg, offset + width() - tab_active.r_width, bg_offset_y, tab_active.r_width, height()); - SkBitmap theme_r = skia::ImageOperations::CreateMaskedBitmap( - tab_r, *tab_alpha.image_r); + SkBitmap theme_r = + SkBitmapOperations::CreateMaskedBitmap(tab_r, *tab_alpha.image_r); canvas->DrawBitmapInt(theme_r, 0, 0, theme_r.width(), theme_r.height() - kToolbarOverlap, width() - theme_r.width(), 0, theme_r.width(), @@ -708,19 +706,17 @@ void TabRenderer::PaintActiveTabBackground(gfx::Canvas* canvas) { SkBitmap* tab_bg = GetThemeProvider()->GetBitmapNamed(IDR_THEME_TOOLBAR); // Draw left edge. - SkBitmap tab_l = skia::ImageOperations::CreateTiledBitmap( + SkBitmap tab_l = SkBitmapOperations::CreateTiledBitmap( *tab_bg, offset, 0, tab_active.l_width, height()); - SkBitmap theme_l = skia::ImageOperations::CreateMaskedBitmap( - tab_l, *tab_alpha.image_l); + SkBitmap theme_l = + SkBitmapOperations::CreateMaskedBitmap(tab_l, *tab_alpha.image_l); canvas->DrawBitmapInt(theme_l, 0, 0); // Draw right edge. - SkBitmap tab_r = skia::ImageOperations::CreateTiledBitmap( - *tab_bg, - offset + width() - tab_active.r_width, 0, - tab_active.r_width, height()); - SkBitmap theme_r = skia::ImageOperations::CreateMaskedBitmap( - tab_r, *tab_alpha.image_r); + SkBitmap tab_r = SkBitmapOperations::CreateTiledBitmap(*tab_bg, + offset + width() - tab_active.r_width, 0, tab_active.r_width, height()); + SkBitmap theme_r = + SkBitmapOperations::CreateMaskedBitmap(tab_r, *tab_alpha.image_r); canvas->DrawBitmapInt(theme_r, width() - tab_active.r_width, 0); // Draw center. Instead of masking out the top portion we simply skip over it @@ -740,12 +736,12 @@ void TabRenderer::PaintActiveTabBackground(gfx::Canvas* canvas) { void TabRenderer::PaintHoverTabBackground(gfx::Canvas* canvas, double opacity) { - SkBitmap left = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive.image_l, *tab_active.image_l, opacity); - SkBitmap center = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive.image_c, *tab_active.image_c, opacity); - SkBitmap right = skia::ImageOperations::CreateBlendedBitmap( - *tab_inactive.image_r, *tab_active.image_r, opacity); + SkBitmap left = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive.image_l, *tab_active.image_l, opacity); + SkBitmap center = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive.image_c, *tab_active.image_c, opacity); + SkBitmap right = SkBitmapOperations::CreateBlendedBitmap( + *tab_inactive.image_r, *tab_active.image_r, opacity); canvas->DrawBitmapInt(left, 0, 0); canvas->TileImageInt(center, tab_active.l_width, 0, diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index 62e392d9d9de95..1ec105ebcc42eb 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -612,8 +612,7 @@ bool RenderView::CaptureThumbnail(WebView* view, const SkBitmap& src_bmp = device.accessBitmap(false); - SkRect dest_rect; - dest_rect.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); + SkRect dest_rect = { 0, 0, SkIntToScalar(w), SkIntToScalar(h) }; float dest_aspect = dest_rect.width() / dest_rect.height(); // Get the src rect so that we can preserve the aspect ratio while filling diff --git a/skia/ext/convolver.cc b/skia/ext/convolver.cc index 9fe9d3cff6622e..ec0c014f65b84e 100644 --- a/skia/ext/convolver.cc +++ b/skia/ext/convolver.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. @@ -112,14 +112,14 @@ class CircularRowBuffer { // |src_data| and continues for the num_values() of the filter. template void ConvolveHorizontally(const unsigned char* src_data, - const ConvolusionFilter1D& filter, + const ConvolutionFilter1D& filter, unsigned char* out_row) { // Loop over each pixel on this row in the output image. int num_values = filter.num_values(); for (int out_x = 0; out_x < num_values; out_x++) { // Get the filter that determines the current output pixel. int filter_offset, filter_length; - const ConvolusionFilter1D::Fixed* filter_values = + const ConvolutionFilter1D::Fixed* filter_values = filter.FilterForValue(out_x, &filter_offset, &filter_length); // Compute the first pixel in this row that the filter affects. It will @@ -129,7 +129,7 @@ void ConvolveHorizontally(const unsigned char* src_data, // Apply the filter to the row to get the destination pixel in |accum|. int accum[4] = {0}; for (int filter_x = 0; filter_x < filter_length; filter_x++) { - ConvolusionFilter1D::Fixed cur_filter = filter_values[filter_x]; + ConvolutionFilter1D::Fixed cur_filter = filter_values[filter_x]; accum[0] += cur_filter * row_to_filter[filter_x * 4 + 0]; accum[1] += cur_filter * row_to_filter[filter_x * 4 + 1]; accum[2] += cur_filter * row_to_filter[filter_x * 4 + 2]; @@ -139,11 +139,11 @@ void ConvolveHorizontally(const unsigned char* src_data, // Bring this value back in range. All of the filter scaling factors // are in fixed point with kShiftBits bits of fractional part. - accum[0] >>= ConvolusionFilter1D::kShiftBits; - accum[1] >>= ConvolusionFilter1D::kShiftBits; - accum[2] >>= ConvolusionFilter1D::kShiftBits; + accum[0] >>= ConvolutionFilter1D::kShiftBits; + accum[1] >>= ConvolutionFilter1D::kShiftBits; + accum[2] >>= ConvolutionFilter1D::kShiftBits; if (has_alpha) - accum[3] >>= ConvolusionFilter1D::kShiftBits; + accum[3] >>= ConvolutionFilter1D::kShiftBits; // Store the new pixel. out_row[out_x * 4 + 0] = ClampTo8(accum[0]); @@ -154,19 +154,19 @@ void ConvolveHorizontally(const unsigned char* src_data, } } -// Does vertical convolusion to produce one output row. The filter values and +// Does vertical convolution to produce one output row. The filter values and // length are given in the first two parameters. These are applied to each // of the rows pointed to in the |source_data_rows| array, with each row // being |pixel_width| wide. // // The output must have room for |pixel_width * 4| bytes. template -void ConvolveVertically(const ConvolusionFilter1D::Fixed* filter_values, +void ConvolveVertically(const ConvolutionFilter1D::Fixed* filter_values, int filter_length, unsigned char* const* source_data_rows, int pixel_width, unsigned char* out_row) { - // We go through each column in the output and do a vertical convolusion, + // We go through each column in the output and do a vertical convolution, // generating one output pixel each time. for (int out_x = 0; out_x < pixel_width; out_x++) { // Compute the number of bytes over in each row that the current column @@ -176,7 +176,7 @@ void ConvolveVertically(const ConvolusionFilter1D::Fixed* filter_values, // Apply the filter to one column of pixels. int accum[4] = {0}; for (int filter_y = 0; filter_y < filter_length; filter_y++) { - ConvolusionFilter1D::Fixed cur_filter = filter_values[filter_y]; + ConvolutionFilter1D::Fixed cur_filter = filter_values[filter_y]; accum[0] += cur_filter * source_data_rows[filter_y][byte_offset + 0]; accum[1] += cur_filter * source_data_rows[filter_y][byte_offset + 1]; accum[2] += cur_filter * source_data_rows[filter_y][byte_offset + 2]; @@ -186,11 +186,11 @@ void ConvolveVertically(const ConvolusionFilter1D::Fixed* filter_values, // Bring this value back in range. All of the filter scaling factors // are in fixed point with kShiftBits bits of precision. - accum[0] >>= ConvolusionFilter1D::kShiftBits; - accum[1] >>= ConvolusionFilter1D::kShiftBits; - accum[2] >>= ConvolusionFilter1D::kShiftBits; + accum[0] >>= ConvolutionFilter1D::kShiftBits; + accum[1] >>= ConvolutionFilter1D::kShiftBits; + accum[2] >>= ConvolutionFilter1D::kShiftBits; if (has_alpha) - accum[3] >>= ConvolusionFilter1D::kShiftBits; + accum[3] >>= ConvolutionFilter1D::kShiftBits; // Store the new pixel. out_row[byte_offset + 0] = ClampTo8(accum[0]); @@ -221,9 +221,9 @@ void ConvolveVertically(const ConvolusionFilter1D::Fixed* filter_values, } // namespace -// ConvolusionFilter1D --------------------------------------------------------- +// ConvolutionFilter1D --------------------------------------------------------- -void ConvolusionFilter1D::AddFilter(int filter_offset, +void ConvolutionFilter1D::AddFilter(int filter_offset, const float* filter_values, int filter_length) { FilterInstance instance; @@ -239,7 +239,7 @@ void ConvolusionFilter1D::AddFilter(int filter_offset, max_filter_ = std::max(max_filter_, filter_length); } -void ConvolusionFilter1D::AddFilter(int filter_offset, +void ConvolutionFilter1D::AddFilter(int filter_offset, const Fixed* filter_values, int filter_length) { FilterInstance instance; @@ -260,8 +260,8 @@ void ConvolusionFilter1D::AddFilter(int filter_offset, void BGRAConvolve2D(const unsigned char* source_data, int source_byte_row_stride, bool source_has_alpha, - const ConvolusionFilter1D& filter_x, - const ConvolusionFilter1D& filter_y, + const ConvolutionFilter1D& filter_x, + const ConvolutionFilter1D& filter_y, unsigned char* output) { int max_y_filter_size = filter_y.max_filter(); @@ -269,22 +269,22 @@ void BGRAConvolve2D(const unsigned char* source_data, // convolved row for. If the filter doesn't start at the beginning of the // image (this is the case when we are only resizing a subset), then we // don't want to generate any output rows before that. Compute the starting - // row for convolusion as the first pixel for the first vertical filter. + // row for convolution as the first pixel for the first vertical filter. int filter_offset, filter_length; - const ConvolusionFilter1D::Fixed* filter_values = + const ConvolutionFilter1D::Fixed* filter_values = filter_y.FilterForValue(0, &filter_offset, &filter_length); int next_x_row = filter_offset; - // We loop over each row in the input doing a horizontal convolusion. This + // We loop over each row in the input doing a horizontal convolution. This // will result in a horizontally convolved image. We write the results into - // a circular buffer of convolved rows and do vertical convolusion as rows + // a circular buffer of convolved rows and do vertical convolution as rows // are available. This prevents us from having to store the entire // intermediate image and helps cache coherency. CircularRowBuffer row_buffer(filter_x.num_values(), max_y_filter_size, filter_offset); // Loop over every possible output row, processing just enough horizontal - // convolusions to run each subsequent vertical convolusion. + // convolutions to run each subsequent vertical convolution. int output_row_byte_width = filter_x.num_values() * 4; int num_output_rows = filter_y.num_values(); for (int out_y = 0; out_y < num_output_rows; out_y++) { diff --git a/skia/ext/convolver.h b/skia/ext/convolver.h index 91c7ccb2b46a75..1f382824c93a8c 100644 --- a/skia/ext/convolver.h +++ b/skia/ext/convolver.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. @@ -18,18 +18,18 @@ namespace skia { // object for the filter values contributing to it. You build up the filter // list by calling AddFilter for each output pixel (in order). // -// We do 2-dimensional convolusion by first convolving each row by one -// ConvolusionFilter1D, then convolving each column by another one. +// We do 2-dimensional convolution by first convolving each row by one +// ConvolutionFilter1D, then convolving each column by another one. // // Entries are stored in fixed point, shifted left by kShiftBits. -class ConvolusionFilter1D { +class ConvolutionFilter1D { public: // The number of bits that fixed point values are shifted by. enum { kShiftBits = 14 }; typedef short Fixed; - ConvolusionFilter1D() : max_filter_(0) { + ConvolutionFilter1D() : max_filter_(0) { } // Convert between floating point and our fixed point representation. @@ -106,7 +106,7 @@ class ConvolusionFilter1D { int max_filter_; }; -// Does a two-dimensional convolusion on the given source image. +// Does a two-dimensional convolution on the given source image. // // It is assumed the source pixel offsets referenced in the input filters // reference only valid pixels, so the source image size is not required. Each @@ -127,8 +127,8 @@ class ConvolusionFilter1D { void BGRAConvolve2D(const unsigned char* source_data, int source_byte_row_stride, bool source_has_alpha, - const ConvolusionFilter1D& xfilter, - const ConvolusionFilter1D& yfilter, + const ConvolutionFilter1D& xfilter, + const ConvolutionFilter1D& yfilter, unsigned char* output); } // namespace skia diff --git a/skia/ext/convolver_unittest.cc b/skia/ext/convolver_unittest.cc index 10db76cc628873..a25de6e67bf50c 100644 --- a/skia/ext/convolver_unittest.cc +++ b/skia/ext/convolver_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. @@ -14,7 +14,7 @@ namespace skia { namespace { // Fills the given filter with impulse functions for the range 0->num_entries. -void FillImpulseFilter(int num_entries, ConvolusionFilter1D* filter) { +void FillImpulseFilter(int num_entries, ConvolutionFilter1D* filter) { float one = 1.0f; for (int i = 0; i < num_entries; i++) filter->AddFilter(i, &one, 1); @@ -22,13 +22,13 @@ void FillImpulseFilter(int num_entries, ConvolusionFilter1D* filter) { // Filters the given input with the impulse function, and verifies that it // does not change. -void TestImpulseConvolusion(const unsigned char* data, int width, int height) { +void TestImpulseConvolution(const unsigned char* data, int width, int height) { int byte_count = width * height * 4; - ConvolusionFilter1D filter_x; + ConvolutionFilter1D filter_x; FillImpulseFilter(width, &filter_x); - ConvolusionFilter1D filter_y; + ConvolutionFilter1D filter_y; FillImpulseFilter(height, &filter_y); std::vector output; @@ -41,7 +41,7 @@ void TestImpulseConvolusion(const unsigned char* data, int width, int height) { // Fills the destination filter with a box filter averaging every two pixels // to produce the output. -void FillBoxFilter(int size, ConvolusionFilter1D* filter) { +void FillBoxFilter(int size, ConvolutionFilter1D* filter) { const float box[2] = { 0.5, 0.5 }; for (int i = 0; i < size; i++) filter->AddFilter(i * 2, box, 2); @@ -68,7 +68,7 @@ TEST(Convolver, Impulse) { input_ptr[(y * width + x) * 4 + channel] = 0xff; // Always set the alpha channel or it will attempt to "fix" it for us. input_ptr[(y * width + x) * 4 + 3] = 0xff; - TestImpulseConvolusion(input_ptr, width, height); + TestImpulseConvolution(input_ptr, width, height); } } } @@ -98,11 +98,11 @@ TEST(Convolver, Halve) { input[i] = rand() * 255 / RAND_MAX; // Compute the filters. - ConvolusionFilter1D filter_x, filter_y; + ConvolutionFilter1D filter_x, filter_y; FillBoxFilter(dest_width, &filter_x); FillBoxFilter(dest_height, &filter_y); - // Do the convolusion. + // Do the convolution. BGRAConvolve2D(&input[0], src_width, true, filter_x, filter_y, &output[0]); // Compute the expected results and check, allowing for a small difference diff --git a/skia/ext/image_operations.cc b/skia/ext/image_operations.cc index afd7b519addebe..ed4673b5f79aa8 100644 --- a/skia/ext/image_operations.cc +++ b/skia/ext/image_operations.cc @@ -1,24 +1,21 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. -// + #define _USE_MATH_DEFINES #include #include -#include #include "skia/ext/image_operations.h" -#include "base/gfx/rect.h" -#include "base/gfx/size.h" +// TODO(pkasting): skia/ext should not depend on base/! #include "base/histogram.h" #include "base/logging.h" #include "base/stack_container.h" #include "base/time.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" #include "skia/ext/convolver.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkRect.h" namespace skia { @@ -68,15 +65,15 @@ class ResizeFilter { ResizeFilter(ImageOperations::ResizeMethod method, int src_full_width, int src_full_height, int dest_width, int dest_height, - const gfx::Rect& dest_subset); + const SkIRect& dest_subset); // Returns the bounds in the input bitmap of data that is used in the output. // The filter offsets are within this rectangle. - const gfx::Rect& src_depend() { return src_depend_; } + const SkIRect& src_depend() { return src_depend_; } // Returns the filled filter values. - const ConvolusionFilter1D& x_filter() { return x_filter_; } - const ConvolusionFilter1D& y_filter() { return y_filter_; } + const ConvolutionFilter1D& x_filter() { return x_filter_; } + const ConvolutionFilter1D& y_filter() { return y_filter_; } private: // Returns the number of pixels that the filer spans, in filter space (the @@ -109,7 +106,7 @@ class ResizeFilter { void ComputeFilters(int src_size, int dest_subset_lo, int dest_subset_size, float scale, float src_support, - ConvolusionFilter1D* output); + ConvolutionFilter1D* output); // Computes the filter value given the coordinate in filter space. inline float ComputeFilter(float pos) { @@ -127,7 +124,7 @@ class ResizeFilter { ImageOperations::ResizeMethod method_; // Subset of source the filters will touch. - gfx::Rect src_depend_; + SkIRect src_depend_; // Size of the filter support on one side only in the destination space. // See GetFilterSupport. @@ -135,18 +132,18 @@ class ResizeFilter { float y_filter_support_; // Subset of scaled destination bitmap to compute. - gfx::Rect out_bounds_; + SkIRect out_bounds_; - ConvolusionFilter1D x_filter_; - ConvolusionFilter1D y_filter_; + ConvolutionFilter1D x_filter_; + ConvolutionFilter1D y_filter_; - DISALLOW_EVIL_CONSTRUCTORS(ResizeFilter); + DISALLOW_COPY_AND_ASSIGN(ResizeFilter); }; ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, int src_full_width, int src_full_height, int dest_width, int dest_height, - const gfx::Rect& dest_subset) + const SkIRect& dest_subset) : method_(method), out_bounds_(dest_subset) { float scale_x = static_cast(dest_width) / @@ -157,25 +154,24 @@ ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method, x_filter_support_ = GetFilterSupport(scale_x); y_filter_support_ = GetFilterSupport(scale_y); - gfx::Rect src_full(0, 0, src_full_width, src_full_height); - gfx::Rect dest_full(0, 0, - static_cast(src_full_width * scale_x + 0.5), - static_cast(src_full_height * scale_y + 0.5)); + SkIRect src_full = { 0, 0, src_full_width, src_full_height }; + SkIRect dest_full = { 0, 0, static_cast(src_full_width * scale_x + 0.5), + static_cast(src_full_height * scale_y + 0.5) }; // Support of the filter in source space. float src_x_support = x_filter_support_ / scale_x; float src_y_support = y_filter_support_ / scale_y; - ComputeFilters(src_full_width, dest_subset.x(), dest_subset.width(), + ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(), scale_x, src_x_support, &x_filter_); - ComputeFilters(src_full_height, dest_subset.y(), dest_subset.height(), + ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(), scale_y, src_y_support, &y_filter_); } void ResizeFilter::ComputeFilters(int src_size, int dest_subset_lo, int dest_subset_size, float scale, float src_support, - ConvolusionFilter1D* output) { + ConvolutionFilter1D* output) { int dest_subset_hi = dest_subset_lo + dest_subset_size; // [lo, hi) // When we're doing a magnification, the scale will be larger than one. This @@ -259,11 +255,12 @@ void ResizeFilter::ComputeFilters(int src_size, SkBitmap ImageOperations::Resize(const SkBitmap& source, ResizeMethod method, int dest_width, int dest_height, - const gfx::Rect& dest_subset) { + const SkIRect& dest_subset) { // Time how long this takes to see if it's a problem for users. base::TimeTicks resize_start = base::TimeTicks::Now(); - DCHECK(gfx::Rect(dest_width, dest_height).Contains(dest_subset)) << + SkIRect dest = { 0, 0, dest_width, dest_height }; + DCHECK(dest.contains(dest_subset)) << "The supplied subset does not fall within the destination image."; // If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just @@ -305,361 +302,9 @@ SkBitmap ImageOperations::Resize(const SkBitmap& source, SkBitmap ImageOperations::Resize(const SkBitmap& source, ResizeMethod method, int dest_width, int dest_height) { - gfx::Rect dest_subset(0, 0, dest_width, dest_height); + SkIRect dest_subset = { 0, 0, dest_width, dest_height }; return Resize(source, method, dest_width, dest_height, dest_subset); } -// static -SkBitmap ImageOperations::CreateBlendedBitmap(const SkBitmap& first, - const SkBitmap& second, - double alpha) { - DCHECK(alpha <= 1 && alpha >= 0); - 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( - SkColorGetA(first_pixel) * first_alpha + - SkColorGetA(second_pixel) * alpha); - int r = static_cast( - SkColorGetR(first_pixel) * first_alpha + - SkColorGetR(second_pixel) * alpha); - int g = static_cast( - SkColorGetG(first_pixel) * first_alpha + - SkColorGetG(second_pixel) * alpha); - int b = static_cast( - SkColorGetB(first_pixel) * first_alpha + - SkColorGetB(second_pixel) * alpha); - - dst_row[x] = SkColorSetARGB(a, r, g, b); - } - } - - return blended; -} - -// static -SkBitmap ImageOperations::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++) { - uint32 alpha_pixel = alpha_row[x]; - SkColor rgb_pixel = SkUnPreMultiply::PMColorToColor(rgb_row[x]); - - int alpha = SkAlphaMul(SkColorGetA(rgb_pixel), SkColorGetA(alpha_pixel)); - dst_row[x] = SkColorSetARGB(alpha, - SkAlphaMul(SkColorGetR(rgb_pixel), alpha), - SkAlphaMul(SkColorGetG(rgb_pixel), alpha), - SkAlphaMul(SkColorGetB(rgb_pixel), alpha)); - } - } - - return masked; -} - -// static -SkBitmap ImageOperations::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(); - - int bg_a = SkColorGetA(color); - int bg_r = SkColorGetR(color); - int bg_g = SkColorGetG(color); - int 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 mask_pixel = mask_row[x]; - uint32 image_pixel = image_row[x % image.width()]; - - int img_a = SkColorGetA(image_pixel); - int img_r = SkColorGetR(image_pixel); - int img_g = SkColorGetG(image_pixel); - int img_b = SkColorGetB(image_pixel); - - double img_alpha = static_cast(img_a) / 255.0; - double img_inv = 1 - img_alpha; - - double mask_a = static_cast(SkColorGetA(mask_pixel)) / 255.0; - - dst_row[x] = SkColorSetARGB( - static_cast(std::min(255, bg_a + img_a) * mask_a), - static_cast((bg_r * img_inv + img_r * img_alpha) * mask_a), - static_cast((bg_g * img_inv + img_g * img_alpha) * mask_a), - static_cast((bg_b * img_inv + img_b * img_alpha) * mask_a)); - } - } - - return background; -} - - -SkBitmap ImageOperations::CreateBlurredBitmap(const SkBitmap& bitmap, - int blur_amount ) { - DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); - - // Blur factor (1 divided by how many pixels the blur takes place over). - double v = 1.0 / pow(static_cast(blur_amount * 2 + 1), 2); - - SkBitmap blurred; - blurred.setConfig(SkBitmap::kARGB_8888_Config, bitmap.width(), - bitmap.height(), 0); - blurred.allocPixels(); - blurred.eraseARGB(0, 0, 0, 0); - - SkAutoLockPixels lock_bitmap(bitmap); - SkAutoLockPixels lock_blurred(blurred); - - // Loop through every pixel in the image. - for (int y = 0; y < bitmap.height(); y++) { // Skip top and bottom edges. - uint32* dst_row = blurred.getAddr32(0, y); - - for (int x = 0; x < bitmap.width(); x++) { // Skip left and right edges. - // Sums for this pixel. - double a = 0; - double r = 0; - double g = 0; - double b = 0; - - for (int ky = -blur_amount; ky <= blur_amount; ky++) { - for (int kx = -blur_amount; kx <= blur_amount; kx++) { - // Calculate the adjacent pixel for this kernel point. Blurs - // are wrapped. - int bx = (x + kx) % bitmap.width(); - while (bx < 0) - bx += bitmap.width(); - int by = (y + ky) % bitmap.height(); - while (by < 0) - by += bitmap.height(); - - uint32 src_pixel = bitmap.getAddr32(0, by)[bx]; - - a += v * static_cast(SkColorGetA(src_pixel)); - r += v * static_cast(SkColorGetR(src_pixel)); - g += v * static_cast(SkColorGetG(src_pixel)); - b += v * static_cast(SkColorGetB(src_pixel)); - } - } - - dst_row[x] = SkColorSetARGB( - static_cast(a), - static_cast(r), - static_cast(g), - static_cast(b)); - } - } - - return blurred; -} - -// static -SkBitmap ImageOperations::CreateHSLShiftedBitmap(const SkBitmap& bitmap, - 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++) { - SkColor color = SkUnPreMultiply::PMColorToColor(pixels[x]); - SkColor shifted = HSLShift(color, hsl_shift); - tinted_pixels[x] = SkPreMultiplyColor(shifted); - } - } - - return shifted; -} - -// static -SkBitmap ImageOperations::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 ImageOperations::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 += 1; - - // 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 += 1; - - // 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; -} - -// static -SkBitmap ImageOperations::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; -} - } // namespace skia diff --git a/skia/ext/image_operations.h b/skia/ext/image_operations.h index 3b172c8f7c1685..e9f448bb7debab 100644 --- a/skia/ext/image_operations.h +++ b/skia/ext/image_operations.h @@ -1,16 +1,12 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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 SKIA_EXT_IMAGE_OPERATIONS_H_ #define SKIA_EXT_IMAGE_OPERATIONS_H_ -#include "base/basictypes.h" -#include "base/gfx/rect.h" -#include "skia/ext/skia_utils.h" -#include "third_party/skia/include/core/SkColor.h" - class SkBitmap; +struct SkIRect; namespace skia { @@ -40,7 +36,7 @@ class ImageOperations { static SkBitmap Resize(const SkBitmap& source, ResizeMethod method, int dest_width, int dest_height, - const gfx::Rect& dest_subset); + const SkIRect& dest_subset); // Alternate version for resizing and returning the entire bitmap rather than // a subset. @@ -48,69 +44,6 @@ class ImageOperations { ResizeMethod method, int dest_width, int dest_height); - // 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); - - // Blur a bitmap using an average-blur algorithm over the rectangle defined - // by |blur_amount|. The blur will wrap around image edges. - static SkBitmap CreateBlurredBitmap(const SkBitmap& bitmap, int blur_amount); - - // 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, - 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); - - // 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); - - // 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: ImageOperations(); // Class for scoping only. }; diff --git a/skia/ext/image_operations_unittest.cc b/skia/ext/image_operations_unittest.cc index dbf4fce4b9cdf7..52d13b98ded922 100644 --- a/skia/ext/image_operations_unittest.cc +++ b/skia/ext/image_operations_unittest.cc @@ -1,14 +1,11 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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 - #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/SkColorPriv.h" -#include "third_party/skia/include/core/SkUnPreMultiply.h" +#include "third_party/skia/include/core/SkRect.h" namespace { @@ -105,7 +102,7 @@ TEST(ImageOperations, HalveSubset) { // 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). - gfx::Rect subset_rect(2, 3, 3, 6); + SkIRect subset_rect = { 2, 3, 3, 6 }; SkBitmap subset_results = skia::ImageOperations::Resize( src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2, subset_rect); @@ -119,7 +116,7 @@ TEST(ImageOperations, HalveSubset) { for (int y = 0; y < subset_rect.height(); y++) { for (int x = 0; x < subset_rect.width(); x++) { ASSERT_EQ( - *full_results.getAddr32(x + subset_rect.x(), y + subset_rect.y()), + *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), *subset_results.getAddr32(x, y)); } } @@ -147,372 +144,3 @@ TEST(ImageOperations, ResampleToSame) { } } } - -// Blend two bitmaps together at 50% alpha and verify that the result -// is the middle-blend of the two. -TEST(ImageOperations, 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 = skia::ImageOperations::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((255 + ((255 - i) % 255)) / 2), - SkColorGetA(*blended.getAddr32(x, y))); - EXPECT_EQ(static_cast(i % 255 / 2), - SkColorGetR(*blended.getAddr32(x, y))); - EXPECT_EQ((static_cast((i * 2) % 255 + (i * 4) % 255) / 2), - SkColorGetG(*blended.getAddr32(x, y))); - EXPECT_EQ(static_cast(i % 255 / 2), - SkColorGetB(*blended.getAddr32(x, y))); - } - } -} - -// Test our masking functions. -TEST(ImageOperations, 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 = skia::ImageOperations::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)); - } - } -} - -// Testing blur without reimplementing the blur algorithm here is tough, -// so we just check to see if the pixels have moved in the direction we -// think they should move in (and also checking the wrapping behavior). -// This will allow us to tweak the blur algorithm to suit speed/visual -// needs without breaking the fundamentals. -TEST(ImageOperations, CreateBlurredBitmap) { - 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++) { - int r = (y == 0) ? 255 : 0; // Make the top row red. - int g = (i % 2 == 0) ? 255 : 0; // Make green alternate in each pixel. - int b = (y == src_h - 1) ? 255 : 0; // Make the bottom row blue. - - *src.getAddr32(x, y) = SkColorSetARGB(255, r, g, b); - i++; - } - } - - // Perform a small blur (enough to shove the values in the direction we - // need - more would just be an unneccessary unit test slowdown). - SkBitmap blurred = skia::ImageOperations::CreateBlurredBitmap(src, 2); - - SkAutoLockPixels src_lock(src); - SkAutoLockPixels blurred_lock(blurred); - for (int y = 0, i = 0; y < src_w; y++) { - for (int x = 0; x < src_h; x++) { - SkColor src_pixel = *src.getAddr32(x, y); - SkColor blurred_pixel = *blurred.getAddr32(x, y); - if (y == 0) { - // We expect our red to have decreased, but our blue to have - // increased (from the wrapping from the bottom line). - EXPECT_TRUE(SkColorGetR(blurred_pixel) < SkColorGetR(src_pixel)); - EXPECT_TRUE(SkColorGetB(blurred_pixel) > SkColorGetB(src_pixel)); - } else if (y == src_h - 1) { - // Now for the opposite. - EXPECT_TRUE(SkColorGetB(blurred_pixel) < SkColorGetB(src_pixel)); - EXPECT_TRUE(SkColorGetR(blurred_pixel) > SkColorGetR(src_pixel)); - } - - // Expect the green channel to have moved towards the center (but - // not past it). - if (i % 2 == 0) { - EXPECT_LT(SkColorGetG(blurred_pixel), SkColorGetG(src_pixel)); - EXPECT_GE(SkColorGetG(blurred_pixel), static_cast(128)); - } else { - EXPECT_GT(SkColorGetG(blurred_pixel), SkColorGetG(src_pixel)); - EXPECT_LE(SkColorGetG(blurred_pixel), static_cast(128)); - } - - i++; - } - } -} - -// Make sure that when shifting a bitmap without any shift parameters, -// the end result is close enough to the original (rounding errors -// notwithstanding). -TEST(ImageOperations, 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++; - } - } - - skia::HSL hsl = { -1, -1, -1 }; - - SkBitmap shifted = skia::ImageOperations::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(ImageOperations, 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. - skia::HSL hsl = { 0, -1, -1 }; - - SkBitmap shifted = skia::ImageOperations::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(ImageOperations, CreateCroppedBitmap) { - int src_w = 16, src_h = 16; - SkBitmap src; - FillDataToBitmap(src_w, src_h, &src); - - SkBitmap cropped = skia::ImageOperations::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(ImageOperations, CreateCroppedBitmapWrapping) { - int src_w = 16, src_h = 16; - SkBitmap src; - FillDataToBitmap(src_w, src_h, &src); - - SkBitmap cropped = skia::ImageOperations::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(ImageOperations, 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 = skia::ImageOperations::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(ImageOperations, 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 = skia::ImageOperations::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 = skia::ImageOperations::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 = skia::ImageOperations::DownsampleByTwo(n_by_one); - SkAutoLockPixels lock3(result); - EXPECT_EQ(1, result.width()); - EXPECT_EQ(300, result.height()); - - // Test an empty bitmap - SkBitmap empty; - result = skia::ImageOperations::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 -TEST(ImageOperations, 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 = skia::ImageOperations::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 = skia::ImageOperations::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 = skia::ImageOperations::DownsampleByTwoUntilSize(large, 6, 6); - - // The result should be divided in half 100x43 -> 50x22 -> 25x11 - EXPECT_EQ(25, result.width()); - EXPECT_EQ(11, result.height()); -} diff --git a/skia/ext/skia_utils.cc b/skia/ext/skia_utils.cc index 84a003bf2cb753..c95054d550257e 100644 --- a/skia/ext/skia_utils.cc +++ b/skia/ext/skia_utils.cc @@ -1,9 +1,10 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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 "skia/ext/skia_utils.h" #include "third_party/skia/include/core/SkColorPriv.h" +#include "third_party/skia/include/core/SkShader.h" #include "third_party/skia/include/effects/SkGradientShader.h" namespace skia { @@ -21,150 +22,5 @@ SkShader* CreateGradientShader(int start_point, grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode); } -// Helper function for HSLToSKColor. -static inline double calcHue(double temp1, double temp2, double hueVal) { - if (hueVal < 0.0) - hueVal++; - else if (hueVal > 1.0) - hueVal--; - - if (hueVal * 6.0 < 1.0) - return temp1 + (temp2 - temp1) * hueVal * 6.0; - if (hueVal * 2.0 < 1.0) - return temp2; - if (hueVal * 3.0 < 2.0) - return temp1 + (temp2 - temp1) * (2.0 / 3.0 - hueVal) * 6.0; - - return temp1; -} - -SkColor HSLToSkColor(U8CPU alpha, HSL hsl) { - 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) { - U8CPU light; - - if (lightness < 0) - light = 0; - else if (lightness >= SK_Scalar1) - 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; - - double rh = calcHue(temp1, temp2, hue + 1.0 / 3.0); - double gh = calcHue(temp1, temp2, hue); - double bh = calcHue(temp1, temp2, hue - 1.0 / 3.0); - - return SkColorSetARGB(alpha, - static_cast(rh * 255), - static_cast(gh * 255), - static_cast(bh * 255)); -} - -void SkColorToHSL(SkColor c, HSL& hsl) { - double r = SkColorGetR(c) / 255.0; - double g = SkColorGetG(c) / 255.0; - double b = SkColorGetB(c) / 255.0; - - double h, s, l; - - double vmax = r > g ? r : g; - vmax = vmax > b ? vmax : b; - double vmin = r < g ? r : g; - vmin = vmin < b ? vmin : b; - double delta = vmax - vmin; - - l = (vmax + vmin) / 2; - - if (delta == 0) { - h = 0; - s = 0; - } else { - if (l < 0.5) - s = delta / (vmax + vmin); - else - s = delta / (2 - vmax - vmin); - - 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) - h = db - dg; - else if (g == vmax) - h = (1.0 / 3.0) + dr - db; - else if (b == vmax) - h = (2.0 / 3.0) + dg - dr; - - if (h < 0) h += 1; - if (h > 1) h -= 1; - } - - hsl.h = h; - hsl.s = s; - hsl.l = l; -} - -SkColor HSLShift(SkColor color, 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 = hsl.s + (1.0 - hsl.s) * - ((shift.s - 0.5) * 2.0); - } - } - - SkColor result = HSLToSkColor(alpha, hsl); - - // Lightness shifts in the style of popular image editors aren't - // actually represented in HSL - the L value does have some effect - // on saturation. - if (shift.l >= 0) { - double r = static_castSkColorGetR(result); - double g = static_castSkColorGetG(result); - double b = static_castSkColorGetB(result); - - if (shift.l <= 0.5) { - r *= (shift.l * 2.0); - g *= (shift.l * 2.0); - b *= (shift.l * 2.0); - } else { - r = (r + (255.0 - r) * ((shift.l - 0.5) * 2.0)); - g = (g + (255.0 - g) * ((shift.l - 0.5) * 2.0)); - b = (b + (255.0 - b) * ((shift.l - 0.5) * 2.0)); - } - - return SkColorSetARGB(alpha, - static_cast(r), - static_cast(g), - static_cast(b)); - } else { - return result; - } -} - - } // namespace skia diff --git a/skia/ext/skia_utils.h b/skia/ext/skia_utils.h index 769d9d4e070343..52d9d626825967 100644 --- a/skia/ext/skia_utils.h +++ b/skia/ext/skia_utils.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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. @@ -6,15 +6,10 @@ #define SKIA_EXT_SKIA_UTILS_H_ #include "third_party/skia/include/core/SkColor.h" -#include "third_party/skia/include/core/SkShader.h" -namespace skia { +class SkShader; -struct HSL { - double h; - double s; - double l; -}; +namespace skia { // Creates a vertical gradient shader. The caller owns the shader. // Example usage to avoid leaks: @@ -26,29 +21,6 @@ SkShader* CreateGradientShader(int start_point, int end_point, SkColor start_color, SkColor end_color); - -// Convert an SkColor to a HSL value. -void SkColorToHSL(SkColor c, HSL& hsl); - -// Convert a HSL color to an SkColor. -SkColor HSLToSkColor(U8CPU alpha, HSL hsl); - -// 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, skia::HSL shift); - } // namespace skia #endif // SKIA_EXT_SKIA_UTILS_H_ diff --git a/views/controls/button/image_button.cc b/views/controls/button/image_button.cc index 50a8f59ce81a05..adb4bc99db9bf3 100644 --- a/views/controls/button/image_button.cc +++ b/views/controls/button/image_button.cc @@ -1,12 +1,12 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// 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 "views/controls/button/image_button.h" #include "app/gfx/canvas.h" +#include "app/gfx/skbitmap_operations.h" #include "app/throb_animation.h" -#include "skia/ext/image_operations.h" namespace views { @@ -41,9 +41,8 @@ void ImageButton::SetBackground(SkColor color, return; } - background_image_ = skia::ImageOperations::CreateButtonBackground(color, - *image, - *mask); + background_image_ = + SkBitmapOperations::CreateButtonBackground(color, *image, *mask); } void ImageButton::SetImageAlignment(HorizontalAlignment h_align, @@ -95,8 +94,8 @@ SkBitmap ImageButton::GetImageToPaint() { SkBitmap img; if (!images_[BS_HOT].isNull() && hover_animation_->IsAnimating()) { - img = skia::ImageOperations::CreateBlendedBitmap(images_[BS_NORMAL], - images_[BS_HOT], hover_animation_->GetCurrentValue()); + img = SkBitmapOperations::CreateBlendedBitmap(images_[BS_NORMAL], + images_[BS_HOT], hover_animation_->GetCurrentValue()); } else { img = images_[state_]; } diff --git a/webkit/tools/test_shell/test_shell.gyp b/webkit/tools/test_shell/test_shell.gyp index 7ff875bd6e5388..56cc14302fb1f0 100644 --- a/webkit/tools/test_shell/test_shell.gyp +++ b/webkit/tools/test_shell/test_shell.gyp @@ -341,7 +341,6 @@ 'sources': [ '../../../skia/ext/convolver_unittest.cc', '../../../skia/ext/image_operations_unittest.cc', - '../../../skia/ext/skia_utils_unittest.cc', '../../../skia/ext/platform_canvas_unittest.cc', '../../../skia/ext/vector_canvas_unittest.cc', '../../appcache/manifest_parser_unittest.cc',