Skip to content

Commit cff5fb8

Browse files
committed
video: pixel_format: Add compare helpers
Add helpers that compare two pixel-format descriptions against each other. Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> Link: https://lore.kernel.org/r/20250714151513.309475-2-tzimmermann@suse.de
1 parent cbdd16b commit cff5fb8

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

include/video/pixel_format.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,62 @@ struct pixel_format {
3838
#define PIXEL_FORMAT_XRGB2101010 \
3939
{ 32, false, { .alpha = {0, 0}, .red = {20, 10}, .green = {10, 10}, .blue = {0, 10} } }
4040

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+
4199
#endif

0 commit comments

Comments
 (0)