| 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 "skia/ext/refptr.h" | 11 #include "skia/ext/refptr.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 #include "third_party/skia/include/core/SkColorFilter.h" | 14 #include "third_party/skia/include/core/SkColorFilter.h" |
| 15 #include "third_party/skia/include/core/SkColorPriv.h" | 15 #include "third_party/skia/include/core/SkColorPriv.h" |
| 16 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 16 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 17 #include "third_party/skia/include/effects/SkBlurImageFilter.h" | 17 #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
| 18 #include "ui/gfx/insets.h" | 18 #include "ui/gfx/insets.h" |
| 19 #include "ui/gfx/point.h" | 19 #include "ui/gfx/point.h" |
| 20 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { | 23 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { |
| 24 DCHECK(image.colorType() == kPMColor_SkColorType); | 24 DCHECK(image.colorType() == kN32_SkColorType); |
| 25 | 25 |
| 26 SkAutoLockPixels lock_image(image); | 26 SkAutoLockPixels lock_image(image); |
| 27 | 27 |
| 28 SkBitmap inverted; | 28 SkBitmap inverted; |
| 29 inverted.allocN32Pixels(image.width(), image.height()); | 29 inverted.allocN32Pixels(image.width(), image.height()); |
| 30 inverted.eraseARGB(0, 0, 0, 0); | 30 inverted.eraseARGB(0, 0, 0, 0); |
| 31 | 31 |
| 32 for (int y = 0; y < image.height(); ++y) { | 32 for (int y = 0; y < image.height(); ++y) { |
| 33 uint32* image_row = image.getAddr32(0, y); | 33 uint32* image_row = image.getAddr32(0, y); |
| 34 uint32* dst_row = inverted.getAddr32(0, y); | 34 uint32* dst_row = inverted.getAddr32(0, y); |
| 35 | 35 |
| 36 for (int x = 0; x < image.width(); ++x) { | 36 for (int x = 0; x < image.width(); ++x) { |
| 37 uint32 image_pixel = image_row[x]; | 37 uint32 image_pixel = image_row[x]; |
| 38 dst_row[x] = (image_pixel & 0xFF000000) | | 38 dst_row[x] = (image_pixel & 0xFF000000) | |
| 39 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); | 39 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 return inverted; | 43 return inverted; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // static | 46 // static |
| 47 SkBitmap SkBitmapOperations::CreateSuperimposedBitmap(const SkBitmap& first, | 47 SkBitmap SkBitmapOperations::CreateSuperimposedBitmap(const SkBitmap& first, |
| 48 const SkBitmap& second) { | 48 const SkBitmap& second) { |
| 49 DCHECK(first.width() == second.width()); | 49 DCHECK(first.width() == second.width()); |
| 50 DCHECK(first.height() == second.height()); | 50 DCHECK(first.height() == second.height()); |
| 51 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); | 51 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); |
| 52 DCHECK(first.colorType() == kPMColor_SkColorType); | 52 DCHECK(first.colorType() == kN32_SkColorType); |
| 53 | 53 |
| 54 SkAutoLockPixels lock_first(first); | 54 SkAutoLockPixels lock_first(first); |
| 55 SkAutoLockPixels lock_second(second); | 55 SkAutoLockPixels lock_second(second); |
| 56 | 56 |
| 57 SkBitmap superimposed; | 57 SkBitmap superimposed; |
| 58 superimposed.allocN32Pixels(first.width(), first.height()); | 58 superimposed.allocN32Pixels(first.width(), first.height()); |
| 59 superimposed.eraseARGB(0, 0, 0, 0); | 59 superimposed.eraseARGB(0, 0, 0, 0); |
| 60 | 60 |
| 61 SkCanvas canvas(superimposed); | 61 SkCanvas canvas(superimposed); |
| 62 | 62 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 73 } | 73 } |
| 74 | 74 |
| 75 // static | 75 // static |
| 76 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, | 76 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, |
| 77 const SkBitmap& second, | 77 const SkBitmap& second, |
| 78 double alpha) { | 78 double alpha) { |
| 79 DCHECK((alpha >= 0) && (alpha <= 1)); | 79 DCHECK((alpha >= 0) && (alpha <= 1)); |
| 80 DCHECK(first.width() == second.width()); | 80 DCHECK(first.width() == second.width()); |
| 81 DCHECK(first.height() == second.height()); | 81 DCHECK(first.height() == second.height()); |
| 82 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); | 82 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); |
| 83 DCHECK(first.colorType() == kPMColor_SkColorType); | 83 DCHECK(first.colorType() == kN32_SkColorType); |
| 84 | 84 |
| 85 // Optimize for case where we won't need to blend anything. | 85 // Optimize for case where we won't need to blend anything. |
| 86 static const double alpha_min = 1.0 / 255; | 86 static const double alpha_min = 1.0 / 255; |
| 87 static const double alpha_max = 254.0 / 255; | 87 static const double alpha_max = 254.0 / 255; |
| 88 if (alpha < alpha_min) | 88 if (alpha < alpha_min) |
| 89 return first; | 89 return first; |
| 90 else if (alpha > alpha_max) | 90 else if (alpha > alpha_max) |
| 91 return second; | 91 return second; |
| 92 | 92 |
| 93 SkAutoLockPixels lock_first(first); | 93 SkAutoLockPixels lock_first(first); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 123 | 123 |
| 124 return blended; | 124 return blended; |
| 125 } | 125 } |
| 126 | 126 |
| 127 // static | 127 // static |
| 128 SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb, | 128 SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb, |
| 129 const SkBitmap& alpha) { | 129 const SkBitmap& alpha) { |
| 130 DCHECK(rgb.width() == alpha.width()); | 130 DCHECK(rgb.width() == alpha.width()); |
| 131 DCHECK(rgb.height() == alpha.height()); | 131 DCHECK(rgb.height() == alpha.height()); |
| 132 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel()); | 132 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel()); |
| 133 DCHECK(rgb.colorType() == kPMColor_SkColorType); | 133 DCHECK(rgb.colorType() == kN32_SkColorType); |
| 134 DCHECK(alpha.colorType() == kPMColor_SkColorType); | 134 DCHECK(alpha.colorType() == kN32_SkColorType); |
| 135 | 135 |
| 136 SkBitmap masked; | 136 SkBitmap masked; |
| 137 masked.allocN32Pixels(rgb.width(), rgb.height()); | 137 masked.allocN32Pixels(rgb.width(), rgb.height()); |
| 138 masked.eraseARGB(0, 0, 0, 0); | 138 masked.eraseARGB(0, 0, 0, 0); |
| 139 | 139 |
| 140 SkAutoLockPixels lock_rgb(rgb); | 140 SkAutoLockPixels lock_rgb(rgb); |
| 141 SkAutoLockPixels lock_alpha(alpha); | 141 SkAutoLockPixels lock_alpha(alpha); |
| 142 SkAutoLockPixels lock_masked(masked); | 142 SkAutoLockPixels lock_masked(masked); |
| 143 | 143 |
| 144 for (int y = 0; y < masked.height(); ++y) { | 144 for (int y = 0; y < masked.height(); ++y) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 return masked; | 163 return masked; |
| 164 } | 164 } |
| 165 | 165 |
| 166 // static | 166 // static |
| 167 SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color, | 167 SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color, |
| 168 const SkBitmap& image, | 168 const SkBitmap& image, |
| 169 const SkBitmap& mask) { | 169 const SkBitmap& mask) { |
| 170 DCHECK(image.colorType() == kPMColor_SkColorType); | 170 DCHECK(image.colorType() == kN32_SkColorType); |
| 171 DCHECK(mask.colorType() == kPMColor_SkColorType); | 171 DCHECK(mask.colorType() == kN32_SkColorType); |
| 172 | 172 |
| 173 SkBitmap background; | 173 SkBitmap background; |
| 174 background.allocN32Pixels(mask.width(), mask.height()); | 174 background.allocN32Pixels(mask.width(), mask.height()); |
| 175 | 175 |
| 176 double bg_a = SkColorGetA(color); | 176 double bg_a = SkColorGetA(color); |
| 177 double bg_r = SkColorGetR(color); | 177 double bg_r = SkColorGetR(color); |
| 178 double bg_g = SkColorGetG(color); | 178 double bg_g = SkColorGetG(color); |
| 179 double bg_b = SkColorGetB(color); | 179 double bg_b = SkColorGetB(color); |
| 180 | 180 |
| 181 SkAutoLockPixels lock_mask(mask); | 181 SkAutoLockPixels lock_mask(mask); |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 // Lightness shift: 0 -> black, 0.5 -> NOP, 1 -> white. | 540 // Lightness shift: 0 -> black, 0.5 -> NOP, 1 -> white. |
| 541 if (hsl_shift.l >= 0 && hsl_shift.l <= (0.5 - HSLShift::epsilon)) | 541 if (hsl_shift.l >= 0 && hsl_shift.l <= (0.5 - HSLShift::epsilon)) |
| 542 L_op = HSLShift::kOpLDec; | 542 L_op = HSLShift::kOpLDec; |
| 543 else if (hsl_shift.l >= (0.5 + HSLShift::epsilon)) | 543 else if (hsl_shift.l >= (0.5 + HSLShift::epsilon)) |
| 544 L_op = HSLShift::kOpLInc; | 544 L_op = HSLShift::kOpLInc; |
| 545 | 545 |
| 546 HSLShift::LineProcessor line_proc = | 546 HSLShift::LineProcessor line_proc = |
| 547 HSLShift::kLineProcessors[H_op][S_op][L_op]; | 547 HSLShift::kLineProcessors[H_op][S_op][L_op]; |
| 548 | 548 |
| 549 DCHECK(bitmap.empty() == false); | 549 DCHECK(bitmap.empty() == false); |
| 550 DCHECK(bitmap.colorType() == kPMColor_SkColorType); | 550 DCHECK(bitmap.colorType() == kN32_SkColorType); |
| 551 | 551 |
| 552 SkBitmap shifted; | 552 SkBitmap shifted; |
| 553 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); | 553 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); |
| 554 shifted.eraseARGB(0, 0, 0, 0); | 554 shifted.eraseARGB(0, 0, 0, 0); |
| 555 | 555 |
| 556 SkAutoLockPixels lock_bitmap(bitmap); | 556 SkAutoLockPixels lock_bitmap(bitmap); |
| 557 SkAutoLockPixels lock_shifted(shifted); | 557 SkAutoLockPixels lock_shifted(shifted); |
| 558 | 558 |
| 559 // Loop through the pixels of the original bitmap. | 559 // Loop through the pixels of the original bitmap. |
| 560 for (int y = 0; y < bitmap.height(); ++y) { | 560 for (int y = 0; y < bitmap.height(); ++y) { |
| 561 SkPMColor* pixels = bitmap.getAddr32(0, y); | 561 SkPMColor* pixels = bitmap.getAddr32(0, y); |
| 562 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); | 562 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); |
| 563 | 563 |
| 564 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width()); | 564 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width()); |
| 565 } | 565 } |
| 566 | 566 |
| 567 return shifted; | 567 return shifted; |
| 568 } | 568 } |
| 569 | 569 |
| 570 // static | 570 // static |
| 571 SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source, | 571 SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source, |
| 572 int src_x, int src_y, | 572 int src_x, int src_y, |
| 573 int dst_w, int dst_h) { | 573 int dst_w, int dst_h) { |
| 574 DCHECK(source.colorType() == kPMColor_SkColorType); | 574 DCHECK(source.colorType() == kN32_SkColorType); |
| 575 | 575 |
| 576 SkBitmap cropped; | 576 SkBitmap cropped; |
| 577 cropped.allocN32Pixels(dst_w, dst_h); | 577 cropped.allocN32Pixels(dst_w, dst_h); |
| 578 cropped.eraseARGB(0, 0, 0, 0); | 578 cropped.eraseARGB(0, 0, 0, 0); |
| 579 | 579 |
| 580 SkAutoLockPixels lock_source(source); | 580 SkAutoLockPixels lock_source(source); |
| 581 SkAutoLockPixels lock_cropped(cropped); | 581 SkAutoLockPixels lock_cropped(cropped); |
| 582 | 582 |
| 583 // Loop through the pixels of the original bitmap. | 583 // Loop through the pixels of the original bitmap. |
| 584 for (int y = 0; y < dst_h; ++y) { | 584 for (int y = 0; y < dst_h; ++y) { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 *dst_pixel = unmultiplied; | 704 *dst_pixel = unmultiplied; |
| 705 } | 705 } |
| 706 } | 706 } |
| 707 } | 707 } |
| 708 | 708 |
| 709 return opaque_bitmap; | 709 return opaque_bitmap; |
| 710 } | 710 } |
| 711 | 711 |
| 712 // static | 712 // static |
| 713 SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) { | 713 SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) { |
| 714 DCHECK(image.colorType() == kPMColor_SkColorType); | 714 DCHECK(image.colorType() == kN32_SkColorType); |
| 715 | 715 |
| 716 SkBitmap transposed; | 716 SkBitmap transposed; |
| 717 transposed.allocN32Pixels(image.height(), image.width()); | 717 transposed.allocN32Pixels(image.height(), image.width()); |
| 718 | 718 |
| 719 SkAutoLockPixels lock_image(image); | 719 SkAutoLockPixels lock_image(image); |
| 720 SkAutoLockPixels lock_transposed(transposed); | 720 SkAutoLockPixels lock_transposed(transposed); |
| 721 | 721 |
| 722 for (int y = 0; y < image.height(); ++y) { | 722 for (int y = 0; y < image.height(); ++y) { |
| 723 uint32* image_row = image.getAddr32(0, y); | 723 uint32* image_row = image.getAddr32(0, y); |
| 724 for (int x = 0; x < image.width(); ++x) { | 724 for (int x = 0; x < image.width(); ++x) { |
| 725 uint32* dst = transposed.getAddr32(y, x); | 725 uint32* dst = transposed.getAddr32(y, x); |
| 726 *dst = image_row[x]; | 726 *dst = image_row[x]; |
| 727 } | 727 } |
| 728 } | 728 } |
| 729 | 729 |
| 730 return transposed; | 730 return transposed; |
| 731 } | 731 } |
| 732 | 732 |
| 733 // static | 733 // static |
| 734 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, | 734 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, |
| 735 SkColor c) { | 735 SkColor c) { |
| 736 DCHECK(bitmap.colorType() == kPMColor_SkColorType); | 736 DCHECK(bitmap.colorType() == kN32_SkColorType); |
| 737 | 737 |
| 738 SkBitmap color_mask; | 738 SkBitmap color_mask; |
| 739 color_mask.allocN32Pixels(bitmap.width(), bitmap.height()); | 739 color_mask.allocN32Pixels(bitmap.width(), bitmap.height()); |
| 740 color_mask.eraseARGB(0, 0, 0, 0); | 740 color_mask.eraseARGB(0, 0, 0, 0); |
| 741 | 741 |
| 742 SkCanvas canvas(color_mask); | 742 SkCanvas canvas(color_mask); |
| 743 | 743 |
| 744 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef( | 744 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef( |
| 745 SkColorFilter::CreateModeFilter(c, SkXfermode::kSrcIn_Mode)); | 745 SkColorFilter::CreateModeFilter(c, SkXfermode::kSrcIn_Mode)); |
| 746 SkPaint paint; | 746 SkPaint paint; |
| 747 paint.setColorFilter(color_filter.get()); | 747 paint.setColorFilter(color_filter.get()); |
| 748 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); | 748 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint); |
| 749 return color_mask; | 749 return color_mask; |
| 750 } | 750 } |
| 751 | 751 |
| 752 // static | 752 // static |
| 753 SkBitmap SkBitmapOperations::CreateDropShadow( | 753 SkBitmap SkBitmapOperations::CreateDropShadow( |
| 754 const SkBitmap& bitmap, | 754 const SkBitmap& bitmap, |
| 755 const gfx::ShadowValues& shadows) { | 755 const gfx::ShadowValues& shadows) { |
| 756 DCHECK(bitmap.colorType() == kPMColor_SkColorType); | 756 DCHECK(bitmap.colorType() == kN32_SkColorType); |
| 757 | 757 |
| 758 // Shadow margin insets are negative values because they grow outside. | 758 // Shadow margin insets are negative values because they grow outside. |
| 759 // Negate them here as grow direction is not important and only pixel value | 759 // Negate them here as grow direction is not important and only pixel value |
| 760 // is of interest here. | 760 // is of interest here. |
| 761 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows); | 761 gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows); |
| 762 | 762 |
| 763 SkBitmap image_with_shadow; | 763 SkBitmap image_with_shadow; |
| 764 image_with_shadow.allocN32Pixels(bitmap.width() + shadow_margin.width(), | 764 image_with_shadow.allocN32Pixels(bitmap.width() + shadow_margin.width(), |
| 765 bitmap.height() + shadow_margin.height()); | 765 bitmap.height() + shadow_margin.height()); |
| 766 image_with_shadow.eraseARGB(0, 0, 0, 0); | 766 image_with_shadow.eraseARGB(0, 0, 0, 0); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 793 | 793 |
| 794 // static | 794 // static |
| 795 SkBitmap SkBitmapOperations::Rotate(const SkBitmap& source, | 795 SkBitmap SkBitmapOperations::Rotate(const SkBitmap& source, |
| 796 RotationAmount rotation) { | 796 RotationAmount rotation) { |
| 797 SkBitmap result; | 797 SkBitmap result; |
| 798 SkScalar angle = SkFloatToScalar(0.0f); | 798 SkScalar angle = SkFloatToScalar(0.0f); |
| 799 | 799 |
| 800 switch (rotation) { | 800 switch (rotation) { |
| 801 case ROTATION_90_CW: | 801 case ROTATION_90_CW: |
| 802 angle = SkFloatToScalar(90.0f); | 802 angle = SkFloatToScalar(90.0f); |
| 803 result.setConfig( | 803 result.allocN32Pixels(source.height(), source.width()); |
| 804 SkBitmap::kARGB_8888_Config, source.height(), source.width()); | |
| 805 break; | 804 break; |
| 806 case ROTATION_180_CW: | 805 case ROTATION_180_CW: |
| 807 angle = SkFloatToScalar(180.0f); | 806 angle = SkFloatToScalar(180.0f); |
| 808 result.setConfig( | 807 result.allocN32Pixels(source.width(), source.height()); |
| 809 SkBitmap::kARGB_8888_Config, source.width(), source.height()); | |
| 810 break; | 808 break; |
| 811 case ROTATION_270_CW: | 809 case ROTATION_270_CW: |
| 812 angle = SkFloatToScalar(270.0f); | 810 angle = SkFloatToScalar(270.0f); |
| 813 result.setConfig( | 811 result.allocN32Pixels(source.height(), source.width()); |
| 814 SkBitmap::kARGB_8888_Config, source.height(), source.width()); | |
| 815 break; | 812 break; |
| 816 } | 813 } |
| 817 result.allocPixels(); | 814 |
| 818 SkCanvas canvas(result); | 815 SkCanvas canvas(result); |
| 819 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); | 816 canvas.clear(SkColorSetARGB(0, 0, 0, 0)); |
| 820 | 817 |
| 821 canvas.translate(SkFloatToScalar(result.width() * 0.5f), | 818 canvas.translate(SkFloatToScalar(result.width() * 0.5f), |
| 822 SkFloatToScalar(result.height() * 0.5f)); | 819 SkFloatToScalar(result.height() * 0.5f)); |
| 823 canvas.rotate(angle); | 820 canvas.rotate(angle); |
| 824 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), | 821 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), |
| 825 -SkFloatToScalar(source.height() * 0.5f)); | 822 -SkFloatToScalar(source.height() * 0.5f)); |
| 826 canvas.drawBitmap(source, 0, 0); | 823 canvas.drawBitmap(source, 0, 0); |
| 827 canvas.flush(); | 824 canvas.flush(); |
| 828 | 825 |
| 829 return result; | 826 return result; |
| 830 } | 827 } |
| OLD | NEW |