Home | History | Annotate | Download | only in thumbnails
      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_SIMPLE_THUMBNAIL_CROP_H_
      6 #define CHROME_BROWSER_THUMBNAILS_SIMPLE_THUMBNAIL_CROP_H_
      7 
      8 #include "chrome/browser/thumbnails/thumbnailing_algorithm.h"
      9 
     10 namespace thumbnails {
     11 
     12 // The implementation of the 'classic' thumbnail cropping algorithm. It is not
     13 // content-driven in any meaningful way (save for score calculation). Rather,
     14 // the choice of a cropping region is based on relation between source and
     15 // target sizes. The selected source region is then rescaled into the target
     16 // thumbnail image.
     17 class SimpleThumbnailCrop : public ThumbnailingAlgorithm {
     18  public:
     19   explicit SimpleThumbnailCrop(const gfx::Size& target_size);
     20 
     21   virtual ClipResult GetCanvasCopyInfo(const gfx::Size& source_size,
     22                                        ui::ScaleFactor scale_factor,
     23                                        gfx::Rect* clipping_rect,
     24                                        gfx::Size* target_size) const OVERRIDE;
     25 
     26   virtual void ProcessBitmap(scoped_refptr<ThumbnailingContext> context,
     27                              const ConsumerCallback& callback,
     28                              const SkBitmap& bitmap) OVERRIDE;
     29 
     30   // Calculates how "boring" a thumbnail is. The boring score is the
     31   // 0,1 ranged percentage of pixels that are the most common
     32   // luma. Higher boring scores indicate that a higher percentage of a
     33   // bitmap are all the same brightness.
     34   // Statically exposed for use by tests only.
     35   static double CalculateBoringScore(const SkBitmap& bitmap);
     36 
     37   // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
     38   // desired width and the desired height. For instance, if the input
     39   // bitmap is vertically long (ex. 400x900) and the desired size is
     40   // square (ex. 100x100), the clipped bitmap will be the top half of the
     41   // input bitmap (400x400).
     42   // Statically exposed for use by tests only.
     43   static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
     44                                    int desired_width,
     45                                    int desired_height,
     46                                    thumbnails::ClipResult* clip_result);
     47   static gfx::Size GetCopySizeForThumbnail(ui::ScaleFactor scale_factor,
     48                                            const gfx::Size& thumbnail_size);
     49   static gfx::Rect GetClippingRect(const gfx::Size& source_size,
     50                                    const gfx::Size& desired_size,
     51                                    ClipResult* clip_result);
     52 
     53   // Computes the size of a thumbnail that should be stored in the database from
     54   // |given_size| (expected to be the thumbnail size we would normally want to
     55   // see). The returned size is expressed in pixels and is determined by
     56   // bumping the resolution up to the maximum scale factor.
     57   static gfx::Size ComputeTargetSizeAtMaximumScale(const gfx::Size& given_size);
     58 
     59  protected:
     60   virtual ~SimpleThumbnailCrop();
     61 
     62  private:
     63   static SkBitmap CreateThumbnail(const SkBitmap& bitmap,
     64                                   const gfx::Size& desired_size,
     65                                   ClipResult* clip_result);
     66 
     67   const gfx::Size target_size_;
     68 
     69   DISALLOW_COPY_AND_ASSIGN(SimpleThumbnailCrop);
     70 };
     71 
     72 }
     73 
     74 #endif  // CHROME_BROWSER_THUMBNAILS_SIMPLE_THUMBNAIL_CROP_H_
     75