1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_THUMBNAILS_CONTENT_ANALYSIS_H_
6#define CHROME_BROWSER_THUMBNAILS_CONTENT_ANALYSIS_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "third_party/skia/include/core/SkColor.h"
12#include "ui/gfx/rect.h"
13#include "ui/gfx/size.h"
14
15class SkBitmap;
16
17namespace thumbnailing_utils {
18
19// Compute in-place gaussian gradient magnitude of |input_bitmap| with sigma
20// |kernel_sigma|. |input_bitmap| is requried to be of SkBitmap::kA8_Config
21// type. The routine computes first-order gaussian derivative on a
22// gaussian-smoothed image. Beware, this is fairly slow since kernel size is
23// 4 * kernel_sigma + 1.
24void ApplyGaussianGradientMagnitudeFilter(SkBitmap* input_bitmap,
25                                          float kernel_sigma);
26
27// Accumulates vertical and horizontal sum of pixel values from a subsection of
28// |input_bitmap| defined by |image_area|. The image is required to be of
29// SkBitmap::kA8_Config type.
30// If non-empty |target_size| is given, the routine will use it to process the
31// profiles with closing operator sized to eliminate gaps which would be smaller
32// than 1 pixel after rescaling to |target_size|.
33// If |apply_log| is true, logarithm of counts are used for morhology (and
34// returned).
35void ExtractImageProfileInformation(const SkBitmap& input_bitmap,
36                                    const gfx::Rect& image_area,
37                                    const gfx::Size& target_size,
38                                    bool apply_log,
39                                    std::vector<float>* rows,
40                                    std::vector<float>* columns);
41
42// Compute a threshold value separating background (low) from signal (high)
43// areas in the |input| profile.
44float AutoSegmentPeaks(const std::vector<float>& input);
45
46// Compute and return a workable (not too distorted, not bigger than the image)
47// target size for retargeting in ConstrainedProfileSegmentation. |target_size|
48// is the desired image size (defines aspect ratio and minimal image size) while
49// |computed_size| is the size of a result of unconstrained segmentation.
50// This routine makes very little sense outside ConstrainedProfileSegmentation
51// and is exposed only for unit tests (it is somehow complicated).
52gfx::Size AdjustClippingSizeToAspectRatio(const gfx::Size& target_size,
53                                          const gfx::Size& image_size,
54                                          const gfx::Size& computed_size);
55
56// Compute thresholding guides |included_rows| and |included_columns| by
57// segmenting 1-d profiles |row_profile| and |column_profile|. The routine will
58// attempt to keep the image which would result from using these guides as close
59// to the desired aspect ratio (given by |target_size|) as reasonable.
60void ConstrainedProfileSegmentation(const std::vector<float>& row_profile,
61                                    const std::vector<float>& column_profile,
62                                    const gfx::Size& target_size,
63                                    std::vector<bool>* included_rows,
64                                    std::vector<bool>* included_columns);
65
66// Shrinks the source |bitmap| by removing rows and columns where |rows| and
67// |columns| are false, respectively. The function returns a new bitmap if the
68// shrinking can be performed and an empty instance otherwise.
69SkBitmap ComputeDecimatedImage(const SkBitmap& bitmap,
70                               const std::vector<bool>& rows,
71                               const std::vector<bool>& columns);
72
73// Creates a new bitmap which contains only 'interesting' areas of
74// |source_bitmap|. The |target_size| is used to estimate some computation
75// parameters, but the resulting bitmap will not necessarily be of that size.
76// |kernel_sigma| defines the degree of image smoothing in gradient computation.
77// For a natural-sized (not shrunk) screenshot at 96 DPI and regular font size
78// 5.0 was determined to be a good value.
79SkBitmap CreateRetargetedThumbnailImage(const SkBitmap& source_bitmap,
80                                        const gfx::Size& target_size,
81                                        float kernel_sigma);
82
83}  // namespace thumbnailing_utils
84
85#endif  // CHROME_BROWSER_THUMBNAILS_CONTENT_ANALYSIS_H_
86