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() == kN32_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()); |
danakj
2014/07/16 15:18:25
will it be filled with opaque values? you can save
hyunki
2014/07/16 15:28:48
Yes. I think so. Please check below codes to fill
danakj
2014/07/16 15:30:42
It depends on the input image right?
hyunki
2014/07/16 15:35:16
Yes. It depends on the input image. All alphas for
danakj
2014/07/16 15:38:11
So, do you want to tell allocN32 if it will be opa
hyunki
2014/07/16 15:42:17
No. I'm telling below operation fills whole alloce
danakj
2014/07/16 15:52:29
So then how about passing true for opaque to alloc
hyunki
2014/07/16 16:03:03
Sorry. For clarification,
(your question) will it
| |
30 inverted.eraseARGB(0, 0, 0, 0); | |
31 | 30 |
32 for (int y = 0; y < image.height(); ++y) { | 31 for (int y = 0; y < image.height(); ++y) { |
33 uint32* image_row = image.getAddr32(0, y); | 32 uint32* image_row = image.getAddr32(0, y); |
34 uint32* dst_row = inverted.getAddr32(0, y); | 33 uint32* dst_row = inverted.getAddr32(0, y); |
35 | 34 |
36 for (int x = 0; x < image.width(); ++x) { | 35 for (int x = 0; x < image.width(); ++x) { |
37 uint32 image_pixel = image_row[x]; | 36 uint32 image_pixel = image_row[x]; |
38 dst_row[x] = (image_pixel & 0xFF000000) | | 37 dst_row[x] = (image_pixel & 0xFF000000) | |
39 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); | 38 (0x00FFFFFF - (image_pixel & 0x00FFFFFF)); |
40 } | 39 } |
41 } | 40 } |
42 | 41 |
43 return inverted; | 42 return inverted; |
44 } | 43 } |
45 | 44 |
46 // static | 45 // static |
47 SkBitmap SkBitmapOperations::CreateSuperimposedBitmap(const SkBitmap& first, | |
48 const SkBitmap& second) { | |
49 DCHECK(first.width() == second.width()); | |
50 DCHECK(first.height() == second.height()); | |
51 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); | |
52 DCHECK(first.colorType() == kN32_SkColorType); | |
53 | |
54 SkAutoLockPixels lock_first(first); | |
55 SkAutoLockPixels lock_second(second); | |
56 | |
57 SkBitmap superimposed; | |
58 superimposed.allocN32Pixels(first.width(), first.height()); | |
59 superimposed.eraseARGB(0, 0, 0, 0); | |
60 | |
61 SkCanvas canvas(superimposed); | |
62 | |
63 SkRect rect; | |
64 rect.fLeft = 0; | |
65 rect.fTop = 0; | |
66 rect.fRight = SkIntToScalar(first.width()); | |
67 rect.fBottom = SkIntToScalar(first.height()); | |
68 | |
69 canvas.drawBitmapRect(first, NULL, rect); | |
70 canvas.drawBitmapRect(second, NULL, rect); | |
71 | |
72 return superimposed; | |
73 } | |
74 | |
75 // static | |
76 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, | 46 SkBitmap SkBitmapOperations::CreateBlendedBitmap(const SkBitmap& first, |
77 const SkBitmap& second, | 47 const SkBitmap& second, |
78 double alpha) { | 48 double alpha) { |
79 DCHECK((alpha >= 0) && (alpha <= 1)); | 49 DCHECK((alpha >= 0) && (alpha <= 1)); |
80 DCHECK(first.width() == second.width()); | 50 DCHECK(first.width() == second.width()); |
81 DCHECK(first.height() == second.height()); | 51 DCHECK(first.height() == second.height()); |
82 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); | 52 DCHECK(first.bytesPerPixel() == second.bytesPerPixel()); |
83 DCHECK(first.colorType() == kN32_SkColorType); | 53 DCHECK(first.colorType() == kN32_SkColorType); |
84 | 54 |
85 // Optimize for case where we won't need to blend anything. | 55 // Optimize for case where we won't need to blend anything. |
86 static const double alpha_min = 1.0 / 255; | 56 static const double alpha_min = 1.0 / 255; |
87 static const double alpha_max = 254.0 / 255; | 57 static const double alpha_max = 254.0 / 255; |
88 if (alpha < alpha_min) | 58 if (alpha < alpha_min) |
89 return first; | 59 return first; |
90 else if (alpha > alpha_max) | 60 else if (alpha > alpha_max) |
91 return second; | 61 return second; |
92 | 62 |
93 SkAutoLockPixels lock_first(first); | 63 SkAutoLockPixels lock_first(first); |
94 SkAutoLockPixels lock_second(second); | 64 SkAutoLockPixels lock_second(second); |
95 | 65 |
96 SkBitmap blended; | 66 SkBitmap blended; |
97 blended.allocN32Pixels(first.width(), first.height()); | 67 blended.allocN32Pixels(first.width(), first.height()); |
98 blended.eraseARGB(0, 0, 0, 0); | |
danakj
2014/07/16 15:18:25
ditto for the rest
hyunki
2014/07/16 15:28:48
ditto.
dst_row[x] = SkColorSetARGB(a, r, g, b);
| |
99 | 68 |
100 double first_alpha = 1 - alpha; | 69 double first_alpha = 1 - alpha; |
101 | 70 |
102 for (int y = 0; y < first.height(); ++y) { | 71 for (int y = 0; y < first.height(); ++y) { |
103 uint32* first_row = first.getAddr32(0, y); | 72 uint32* first_row = first.getAddr32(0, y); |
104 uint32* second_row = second.getAddr32(0, y); | 73 uint32* second_row = second.getAddr32(0, y); |
105 uint32* dst_row = blended.getAddr32(0, y); | 74 uint32* dst_row = blended.getAddr32(0, y); |
106 | 75 |
107 for (int x = 0; x < first.width(); ++x) { | 76 for (int x = 0; x < first.width(); ++x) { |
108 uint32 first_pixel = first_row[x]; | 77 uint32 first_pixel = first_row[x]; |
(...skipping 19 matching lines...) Expand all Loading... | |
128 SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb, | 97 SkBitmap SkBitmapOperations::CreateMaskedBitmap(const SkBitmap& rgb, |
129 const SkBitmap& alpha) { | 98 const SkBitmap& alpha) { |
130 DCHECK(rgb.width() == alpha.width()); | 99 DCHECK(rgb.width() == alpha.width()); |
131 DCHECK(rgb.height() == alpha.height()); | 100 DCHECK(rgb.height() == alpha.height()); |
132 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel()); | 101 DCHECK(rgb.bytesPerPixel() == alpha.bytesPerPixel()); |
133 DCHECK(rgb.colorType() == kN32_SkColorType); | 102 DCHECK(rgb.colorType() == kN32_SkColorType); |
134 DCHECK(alpha.colorType() == kN32_SkColorType); | 103 DCHECK(alpha.colorType() == kN32_SkColorType); |
135 | 104 |
136 SkBitmap masked; | 105 SkBitmap masked; |
137 masked.allocN32Pixels(rgb.width(), rgb.height()); | 106 masked.allocN32Pixels(rgb.width(), rgb.height()); |
138 masked.eraseARGB(0, 0, 0, 0); | |
139 | 107 |
140 SkAutoLockPixels lock_rgb(rgb); | 108 SkAutoLockPixels lock_rgb(rgb); |
141 SkAutoLockPixels lock_alpha(alpha); | 109 SkAutoLockPixels lock_alpha(alpha); |
142 SkAutoLockPixels lock_masked(masked); | 110 SkAutoLockPixels lock_masked(masked); |
143 | 111 |
144 for (int y = 0; y < masked.height(); ++y) { | 112 for (int y = 0; y < masked.height(); ++y) { |
145 uint32* rgb_row = rgb.getAddr32(0, y); | 113 uint32* rgb_row = rgb.getAddr32(0, y); |
146 uint32* alpha_row = alpha.getAddr32(0, y); | 114 uint32* alpha_row = alpha.getAddr32(0, y); |
147 uint32* dst_row = masked.getAddr32(0, y); | 115 uint32* dst_row = masked.getAddr32(0, y); |
148 | 116 |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 L_op = HSLShift::kOpLInc; | 512 L_op = HSLShift::kOpLInc; |
545 | 513 |
546 HSLShift::LineProcessor line_proc = | 514 HSLShift::LineProcessor line_proc = |
547 HSLShift::kLineProcessors[H_op][S_op][L_op]; | 515 HSLShift::kLineProcessors[H_op][S_op][L_op]; |
548 | 516 |
549 DCHECK(bitmap.empty() == false); | 517 DCHECK(bitmap.empty() == false); |
550 DCHECK(bitmap.colorType() == kN32_SkColorType); | 518 DCHECK(bitmap.colorType() == kN32_SkColorType); |
551 | 519 |
552 SkBitmap shifted; | 520 SkBitmap shifted; |
553 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); | 521 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); |
554 shifted.eraseARGB(0, 0, 0, 0); | |
555 | 522 |
556 SkAutoLockPixels lock_bitmap(bitmap); | 523 SkAutoLockPixels lock_bitmap(bitmap); |
557 SkAutoLockPixels lock_shifted(shifted); | 524 SkAutoLockPixels lock_shifted(shifted); |
558 | 525 |
559 // Loop through the pixels of the original bitmap. | 526 // Loop through the pixels of the original bitmap. |
560 for (int y = 0; y < bitmap.height(); ++y) { | 527 for (int y = 0; y < bitmap.height(); ++y) { |
561 SkPMColor* pixels = bitmap.getAddr32(0, y); | 528 SkPMColor* pixels = bitmap.getAddr32(0, y); |
562 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); | 529 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); |
563 | 530 |
564 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width()); | 531 (*line_proc)(hsl_shift, pixels, tinted_pixels, bitmap.width()); |
565 } | 532 } |
566 | 533 |
567 return shifted; | 534 return shifted; |
568 } | 535 } |
569 | 536 |
570 // static | 537 // static |
571 SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source, | 538 SkBitmap SkBitmapOperations::CreateTiledBitmap(const SkBitmap& source, |
572 int src_x, int src_y, | 539 int src_x, int src_y, |
573 int dst_w, int dst_h) { | 540 int dst_w, int dst_h) { |
574 DCHECK(source.colorType() == kN32_SkColorType); | 541 DCHECK(source.colorType() == kN32_SkColorType); |
575 | 542 |
576 SkBitmap cropped; | 543 SkBitmap cropped; |
577 cropped.allocN32Pixels(dst_w, dst_h); | 544 cropped.allocN32Pixels(dst_w, dst_h); |
578 cropped.eraseARGB(0, 0, 0, 0); | |
579 | 545 |
580 SkAutoLockPixels lock_source(source); | 546 SkAutoLockPixels lock_source(source); |
581 SkAutoLockPixels lock_cropped(cropped); | 547 SkAutoLockPixels lock_cropped(cropped); |
582 | 548 |
583 // Loop through the pixels of the original bitmap. | 549 // Loop through the pixels of the original bitmap. |
584 for (int y = 0; y < dst_h; ++y) { | 550 for (int y = 0; y < dst_h; ++y) { |
585 int y_pix = (src_y + y) % source.height(); | 551 int y_pix = (src_y + y) % source.height(); |
586 while (y_pix < 0) | 552 while (y_pix < 0) |
587 y_pix += source.height(); | 553 y_pix += source.height(); |
588 | 554 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
818 canvas.translate(SkFloatToScalar(result.width() * 0.5f), | 784 canvas.translate(SkFloatToScalar(result.width() * 0.5f), |
819 SkFloatToScalar(result.height() * 0.5f)); | 785 SkFloatToScalar(result.height() * 0.5f)); |
820 canvas.rotate(angle); | 786 canvas.rotate(angle); |
821 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), | 787 canvas.translate(-SkFloatToScalar(source.width() * 0.5f), |
822 -SkFloatToScalar(source.height() * 0.5f)); | 788 -SkFloatToScalar(source.height() * 0.5f)); |
823 canvas.drawBitmap(source, 0, 0); | 789 canvas.drawBitmap(source, 0, 0); |
824 canvas.flush(); | 790 canvas.flush(); |
825 | 791 |
826 return result; | 792 return result; |
827 } | 793 } |
OLD | NEW |