pixel_format.h 2.7 KB

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