SkImageInfoPriv.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef SkImageInfoPriv_DEFINED
  8. #define SkImageInfoPriv_DEFINED
  9. #include "SkImageInfo.h"
  10. enum SkColorTypeComponentFlag {
  11. kRed_SkColorTypeComponentFlag = 0x1,
  12. kGreen_SkColorTypeComponentFlag = 0x2,
  13. kBlue_SkColorTypeComponentFlag = 0x4,
  14. kAlpha_SkColorTypeComponentFlag = 0x8,
  15. kGray_SkColorTypeComponentFlag = 0x10,
  16. kRGB_SkColorTypeComponentFlags = kRed_SkColorTypeComponentFlag |
  17. kGreen_SkColorTypeComponentFlag |
  18. kBlue_SkColorTypeComponentFlag,
  19. kRGBA_SkColorTypeComponentFlags = kRGB_SkColorTypeComponentFlags |
  20. kAlpha_SkColorTypeComponentFlag,
  21. };
  22. static inline uint32_t SkColorTypeComponentFlags(SkColorType ct) {
  23. switch (ct) {
  24. case kUnknown_SkColorType: return 0;
  25. case kAlpha_8_SkColorType: return kAlpha_SkColorTypeComponentFlag;
  26. case kRGB_565_SkColorType: return kRGB_SkColorTypeComponentFlags;
  27. case kARGB_4444_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  28. case kRGBA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  29. case kRGB_888x_SkColorType: return kRGB_SkColorTypeComponentFlags;
  30. case kBGRA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  31. case kRGBA_1010102_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  32. case kRGB_101010x_SkColorType: return kRGB_SkColorTypeComponentFlags;
  33. case kGray_8_SkColorType: return kGray_SkColorTypeComponentFlag;
  34. case kRGBA_F16_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  35. case kRGBA_F32_SkColorType: return kRGBA_SkColorTypeComponentFlags;
  36. }
  37. return 0;
  38. }
  39. static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
  40. return kAlpha_SkColorTypeComponentFlag == SkColorTypeComponentFlags(ct);
  41. }
  42. static inline bool SkAlphaTypeIsValid(unsigned value) {
  43. return value <= kLastEnum_SkAlphaType;
  44. }
  45. static inline bool SkColorTypeIsGray(SkColorType ct) {
  46. auto flags = SkColorTypeComponentFlags(ct);
  47. // Currently assuming that a color type has only gray or does not have gray.
  48. SkASSERT(!(kGray_SkColorTypeComponentFlag & flags) || kGray_SkColorTypeComponentFlag == flags);
  49. return kGray_SkColorTypeComponentFlag == flags;
  50. }
  51. static int SkColorTypeShiftPerPixel(SkColorType ct) {
  52. switch (ct) {
  53. case kUnknown_SkColorType: return 0;
  54. case kAlpha_8_SkColorType: return 0;
  55. case kRGB_565_SkColorType: return 1;
  56. case kARGB_4444_SkColorType: return 1;
  57. case kRGBA_8888_SkColorType: return 2;
  58. case kRGB_888x_SkColorType: return 2;
  59. case kBGRA_8888_SkColorType: return 2;
  60. case kRGBA_1010102_SkColorType: return 2;
  61. case kRGB_101010x_SkColorType: return 2;
  62. case kGray_8_SkColorType: return 0;
  63. case kRGBA_F16_SkColorType: return 3;
  64. case kRGBA_F32_SkColorType: return 4;
  65. }
  66. return 0;
  67. }
  68. static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
  69. return width * SkColorTypeBytesPerPixel(ct);
  70. }
  71. static inline bool SkColorTypeIsValid(unsigned value) {
  72. return value <= kLastEnum_SkColorType;
  73. }
  74. static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
  75. if (kUnknown_SkColorType == ct) {
  76. return 0;
  77. }
  78. return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
  79. }
  80. /**
  81. * Returns true if |info| contains a valid combination of width, height, colorType, and alphaType.
  82. */
  83. static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
  84. if (info.width() <= 0 || info.height() <= 0) {
  85. return false;
  86. }
  87. const int kMaxDimension = SK_MaxS32 >> 2;
  88. if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
  89. return false;
  90. }
  91. if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
  98. * Returns false otherwise.
  99. */
  100. static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
  101. return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
  102. }
  103. #endif // SkImageInfoPriv_DEFINED