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