1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/thumbnails/content_analysis.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <limits>
#include <numeric>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
namespace {
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
unsigned long ImagePixelSum(const SkBitmap& bitmap, const gfx::Rect& rect) {
// Get the sum of pixel values in the rectangle. Applicable only to
// monochrome bitmaps.
DCHECK_EQ(SkBitmap::kA8_Config, bitmap.config());
unsigned long total = 0;
for (int r = rect.y(); r < rect.bottom(); ++r) {
const uint8* row_data = static_cast<const uint8*>(
bitmap.getPixels()) + r * bitmap.rowBytes();
for (int c = rect.x(); c < rect.right(); ++c)
total += row_data[c];
}
return total;
}
bool CompareImageFragments(const SkBitmap& bitmap_left,
const SkBitmap& bitmap_right,
const gfx::Size& comparison_area,
const gfx::Point& origin_left,
const gfx::Point& origin_right) {
SkAutoLockPixels left_lock(bitmap_left);
SkAutoLockPixels right_lock(bitmap_right);
for (int r = 0; r < comparison_area.height(); ++r) {
for (int c = 0; c < comparison_area.width(); ++c) {
SkColor color_left = bitmap_left.getColor(origin_left.x() + c,
origin_left.y() + r);
SkColor color_right = bitmap_right.getColor(origin_right.x() + c,
origin_right.y() + r);
if (color_left != color_right)
return false;
}
}
return true;
}
float AspectDifference(const gfx::Size& reference, const gfx::Size& candidate) {
return std::abs(static_cast<float>(candidate.width()) / candidate.height() -
static_cast<float>(reference.width()) / reference.height());
}
} // namespace
namespace thumbnailing_utils {
class ThumbnailContentAnalysisTest : public testing::Test {
};
TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnImpulse) {
gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true);
// The image consists of a point spike on uniform (non-zero) background.
canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(10, 10, 10));
canvas.FillRect(gfx::Rect(400, 300, 1, 1), SkColorSetRGB(255, 255, 255));
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap reduced_color;
reduced_color.setConfig(
SkBitmap::kA8_Config, source.width(), source.height());
reduced_color.allocPixels();
gfx::Vector3dF transform(0.299f, 0.587f, 0.114f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &reduced_color));
float sigma = 2.5f;
ApplyGaussianGradientMagnitudeFilter(&reduced_color, sigma);
// Expect everything to be within 8 * sigma.
int tail_length = static_cast<int>(8.0f * sigma + 0.5f);
gfx::Rect echo_rect(399 - tail_length, 299 - tail_length,
2 * tail_length + 1, 2 * tail_length + 1);
unsigned long data_sum = ImagePixelSum(reduced_color, echo_rect);
unsigned long all_sum = ImagePixelSum(reduced_color, gfx::Rect(800, 600));
EXPECT_GT(data_sum, 0U);
EXPECT_EQ(data_sum, all_sum);
sigma = 5.0f;
ApplyGaussianGradientMagnitudeFilter(&reduced_color, sigma);
// Expect everything to be within 8 * sigma.
tail_length = static_cast<int>(8.0f * sigma + 0.5f);
echo_rect = gfx::Rect(399 - tail_length, 299 - tail_length,
2 * tail_length + 1, 2 * tail_length + 1);
data_sum = ImagePixelSum(reduced_color, echo_rect);
all_sum = ImagePixelSum(reduced_color, gfx::Rect(800, 600));
EXPECT_GT(data_sum, 0U);
EXPECT_EQ(data_sum, all_sum);
}
TEST_F(ThumbnailContentAnalysisTest, ApplyGradientMagnitudeOnFrame) {
gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true);
// The image consists of a single white block in the centre.
gfx::Rect draw_rect(300, 200, 200, 200);
canvas.FillRect(gfx::Rect(0, 0, 800, 600), SkColorSetRGB(0, 0, 0));
canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255));
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap reduced_color;
reduced_color.setConfig(
SkBitmap::kA8_Config, source.width(), source.height());
reduced_color.allocPixels();
gfx::Vector3dF transform(0.299f, 0.587f, 0.114f);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &reduced_color));
float sigma = 2.5f;
ApplyGaussianGradientMagnitudeFilter(&reduced_color, sigma);
int tail_length = static_cast<int>(8.0f * sigma + 0.5f);
gfx::Rect outer_rect(draw_rect.x() - tail_length,
draw_rect.y() - tail_length,
draw_rect.width() + 2 * tail_length,
draw_rect.height() + 2 * tail_length);
gfx::Rect inner_rect(draw_rect.x() + tail_length,
draw_rect.y() + tail_length,
draw_rect.width() - 2 * tail_length,
draw_rect.height() - 2 * tail_length);
unsigned long data_sum = ImagePixelSum(reduced_color, outer_rect);
unsigned long all_sum = ImagePixelSum(reduced_color, gfx::Rect(800, 600));
EXPECT_GT(data_sum, 0U);
EXPECT_EQ(data_sum, all_sum);
EXPECT_EQ(ImagePixelSum(reduced_color, inner_rect), 0U);
}
TEST_F(ThumbnailContentAnalysisTest, ExtractImageProfileInformation) {
gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true);
// The image consists of a white frame drawn in the centre.
gfx::Rect draw_rect(100, 100, 200, 100);
gfx::Rect image_rect(0, 0, 800, 600);
canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0));
canvas.DrawRect(draw_rect, SkColorSetRGB(255, 255, 255));
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap reduced_color;
reduced_color.setConfig(
SkBitmap::kA8_Config, source.width(), source.height());
reduced_color.allocPixels();
gfx::Vector3dF transform(1, 0, 0);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &reduced_color));
std::vector<float> column_profile;
std::vector<float> row_profile;
ExtractImageProfileInformation(reduced_color,
image_rect,
gfx::Size(),
false,
&row_profile,
&column_profile);
EXPECT_EQ(0, std::accumulate(column_profile.begin(),
column_profile.begin() + draw_rect.x() - 1,
0));
EXPECT_EQ(column_profile[draw_rect.x()], 255U * (draw_rect.height() + 1));
EXPECT_EQ(2 * 255 * (draw_rect.width() - 2),
std::accumulate(column_profile.begin() + draw_rect.x() + 1,
column_profile.begin() + draw_rect.right() - 1,
0));
EXPECT_EQ(0, std::accumulate(row_profile.begin(),
row_profile.begin() + draw_rect.y() - 1,
0));
EXPECT_EQ(row_profile[draw_rect.y()], 255U * (draw_rect.width() + 1));
EXPECT_EQ(2 * 255 * (draw_rect.height() - 2),
std::accumulate(row_profile.begin() + draw_rect.y() + 1,
row_profile.begin() + draw_rect.bottom() - 1,
0));
gfx::Rect test_rect(150, 80, 400, 100);
ExtractImageProfileInformation(reduced_color,
test_rect,
gfx::Size(),
false,
&row_profile,
&column_profile);
// Some overlap with the drawn rectagle. If you work it out on a piece of
// paper, sums should be as follows.
EXPECT_EQ(255 * (test_rect.bottom() - draw_rect.y()) +
255 * (draw_rect.right() - test_rect.x()),
std::accumulate(row_profile.begin(), row_profile.end(), 0));
EXPECT_EQ(255 * (test_rect.bottom() - draw_rect.y()) +
255 * (draw_rect.right() - test_rect.x()),
std::accumulate(column_profile.begin(), column_profile.end(), 0));
}
TEST_F(ThumbnailContentAnalysisTest,
ExtractImageProfileInformationWithClosing) {
gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true);
// The image consists of a two white frames drawn side by side, with a
// single-pixel vertical gap in between.
gfx::Rect image_rect(0, 0, 800, 600);
canvas.FillRect(image_rect, SkColorSetRGB(0, 0, 0));
canvas.DrawRect(gfx::Rect(300, 250, 99, 100), SkColorSetRGB(255, 255, 255));
canvas.DrawRect(gfx::Rect(401, 250, 99, 100), SkColorSetRGB(255, 255, 255));
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap reduced_color;
reduced_color.setConfig(
SkBitmap::kA8_Config, source.width(), source.height());
reduced_color.allocPixels();
gfx::Vector3dF transform(1, 0, 0);
EXPECT_TRUE(color_utils::ApplyColorReduction(
source, transform, true, &reduced_color));
std::vector<float> column_profile;
std::vector<float> row_profile;
ExtractImageProfileInformation(reduced_color,
image_rect,
gfx::Size(),
true,
&row_profile,
&column_profile);
// Column profiles should have two spikes in the middle, with a single
// 0-valued value between them.
EXPECT_GT(column_profile[398], 0.0f);
EXPECT_GT(column_profile[399], column_profile[398]);
EXPECT_GT(column_profile[402], 0.0f);
EXPECT_GT(column_profile[401], column_profile[402]);
EXPECT_EQ(column_profile[401], column_profile[399]);
EXPECT_EQ(column_profile[402], column_profile[398]);
EXPECT_EQ(column_profile[400], 0.0f);
EXPECT_EQ(column_profile[299], 0.0f);
EXPECT_EQ(column_profile[502], 0.0f);
// Now the same with closing applied. The space in the middle will be closed.
ExtractImageProfileInformation(reduced_color,
image_rect,
gfx::Size(200, 100),
true,
&row_profile,
&column_profile);
EXPECT_GT(column_profile[398], 0);
EXPECT_GT(column_profile[400], 0);
EXPECT_GT(column_profile[402], 0);
EXPECT_EQ(column_profile[299], 0);
EXPECT_EQ(column_profile[502], 0);
EXPECT_EQ(column_profile[399], column_profile[401]);
EXPECT_EQ(column_profile[398], column_profile[402]);
}
TEST_F(ThumbnailContentAnalysisTest, AdjustClippingSizeToAspectRatio) {
// The test will exercise several relations of sizes. Basic invariants
// checked in each case: each dimension in adjusted_size ougth not be greater
// than the source image and not lesser than requested target. Aspect ratio
// of adjusted_size should never be worse than that of computed_size.
gfx::Size target_size(212, 100);
gfx::Size image_size(1000, 2000);
gfx::Size computed_size(420, 200);
gfx::Size adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// This case is special (and trivial): no change expected.
EXPECT_EQ(computed_size, adjusted_size);
// Computed size is too tall. Adjusted size has to add rows.
computed_size.SetSize(600, 150);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_EQ(computed_size.width(), adjusted_size.width());
EXPECT_LE(computed_size.height(), adjusted_size.height());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
// Computed size is too wide. Adjusted size has to add columns.
computed_size.SetSize(200, 400);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
target_size.SetSize(416, 205);
image_size.SetSize(1200, 1200);
computed_size.SetSize(900, 300);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_EQ(computed_size.width(), adjusted_size.width());
EXPECT_LE(computed_size.height(), adjusted_size.height());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
target_size.SetSize(416, 205);
image_size.SetSize(1200, 1200);
computed_size.SetSize(300, 300);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_EQ(computed_size.height(), adjusted_size.height());
EXPECT_LE(computed_size.width(), adjusted_size.width());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
computed_size.SetSize(200, 300);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_EQ(computed_size.height(), adjusted_size.height());
EXPECT_LE(computed_size.width(), adjusted_size.width());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
target_size.SetSize(416, 205);
image_size.SetSize(1400, 600);
computed_size.SetSize(300, 300);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_EQ(computed_size.height(), adjusted_size.height());
EXPECT_LE(computed_size.width(), adjusted_size.width());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
computed_size.SetSize(900, 300);
adjusted_size = AdjustClippingSizeToAspectRatio(
target_size, image_size, computed_size);
// Invariant check.
EXPECT_LE(adjusted_size.width(), image_size.width());
EXPECT_LE(adjusted_size.height(), image_size.height());
EXPECT_GE(adjusted_size.width(), target_size.width());
EXPECT_GE(adjusted_size.height(), target_size.height());
EXPECT_LE(AspectDifference(target_size, adjusted_size),
AspectDifference(target_size, computed_size));
// Specific to this case.
EXPECT_LE(computed_size.height(), adjusted_size.height());
EXPECT_NEAR(
static_cast<float>(target_size.width()) / target_size.height(),
static_cast<float>(adjusted_size.width()) / adjusted_size.height(),
0.02f);
}
TEST_F(ThumbnailContentAnalysisTest, AutoSegmentPeaks) {
std::vector<float> profile_info;
EXPECT_EQ(AutoSegmentPeaks(profile_info), std::numeric_limits<float>::max());
profile_info.resize(1000, 1.0f);
EXPECT_EQ(AutoSegmentPeaks(profile_info), 1.0f);
std::srand(42);
std::generate(profile_info.begin(), profile_info.end(), std::rand);
float threshold = AutoSegmentPeaks(profile_info);
EXPECT_GT(threshold, 0); // Not much to expect.
// There should be roughly 50% above and below the threshold.
// Random is not really random thanks to srand, so we can sort-of compare.
int above_count = std::count_if(
profile_info.begin(),
profile_info.end(),
std::bind2nd(std::greater<float>(), threshold));
EXPECT_GT(above_count, 450); // Not much to expect.
EXPECT_LT(above_count, 550);
for (unsigned i = 0; i < profile_info.size(); ++i) {
float y = std::sin(M_PI * i / 250.0f);
profile_info[i] = y > 0 ? y : 0;
}
threshold = AutoSegmentPeaks(profile_info);
above_count = std::count_if(
profile_info.begin(),
profile_info.end(),
std::bind2nd(std::greater<float>(), threshold));
EXPECT_LT(above_count, 500); // Negative y expected to fall below threshold.
// Expect two peaks around between 0 and 250 and 500 and 750.
std::vector<bool> thresholded_values(profile_info.size(), false);
std::transform(profile_info.begin(),
profile_info.end(),
thresholded_values.begin(),
std::bind2nd(std::greater<float>(), threshold));
EXPECT_TRUE(thresholded_values[125]);
EXPECT_TRUE(thresholded_values[625]);
int transitions = 0;
for (unsigned i = 1; i < thresholded_values.size(); ++i) {
if (thresholded_values[i] != thresholded_values[i-1])
transitions++;
}
EXPECT_EQ(transitions, 4); // We have two contiguous peaks. Good going!
}
TEST_F(ThumbnailContentAnalysisTest, ConstrainedProfileSegmentation) {
const size_t kRowCount = 800;
const size_t kColumnCount = 1400;
const gfx::Size target_size(300, 150);
std::vector<float> rows_profile(kRowCount);
std::vector<float> columns_profile(kColumnCount);
std::srand(42);
std::generate(rows_profile.begin(), rows_profile.end(), std::rand);
std::generate(columns_profile.begin(), columns_profile.end(), std::rand);
// Bring noise level to 0-1.
std::transform(rows_profile.begin(),
rows_profile.end(),
rows_profile.begin(),
std::bind2nd(std::divides<float>(), RAND_MAX));
std::transform(columns_profile.begin(),
columns_profile.end(),
columns_profile.begin(),
std::bind2nd(std::divides<float>(), RAND_MAX));
// Set up values to 0-1.
std::transform(rows_profile.begin(),
rows_profile.end(),
rows_profile.begin(),
std::bind2nd(std::plus<float>(), 1.0f));
std::transform(columns_profile.begin(),
columns_profile.end(),
columns_profile.begin(),
std::bind2nd(std::plus<float>(), 1.0f));
std::transform(rows_profile.begin() + 300,
rows_profile.begin() + 450,
rows_profile.begin() + 300,
std::bind2nd(std::plus<float>(), 8.0f));
std::transform(columns_profile.begin() + 400,
columns_profile.begin() + 1000,
columns_profile.begin() + 400,
std::bind2nd(std::plus<float>(), 10.0f));
// Make sure that threshold falls somewhere reasonable.
float row_threshold = AutoSegmentPeaks(rows_profile);
EXPECT_GT(row_threshold, 1.0f);
EXPECT_LT(row_threshold, 9.0f);
int row_above_count = std::count_if(
rows_profile.begin(),
rows_profile.end(),
std::bind2nd(std::greater<float>(), row_threshold));
EXPECT_EQ(row_above_count, 150);
float column_threshold = AutoSegmentPeaks(columns_profile);
EXPECT_GT(column_threshold, 1.0f);
EXPECT_LT(column_threshold, 11.0f);
int column_above_count = std::count_if(
columns_profile.begin(),
columns_profile.end(),
std::bind2nd(std::greater<float>(), column_threshold));
EXPECT_EQ(column_above_count, 600);
std::vector<bool> rows_guide;
std::vector<bool> columns_guide;
ConstrainedProfileSegmentation(
rows_profile, columns_profile, target_size, &rows_guide, &columns_guide);
int row_count = std::count(rows_guide.begin(), rows_guide.end(), true);
int column_count = std::count(
columns_guide.begin(), columns_guide.end(), true);
float expected_aspect =
static_cast<float>(target_size.width()) / target_size.height();
float actual_aspect = static_cast<float>(column_count) / row_count;
EXPECT_GE(1.05f, expected_aspect / actual_aspect);
EXPECT_GE(1.05f, actual_aspect / expected_aspect);
}
TEST_F(ThumbnailContentAnalysisTest, ComputeDecimatedImage) {
gfx::Size image_size(1600, 1200);
gfx::Canvas canvas(image_size, 1.0f, true);
// Make some content we will later want to keep.
canvas.FillRect(gfx::Rect(100, 200, 100, 100), SkColorSetRGB(125, 0, 0));
canvas.FillRect(gfx::Rect(300, 200, 100, 100), SkColorSetRGB(0, 200, 0));
canvas.FillRect(gfx::Rect(500, 200, 100, 100), SkColorSetRGB(0, 0, 225));
canvas.FillRect(gfx::Rect(100, 400, 600, 100), SkColorSetRGB(125, 200, 225));
std::vector<bool> rows(image_size.height(), false);
std::fill_n(rows.begin() + 200, 100, true);
std::fill_n(rows.begin() + 400, 100, true);
std::vector<bool> columns(image_size.width(), false);
std::fill_n(columns.begin() + 100, 100, true);
std::fill_n(columns.begin() + 300, 100, true);
std::fill_n(columns.begin() + 500, 100, true);
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap result = ComputeDecimatedImage(source, rows, columns);
EXPECT_FALSE(result.empty());
EXPECT_EQ(300, result.width());
EXPECT_EQ(200, result.height());
// The call should have removed all empty spaces.
ASSERT_TRUE(CompareImageFragments(source,
result,
gfx::Size(100, 100),
gfx::Point(100, 200),
gfx::Point(0, 0)));
ASSERT_TRUE(CompareImageFragments(source,
result,
gfx::Size(100, 100),
gfx::Point(300, 200),
gfx::Point(100, 0)));
ASSERT_TRUE(CompareImageFragments(source,
result,
gfx::Size(100, 100),
gfx::Point(500, 200),
gfx::Point(200, 0)));
ASSERT_TRUE(CompareImageFragments(source,
result,
gfx::Size(100, 100),
gfx::Point(100, 400),
gfx::Point(0, 100)));
}
TEST_F(ThumbnailContentAnalysisTest, CreateRetargetedThumbnailImage) {
gfx::Size image_size(1200, 1300);
gfx::Canvas canvas(image_size, 1.0f, true);
// The following will create a 'fake image' consisting of color blocks placed
// on a neutral background. The entire layout is supposed to mimic a
// screenshot of a web page.
// The tested function is supposed to locate the interesing areas in the
// middle.
const int margin_horizontal = 60;
const int margin_vertical = 20;
canvas.FillRect(gfx::Rect(image_size), SkColorSetRGB(200, 210, 210));
const gfx::Rect header_rect(margin_horizontal,
margin_vertical,
image_size.width() - 2 * margin_horizontal,
100);
const gfx::Rect footer_rect(margin_horizontal,
image_size.height() - margin_vertical - 100,
image_size.width() - 2 * margin_horizontal,
100);
const gfx::Rect body_rect(margin_horizontal,
header_rect.bottom() + margin_vertical,
image_size.width() - 2 * margin_horizontal,
footer_rect.y() - header_rect.bottom() -
2 * margin_vertical);
canvas.FillRect(header_rect, SkColorSetRGB(200, 40, 10));
canvas.FillRect(footer_rect, SkColorSetRGB(10, 40, 180));
canvas.FillRect(body_rect, SkColorSetRGB(150, 180, 40));
// 'Fine print' at the bottom.
const int fine_print = 8;
const SkColor print_color = SkColorSetRGB(45, 30, 30);
for (int y = footer_rect.y() + fine_print;
y < footer_rect.bottom() - fine_print;
y += 2 * fine_print) {
for (int x = footer_rect.x() + fine_print;
x < footer_rect.right() - fine_print;
x += 2 * fine_print) {
canvas.DrawRect(gfx::Rect(x, y, fine_print, fine_print), print_color);
}
}
// Blocky content at the top.
const int block_size = header_rect.height() - margin_vertical;
for (int x = header_rect.x() + margin_horizontal;
x < header_rect.right() - block_size;
x += block_size + margin_horizontal) {
const int half_block = block_size / 2 - 5;
const SkColor block_color = SkColorSetRGB(255, 255, 255);
const int y = header_rect.y() + margin_vertical / 2;
int second_col = x + half_block + 10;
int second_row = y + half_block + 10;
canvas.FillRect(gfx::Rect(x, y, half_block, block_size), block_color);
canvas.FillRect(gfx::Rect(second_col, y, half_block, half_block),
block_color);
canvas.FillRect(gfx::Rect(second_col, second_row, half_block, half_block),
block_color);
}
// Now the main body. Mostly text with some 'pictures'.
for (int y = body_rect.y() + fine_print;
y < body_rect.bottom() - fine_print;
y += 2 * fine_print) {
for (int x = body_rect.x() + fine_print;
x < body_rect.right() - fine_print;
x += 2 * fine_print) {
canvas.DrawRect(gfx::Rect(x, y, fine_print, fine_print), print_color);
}
}
for (int line = 0; line < 3; ++line) {
int alignment = line % 2;
const int y = body_rect.y() +
body_rect.height() / 3 * line + margin_vertical;
const int x = body_rect.x() +
alignment * body_rect.width() / 2 + margin_vertical;
gfx::Rect pict_rect(x, y,
body_rect.width() / 2 - 2 * margin_vertical,
body_rect.height() / 3 - 2 * margin_vertical);
canvas.FillRect(pict_rect, SkColorSetRGB(255, 255, 255));
canvas.DrawRect(pict_rect, SkColorSetRGB(0, 0, 0));
}
SkBitmap source =
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
SkBitmap result = CreateRetargetedThumbnailImage(
source, gfx::Size(424, 264), 2.5);
EXPECT_FALSE(result.empty());
// Given the nature of computation We can't really assert much here about the
// image itself. We know it should have been computed, should be smaller than
// the original and it must not be zero.
EXPECT_LT(result.width(), image_size.width());
EXPECT_LT(result.height(), image_size.height());
int histogram[256] = {};
color_utils::BuildLumaHistogram(result, histogram);
int non_zero_color_count = std::count_if(
histogram, histogram + 256, std::bind2nd(std::greater<int>(), 0));
EXPECT_GT(non_zero_color_count, 4);
}
} // namespace thumbnailing_utils
|