| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/skbitmap_operations.h" | 5 #include "ui/gfx/skbitmap_operations.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "third_party/skia/include/core/SkColorFilter.h" | 13 #include "third_party/skia/include/core/SkColorFilter.h" |
| 14 #include "third_party/skia/include/core/SkColorPriv.h" | 14 #include "third_party/skia/include/core/SkColorPriv.h" |
| 15 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 15 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 16 #include "third_party/skia/include/effects/SkBlurImageFilter.h" | 16 #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
| 17 #include "ui/gfx/insets.h" |
| 17 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
| 18 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { | 22 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { |
| 22 DCHECK(image.config() == SkBitmap::kARGB_8888_Config); | 23 DCHECK(image.config() == SkBitmap::kARGB_8888_Config); |
| 23 | 24 |
| 24 SkAutoLockPixels lock_image(image); | 25 SkAutoLockPixels lock_image(image); |
| 25 | 26 |
| 26 SkBitmap inverted; | 27 SkBitmap inverted; |
| (...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 for (int x = 0; x < image.width(); ++x) { | 737 for (int x = 0; x < image.width(); ++x) { |
| 737 uint32* dst = transposed.getAddr32(y, x); | 738 uint32* dst = transposed.getAddr32(y, x); |
| 738 *dst = image_row[x]; | 739 *dst = image_row[x]; |
| 739 } | 740 } |
| 740 } | 741 } |
| 741 | 742 |
| 742 return transposed; | 743 return transposed; |
| 743 } | 744 } |
| 744 | 745 |
| 745 // static | 746 // static |
| 746 SkBitmap SkBitmapOperations::CreateResizedBitmap(const SkBitmap& bitmap, | |
| 747 const gfx::Size& size) { | |
| 748 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | |
| 749 | |
| 750 SkBitmap src = bitmap; | |
| 751 src.buildMipMap(false); | |
| 752 | |
| 753 SkBitmap resized; | |
| 754 resized.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); | |
| 755 resized.allocPixels(); | |
| 756 resized.eraseARGB(0, 0, 0, 0); | |
| 757 | |
| 758 SkCanvas canvas(resized); | |
| 759 | |
| 760 SkIRect src_rect = SkIRect::MakeWH(src.width(), src.height()); | |
| 761 SkRect dst_rect = SkRect::MakeWH(size.width(), size.height()); | |
| 762 | |
| 763 SkPaint paint; | |
| 764 paint.setFilterBitmap(true); | |
| 765 canvas.drawBitmapRect(src, &src_rect, dst_rect, &paint); | |
| 766 return resized; | |
| 767 } | |
| 768 | |
| 769 // static | |
| 770 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, | 747 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, |
| 771 SkColor c) { | 748 SkColor c) { |
| 772 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | 749 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); |
| 773 | 750 |
| 774 SkBitmap color_mask; | 751 SkBitmap color_mask; |
| 775 color_mask.setConfig(SkBitmap::kARGB_8888_Config, | 752 color_mask.setConfig(SkBitmap::kARGB_8888_Config, |
| 776 bitmap.width(), bitmap.height()); | 753 bitmap.width(), bitmap.height()); |
| 777 color_mask.allocPixels(); | 754 color_mask.allocPixels(); |
| 778 color_mask.eraseARGB(0, 0, 0, 0); | 755 color_mask.eraseARGB(0, 0, 0, 0); |
| 779 | 756 |
| 780 SkCanvas canvas(color_mask); | 757 SkCanvas canvas(color_mask); |
| 781 | 758 |
| 782 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( | 759 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( |
| 783 c, SkXfermode::kSrcIn_Mode); | 760 c, SkXfermode::kSrcIn_Mode); |
| 784 SkPaint paint; | 761 SkPaint paint; |
| 785 paint.setColorFilter(color_filter)->unref(); | 762 paint.setColorFilter(color_filter)->unref(); |
| 786 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | 763 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); |
| 787 return color_mask; | 764 return color_mask; |
| 788 } | 765 } |
| 789 | 766 |
| 790 // static | 767 // static |
| 791 SkBitmap SkBitmapOperations::CreateDropShadow(const SkBitmap& bitmap, | 768 SkBitmap SkBitmapOperations::CreateDropShadow( |
| 792 int shadow_count, | 769 const SkBitmap& bitmap, |
| 793 const SkColor* shadow_color, | 770 const gfx::ShadowValues& shadows) { |
| 794 const gfx::Point* shadow_offset, | |
| 795 const SkScalar* shadow_radius) { | |
| 796 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | 771 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); |
| 797 | 772 |
| 798 int padding = 0; | 773 // Shadow margin insets are negative values because they grow outside. |
| 799 for (int i = 0; i < shadow_count; ++i) { | 774 // Negate them here as grow direction is not important and only pixel value |
| 800 int shadow_space = std::max(abs(shadow_offset[i].x()), | 775 // is of interest here. |
| 801 abs(shadow_offset[i].y())) + shadow_radius[i]; | 776 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows); |
| 802 if (shadow_space > padding) | |
| 803 padding = shadow_space; | |
| 804 } | |
| 805 | 777 |
| 806 SkBitmap image_with_shadow; | 778 SkBitmap image_with_shadow; |
| 807 image_with_shadow.setConfig(SkBitmap::kARGB_8888_Config, | 779 image_with_shadow.setConfig(SkBitmap::kARGB_8888_Config, |
| 808 bitmap.width() + 2 * padding, | 780 bitmap.width() + shadow_margin.width(), |
| 809 bitmap.height() + 2 * padding); | 781 bitmap.height() + shadow_margin.height()); |
| 810 image_with_shadow.allocPixels(); | 782 image_with_shadow.allocPixels(); |
| 811 image_with_shadow.eraseARGB(0, 0, 0, 0); | 783 image_with_shadow.eraseARGB(0, 0, 0, 0); |
| 812 | 784 |
| 813 SkCanvas canvas(image_with_shadow); | 785 SkCanvas canvas(image_with_shadow); |
| 814 canvas.translate(SkIntToScalar(padding), SkIntToScalar(padding)); | 786 canvas.translate(SkIntToScalar(shadow_margin.left()), |
| 787 SkIntToScalar(shadow_margin.top())); |
| 815 | 788 |
| 816 SkPaint paint; | 789 SkPaint paint; |
| 817 for (int i = 0; i < shadow_count; ++i) { | 790 for (size_t i = 0; i < shadows.size(); ++i) { |
| 818 SkBitmap shadow = SkBitmapOperations::CreateColorMask(bitmap, | 791 const gfx::ShadowValue& shadow = shadows[i]; |
| 819 shadow_color[i]); | 792 SkBitmap shadow_image = SkBitmapOperations::CreateColorMask(bitmap, |
| 793 shadow.color()); |
| 820 | 794 |
| 821 paint.setImageFilter( | 795 paint.setImageFilter( |
| 822 new SkBlurImageFilter(shadow_radius[i], shadow_radius[i]))->unref(); | 796 new SkBlurImageFilter(SkDoubleToScalar(shadow.blur()), |
| 797 SkDoubleToScalar(shadow.blur())))->unref(); |
| 823 | 798 |
| 824 canvas.saveLayer(0, &paint); | 799 canvas.saveLayer(0, &paint); |
| 825 canvas.drawBitmap(shadow, | 800 canvas.drawBitmap(shadow_image, |
| 826 SkIntToScalar(shadow_offset[i].x()), | 801 SkIntToScalar(shadow.x()), |
| 827 SkIntToScalar(shadow_offset[i].y())); | 802 SkIntToScalar(shadow.y())); |
| 828 canvas.restore(); | 803 canvas.restore(); |
| 829 } | 804 } |
| 830 | 805 |
| 831 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); | 806 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); |
| 832 return image_with_shadow; | 807 return image_with_shadow; |
| 833 } | 808 } |
| 834 | 809 |
| OLD | NEW |