Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Image library #520

Merged
merged 11 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions internal/c/libqb/include/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@
#include <stdlib.h>

#if defined(IMAGE_DEBUG) && IMAGE_DEBUG > 0
# ifdef _MSC_VER
# define IMAGE_DEBUG_PRINT(_fmt_, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " _fmt_ "\n", __FILE__, __LINE__, __func__, __VA_ARGS__)
# else
# define IMAGE_DEBUG_PRINT(_fmt_, _args_...) fprintf(stderr, "DEBUG: %s:%d:%s(): " _fmt_ "\n", __FILE__, __LINE__, __func__, ##_args_)
# endif
# define IMAGE_DEBUG_PRINT(_fmt_, _args_...) \
fprintf(stderr, "\e[1;37mDEBUG: %s:%d:%s(): \e[1;33m" _fmt_ "\e[1;37m\n", __FILE__, __LINE__, __func__, ##_args_)
# define IMAGE_DEBUG_CHECK(_exp_) \
if (!(_exp_)) \
IMAGE_DEBUG_PRINT("Condition (%s) failed", #_exp_)
IMAGE_DEBUG_PRINT("\e[0;31mCondition (%s) failed", #_exp_)
#else
# ifdef _MSC_VER
# define IMAGE_DEBUG_PRINT(_fmt_, ...) // Don't do anything in release builds
# else
# define IMAGE_DEBUG_PRINT(_fmt_, _args_...) // Don't do anything in release builds
# endif
# define IMAGE_DEBUG_CHECK(_exp_) // Don't do anything in release builds
# define IMAGE_DEBUG_PRINT(_fmt_, _args_...) // Don't do anything in release builds
# define IMAGE_DEBUG_CHECK(_exp_) // Don't do anything in release builds
#endif

// This is returned to the caller if something goes wrong while loading the image
Expand All @@ -58,7 +51,9 @@ static inline constexpr uint8_t image_get_bgra_alpha(const uint32_t c) { return

static inline constexpr uint32_t image_get_bgra_bgr(const uint32_t c) { return (uint32_t)(c & 0xFFFFFFu); }

static inline constexpr uint32_t image_make_bgra(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) {
static inline constexpr uint32_t image_set_bgra_alpha(const uint32_t c, const uint8_t a = 0xFFu) { return uint32_t(c & 0xFFFFFFu) | (uint32_t(a) << 24); }

static inline constexpr uint32_t image_make_bgra(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a = 0xFFu) {
return (uint32_t)(b) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 16) | ((uint32_t)(a) << 24);
}

Expand Down
4 changes: 3 additions & 1 deletion internal/c/parts/os/clipboard/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
// We need 'qbs' and 'image' structs stuff from here. Stop using this when image and friends are refactored
#include "../../../libqb.h"

// Uncomment this to to print debug messages to stderr
// #define IMAGE_DEBUG 1

// This is not strictly needed. But we'll leave it here for VSCode to do it's magic
#define CLIP_ENABLE_IMAGE 1
#include "clip/clip.h"
#include "clipboard.h"
#include "error_handle.h"
#define IMAGE_DEBUG 0
#include "image.h"
#include "qbs.h"
#include <vector>
Expand Down
20 changes: 17 additions & 3 deletions internal/c/parts/video/image/build.mk
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@

IMAGE_SRCS := \
image.cpp
image.cpp \
jo_gif/jo_gif.cpp \
nanosvg/nanosvg.cpp \
pixelscalers/hqx.cpp \
pixelscalers/mmpx.cpp \
pixelscalers/sxbr.cpp \
qoi/qoi.cpp \
sg_curico/sg_curico.cpp \
sg_pcx/sg_pcx.cpp \
stb/stb_image.cpp

IMAGE_OBJS := $(patsubst %.cpp,$(PATH_INTERNAL_C)/parts/video/image/%.o,$(IMAGE_SRCS))

$(PATH_INTERNAL_C)/parts/video/image/%.o: $(PATH_INTERNAL_C)/parts/video/image/%.cpp
$(CXX) -O2 $(CXXFLAGS) -DDEPENDENCY_CONSOLE_ONLY -Wall $< -c -o $@

IMAGE_LIB := $(PATH_INTERNAL_C)/parts/video/image/image.a

$(IMAGE_LIB): $(IMAGE_OBJS)
$(AR) rcs $@ $(IMAGE_OBJS)

ifdef DEP_IMAGE_CODEC
EXE_LIBS += $(IMAGE_OBJS)
EXE_LIBS += $(IMAGE_LIB)
endif

CLEAN_LIST += $(IMAGE_OBJS)
CLEAN_LIST += $(IMAGE_OBJS) $(IMAGE_LIB)
91 changes: 61 additions & 30 deletions internal/c/parts/video/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,32 @@
// qoi (https://qoiformat.org)
// pixelscalers (https://github.com/janert/pixelscalers)
// mmpx (https://github.com/ITotalJustice/mmpx)
// jo_gif (https://www.jonolick.com)
//
//-----------------------------------------------------------------------------------------------------

// Set this to 1 if we want to print debug messages to stderr
#define IMAGE_DEBUG 0
// Uncomment this to to print debug messages to stderr
// #define IMAGE_DEBUG 1

#include "image.h"
// We need 'qbs' and 'image' structs stuff from here
// This should eventually change when things are moved to smaller, logical and self-contained files
#include "../../../libqb.h"
#include "error_handle.h"
#include "filepath.h"
#include "jo_gif/jo_gif.h"
#include "nanosvg/nanosvg.h"
#include "nanosvg/nanosvgrast.h"
#include "pixelscalers/pixelscalers.h"
#include "qbs.h"
#include "qoi/qoi.h"
#include "sg_curico/sg_curico.h"
#include "sg_pcx/sg_pcx.h"
#include "stb/stb_image.h"
#include "stb/stb_image_write.h"
#include <algorithm>
#include <cctype>
#include <string>
#include <unordered_map>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define QOI_IMPLEMENTATION
#include "qoi.h"
#define NANOSVG_ALL_COLOR_KEYWORDS
#define NANOSVG_IMPLEMENTATION
#include "nanosvg/nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvg/nanosvgrast.h"
#include "sg_pcx.hpp"
#define SXBR_IMPLEMENTATION
#include "pixelscalers/sxbr.hpp"
#define MMPX_IMPLEMENTATION
#include "pixelscalers/mmpx.hpp"
#define HQX_IMPLEMENTATION
#include "pixelscalers/hqx.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"

#ifdef QB64_WINDOWS
# define ZERO_VARIABLE(_v_) ZeroMemory(&(_v_), sizeof(_v_))
Expand Down Expand Up @@ -176,9 +170,18 @@ static uint32_t *image_svg_load_from_file(const char *fileName, int32_t *xOut, i
if (!fp)
return nullptr;

fseek(fp, 0, SEEK_END);
if (fseek(fp, 0, SEEK_END)) {
fclose(fp);
return nullptr;
}

auto size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (size < 0) {
fclose(fp);
return nullptr;
}

rewind(fp);

auto svgString = (char *)malloc(size + 1);
if (!svgString) {
Expand Down Expand Up @@ -311,8 +314,13 @@ static uint32_t *image_decode_from_file(const char *fileName, int32_t *xOut, int
pixels = image_svg_load_from_file(fileName, xOut, yOut, scaler, &compOut, &isVG);
IMAGE_DEBUG_PRINT("Image dimensions (nanosvg) = (%i, %i)", *xOut, *yOut);

if (!pixels)
return nullptr; // Return NULL if all attempts failed
if (!pixels) {
pixels = curico_load_file(fileName, xOut, yOut, &compOut);
IMAGE_DEBUG_PRINT("Image dimensions (sg_curico) = (%i, %i)", *xOut, *yOut);

if (!pixels)
return nullptr; // Return NULL if all attempts failed
}
}
}
}
Expand Down Expand Up @@ -354,8 +362,13 @@ static uint32_t *image_decode_from_memory(const uint8_t *data, size_t size, int3
pixels = image_svg_load_from_memory(data, size, xOut, yOut, scaler, &compOut, &isVG);
IMAGE_DEBUG_PRINT("Image dimensions (nanosvg) = (%i, %i)", *xOut, *yOut);

if (!pixels)
return nullptr; // Return NULL if all attempts failed
if (!pixels) {
pixels = curico_load_memory(data, size, xOut, yOut, &compOut);
IMAGE_DEBUG_PRINT("Image dimensions (sg_curico) = (%i, %i)", *xOut, *yOut);

if (!pixels)
return nullptr; // Return NULL if all attempts failed
}
}
}
}
Expand Down Expand Up @@ -684,8 +697,8 @@ int32_t func__loadimage(qbs *qbsFileName, int32_t bpp, qbs *qbsRequirements, int
/// @param qbsRequirements Optional: Extra format and setting arguments
/// @param passed Optional parameters
void sub__saveimage(qbs *qbsFileName, int32_t imageHandle, qbs *qbsRequirements, int32_t passed) {
enum struct SaveFormat { PNG = 0, QOI, BMP, TGA, JPG, HDR };
static const char *formatName[] = {"png", "qoi", "bmp", "tga", "jpg", "hdr"};
enum struct SaveFormat { PNG = 0, QOI, BMP, TGA, JPG, HDR, GIF, ICO };
static const char *formatName[] = {"png", "qoi", "bmp", "tga", "jpg", "hdr", "gif", "ico"};

if (new_error) // leave if there was an error
return;
Expand Down Expand Up @@ -943,6 +956,24 @@ void sub__saveimage(qbs *qbsFileName, int32_t imageHandle, qbs *qbsRequirements,
}
} break;

case SaveFormat::GIF: {
auto gif = jo_gif_start(fileName.c_str(), short(width), short(height), 0, 255);
if (gif.fp) {
jo_gif_frame(&gif, reinterpret_cast<unsigned char *>(pixels.data()), 0, false);
jo_gif_end(&gif);
} else {
IMAGE_DEBUG_PRINT("jo_gif_start() failed");
error(QB_ERROR_ILLEGAL_FUNCTION_CALL);
}
} break;

case SaveFormat::ICO: {
if (!curico_save_file(fileName.c_str(), width, height, sizeof(uint32_t), pixels.data())) {
IMAGE_DEBUG_PRINT("curico_save_file() failed");
error(QB_ERROR_ILLEGAL_FUNCTION_CALL);
}
} break;

default:
IMAGE_DEBUG_PRINT("Save handler not implemented");
error(QB_ERROR_INTERNAL_ERROR);
Expand Down
Loading