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