reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkImageInfo_DEFINED |
| 9 | #define SkImageInfo_DEFINED |
| 10 | |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 11 | #include "SkColorSpace.h" |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 12 | #include "SkMath.h" |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 13 | #include "SkRect.h" |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 14 | #include "SkSize.h" |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 16 | class SkReadBuffer; |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 17 | class SkWriteBuffer; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 18 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 19 | /** |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 20 | * Describes how to interpret the alpha component of a pixel. |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 21 | */ |
| 22 | enum SkAlphaType { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 23 | kUnknown_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 24 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 25 | /** |
| 26 | * All pixels are stored as opaque. This differs slightly from kIgnore in |
| 27 | * that kOpaque has correct "opaque" values stored in the pixels, while |
| 28 | * kIgnore may not, but in both cases the caller should treat the pixels |
| 29 | * as opaque. |
| 30 | */ |
| 31 | kOpaque_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 32 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 33 | /** |
| 34 | * All pixels have their alpha premultiplied in their color components. |
| 35 | * This is the natural format for the rendering target pixels. |
| 36 | */ |
| 37 | kPremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 38 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 39 | /** |
| 40 | * All pixels have their color components stored without any regard to the |
| 41 | * alpha. e.g. this is the default configuration for PNG images. |
| 42 | * |
Joe Gregorio | c485907 | 2017-01-20 14:21:27 +0000 | [diff] [blame] | 43 | * This alpha-type is ONLY supported for input images. Rendering cannot |
| 44 | * generate this on output. |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 45 | */ |
| 46 | kUnpremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 47 | |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 48 | kLastEnum_SkAlphaType = kUnpremul_SkAlphaType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 52 | return kOpaque_SkAlphaType == at; |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 55 | static inline bool SkAlphaTypeIsValid(unsigned value) { |
| 56 | return value <= kLastEnum_SkAlphaType; |
| 57 | } |
| 58 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame^] | 61 | /** Temporary macro that allows us to add new color types without breaking Chrome compile. */ |
| 62 | #define SK_EXTENDED_COLOR_TYPES |
| 63 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Describes how to interpret the components of a pixel. |
reed | 6c6ddb8 | 2014-06-30 12:44:03 -0700 | [diff] [blame] | 66 | * |
| 67 | * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native" |
| 68 | * form for skia's blitters. Use this if you don't have a swizzle preference |
| 69 | * for 32bit pixels. |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 70 | */ |
| 71 | enum SkColorType { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 72 | kUnknown_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 73 | kAlpha_8_SkColorType, |
| 74 | kRGB_565_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 75 | kARGB_4444_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 76 | kRGBA_8888_SkColorType, |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame^] | 77 | kRGB_888x_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 78 | kBGRA_8888_SkColorType, |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame^] | 79 | kRGBA_1010102_SkColorType, |
| 80 | kRGB_101010x_SkColorType, |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 81 | kGray_8_SkColorType, |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 82 | kRGBA_F16_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 83 | |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 84 | kLastEnum_SkColorType = kRGBA_F16_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 85 | |
| 86 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 87 | kN32_SkColorType = kBGRA_8888_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 88 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 89 | kN32_SkColorType = kRGBA_8888_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 90 | #else |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 91 | #error "SK_*32_SHIFT values must correspond to BGRA or RGBA byte order" |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 92 | #endif |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
reed | 6d7cd1f | 2016-04-15 10:03:03 -0700 | [diff] [blame] | 95 | static int SkColorTypeBytesPerPixel(SkColorType ct) { |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame^] | 96 | switch (ct) { |
| 97 | case kUnknown_SkColorType: return 0; |
| 98 | case kAlpha_8_SkColorType: return 1; |
| 99 | case kRGB_565_SkColorType: return 2; |
| 100 | case kARGB_4444_SkColorType: return 2; |
| 101 | case kRGBA_8888_SkColorType: return 4; |
| 102 | case kBGRA_8888_SkColorType: return 4; |
| 103 | case kRGB_888x_SkColorType: return 4; |
| 104 | case kRGBA_1010102_SkColorType: return 4; |
| 105 | case kRGB_101010x_SkColorType: return 4; |
| 106 | case kGray_8_SkColorType: return 1; |
| 107 | case kRGBA_F16_SkColorType: return 8; |
| 108 | } |
| 109 | return 0; |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 112 | static int SkColorTypeShiftPerPixel(SkColorType ct) { |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame^] | 113 | switch (ct) { |
| 114 | case kUnknown_SkColorType: return 0; |
| 115 | case kAlpha_8_SkColorType: return 0; |
| 116 | case kRGB_565_SkColorType: return 1; |
| 117 | case kARGB_4444_SkColorType: return 1; |
| 118 | case kRGBA_8888_SkColorType: return 2; |
| 119 | case kRGB_888x_SkColorType: return 2; |
| 120 | case kBGRA_8888_SkColorType: return 2; |
| 121 | case kRGBA_1010102_SkColorType: return 2; |
| 122 | case kRGB_101010x_SkColorType: return 2; |
| 123 | case kGray_8_SkColorType: return 0; |
| 124 | case kRGBA_F16_SkColorType: return 3; |
| 125 | } |
| 126 | return 0; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 127 | } |
| 128 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 129 | static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { |
| 130 | return width * SkColorTypeBytesPerPixel(ct); |
| 131 | } |
| 132 | |
| 133 | static inline bool SkColorTypeIsValid(unsigned value) { |
| 134 | return value <= kLastEnum_SkColorType; |
| 135 | } |
| 136 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 137 | static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 138 | if (kUnknown_SkColorType == ct) { |
| 139 | return 0; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 140 | } |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 141 | return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct)); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 142 | } |
| 143 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | |
| 146 | /** |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 147 | * Return true if alphaType is supported by colorType. If there is a canonical |
| 148 | * alphaType for this colorType, return it in canonical. |
| 149 | */ |
| 150 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 151 | SkAlphaType* canonical = nullptr); |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 152 | |
| 153 | /////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | /** |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 156 | * Describes the color space a YUV pixel. |
| 157 | */ |
| 158 | enum SkYUVColorSpace { |
| 159 | /** Standard JPEG color space. */ |
| 160 | kJPEG_SkYUVColorSpace, |
| 161 | /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color |
| 162 | range. See http://en.wikipedia.org/wiki/Rec._601 for details. */ |
| 163 | kRec601_SkYUVColorSpace, |
rileya | 1340039 | 2015-07-20 15:00:03 -0700 | [diff] [blame] | 164 | /** HDTV standard Rec. 709 color space. Uses "studio swing" [16, 235] color |
| 165 | range. See http://en.wikipedia.org/wiki/Rec._709 for details. */ |
| 166 | kRec709_SkYUVColorSpace, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 167 | |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 168 | kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /////////////////////////////////////////////////////////////////////////////// |
| 172 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 173 | enum class SkDestinationSurfaceColorMode { |
| 174 | kLegacy, |
| 175 | kGammaAndColorSpaceAware, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 178 | /** |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 179 | * Describe an image's dimensions and pixel type. |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 180 | * Used for both src images and render-targets (surfaces). |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 181 | */ |
reed | 4181492 | 2015-01-30 14:54:38 -0800 | [diff] [blame] | 182 | struct SK_API SkImageInfo { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 183 | public: |
| 184 | SkImageInfo() |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 185 | : fColorSpace(nullptr) |
| 186 | , fWidth(0) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 187 | , fHeight(0) |
| 188 | , fColorType(kUnknown_SkColorType) |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 189 | , fAlphaType(kUnknown_SkAlphaType) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 190 | {} |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 191 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 192 | static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 193 | sk_sp<SkColorSpace> cs = nullptr) { |
| 194 | return SkImageInfo(width, height, ct, at, std::move(cs)); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets colortype to the native ARGB32 type. |
| 199 | */ |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 200 | static SkImageInfo MakeN32(int width, int height, SkAlphaType at, |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 201 | sk_sp<SkColorSpace> cs = nullptr) { |
| 202 | return Make(width, height, kN32_SkColorType, at, cs); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 203 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 204 | |
reed | fbce71f | 2016-06-02 12:40:22 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Create an ImageInfo marked as SRGB with N32 swizzle. |
| 207 | */ |
| 208 | static SkImageInfo MakeS32(int width, int height, SkAlphaType at); |
| 209 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Sets colortype to the native ARGB32 type, and the alphatype to premul. |
| 212 | */ |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 213 | static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) { |
| 214 | return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, cs); |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static SkImageInfo MakeN32Premul(const SkISize& size) { |
| 218 | return MakeN32Premul(size.width(), size.height()); |
| 219 | } |
| 220 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 221 | static SkImageInfo MakeA8(int width, int height) { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 222 | return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 223 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 224 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 225 | static SkImageInfo MakeUnknown(int width, int height) { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 226 | return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr); |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 227 | } |
| 228 | |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 229 | static SkImageInfo MakeUnknown() { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 230 | return MakeUnknown(0, 0); |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 231 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 232 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 233 | int width() const { return fWidth; } |
| 234 | int height() const { return fHeight; } |
| 235 | SkColorType colorType() const { return fColorType; } |
| 236 | SkAlphaType alphaType() const { return fAlphaType; } |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 237 | SkColorSpace* colorSpace() const { return fColorSpace.get(); } |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 238 | sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; } |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 239 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 240 | bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; } |
| 241 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 242 | bool isOpaque() const { |
| 243 | return SkAlphaTypeIsOpaque(fAlphaType); |
| 244 | } |
| 245 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 246 | SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 247 | SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 248 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 249 | bool gammaCloseToSRGB() const { |
| 250 | return fColorSpace && fColorSpace->gammaCloseToSRGB(); |
| 251 | } |
| 252 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Return a new ImageInfo with the same colortype and alphatype as this info, |
| 255 | * but with the specified width and height. |
| 256 | */ |
| 257 | SkImageInfo makeWH(int newWidth, int newHeight) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 258 | return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace); |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 259 | } |
| 260 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 261 | SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 262 | return Make(fWidth, fHeight, fColorType, newAlphaType, fColorSpace); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 263 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 264 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 265 | SkImageInfo makeColorType(SkColorType newColorType) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 266 | return Make(fWidth, fHeight, newColorType, fAlphaType, fColorSpace); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 267 | } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 268 | |
msarett | 804b461 | 2016-06-09 11:03:45 -0700 | [diff] [blame] | 269 | SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 270 | return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs)); |
msarett | 804b461 | 2016-06-09 11:03:45 -0700 | [diff] [blame] | 271 | } |
| 272 | |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 273 | int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); } |
| 274 | |
| 275 | int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); } |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 276 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 277 | uint64_t minRowBytes64() const { |
| 278 | return sk_64_mul(fWidth, this->bytesPerPixel()); |
| 279 | } |
| 280 | |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 281 | size_t minRowBytes() const { |
Matt Sarett | 29121eb | 2016-10-17 14:32:46 -0400 | [diff] [blame] | 282 | uint64_t minRowBytes = this->minRowBytes64(); |
| 283 | if (!sk_64_isS32(minRowBytes)) { |
| 284 | return 0; |
| 285 | } |
| 286 | return sk_64_asS32(minRowBytes); |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 287 | } |
skia.committer@gmail.com | 8ed6443 | 2014-02-11 03:02:13 +0000 | [diff] [blame] | 288 | |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 289 | size_t computeOffset(int x, int y, size_t rowBytes) const { |
| 290 | SkASSERT((unsigned)x < (unsigned)fWidth); |
| 291 | SkASSERT((unsigned)y < (unsigned)fHeight); |
| 292 | return SkColorTypeComputeOffset(fColorType, x, y, rowBytes); |
| 293 | } |
| 294 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 295 | bool operator==(const SkImageInfo& other) const { |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 296 | return fWidth == other.fWidth && fHeight == other.fHeight && |
| 297 | fColorType == other.fColorType && fAlphaType == other.fAlphaType && |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 298 | SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get()); |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 299 | } |
| 300 | bool operator!=(const SkImageInfo& other) const { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 301 | return !(*this == other); |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 302 | } |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 303 | |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 304 | void unflatten(SkReadBuffer& buffer); |
| 305 | void flatten(SkWriteBuffer& buffer) const; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 306 | |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 307 | /** |
| 308 | * Returns the size (in bytes) of the image buffer that this info needs, given the specified |
| 309 | * rowBytes. The rowBytes must be >= this->minRowBytes(). |
Mike Reed | 3758c75 | 2017-10-11 11:30:31 -0400 | [diff] [blame] | 310 | * |
| 311 | * if (height == 0) { |
| 312 | * return 0; |
| 313 | * } else { |
| 314 | * return (height - 1) * rowBytes + width * bytes_per_pixel; |
| 315 | * } |
| 316 | * |
Mike Reed | 7281801 | 2017-10-09 11:37:44 -0400 | [diff] [blame] | 317 | * If the calculation overflows this returns SK_MaxSizeT |
| 318 | */ |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 319 | size_t computeByteSize(size_t rowBytes) const; |
| 320 | |
| 321 | /** |
| 322 | * Returns the minimum size (in bytes) of the image buffer that this info needs. |
| 323 | * If the calculation overflows, or if the height is 0, this returns 0. |
| 324 | */ |
| 325 | size_t computeMinByteSize() const { |
| 326 | return this->computeByteSize(this->minRowBytes()); |
| 327 | } |
| 328 | |
Mike Reed | c5eb97d | 2017-10-09 10:42:51 -0400 | [diff] [blame] | 329 | // Returns true if the result of computeByteSize (or computeMinByteSize) overflowed |
| 330 | static bool ByteSizeOverflowed(size_t byteSize) { |
Mike Reed | 7281801 | 2017-10-09 11:37:44 -0400 | [diff] [blame] | 331 | return SK_MaxSizeT == byteSize; |
Mike Reed | c5eb97d | 2017-10-09 10:42:51 -0400 | [diff] [blame] | 332 | } |
| 333 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 334 | bool validRowBytes(size_t rowBytes) const { |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 335 | uint64_t minRB = sk_64_mul(fWidth, this->bytesPerPixel()); |
| 336 | return rowBytes >= minRB; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 337 | } |
| 338 | |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 339 | void reset() { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 340 | fColorSpace = nullptr; |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 341 | fWidth = 0; |
| 342 | fHeight = 0; |
| 343 | fColorType = kUnknown_SkColorType; |
| 344 | fAlphaType = kUnknown_SkAlphaType; |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 345 | } |
| 346 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 347 | SkDEBUGCODE(void validate() const;) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 348 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 349 | private: |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 350 | sk_sp<SkColorSpace> fColorSpace; |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 351 | int fWidth; |
| 352 | int fHeight; |
| 353 | SkColorType fColorType; |
| 354 | SkAlphaType fAlphaType; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 355 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 356 | SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 357 | : fColorSpace(std::move(cs)) |
| 358 | , fWidth(width) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 359 | , fHeight(height) |
| 360 | , fColorType(ct) |
| 361 | , fAlphaType(at) |
| 362 | {} |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 365 | #endif |