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" |
19 #include "ui/gfx/shadow_value.h" | |
18 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
19 | 21 |
20 // static | 22 // static |
21 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { | 23 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { |
22 DCHECK(image.config() == SkBitmap::kARGB_8888_Config); | 24 DCHECK(image.config() == SkBitmap::kARGB_8888_Config); |
23 | 25 |
24 SkAutoLockPixels lock_image(image); | 26 SkAutoLockPixels lock_image(image); |
25 | 27 |
26 SkBitmap inverted; | 28 SkBitmap inverted; |
27 inverted.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height(), | 29 inverted.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height(), |
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
736 for (int x = 0; x < image.width(); ++x) { | 738 for (int x = 0; x < image.width(); ++x) { |
737 uint32* dst = transposed.getAddr32(y, x); | 739 uint32* dst = transposed.getAddr32(y, x); |
738 *dst = image_row[x]; | 740 *dst = image_row[x]; |
739 } | 741 } |
740 } | 742 } |
741 | 743 |
742 return transposed; | 744 return transposed; |
743 } | 745 } |
744 | 746 |
745 // static | 747 // 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, | 748 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, |
771 SkColor c) { | 749 SkColor c) { |
772 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | 750 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); |
773 | 751 |
774 SkBitmap color_mask; | 752 SkBitmap color_mask; |
775 color_mask.setConfig(SkBitmap::kARGB_8888_Config, | 753 color_mask.setConfig(SkBitmap::kARGB_8888_Config, |
776 bitmap.width(), bitmap.height()); | 754 bitmap.width(), bitmap.height()); |
777 color_mask.allocPixels(); | 755 color_mask.allocPixels(); |
778 color_mask.eraseARGB(0, 0, 0, 0); | 756 color_mask.eraseARGB(0, 0, 0, 0); |
779 | 757 |
780 SkCanvas canvas(color_mask); | 758 SkCanvas canvas(color_mask); |
781 | 759 |
782 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( | 760 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( |
783 c, SkXfermode::kSrcIn_Mode); | 761 c, SkXfermode::kSrcIn_Mode); |
784 SkPaint paint; | 762 SkPaint paint; |
785 paint.setColorFilter(color_filter)->unref(); | 763 paint.setColorFilter(color_filter)->unref(); |
786 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | 764 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); |
787 return color_mask; | 765 return color_mask; |
788 } | 766 } |
789 | 767 |
790 // static | 768 // static |
791 SkBitmap SkBitmapOperations::CreateDropShadow(const SkBitmap& bitmap, | 769 SkBitmap SkBitmapOperations::CreateDropShadow( |
792 int shadow_count, | 770 const SkBitmap& bitmap, |
793 const SkColor* shadow_color, | 771 const std::vector<gfx::ShadowValue>& shadows) { |
794 const gfx::Point* shadow_offset, | |
795 const SkScalar* shadow_radius) { | |
796 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); | 772 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config); |
797 | 773 |
798 int padding = 0; | 774 // Shadow margin insets are negative values because they grow outside. |
799 for (int i = 0; i < shadow_count; ++i) { | 775 // Negate them here as grow direction is not important and only pixel value |
800 int shadow_space = std::max(abs(shadow_offset[i].x()), | 776 // is of interest here. |
801 abs(shadow_offset[i].y())) + shadow_radius[i]; | 777 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows); |
802 if (shadow_space > padding) | |
803 padding = shadow_space; | |
804 } | |
805 | 778 |
806 SkBitmap image_with_shadow; | 779 SkBitmap image_with_shadow; |
807 image_with_shadow.setConfig(SkBitmap::kARGB_8888_Config, | 780 image_with_shadow.setConfig(SkBitmap::kARGB_8888_Config, |
808 bitmap.width() + 2 * padding, | 781 bitmap.width() + shadow_margin.width(), |
809 bitmap.height() + 2 * padding); | 782 bitmap.height() + shadow_margin.height()); |
810 image_with_shadow.allocPixels(); | 783 image_with_shadow.allocPixels(); |
811 image_with_shadow.eraseARGB(0, 0, 0, 0); | 784 image_with_shadow.eraseARGB(0, 0, 0, 0); |
812 | 785 |
813 SkCanvas canvas(image_with_shadow); | 786 SkCanvas canvas(image_with_shadow); |
814 canvas.translate(SkIntToScalar(padding), SkIntToScalar(padding)); | 787 canvas.translate(SkIntToScalar(shadow_margin.left()), |
788 SkIntToScalar(shadow_margin.top())); | |
815 | 789 |
816 SkPaint paint; | 790 SkPaint paint; |
817 for (int i = 0; i < shadow_count; ++i) { | 791 for (size_t i = 0; i < shadows.size(); ++i) { |
818 SkBitmap shadow = SkBitmapOperations::CreateColorMask(bitmap, | 792 const gfx::ShadowValue& shadow = shadows[i]; |
819 shadow_color[i]); | 793 SkBitmap shadow_image = SkBitmapOperations::CreateColorMask(bitmap, |
794 shadow.color()); | |
Alexei Svitkine (slow)
2012/05/25 18:16:06
Indentation looks wrong. Suggest moving everything
xiyuan
2012/05/25 19:34:01
Done.
| |
820 | 795 |
821 paint.setImageFilter( | 796 paint.setImageFilter( |
822 new SkBlurImageFilter(shadow_radius[i], shadow_radius[i]))->unref(); | 797 new SkBlurImageFilter(SkDoubleToScalar(shadow.blur()), |
798 SkDoubleToScalar(shadow.blur())))->unref(); | |
823 | 799 |
824 canvas.saveLayer(0, &paint); | 800 canvas.saveLayer(0, &paint); |
825 canvas.drawBitmap(shadow, | 801 canvas.drawBitmap(shadow_image, |
826 SkIntToScalar(shadow_offset[i].x()), | 802 SkIntToScalar(shadow.x()), |
827 SkIntToScalar(shadow_offset[i].y())); | 803 SkIntToScalar(shadow.y())); |
828 canvas.restore(); | 804 canvas.restore(); |
829 } | 805 } |
830 | 806 |
831 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); | 807 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0)); |
832 return image_with_shadow; | 808 return image_with_shadow; |
833 } | 809 } |
834 | 810 |
OLD | NEW |