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