Skip to content

Commit

Permalink
Add qoi format support
Browse files Browse the repository at this point in the history
Co-authored-by: Ozkan Sezer <[email protected]>
  • Loading branch information
2 people authored and slouken committed Dec 28, 2021
1 parent a31da6b commit 531c514
Show file tree
Hide file tree
Showing 10 changed files with 844 additions and 31 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ endif()

target_sources(SDL2_image PRIVATE IMG.c IMG_png.c IMG_bmp.c IMG_gif.c
IMG_jpg.c IMG_lbm.c IMG_pcx.c IMG_pnm.c IMG_svg.c IMG_tga.c
IMG_tif.c IMG_webp.c IMG_WIC.c IMG_xcf.c IMG_xpm.c IMG_xv.c IMG_xxx.c ${IMAGEIO_SOURCES})
IMG_tif.c IMG_webp.c IMG_WIC.c IMG_xcf.c IMG_xpm.c IMG_xv.c
IMG_qoi.c IMG_xxx.c ${IMAGEIO_SOURCES})

target_compile_definitions(SDL2_image PRIVATE
-DLOAD_BMP -DLOAD_GIF -DLOAD_LBM -DLOAD_PCX -DLOAD_PNM
-DLOAD_TGA -DLOAD_XCF -DLOAD_XPM -DLOAD_XV -DLOAD_XPM)
-DLOAD_TGA -DLOAD_XCF -DLOAD_XPM -DLOAD_XV -DLOAD_XPM
-DLOAD_QOI)

if (SUPPORT_JPG)
target_compile_definitions(SDL2_image PRIVATE -DLOAD_JPG)
Expand Down
1 change: 1 addition & 0 deletions IMG.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static struct {
{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
{ "XV", IMG_isXV, IMG_LoadXV_RW },
{ "WEBP", IMG_isWEBP, IMG_LoadWEBP_RW },
{ "QOI", IMG_isQOI, IMG_LoadQOI_RW },
};

/* Table of animation detection and loading functions */
Expand Down
115 changes: 115 additions & 0 deletions IMG_qoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

/* This file use QOI library:
* https://github.com/phoboslab/qoi
*/

#include "SDL_image.h"

#ifdef LOAD_QOI

#define QOI_MALLOC(sz) SDL_malloc(sz)
#define QOI_FREE(p) SDL_free(p)
#define QOI_ZEROARR(a) SDL_zeroa(a)

#define QOI_NO_STDIO
#define QOI_IMPLEMENTATION
#include "qoi.h"

/* See if an image is contained in a data source */
int IMG_isQOI(SDL_RWops *src)
{
Sint64 start;
int is_QOI;
char magic[4];

if ( !src )
return 0;
start = SDL_RWtell(src);
is_QOI = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
if ( SDL_strncmp(magic, "qoif", 4) == 0 ) {
is_QOI = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_QOI);
}

/* Load a QOI type image from an SDL datasource */
SDL_Surface *IMG_LoadQOI_RW(SDL_RWops *src)
{
void *data;
size_t size;
void *pixel_data;
qoi_desc image_info;
SDL_Surface *surface = NULL;

data = (void *)SDL_LoadFile_RW(src, &size, SDL_FALSE);
if ( !data ) {
return NULL;
}

pixel_data = qoi_decode(data, size, &image_info, 4);
SDL_free(data);
if ( !pixel_data ) {
IMG_SetError("Couldn't parse QOI image");
return NULL;
}

surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
image_info.width,
image_info.height,
32,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000);
if ( !surface ) {
SDL_free(pixel_data);
IMG_SetError("Couldn't create SDL_Surface");
return NULL;
}

SDL_memcpy(surface->pixels, pixel_data, image_info.width*image_info.height*4);

return surface;
}

#else
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif

/* See if an image is contained in a data source */
int IMG_isQOI(SDL_RWops *src)
{
return(0);
}

