xref: /linux/include/video/pixel_format.h (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef VIDEO_PIXEL_FORMAT_H
4 #define VIDEO_PIXEL_FORMAT_H
5 
6 struct pixel_format {
7 	unsigned char bits_per_pixel;
8 	bool indexed;
9 	union {
10 		struct {
11 			struct {
12 				unsigned char offset;
13 				unsigned char length;
14 			} alpha, red, green, blue;
15 		};
16 		struct {
17 			unsigned char offset;
18 			unsigned char length;
19 		} index;
20 	};
21 };
22 
23 #define PIXEL_FORMAT_C8 \
24 	{ 8, true, { .index = {0, 8}, } }
25 
26 #define PIXEL_FORMAT_XRGB1555 \
27 	{ 16, false, { .alpha = {0, 0}, .red = {10, 5}, .green = {5, 5}, .blue = {0, 5} } }
28 
29 #define PIXEL_FORMAT_RGB565 \
30 	{ 16, false, { .alpha = {0, 0}, .red = {11, 5}, .green = {5, 6}, .blue = {0, 5} } }
31 
32 #define PIXEL_FORMAT_RGB888 \
33 	{ 24, false, { .alpha = {0, 0}, .red = {16, 8}, .green = {8, 8}, .blue = {0, 8} } }
34 
35 #define PIXEL_FORMAT_XRGB8888 \
36 	{ 32, false, { .alpha = {0, 0}, .red = {16, 8}, .green = {8, 8}, .blue = {0, 8} } }
37 
38 #define PIXEL_FORMAT_XBGR8888 \
39 	{ 32, false, { .alpha = {0, 0}, .red = {0, 8}, .green = {8, 8}, .blue = {16, 8} } }
40 
41 #define PIXEL_FORMAT_XRGB2101010 \
42 	{ 32, false, { .alpha = {0, 0}, .red = {20, 10}, .green = {10, 10}, .blue = {0, 10} } }
43 
44 #define __pixel_format_cmp_field(lhs, rhs, name) \
45 	{ \
46 		int ret = ((lhs)->name) - ((rhs)->name); \
47 		if (ret) \
48 			return ret; \
49 	}
50 
51 #define __pixel_format_cmp_bitfield(lhs, rhs, name) \
52 	{ \
53 		__pixel_format_cmp_field(lhs, rhs, name.offset); \
54 		__pixel_format_cmp_field(lhs, rhs, name.length); \
55 	}
56 
57 /**
58  * pixel_format_cmp - Compares two pixel-format descriptions
59  *
60  * @lhs: a pixel-format description
61  * @rhs: a pixel-format description
62  *
63  * Compares two pixel-format descriptions for their order. The semantics
64  * are equivalent to memcmp().
65  *
66  * Returns:
67  * 0 if both arguments describe the same pixel format, less-than-zero if lhs < rhs,
68  * or greater-than-zero if lhs > rhs.
69  */
70 static inline int pixel_format_cmp(const struct pixel_format *lhs, const struct pixel_format *rhs)
71 {
72 	__pixel_format_cmp_field(lhs, rhs, bits_per_pixel);
73 	__pixel_format_cmp_field(lhs, rhs, indexed);
74 
75 	if (lhs->indexed) {
76 		__pixel_format_cmp_bitfield(lhs, rhs, index);
77 	} else {
78 		__pixel_format_cmp_bitfield(lhs, rhs, alpha);
79 		__pixel_format_cmp_bitfield(lhs, rhs, red);
80 		__pixel_format_cmp_bitfield(lhs, rhs, green);
81 		__pixel_format_cmp_bitfield(lhs, rhs, blue);
82 	}
83 
84 	return 0;
85 }
86 
87 /**
88  * pixel_format_equal - Compares two pixel-format descriptions for equality
89  *
90  * @lhs: a pixel-format description
91  * @rhs: a pixel-format description
92  *
93  * Returns:
94  * True if both arguments describe the same pixel format, or false otherwise.
95  */
96 static inline bool pixel_format_equal(const struct pixel_format *lhs,
97 				      const struct pixel_format *rhs)
98 {
99 	return !pixel_format_cmp(lhs, rhs);
100 }
101 
102 #endif
103