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 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 57 | /** Temporary macro that allows us to add new color types without breaking Chrome compile. */ |
| 58 | #define SK_EXTENDED_COLOR_TYPES |
| 59 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Describes how to interpret the components of a pixel. |
reed | 6c6ddb8 | 2014-06-30 12:44:03 -0700 | [diff] [blame] | 62 | * |
| 63 | * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native" |
| 64 | * form for skia's blitters. Use this if you don't have a swizzle preference |
| 65 | * for 32bit pixels. |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 66 | */ |
| 67 | enum SkColorType { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 68 | kUnknown_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 69 | kAlpha_8_SkColorType, |
| 70 | kRGB_565_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 71 | kARGB_4444_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 72 | kRGBA_8888_SkColorType, |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 73 | kRGB_888x_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 74 | kBGRA_8888_SkColorType, |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 75 | kRGBA_1010102_SkColorType, |
| 76 | kRGB_101010x_SkColorType, |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 77 | kGray_8_SkColorType, |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 78 | kRGBA_F16_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 79 | |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 80 | kLastEnum_SkColorType = kRGBA_F16_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 81 | |
| 82 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 83 | kN32_SkColorType = kBGRA_8888_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 84 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 85 | kN32_SkColorType = kRGBA_8888_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 86 | #else |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 87 | #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] | 88 | #endif |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 91 | /** |
| 92 | * Returns the number of bytes-per-pixel for the specified colortype, or 0 if invalid. |
| 93 | */ |
| 94 | SK_API int SkColorTypeBytesPerPixel(SkColorType ct); |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 97 | * Tries to validate the colortype, alphatype pair. In all cases if it returns true, it |
| 98 | * will set canonical to the "canonical" answer if it is non-null, and ignore the parameter if |
| 99 | * it is set to null. |
| 100 | * |
| 101 | * If the specified colortype has only 1 valid alphatype (e.g. 565 must always be opaque) then |
| 102 | * canonical will be set to that valid alphatype. |
| 103 | * |
| 104 | * If the specified colortype treats more than one alphatype the same (e.g. Alpha_8 colortype |
| 105 | * treates Premul and Unpremul the same) and the specified alphatype is one of those, |
| 106 | * then canonical will be set to the "canonical" answer (Premul in the case of Alpha_8 colortype). |
| 107 | * |
| 108 | * If the colortype supports multiple alphatypes, and the specified alphatype is one of them, |
| 109 | * then canonical will be set to the specified alphatype. If the specified alphatype is not |
| 110 | * one of them (e.g. kUnknown_SkAlphaType is not valid for any colortype except |
| 111 | * kUnknown_SkColorType), then the function returns false, and canonical's value is undefined. |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 112 | */ |
| 113 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 114 | SkAlphaType* canonical = nullptr); |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 115 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 116 | |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | /** |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 120 | * Describes the color space a YUV pixel. |
| 121 | */ |
| 122 | enum SkYUVColorSpace { |
| 123 | /** Standard JPEG color space. */ |
| 124 | kJPEG_SkYUVColorSpace, |
| 125 | /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color |
| 126 | range. See http://en.wikipedia.org/wiki/Rec._601 for details. */ |
| 127 | kRec601_SkYUVColorSpace, |
rileya | 1340039 | 2015-07-20 15:00:03 -0700 | [diff] [blame] | 128 | /** HDTV standard Rec. 709 color space. Uses "studio swing" [16, 235] color |
| 129 | range. See http://en.wikipedia.org/wiki/Rec._709 for details. */ |
| 130 | kRec709_SkYUVColorSpace, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 131 | |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 132 | kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace, |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 137 | enum class SkDestinationSurfaceColorMode { |
| 138 | kLegacy, |
| 139 | kGammaAndColorSpaceAware, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 142 | /** |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 143 | * Describe an image's dimensions and pixel type. |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 144 | * Used for both src images and render-targets (surfaces). |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 145 | */ |
reed | 4181492 | 2015-01-30 14:54:38 -0800 | [diff] [blame] | 146 | struct SK_API SkImageInfo { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 147 | public: |
| 148 | SkImageInfo() |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 149 | : fColorSpace(nullptr) |
| 150 | , fWidth(0) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 151 | , fHeight(0) |
| 152 | , fColorType(kUnknown_SkColorType) |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 153 | , fAlphaType(kUnknown_SkAlphaType) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 154 | {} |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 155 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 156 | static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 157 | sk_sp<SkColorSpace> cs = nullptr) { |
| 158 | return SkImageInfo(width, height, ct, at, std::move(cs)); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets colortype to the native ARGB32 type. |
| 163 | */ |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 164 | static SkImageInfo MakeN32(int width, int height, SkAlphaType at, |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 165 | sk_sp<SkColorSpace> cs = nullptr) { |
| 166 | return Make(width, height, kN32_SkColorType, at, cs); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 167 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 168 | |
reed | fbce71f | 2016-06-02 12:40:22 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Create an ImageInfo marked as SRGB with N32 swizzle. |
| 171 | */ |
| 172 | static SkImageInfo MakeS32(int width, int height, SkAlphaType at); |
| 173 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Sets colortype to the native ARGB32 type, and the alphatype to premul. |
| 176 | */ |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 177 | static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) { |
| 178 | return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, cs); |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static SkImageInfo MakeN32Premul(const SkISize& size) { |
| 182 | return MakeN32Premul(size.width(), size.height()); |
| 183 | } |
| 184 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 185 | static SkImageInfo MakeA8(int width, int height) { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 186 | return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 187 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 188 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 189 | static SkImageInfo MakeUnknown(int width, int height) { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 190 | return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr); |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 191 | } |
| 192 | |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 193 | static SkImageInfo MakeUnknown() { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 194 | return MakeUnknown(0, 0); |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 195 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 196 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 197 | int width() const { return fWidth; } |
| 198 | int height() const { return fHeight; } |
| 199 | SkColorType colorType() const { return fColorType; } |
| 200 | SkAlphaType alphaType() const { return fAlphaType; } |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 201 | SkColorSpace* colorSpace() const { return fColorSpace.get(); } |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 202 | sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; } |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 203 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 204 | bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; } |
| 205 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 206 | bool isOpaque() const { |
| 207 | return SkAlphaTypeIsOpaque(fAlphaType); |
| 208 | } |
| 209 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 210 | SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 211 | SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 212 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 213 | bool gammaCloseToSRGB() const { |
| 214 | return fColorSpace && fColorSpace->gammaCloseToSRGB(); |
| 215 | } |
| 216 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Return a new ImageInfo with the same colortype and alphatype as this info, |
| 219 | * but with the specified width and height. |
| 220 | */ |
| 221 | SkImageInfo makeWH(int newWidth, int newHeight) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 222 | return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace); |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 223 | } |
| 224 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 225 | SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 226 | return Make(fWidth, fHeight, fColorType, newAlphaType, fColorSpace); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 227 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 228 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 229 | SkImageInfo makeColorType(SkColorType newColorType) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 230 | return Make(fWidth, fHeight, newColorType, fAlphaType, fColorSpace); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 231 | } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 232 | |
msarett | 804b461 | 2016-06-09 11:03:45 -0700 | [diff] [blame] | 233 | SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 234 | return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs)); |
msarett | 804b461 | 2016-06-09 11:03:45 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 237 | int bytesPerPixel() const; |
| 238 | int shiftPerPixel() const; |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 239 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 240 | uint64_t minRowBytes64() const { |
| 241 | return sk_64_mul(fWidth, this->bytesPerPixel()); |
| 242 | } |
| 243 | |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 244 | size_t minRowBytes() const { |
Matt Sarett | 29121eb | 2016-10-17 14:32:46 -0400 | [diff] [blame] | 245 | uint64_t minRowBytes = this->minRowBytes64(); |
| 246 | if (!sk_64_isS32(minRowBytes)) { |
| 247 | return 0; |
| 248 | } |
| 249 | return sk_64_asS32(minRowBytes); |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 250 | } |
skia.committer@gmail.com | 8ed6443 | 2014-02-11 03:02:13 +0000 | [diff] [blame] | 251 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 252 | size_t computeOffset(int x, int y, size_t rowBytes) const; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 253 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 254 | bool operator==(const SkImageInfo& other) const { |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 255 | return fWidth == other.fWidth && fHeight == other.fHeight && |
| 256 | fColorType == other.fColorType && fAlphaType == other.fAlphaType && |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 257 | SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get()); |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 258 | } |
| 259 | bool operator!=(const SkImageInfo& other) const { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 260 | return !(*this == other); |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 261 | } |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 262 | |
Cary Clark | 745c398 | 2018-01-24 15:37:13 -0500 | [diff] [blame] | 263 | void unflatten(SkReadBuffer& buffer); |
| 264 | void flatten(SkWriteBuffer& buffer) const; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 265 | |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 266 | /** |
| 267 | * Returns the size (in bytes) of the image buffer that this info needs, given the specified |
| 268 | * rowBytes. The rowBytes must be >= this->minRowBytes(). |
Mike Reed | 3758c75 | 2017-10-11 11:30:31 -0400 | [diff] [blame] | 269 | * |
| 270 | * if (height == 0) { |
| 271 | * return 0; |
| 272 | * } else { |
| 273 | * return (height - 1) * rowBytes + width * bytes_per_pixel; |
| 274 | * } |
| 275 | * |
Mike Reed | 7281801 | 2017-10-09 11:37:44 -0400 | [diff] [blame] | 276 | * If the calculation overflows this returns SK_MaxSizeT |
| 277 | */ |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 278 | size_t computeByteSize(size_t rowBytes) const; |
| 279 | |
| 280 | /** |
| 281 | * Returns the minimum size (in bytes) of the image buffer that this info needs. |
| 282 | * If the calculation overflows, or if the height is 0, this returns 0. |
| 283 | */ |
| 284 | size_t computeMinByteSize() const { |
| 285 | return this->computeByteSize(this->minRowBytes()); |
| 286 | } |
| 287 | |
Mike Reed | c5eb97d | 2017-10-09 10:42:51 -0400 | [diff] [blame] | 288 | // Returns true if the result of computeByteSize (or computeMinByteSize) overflowed |
| 289 | static bool ByteSizeOverflowed(size_t byteSize) { |
Mike Reed | 7281801 | 2017-10-09 11:37:44 -0400 | [diff] [blame] | 290 | return SK_MaxSizeT == byteSize; |
Mike Reed | c5eb97d | 2017-10-09 10:42:51 -0400 | [diff] [blame] | 291 | } |
| 292 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 293 | bool validRowBytes(size_t rowBytes) const { |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 294 | uint64_t minRB = sk_64_mul(fWidth, this->bytesPerPixel()); |
| 295 | return rowBytes >= minRB; |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 296 | } |
| 297 | |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 298 | void reset() { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 299 | fColorSpace = nullptr; |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 300 | fWidth = 0; |
| 301 | fHeight = 0; |
| 302 | fColorType = kUnknown_SkColorType; |
| 303 | fAlphaType = kUnknown_SkAlphaType; |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 304 | } |
| 305 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 306 | SkDEBUGCODE(void validate() const;) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 307 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 308 | private: |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 309 | sk_sp<SkColorSpace> fColorSpace; |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 310 | int fWidth; |
| 311 | int fHeight; |
| 312 | SkColorType fColorType; |
| 313 | SkAlphaType fAlphaType; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 314 | |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 315 | SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 316 | : fColorSpace(std::move(cs)) |
| 317 | , fWidth(width) |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 318 | , fHeight(height) |
| 319 | , fColorType(ct) |
| 320 | , fAlphaType(at) |
| 321 | {} |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 324 | #endif |