Skip to content

Commit

Permalink
Prepare STB to use the new version of resize (and fall back to old)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hzeller committed Jan 15, 2024
1 parent bcf9b74 commit 13c4ca1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ 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)
if(NOT HAVE_STB)
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()

Expand All @@ -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)
Expand Down
35 changes: 28 additions & 7 deletions src/stb-image-source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,45 @@
#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:
PreprocessedFrame(const uint8_t *image_data, int source_w, int source_h,
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,
Expand Down

0 comments on commit 13c4ca1

Please sign in to comment.