Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Hal Canary | 6b20a55 | 2017-02-07 14:09:38 -0500 | [diff] [blame] | 8 | #ifndef SkImageInfoPriv_DEFINED |
| 9 | #define SkImageInfoPriv_DEFINED |
| 10 | |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImageInfo.h" |
Hal Canary | 6b20a55 | 2017-02-07 14:09:38 -0500 | [diff] [blame] | 13 | |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 14 | static inline uint32_t SkColorTypeChannelFlags(SkColorType ct) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 15 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 16 | case kUnknown_SkColorType: return 0; |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 17 | case kAlpha_8_SkColorType: return kAlpha_SkColorChannelFlag; |
| 18 | case kRGB_565_SkColorType: return kRGB_SkColorChannelFlags; |
| 19 | case kARGB_4444_SkColorType: return kRGBA_SkColorChannelFlags; |
| 20 | case kRGBA_8888_SkColorType: return kRGBA_SkColorChannelFlags; |
| 21 | case kRGB_888x_SkColorType: return kRGB_SkColorChannelFlags; |
| 22 | case kBGRA_8888_SkColorType: return kRGBA_SkColorChannelFlags; |
| 23 | case kRGBA_1010102_SkColorType: return kRGBA_SkColorChannelFlags; |
| 24 | case kRGB_101010x_SkColorType: return kRGB_SkColorChannelFlags; |
| 25 | case kBGRA_1010102_SkColorType: return kRGBA_SkColorChannelFlags; |
| 26 | case kBGR_101010x_SkColorType: return kRGB_SkColorChannelFlags; |
| 27 | case kGray_8_SkColorType: return kGray_SkColorChannelFlag; |
| 28 | case kRGBA_F16Norm_SkColorType: return kRGBA_SkColorChannelFlags; |
| 29 | case kRGBA_F16_SkColorType: return kRGBA_SkColorChannelFlags; |
| 30 | case kRGBA_F32_SkColorType: return kRGBA_SkColorChannelFlags; |
| 31 | case kR8G8_unorm_SkColorType: return kRG_SkColorChannelFlags; |
| 32 | case kA16_unorm_SkColorType: return kAlpha_SkColorChannelFlag; |
| 33 | case kR16G16_unorm_SkColorType: return kRG_SkColorChannelFlags; |
| 34 | case kA16_float_SkColorType: return kAlpha_SkColorChannelFlag; |
| 35 | case kR16G16_float_SkColorType: return kRG_SkColorChannelFlags; |
| 36 | case kR16G16B16A16_unorm_SkColorType: return kRGBA_SkColorChannelFlags; |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 37 | case kSRGBA_8888_SkColorType: return kRGBA_SkColorChannelFlags; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 38 | } |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 39 | SkUNREACHABLE; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) { |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 43 | return SkColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 46 | static inline bool SkAlphaTypeIsValid(unsigned value) { |
| 47 | return value <= kLastEnum_SkAlphaType; |
| 48 | } |
| 49 | |
| 50 | static int SkColorTypeShiftPerPixel(SkColorType ct) { |
| 51 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 52 | case kUnknown_SkColorType: return 0; |
| 53 | case kAlpha_8_SkColorType: return 0; |
| 54 | case kRGB_565_SkColorType: return 1; |
| 55 | case kARGB_4444_SkColorType: return 1; |
| 56 | case kRGBA_8888_SkColorType: return 2; |
| 57 | case kRGB_888x_SkColorType: return 2; |
| 58 | case kBGRA_8888_SkColorType: return 2; |
| 59 | case kRGBA_1010102_SkColorType: return 2; |
| 60 | case kRGB_101010x_SkColorType: return 2; |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 61 | case kBGRA_1010102_SkColorType: return 2; |
| 62 | case kBGR_101010x_SkColorType: return 2; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 63 | case kGray_8_SkColorType: return 0; |
| 64 | case kRGBA_F16Norm_SkColorType: return 3; |
| 65 | case kRGBA_F16_SkColorType: return 3; |
| 66 | case kRGBA_F32_SkColorType: return 4; |
| 67 | case kR8G8_unorm_SkColorType: return 1; |
| 68 | case kA16_unorm_SkColorType: return 1; |
| 69 | case kR16G16_unorm_SkColorType: return 2; |
| 70 | case kA16_float_SkColorType: return 1; |
| 71 | case kR16G16_float_SkColorType: return 2; |
| 72 | case kR16G16B16A16_unorm_SkColorType: return 3; |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 73 | case kSRGBA_8888_SkColorType: return 2; |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 74 | } |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 75 | SkUNREACHABLE; |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { |
Mike Klein | 89c909e | 2020-06-01 15:50:19 -0500 | [diff] [blame] | 79 | return (size_t)(width * SkColorTypeBytesPerPixel(ct)); |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static inline bool SkColorTypeIsValid(unsigned value) { |
| 83 | return value <= kLastEnum_SkColorType; |
| 84 | } |
| 85 | |
| 86 | static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) { |
| 87 | if (kUnknown_SkColorType == ct) { |
| 88 | return 0; |
| 89 | } |
Mike Klein | 89c909e | 2020-06-01 15:50:19 -0500 | [diff] [blame] | 90 | return (size_t)y * rowBytes + ((size_t)x << SkColorTypeShiftPerPixel(ct)); |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Mike Klein | b2ab7d1 | 2019-11-08 10:42:13 -0600 | [diff] [blame] | 93 | static inline bool SkColorTypeIsNormalized(SkColorType ct) { |
| 94 | switch (ct) { |
| 95 | case kUnknown_SkColorType: |
| 96 | case kAlpha_8_SkColorType: |
| 97 | case kRGB_565_SkColorType: |
| 98 | case kARGB_4444_SkColorType: |
| 99 | case kRGBA_8888_SkColorType: |
| 100 | case kRGB_888x_SkColorType: |
| 101 | case kBGRA_8888_SkColorType: |
| 102 | case kRGBA_1010102_SkColorType: |
| 103 | case kRGB_101010x_SkColorType: |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 104 | case kBGRA_1010102_SkColorType: |
| 105 | case kBGR_101010x_SkColorType: |
Mike Klein | b2ab7d1 | 2019-11-08 10:42:13 -0600 | [diff] [blame] | 106 | case kGray_8_SkColorType: |
| 107 | case kRGBA_F16Norm_SkColorType: |
| 108 | case kR8G8_unorm_SkColorType: |
| 109 | case kA16_unorm_SkColorType: |
| 110 | case kA16_float_SkColorType: /*subtle... alpha is always [0,1]*/ |
| 111 | case kR16G16_unorm_SkColorType: |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 112 | case kR16G16B16A16_unorm_SkColorType: |
| 113 | case kSRGBA_8888_SkColorType: return true; |
Mike Klein | b2ab7d1 | 2019-11-08 10:42:13 -0600 | [diff] [blame] | 114 | |
| 115 | case kRGBA_F16_SkColorType: |
| 116 | case kRGBA_F32_SkColorType: |
| 117 | case kR16G16_float_SkColorType: return false; |
| 118 | } |
| 119 | SkUNREACHABLE; |
| 120 | } |
| 121 | |
Mike Reed | b9bd12d | 2021-03-04 08:13:56 -0500 | [diff] [blame] | 122 | static inline int SkColorTypeMaxBitsPerChannel(SkColorType ct) { |
| 123 | switch (ct) { |
| 124 | case kUnknown_SkColorType: |
| 125 | return 0; |
| 126 | |
| 127 | case kARGB_4444_SkColorType: |
| 128 | return 4; |
| 129 | |
| 130 | case kRGB_565_SkColorType: |
| 131 | return 6; |
| 132 | |
| 133 | case kAlpha_8_SkColorType: |
| 134 | case kRGBA_8888_SkColorType: |
| 135 | case kRGB_888x_SkColorType: |
| 136 | case kBGRA_8888_SkColorType: |
| 137 | case kGray_8_SkColorType: |
| 138 | case kR8G8_unorm_SkColorType: |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 139 | case kSRGBA_8888_SkColorType: |
Mike Reed | b9bd12d | 2021-03-04 08:13:56 -0500 | [diff] [blame] | 140 | return 8; |
| 141 | |
| 142 | case kRGBA_1010102_SkColorType: |
| 143 | case kRGB_101010x_SkColorType: |
| 144 | case kBGRA_1010102_SkColorType: |
| 145 | case kBGR_101010x_SkColorType: |
| 146 | return 10; |
| 147 | |
| 148 | case kRGBA_F16Norm_SkColorType: |
| 149 | case kA16_unorm_SkColorType: |
| 150 | case kA16_float_SkColorType: |
| 151 | case kR16G16_unorm_SkColorType: |
| 152 | case kR16G16B16A16_unorm_SkColorType: |
| 153 | case kRGBA_F16_SkColorType: |
| 154 | case kR16G16_float_SkColorType: |
| 155 | return 16; |
| 156 | |
| 157 | case kRGBA_F32_SkColorType: |
| 158 | return 32; |
| 159 | } |
| 160 | SkUNREACHABLE; |
| 161 | } |
| 162 | |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 163 | /** |
Brian Salomon | 2cae3b0 | 2020-03-27 11:18:08 -0400 | [diff] [blame] | 164 | * Returns true if |info| contains a valid colorType and alphaType. |
| 165 | */ |
| 166 | static inline bool SkColorInfoIsValid(const SkColorInfo& info) { |
| 167 | return info.colorType() != kUnknown_SkColorType && info.alphaType() != kUnknown_SkAlphaType; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns true if |info| contains a valid combination of width, height and colorInfo. |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 172 | */ |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 173 | static inline bool SkImageInfoIsValid(const SkImageInfo& info) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 174 | if (info.width() <= 0 || info.height() <= 0) { |
| 175 | return false; |
| 176 | } |
| 177 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 178 | const int kMaxDimension = SK_MaxS32 >> 2; |
| 179 | if (info.width() > kMaxDimension || info.height() > kMaxDimension) { |
| 180 | return false; |
| 181 | } |
| 182 | |
Brian Salomon | 2cae3b0 | 2020-03-27 11:18:08 -0400 | [diff] [blame] | 183 | return SkColorInfoIsValid(info.colorInfo()); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 187 | * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|. |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 188 | * Returns false otherwise. |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 189 | */ |
| 190 | static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) { |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 191 | return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src); |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 192 | } |
Hal Canary | 6b20a55 | 2017-02-07 14:09:38 -0500 | [diff] [blame] | 193 | #endif // SkImageInfoPriv_DEFINED |