sharpyuv.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <inttypes.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. // SharpYUV API version following the convention from semver.org
  18. #define SHARPYUV_VERSION_MAJOR 0
  19. #define SHARPYUV_VERSION_MINOR 1
  20. #define SHARPYUV_VERSION_PATCH 0
  21. // Version as a uint32_t. The major number is the high 8 bits.
  22. // The minor number is the middle 8 bits. The patch number is the low 16 bits.
  23. #define SHARPYUV_MAKE_VERSION(MAJOR, MINOR, PATCH) \
  24. (((MAJOR) << 24) | ((MINOR) << 16) | (PATCH))
  25. #define SHARPYUV_VERSION \
  26. SHARPYUV_MAKE_VERSION(SHARPYUV_VERSION_MAJOR, SHARPYUV_VERSION_MINOR, \
  27. SHARPYUV_VERSION_PATCH)
  28. // RGB to YUV conversion matrix, in 16 bit fixed point.
  29. // y = rgb_to_y[0] * r + rgb_to_y[1] * g + rgb_to_y[2] * b + rgb_to_y[3]
  30. // u = rgb_to_u[0] * r + rgb_to_u[1] * g + rgb_to_u[2] * b + rgb_to_u[3]
  31. // v = rgb_to_v[0] * r + rgb_to_v[1] * g + rgb_to_v[2] * b + rgb_to_v[3]
  32. // Then y, u and v values are divided by 1<<16 and rounded.
  33. typedef struct {
  34. int rgb_to_y[4];
  35. int rgb_to_u[4];
  36. int rgb_to_v[4];
  37. } SharpYuvConversionMatrix;
  38. // Converts RGB to YUV420 using a downsampling algorithm that minimizes
  39. // artefacts caused by chroma subsampling.
  40. // This is slower than standard downsampling (averaging of 4 UV values).
  41. // Assumes that the image will be upsampled using a bilinear filter. If nearest
  42. // neighbor is used instead, the upsampled image might look worse than with
  43. // standard downsampling.
  44. // r_ptr, g_ptr, b_ptr: pointers to the source r, g and b channels. Should point
  45. // to uint8_t buffers if rgb_bit_depth is 8, or uint16_t buffers otherwise.
  46. // rgb_step: distance in bytes between two horizontally adjacent pixels on the
  47. // r, g and b channels. If rgb_bit_depth is > 8, it should be a
  48. // multiple of 2.
  49. // rgb_stride: distance in bytes between two vertically adjacent pixels on the
  50. // r, g, and b channels. If rgb_bit_depth is > 8, it should be a
  51. // multiple of 2.
  52. // rgb_bit_depth: number of bits for each r/g/b value. One of: 8, 10, 12, 16.
  53. // Note: 16 bit input is truncated to 14 bits before conversion to yuv.
  54. // yuv_bit_depth: number of bits for each y/u/v value. One of: 8, 10, 12.
  55. // y_ptr, u_ptr, v_ptr: pointers to the destination y, u and v channels. Should
  56. // point to uint8_t buffers if yuv_bit_depth is 8, or uint16_t buffers
  57. // otherwise.
  58. // y_stride, u_stride, v_stride: distance in bytes between two vertically
  59. // adjacent pixels on the y, u and v channels. If yuv_bit_depth > 8, they
  60. // should be multiples of 2.
  61. // width, height: width and height of the image in pixels
  62. int SharpYuvConvert(const void* r_ptr, const void* g_ptr, const void* b_ptr,
  63. int rgb_step, int rgb_stride, int rgb_bit_depth,
  64. void* y_ptr, int y_stride, void* u_ptr, int u_stride,
  65. void* v_ptr, int v_stride, int yuv_bit_depth, int width,
  66. int height, const SharpYuvConversionMatrix* yuv_matrix);
  67. // TODO(b/194336375): Add YUV444 to YUV420 conversion. Maybe also add 422
  68. // support (it's rarely used in practice, especially for images).
  69. #ifdef __cplusplus
  70. } // extern "C"
  71. #endif
  72. #endif // WEBP_SHARPYUV_SHARPYUV_H_