diff --git a/genethumb/genethumb.c b/genethumb/genethumb.c index 61ab640..899adbe 100644 --- a/genethumb/genethumb.c +++ b/genethumb/genethumb.c @@ -1,5 +1,18 @@ +#include "config.h" +#include "common.h" + +#include + int main(void) { + pipi_image_t *i, *j; + + i = pipi_load("irc.png"); + j = pipi_resize(i, 100, 200); + pipi_save(j, "irc.bmp"); + pipi_free(i); + pipi_free(j); + return 0; } diff --git a/pipi/Makefile.am b/pipi/Makefile.am index b30cdf0..eba82dd 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -17,6 +17,7 @@ libpipi_la_SOURCES = \ pipi_internals.h \ io.c \ pixels.c \ + resize.c \ $(NULL) libpipi_la_CFLAGS = $(CFLAGS_EXTRA) libpipi_la_LDFLAGS = $(LDFLAGS_EXTRA) \ diff --git a/pipi/pipi.h b/pipi/pipi.h index 97e840b..9a564d9 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -37,6 +37,8 @@ extern int pipi_getpixel(pipi_image_t const *img, int x, int y, int *r, int *g, int *b); extern int pipi_setpixel(pipi_image_t *img, int x, int y, int r, int g, int b); +extern pipi_image_t *pipi_resize(pipi_image_t const *, int, int); + #ifdef __cplusplus } #endif diff --git a/pipi/resize.c b/pipi/resize.c new file mode 100644 index 0000000..dd45490 --- /dev/null +++ b/pipi/resize.c @@ -0,0 +1,64 @@ +/* + * libpipi Proper image processing implementation library + * Copyright (c) 2004-2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This library is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * resize.c: image resizing functions + */ + +#include "config.h" +#include "common.h" + +#include "pipi_internals.h" +#include "pipi.h" + +pipi_image_t *pipi_resize(pipi_image_t const *src, int w, int h) +{ + pipi_image_t *dst; + int x, y, x0, y0; + dst = pipi_new(w, h); + + int sw = src->width; + int dw = dst->width; + + for(y = 0, y0 = 0; y < dst->height; y++) + { + for(x = 0, x0 = 0; x < dst->width; x++) + { + int rem = 0, tot = 0; + int ar = 0, ag = 0, ab = 0; + int r = 0, g = 0, b = 0; + int n; + + while(tot < sw) + { + if(rem == 0) + { + pipi_getpixel(src, x0, y, &r, &g, &b); + x0++; + rem = dw; + } + + n = (tot + rem <= sw) ? rem : sw - tot; + ar += n * r; ag += n * g; ab += n * b; + tot += n; + rem -= n; + } + + pipi_setpixel(dst, x, y, ar / sw, ag / sw, ab / sw); + } + } + + return dst; +} +