WebP support for radiant and q3map2.
authorStijn Buys <ingar@osirion.org>
Sun, 30 Jun 2013 12:11:59 +0000 (14:11 +0200)
committerThomas Debesse <dev@illwieckz.net>
Sun, 28 Oct 2018 22:21:24 +0000 (23:21 +0100)
Makefile
plugins/imagewebp/imagewebp.def [new file with mode: 0644]
plugins/imagewebp/plugin.cpp [new file with mode: 0644]
plugins/imagewebp/plugin.h [new file with mode: 0644]
tools/quake3/q3map2/image.c

index 0a2d4b9..99cfc90 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -67,6 +67,9 @@ LIBS_XML           ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) li
 CPPFLAGS_PNG       ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libpng --cflags $(STDERR_TO_DEVNULL))
 LIBS_PNG           ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libpng --libs-only-L $(STDERR_TO_DEVNULL)) \
                       $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libpng --libs-only-l $(STDERR_TO_DEVNULL))
+CPPFLAGS_WEBP      ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libwebp --cflags $(STDERR_TO_DEVNULL))
+LIBS_WEBP          ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libwebp --libs-only-L $(STDERR_TO_DEVNULL)) \
+                      $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) libwebp --libs-only-l $(STDERR_TO_DEVNULL))
 CPPFLAGS_GTK       ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) gtk+-2.0 --cflags $(STDERR_TO_DEVNULL))
 LIBS_GTK           ?= $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) gtk+-2.0 --libs-only-L $(STDERR_TO_DEVNULL)) \
                       $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKGCONFIG) gtk+-2.0 --libs-only-l $(STDERR_TO_DEVNULL))
@@ -351,6 +354,7 @@ dependencies-check:
        checkheader libglib2.0-dev glib.h g_path_is_absolute "$(CPPFLAGS_GLIB)" "$(LIBS_GLIB)"; \
        checkheader libxml2-dev libxml/xpath.h xmlXPathInit "$(CPPFLAGS_XML)" "$(LIBS_XML)"; \
        checkheader libpng12-dev png.h png_create_read_struct "$(CPPFLAGS_PNG)" "$(LIBS_PNG)"; \
+       checkheader libwebp-dev webp/decode.h WebPGetInfo "$(CPPFLAGS_WEBP)" "$(LIBS_WEBP)"; \
        checkheader "mesa-common-dev (or another OpenGL library)" GL/gl.h glClear "$(CPPFLAGS_GL)" "$(LIBS_GL)"; \
        checkheader libgtk2.0-dev gtk/gtkdialog.h gtk_dialog_run "$(CPPFLAGS_GTK)" "$(LIBS_GTK)"; \
        checkheader libpango1.0-dev pango/pangoft2.h pango_ft2_font_map_new "$(CPPFLAGS_PANGOFT2)" "$(LIBS_PANGOFT2)"; \
@@ -381,6 +385,7 @@ binaries-radiant-modules: \
        $(INSTALLDIR)/modules/image.$(DLL) \
        $(INSTALLDIR)/modules/imagehl.$(DLL) \
        $(INSTALLDIR)/modules/imagepng.$(DLL) \
+       $(INSTALLDIR)/modules/imagewebp.$(DLL) \
        $(INSTALLDIR)/modules/imageq2.$(DLL) \
        $(INSTALLDIR)/modules/mapq3.$(DLL) \
        $(INSTALLDIR)/modules/mapxml.$(DLL) \
@@ -483,8 +488,8 @@ endif
        $(CC) $< $(CFLAGS) $(CFLAGS_COMMON) $(CPPFLAGS_EXTRA) $(CPPFLAGS_COMMON) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@
 
 
-$(INSTALLDIR)/q3map2.$(EXE): LIBS_EXTRA := $(LIBS_XML) $(LIBS_GLIB) $(LIBS_PNG) $(LIBS_JPEG) $(LIBS_ZLIB)
-$(INSTALLDIR)/q3map2.$(EXE): CPPFLAGS_EXTRA := $(CPPFLAGS_XML) $(CPPFLAGS_GLIB) $(CPPFLAGS_PNG) $(CPPFLAGS_JPEG) -Itools/quake3/common -Ilibs -Iinclude
+$(INSTALLDIR)/q3map2.$(EXE): LIBS_EXTRA := $(LIBS_XML) $(LIBS_GLIB) $(LIBS_PNG) $(LIBS_JPEG) $(LIBS_WEBP) $(LIBS_ZLIB)
+$(INSTALLDIR)/q3map2.$(EXE): CPPFLAGS_EXTRA := $(CPPFLAGS_XML) $(CPPFLAGS_GLIB) $(CPPFLAGS_PNG) $(CPPFLAGS_JPEG) $(CPPFLAGS_WEBP) -Itools/quake3/common -Ilibs -Iinclude
 $(INSTALLDIR)/q3map2.$(EXE): \
        tools/quake3/common/cmdlib.o \
        tools/quake3/common/imagelib.o \
@@ -824,6 +829,11 @@ $(INSTALLDIR)/modules/imagepng.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_PNG) -Ilibs
 $(INSTALLDIR)/modules/imagepng.$(DLL): \
        plugins/imagepng/plugin.o \
 
