| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 7 #include <string.h> | 9 #include <string.h> |
| 8 #include <algorithm> | 10 #include <algorithm> |
| 9 | 11 |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "skia/ext/refptr.h" | 13 #include "skia/ext/refptr.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 #include "third_party/skia/include/core/SkColorFilter.h" | 16 #include "third_party/skia/include/core/SkColorFilter.h" |
| 15 #include "third_party/skia/include/core/SkColorPriv.h" | 17 #include "third_party/skia/include/core/SkColorPriv.h" |
| 16 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 18 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 17 #include "third_party/skia/include/effects/SkBlurImageFilter.h" | 19 #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
| 18 #include "ui/gfx/geometry/insets.h" | 20 #include "ui/gfx/geometry/insets.h" |
| 19 #include "ui/gfx/geometry/point.h" | 21 #include "ui/gfx/geometry/point.h" |
| 20 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
| 21 | 23 |
| 22 // static | 24 // static |
| 23 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { | 25 SkBitmap SkBitmapOperations::CreateInvertedBitmap(const SkBitmap& image) { |
| 24 DCHECK(image.colorType() == kN32_SkColorType); | 26 DCHECK(image.colorType() == kN32_SkColorType); |
| 25 | 27 |
| 26 SkAutoLockPixels lock_image(image); | 28 SkAutoLockPixels lock_image(image); |
| 27 | 29 |
| 28 SkBitmap inverted; | 30 SkBitmap inverted; |
| 29 inverted.allocN32Pixels(image.width(), image.height()); | 31 inverted.allocN32Pixels(image.width(), image.height()); |
| 30 | 32 |
| 31 for (int y = 0; y < image.height(); ++y) { | 33 for (int y = 0; y < image.height(); ++y) { |
| 32 uint32* image_row = image.getAddr32(0, y); | 34 uint32_t* image_row = image.getAddr32(0, y); |
| 33 uint32* dst_row = inverted.getAddr32(0, y); | 35 uint32_t* dst_row = inverted.getAddr32(0, y); |
| 34 | 36 |
| 35 for (int x = 0; x < image.width(); ++x) { | 37 for (int x = 0; x < image.width(); ++x) { |
| 36 uint32 image_pixel = image_row[x]; | 38 uint32_t image_pixel = image_row[x]; |
| 37 dst_row[x] = (image_pixel & 0xFF000000) | | 39 dst_row[x] = (image_pixel & 0xFF000000) | |
| 38 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); | 40 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); |
| 39 } | 41 } |
| 40 } | 42 } |
| 41 | 43 |
| 42 return inverted; | 44 return inverted; |
| 43 } | 45 } |
| 44 | 46 |
| 45 // static | 47 // static |
| 46 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, | 48 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 62 | 64 |
| 63 SkAutoLockPixels lock_first(first); | 65 SkAutoLockPixels lock_first(first); |
| 64 SkAutoLockPixels lock_second(second); | 66 SkAutoLockPixels lock_second(second); |
| 65 | 67 |
| 66 SkBitmap blended; | 68 SkBitmap blended; |
| 67 blended.allocN32Pixels(first.width(), first.height()); | 69 blended.allocN32Pixels(first.width(), first.height()); |
| 68 | 70 |
| 69 double first_alpha = 1 - alpha; | 71 double first_alpha = 1 - alpha; |
| 70 | 72 |
| 71 for (int y = 0; y < first.height(); ++y) { | 73 for (int y = 0; y < first.height(); ++y) { |
| 72 uint32* first_row = first.getAddr32(0, y); | 74 uint32_t* first_row = first.getAddr32(0, y); |
| 73 uint32* second_row = second.getAddr32(0, y); | 75 uint32_t* second_row = second.getAddr32(0, y); |
| 74 uint32* dst_row = blended.getAddr32(0, y); | 76 uint32_t* dst_row = blended.getAddr32(0, y); |
| 75 | 77 |
| 76 for (int x = 0; x < first.width(); ++x) { | 78 for (int x = 0; x < first.width(); ++x) { |
| 77 uint32 first_pixel = first_row[x]; | 79 uint32_t first_pixel = first_row[x]; |
| 78 uint32 second_pixel = second_row[x]; | 80 uint32_t second_pixel = second_row[x]; |
| 79 | 81 |
| 80 int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) + | 82 int a = static_cast<int>((SkColorGetA(first_pixel) * first_alpha) + |
| 81 (SkColorGetA(second_pixel) * alpha)); | 83 (SkColorGetA(second_pixel) * alpha)); |
| 82 int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) + | 84 int r = static_cast<int>((SkColorGetR(first_pixel) * first_alpha) + |
| 83 (SkColorGetR(second_pixel) * alpha)); | 85 (SkColorGetR(second_pixel) * alpha)); |
| 84 int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) + | 86 int g = static_cast<int>((SkColorGetG(first_pixel) * first_alpha) + |
| 85 (SkColorGetG(second_pixel) * alpha)); | 87 (SkColorGetG(second_pixel) * alpha)); |
| 86 int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) + | 88 int b = static_cast<int>((SkColorGetB(first_pixel) * first_alpha) + |
| 87 (SkColorGetB(second_pixel) * alpha)); | 89 (SkColorGetB(second_pixel) * alpha)); |
| 88 | 90 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 DCHECK(alpha.colorType() == kN32_SkColorType); | 105 DCHECK(alpha.colorType() == kN32_SkColorType); |
| 104 | 106 |
| 105 SkBitmap masked; | 107 SkBitmap masked; |
| 106 masked.allocN32Pixels(rgb.width(), rgb.height()); | 108 masked.allocN32Pixels(rgb.width(), rgb.height()); |
| 107 | 109 |
| 108 SkAutoLockPixels lock_rgb(rgb); | 110 SkAutoLockPixels lock_rgb(rgb); |
| 109 SkAutoLockPixels lock_alpha(alpha); | 111 SkAutoLockPixels lock_alpha(alpha); |
| 110 SkAutoLockPixels lock_masked(masked); | 112 SkAutoLockPixels lock_masked(masked); |
| 111 | 113 |
| 112 for (int y = 0; y < masked.height(); ++y) { | 114 for (int y = 0; y < masked.height(); ++y) { |
| 113 uint32* rgb_row = rgb.getAddr32(0, y); | 115 uint32_t* rgb_row = rgb.getAddr32(0, y); |
| 114 uint32* alpha_row = alpha.getAddr32(0, y); | 116 uint32_t* alpha_row = alpha.getAddr32(0, y); |
| 115 uint32* dst_row = masked.getAddr32(0, y); | 117 uint32_t* dst_row = masked.getAddr32(0, y); |
| 116 | 118 |
| 117 for (int x = 0; x < masked.width(); ++x) { | 119 for (int x = 0; x < masked.width(); ++x) { |
| 118 unsigned alpha = SkGetPackedA32(alpha_row[x]); | 120 unsigned alpha = SkGetPackedA32(alpha_row[x]); |
| 119 unsigned scale = SkAlpha255To256(alpha); | 121 unsigned scale = SkAlpha255To256(alpha); |
| 120 dst_row[x] = SkAlphaMulQ(rgb_row[x], scale); | 122 dst_row[x] = SkAlphaMulQ(rgb_row[x], scale); |
| 121 } | 123 } |
| 122 } | 124 } |
| 123 | 125 |
| 124 return masked; | 126 return masked; |
| 125 } | 127 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 137 double bg_a = SkColorGetA(color); | 139 double bg_a = SkColorGetA(color); |
| 138 double bg_r = SkColorGetR(color); | 140 double bg_r = SkColorGetR(color); |
| 139 double bg_g = SkColorGetG(color); | 141 double bg_g = SkColorGetG(color); |
| 140 double bg_b = SkColorGetB(color); | 142 double bg_b = SkColorGetB(color); |
| 141 | 143 |
| 142 SkAutoLockPixels lock_mask(mask); | 144 SkAutoLockPixels lock_mask(mask); |
| 143 SkAutoLockPixels lock_image(image); | 145 SkAutoLockPixels lock_image(image); |
| 144 SkAutoLockPixels lock_background(background); | 146 SkAutoLockPixels lock_background(background); |
| 145 | 147 |
| 146 for (int y = 0; y < mask.height(); ++y) { | 148 for (int y = 0; y < mask.height(); ++y) { |
| 147 uint32* dst_row = background.getAddr32(0, y); | 149 uint32_t* dst_row = background.getAddr32(0, y); |
| 148 uint32* image_row = image.getAddr32(0, y % image.height()); | 150 uint32_t* image_row = image.getAddr32(0, y % image.height()); |
| 149 uint32* mask_row = mask.getAddr32(0, y); | 151 uint32_t* mask_row = mask.getAddr32(0, y); |
| 150 | 152 |
| 151 for (int x = 0; x < mask.width(); ++x) { | 153 for (int x = 0; x < mask.width(); ++x) { |
| 152 uint32 image_pixel = image_row[x % image.width()]; | 154 uint32_t image_pixel = image_row[x % image.width()]; |
| 153 | 155 |
| 154 double img_a = SkColorGetA(image_pixel); | 156 double img_a = SkColorGetA(image_pixel); |
| 155 double img_r = SkColorGetR(image_pixel); | 157 double img_r = SkColorGetR(image_pixel); |
| 156 double img_g = SkColorGetG(image_pixel); | 158 double img_g = SkColorGetG(image_pixel); |
| 157 double img_b = SkColorGetB(image_pixel); | 159 double img_b = SkColorGetB(image_pixel); |
| 158 | 160 |
| 159 double img_alpha = static_cast<double>(img_a) / 255.0; | 161 double img_alpha = static_cast<double>(img_a) / 255.0; |
| 160 double img_inv = 1 - img_alpha; | 162 double img_inv = 1 - img_alpha; |
| 161 | 163 |
| 162 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; | 164 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 | 540 |
| 539 SkAutoLockPixels lock_source(source); | 541 SkAutoLockPixels lock_source(source); |
| 540 SkAutoLockPixels lock_cropped(cropped); | 542 SkAutoLockPixels lock_cropped(cropped); |
| 541 | 543 |
| 542 // Loop through the pixels of the original bitmap. | 544 // Loop through the pixels of the original bitmap. |
| 543 for (int y = 0; y < dst_h; ++y) { | 545 for (int y = 0; y < dst_h; ++y) { |
| 544 int y_pix = (src_y + y) % source.height(); | 546 int y_pix = (src_y + y) % source.height(); |
| 545 while (y_pix < 0) | 547 while (y_pix < 0) |
| 546 y_pix += source.height(); | 548 y_pix += source.height(); |
| 547 | 549 |
| 548 uint32* source_row = source.getAddr32(0, y_pix); | 550 uint32_t* source_row = source.getAddr32(0, y_pix); |
| 549 uint32* dst_row = cropped.getAddr32(0, y); | 551 uint32_t* dst_row = cropped.getAddr32(0, y); |
| 550 | 552 |
| 551 for (int x = 0; x < dst_w; ++x) { | 553 for (int x = 0; x < dst_w; ++x) { |
| 552 int x_pix = (src_x + x) % source.width(); | 554 int x_pix = (src_x + x) % source.width(); |
| 553 while (x_pix < 0) | 555 while (x_pix < 0) |
| 554 x_pix += source.width(); | 556 x_pix += source.width(); |
| 555 | 557 |
| 556 dst_row[x] = source_row[x_pix]; | 558 dst_row[x] = source_row[x_pix]; |
| 557 } | 559 } |
| 558 } | 560 } |
| 559 | 561 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 SkImageInfo::Make(info.width(), info.height(), info.colorType(), | 654 SkImageInfo::Make(info.width(), info.height(), info.colorType(), |
| 653 kOpaque_SkAlphaType, info.profileType()); | 655 kOpaque_SkAlphaType, info.profileType()); |
| 654 SkBitmap opaque_bitmap; | 656 SkBitmap opaque_bitmap; |
| 655 opaque_bitmap.allocPixels(opaque_info); | 657 opaque_bitmap.allocPixels(opaque_info); |
| 656 | 658 |
| 657 { | 659 { |
| 658 SkAutoLockPixels bitmap_lock(bitmap); | 660 SkAutoLockPixels bitmap_lock(bitmap); |
| 659 SkAutoLockPixels opaque_bitmap_lock(opaque_bitmap); | 661 SkAutoLockPixels opaque_bitmap_lock(opaque_bitmap); |
| 660 for (int y = 0; y < opaque_bitmap.height(); y++) { | 662 for (int y = 0; y < opaque_bitmap.height(); y++) { |
| 661 for (int x = 0; x < opaque_bitmap.width(); x++) { | 663 for (int x = 0; x < opaque_bitmap.width(); x++) { |
| 662 uint32 src_pixel = *bitmap.getAddr32(x, y); | 664 uint32_t src_pixel = *bitmap.getAddr32(x, y); |
| 663 uint32* dst_pixel = opaque_bitmap.getAddr32(x, y); | 665 uint32_t* dst_pixel = opaque_bitmap.getAddr32(x, y); |
| 664 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel); | 666 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(src_pixel); |
| 665 *dst_pixel = unmultiplied; | 667 *dst_pixel = unmultiplied; |
| 666 } | 668 } |
| 667 } | 669 } |
| 668 } | 670 } |
| 669 | 671 |
| 670 return opaque_bitmap; | 672 return opaque_bitmap; |
| 671 } | 673 } |
| 672 | 674 |
| 673 // static | 675 // static |
| 674 SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) { | 676 SkBitmap SkBitmapOperations::CreateTransposedBitmap(const SkBitmap& image) { |
| 675 DCHECK(image.colorType() == kN32_SkColorType); | 677 DCHECK(image.colorType() == kN32_SkColorType); |
| 676 | 678 |
| 677 SkBitmap transposed; | 679 SkBitmap transposed; |
| 678 transposed.allocN32Pixels(image.height(), image.width()); | 680 transposed.allocN32Pixels(image.height(), image.width()); |
| 679 | 681 |
| 680 SkAutoLockPixels lock_image(image); | 682 SkAutoLockPixels lock_image(image); |
| 681 SkAutoLockPixels lock_transposed(transposed); | 683 SkAutoLockPixels lock_transposed(transposed); |
| 682 | 684 |
| 683 for (int y = 0; y < image.height(); ++y) { | 685 for (int y = 0; y < image.height(); ++y) { |
| 684 uint32* image_row = image.getAddr32(0, y); | 686 uint32_t* image_row = image.getAddr32(0, y); |
| 685 for (int x = 0; x < image.width(); ++x) { | 687 for (int x = 0; x < image.width(); ++x) { |
| 686 uint32* dst = transposed.getAddr32(y, x); | 688 uint32_t* dst = transposed.getAddr32(y, x); |
| 687 *dst = image_row[x]; | 689 *dst = image_row[x]; |
| 688 } | 690 } |
| 689 } | 691 } |
| 690 | 692 |
| 691 return transposed; | 693 return transposed; |
| 692 } | 694 } |
| 693 | 695 |
| 694 // static | 696 // static |
| 695 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, | 697 SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap, |
| 696 SkColor c) { | 698 SkColor c) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 canvas.translate(SkFloatToScalar(result.width() * 0.5f), | 786 canvas.translate(SkFloatToScalar(result.width() * 0.5f), |
| 785 SkFloatToScalar(result.height() * 0.5f)); | 787 SkFloatToScalar(result.height() * 0.5f)); |
| 786 canvas.rotate(angle); | 788 canvas.rotate(angle); |
| 787 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), | 789 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), |
| 788 -SkFloatToScalar(source.height() * 0.5f)); | 790 -SkFloatToScalar(source.height() * 0.5f)); |
| 789 canvas.drawBitmap(source, 0, 0); | 791 canvas.drawBitmap(source, 0, 0); |
| 790 canvas.flush(); | 792 canvas.flush(); |
| 791 | 793 |
| 792 return result; | 794 return result; |
| 793 } | 795 } |
| OLD | NEW |