sharpyuv.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2022 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Sharp RGB to YUV conversion.
  11. #ifndef WEBP_SHARPYUV_SHARPYUV_H_
  12. #define WEBP_SHARPYUV_SHARPYUV_H_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifndef SHARPYUV_EXTERN
  17. #ifdef WEBP_EXTERN
  18. #define SHARPYUV_EXTERN WEBP_EXTERN
  19. #else
  20. // This explicitly marks library functions and allows for changing the
  21. // signature for e.g., Windows DLL builds.
  22. #if defined(__GNUC__) && __GNUC__ >= 4
  23. #define SHARPYUV_EXTERN extern __attribute__((visibility("default")))
  24. #else
  25. #if defined(_MSC_VER) && defined(WEBP_DLL)
  26. #define SHARPYUV_EXTERN __declspec(dllexport)
  27. #else
  28. #define SHARPYUV_EXTERN extern
  29. #endif /* _MSC_VER && WEBP_DLL */
  30. #endif /* __GNUC__ >= 4 */
  31. #endif /* WEBP_EXTERN */
  32. #endif /* SHARPYUV_EXTERN */
  33. // SharpYUV API version following the convention from semver.org
  34. #define SHARPYUV_VERSION_MAJOR 0
  35. #define SHARPYUV_VERSION_MINOR 2
  36. #define SHARPYUV_VERSION_PATCH 1
  37. // Version as a uint32_t. The major number is the high 8 bits.
  38. // The minor number is the middle 8 bits. The patch number is the low 16 bits.
  39. #define SHARPYUV_MAKE_VERSION(MAJOR, MINOR, PATCH) \
  40. (((MAJOR) << 24) | ((MINOR) << 16) | (PATCH))
  41. #define SHARPYUV_VERSION \
  42. SHARPYUV_MAKE_VERSION(SHARPYUV_VERSION_MAJOR, SHARPYUV_VERSION_MINOR, \
  43. SHARPYUV_VERSION_PATCH)
  44. // Returns the library's version number, packed in hexadecimal. See
  45. // SHARPYUV_VERSION.
  46. SHARPYUV_EXTERN int SharpYuvGetVersion(void);
  47. // RGB to YUV conversion matrix, in 16 bit fixed point.
  48. // y = rgb_to_y[0] * r + rgb_to_y[1] * g + rgb_to_y[2] * b + rgb_to_y[3]
  49. // u = rgb_to_u[0] * r + rgb_to_u[1] * g + rgb_to_u[2] * b + rgb_to_u[3]
  50. // v = rgb_to_v[0] * r + rgb_to_v[1] * g + rgb_to_v[2] * b + rgb_to_v[3]
  51. // Then y, u and v values are divided by 1<<16 and rounded.
  52. typedef struct {
  53. int rgb_to_y[4];
  54. int rgb_to_u[4];
  55. int rgb_to_v[4];
  56. } SharpYuvConversionMatrix;
  57. // Converts RGB to YUV420 using a downsampling algorithm that minimizes
  58. // artefacts caused by chroma subsampling.
  59. // This is slower than standard downsampling (averaging of 4 UV values).
  60. // Assumes that the image will be upsampled using a bilinear filter. If nearest
  61. // neighbor is used instead, the upsampled image might look worse than with
  62. // standard downsampling.
  63. // r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
  64. // to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
  65. // rgb_step: distance in bytes between two horizontally adjacent pixels on the
  66. // r, g and b channels. If rgb_bit_depth is > 8, it should be a
  67. // multiple of 2.
  68. // rgb_stride: distance in bytes between two vertically adjacent pixels on the
  69. // r, g, and b channels. If rgb_bit_depth is > 8, it should be a
  70. // multiple of 2.
  71. // rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.
  72. // Note: 16 bit input is truncated to 14 bits before conversion to yuv.
  73. // yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.
  74. // y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should
  75. // point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers
  76. // otherwise.
  77. // y_stride, u_stride, v_stride: distance in bytes between two vertically
  78. // adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they
  79. // should be multiples of 2.
  80. // width, height: width and height of the image in pixels
  81. SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr,
  82. const void* b_ptr, int rgb_step,
  83. int rgb_stride, int rgb_bit_depth,
  84. void* y_ptr, int y_stride, void* u_ptr,
  85. int u_stride, void* v_ptr, int v_stride,
  86. int yuv_bit_depth, int width, int height,
  87. const SharpYuvConversionMatrix* yuv_matrix);
  88. // TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
  89. // support (it's rarely used in practice, especially for images).
  90. #ifdef __cplusplus
  91. } // extern "C"
  92. #endif
  93. #endif // WEBP_SHARPYUV_SHARPYUV_H_