+$(INSTALLDIR)/modules/imagewebp.$(DLL): LIBS_EXTRA := $(LIBS_WEBP)
+$(INSTALLDIR)/modules/imagewebp.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_WEBP) -Ilibs -Iinclude
+$(INSTALLDIR)/modules/imagewebp.$(DLL): \
+       plugins/imagewebp/plugin.o \
+
 $(INSTALLDIR)/modules/mapq3.$(DLL): CPPFLAGS_EXTRA := $(CPPFLAGS_GLIB) -Ilibs -Iinclude
 $(INSTALLDIR)/modules/mapq3.$(DLL): \
        plugins/mapq3/parse.o \
diff --git a/plugins/imagewebp/imagewebp.def b/plugins/imagewebp/imagewebp.def
new file mode 100644 (file)
index 0000000..d04effc
--- /dev/null
@@ -0,0 +1,7 @@
+; imagewebp.def : Declares the module parameters for the DLL.
+
+LIBRARY      "IMAGEWEBP"
+
+EXPORTS
+    ; Explicit exports can go here
+       Radiant_RegisterModules @1
diff --git a/plugins/imagewebp/plugin.cpp b/plugins/imagewebp/plugin.cpp
new file mode 100644 (file)
index 0000000..0dcd7ed
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+   Copyright (C) 1999-2006 Id Software, Inc. and contributors.
+   For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+   This file is part of NetRadiant.
+
+   NetRadiant 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; either version 2 of the License, or
+   (at your option) any later version.
+
+   NetRadiant 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 NetRadiant; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "plugin.h"
+#include "debugging/debugging.h"
+#include "ifilesystem.h"
+#include "iimage.h"
+
+#include "imagelib.h"
+
+// ====== WEBP loader functionality ======
+
+#include "webp/decode.h"
+
+Image* LoadWEBPBuff( unsigned char* buffer, size_t buffer_length ){
+       int image_width;
+       int image_height;
+
+       if ( !WebPGetInfo( (byte *) buffer, buffer_length, &image_width, &image_height) ){
+               globalErrorStream() << "libwebp error: WebPGetInfo: can't get image info\n";
+               return 0;
+       }
+           
+       // allocate the pixel buffer
+       RGBAImage* image = new RGBAImage( image_width, image_height );
+       int out_stride = image_width  *sizeof(RGBAPixel);
+       int out_size =  image_height * out_stride;
+       
+        if ( !WebPDecodeRGBAInto( (byte *) buffer, buffer_length, image->getRGBAPixels(), out_size, out_stride ) )
+        {
+                return 0;
+        }
+
+       return image;
+}
+
+Image* LoadWEBP( ArchiveFile& file ){
+       ScopedArchiveBuffer buffer( file );
+       return LoadWEBPBuff( buffer.buffer, buffer.length );
+}
+
+
+#include "modulesystem/singletonmodule.h"
+
+
+class ImageDependencies : public GlobalFileSystemModuleRef
+{
+};
+
+class ImageWEBPAPI
+{
+_QERPlugImageTable m_imagewebp;
+public:
+typedef _QERPlugImageTable Type;
+STRING_CONSTANT( Name, "webp" );
+
+ImageWEBPAPI(){
+       m_imagewebp.loadImage = LoadWEBP;
+}
+_QERPlugImageTable* getTable(){
+       return &m_imagewebp;
+}
+};
+
+typedef SingletonModule<ImageWEBPAPI, ImageDependencies> ImageWEBPModule;
+
+ImageWEBPModule g_ImageWEBPModule;
+
+
+extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
+       initialiseModule( server );
+
+       g_ImageWEBPModule.selfRegister();
+}
diff --git a/plugins/imagewebp/plugin.h b/plugins/imagewebp/plugin.h
new file mode 100644 (file)
index 0000000..28a2933
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+   Copyright (C) 1999-2006 Id Software, Inc. and contributors.
+   For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+   This file is part of NetRadiant.
+
+   NetRadiant 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; either version 2 of the License, or
+   (at your option) any later version.
+
+   NetRadiant 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 NetRadiant; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#if !defined( INCLUDED_PLUGIN_H )
+#define INCLUDED_PLUGIN_H
+
+#endif
index 4fb3f74..c947085 100644 (file)
@@ -36,7 +36,7 @@
 /* dependencies */
 #include "q3map2.h"
 
-
+#include "webp/decode.h"
 
 /* -------------------------------------------------------------------------------
 
@@ -248,6 +248,34 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
 
 
 
+static void LoadWEBPBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
+
+       int image_width;
+       int image_height;
+       
+       if ( !WebPGetInfo( buffer, ( size_t) size, &image_width, &image_height ) )
+       {
+               Sys_Printf( "WARNING: An error occurred reading WEBP image info\n" );
+               return;
+       }
+
+       /* create image pixel buffer */
+       *pixels = safe_malloc( image_width * image_height * 4 );
+       *width = image_width;
+       *height = image_height;
+
+       int out_stride = image_width  * 4;
+       int out_size =  image_height * out_stride;
+
+               if ( !WebPDecodeRGBAInto( buffer, (size_t) size, *pixels, out_size, out_stride ) )
+               {
+               Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image\n" );
+                       return;
+               }
+}
+
+
+
 /*
    ImageInit()
    implicitly called by every function to set up image list
@@ -472,6 +500,15 @@ image_t *ImageLoad( const char *filename ){
                        break;
                }
                #endif // BUILD_CRUNCH
+
+               /* attempt to load webp */
+               StripExtension( name );
+               strcat( name, ".webp" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadWEBPBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+                       break;
+               }
        } while (qfalse);
 
        /* free file buffer */