blob: 9053f41696de8d4c47cd6385b4cf447eb77f8399 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImageInfo.h"
Hal Canary6b20a552017-02-07 14:09:38 -050012
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) {
Mike Kleinb70990e2019-02-28 10:03:27 -060028 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_F16Norm_SkColorType: return kRGBA_SkColorTypeComponentFlags;
39 case kRGBA_F16_SkColorType: return kRGBA_SkColorTypeComponentFlags;
40 case kRGBA_F32_SkColorType: return kRGBA_SkColorTypeComponentFlags;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040041 }
42 return 0;
43}
44
45static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
46 return kAlpha_SkColorTypeComponentFlag == SkColorTypeComponentFlags(ct);
47}
48
Mike Reed7fcfb622018-02-09 13:26:46 -050049static inline bool SkAlphaTypeIsValid(unsigned value) {
50 return value <= kLastEnum_SkAlphaType;
51}
52
Brian Salomon5fba7ad2018-03-22 10:01:16 -040053static inline bool SkColorTypeIsGray(SkColorType ct) {
54 auto flags = SkColorTypeComponentFlags(ct);
55 // Currently assuming that a color type has only gray or does not have gray.
56 SkASSERT(!(kGray_SkColorTypeComponentFlag & flags) || kGray_SkColorTypeComponentFlag == flags);
57 return kGray_SkColorTypeComponentFlag == flags;
58}
59
Mike Reed7fcfb622018-02-09 13:26:46 -050060static int SkColorTypeShiftPerPixel(SkColorType ct) {
61 switch (ct) {
Mike Kleinb70990e2019-02-28 10:03:27 -060062 case kUnknown_SkColorType: return 0;
63 case kAlpha_8_SkColorType: return 0;
64 case kRGB_565_SkColorType: return 1;
65 case kARGB_4444_SkColorType: return 1;
66 case kRGBA_8888_SkColorType: return 2;
67 case kRGB_888x_SkColorType: return 2;
68 case kBGRA_8888_SkColorType: return 2;
69 case kRGBA_1010102_SkColorType: return 2;
70 case kRGB_101010x_SkColorType: return 2;
71 case kGray_8_SkColorType: return 0;
72 case kRGBA_F16Norm_SkColorType: return 3;
73 case kRGBA_F16_SkColorType: return 3;
74 case kRGBA_F32_SkColorType: return 4;
Mike Reed7fcfb622018-02-09 13:26:46 -050075 }
76 return 0;
77}
78
79static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
80 return width * SkColorTypeBytesPerPixel(ct);
81}
82
83static inline bool SkColorTypeIsValid(unsigned value) {
84 return value <= kLastEnum_SkColorType;
85}
86
87static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
88 if (kUnknown_SkColorType == ct) {
89 return 0;
90 }
91 return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
92}
93
Matt Sarettcb6266b2017-01-17 10:48:53 -050094/**
Brian Osmane1adc3a2018-06-04 09:21:17 -040095 * Returns true if |info| contains a valid combination of width, height, colorType, and alphaType.
Matt Sarettcb6266b2017-01-17 10:48:53 -050096 */
Brian Osmane1adc3a2018-06-04 09:21:17 -040097static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
Matt Sarettcb6266b2017-01-17 10:48:53 -050098 if (info.width() <= 0 || info.height() <= 0) {
99 return false;
100 }
101
Matt Sarett8572d852017-02-14 11:21:02 -0500102 const int kMaxDimension = SK_MaxS32 >> 2;
103 if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
104 return false;
105 }
106
Matt Sarettcb6266b2017-01-17 10:48:53 -0500107 if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
108 return false;
109 }
110
Matt Sarett26b44df2017-05-02 16:04:56 -0400111 return true;
112}
113
114/**
Matt Sarettcb6266b2017-01-17 10:48:53 -0500115 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
Mike Klein12d4b6e2018-08-17 14:09:55 -0400116 * Returns false otherwise.
Matt Sarettcb6266b2017-01-17 10:48:53 -0500117 */
118static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400119 return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500120}
Hal Canary6b20a552017-02-07 14:09:38 -0500121#endif // SkImageInfoPriv_DEFINED