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 <algorithm> | 5 #include <algorithm> |
6 #include <cmath> | 6 #include <cmath> |
7 #include <iomanip> | 7 #include <iomanip> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // Returns true if each channel of the given two colors are "close." This is | 104 // Returns true if each channel of the given two colors are "close." This is |
105 // used for comparing colors where rounding errors may cause off-by-one. | 105 // used for comparing colors where rounding errors may cause off-by-one. |
106 bool ColorsClose(uint32_t a, uint32_t b) { | 106 bool ColorsClose(uint32_t a, uint32_t b) { |
107 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && | 107 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && |
108 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && | 108 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && |
109 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 && | 109 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 && |
110 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; | 110 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; |
111 } | 111 } |
112 | 112 |
113 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { | 113 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { |
114 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 114 bmp->allocN32Pixels(w, h); |
115 bmp->allocPixels(); | |
116 | 115 |
117 for (int y = 0; y < h; ++y) { | 116 for (int y = 0; y < h; ++y) { |
118 for (int x = 0; x < w; ++x) { | 117 for (int x = 0; x < w; ++x) { |
119 const uint8_t component = static_cast<uint8_t>(y * w + x); | 118 const uint8_t component = static_cast<uint8_t>(y * w + x); |
120 const SkColor pixel = SkColorSetARGB(component, component, | 119 const SkColor pixel = SkColorSetARGB(component, component, |
121 component, component); | 120 component, component); |
122 *bmp->getAddr32(x, y) = pixel; | 121 *bmp->getAddr32(x, y) = pixel; |
123 } | 122 } |
124 } | 123 } |
125 } | 124 } |
126 | 125 |
127 // Draws a horizontal and vertical grid into the w x h bitmap passed in. | 126 // Draws a horizontal and vertical grid into the w x h bitmap passed in. |
128 // Each line in the grid is drawn with a width of "grid_width" pixels, | 127 // Each line in the grid is drawn with a width of "grid_width" pixels, |
129 // and those lines repeat every "grid_pitch" pixels. The top left pixel (0, 0) | 128 // and those lines repeat every "grid_pitch" pixels. The top left pixel (0, 0) |
130 // is considered to be part of a grid line. | 129 // is considered to be part of a grid line. |
131 // The pixels that fall on a line are colored with "grid_color", while those | 130 // The pixels that fall on a line are colored with "grid_color", while those |
132 // outside of the lines are colored in "background_color". | 131 // outside of the lines are colored in "background_color". |
133 // Note that grid_with can be greather than or equal to grid_pitch, in which | 132 // Note that grid_with can be greather than or equal to grid_pitch, in which |
134 // case the resulting bitmap will be a solid color "grid_color". | 133 // case the resulting bitmap will be a solid color "grid_color". |
135 void DrawGridToBitmap(int w, int h, | 134 void DrawGridToBitmap(int w, int h, |
136 SkColor background_color, SkColor grid_color, | 135 SkColor background_color, SkColor grid_color, |
137 int grid_pitch, int grid_width, | 136 int grid_pitch, int grid_width, |
138 SkBitmap* bmp) { | 137 SkBitmap* bmp) { |
139 ASSERT_GT(grid_pitch, 0); | 138 ASSERT_GT(grid_pitch, 0); |
140 ASSERT_GT(grid_width, 0); | 139 ASSERT_GT(grid_width, 0); |
141 ASSERT_NE(background_color, grid_color); | 140 ASSERT_NE(background_color, grid_color); |
142 | 141 |
143 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 142 bmp->allocN32Pixels(w, h); |
144 bmp->allocPixels(); | |
145 | 143 |
146 for (int y = 0; y < h; ++y) { | 144 for (int y = 0; y < h; ++y) { |
147 bool y_on_grid = ((y % grid_pitch) < grid_width); | 145 bool y_on_grid = ((y % grid_pitch) < grid_width); |
148 | 146 |
149 for (int x = 0; x < w; ++x) { | 147 for (int x = 0; x < w; ++x) { |
150 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width)); | 148 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width)); |
151 | 149 |
152 *bmp->getAddr32(x, y) = (on_grid ? grid_color : background_color); | 150 *bmp->getAddr32(x, y) = (on_grid ? grid_color : background_color); |
153 } | 151 } |
154 } | 152 } |
155 } | 153 } |
156 | 154 |
157 // Draws a checkerboard pattern into the w x h bitmap passed in. | 155 // Draws a checkerboard pattern into the w x h bitmap passed in. |
158 // Each rectangle is rect_w in width, rect_h in height. | 156 // Each rectangle is rect_w in width, rect_h in height. |
159 // The colors alternate between color1 and color2, color1 being used | 157 // The colors alternate between color1 and color2, color1 being used |
160 // in the rectangle at the top left corner. | 158 // in the rectangle at the top left corner. |
161 void DrawCheckerToBitmap(int w, int h, | 159 void DrawCheckerToBitmap(int w, int h, |
162 SkColor color1, SkColor color2, | 160 SkColor color1, SkColor color2, |
163 int rect_w, int rect_h, | 161 int rect_w, int rect_h, |
164 SkBitmap* bmp) { | 162 SkBitmap* bmp) { |
165 ASSERT_GT(rect_w, 0); | 163 ASSERT_GT(rect_w, 0); |
166 ASSERT_GT(rect_h, 0); | 164 ASSERT_GT(rect_h, 0); |
167 ASSERT_NE(color1, color2); | 165 ASSERT_NE(color1, color2); |
168 | 166 |
169 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 167 bmp->allocN32Pixels(w, h); |
170 bmp->allocPixels(); | |
171 | 168 |
172 for (int y = 0; y < h; ++y) { | 169 for (int y = 0; y < h; ++y) { |
173 bool y_bit = (((y / rect_h) & 0x1) == 0); | 170 bool y_bit = (((y / rect_h) & 0x1) == 0); |
174 | 171 |
175 for (int x = 0; x < w; ++x) { | 172 for (int x = 0; x < w; ++x) { |
176 bool x_bit = (((x / rect_w) & 0x1) == 0); | 173 bool x_bit = (((x / rect_w) & 0x1) == 0); |
177 | 174 |
178 bool use_color2 = (x_bit != y_bit); // xor | 175 bool use_color2 = (x_bit != y_bit); // xor |
179 | 176 |
180 *bmp->getAddr32(x, y) = (use_color2 ? color2 : color1); | 177 *bmp->getAddr32(x, y) = (use_color2 ? color2 : color1); |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 ASSERT_EQ( | 454 ASSERT_EQ( |
458 *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), | 455 *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), |
459 *subset_results.getAddr32(x, y)); | 456 *subset_results.getAddr32(x, y)); |
460 } | 457 } |
461 } | 458 } |
462 } | 459 } |
463 | 460 |
464 TEST(ImageOperations, InvalidParams) { | 461 TEST(ImageOperations, InvalidParams) { |
465 // Make our source bitmap. | 462 // Make our source bitmap. |
466 SkBitmap src; | 463 SkBitmap src; |
467 src.setConfig(SkBitmap::kA8_Config, 16, 34); | 464 src.allocPixels(SkImageInfo::MakeA8(16, 34)); |
468 src.allocPixels(); | |
469 | 465 |
470 // Scale it, don't die. | 466 // Scale it, don't die. |
471 SkBitmap full_results = skia::ImageOperations::Resize( | 467 SkBitmap full_results = skia::ImageOperations::Resize( |
472 src, skia::ImageOperations::RESIZE_BOX, 10, 20); | 468 src, skia::ImageOperations::RESIZE_BOX, 10, 20); |
473 } | 469 } |
474 | 470 |
475 // Resamples an image to the same image, it should give the same result. | 471 // Resamples an image to the same image, it should give the same result. |
476 TEST(ImageOperations, ResampleToSameHamming1) { | 472 TEST(ImageOperations, ResampleToSameHamming1) { |
477 CheckResampleToSame(skia::ImageOperations::RESIZE_HAMMING1); | 473 CheckResampleToSame(skia::ImageOperations::RESIZE_HAMMING1); |
478 } | 474 } |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 if (fabs(offset) >= 3) return 0.0; | 656 if (fabs(offset) >= 3) return 0.0; |
661 return sinc(offset) * sinc(offset / 3.0); | 657 return sinc(offset) * sinc(offset / 3.0); |
662 } | 658 } |
663 | 659 |
664 TEST(ImageOperations, ScaleUp) { | 660 TEST(ImageOperations, ScaleUp) { |
665 const int src_w = 3; | 661 const int src_w = 3; |
666 const int src_h = 3; | 662 const int src_h = 3; |
667 const int dst_w = 9; | 663 const int dst_w = 9; |
668 const int dst_h = 9; | 664 const int dst_h = 9; |
669 SkBitmap src; | 665 SkBitmap src; |
670 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 666 src.allocN32Pixels(src_w, src_h); |
671 src.allocPixels(); | |
672 | 667 |
673 for (int src_y = 0; src_y < src_h; ++src_y) { | 668 for (int src_y = 0; src_y < src_h; ++src_y) { |
674 for (int src_x = 0; src_x < src_w; ++src_x) { | 669 for (int src_x = 0; src_x < src_w; ++src_x) { |
675 *src.getAddr32(src_x, src_y) = SkColorSetARGBInline(255, | 670 *src.getAddr32(src_x, src_y) = SkColorSetARGBInline(255, |
676 10 + src_x * 100, | 671 10 + src_x * 100, |
677 10 + src_y * 100, | 672 10 + src_y * 100, |
678 0); | 673 0); |
679 } | 674 } |
680 } | 675 } |
681 | 676 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); | 717 EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); |
723 EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); | 718 EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); |
724 EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); | 719 EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); |
725 EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); | 720 EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); |
726 if (HasFailure()) { | 721 if (HasFailure()) { |
727 return; | 722 return; |
728 } | 723 } |
729 } | 724 } |
730 } | 725 } |
731 } | 726 } |
OLD | NEW |