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