Chromium Code Reviews| 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> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 | 124 |
| 125 return masked; | 125 return masked; |
| 126 } | 126 } |
| 127 | 127 |
| 128 // static | 128 // static |
| 129 SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color, | 129 SkBitmap SkBitmapOperations::CreateButtonBackground(SkColor color, |
| 130 const SkBitmap& image, | 130 const SkBitmap& image, |
| 131 const SkBitmap& mask) { | 131 const SkBitmap& mask) { |
| 132 // TODO: can we just do this with a couple SkCanvas draw calls? | |
|
danakj
2016/06/22 00:51:27
TODO format is:
TODO(chromiumname): text
or
TODO(
mtklein_C
2016/07/28 19:56:04
Gotcha. Good point... no one will probably ever c
| |
| 133 // e.g. drawColor(color) -> drawBitmap(image) -> drawBitmap(mask, kDstIn_Mode) | |
| 134 // This requires we first pin down the premul/unpremul state of each bitmap. | |
| 135 | |
| 136 // TODO: It sure seems like image is actually unpremultiplied. | |
| 137 // The math producing dst_row[x] below is a correct SrcOver when bg is | |
|
mtklein_C
2016/06/22 15:19:40
Is this one too far away?
sky
2016/06/22 15:37:43
It wasn't obvious to me that the two relate. Comme
mtklein_C
2016/07/28 19:56:04
Fixed up, I think.
| |
| 138 // premultiplied and img is unpremultiplied. | |
| 132 DCHECK(image.colorType() == kN32_SkColorType); | 139 DCHECK(image.colorType() == kN32_SkColorType); |
| 133 DCHECK(mask.colorType() == kN32_SkColorType); | 140 DCHECK(mask.colorType() == kN32_SkColorType); |
| 134 | 141 |
| 135 SkBitmap background; | 142 SkBitmap background; |
| 136 background.allocN32Pixels(mask.width(), mask.height()); | 143 background.allocN32Pixels(mask.width(), mask.height()); |
| 137 | 144 |
| 145 // We premultiply bg here. | |
|
sky
2016/06/22 15:17:40
This comment just documents the code, and isn't ve
mtklein_C
2016/07/28 19:56:04
Gone.
| |
| 138 double bg_a = SkColorGetA(color); | 146 double bg_a = SkColorGetA(color); |
| 139 double bg_r = SkColorGetR(color); | 147 double bg_r = SkColorGetR(color) * (bg_a / 255.0); |
| 140 double bg_g = SkColorGetG(color); | 148 double bg_g = SkColorGetG(color) * (bg_a / 255.0); |
| 141 double bg_b = SkColorGetB(color); | 149 double bg_b = SkColorGetB(color) * (bg_a / 255.0); |
| 142 | 150 |
| 143 SkAutoLockPixels lock_mask(mask); | 151 SkAutoLockPixels lock_mask(mask); |
| 144 SkAutoLockPixels lock_image(image); | 152 SkAutoLockPixels lock_image(image); |
| 145 SkAutoLockPixels lock_background(background); | 153 SkAutoLockPixels lock_background(background); |
| 146 | 154 |
| 147 for (int y = 0; y < mask.height(); ++y) { | 155 for (int y = 0; y < mask.height(); ++y) { |
| 148 uint32_t* dst_row = background.getAddr32(0, y); | 156 uint32_t* dst_row = background.getAddr32(0, y); |
| 149 uint32_t* image_row = image.getAddr32(0, y % image.height()); | 157 uint32_t* image_row = image.getAddr32(0, y % image.height()); |
| 150 uint32_t* mask_row = mask.getAddr32(0, y); | 158 uint32_t* mask_row = mask.getAddr32(0, y); |
| 151 | 159 |
| 152 for (int x = 0; x < mask.width(); ++x) { | 160 for (int x = 0; x < mask.width(); ++x) { |
| 153 uint32_t image_pixel = image_row[x % image.width()]; | 161 uint32_t image_pixel = image_row[x % image.width()]; |
| 154 | 162 |
| 155 double img_a = SkColorGetA(image_pixel); | 163 double img_a = SkColorGetA(image_pixel); |
| 156 double img_r = SkColorGetR(image_pixel); | 164 double img_r = SkColorGetR(image_pixel); |
| 157 double img_g = SkColorGetG(image_pixel); | 165 double img_g = SkColorGetG(image_pixel); |
| 158 double img_b = SkColorGetB(image_pixel); | 166 double img_b = SkColorGetB(image_pixel); |
| 159 | 167 |
| 160 double img_alpha = static_cast<double>(img_a) / 255.0; | 168 double img_alpha = img_a / 255.0; |
| 161 double img_inv = 1 - img_alpha; | 169 double img_inv = 1 - img_alpha; |
| 162 | 170 |
| 163 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; | 171 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; |
| 164 | 172 |
| 165 dst_row[x] = SkColorSetARGB( | 173 dst_row[x] = SkColorSetARGB( |
| 174 // TODO: why not the usual SrcOver alpha? | |
| 166 static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a), | 175 static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a), |
| 167 static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a), | 176 static_cast<int>(((bg_r * img_inv) + (img_r * img_alpha)) * mask_a), |
| 168 static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a), | 177 static_cast<int>(((bg_g * img_inv) + (img_g * img_alpha)) * mask_a), |
| 169 static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a)); | 178 static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a)); |
| 170 } | 179 } |
| 171 } | 180 } |
| 172 | 181 |
| 173 return background; | 182 return background; |
| 174 } | 183 } |
| 175 | 184 |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 canvas.translate(SkFloatToScalar(result.width() * 0.5f), | 791 canvas.translate(SkFloatToScalar(result.width() * 0.5f), |
| 783 SkFloatToScalar(result.height() * 0.5f)); | 792 SkFloatToScalar(result.height() * 0.5f)); |
| 784 canvas.rotate(angle); | 793 canvas.rotate(angle); |
| 785 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), | 794 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), |
| 786 -SkFloatToScalar(source.height() * 0.5f)); | 795 -SkFloatToScalar(source.height() * 0.5f)); |
| 787 canvas.drawBitmap(source, 0, 0); | 796 canvas.drawBitmap(source, 0, 0); |
| 788 canvas.flush(); | 797 canvas.flush(); |
| 789 | 798 |
| 790 return result; | 799 return result; |
| 791 } | 800 } |
| OLD | NEW |