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 "chrome/browser/thumbnails/content_analysis.h" | 5 #include "chrome/browser/thumbnails/content_analysis.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 class ThumbnailContentAnalysisTest : public testing::Test { | 81 class ThumbnailContentAnalysisTest : public testing::Test { |
82 }; | 82 }; |
83 | 83 |
84 TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnImpulse) { | 84 TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnImpulse) { |
85 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); | 85 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); |
86 | 86 |
87 // The image consists of a point spike on uniform (non-zero) background. | 87 // The image consists of a point spike on uniform (non-zero) background. |
88 canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(10, 10, 10)); | 88 canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(10, 10, 10)); |
89 canvas.FillRect(gfx::Rect(400, 300, 1, 1), SkColorSetRGB(255, 255, 255)); | 89 canvas.FillRect(gfx::Rect(400, 300, 1, 1), SkColorSetRGB(255, 255, 255)); |
90 | 90 |
91 SkBitmap source = canvas.ToBitmap(); | 91 SkBitmap source = canvas.GetBitmap(); |
92 | 92 |
93 SkBitmap reduced_color; | 93 SkBitmap reduced_color; |
94 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), | 94 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), |
95 source.height())); | 95 source.height())); |
96 | 96 |
97 gfx::Vector3dF transform(0.299f, 0.587f, 0.114f); | 97 gfx::Vector3dF transform(0.299f, 0.587f, 0.114f); |
98 EXPECT_TRUE(color_utils::ApplyColorReduction( | 98 EXPECT_TRUE(color_utils::ApplyColorReduction( |
99 source, transform, true, &reduced_color)); | 99 source, transform, true, &reduced_color)); |
100 | 100 |
101 float sigma = 2.5f; | 101 float sigma = 2.5f; |
(...skipping 22 matching lines...) Expand all Loading... |
124 } | 124 } |
125 | 125 |
126 TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnFrame) { | 126 TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnFrame) { |
127 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); | 127 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); |
128 | 128 |
129 // The image consists of a single white block in the centre. | 129 // The image consists of a single white block in the centre. |
130 gfx::Rect draw_rect(300, 200, 200, 200); | 130 gfx::Rect draw_rect(300, 200, 200, 200); |
131 canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(0, 0, 0)); | 131 canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(0, 0, 0)); |
132 canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255)); | 132 canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255)); |
133 | 133 |
134 SkBitmap source = canvas.ToBitmap(); | 134 SkBitmap source = canvas.GetBitmap(); |
135 | 135 |
136 SkBitmap reduced_color; | 136 SkBitmap reduced_color; |
137 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), | 137 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), |
138 source.height())); | 138 source.height())); |
139 | 139 |
140 gfx::Vector3dF transform(0.299f, 0.587f, 0.114f); | 140 gfx::Vector3dF transform(0.299f, 0.587f, 0.114f); |
141 EXPECT_TRUE(color_utils::ApplyColorReduction( | 141 EXPECT_TRUE(color_utils::ApplyColorReduction( |
142 source, transform, true, &reduced_color)); | 142 source, transform, true, &reduced_color)); |
143 | 143 |
144 float sigma = 2.5f; | 144 float sigma = 2.5f; |
(...skipping 17 matching lines...) Expand all Loading... |
162 | 162 |
163 TEST_F(ThumbnailContentAnalysisTest, ExtractImageProfileInformation) { | 163 TEST_F(ThumbnailContentAnalysisTest, ExtractImageProfileInformation) { |
164 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); | 164 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); |
165 | 165 |
166 // The image consists of a white frame drawn in the centre. | 166 // The image consists of a white frame drawn in the centre. |
167 gfx::Rect draw_rect(100, 100, 200, 100); | 167 gfx::Rect draw_rect(100, 100, 200, 100); |
168 gfx::Rect image_rect(0, 0, 800, 600); | 168 gfx::Rect image_rect(0, 0, 800, 600); |
169 canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0)); | 169 canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0)); |
170 canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255)); | 170 canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255)); |
171 | 171 |
172 SkBitmap source = canvas.ToBitmap(); | 172 SkBitmap source = canvas.GetBitmap(); |
173 SkBitmap reduced_color; | 173 SkBitmap reduced_color; |
174 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), | 174 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), |
175 source.height())); | 175 source.height())); |
176 | 176 |
177 gfx::Vector3dF transform(1, 0, 0); | 177 gfx::Vector3dF transform(1, 0, 0); |
178 EXPECT_TRUE(color_utils::ApplyColorReduction( | 178 EXPECT_TRUE(color_utils::ApplyColorReduction( |
179 source, transform, true, &reduced_color)); | 179 source, transform, true, &reduced_color)); |
180 std::vector<float> column_profile; | 180 std::vector<float> column_profile; |
181 std::vector<float> row_profile; | 181 std::vector<float> row_profile; |
182 ExtractImageProfileInformation(reduced_color, | 182 ExtractImageProfileInformation(reduced_color, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ExtractImageProfileInformationWithClosing) { | 225 ExtractImageProfileInformationWithClosing) { |
226 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); | 226 gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true); |
227 | 227 |
228 // The image consists of a two white frames drawn side by side, with a | 228 // The image consists of a two white frames drawn side by side, with a |
229 // single-pixel vertical gap in between. | 229 // single-pixel vertical gap in between. |
230 gfx::Rect image_rect(0, 0, 800, 600); | 230 gfx::Rect image_rect(0, 0, 800, 600); |
231 canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0)); | 231 canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0)); |
232 canvas.DrawRect(gfx::Rect(300, 250, 99, 100), SkColorSetRGB(255, 255, 255)); | 232 canvas.DrawRect(gfx::Rect(300, 250, 99, 100), SkColorSetRGB(255, 255, 255)); |
233 canvas.DrawRect(gfx::Rect(401, 250, 99, 100), SkColorSetRGB(255, 255, 255)); | 233 canvas.DrawRect(gfx::Rect(401, 250, 99, 100), SkColorSetRGB(255, 255, 255)); |
234 | 234 |
235 SkBitmap source = canvas.ToBitmap(); | 235 SkBitmap source = canvas.GetBitmap(); |
236 SkBitmap reduced_color; | 236 SkBitmap reduced_color; |
237 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), | 237 reduced_color.allocPixels(SkImageInfo::MakeA8(source.width(), |
238 source.height())); | 238 source.height())); |
239 | 239 |
240 gfx::Vector3dF transform(1, 0, 0); | 240 gfx::Vector3dF transform(1, 0, 0); |
241 EXPECT_TRUE(color_utils::ApplyColorReduction( | 241 EXPECT_TRUE(color_utils::ApplyColorReduction( |
242 source, transform, true, &reduced_color)); | 242 source, transform, true, &reduced_color)); |
243 std::vector<float> column_profile; | 243 std::vector<float> column_profile; |
244 std::vector<float> row_profile; | 244 std::vector<float> row_profile; |
245 | 245 |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 | 547 |
548 std::vector<bool> rows(image_size.height(), false); | 548 std::vector<bool> rows(image_size.height(), false); |
549 std::fill_n(rows.begin() + 200, 100, true); | 549 std::fill_n(rows.begin() + 200, 100, true); |
550 std::fill_n(rows.begin() + 400, 100, true); | 550 std::fill_n(rows.begin() + 400, 100, true); |
551 | 551 |
552 std::vector<bool> columns(image_size.width(), false); | 552 std::vector<bool> columns(image_size.width(), false); |
553 std::fill_n(columns.begin() + 100, 100, true); | 553 std::fill_n(columns.begin() + 100, 100, true); |
554 std::fill_n(columns.begin() + 300, 100, true); | 554 std::fill_n(columns.begin() + 300, 100, true); |
555 std::fill_n(columns.begin() + 500, 100, true); | 555 std::fill_n(columns.begin() + 500, 100, true); |
556 | 556 |
557 SkBitmap source = canvas.ToBitmap(); | 557 SkBitmap source = canvas.GetBitmap(); |
558 SkBitmap result = ComputeDecimatedImage(source, rows, columns); | 558 SkBitmap result = ComputeDecimatedImage(source, rows, columns); |
559 EXPECT_FALSE(result.empty()); | 559 EXPECT_FALSE(result.empty()); |
560 EXPECT_EQ(300, result.width()); | 560 EXPECT_EQ(300, result.width()); |
561 EXPECT_EQ(200, result.height()); | 561 EXPECT_EQ(200, result.height()); |
562 | 562 |
563 // The call should have removed all empty spaces. | 563 // The call should have removed all empty spaces. |
564 ASSERT_TRUE(CompareImageFragments(source, | 564 ASSERT_TRUE(CompareImageFragments(source, |
565 result, | 565 result, |
566 gfx::Size(100, 100), | 566 gfx::Size(100, 100), |
567 gfx::Point(100, 200), | 567 gfx::Point(100, 200), |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 body_rect.height() / 3 * line + margin_vertical; | 659 body_rect.height() / 3 * line + margin_vertical; |
660 const int x = body_rect.x() + | 660 const int x = body_rect.x() + |
661 alignment * body_rect.width() / 2 + margin_vertical; | 661 alignment * body_rect.width() / 2 + margin_vertical; |
662 gfx::Rect pict_rect(x, y, | 662 gfx::Rect pict_rect(x, y, |
663 body_rect.width() / 2 - 2 * margin_vertical, | 663 body_rect.width() / 2 - 2 * margin_vertical, |
664 body_rect.height() / 3 - 2 * margin_vertical); | 664 body_rect.height() / 3 - 2 * margin_vertical); |
665 canvas.FillRect(pict_rect, SkColorSetRGB(255, 255, 255)); | 665 canvas.FillRect(pict_rect, SkColorSetRGB(255, 255, 255)); |
666 canvas.DrawRect(pict_rect, SkColorSetRGB(0, 0, 0)); | 666 canvas.DrawRect(pict_rect, SkColorSetRGB(0, 0, 0)); |
667 } | 667 } |
668 | 668 |
669 SkBitmap source = canvas.ToBitmap(); | 669 SkBitmap source = canvas.GetBitmap(); |
670 | 670 |
671 SkBitmap result = CreateRetargetedThumbnailImage( | 671 SkBitmap result = CreateRetargetedThumbnailImage( |
672 source, gfx::Size(424, 264), 2.5); | 672 source, gfx::Size(424, 264), 2.5); |
673 EXPECT_FALSE(result.empty()); | 673 EXPECT_FALSE(result.empty()); |
674 | 674 |
675 // Given the nature of computation We can't really assert much here about the | 675 // Given the nature of computation We can't really assert much here about the |
676 // image itself. We know it should have been computed, should be smaller than | 676 // image itself. We know it should have been computed, should be smaller than |
677 // the original and it must not be zero. | 677 // the original and it must not be zero. |
678 EXPECT_LT(result.width(), image_size.width()); | 678 EXPECT_LT(result.width(), image_size.width()); |
679 EXPECT_LT(result.height(), image_size.height()); | 679 EXPECT_LT(result.height(), image_size.height()); |
680 | 680 |
681 int histogram[256] = {}; | 681 int histogram[256] = {}; |
682 color_utils::BuildLumaHistogram(result, histogram); | 682 color_utils::BuildLumaHistogram(result, histogram); |
683 int non_zero_color_count = std::count_if( | 683 int non_zero_color_count = std::count_if( |
684 histogram, histogram + 256, [](int value) { return value > 0; }); | 684 histogram, histogram + 256, [](int value) { return value > 0; }); |
685 EXPECT_GT(non_zero_color_count, 4); | 685 EXPECT_GT(non_zero_color_count, 4); |
686 | 686 |
687 } | 687 } |
688 | 688 |
689 } // namespace thumbnailing_utils | 689 } // namespace thumbnailing_utils |
OLD | NEW |