From 13c4ca1cbeac878dc034da5f5e980fcafb9f80c1 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Mon, 15 Jan 2024 10:50:55 -0800 Subject: [PATCH] Prepare STB to use the new version of resize (and fall back to old) Newer versions of STB don't have the stb_image_resize.h anymore, but a new stb_image_resize2.h with slightly different function names. Use the new one if available, and provide adapter functions to work in both cases. --- src/CMakeLists.txt | 7 ++++++- src/stb-image-source.cc | 35 ++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8cb416f..fdb21cc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -101,6 +101,7 @@ if(WITH_VIDEO_DECODING) endif() if(WITH_STB_IMAGE) + # STB usuall does not come with pkg-config. Just check if we see the include include(CheckIncludeFileCXX) CHECK_INCLUDE_FILE_CXX(stb/stb_image.h HAVE_STB) target_sources(timg PUBLIC stb-image-source.h stb-image-source.cc) @@ -108,6 +109,10 @@ if(WITH_STB_IMAGE) message("--> Using STB from third_party/ instead") target_include_directories(timg PRIVATE ${CMAKE_SOURCE_DIR}/third_party) endif() + CHECK_INCLUDE_FILE_CXX(stb/stb_image_resize2.h STB_RESIZE_VERSION2) + if(STB_RESIZE_VERSION2) + target_compile_definitions(timg PUBLIC STB_RESIZE_VERSION2) + endif() target_compile_definitions(timg PUBLIC WITH_TIMG_STB) endif() @@ -116,7 +121,7 @@ if(WITH_QOI_IMAGE) CHECK_INCLUDE_FILE_CXX(qoi.h HAVE_QOI) target_sources(timg PUBLIC qoi-image-source.h qoi-image-source.cc) if(NOT HAVE_QOI) - message("--> Using OTB from third_party/ instead") + message("--> Using QOI from third_party/ instead") target_include_directories(timg PRIVATE ${CMAKE_SOURCE_DIR}/third_party/qoi) endif() target_compile_definitions(timg PUBLIC WITH_TIMG_QOI) diff --git a/src/stb-image-source.cc b/src/stb-image-source.cc index 88e2ca5..f356921 100644 --- a/src/stb-image-source.cc +++ b/src/stb-image-source.cc @@ -21,14 +21,36 @@ #define STB_IMAGE_RESIZE_IMPLEMENTATION // Least amount of fuzziness on upscaling #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_BOX -#include "stb/stb_image_resize.h" + +// There is an old and a new version of stb resize. Prefer newer if available +// (tested for in CMake), and provide adapter functions to use either. +#if STB_RESIZE_VERSION2 +# include "stb/stb_image_resize2.h" +inline void stb_resize_image(const unsigned char *input_pixels, int input_w, + int input_h, unsigned char *output_pixels, + int output_w, int output_h) { + static constexpr stbir_pixel_layout kFramebufferFormat = STBIR_RGBA; + stbir_resize_uint8_linear(input_pixels, input_w, input_h, 0, // + output_pixels, output_w, output_h, 0, + kFramebufferFormat); +} + +#else +# include "stb/stb_image_resize.h" +inline void stb_resize_image(const unsigned char *input_pixels, int input_w, + int input_h, unsigned char *output_pixels, + int output_w, int output_h) { + static constexpr int kFramebufferFormat = 4; // RGBA. + stbir_resize_uint8(input_pixels, input_w, input_h, 0, // + output_pixels, output_w, output_h, 0, // + kFramebufferFormat); +} +#endif // TODO: preprocessed frame and SendFrames() are similar to // image-display.cc. Maybe things can be consolidated. -// Normalize on RGBA, so that it fits our Framebuffer format. -static constexpr int kDesiredChannels = 4; - +static constexpr int kDesiredChannels = 4; // RGBA, our framebuffer format namespace timg { class STBImageSource::PreprocessedFrame { public: @@ -36,9 +58,8 @@ class STBImageSource::PreprocessedFrame { int target_w, int target_h, const Duration &delay, const DisplayOptions &opt) : delay_(delay), framebuffer_(target_w, target_h) { - stbir_resize_uint8(image_data, source_w, source_h, 0, - (uint8_t *)framebuffer_.begin(), target_w, target_h, - 0, kDesiredChannels /*RGBA*/); + stb_resize_image(image_data, source_w, source_h, + (uint8_t *)framebuffer_.begin(), target_w, target_h); framebuffer_.AlphaComposeBackground( opt.bgcolor_getter, opt.bg_pattern_color, opt.pattern_size * opt.cell_x_px,