| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkImageInfo_DEFINED | 8 #ifndef SkImageInfo_DEFINED |
| 9 #define SkImageInfo_DEFINED | 9 #define SkImageInfo_DEFINED |
| 10 | 10 |
| 11 #include "SkTypes.h" | 11 #include "SkMath.h" |
| 12 #include "SkSize.h" | 12 #include "SkSize.h" |
| 13 | 13 |
| 14 class SkWriteBuffer; | 14 class SkWriteBuffer; |
| 15 class SkReadBuffer; | 15 class SkReadBuffer; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Describes how to interpret the alpha compoent of a pixel. | 18 * Describes how to interpret the alpha compoent of a pixel. |
| 19 */ | 19 */ |
| 20 enum SkAlphaType { | 20 enum SkAlphaType { |
| 21 /** | 21 /** |
| (...skipping 30 matching lines...) Expand all Loading... |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { | 54 static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { |
| 55 SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_o
rder); | 55 SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_o
rder); |
| 56 SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_o
rder); | 56 SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_o
rder); |
| 57 SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype
_order); | 57 SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype
_order); |
| 58 | 58 |
| 59 return (unsigned)at <= kOpaque_SkAlphaType; | 59 return (unsigned)at <= kOpaque_SkAlphaType; |
| 60 } | 60 } |
| 61 | 61 |
| 62 static inline bool SkAlphaTypeIsValid(unsigned value) { |
| 63 return value >= kIgnore_SkAlphaType && value <= kLastEnum_SkAlphaType; |
| 64 } |
| 65 |
| 62 /////////////////////////////////////////////////////////////////////////////// | 66 /////////////////////////////////////////////////////////////////////////////// |
| 63 | 67 |
| 64 /** | 68 /** |
| 65 * Describes how to interpret the components of a pixel. | 69 * Describes how to interpret the components of a pixel. |
| 66 */ | 70 */ |
| 67 enum SkColorType { | 71 enum SkColorType { |
| 72 kUnknown_SkColorType, |
| 68 kAlpha_8_SkColorType, | 73 kAlpha_8_SkColorType, |
| 69 kRGB_565_SkColorType, | 74 kRGB_565_SkColorType, |
| 70 kARGB_4444_SkColorType, | 75 kARGB_4444_SkColorType, |
| 71 kRGBA_8888_SkColorType, | 76 kRGBA_8888_SkColorType, |
| 72 kBGRA_8888_SkColorType, | 77 kBGRA_8888_SkColorType, |
| 73 kIndex_8_SkColorType, | 78 kIndex_8_SkColorType, |
| 74 | 79 |
| 75 kLastEnum_SkColorType = kIndex_8_SkColorType, | 80 kLastEnum_SkColorType = kIndex_8_SkColorType, |
| 76 | 81 |
| 77 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) | 82 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
| 78 kPMColor_SkColorType = kBGRA_8888_SkColorType | 83 kPMColor_SkColorType = kBGRA_8888_SkColorType |
| 79 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) | 84 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
| 80 kPMColor_SkColorType = kRGBA_8888_SkColorType | 85 kPMColor_SkColorType = kRGBA_8888_SkColorType |
| 81 #else | 86 #else |
| 82 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" | 87 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" |
| 83 #endif | 88 #endif |
| 84 }; | 89 }; |
| 85 | 90 |
| 86 static int SkColorTypeBytesPerPixel(SkColorType ct) { | 91 static int SkColorTypeBytesPerPixel(SkColorType ct) { |
| 87 static const uint8_t gSize[] = { | 92 static const uint8_t gSize[] = { |
| 93 0, // Unknown |
| 88 1, // Alpha_8 | 94 1, // Alpha_8 |
| 89 2, // RGB_565 | 95 2, // RGB_565 |
| 90 2, // ARGB_4444 | 96 2, // ARGB_4444 |
| 91 4, // RGBA_8888 | 97 4, // RGBA_8888 |
| 92 4, // BGRA_8888 | 98 4, // BGRA_8888 |
| 93 1, // kIndex_8 | 99 1, // kIndex_8 |
| 94 }; | 100 }; |
| 95 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType +
1), | 101 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType +
1), |
| 96 size_mismatch_with_SkColorType_enum); | 102 size_mismatch_with_SkColorType_enum); |
| 97 | 103 |
| 98 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize)); | 104 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize)); |
| 99 return gSize[ct]; | 105 return gSize[ct]; |
| 100 } | 106 } |
| 101 | 107 |
| 108 static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { |
| 109 return width * SkColorTypeBytesPerPixel(ct); |
| 110 } |
| 111 |
| 112 static inline bool SkColorTypeIsValid(unsigned value) { |
| 113 return value >= kUnknown_SkColorType && value <= kLastEnum_SkColorType; |
| 114 } |
| 115 |
| 102 /////////////////////////////////////////////////////////////////////////////// | 116 /////////////////////////////////////////////////////////////////////////////// |
| 103 | 117 |
| 104 /** | 118 /** |
| 105 * Describe an image's dimensions and pixel type. | 119 * Describe an image's dimensions and pixel type. |
| 106 */ | 120 */ |
| 107 struct SkImageInfo { | 121 struct SkImageInfo { |
| 108 int fWidth; | 122 int fWidth; |
| 109 int fHeight; | 123 int fHeight; |
| 110 SkColorType fColorType; | 124 SkColorType fColorType; |
| 111 SkAlphaType fAlphaType; | 125 SkAlphaType fAlphaType; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 166 |
| 153 static SkImageInfo MakeA8(int width, int height) { | 167 static SkImageInfo MakeA8(int width, int height) { |
| 154 SkASSERT(width >= 0); | 168 SkASSERT(width >= 0); |
| 155 SkASSERT(height >= 0); | 169 SkASSERT(height >= 0); |
| 156 SkImageInfo info = { | 170 SkImageInfo info = { |
| 157 width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType | 171 width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType |
| 158 }; | 172 }; |
| 159 return info; | 173 return info; |
| 160 } | 174 } |
| 161 | 175 |
| 176 int width() const { return fWidth; } |
| 177 int height() const { return fHeight; } |
| 178 SkColorType colorType() const { return fColorType; } |
| 179 SkAlphaType alphaType() const { return fAlphaType; } |
| 180 |
| 181 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; } |
| 182 |
| 162 bool isOpaque() const { | 183 bool isOpaque() const { |
| 163 return SkAlphaTypeIsOpaque(fAlphaType); | 184 return SkAlphaTypeIsOpaque(fAlphaType); |
| 164 } | 185 } |
| 165 | 186 |
| 187 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } |
| 188 |
| 166 int bytesPerPixel() const { | 189 int bytesPerPixel() const { |
| 167 return SkColorTypeBytesPerPixel(fColorType); | 190 return SkColorTypeBytesPerPixel(fColorType); |
| 168 } | 191 } |
| 169 | 192 |
| 193 uint64_t minRowBytes64() const { |
| 194 return sk_64_mul(fWidth, this->bytesPerPixel()); |
| 195 } |
| 196 |
| 170 size_t minRowBytes() const { | 197 size_t minRowBytes() const { |
| 171 return fWidth * this->bytesPerPixel(); | 198 return (size_t)this->minRowBytes64(); |
| 172 } | 199 } |
| 173 | 200 |
| 174 bool operator==(const SkImageInfo& other) const { | 201 bool operator==(const SkImageInfo& other) const { |
| 175 return 0 == memcmp(this, &other, sizeof(other)); | 202 return 0 == memcmp(this, &other, sizeof(other)); |
| 176 } | 203 } |
| 177 bool operator!=(const SkImageInfo& other) const { | 204 bool operator!=(const SkImageInfo& other) const { |
| 178 return 0 != memcmp(this, &other, sizeof(other)); | 205 return 0 != memcmp(this, &other, sizeof(other)); |
| 179 } | 206 } |
| 180 | 207 |
| 181 void unflatten(SkReadBuffer&); | 208 void unflatten(SkReadBuffer&); |
| 182 void flatten(SkWriteBuffer&) const; | 209 void flatten(SkWriteBuffer&) const; |
| 183 | 210 |
| 184 size_t getSafeSize(size_t rowBytes) const { | 211 int64_t getSafeSize64(size_t rowBytes) const { |
| 185 if (0 == fHeight) { | 212 if (0 == fHeight) { |
| 186 return 0; | 213 return 0; |
| 187 } | 214 } |
| 188 return (fHeight - 1) * rowBytes + fWidth * this->bytesPerPixel(); | 215 return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel()
; |
| 189 } | 216 } |
| 217 |
| 218 size_t getSafeSize(size_t rowBytes) const { |
| 219 return (size_t)this->getSafeSize64(rowBytes); |
| 220 } |
| 221 |
| 222 bool validRowBytes(size_t rowBytes) const { |
| 223 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel()); |
| 224 return rowBytes >= rb; |
| 225 } |
| 226 |
| 227 SkDEBUGCODE(void validate() const;) |
| 190 }; | 228 }; |
| 191 | 229 |
| 192 #endif | 230 #endif |
| OLD | NEW |