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 | |
| 11 | #include "SkTypes.h" |
| 12 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 13 | class SkFlattenableWriteBuffer; |
| 14 | class SkFlattenableReadBuffer; |
| 15 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Describes how to interpret the alpha compoent of a pixel. |
| 18 | */ |
| 19 | enum SkAlphaType { |
| 20 | /** |
| 21 | * All pixels should be treated as opaque, regardless of the value stored |
| 22 | * in their alpha field. Used for legacy images that wrote 0 or garbarge |
| 23 | * in their alpha field, but intended the RGB to be treated as opaque. |
| 24 | */ |
| 25 | kIgnore_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 26 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 27 | /** |
| 28 | * All pixels are stored as opaque. This differs slightly from kIgnore in |
| 29 | * that kOpaque has correct "opaque" values stored in the pixels, while |
| 30 | * kIgnore may not, but in both cases the caller should treat the pixels |
| 31 | * as opaque. |
| 32 | */ |
| 33 | kOpaque_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 34 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 35 | /** |
| 36 | * All pixels have their alpha premultiplied in their color components. |
| 37 | * This is the natural format for the rendering target pixels. |
| 38 | */ |
| 39 | kPremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 40 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 41 | /** |
| 42 | * All pixels have their color components stored without any regard to the |
| 43 | * alpha. e.g. this is the default configuration for PNG images. |
| 44 | * |
| 45 | * This alpha-type is ONLY supported for input images. Rendering cannot |
| 46 | * generate this on output. |
| 47 | */ |
| 48 | kUnpremul_SkAlphaType, |
skia.committer@gmail.com | 73a5d53 | 2013-11-14 07:02:31 +0000 | [diff] [blame] | 49 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 50 | kLastEnum_SkAlphaType = kUnpremul_SkAlphaType |
| 51 | }; |
| 52 | |
| 53 | static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { |
| 54 | SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order); |
| 55 | SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); |
| 56 | SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); |
| 57 | |
| 58 | return (unsigned)at <= kOpaque_SkAlphaType; |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | /** |
| 64 | * Describes how to interpret the components of a pixel. |
| 65 | */ |
| 66 | enum SkColorType { |
| 67 | kAlpha_8_SkColorType, |
| 68 | kRGB_565_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 69 | kARGB_4444_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 70 | kRGBA_8888_SkColorType, |
| 71 | kBGRA_8888_SkColorType, |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 72 | kIndex_8_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 73 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 74 | kLastEnum_SkColorType = kIndex_8_SkColorType, |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 75 | |
| 76 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
| 77 | kPMColor_SkColorType = kBGRA_8888_SkColorType |
| 78 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
| 79 | kPMColor_SkColorType = kRGBA_8888_SkColorType |
| 80 | #else |
| 81 | #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" |
| 82 | #endif |
| 83 | }; |
| 84 | |
| 85 | static int SkColorTypeBytesPerPixel(SkColorType ct) { |
| 86 | static const uint8_t gSize[] = { |
| 87 | 1, // Alpha_8 |
| 88 | 2, // RGB_565 |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 89 | 2, // ARGB_4444 |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 90 | 4, // RGBA_8888 |
| 91 | 4, // BGRA_8888 |
| 92 | 1, // kIndex_8 |
| 93 | }; |
| 94 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1), |
| 95 | size_mismatch_with_SkColorType_enum); |
| 96 | |
| 97 | SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize)); |
| 98 | return gSize[ct]; |
| 99 | } |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | /** |
| 104 | * Describe an image's dimensions and pixel type. |
| 105 | */ |
| 106 | struct SkImageInfo { |
| 107 | int fWidth; |
| 108 | int fHeight; |
| 109 | SkColorType fColorType; |
| 110 | SkAlphaType fAlphaType; |
| 111 | |
| 112 | bool isOpaque() const { |
| 113 | return SkAlphaTypeIsOpaque(fAlphaType); |
| 114 | } |
| 115 | |
| 116 | int bytesPerPixel() const { |
| 117 | return SkColorTypeBytesPerPixel(fColorType); |
| 118 | } |
| 119 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 120 | size_t minRowBytes() const { |
| 121 | return fWidth * this->bytesPerPixel(); |
| 122 | } |
| 123 | |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 124 | bool operator==(const SkImageInfo& other) const { |
| 125 | return 0 == memcmp(this, &other, sizeof(other)); |
| 126 | } |
| 127 | bool operator!=(const SkImageInfo& other) const { |
| 128 | return 0 != memcmp(this, &other, sizeof(other)); |
| 129 | } |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame^] | 130 | |
| 131 | void unflatten(SkFlattenableReadBuffer&); |
| 132 | void flatten(SkFlattenableWriteBuffer&) const; |
| 133 | |
| 134 | size_t getSafeSize(size_t rowBytes) const { |
| 135 | if (0 == fHeight) { |
| 136 | return 0; |
| 137 | } |
| 138 | return (fHeight - 1) * rowBytes + fWidth * this->bytesPerPixel(); |
| 139 | } |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #endif |