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 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 12 | #include "SkSize.h" |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | class SkWriteBuffer; |
| 15 | class SkReadBuffer; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 16 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Describes how to interpret the alpha compoent of a pixel. |
| 19 | */ |
| 20 | enum SkAlphaType { |
| 21 | /** |
| 22 | * All pixels should be treated as opaque, regardless of the value stored |
| 23 | * in their alpha field. Used for legacy images that wrote 0 or garbarge |
| 24 | * in their alpha field, but intended the RGB to be treated as opaque. |
| 25 | */ |
| 26 | kIgnore_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 27 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 28 | /** |
| 29 | * All pixels are stored as opaque. This differs slightly from kIgnore in |
| 30 | * that kOpaque has correct "opaque" values stored in the pixels, while |
| 31 | * kIgnore may not, but in both cases the caller should treat the pixels |
| 32 | * as opaque. |
| 33 | */ |
| 34 | kOpaque_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 35 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 36 | /** |
| 37 | * All pixels have their alpha premultiplied in their color components. |
| 38 | * This is the natural format for the rendering target pixels. |
| 39 | */ |
| 40 | kPremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 41 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 42 | /** |
| 43 | * All pixels have their color components stored without any regard to the |
| 44 | * alpha. e.g. this is the default configuration for PNG images. |
| 45 | * |
| 46 | * This alpha-type is ONLY supported for input images. Rendering cannot |
| 47 | * generate this on output. |
| 48 | */ |
| 49 | kUnpremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 50 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 51 | kLastEnum_SkAlphaType = kUnpremul_SkAlphaType |
| 52 | }; |
| 53 | |
| 54 | static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { |
| 55 | SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order); |
| 56 | SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); |
| 57 | SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); |
| 58 | |
| 59 | return (unsigned)at <= kOpaque_SkAlphaType; |
| 60 | } |
| 61 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 62 | static inline bool SkAlphaTypeIsValid(unsigned value) { |
| 63 | return value <= kLastEnum_SkAlphaType; |
| 64 | } |
| 65 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | /** |
| 69 | * Describes how to interpret the components of a pixel. |
reed | 6c6ddb8 | 2014-06-30 12:44:03 -0700 | [diff] [blame] | 70 | * |
| 71 | * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native" |
| 72 | * form for skia's blitters. Use this if you don't have a swizzle preference |
| 73 | * for 32bit pixels. |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 74 | */ |
| 75 | enum SkColorType { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 76 | kUnknown_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 77 | kAlpha_8_SkColorType, |
| 78 | kRGB_565_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 79 | kARGB_4444_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 80 | kRGBA_8888_SkColorType, |
| 81 | kBGRA_8888_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 82 | kIndex_8_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 83 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 84 | kLastEnum_SkColorType = kIndex_8_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 |
| 91 | #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" |
| 92 | #endif |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef SK_SUPPORT_LEGACY_N32_NAME |
| 95 | kPMColor_SkColorType = kN32_SkColorType |
| 96 | #endif |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static int SkColorTypeBytesPerPixel(SkColorType ct) { |
| 100 | static const uint8_t gSize[] = { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 101 | 0, // Unknown |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 102 | 1, // Alpha_8 |
| 103 | 2, // RGB_565 |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 104 | 2, // ARGB_4444 |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 105 | 4, // RGBA_8888 |
| 106 | 4, // BGRA_8888 |
| 107 | 1, // kIndex_8 |
| 108 | }; |
| 109 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1), |
| 110 | size_mismatch_with_SkColorType_enum); |
| 111 | |
| 112 | SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize)); |
| 113 | return gSize[ct]; |
| 114 | } |
| 115 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 116 | static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { |
| 117 | return width * SkColorTypeBytesPerPixel(ct); |
| 118 | } |
| 119 | |
| 120 | static inline bool SkColorTypeIsValid(unsigned value) { |
| 121 | return value <= kLastEnum_SkColorType; |
| 122 | } |
| 123 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
| 126 | /** |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 127 | * Return true if alphaType is supported by colorType. If there is a canonical |
| 128 | * alphaType for this colorType, return it in canonical. |
| 129 | */ |
| 130 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
| 131 | SkAlphaType* canonical = NULL); |
| 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | /** |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 136 | * Describe an image's dimensions and pixel type. |
| 137 | */ |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 138 | struct SK_API SkImageInfo { |
| 139 | public: |
| 140 | SkImageInfo() {} |
| 141 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 142 | int fWidth; |
| 143 | int fHeight; |
| 144 | SkColorType fColorType; |
| 145 | SkAlphaType fAlphaType; |
| 146 | |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 147 | /* |
| 148 | * Return an info with the specified attributes, tagged as sRGB. Note that if the requested |
| 149 | * color type does not make sense with sRGB (e.g. kAlpha_8) then the sRGB request is ignored. |
| 150 | * |
| 151 | * You can call isSRGB() on the returned info to determine if the request was fulfilled. |
| 152 | */ |
| 153 | static SkImageInfo MakeSRGB(int width, int height, SkColorType ct, SkAlphaType at); |
| 154 | |
| 155 | /* |
| 156 | * Return an info with the specified attributes, tagged with a specific gamma. |
| 157 | * Note that if the requested gamma is unsupported for the requested color type, then the gamma |
| 158 | * value will be set to 1.0 (the default). |
| 159 | * |
| 160 | * You can call gamma() to query the resulting gamma value. |
| 161 | */ |
| 162 | static SkImageInfo MakeWithGamma(int width, int height, SkColorType ct, SkAlphaType at, |
| 163 | float gamma); |
| 164 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 165 | static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at) { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 166 | return MakeWithGamma(width, height, ct, at, 1); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets colortype to the native ARGB32 type. |
| 171 | */ |
| 172 | static SkImageInfo MakeN32(int width, int height, SkAlphaType at) { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 173 | return SkImageInfo(width, height, kN32_SkColorType, at, kExponential_Profile, 1); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets colortype to the native ARGB32 type, and the alphatype to premul. |
| 178 | */ |
| 179 | static SkImageInfo MakeN32Premul(int width, int height) { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 180 | return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType, |
| 181 | kExponential_Profile, 1); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 182 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 183 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 184 | /** |
| 185 | * Sets colortype to the native ARGB32 type, and the alphatype to premul. |
| 186 | */ |
| 187 | static SkImageInfo MakeN32Premul(const SkISize& size) { |
| 188 | return MakeN32Premul(size.width(), size.height()); |
| 189 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 190 | |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 191 | static SkImageInfo MakeA8(int width, int height) { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 192 | return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, |
| 193 | kUnknown_Profile, 0); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 194 | } |
skia.committer@gmail.com | d77b3ec | 2014-01-15 07:01:43 +0000 | [diff] [blame] | 195 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 196 | static SkImageInfo MakeUnknown(int width, int height) { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 197 | return SkImageInfo(width, height, kUnknown_SkColorType, kIgnore_SkAlphaType, |
| 198 | kUnknown_Profile, 0); |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 199 | } |
| 200 | |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 201 | static SkImageInfo MakeUnknown() { |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 202 | return SkImageInfo(0, 0, kUnknown_SkColorType, kIgnore_SkAlphaType, kUnknown_Profile, 0); |
reed | f252f64 | 2014-06-14 04:24:56 -0700 | [diff] [blame] | 203 | } |
| 204 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 205 | int width() const { return fWidth; } |
| 206 | int height() const { return fHeight; } |
| 207 | SkColorType colorType() const { return fColorType; } |
| 208 | SkAlphaType alphaType() const { return fAlphaType; } |
| 209 | |
| 210 | bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; } |
| 211 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 212 | bool isOpaque() const { |
| 213 | return SkAlphaTypeIsOpaque(fAlphaType); |
| 214 | } |
| 215 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 216 | SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } |
| 217 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Return a new ImageInfo with the same colortype and alphatype as this info, |
| 220 | * but with the specified width and height. |
| 221 | */ |
| 222 | SkImageInfo makeWH(int newWidth, int newHeight) const { |
| 223 | return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType); |
| 224 | } |
| 225 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 226 | int bytesPerPixel() const { |
| 227 | return SkColorTypeBytesPerPixel(fColorType); |
| 228 | } |
| 229 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 230 | uint64_t minRowBytes64() const { |
| 231 | return sk_64_mul(fWidth, this->bytesPerPixel()); |
| 232 | } |
| 233 | |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 234 | size_t minRowBytes() const { |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 235 | return (size_t)this->minRowBytes64(); |
mike@reedtribe.org | 169a0ed | 2014-02-11 02:20:17 +0000 | [diff] [blame] | 236 | } |
skia.committer@gmail.com | 8ed6443 | 2014-02-11 03:02:13 +0000 | [diff] [blame] | 237 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 238 | bool operator==(const SkImageInfo& other) const { |
| 239 | return 0 == memcmp(this, &other, sizeof(other)); |
| 240 | } |
| 241 | bool operator!=(const SkImageInfo& other) const { |
| 242 | return 0 != memcmp(this, &other, sizeof(other)); |
| 243 | } |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 244 | |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 245 | // DEPRECATED : use the static Unflatten |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 246 | void unflatten(SkReadBuffer&); |
| 247 | void flatten(SkWriteBuffer&) const; |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 248 | |
| 249 | static SkImageInfo Unflatten(SkReadBuffer&); |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 250 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 251 | int64_t getSafeSize64(size_t rowBytes) const { |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 252 | if (0 == fHeight) { |
| 253 | return 0; |
| 254 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 255 | return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel(); |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 256 | } |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 257 | |
| 258 | size_t getSafeSize(size_t rowBytes) const { |
| 259 | return (size_t)this->getSafeSize64(rowBytes); |
| 260 | } |
| 261 | |
| 262 | bool validRowBytes(size_t rowBytes) const { |
| 263 | uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel()); |
| 264 | return rowBytes >= rb; |
| 265 | } |
| 266 | |
| 267 | SkDEBUGCODE(void validate() const;) |
reed | 228b285 | 2014-08-29 04:08:43 -0700 | [diff] [blame^] | 268 | |
| 269 | /** |
| 270 | * If the Info was tagged to be sRGB, return true, else return false. |
| 271 | */ |
| 272 | bool isSRGB() const { return kSRGB_Profile == fProfile; } |
| 273 | |
| 274 | /** |
| 275 | * If this was tagged with an explicit gamma value, return that value, else return 0. |
| 276 | * If this was tagged as sRGB, return 0. |
| 277 | */ |
| 278 | float gamma() const { return fGamma; } |
| 279 | |
| 280 | private: |
| 281 | enum Profile { |
| 282 | kUnknown_Profile, |
| 283 | kSRGB_Profile, |
| 284 | kExponential_Profile, |
| 285 | }; |
| 286 | |
| 287 | uint32_t fProfile; |
| 288 | float fGamma; |
| 289 | |
| 290 | SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, Profile p, float g) |
| 291 | : fWidth(width) |
| 292 | , fHeight(height) |
| 293 | , fColorType(ct) |
| 294 | , fAlphaType(at) |
| 295 | , fProfile(p) |
| 296 | , fGamma(g) |
| 297 | {} |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | #endif |