SDL: testyuv: added GetColorspaceForYUVConversionMode()

From 991ad27de8a771cb419759dbc689a6dd9dd1cb16 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 1 Mar 2024 02:02:53 -0800
Subject: [PATCH] testyuv: added GetColorspaceForYUVConversionMode()

---
 test/testyuv.c     | 28 ++--------------------------
 test/testyuv_cvt.c | 36 ++++++++++++++++++++++++++++++++++++
 test/testyuv_cvt.h |  1 +
 3 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/test/testyuv.c b/test/testyuv.c
index 96858f7d79689..4beaee8ec9728 100644
--- a/test/testyuv.c
+++ b/test/testyuv.c
@@ -116,11 +116,6 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
         SDL_PIXELFORMAT_UYVY,
         SDL_PIXELFORMAT_YVYU
     };
-    const SDL_Colorspace colorspaces[] = {
-        SDL_COLORSPACE_JPEG,
-        SDL_COLORSPACE_BT601_LIMITED,
-        SDL_COLORSPACE_BT709_LIMITED
-    };
     int i, j;
     SDL_Surface *pattern = generate_test_pattern(pattern_size);
     const int yuv_len = MAX_YUV_SURFACE_SIZE(pattern->w, pattern->h, extra_pitch);
@@ -137,8 +132,7 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
     }
 
     mode = GetYUVConversionModeForResolution(pattern->w, pattern->h);
-    SDL_assert(mode < SDL_arraysize(colorspaces));
-    colorspace = colorspaces[mode];
+    colorspace = GetColorspaceForYUVConversionMode(mode);
 
     /* Verify conversion from YUV formats */
     for (i = 0; i < SDL_arraysize(formats); ++i) {
@@ -395,36 +389,18 @@ int main(int argc, char **argv)
     switch (yuv_mode) {
     case YUV_CONVERSION_JPEG:
         yuv_mode_name = "JPEG";
-        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
-                                           SDL_COLOR_RANGE_FULL,
-                                           SDL_COLOR_PRIMARIES_BT709,
-                                           SDL_TRANSFER_CHARACTERISTICS_BT601,
-                                           SDL_MATRIX_COEFFICIENTS_BT601,
-                                           SDL_CHROMA_LOCATION_CENTER);
         break;
     case YUV_CONVERSION_BT601:
         yuv_mode_name = "BT.601";
-        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
-                                           SDL_COLOR_RANGE_LIMITED,
-                                           SDL_COLOR_PRIMARIES_BT709,
-                                           SDL_TRANSFER_CHARACTERISTICS_BT601,
-                                           SDL_MATRIX_COEFFICIENTS_BT601,
-                                           SDL_CHROMA_LOCATION_CENTER);
         break;
     case YUV_CONVERSION_BT709:
         yuv_mode_name = "BT.709";
-        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
-                                           SDL_COLOR_RANGE_LIMITED,
-                                           SDL_COLOR_PRIMARIES_BT709,
-                                           SDL_TRANSFER_CHARACTERISTICS_BT709,
-                                           SDL_MATRIX_COEFFICIENTS_BT709,
-                                           SDL_CHROMA_LOCATION_CENTER);
         break;
     default:
         yuv_mode_name = "UNKNOWN";
-        colorspace = SDL_COLORSPACE_UNKNOWN;
         break;
     }
+    colorspace = GetColorspaceForYUVConversionMode(yuv_mode);
 
     raw_yuv = SDL_calloc(1, MAX_YUV_SURFACE_SIZE(original->w, original->h, 0));
     ConvertRGBtoYUV(yuv_format, original->pixels, original->pitch, raw_yuv, original->w, original->h, yuv_mode, 0, 100);
diff --git a/test/testyuv_cvt.c b/test/testyuv_cvt.c
index 67a0ab6d88db4..dab0709aefe60 100644
--- a/test/testyuv_cvt.c
+++ b/test/testyuv_cvt.c
@@ -41,6 +41,42 @@ YUV_CONVERSION_MODE GetYUVConversionModeForResolution(int width, int height)
     return mode;
 }
 
+SDL_Colorspace GetColorspaceForYUVConversionMode(YUV_CONVERSION_MODE mode)
+{
+    SDL_Colorspace colorspace;
+
+    switch (mode) {
+    case YUV_CONVERSION_JPEG:
+        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
+                                           SDL_COLOR_RANGE_FULL,
+                                           SDL_COLOR_PRIMARIES_BT709,
+                                           SDL_TRANSFER_CHARACTERISTICS_BT601,
+                                           SDL_MATRIX_COEFFICIENTS_BT601,
+                                           SDL_CHROMA_LOCATION_CENTER);
+        break;
+    case YUV_CONVERSION_BT601:
+        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
+                                           SDL_COLOR_RANGE_LIMITED,
+                                           SDL_COLOR_PRIMARIES_BT709,
+                                           SDL_TRANSFER_CHARACTERISTICS_BT601,
+                                           SDL_MATRIX_COEFFICIENTS_BT601,
+                                           SDL_CHROMA_LOCATION_CENTER);
+        break;
+    case YUV_CONVERSION_BT709:
+        colorspace = SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
+                                           SDL_COLOR_RANGE_LIMITED,
+                                           SDL_COLOR_PRIMARIES_BT709,
+                                           SDL_TRANSFER_CHARACTERISTICS_BT709,
+                                           SDL_MATRIX_COEFFICIENTS_BT709,
+                                           SDL_CHROMA_LOCATION_CENTER);
+        break;
+    default:
+        colorspace = SDL_COLORSPACE_UNKNOWN;
+        break;
+    }
+    return colorspace;
+}
+
 static float clip3(float x, float y, float z)
 {
     return (z < x) ? x : ((z > y) ? y : z);
diff --git a/test/testyuv_cvt.h b/test/testyuv_cvt.h
index 62e6ab2abe85a..c8962142bcb8a 100644
--- a/test/testyuv_cvt.h
+++ b/test/testyuv_cvt.h
@@ -22,5 +22,6 @@ typedef enum
 
 extern void SetYUVConversionMode(YUV_CONVERSION_MODE mode);
 extern YUV_CONVERSION_MODE GetYUVConversionModeForResolution(int width, int height);
+extern SDL_Colorspace GetColorspaceForYUVConversionMode(YUV_CONVERSION_MODE mode);
 extern SDL_bool ConvertRGBtoYUV(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, YUV_CONVERSION_MODE mode, int monochrome, int luminance);
 extern int CalculateYUVPitch(Uint32 format, int width);