| 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 // Despite this assert, it seems like image is actually unpremultiplied. |
| 133 // The math producing dst_row[x] below is a correct SrcOver when |
| 134 // bg_* are premultiplied and img_* are unpremultiplied. |
| 132 DCHECK(image.colorType() == kN32_SkColorType); | 135 DCHECK(image.colorType() == kN32_SkColorType); |
| 133 DCHECK(mask.colorType() == kN32_SkColorType); | 136 DCHECK(mask.colorType() == kN32_SkColorType); |
| 134 | 137 |
| 135 SkBitmap background; | 138 SkBitmap background; |
| 136 background.allocN32Pixels(mask.width(), mask.height()); | 139 background.allocN32Pixels(mask.width(), mask.height()); |
| 137 | 140 |
| 138 double bg_a = SkColorGetA(color); | 141 double bg_a = SkColorGetA(color); |
| 139 double bg_r = SkColorGetR(color); | 142 double bg_r = SkColorGetR(color) * (bg_a / 255.0); |
| 140 double bg_g = SkColorGetG(color); | 143 double bg_g = SkColorGetG(color) * (bg_a / 255.0); |
| 141 double bg_b = SkColorGetB(color); | 144 double bg_b = SkColorGetB(color) * (bg_a / 255.0); |
| 142 | 145 |
| 143 SkAutoLockPixels lock_mask(mask); | 146 SkAutoLockPixels lock_mask(mask); |
| 144 SkAutoLockPixels lock_image(image); | 147 SkAutoLockPixels lock_image(image); |
| 145 SkAutoLockPixels lock_background(background); | 148 SkAutoLockPixels lock_background(background); |
| 146 | 149 |
| 147 for (int y = 0; y < mask.height(); ++y) { | 150 for (int y = 0; y < mask.height(); ++y) { |
| 148 uint32_t* dst_row = background.getAddr32(0, y); | 151 uint32_t* dst_row = background.getAddr32(0, y); |
| 149 uint32_t* image_row = image.getAddr32(0, y % image.height()); | 152 uint32_t* image_row = image.getAddr32(0, y % image.height()); |
| 150 uint32_t* mask_row = mask.getAddr32(0, y); | 153 uint32_t* mask_row = mask.getAddr32(0, y); |
| 151 | 154 |
| 152 for (int x = 0; x < mask.width(); ++x) { | 155 for (int x = 0; x < mask.width(); ++x) { |
| 153 uint32_t image_pixel = image_row[x % image.width()]; | 156 uint32_t image_pixel = image_row[x % image.width()]; |
| 154 | 157 |
| 155 double img_a = SkColorGetA(image_pixel); | 158 double img_a = SkColorGetA(image_pixel); |
| 156 double img_r = SkColorGetR(image_pixel); | 159 double img_r = SkColorGetR(image_pixel); |
| 157 double img_g = SkColorGetG(image_pixel); | 160 double img_g = SkColorGetG(image_pixel); |
| 158 double img_b = SkColorGetB(image_pixel); | 161 double img_b = SkColorGetB(image_pixel); |
| 159 | 162 |
| 160 double img_alpha = static_cast<double>(img_a) / 255.0; | 163 double img_alpha = img_a / 255.0; |
| 161 double img_inv = 1 - img_alpha; | 164 double img_inv = 1 - img_alpha; |
| 162 | 165 |
| 163 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; | 166 double mask_a = static_cast<double>(SkColorGetA(mask_row[x])) / 255.0; |
| 164 | 167 |
| 165 dst_row[x] = SkColorSetARGB( | 168 dst_row[x] = SkColorSetARGB( |
| 169 // This is pretty weird; why not the usual SrcOver alpha? |
| 166 static_cast<int>(std::min(255.0, bg_a + img_a) * mask_a), | 170 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), | 171 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), | 172 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)); | 173 static_cast<int>(((bg_b * img_inv) + (img_b * img_alpha)) * mask_a)); |
| 170 } | 174 } |
| 171 } | 175 } |
| 172 | 176 |
| 173 return background; | 177 return background; |
| 174 } | 178 } |
| 175 | 179 |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 canvas.translate(SkFloatToScalar(result.width() * 0.5f), | 784 canvas.translate(SkFloatToScalar(result.width() * 0.5f), |
| 781 SkFloatToScalar(result.height() * 0.5f)); | 785 SkFloatToScalar(result.height() * 0.5f)); |
| 782 canvas.rotate(angle); | 786 canvas.rotate(angle); |
| 783 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), | 787 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), |
| 784 -SkFloatToScalar(source.height() * 0.5f)); | 788 -SkFloatToScalar(source.height() * 0.5f)); |
| 785 canvas.drawBitmap(source, 0, 0); | 789 canvas.drawBitmap(source, 0, 0); |
| 786 canvas.flush(); | 790 canvas.flush(); |
| 787 | 791 |
| 788 return result; | 792 return result; |
| 789 } | 793 } |
| OLD | NEW |