From 531c51434f6ef0343676676ae4e99d5a42a135c6 Mon Sep 17 00:00:00 2001
From: Nika Starceva <[EMAIL REDACTED]>
Date: Mon, 20 Dec 2021 14:33:50 +0300
Subject: [PATCH] Add qoi format support
Co-authored-by: Ozkan Sezer <sezero@users.sourceforge.net>
---
CMakeLists.txt | 6 +-
IMG.c | 1 +
IMG_qoi.c | 115 +++++++++
Makefile.am | 4 +-
Makefile.in | 30 ++-
Makefile.os2 | 4 +-
SDL_image.h | 2 +
configure | 42 ++--
configure.ac | 6 +
qoi.h | 665 +++++++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 844 insertions(+), 31 deletions(-)
create mode 100644 IMG_qoi.c
create mode 100644 qoi.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ebc3e3..c4af2a7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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)
diff --git a/IMG.c b/IMG.c
index e080d27..8ff082f 100644
--- a/IMG.c
+++ b/IMG.c
@@ -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 */
diff --git a/IMG_qoi.c b/IMG_qoi.c
new file mode 100644
index 0000000..612a7ba
--- /dev/null
+++ b/IMG_qoi.c
@@ -0,0 +1,115 @@
+/*
+ SDL_image: An example image loading library for use with SDL
+ Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
+
+ 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 */
diff --git a/Makefile.am b/Makefile.am
index de59b51..5c42cdf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
@@ -31,7 +32,8 @@ libSDL2_image_la_SOURCES = \
$(IMAGEIO_SOURCE) \
miniz.h \
nanosvg.h \
- nanosvgrast.h
+ nanosvgrast.h \
+ qoi.h
EXTRA_DIST = \
Android.mk \
diff --git a/Makefile.in b/Makefile.in
index d37838a..84415e6 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -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@)
@@ -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)
@@ -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 \
@@ -454,7 +456,8 @@ libSDL2_image_la_SOURCES = \
$(IMAGEIO_SOURCE) \
miniz.h \
nanosvg.h \
- nanosvgrast.h
+ nanosvgrast.h \
+ qoi.h
EXTRA_DIST = \
Android.mk \
@@ -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
@@ -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
@@ -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
diff --git a/Makefile.os2 b/Makefile.os2
index b2ec619..69e283f 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -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
@@ -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:
diff --git a/SDL_image.h b/SDL_image.h
index 32d6a20..9f2819f 100644
--- a/SDL_image.h
+++ b/SDL_image.h
@@ -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);
@@ -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);
diff --git a/configure b/configure
index 0c3e0e8..e003f61 100755
--- a/configure
+++ b/configure
@@ -954,6 +954,7 @@ enable_xv
enable_webp
enable_webpdec
enable_webp_shared
+enable_qoi
with_sdl_prefix
with_sdl_exec_prefix
enable_sdltest
@@ -1629,6 +1630,7 @@ Optional Features:
--enable-webpdec support loading WEBP images via libwebpdecoder
instead of libwebp [default=no]
--enable-webp-shared dynamically load WEBP support [default=yes]
+ --enable-qoi support loading QOI images [default=yes]
--disable-sdltest Do not try to compile and run a test SDL program
Optional Packages:
@@ -4061,13 +4063,13 @@ if ${lt_cv_nm_interface+:} false; then :
else
lt_cv_nm_interface="BSD nm"
echo "int some_variable = 0;" > conftest.$ac_ext
- (eval echo "\"\$as_me:4064: $ac_compile\"" >&5)
+ (eval echo "\"\$as_me:4066: $ac_compile\"" >&5)
(eval "$ac_compile" 2>conftest.err)
cat conftest.err >&5
- (eval echo "\"\$as_me:4067: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
+ (eval echo "\"\$as_me:4069: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
(eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
cat conftest.err >&5
- (eval echo "\"\$as_me:4070: output\"" >&5)
+ (eval echo "\"\$as_me:4072: output\"" >&5)
cat conftest.out >&5
if $GREP 'External.*some_variable' conftest.out > /dev/null; then
lt_cv_nm_interface="MS dumpbin"
@@ -5281,7 +5283,7 @@ ia64-*-hpux*)
;;
*-*-irix6*)
# Find out which ABI we are using.
- echo '#line 5284 "configure"' > conftest.$ac_ext
+ echo '#line 5286 "configure"' > conftest.$ac_ext
if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
(eval $ac_compile) 2>&5
ac_status=$?
@@ -7106,11 +7108,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7109: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7111: $lt_compile\"" >&5)
(eval "$lt_compile" 2>conftest.err)
ac_status=$?
cat conftest.err >&5
- echo "$as_me:7113: \$? = $ac_status" >&5
+ echo "$as_me:7115: \$? = $ac_status" >&5
if (exit $ac_status) && test -s "$ac_outfile"; then
# The compiler can only warn and ignore the option if not recognized
# So say no if there are warnings other than the usual output.
@@ -7455,11 +7457,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7458: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7460: $lt_compile\"" >&5)
(eval "$lt_compile" 2>conftest.err)
ac_status=$?
cat conftest.err >&5
- echo "$as_me:7462: \$? = $ac_status" >&5
+ echo "$as_me:7464: \$? = $ac_status" >&5
if (exit $ac_status) && test -s "$ac_outfile"; then
# The compiler can only warn and ignore the option if not recognized
# So say no if there are warnings other than the usual output.
@@ -7560,11 +7562,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7563: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7565: $lt_compile\"" >&5)
(eval "$lt_compile" 2>out/conftest.err)
ac_status=$?
cat out/conftest.err >&5
- echo "$as_me:7567: \$? = $ac_status" >&5
+ echo "$as_me:7569: \$? = $ac_status" >&5
if (exit $ac_status) && test -s out/conftest2.$ac_objext
then
# The compiler can only warn and ignore the option if not recognized
@@ -7615,11 +7617,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7618: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7620: $lt_compile\"" >&5)
(eval "$lt_compile" 2>out/conftest.err)
ac_status=$?
cat out/conftest.err >&5
- echo "$as_me:7622: \$? = $ac_status" >&5
+ echo "$as_me:7624: \$? = $ac_status" >&5
if (exit $ac_status) && test -s out/conftest2.$ac_objext
then
# The compiler can only warn and ignore the option if not recognized
@@ -10058,7 +10060,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 10061 "configure"
+#line 10063 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -10154,7 +10156,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 10157 "configure"
+#line 10159 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -12645,6 +12647,13 @@ else
enable_webp_shared=yes
fi
+# Check whether --enable-qoi was given.
+if test "${enable_qoi+set}" = set; then :
+ enableval=$enable_qoi;
+else
+ enable_qoi=yes
+fi
+
SDL_VERSION=2.0.8
@@ -13705,6 +13714,11 @@ if test x$enable_xv = xyes; then
fi
+if test x$enable_qoi = xyes; then
+ $as_echo "#define LOAD_QOI 1" >>confdefs.h
+
+fi
+
if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
CFLAGS="$LIBWEBP_CFLAGS $CFLAGS"
if test x$enable_webp_shared = xyes && test x$webp_lib != x; then
diff --git a/configure.ac b/configure.ac
index 89b3bfb..4c9afdd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -193,6 +193,8 @@ AC_ARG_ENABLE([webpdec], [AS_HELP_STRING([--enable-webpdec], [support loading WE
[], [enable_webpdec=no])
AC_ARG_ENABLE([webp-shared], [AS_HELP_STRING([--enable-webp-shared], [dynamically load WEBP support [default=yes]])],
[], [enable_webp_shared=yes])
+AC_ARG_ENABLE([qoi], [AS_HELP_STRING([--enable-qoi], [support loading QOI images [default=yes]])],
+ [], [enable_qoi=yes])
dnl Check for SDL
SDL_VERSION=2.0.8
@@ -377,6 +379,10 @@ if test x$enable_xv = xyes; then
AC_DEFINE([LOAD_XV])
fi
+if test x$enable_qoi = xyes; then
+ AC_DEFINE([LOAD_QOI])
+fi
+
if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
CFLAGS="$LIBWEBP_CFLAGS $CFLAGS"
if test x$enable_webp_shared = xyes && test x$webp_lib != x; then
diff --git a/qoi.h b/qoi.h
new file mode 100644
index 0000000..491c6f3
--- /dev/null
+++ b/qoi.h
@@ -0,0 +1,665 @@
+/*
+
+QOI - The "Quite OK Image" format for fast, lossless image compression
+
+Dominic Szablewski - https://phoboslab.org
+
+
+-- LICENSE: The MIT License(MIT)
+
+Copyright(c) 2021 Dominic Szablewski
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files(the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions :
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+
+-- About
+
+QOI encodes and decodes images in a lossless format. Compared to stb_image and
+stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and
+20% better compression.
+
+
+-- Synopsis
+
+// Define `QOI_IMPLEMENTATION` in *one* C/C++ file before including this
+// library to create the implementation.
+
+#define QOI_IMPLEMENTATION
+#include "qoi.h"
+
+// Encode and store an RGBA buffer to the file system. The qoi_desc describes
+// the input pixel data.
+qoi_write("image_new.qoi", rgba_pixels, &(qoi_desc){
+ .width = 1920,
+ .height = 1080,
+ .channels = 4,
+ .colorspace = QOI_SRGB
+});
+
+// Load and decode a QOI image from the file system into a 32bbp RGBA buffer.
+// The qoi_desc struct will be filled with the width, height, number of channels
+// and colorspace read from the file header.
+qoi_desc desc;
+void *rgba_pixels = qoi_read("image.qoi", &desc, 4);
+
+
+
+-- Documentation
+
+This library provides the following functions;
+- qoi_read -- read and decode a QOI file
+- qoi_decode -- decode the raw bytes of a QOI image from memory
+- qoi_write -- encode and write a QOI file
+- qoi_encode -- encode an rgba buffer into a QOI image in memory
+
+See the function declaration below for the signature and more information.
+
+If you don't want/need the qoi_read and qoi_write functions, you can define
+QOI_NO_STDIO before including this library.
+
+This library uses malloc() and free(). To supply your own malloc implementation
+you can define QOI_MALLOC and QOI_FREE before including this library.
+
+This library uses memset() to zero-initialize the index. To supply your own
+implementation you can define QOI_ZEROARR before including this library.
+
+
+-- Data Format
+
+A QOI file has a 14 byte header, followed by any number of data "chunks" and an
+8-byte end marker.
+
+struct qoi_header_t {
+ char magic[4]; // magic bytes "qoif"
+ uint32_t width; // image width in pixels (BE)
+ uint32_t height; // image height in pixels (BE)
+ uint8_t channels; // 3 = RGB, 4 = RGBA
+ uint8_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels linear
+};
+
+Images are encoded from top to bottom, left to right. The decoder and encoder
+start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An image is
+complete when all pixels specified by width * height have been covered.
+
+Pixels are encoded as
+ - a run of the previous pixel
+ - an index into an array of previously seen pixels
+ - a difference to the previous pixel value in r,g,b
+ - full r,g,b or r,g,b,a values
+
+The color channels are assumed to not be premultiplied with the alpha channel
+("un-premultiplied alpha").
+
+A running array[64] (zero-initialized) of previously seen pixel values is
+maintained by the encoder and decoder. Each pixel that is seen by the encoder
+and decoder is put into this array at the position formed by a hash function of
+the color value. In the encoder, if the pixel value at the index matches the
+current pixel, this index position is written to the stream as QOI_OP_INDEX.
+The hash function for the index is:
+
+ index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64
+
+Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The
+bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All
+values encoded in these data bits have the most significant bit on the left.
+
+The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the
+presence of an 8-bit tag first.
+
+The byte stream's end is marked with 7 0x00 bytes followed a single 0x01 byte.
+
+
+The possible chunks are:
+
+
+.- QOI_OP_INDEX ----------.
+| Byte[0] |
+| 7 6 5 4 3 2 1 0 |
+|-------+-----------------|
+| 0 0 | index |
+`-------------------------`
+2-bit tag b00
+6-bit index into the color index array: 0..63
+
+A valid encoder must not issue 7 or more consecutive QOI_OP_INDEX chunks to the
+index 0, to avoid confusion with the 8 byte end marker.
+
+
+.- QOI_OP_DIFF -----------.
+| Byte[0] |
+| 7 6 5 4 3 2 1 0 |
+|-------+-----+-----+-----|
+| 0 1 | dr | dg | db |
+`-------------------------`
+2-bit tag b01
+2-bit red channel difference from the previous pixel between -2..1
+2-bit green channel difference from the previous pixel between -2..1
+2-bit blue channel difference from the previous pixel between -2..1
+
+The difference to the current channel values are using a wraparound operation,
+so "1 - 2" will result in 255, while "255 + 1" will result in 0.
+
+Values are stored as unsigned integers with a bias of 2. E.g. -2 is stored as
+0 (b00). 1 is stored as 3 (b11).
+
+
+.- QOI_OP_LUMA -------------------------------------.
+| Byte[0] | Byte[1] |
+| 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |
+|-------+-----------------+-------------+-----------|
+| 1 0 | green diff | dr - dg | db - dg |
+`---------------------------------------------------`
+2-bit tag b10
+6-bit green channel difference from the previous pixel -32..31
+4-bit red channel difference minus green channel difference -8..7
+4-bit blue channel difference minus green channel difference -8..7
+
+The green channel is used to indicate the general direction of change and is
+encoded in 6 bits. The red and green channels (dr and db) base their diffs off
+of the green channel difference and are encoded in 4 bits. I.e.:
+ dr_dg = (last_px.r - cur_px.r) - (last_px.g - cur_px.g)
+ db_dg = (last_px.b - cur_px.b) - (last_px.g - cur_px.g)
+
+The difference to the current channel values are using a wraparound operation,
+so "10 - 13" will result in 253, while "250 + 7" will result in 1.
+
+Values are stored as unsigned integers with a bias of 32 for the green channel
+and a bias of 8 for the red and blue channel.
+
+
+.- QOI_OP_RUN ------------.
+| Byte[0] |
+| 7 6 5 4 3 2 1 0 |
+|-------+-----------------|
+| 1 1 | run |
+`-------------------------`
+2-bit tag b11
+6-bit run-length repeating the previous pixel: 1..62
+
+The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64
+(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and
+QOI_OP_RGBA tags.
+
+
+.- QOI_OP_RGB ------------------------------------------.
+| Byte[0] | Byte[1] | Byte[2] | Byte[3] |
+| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
+|-------------------------+---------+---------+---------|
+| 1 1 1 1 1 1 1 0 | red | green | blue |
+`-------------------------------------------------------`
+8-bit tag b11111110
+8-bit red channel value
+8-bit green channel value
+8-bit blue channel value
+
+
+.- QOI_OP_RGBA ---------------------------------------------------.
+| Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] |
+| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
+|-------------------------+---------+---------+---------+---------|
+| 1 1 1 1 1 1 1 1 | red | green | blue | alpha |
+`-----------------------------------------------------------------`
+8-bit tag b11111111
+8-bit red channel value
+8-bit green channel value
+8-bit blue channel value
+8-bit alpha channel value
+
+*/
+
+
+/* -----------------------------------------------------------------------------
+Header - Public functions */
+
+#ifndef QOI_H
+#define QOI_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* A pointer to a qoi_desc struct has to be supplied to all of qoi's functions.
+It describes either the input format (for qoi_write and qoi_encode), or is
+filled with the description read from the file header (for qoi_read and
+qoi_decode).
+
+The colorspace in this qoi_desc is an enum where
+ 0 = sRGB, i.e. gamma scaled RGB channels and a linear alpha channel
+ 1 = all channels are linear
+You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely
+informative. It will be saved to the file header, but does not affect
+en-/decoding in any way. */
+
+#define QOI_SRGB 0
+#define QOI_LINEAR 1
+
+typedef struct {
+ unsigned int width;
+ unsigned int height;
+ unsigned char channels;
+ unsigned char colorspace;
+} qoi_desc;
+
+#ifndef QOI_NO_STDIO
+
+/* Encode raw RGB or RGBA pixels into a QOI image and write it to the file
+system. The qoi_desc struct must be filled with the image width, height,
+number of channels (3 = RGB, 4 = RGBA) and the colorspace.
+
+The function returns 0 on failure (invalid parameters, or fopen or malloc
+failed) or the number of bytes written on success. */
+
+int qoi_write(const char *filename, const void *data, const qoi_desc *desc);
+
+
+/* Read and decode a QOI image from the file system. If channels is 0, the
+number of channels from the file header is used. If channels is 3 or 4 the
+output format will be forced into this number of channels.
+
+The function either returns NULL on failure (invalid data, or malloc or fopen
+failed) or a pointer to the decoded pixels. On success, the qoi_desc struct
+will be filled with the description from the file header.
+
+The returned pixel data should be free()d after use. */
+
+void *qoi_read(const char *filename, qoi_desc *desc, int channels);
+
+#endif /* QOI_NO_STDIO */
+
+
+/* Encode raw RGB or RGBA pixels into a QOI image in memory.
+
+The function either returns NULL on failure (invalid parameters or malloc
+failed) or a pointer to the encoded data on success. On success the out_len
+is set to the size in bytes of the encoded data.
+
+The returned qoi data should be free()d after use. */
+
+void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len);
+
+
+/* Decode a QOI image from memory.
+
+The function either returns NULL on failure (invalid parameters or malloc
+failed) or a pointer to the decoded pixels. On success, the qoi_desc struct
+is filled with the description from the file header.
+
+The returned pixel data should be free()d after use. */
+
+void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels);
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* QOI_H */
+
+
+/* -----------------------------------------------------------------------------
+Implementation */
+
+#ifdef QOI_IMPLEMENTATION
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef QOI_MALLOC
+ #define QOI_MALLOC(sz) malloc(sz)
+ #define QOI_FREE(p) free(p)
+#endif
+#ifndef QOI_ZEROARR
+ #define QOI_ZEROARR(a) memset((a),0,sizeof(a))
+#endif
+
+#define QOI_OP_INDEX 0x00 /* 00xxxxxx */
+#define QOI_OP_DIFF 0x40 /* 01xxxxxx */
+#define QOI_OP_LUMA 0x80 /* 10xxxxxx */
+#define QOI_OP_RUN 0xc0 /* 11xxxxxx */
+#define QOI_OP_RGB 0xfe /* 11111110 */
+#define QOI_OP_RGBA 0xff /* 11111111 */
+
+#define QOI_MASK_2 0xc0 /* 11000000 */
+
+#define QOI_COLOR_HASH(C) (C.rgba.r*3 + C.rgba.g*5 + C.rgba.b*7 + C.rgba.a*11)
+#define QOI_MAGIC \
+ (((unsigned int)'q') << 24 | ((unsigned int)'o') << 16 | \
+ ((unsigned int)'i') << 8 | ((unsigned int)'f'))
+#define QOI_HEADER_SIZE 14
+
+/* 2GB is the max file size that this implementation can safely handle. We guard
+against anything larger than that, assuming the worst case with 5 bytes per
+pixel, rounded down to a nice clean value. 400 million pixels ought to be
+enough for anybody. */
+#define QOI_PIXELS_MAX ((unsigned int)400000000)
+
+typedef union {
+ struct { unsigned char r, g, b, a; } rgba;
+ unsigned int v;
+} qoi_rgba_t;
+
+static const unsigned char qoi_padding[8] = {0,0,0,0,0,0,0,1};
+
+void qoi_write_32(unsigned char *bytes, int *p, unsigned int v) {
+ bytes[(*p)++] = (0xff000000 & v) >> 24;
+ bytes[(*p)++] = (0x00ff0000 & v) >> 16;
+ bytes[(*p)++] = (0x0000ff00 & v) >> 8;
+ bytes[(*p)++] = (0x000000ff & v);
+}
+
+unsigned int qoi_read_32(const unsigned char *bytes, int *p) {
+ unsigned int a = bytes[(*p)++];
+ unsigned int b = bytes[(*p)++];
+ unsigned int c = bytes[(*p)++];
+ unsigned int d = bytes[(*p)++];
+ return a << 24 | b << 16 | c << 8 | d;
+}
+
+void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) {
+ int i, max_size, p, run;
+ int px_len, px_end, px_pos, channels;
+ unsigned char *bytes;
+ const unsigned char *pixels;
+ qoi_rgba_t index[64];
+ qoi_rgba_t px, px_prev;
+
+ if (
+ data == NULL || out_len == NULL || desc == NULL ||
+ desc->width == 0 || desc->height == 0 ||
+ desc->channels < 3 || desc->channels > 4 ||
+ desc->colorspace > 1 ||
+ desc->height >= QOI_PIXELS_MAX / desc->width
+ ) {
+ return NULL;
+ }
+
+ max_size =
+ desc->width * desc->height * (desc->channels + 1) +
+ QOI_HEADER_SIZE + sizeof(qoi_padding);
+
+ p = 0;
+ bytes = (unsigned char *) QOI_MALLOC(max_size);
+ if (!bytes) {
+ return NULL;
+ }
+
+ qoi_write_32(bytes, &p, QOI_MAGIC);
+ qoi_write_32(bytes, &p, desc->width);
+ qoi_write_32(bytes,
(Patch may be truncated, please check the link at the top of this post.)