├── COPYING ├── Makefile ├── README.md ├── hash.sh ├── imlib2-version.sh ├── imlib2_common.h ├── loader.h ├── loader_webp.c └── sample ├── commands.mk ├── imlib2-thumbnailer.c ├── makefile ├── webp-thumbnailer.desktop ├── webp-thumbnailer.schemas └── webp.xml /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) The Regents of the University of California. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 3. Neither the name of the University nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 | SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TARGET = webp.so 2 | MAJOR = 1 3 | MINOR = 1 4 | PATCH = 3 5 | 6 | OPTS := -O2 7 | CFLAGS := -std=c99 $(OPTS) $(shell pkg-config imlib2 --cflags) -fPIC -Wall 8 | LDFLAGS := $(shell pkg-config imlib2 --libs) $(shell pkg-config imlib2 --libs libwebp) $(shell pkg-config imlib2 --libs libwebpdemux) 9 | 10 | SRC = $(wildcard *.c) 11 | OBJ = $(foreach obj, $(SRC:.c=.o), $(notdir $(obj))) 12 | DEP = $(SRC:.c=.d) 13 | 14 | LIBDIR ?= $(shell pkg-config imlib2 --variable=libdir) 15 | LOADERDIR ?= $(LIBDIR)/imlib2/loaders/ 16 | 17 | version = $(MAJOR).$(MINOR).$(PATCH) 18 | CFLAGS += -DVERSION="\"$(version)\"" 19 | 20 | commit = $(shell ./hash.sh) 21 | ifneq ($(commit), UNKNOWN) 22 | CFLAGS += -DCOMMIT="\"$(commit)\"" 23 | endif 24 | 25 | imlib2_version = $(shell ./imlib2-version.sh) 26 | ifeq ($(imlib2_version), "") 27 | $(error cannot guess imlib2 version) 28 | endif 29 | CFLAGS += $(imlib2_version) 30 | 31 | 32 | ifndef DISABLE_DEBUG 33 | CFLAGS += -ggdb 34 | endif 35 | 36 | .PHONY: all clean install 37 | 38 | all: $(TARGET) 39 | 40 | $(TARGET): $(OBJ) 41 | $(CC) -shared -o $@ $^ $(LDFLAGS) 42 | 43 | %.o: %.c 44 | $(CC) -Wp,-MMD,$*.d -c $(CFLAGS) -o $@ $< 45 | 46 | clean: 47 | rm -f $(DEP) 48 | rm -f $(OBJ) 49 | rm -f $(TARGET) 50 | 51 | install: 52 | install -d $(DESTDIR)$(LOADERDIR) 53 | install -s -m 444 webp.so $(DESTDIR)$(LOADERDIR) 54 | 55 | uninstall: 56 | rm -f $(LOADERDIR)/$(TARGET) 57 | 58 | -include $(DEP) 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Imlib2-WebP 2 | 3 | A WebP loader for Imlib2. Published under the BSD license. 4 | 5 | ## About WebP 6 | 7 | WebP is a new image format that provides lossy compression for photographic 8 | images. In a large scale study of 900,000 web images, WebP images were 39.8% 9 | smaller than jpeg images of similar quality. 10 | 11 | A WebP file consists of VP8 image data, and a container based on RIFF. The 12 | standalone libwebp library serves as a reference implementation for the 13 | WebP specification and is available at this git repository and as a tarball. 14 | Webmasters and web developers can use the WebP image format to create smaller 15 | and better looking images that can help make the web faster. 16 | 17 | You may find more informations at http://code.google.com/speed/webp 18 | 19 | ## About Imlib2 20 | 21 | Imlib2 is an advanced replacement for libraries like libXpm. Imlib2 provides 22 | many more features with much greater flexibility and speed than standard 23 | libraries, including font rasterization, rotation, RGBA space rendering and 24 | blending, dynamic binary filters, scripting, and more. 25 | 26 | You may find more informations at http://docs.enlightenment.org/api/imlib2/html 27 | 28 | ## INSTALLATION 29 | 30 | You will need a C compiler like gcc, make and the following dependencies : 31 | 32 | * libimlib2-dev 33 | * libwebp-dev 34 | * pkg-config 35 | 36 | On debian use the following command as root to install the required packages : 37 | 38 | # aptitude install libimlib2-dev libwebp-dev pkg-config 39 | 40 | Then enter the following command : 41 | 42 | $ make 43 | $ sudo make install 44 | 45 | This will install the webp loader for Imlib2. 46 | 47 | Please take advice that BSD make probably won't work, you need to use gmake 48 | instead. 49 | -------------------------------------------------------------------------------- /hash.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # script to determine git hash of current source tree 3 | # try to use whatever git tells us if there is a .git folder 4 | if [ -d .git -a -r .git ] 5 | then 6 | hash=$(git log 2>/dev/null | head -n1 2>/dev/null | sed "s/.* //" 2>/dev/null) 7 | fi 8 | 9 | if [ x"$hash" != x ] 10 | then 11 | echo $hash 12 | else 13 | echo "UNKNOWN" 14 | fi 15 | exit 0 16 | -------------------------------------------------------------------------------- /imlib2-version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # script to determine the version of imlib2 3 | 4 | version=$(pkg-config imlib2 --version) 5 | major=$(echo "$version" | cut -d'.' -f1) 6 | minor=$(echo "$version" | cut -d'.' -f2) 7 | patch=$(echo "$version" | cut -d'.' -f3) 8 | 9 | echo "-DIMLIB2_VERSION_MAJOR=$major -DIMLIB2_VERSION_MINOR=$minor -DIMLIB2_VERSION_PATCH=$patch" 10 | -------------------------------------------------------------------------------- /imlib2_common.h: -------------------------------------------------------------------------------- 1 | /* File: imlib2_common.h 2 | Time-stamp: <2011-10-10 00:41:40 gawen> 3 | 4 | Copyright (c) 2011 David Hauweele 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions 9 | are met: 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 3. Neither the name of the University nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | SUCH DAMAGE. */ 30 | 31 | #ifndef _IMLIB2_COMMON_H_ 32 | #define _IMLIB2_COMMON_H_ 33 | 34 | /* most of this file comes directly from imlib2 source code */ 35 | 36 | #include 37 | 38 | #define UNSET_FLAGS(flags, f) ((flags) &= ~(f)) 39 | #define SET_FLAGS(flags, f) ((flags) |= (f)) 40 | 41 | typedef struct _imlibimage ImlibImage; 42 | # ifdef BUILD_X11 43 | typedef struct _imlibimagepixmap ImlibImagePixmap; 44 | # endif 45 | typedef struct _imlibborder ImlibBorder; 46 | typedef struct _imlibloader ImlibLoader; 47 | typedef struct _imlibimagetag ImlibImageTag; 48 | 49 | typedef int (*ImlibProgressFunction)(ImlibImage *im, char percent, 50 | int update_x, int update_y, 51 | int update_w, int update_h); 52 | typedef void (*ImlibDataDestructorFunction)(ImlibImage *im, void *data); 53 | 54 | enum _iflags 55 | { 56 | F_NONE = 0, 57 | F_HAS_ALPHA = (1 << 0), 58 | F_UNLOADED = (1 << 1), 59 | F_UNCACHEABLE = (1 << 2), 60 | F_ALWAYS_CHECK_DISK = (1 << 3), 61 | F_INVALID = (1 << 4), 62 | F_DONT_FREE_DATA = (1 << 5), 63 | F_FORMAT_IRRELEVANT = (1 << 6), 64 | F_BORDER_IRRELEVANT = (1 << 7), 65 | F_ALPHA_IRRELEVANT = (1 << 8) 66 | }; 67 | 68 | typedef enum _iflags ImlibImageFlags; 69 | 70 | struct _imlibborder 71 | { 72 | int left, right, top, bottom; 73 | }; 74 | 75 | struct _imlibimagetag 76 | { 77 | char *key; 78 | int val; 79 | void *data; 80 | void (*destructor)(ImlibImage *im, void *data); 81 | ImlibImageTag *next; 82 | }; 83 | 84 | struct _imlibimage 85 | { 86 | char *file; 87 | int w, h; 88 | DATA32 *data; 89 | ImlibImageFlags flags; 90 | time_t moddate; 91 | ImlibBorder border; 92 | int references; 93 | ImlibLoader *loader; 94 | char *format; 95 | ImlibImage *next; 96 | ImlibImageTag *tags; 97 | char *real_file; 98 | char *key; 99 | }; 100 | 101 | # ifdef BUILD_X11 102 | struct _imlibimagepixmap 103 | { 104 | int w, h; 105 | Pixmap pixmap, mask; 106 | Display *display; 107 | Visual *visual; 108 | int depth; 109 | int source_x, source_y, source_w, source_h; 110 | Colormap colormap; 111 | char antialias, hi_quality, dither_mask; 112 | ImlibBorder border; 113 | ImlibImage *image; 114 | char *file; 115 | char dirty; 116 | int references; 117 | DATABIG modification_count; 118 | ImlibImagePixmap *next; 119 | }; 120 | # endif 121 | 122 | struct _imlibloader 123 | { 124 | char *file; 125 | int num_formats; 126 | char **formats; 127 | void *handle; 128 | char (*load)(ImlibImage *im, 129 | ImlibProgressFunction progress, 130 | char progress_granularity, char immediate_load); 131 | char (*save)(ImlibImage *im, 132 | ImlibProgressFunction progress, 133 | char progress_granularity); 134 | ImlibLoader *next; 135 | }; 136 | 137 | # define IMAGE_HAS_ALPHA(im) ((im)->flags & F_HAS_ALPHA) 138 | # define IMAGE_IS_UNLOADED(im) ((im)->flags & F_UNLOADED) 139 | # define IMAGE_IS_UNCACHEABLE(im) ((im)->flags & F_UNCACHEABLE) 140 | # define IMAGE_ALWAYS_CHECK_DISK(im) ((im)->flags & F_ALWAYS_CHECK_DISK) 141 | # define IMAGE_IS_VALID(im) (!((im)->flags & F_INVALID)) 142 | # define IMAGE_FREE_DATA(im) (!((im)->flags & F_DONT_FREE_DATA)) 143 | 144 | # define SET_FLAG(flags, f) ((flags) |= (f)) 145 | # define UNSET_FLAG(flags, f) ((flags) &= (~f)) 146 | 147 | # define IMAGE_DIMENSIONS_OK(w, h) \ 148 | ( ((w) > 0) && ((h) > 0) && \ 149 | ((unsigned long long)(w) * (unsigned long long)(h) <= (1ULL << 29) - 1) ) 150 | 151 | EAPI ImlibImageTag *__imlib_GetTag(ImlibImage *im, const char *key); 152 | 153 | #endif /* _IMLIB2_COMMON_H_ */ 154 | -------------------------------------------------------------------------------- /loader.h: -------------------------------------------------------------------------------- 1 | /* File: loader.h 2 | Time-stamp: <2011-10-10 01:04:37 gawen> 3 | 4 | Copyright (c) 2011 David Hauweele 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions 9 | are met: 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 3. Neither the name of the University nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | SUCH DAMAGE. */ 30 | 31 | #ifndef _LOADER_H_ 32 | #define _LOADER_H_ 33 | 34 | #include "imlib2_common.h" 35 | 36 | EAPI char load(ImlibImage * im, ImlibProgressFunction progress, 37 | char progress_granularity, char immediate_load); 38 | EAPI char save(ImlibImage *im, ImlibProgressFunction progress, 39 | char progress_granularity); 40 | EAPI void formats(ImlibLoader *l); 41 | 42 | #endif /* _LOADER_H_ */ 43 | -------------------------------------------------------------------------------- /loader_webp.c: -------------------------------------------------------------------------------- 1 | /* File: loader_webp.c 2 | Time-stamp: <2012-12-09 21:19:30 gawen> 3 | 4 | Copyright (c) 2011 David Hauweele 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions 9 | are met: 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 3. Neither the name of the University nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | SUCH DAMAGE. */ 30 | 31 | #define _BSD_SOURCE 1 32 | #define _DEFAULT_SOURCE 1 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include "imlib2_common.h" 47 | #include "loader.h" 48 | 49 | /* Store version and commit as unused string constant. */ 50 | #ifdef VERSION 51 | const char loader_webp_version[] = VERSION; 52 | #endif 53 | #ifdef COMMIT 54 | const char loader_webp_commit[] = COMMIT; 55 | #endif 56 | 57 | #if (IMLIB2_VERSION_MAJOR >= 1 && IMLIB2_VERSION_MINOR >= 6) 58 | DATA32 * __imlib_AllocateData(ImlibImage *im); 59 | void __imlib_FreeData(ImlibImage *im); 60 | #else 61 | /* These symbols do not exist in Imlib2 <1.6.0, we redefine them. */ 62 | static DATA32 * __imlib_AllocateData(ImlibImage *im) 63 | { 64 | int w = im->w; 65 | int h = im->h; 66 | 67 | if(w <= 0 || h <= 0) 68 | return NULL; 69 | 70 | im->data = malloc(w * h * sizeof(DATA32)); 71 | 72 | return im->data; 73 | } 74 | 75 | static void __imlib_FreeData(ImlibImage *im) 76 | { 77 | if(im->data) { 78 | free(im->data); 79 | im->data = NULL; 80 | } 81 | 82 | im->w = 0; 83 | im->h = 0; 84 | } 85 | #endif /* IMLIB2_VERSION */ 86 | 87 | static uint8_t * read_file(const char *filename, size_t *size, 88 | ImlibProgressFunction progress) 89 | { 90 | struct stat buf; 91 | uint8_t *data = NULL; 92 | int fd; 93 | 94 | #ifndef __EMX__ 95 | if((fd = open(filename, O_RDONLY)) < 0) 96 | #else 97 | if((fd = open(filename, O_RDONLY | O_BINARY)) < 0) 98 | #endif 99 | return NULL; 100 | 101 | if(fstat(fd, &buf) < 0 || 102 | !(data = malloc(buf.st_size))) 103 | goto EXIT; 104 | 105 | *size = read(fd, data, buf.st_size); 106 | 107 | EXIT: 108 | close(fd); 109 | return data; 110 | } 111 | 112 | char load(ImlibImage * im, ImlibProgressFunction progress, 113 | char progress_granularity, char immediate_load) 114 | { 115 | uint8_t *data; 116 | size_t size; 117 | int has_alpha; 118 | int has_animation; 119 | char ret = 0; 120 | #if (WEBP_DECODER_ABI_VERSION >= 0x200) 121 | WebPBitstreamFeatures features; 122 | #endif 123 | 124 | /* Load the WebP file into one single buffer. */ 125 | if(!(data = read_file(im->real_file, &size, progress))) 126 | return 0; 127 | 128 | /* Extract WebP features. */ 129 | #if (WEBP_DECODER_ABI_VERSION >= 0x200) 130 | if(WebPGetFeatures(data, size, &features) != VP8_STATUS_OK) 131 | goto EXIT; 132 | im->w = features.width; 133 | im->h = features.height; 134 | has_alpha = features.has_alpha; 135 | has_animation = features.has_animation; 136 | #else /* compatibility with versions <= 0.1.3 */ 137 | if (!WebPGetInfo(data, size, &im->w, &im->h)) 138 | goto EXIT; 139 | has_alpha = 0; 140 | has_animation = 0; 141 | #endif 142 | 143 | if(!IMAGE_DIMENSIONS_OK(im->w, im->h)) 144 | goto EXIT; 145 | 146 | if(!has_alpha) 147 | UNSET_FLAGS(im->flags, F_HAS_ALPHA); 148 | else 149 | SET_FLAGS(im->flags, F_HAS_ALPHA); 150 | 151 | im->format = strdup("webp"); 152 | 153 | /* If this was not true, then we are only interested 154 | in the image size and format so we stop here. */ 155 | if(im->loader || immediate_load || progress) { 156 | /* Now we are commited to load the image. 157 | Note that Imlib2 would call __imlib_FreeData(im) 158 | if we return with an error code. But we still free 159 | manually to ensure it works with Imlib2 <1.6.0. */ 160 | __imlib_AllocateData(im); 161 | 162 | if(has_animation) { 163 | /* Unfortunately Imlib2 does not support animation. 164 | So we only decode the first frame for animated WebP. */ 165 | struct WebPData webp_data = { 166 | .bytes = (uint8_t*)data, 167 | .size = size 168 | }; 169 | 170 | WebPDemuxer* demux = WebPDemux(&webp_data); 171 | WebPIterator iter; 172 | WebPDecoderConfig config; 173 | 174 | WebPInitDecoderConfig(&config); 175 | 176 | config.options.use_threads = 1; 177 | config.output.colorspace = MODE_BGRA; 178 | 179 | config.output.u.RGBA.rgba = (uint8_t *)im->data; 180 | config.output.u.RGBA.stride = im->w * sizeof(DATA32); 181 | config.output.u.RGBA.size = config.output.u.RGBA.stride * im->h; 182 | config.output.is_external_memory = 1; 183 | 184 | if(WebPDemuxGetFrame(demux, 1, &iter) && 185 | WebPDecode(iter.fragment.bytes, iter.fragment.size, &config) == VP8_STATUS_OK) 186 | ret = 1; 187 | 188 | WebPDemuxReleaseIterator(&iter); 189 | WebPDemuxDelete(demux); 190 | 191 | if(!ret) 192 | goto ERROR; 193 | } 194 | else if (!WebPDecodeBGRAInto(data, size, (uint8_t *)im->data, im->w * im->h * sizeof(DATA32), im->w * sizeof(DATA32))) 195 | goto ERROR; 196 | } 197 | 198 | if(progress) 199 | progress(im, 100, 0, 0, im->w, im->h); 200 | 201 | ret = 1; 202 | goto EXIT; 203 | 204 | ERROR: 205 | __imlib_FreeData(im); 206 | EXIT: 207 | free(data); 208 | return ret; 209 | } 210 | 211 | char save(ImlibImage *im, ImlibProgressFunction progress, 212 | char progress_granularity) 213 | { 214 | ImlibImageTag *tag; 215 | uint8_t *data; 216 | float fqual; 217 | size_t size; 218 | int fd; 219 | int quality = 75; 220 | char ret = 0; 221 | 222 | /* Open the destination file. */ 223 | #ifndef __EMX__ 224 | if((fd = open(im->real_file, O_WRONLY | O_CREAT, 225 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) 226 | #else 227 | if((fd = open(im->real_file, O_WRONLY | O_CREAT | O_BINARY, 228 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) 229 | #endif 230 | return 0; 231 | 232 | /* Look for tags attached to image to get extra parameters like quality 233 | settings etc. - this is the "api" to hint for extra information for 234 | saver modules */ 235 | tag = __imlib_GetTag(im, "compression"); 236 | if(tag) { 237 | int compression = tag->val; 238 | 239 | if(compression < 0) 240 | compression = 0; 241 | else if(compression > 9) 242 | compression = 9; 243 | 244 | quality = ((9 - compression) * 100) / 9; 245 | } 246 | 247 | tag = __imlib_GetTag(im, "quality"); 248 | if(tag) { 249 | quality = tag->val; 250 | 251 | if(quality < 0) 252 | quality = 0; 253 | else if(quality > 100) 254 | quality = 100; 255 | } 256 | 257 | fqual = (float)quality; 258 | 259 | if(!(size = WebPEncodeBGRA((const uint8_t *)im->data, im->w, im->h, 260 | im->w << 2, fqual, &data))) 261 | goto EXIT; 262 | 263 | if(write(fd, data, size) != (ssize_t)size) 264 | goto EXIT; 265 | 266 | if(progress) 267 | progress(im, 100, 0, 0, im->w, im->h); 268 | 269 | ret = 1; 270 | 271 | EXIT: 272 | close(fd); 273 | if(data) 274 | free(data); 275 | return ret; 276 | } 277 | 278 | void formats(ImlibLoader *l) 279 | { 280 | int i; 281 | char *list_formats[] = { "webp" }; 282 | 283 | l->num_formats = (sizeof(list_formats) / sizeof(char *)); 284 | l->formats = malloc(sizeof(char *) * l->num_formats); 285 | for(i = 0 ; i < l->num_formats ; i++) 286 | l->formats[i] = strdup(list_formats[i]); 287 | } 288 | -------------------------------------------------------------------------------- /sample/commands.mk: -------------------------------------------------------------------------------- 1 | ../commands.mk -------------------------------------------------------------------------------- /sample/imlib2-thumbnailer.c: -------------------------------------------------------------------------------- 1 | /* File: imlib2-thumbnailer.c 2 | Time-stamp: <2011-10-10 21:07:35 gawen> 3 | 4 | Copyright (c) 2011 David Hauweele 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions 9 | are met: 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 3. Neither the name of the University nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | SUCH DAMAGE. */ 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | static const char * get_selfname(const char *argv0) { 38 | const char *progname = (const char *)strrchr(argv0, '/'); 39 | return progname ? (progname + 1) : argv0; 40 | } 41 | 42 | int main(int argc, char **argv) 43 | { 44 | const char *tmp; 45 | int size, w, h; 46 | Imlib_Image src_img; 47 | Imlib_Image dst_img; 48 | 49 | if(argc != 4) 50 | errx(1, "invalid usage:\n" 51 | " %s ", get_selfname(argv[0])); 52 | 53 | size = atoi(argv[3]); 54 | src_img = imlib_load_image(argv[1]); 55 | 56 | if(!src_img) 57 | errx(2, "cannot load source image"); 58 | 59 | imlib_context_set_image(src_img); 60 | 61 | w = imlib_image_get_width(); 62 | h = imlib_image_get_height(); 63 | 64 | dst_img = imlib_create_cropped_scaled_image(0, 0, 65 | w, h, 66 | size * w / h, size); 67 | imlib_free_image(); 68 | 69 | imlib_context_set_image(dst_img); 70 | 71 | tmp = strrchr(argv[2], '.'); 72 | if(tmp) 73 | imlib_image_set_format(tmp + 1); 74 | 75 | imlib_save_image(argv[2]); 76 | 77 | imlib_free_image(); 78 | 79 | return 0; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /sample/makefile: -------------------------------------------------------------------------------- 1 | include commands.mk 2 | 3 | OPTS := -O2 4 | CFLAGS := -std=c99 $(OPTS) $(shell imlib2-config --cflags) -fPIC -Wall 5 | LDFLAGS := $(shell imlib2-config --libs) -lwebp 6 | 7 | SRC = $(wildcard *.c) 8 | OBJ = $(foreach obj, $(SRC:.c=.o), $(notdir $(obj))) 9 | DEP = $(SRC:.c=.d) 10 | 11 | PREFIX ?= /usr/local 12 | MIME ?= /share/mime 13 | THUMB ?= /share/thumbnailers/ 14 | GCONF ?= /share/gconf 15 | BIN ?= /bin 16 | 17 | ifndef DISABLE_DEBUG 18 | CFLAGS += -ggdb 19 | endif 20 | 21 | .PHONY: all clean 22 | 23 | all: imlib2-thumbnailer 24 | 25 | imlib2-thumbnailer: imlib2-thumbnailer.o 26 | $(CC) -o $@ $^ $(LDFLAGS) 27 | 28 | %.o: %.c 29 | $(CC) -Wp,-MMD,$*.d -c $(CFLAGS) -o $@ $< 30 | 31 | clean: 32 | $(RM) $(DEP) 33 | $(RM) $(OBJ) 34 | $(RM) imlib2-thumbnailer 35 | 36 | install: all 37 | $(INSTALL_DIR) $(DESTDIR)/$(PREFIX)/$(BIN) 38 | $(INSTALL_DIR) $(DESTDIR)/$(PREFIX)/$(THUMB) 39 | $(INSTALL_PROGRAM) imlib2-thumbnailer $(DESTDIR)/$(PREFIX)/$(BIN) 40 | $(INSTALL_DATA) webp.xml $(DESTDIR)/$(PREFIX)/$(MIME)/packages 41 | $(INSTALL_DATA) webp-thumbnailer.desktop $(DESTDIR)/$(PREFIX)/$(THUMB) 42 | $(INSTALL_DATA) webp-thumbnailer.schemas $(DESTDIR)/usr/$(GCONF)/schemas #ugly 43 | gconf-schemas --register webp-thumbnailer.schemas 44 | update-mime-database $(DESTDIR)/$(PREFIX)/$(MIME) 45 | 46 | 47 | uninstall: 48 | $(RM) $(DESTDIR)/$(PREFIX)/$(BIN)/imlib2-thumbnailer 49 | 50 | -include $(DEP) 51 | -------------------------------------------------------------------------------- /sample/webp-thumbnailer.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Version=1.0 3 | Encoding=UTF-8 4 | Type=X-Thumbnailer 5 | Name=WebP thumbnailer 6 | MimeType=image/webp; 7 | X-Thumbnailer-Exec=/usr/local/bin/imlib2-thumbnailer %i %o %s 8 | -------------------------------------------------------------------------------- /sample/webp-thumbnailer.schemas: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | /schemas/desktop/gnome/thumbnailers/image@webp/enable 5 | /desktop/gnome/thumbnailers/image@webp/enable 6 | webp-thumbnailer 7 | bool 8 | true 9 | 10 | 11 | 12 | 13 | 14 | 15 | /schemas/desktop/gnome/thumbnailers/image@webp/command 16 | /desktop/gnome/thumbnailers/image@webp/command 17 | webp-thumbnailer 18 | string 19 | imlib2-thumbnailer %i %o %s 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/webp.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WebP image 5 | 6 | 7 | 8 | --------------------------------------------------------------------------------