blob: aad9e791b997f2f19dc10001d245a914d62a97d7 [file] [log] [blame]
Matt Sarettcb6266b2017-01-17 10:48:53 -05001/*
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 Canary6b20a552017-02-07 14:09:38 -05008#ifndef SkImageInfoPriv_DEFINED
9#define SkImageInfoPriv_DEFINED
10
11#include "SkImageInfo.h"
12
Brian Salomon5fba7ad2018-03-22 10:01:16 -040013enum 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
26static 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 Klein37854712018-06-26 11:43:06 -040039 case kRGBA_F32_SkColorType: return kRGBA_SkColorTypeComponentFlags;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040040 }
41 return 0;
42}
43
44static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
45 return kAlpha_SkColorTypeComponentFlag == SkColorTypeComponentFlags(ct);
46}
47
Mike Reed7fcfb622018-02-09 13:26:46 -050048static inline bool SkAlphaTypeIsValid(unsigned value) {
49 return value <= kLastEnum_SkAlphaType;
50}
51
Brian Salomon5fba7ad2018-03-22 10:01:16 -040052static 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 Reed7fcfb622018-02-09 13:26:46 -050059static 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 Klein37854712018-06-26 11:43:06 -040072 case kRGBA_F32_SkColorType: return 4;
Mike Reed7fcfb622018-02-09 13:26:46 -050073 }
74 return 0;
75}
76
77static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
78 return width * SkColorTypeBytesPerPixel(ct);
79}
80
81static inline bool SkColorTypeIsValid(unsigned value) {
82 return value <= kLastEnum_SkColorType;
83}
84
85static 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 Sarettcb6266b2017-01-17 10:48:53 -050092/**
Brian Osmane1adc3a2018-06-04 09:21:17 -040093 * Returns true if |info| contains a valid combination of width, height, colorType, and alphaType.
Matt Sarettcb6266b2017-01-17 10:48:53 -050094 */
Brian Osmane1adc3a2018-06-04 09:21:17 -040095static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
Matt Sarettcb6266b2017-01-17 10:48:53 -050096 if (info.width() <= 0 || info.height() <= 0) {
97 return false;
98 }
99
Matt Sarett8572d852017-02-14 11:21:02 -0500100 const int kMaxDimension = SK_MaxS32 >> 2;
101 if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
102 return false;
103 }
104
Matt Sarettcb6266b2017-01-17 10:48:53 -0500105 if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
106 return false;
107 }
108
109 if (kOpaque_SkAlphaType != info.alphaType() &&
110 (kRGB_565_SkColorType == info.colorType() || kGray_8_SkColorType == info.colorType())) {
111 return false;
112 }
113
Matt Sarett26b44df2017-05-02 16:04:56 -0400114 return true;
115}
116
117/**
Matt Sarettcb6266b2017-01-17 10:48:53 -0500118 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
Mike Klein12d4b6e2018-08-17 14:09:55 -0400119 * Returns false otherwise.
Matt Sarettcb6266b2017-01-17 10:48:53 -0500120 */
121static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400122 return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500123}
Hal Canary6b20a552017-02-07 14:09:38 -0500124#endif // SkImageInfoPriv_DEFINED