Home | History | Annotate | Download | only in gfx
      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_SKBITMAP_OPERATIONS_H_
      6 #define UI_GFX_SKBITMAP_OPERATIONS_H_
      7 
      8 #include "base/gtest_prod_util.h"
      9 #include "ui/gfx/color_utils.h"
     10 #include "ui/gfx/gfx_export.h"
     11 #include "ui/gfx/shadow_value.h"
     12 
     13 namespace gfx {
     14 class Point;
     15 class Size;
     16 }
     17 
     18 class SkBitmap;
     19 
     20 class GFX_EXPORT SkBitmapOperations {
     21  public:
     22   // Enum for use in rotating images (must be in 90 degree increments),
     23   // see: Rotate.
     24   enum RotationAmount {
     25     ROTATION_90_CW,
     26     ROTATION_180_CW,
     27     ROTATION_270_CW,
     28   };
     29 
     30   // Create a bitmap that is an inverted image of the passed in image.
     31   // Each color becomes its inverse in the color wheel. So (255, 15, 0) becomes
     32   // (0, 240, 255). The alpha value is not inverted.
     33   static SkBitmap CreateInvertedBitmap(const SkBitmap& image);
     34 
     35   // Create a bitmap that is a blend of two others. The alpha argument
     36   // specifies the opacity of the second bitmap. The provided bitmaps must
     37   // use have the kARGB_8888_Config config and be of equal dimensions.
     38   static SkBitmap CreateBlendedBitmap(const SkBitmap& first,
     39                                       const SkBitmap& second,
     40                                       double alpha);
     41 
     42   // Create a bitmap that is the original bitmap masked out by the mask defined
     43   // in the alpha bitmap. The images must use the kARGB_8888_Config config and
     44   // be of equal dimensions.
     45   static SkBitmap CreateMaskedBitmap(const SkBitmap& first,
     46                                      const SkBitmap& alpha);
     47 
     48   // We create a button background image by compositing the color and image
     49   // together, then applying the mask. This is a highly specialized composite
     50   // operation that is the equivalent of drawing a background in |color|,
     51   // tiling |image| over the top, and then masking the result out with |mask|.
     52   // The images must use kARGB_8888_Config config.
     53   static SkBitmap CreateButtonBackground(SkColor color,
     54                                          const SkBitmap& image,
     55                                          const SkBitmap& mask);
     56 
     57   // Shift a bitmap's HSL values. The shift values are in the range of 0-1,
     58   // with the option to specify -1 for 'no change'. The shift values are
     59   // defined as:
     60   // hsl_shift[0] (hue): The absolute hue value for the image - 0 and 1 map
     61   //    to 0 and 360 on the hue color wheel (red).
     62   // hsl_shift[1] (saturation): A saturation shift for the image, with the
     63   //    following key values:
     64   //    0 = remove all color.
     65   //    0.5 = leave unchanged.
     66   //    1 = fully saturate the image.
     67   // hsl_shift[2] (lightness): A lightness shift for the image, with the
     68   //    following key values:
     69   //    0 = remove all lightness (make all pixels black).
     70   //    0.5 = leave unchanged.
     71   //    1 = full lightness (make all pixels white).
     72   static SkBitmap CreateHSLShiftedBitmap(const SkBitmap& bitmap,
     73                                          const color_utils::HSL& hsl_shift);
     74 
     75   // Create a bitmap that is cropped from another bitmap. This is special
     76   // because it tiles the original bitmap, so your coordinates can extend
     77   // outside the bounds of the original image.
     78   static SkBitmap CreateTiledBitmap(const SkBitmap& bitmap,
     79                                     int src_x, int src_y,
     80                                     int dst_w, int dst_h);
     81 
     82   // Iteratively downsamples by 2 until the bitmap is no smaller than the
     83   // input size. The normal use of this is to downsample the bitmap "close" to
     84   // the final size, and then use traditional resampling on the result.
     85   // Because the bitmap will be closer to the final size, it will be faster,
     86   // and linear interpolation will generally work well as a second step.
     87   static SkBitmap DownsampleByTwoUntilSize(const SkBitmap& bitmap,
     88                                            int min_w, int min_h);
     89 
     90   // Makes a bitmap half has large in each direction by averaging groups of
     91   // 4 pixels. This is one step in generating a mipmap.
     92   static SkBitmap DownsampleByTwo(const SkBitmap& bitmap);
     93 
     94   // Unpremultiplies all pixels in |bitmap|. You almost never want to call
     95   // this, as |SkBitmap|s are always premultiplied by conversion. Call this
     96   // only if you will pass the bitmap's data into a system function that
     97   // doesn't expect premultiplied colors.
     98   static SkBitmap UnPreMultiply(const SkBitmap& bitmap);
     99 
    100   // Transpose the pixels in |bitmap| by swapping x and y.
    101   static SkBitmap CreateTransposedBitmap(const SkBitmap& bitmap);
    102 
    103   // Create a bitmap by combining alpha channel of |bitmap| and color |c|.
    104   // The image must use the kARGB_8888_Config config.
    105   static SkBitmap CreateColorMask(const SkBitmap& bitmap, SkColor c);
    106 
    107   // Create a bitmap with drop shadow added to |bitmap|. |shadows| defines
    108   // the shadows to add. The created bitmap would be padded to have enough space
    109   // for shadows and have original bitmap in the center. The image must use the
    110   // kARGB_8888_Config config.
    111   static SkBitmap CreateDropShadow(const SkBitmap& bitmap,
    112                                    const gfx::ShadowValues& shadows);
    113 
    114   // Rotates the given source bitmap clockwise by the requested amount.
    115   static SkBitmap Rotate(const SkBitmap& source, RotationAmount rotation);
    116 
    117  private:
    118   SkBitmapOperations();  // Class for scoping only.
    119 
    120   FRIEND_TEST_ALL_PREFIXES(SkBitmapOperationsTest, DownsampleByTwo);
    121   FRIEND_TEST_ALL_PREFIXES(SkBitmapOperationsTest, DownsampleByTwoSmall);
    122 };
    123 
    124 #endif  // UI_GFX_SKBITMAP_OPERATIONS_H_
    125