/* Load a QOI type image from an SDL datasource */
SDL_Surface *IMG_LoadQOI_RW(SDL_RWops *src)
{
return(NULL);
}

#endif /* LOAD_QOI */
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ libSDL2_image_la_SOURCES = \
IMG_pcx.c \
IMG_png.c \
IMG_pnm.c \
IMG_qoi.c \
IMG_svg.c \
IMG_tga.c \
IMG_tif.c \
Expand All @@ -31,7 +32,8 @@ libSDL2_image_la_SOURCES = \
$(IMAGEIO_SOURCE) \
miniz.h \
nanosvg.h \
nanosvgrast.h
nanosvgrast.h \
qoi.h

EXTRA_DIST = \
Android.mk \
Expand Down
30 changes: 18 additions & 12 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(pkgconfigdir)" \
LTLIBRARIES = $(lib_LTLIBRARIES)
am__DEPENDENCIES_1 =
am__libSDL2_image_la_SOURCES_DIST = IMG.c IMG_bmp.c IMG_gif.c \
IMG_jpg.c IMG_lbm.c IMG_pcx.c IMG_png.c IMG_pnm.c IMG_svg.c \
IMG_tga.c IMG_tif.c IMG_xcf.c IMG_xpm.c IMG_xv.c IMG_webp.c \
IMG_WIC.c IMG_ImageIO.m miniz.h nanosvg.h nanosvgrast.h
IMG_jpg.c IMG_lbm.c IMG_pcx.c IMG_png.c IMG_pnm.c IMG_qoi.c \
IMG_svg.c IMG_tga.c IMG_tif.c IMG_xcf.c IMG_xpm.c IMG_xv.c \
IMG_webp.c IMG_WIC.c IMG_ImageIO.m miniz.h nanosvg.h \
nanosvgrast.h qoi.h
@USE_IMAGEIO_TRUE@am__objects_1 = IMG_ImageIO.lo
am_libSDL2_image_la_OBJECTS = IMG.lo IMG_bmp.lo IMG_gif.lo IMG_jpg.lo \
IMG_lbm.lo IMG_pcx.lo IMG_png.lo IMG_pnm.lo IMG_svg.lo \
IMG_tga.lo IMG_tif.lo IMG_xcf.lo IMG_xpm.lo IMG_xv.lo \
IMG_webp.lo IMG_WIC.lo $(am__objects_1)
IMG_lbm.lo IMG_pcx.lo IMG_png.lo IMG_pnm.lo IMG_qoi.lo \
IMG_svg.lo IMG_tga.lo IMG_tif.lo IMG_xcf.lo IMG_xpm.lo \
IMG_xv.lo IMG_webp.lo IMG_WIC.lo $(am__objects_1)
libSDL2_image_la_OBJECTS = $(am_libSDL2_image_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
Expand Down Expand Up @@ -188,11 +189,11 @@ am__depfiles_remade = ./$(DEPDIR)/IMG.Plo ./$(DEPDIR)/IMG_ImageIO.Plo \
./$(DEPDIR)/IMG_gif.Plo ./$(DEPDIR)/IMG_jpg.Plo \
./$(DEPDIR)/IMG_lbm.Plo ./$(DEPDIR)/IMG_pcx.Plo \
./$(DEPDIR)/IMG_png.Plo ./$(DEPDIR)/IMG_pnm.Plo \
./$(DEPDIR)/IMG_svg.Plo ./$(DEPDIR)/IMG_tga.Plo \
./$(DEPDIR)/IMG_tif.Plo ./$(DEPDIR)/IMG_webp.Plo \
./$(DEPDIR)/IMG_xcf.Plo ./$(DEPDIR)/IMG_xpm.Plo \
./$(DEPDIR)/IMG_xv.Plo ./$(DEPDIR)/showanim.Po \
./$(DEPDIR)/showimage.Po
./$(DEPDIR)/IMG_qoi.Plo ./$(DEPDIR)/IMG_svg.Plo \
./$(DEPDIR)/IMG_tga.Plo ./$(DEPDIR)/IMG_tif.Plo \
./$(DEPDIR)/IMG_webp.Plo ./$(DEPDIR)/IMG_xcf.Plo \
./$(DEPDIR)/IMG_xpm.Plo ./$(DEPDIR)/IMG_xv.Plo \
./$(DEPDIR)/showanim.Po ./$(DEPDIR)/showimage.Po
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
Expand Down Expand Up @@ -443,6 +444,7 @@ libSDL2_image_la_SOURCES = \
IMG_pcx.c \
IMG_png.c \
IMG_pnm.c \
IMG_qoi.c \
IMG_svg.c \
IMG_tga.c \
IMG_tif.c \
Expand All @@ -454,7 +456,8 @@ libSDL2_image_la_SOURCES = \
$(IMAGEIO_SOURCE) \
miniz.h \
nanosvg.h \
nanosvgrast.h
nanosvgrast.h \
qoi.h

EXTRA_DIST = \
Android.mk \
Expand Down Expand Up @@ -603,6 +606,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_pcx.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_png.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_pnm.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_qoi.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_svg.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_tga.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IMG_tif.Plo@am__quote@ # am--include-marker
Expand Down Expand Up @@ -996,6 +1000,7 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/IMG_pcx.Plo
-rm -f ./$(DEPDIR)/IMG_png.Plo
-rm -f ./$(DEPDIR)/IMG_pnm.Plo
-rm -f ./$(DEPDIR)/IMG_qoi.Plo
-rm -f ./$(DEPDIR)/IMG_svg.Plo
-rm -f ./$(DEPDIR)/IMG_tga.Plo
-rm -f ./$(DEPDIR)/IMG_tif.Plo
Expand Down Expand Up @@ -1063,6 +1068,7 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/IMG_pcx.Plo
-rm -f ./$(DEPDIR)/IMG_png.Plo
-rm -f ./$(DEPDIR)/IMG_pnm.Plo
-rm -f ./$(DEPDIR)/IMG_qoi.Plo
-rm -f ./$(DEPDIR)/IMG_svg.Plo
-rm -f ./$(DEPDIR)/IMG_tga.Plo
-rm -f ./$(DEPDIR)/IMG_tif.Plo
Expand Down
4 changes: 2 additions & 2 deletions Makefile.os2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DEPS_LIB=C:\SDL2DEV\lib

SRCS = IMG.c IMG_bmp.c IMG_gif.c IMG_jpg.c IMG_lbm.c IMG_pcx.c IMG_png.c &
IMG_pnm.c IMG_svg.c IMG_tga.c IMG_tif.c IMG_xcf.c IMG_xpm.c IMG_xv.c &
IMG_webp.c
IMG_webp.c IMG_qoi.c

LIBS = libpng.lib libtiff.lib zlib.lib jpeg.lib webpdec.lib SDL2.lib

Expand All @@ -38,7 +38,7 @@ CFLAGS+= -DBUILD_SDL
# wanted formats:
CFLAGS+= -DLOAD_JPG -DLOAD_PNG -DLOAD_BMP -DLOAD_GIF -DLOAD_LBM &
-DLOAD_PCX -DLOAD_PNM -DLOAD_TGA -DLOAD_XCF -DLOAD_XPM &
-DLOAD_XV -DLOAD_SVG -DLOAD_TIF -DLOAD_WEBP
-DLOAD_XV -DLOAD_SVG -DLOAD_TIF -DLOAD_WEBP -DLOAD_QOI


.extensions:
Expand Down
2 changes: 2 additions & 0 deletions SDL_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isSVG(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isQOI(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isTIF(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src);
extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src);
Expand All @@ -133,6 +134,7 @@ extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadSVG_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadQOI_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTIF_RW(SDL_RWops *src);
extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src);
Expand Down
Loading

0 comments on commit 531c514

Please sign in to comment.