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
3 // found in the LICENSE file.
4 
5 #ifndef UI_GFX_IMAGE_IMAGE_SKIA_OPERATIONS_H_
6 #define UI_GFX_IMAGE_IMAGE_SKIA_OPERATIONS_H_
7 
8 #include "skia/ext/image_operations.h"
9 #include "third_party/skia/include/core/SkDrawLooper.h"
10 #include "third_party/skia/include/core/SkPaint.h"
11 #include "third_party/skia/include/core/SkRRect.h"
12 #include "ui/gfx/color_utils.h"
13 #include "ui/gfx/gfx_export.h"
14 #include "ui/gfx/shadow_value.h"
15 #include "ui/gfx/skbitmap_operations.h"
16 
17 namespace gfx {
18 class ImageSkia;
19 class Rect;
20 class Size;
21 
22 class GFX_EXPORT ImageSkiaOperations {
23  public:
24   // Create an image that is a blend of two others. The alpha argument
25   // specifies the opacity of the second imag. The provided image must
26   // use the kARGB_8888_Config config and be of equal dimensions.
27   static ImageSkia CreateBlendedImage(const ImageSkia& first,
28                                       const ImageSkia& second,
29                                       double alpha);
30 
31   // Creates an image that is the original image with opacity set to |alpha|.
32   static ImageSkia CreateTransparentImage(const ImageSkia& image, double alpha);
33 
34   // Creates new image by painting first and second image respectively.
35   // The second image is centered in respect to the first image.
36   static ImageSkia CreateSuperimposedImage(const ImageSkia& first,
37                                            const ImageSkia& second);
38 
39   // Create an image that is the original image masked out by the mask defined
40   // in the alpha image. The images must use the kARGB_8888_Config config and
41   // be of equal dimensions.
42   static ImageSkia CreateMaskedImage(const ImageSkia& first,
43                                      const ImageSkia& alpha);
44 
45   // Create an image that is cropped from another image. This is special
46   // because it tiles the original image, so your coordinates can extend
47   // outside the bounds of the original image.
48   static ImageSkia CreateTiledImage(const ImageSkia& image,
49                                     int src_x, int src_y,
50                                     int dst_w, int dst_h);
51 
52   // Shift an image's HSL values. The shift values are in the range of 0-1,
53   // with the option to specify -1 for 'no change'. The shift values are
54   // defined as:
55   // hsl_shift[0] (hue): The absolute hue value for the image - 0 and 1 map
56   //    to 0 and 360 on the hue color wheel (red).
57   // hsl_shift[1] (saturation): A saturation shift for the image, with the
58   //    following key values:
59   //    0 = remove all color.
60   //    0.5 = leave unchanged.
61   //    1 = fully saturate the image.
62   // hsl_shift[2] (lightness): A lightness shift for the image, with the
63   //    following key values:
64   //    0 = remove all lightness (make all pixels black).
65   //    0.5 = leave unchanged.
66   //    1 = full lightness (make all pixels white).
67   static ImageSkia CreateHSLShiftedImage(const gfx::ImageSkia& image,
68                                          const color_utils::HSL& hsl_shift);
69 
70   // Creates a button background image by compositing the color and image
71   // together, then applying the mask. This is a highly specialized composite
72   // operation that is the equivalent of drawing a background in |color|,
73   // tiling |image| over the top, and then masking the result out with |mask|.
74   // The images must use kARGB_8888_Config config.
75   static ImageSkia CreateButtonBackground(SkColor color,
76                                           const gfx::ImageSkia& image,
77                                           const gfx::ImageSkia& mask);
78 
79   // Returns an image which is a subset of |image| with bounds |subset_bounds|.
80   // The |image| cannot use kA1_Config config.
81   static ImageSkia ExtractSubset(const gfx::ImageSkia& image,
82                                  const gfx::Rect& subset_bounds);
83 
84   // Creates an image by resizing |source| to given |target_dip_size|.
85   static ImageSkia CreateResizedImage(const ImageSkia& source,
86                                       skia::ImageOperations::ResizeMethod methd,
87                                       const Size& target_dip_size);
88 
89   // Creates an image with drop shadow defined in |shadows| for |source|.
90   static ImageSkia CreateImageWithDropShadow(const ImageSkia& source,
91                                              const ShadowValues& shadows);
92 
93   // Creates an image that is 1dp wide, suitable for tiling horizontally to
94   // create a drop shadow effect. The purpose of tiling a static image is to
95   // avoid repeatedly asking Skia to draw a shadow.
96   static ImageSkia CreateHorizontalShadow(
97       const std::vector<ShadowValue>& shadows,
98       bool fades_down);
99 
100   // Creates an image which is a rotation of the |source|. |rotation| is the
101   // amount of clockwise rotation in degrees.
102   static ImageSkia CreateRotatedImage(
103       const ImageSkia& source,
104       SkBitmapOperations::RotationAmount rotation);
105 
106   // Creates an icon by painting the second icon as a badge to the first one.
107   // The second icon is in the right corner of the first icon. If the icon
108   // is valid and the badge is not, the icon will be returned.
109   static ImageSkia CreateIconWithBadge(const ImageSkia& icon,
110                                        const ImageSkia& badge);
111 
112   // Creates an image by combining |image| and color |color|.
113   // The image must use the kARGB_8888_Config config.
114   static ImageSkia CreateColorMask(const gfx::ImageSkia& image, SkColor color);
115 
116   // Creates an image with a circle background. |color| and |radius| is the
117   // color and radius of the circle background.
118   static ImageSkia CreateImageWithCircleBackground(int radius,
119                                                    SkColor color,
120                                                    const ImageSkia& image);
121 
122  private:
123   ImageSkiaOperations();  // Class for scoping only.
124 };
125 
126 }  // namespace gfx
127 
128 #endif  // UI_GFX_IMAGE_IMAGE_SKIA_OPERATIONS_H_
129