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 | |
| 11 | #include "SkImageInfo.h" |
| 12 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 13 | enum SkColorTypeComponentFlag { |
| 14 | kRed_SkColorTypeComponentFlag = 0x1, |
| 15 | kGreen_SkColorTypeComponentFlag = 0x2, |
| 16 | kBlue_SkColorTypeComponentFlag = 0x4, |
| 17 | kAlpha_SkColorTypeComponentFlag = 0x8, |
| 18 | kGray_SkColorTypeComponentFlag = 0x10, |
| 19 | kRGB_SkColorTypeComponentFlags = kRed_SkColorTypeComponentFlag | |
| 20 | kGreen_SkColorTypeComponentFlag | |
| 21 | kBlue_SkColorTypeComponentFlag, |
| 22 | kRGBA_SkColorTypeComponentFlags = kRGB_SkColorTypeComponentFlags | |
| 23 | kAlpha_SkColorTypeComponentFlag, |
| 24 | }; |
| 25 | |
| 26 | static inline uint32_t SkColorTypeComponentFlags(SkColorType ct) { |
| 27 | switch (ct) { |
| 28 | case kUnknown_SkColorType: return 0; |
| 29 | case kAlpha_8_SkColorType: return kAlpha_SkColorTypeComponentFlag; |
| 30 | case kRGB_565_SkColorType: return kRGB_SkColorTypeComponentFlags; |
| 31 | case kARGB_4444_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
| 32 | case kRGBA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
| 33 | case kRGB_888x_SkColorType: return kRGB_SkColorTypeComponentFlags; |
| 34 | case kBGRA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
| 35 | case kRGBA_1010102_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
| 36 | case kRGB_101010x_SkColorType: return kRGB_SkColorTypeComponentFlags; |
| 37 | case kGray_8_SkColorType: return kGray_SkColorTypeComponentFlag; |
| 38 | case kRGBA_F16_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 39 | case kRGBA_F32_SkColorType: return kRGBA_SkColorTypeComponentFlags; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) { |
| 45 | return kAlpha_SkColorTypeComponentFlag == SkColorTypeComponentFlags(ct); |
| 46 | } |
| 47 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 48 | static inline bool SkAlphaTypeIsValid(unsigned value) { |
| 49 | return value <= kLastEnum_SkAlphaType; |
| 50 | } |
| 51 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 52 | static inline bool SkColorTypeIsGray(SkColorType ct) { |
| 53 | auto flags = SkColorTypeComponentFlags(ct); |
| 54 | // Currently assuming that a color type has only gray or does not have gray. |
| 55 | SkASSERT(!(kGray_SkColorTypeComponentFlag & flags) || kGray_SkColorTypeComponentFlag == flags); |
| 56 | return kGray_SkColorTypeComponentFlag == flags; |
| 57 | } |
| 58 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 59 | static int SkColorTypeShiftPerPixel(SkColorType ct) { |
| 60 | switch (ct) { |
| 61 | case kUnknown_SkColorType: return 0; |
| 62 | case kAlpha_8_SkColorType: return 0; |
| 63 | case kRGB_565_SkColorType: return 1; |
| 64 | case kARGB_4444_SkColorType: return 1; |
| 65 | case kRGBA_8888_SkColorType: return 2; |
| 66 | case kRGB_888x_SkColorType: return 2; |
| 67 | case kBGRA_8888_SkColorType: return 2; |
| 68 | case kRGBA_1010102_SkColorType: return 2; |
| 69 | case kRGB_101010x_SkColorType: return 2; |
| 70 | case kGray_8_SkColorType: return 0; |
| 71 | case kRGBA_F16_SkColorType: return 3; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 72 | case kRGBA_F32_SkColorType: return 4; |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { |
| 78 | return width * SkColorTypeBytesPerPixel(ct); |
| 79 | } |
| 80 | |
| 81 | static inline bool SkColorTypeIsValid(unsigned value) { |
| 82 | return value <= kLastEnum_SkColorType; |
| 83 | } |
| 84 | |
| 85 | static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) { |
| 86 | if (kUnknown_SkColorType == ct) { |
| 87 | return 0; |
| 88 | } |
| 89 | return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct)); |
| 90 | } |
| 91 | |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 92 | /** |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 93 | * Returns true if |info| contains a valid combination of width, height, colorType, and alphaType. |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 94 | */ |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 95 | static inline bool SkImageInfoIsValid(const SkImageInfo& info) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 96 | if (info.width() <= 0 || info.height() <= 0) { |
| 97 | return false; |
| 98 | } |
| 99 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 100 | const int kMaxDimension = SK_MaxS32 >> 2; |
| 101 | if (info.width() > kMaxDimension || info.height() > kMaxDimension) { |
| 102 | return false; |
| 103 | } |
| 104 | |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 105 | if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) { |
| 106 | return false; |
| 107 | } |
| 108 | |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
| 112 | /** |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 113 | * 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] | 114 | * Returns false otherwise. |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 115 | */ |
| 116 | static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) { |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 117 | return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src); |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 118 | } |
Hal Canary | 6b20a55 | 2017-02-07 14:09:38 -0500 | [diff] [blame] | 119 | #endif // SkImageInfoPriv_DEFINED |