From 44b1d5a466e246f6a39ea0c99031c8f7781c2325 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Fri, 2 Mar 2018 02:38:37 +0100 Subject: [PATCH] Try to load images with RECOIL (REtro COmputer IMage Library) --- 3rdparty/Makefile | 19 ++++++- src/.gitignore | 2 + src/Makefile | 10 ++++ src/loadrecoil.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++ src/loadsave.c | 19 +++++-- 5 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 src/.gitignore create mode 100644 src/loadrecoil.c diff --git a/3rdparty/Makefile b/3rdparty/Makefile index 655ce26e..f8f51fed 100644 --- a/3rdparty/Makefile +++ b/3rdparty/Makefile @@ -28,6 +28,11 @@ LUA=lua-5.3.4 LUAARCH=$(LUA).tar.gz LUAURL=https://www.lua.org/ftp/$(LUAARCH) # https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-0.6.1.tar.gz +RECOILVER=4.2.0 +RECOIL=recoil-$(RECOILVER) +RECOILARCH=$(RECOIL).tar.gz +#RECOILURL=https://downloads.sourceforge.net/project/recoil/recoil/$(RECOILVER)/$(RECOILARCH) +RECOILURL=https://excellmedia.dl.sourceforge.net/project/recoil/recoil/$(RECOILVER)/$(RECOILARCH) PREFIX = $(PWD)/usr @@ -53,7 +58,7 @@ endif HOST = $(shell $(CC) -dumpmachine) #HOST = i686-pc-mingw32 -.PHONY: all clean clean_archives clean_all libs libpng libsdl libsdl_image libsdl_ttf libjpeg libtiff zlib freetype lua +.PHONY: all clean clean_archives clean_all libs libpng libsdl libsdl_image libsdl_ttf libjpeg libtiff zlib freetype lua recoil all: libs @@ -61,7 +66,7 @@ clean_all: clean clean_archives clean: $(RM) -r usr/ $(LIBPNG) $(ZLIB) $(SDLIMAGE) $(JPEGDIR) $(LIBTIFF) - $(RM) -r $(SDLTTF) $(FREETYPE) SDL-1.2.15 $(LUA) + $(RM) -r $(SDLTTF) $(FREETYPE) SDL-1.2.15 $(LUA) $(RECOIL) clean_archives: $(RM) -r archives @@ -193,6 +198,12 @@ $(ZLIB)/.ok: archives/$(ZLIBARCH) $(TAR) xzf $< touch $@ +recoil: $(RECOIL)/.ok + +$(RECOIL)/.ok: archives/$(RECOILARCH) + $(TAR) xzf $< + touch $@ + archives/$(SDLDEVEL): @$(MKDIR) $(@D) cd $(@D) && $(GETURL) $(SDLDEVELURL) @@ -228,3 +239,7 @@ archives/$(FREETYPEARCH): archives/$(LUAARCH): @$(MKDIR) $(@D) cd $(@D) && $(GETURL) $(LUAURL) + +archives/$(RECOILARCH): + @$(MKDIR) $(@D) + cd $(@D) && $(GETURL) $(RECOILURL) diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 00000000..a76ba8b2 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +recoil.c +recoil.h diff --git a/src/Makefile b/src/Makefile index c07b67f6..5e9f2102 100644 --- a/src/Makefile +++ b/src/Makefile @@ -50,6 +50,8 @@ endif CFLAGS_CACHE = .cflags.cache + RECOILVER = 4.2.0 + ### PLATFORM DETECTION AND CONFIGURATION ### PLATFORMOBJ = @@ -545,6 +547,7 @@ OBJS = main.o init.o graph.o sdlscreen.o misc.o special.o \ fileformats.o miscfileformats.o libraw2crtc.o \ brush_ops.o buttons_effects.o layers.o \ oldies.o tiles.o colorred.o unicode.o +OBJS += loadrecoil.o recoil.o OBJ = $(addprefix $(OBJDIR)/,$(OBJS)) SKINS = skin_classic.png skin_modern.png skin_DPaint.png \ @@ -713,6 +716,13 @@ $(OBJDIR)/versiontag: pversion.c $(REVISION_CACHE) RES := $(shell if [ ! -f $(CFLAGS_CACHE) ] || [ "`cat $(CFLAGS_CACHE)`" != "$(COPT) $(CFLAGS)" ] ; then echo "$(COPT) $(CFLAGS)" > $(CFLAGS_CACHE) ; fi ) +recoil.c: ../3rdparty/recoil-$(RECOILVER)/recoil.c + $(CP) $< $@ + $(CP) $(subst .c,.h,$< $@) + +../3rdparty/recoil-$(RECOILVER)/recoil.c: + $(MAKE) -C ../3rdparty recoil + $(OBJ): $(CFLAGS_CACHE) $(OBJDIR)/%.o : %.c diff --git a/src/loadrecoil.c b/src/loadrecoil.c new file mode 100644 index 00000000..4930f7ab --- /dev/null +++ b/src/loadrecoil.c @@ -0,0 +1,142 @@ +/* vim:expandtab:ts=2 sw=2: +*/ +/* Grafx2 - The Ultimate 256-color bitmap paint program + + Copyright 2018 Thomas Bernard + + Grafx2 is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 + of the License. + + Grafx2 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Grafx2; if not, see +*/ + + +#include "struct.h" +#include "global.h" +#include "loadsave.h" +#include "io.h" +#include "errors.h" + +#include "recoil.h" + +void Load_Recoil_Image(T_IO_Context *context) +{ + RECOIL *recoil; + byte * file_content; + unsigned long file_length; + FILE * f; + + f = Open_file_read(context); + if (f == NULL) + { + File_error = 1; + return; + } + file_length = File_length_file(f); + if (file_length == 0) + { + fclose(f); + File_error = 1; + return; + } + file_content = malloc(file_length); + if (file_content == NULL) + { + Warning("Memory allocation error"); + fclose(f); + File_error = 1; + return; + } + if (!Read_bytes(f, file_content, file_length)) + { + Warning("Read error"); + fclose(f); + free(file_content); + File_error = 1; + return; + } + fclose(f); + + recoil = RECOIL_New(); + if (recoil) + { + if (RECOIL_Decode(recoil, context->File_name, file_content, file_length)) + { + int width, height; + int x, y; + byte * pixels; + const int *palette; + enum PIXEL_RATIO ratio = PIXEL_SIMPLE; + + width = RECOIL_GetWidth(recoil); + height = RECOIL_GetHeight(recoil); + pixels = malloc(width * height); + if (pixels == NULL) + { + Warning("Memory allocation error"); + File_error = 1; + } + else + { + // try to convert to 8bpp image + File_error = 0; + palette = RECOIL_ToPalette(recoil, pixels); + if (palette == NULL) + { + // 24bits + const int * tc_pixels; + + Pre_load(context, width, height, file_length, FORMAT_MISC, ratio, 24); + tc_pixels = RECOIL_GetPixels(recoil); + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + { + Set_pixel_24b(context, x, y, *tc_pixels >> 16, *tc_pixels >> 8, *tc_pixels); + tc_pixels++; + } + } + else + { + // 8bits + int i; + int bpp; + int ncolors = RECOIL_GetColors(recoil); + const byte * p = pixels; + + bpp = 8; + while (ncolors <= (1 << (bpp - 1))) + bpp--; + if (Config.Clear_palette) + memset(context->Palette,0,sizeof(T_Palette)); + for (i = 0; i < ncolors; i++) + { + context->Palette[i].R = (byte)(palette[i] >> 16); + context->Palette[i].G = (byte)(palette[i] >> 8); + context->Palette[i].B = (byte)(palette[i]); + } + Pre_load(context, width, height, file_length, FORMAT_MISC, ratio, bpp); + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + { + Set_pixel(context, x, y, *p++); + } + } + free(pixels); + if (!File_error) + { + snprintf(context->Comment, COMMENT_SIZE + 1, "RECOIL: %s", RECOIL_GetPlatform(recoil)); + } + } + } + RECOIL_Delete(recoil); + } + free(file_content); +} diff --git a/src/loadsave.c b/src/loadsave.c index bd5665b0..15feca19 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -174,6 +174,10 @@ void Save_PNG(T_IO_Context *); // (TGA, BMP, PNM, XPM, XCF, PCX, GIF, JPG, TIF, IFF, PNG, ICO) void Load_SDL_Image(T_IO_Context *); +// -- Recoil ---------------------------------------------------------------- +// 8bits and 16bits computer graphics +void Load_Recoil_Image(T_IO_Context *); + // ENUM Name TestFunc LoadFunc SaveFunc PalOnly Comment Layers Ext Exts const T_Format File_formats[] = { {FORMAT_ALL_IMAGES, "(all)", NULL, NULL, NULL, 0, 0, 0, "", "gif;png;bmp;2bp;pcx;pkm;iff;lbm;ilbm;sham;ham;ham6;ham8;acbm;pic;anim;img;sci;scq;scf;scn;sco;pi1;pc1;cel;neo;c64;koa;koala;fli;bml;cdu;prg;tga;pnm;xpm;xcf;jpg;jpeg;tif;tiff;ico;ic2;cur;cm5;pph"}, @@ -661,8 +665,13 @@ void Load_image(T_IO_Context *context) if (File_error) { context->Format = DEFAULT_FILEFORMAT; - // Last try: with SDL_image - Load_SDL_Image(context); + // try with recoil + Load_Recoil_Image(context); + if (File_error) + { + // Last try: with SDL_image + Load_SDL_Image(context); + } if (File_error) { @@ -1120,10 +1129,10 @@ void Save_image(T_IO_Context *context) return; } } - - + + void Load_SDL_Image(T_IO_Context *context) -{ +{ char filename[MAX_PATH_CHARACTERS]; // Nom complet du fichier word x_pos,y_pos; // long file_size;