From e2c85878f6a33f29948b43d3492d9cdaf801aa54 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Thu, 29 Jun 2023 15:58:30 +0200 Subject: [PATCH] Add an initializer for the SharpYuvOptions struct. Change-Id: I36510bc361b040d3d3e7d261ab67b24ffc3013a6 --- sharpyuv/sharpyuv.c | 15 +++++++++++++++ sharpyuv/sharpyuv.h | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/sharpyuv/sharpyuv.c b/sharpyuv/sharpyuv.c index 61894513..b94885a6 100644 --- a/sharpyuv/sharpyuv.c +++ b/sharpyuv/sharpyuv.c @@ -483,6 +483,21 @@ int SharpYuvConvert(const void* r_ptr, const void* g_ptr, const void* b_ptr, u_ptr, u_stride, v_ptr, v_stride, yuv_bit_depth, width, height, &options); } +int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix* yuv_matrix, + SharpYuvOptions* options, int version) { + const int major = (version >> 24); + const int minor = (version >> 16) & 0xff; + if (options == NULL || yuv_matrix == NULL || + (major == SHARPYUV_VERSION_MAJOR && major == 0 && + minor != SHARPYUV_VERSION_MINOR) || + (major != SHARPYUV_VERSION_MAJOR)) { + return 0; + } + options->yuv_matrix = yuv_matrix; + options->transfer_type = kSharpYuvTransferFunctionSrgb; + return 1; +} + int SharpYuvConvertWithOptions(const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step, int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, diff --git a/sharpyuv/sharpyuv.h b/sharpyuv/sharpyuv.h index cda4b250..23a69ce3 100644 --- a/sharpyuv/sharpyuv.h +++ b/sharpyuv/sharpyuv.h @@ -34,9 +34,26 @@ extern "C" { #endif /* WEBP_EXTERN */ #endif /* SHARPYUV_EXTERN */ +#ifndef SHARPYUV_INLINE +#ifdef WEBP_INLINE +#define SHARPYUV_INLINE WEBP_INLINE +#else +#ifndef _MSC_VER +#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \ + (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) +#define SHARPYUV_INLINE inline +#else +#define SHARPYUV_INLINE +#endif +#else +#define SHARPYUV_INLINE __forceinline +#endif /* _MSC_VER */ +#endif /* WEBP_INLINE */ +#endif /* SHARPYUV_INLINE */ + // SharpYUV API version following the convention from semver.org #define SHARPYUV_VERSION_MAJOR 0 -#define SHARPYUV_VERSION_MINOR 3 +#define SHARPYUV_VERSION_MINOR 4 #define SHARPYUV_VERSION_PATCH 0 // Version as a uint32_t. The major number is the high 8 bits. // The minor number is the middle 8 bits. The patch number is the low 16 bits. @@ -113,7 +130,7 @@ typedef enum SharpYuvTransferFunctionType { // should be multiples of 2. // width, height: width and height of the image in pixels // This function calls SharpYuvConvertWithOptions with a default transfer -// function of kSharpYuvTransferFunctionSRGB. +// function of kSharpYuvTransferFunctionSrgb. SHARPYUV_EXTERN int SharpYuvConvert(const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step, int rgb_stride, int rgb_bit_depth, @@ -129,6 +146,18 @@ struct SharpYuvOptions { SharpYuvTransferFunctionType transfer_type; }; +// Internal, version-checked, entry point +SHARPYUV_EXTERN int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix*, + SharpYuvOptions*, int); + +// Should always be called, to initialize a fresh SharpYuvOptions +// structure before modification. SharpYuvOptionsInit() must have succeeded +// before using the 'options' object. +static SHARPYUV_INLINE int SharpYuvOptionsInit( + const SharpYuvConversionMatrix* yuv_matrix, SharpYuvOptions* options) { + return SharpYuvOptionsInitInternal(yuv_matrix, options, SHARPYUV_VERSION); +} + SHARPYUV_EXTERN int SharpYuvConvertWithOptions( const void* r_ptr, const void* g_ptr, const void* b_ptr, int rgb_step, int rgb_stride, int rgb_bit_depth, void* y_ptr, int y_stride, void* u_ptr,