Skip to content

Commit

Permalink
added QOI image loading support
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelFB committed Oct 1, 2023
1 parent 4e57617 commit 859503e
Show file tree
Hide file tree
Showing 4 changed files with 725 additions and 23 deletions.
1 change: 1 addition & 0 deletions Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endif()

add_library(FosterPlatform SHARED
src/foster_platform.c
src/foster_image.c
src/foster_renderer.c
src/foster_renderer_d3d11.c
src/foster_renderer_opengl.c
Expand Down
75 changes: 75 additions & 0 deletions Platform/src/foster_image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "foster_platform.h"
#include "foster_internal.h"
#include <SDL.h>

#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "third_party/stb_image_write.h"

#define QOI_NO_STDIO
#define QOI_IMPLEMENTATION
#define QOI_MALLOC(sz) STBI_MALLOC(sz)
#define QOI_FREE(p) STBI_FREE(p)
#include "third_party/qoi.h"

bool FosterImage_TestQOI(const unsigned char* data, int length);
unsigned char* FosterImage_LoadQOI(const unsigned char* data, int length, int* w, int * h);

unsigned char* FosterImageLoad(const unsigned char* data, int length, int* w, int* h)
{
// Test for QOI image first
if (FosterImage_TestQOI(data, length))
{
return FosterImage_LoadQOI(data, length, w, h);
}
// fallback to normal stb image loading (png, bmp, etc)
else
{
int c;
return stbi_load_from_memory(data, length, w, h, &c, 4);
}
}

void FosterImageFree(unsigned char* data)
{
stbi_image_free(data);
}

bool FosterImageWrite(FosterWriteFn* func, void* context, int w, int h, const void* data)
{
// note: 'FosterWriteFn' and 'stbi_write_func' must be the same
return stbi_write_png_to_func((stbi_write_func*)func, context, w, h, 4, data, w * 4) != 0;
}

bool FosterImage_TestQOI(const unsigned char* data, int length)
{
if (length < 4)
return false;

for (int i = 0; i < SDL_max(4, length); i ++)
if (data[i] != "qoif"[i])
return false;

return true;
}

unsigned char* FosterImage_LoadQOI(const unsigned char* data, int length, int* w, int * h)
{
qoi_desc desc;
void* result = qoi_decode(data, length, &desc, 4);

if (result != NULL)
{
*w = desc.width;
*h = desc.height;
return result;
}
else
{
*w = 0;
*h = 0;
return NULL;
}
}
23 changes: 0 additions & 23 deletions Platform/src/foster_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
#include "foster_internal.h"
#include <SDL.h>

#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "third_party/stb_image_write.h"

#define STBTT_STATIC
#define STB_TRUETYPE_IMPLEMENTATION
#include "third_party/stb_truetype.h"
Expand Down Expand Up @@ -412,23 +406,6 @@ bool FosterGetFocused()
return (flags & (SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS)) != 0;
}

unsigned char* FosterImageLoad(const unsigned char* memory, int length, int* w, int* h)
{
int c;
return stbi_load_from_memory(memory, length, w, h, &c, 4);
}

void FosterImageFree(unsigned char* data)
{
stbi_image_free(data);
}

bool FosterImageWrite(FosterWriteFn* func, void* context, int w, int h, const void* data)
{
// note: 'FosterWriteFn' and 'stbi_write_func' must be the same
return stbi_write_png_to_func((stbi_write_func*)func, context, w, h, 4, data, w * 4) != 0;
}

FosterFont* FosterFontInit(unsigned char* data, int length)
{
stbtt_fontinfo* info = (stbtt_fontinfo*)SDL_malloc(sizeof(stbtt_fontinfo));
Expand Down
Loading

0 comments on commit 859503e

Please sign in to comment.