From b72179aad953f77ba21a713efd7fd6198ade7690 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 16 Oct 2010 21:52:38 +0000 Subject: [PATCH] Move resize.c to an algorithm-specific file so that we can implement other resizing methods. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@4690 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 6 ++- pipi/crop.c | 68 +++++++++++++++++++++++++ pipi/libpipi.vcproj | 10 +++- pipi/{resize.c => resample/bresenham.c} | 50 +++--------------- 4 files changed, 88 insertions(+), 46 deletions(-) create mode 100644 pipi/crop.c rename pipi/{resize.c => resample/bresenham.c} (79%) diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 0e73101..4f8d985 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -25,11 +25,12 @@ libpipi_la_SOURCES = \ codec.c \ stock.c \ colorstring.c \ - resize.c \ + crop.c \ dither.c \ accessors.c \ sequence.c \ $(codec_sources) \ + $(resample_sources) \ $(paint_sources) \ $(render_sources) \ $(combine_sources) \ @@ -49,6 +50,9 @@ codec_libs = codec_sources = \ codec/oric.c +resample_sources = \ + resample/bresenham.c + paint_sources = \ paint/floodfill.c \ paint/line.c \ diff --git a/pipi/crop.c b/pipi/crop.c new file mode 100644 index 0000000..540d330 --- /dev/null +++ b/pipi/crop.c @@ -0,0 +1,68 @@ +/* + * libpipi Pathetic image processing interface library + * Copyright (c) 2004-2009 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. + */ + +/* + * crop.c: image cropping functions + */ + +#include "config.h" + +#include +#include + +#include "pipi.h" +#include "pipi_internals.h" + +pipi_image_t *pipi_crop(pipi_image_t *src, int w, int h, int dx, int dy) +{ + float *srcdata, *dstdata; + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + int y, off, len; + + srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); + dstdata = (float *)dstp->pixels; + + off = dx; + len = w; + + if(dx < 0) + { + len += dx; + dx = 0; + } + + if(dx + len > srcp->w) + len = srcp->w - dx; + + if(len > 0) + { + for(y = 0; y < h; y++) + { + if(y + dy < 0 || y + dy >= srcp->h) + continue; + + memcpy(dstdata + y * w * 4, + srcdata + ((y + dy) * srcp->w + dx) * 4, + len * 4 * sizeof(float)); + } + } + + return dst; +} + diff --git a/pipi/libpipi.vcproj b/pipi/libpipi.vcproj index 10db1a2..f2d85e3 100644 --- a/pipi/libpipi.vcproj +++ b/pipi/libpipi.vcproj @@ -239,7 +239,7 @@ > + + + + diff --git a/pipi/resize.c b/pipi/resample/bresenham.c similarity index 79% rename from pipi/resize.c rename to pipi/resample/bresenham.c index 28611d0..3180fad 100644 --- a/pipi/resize.c +++ b/pipi/resample/bresenham.c @@ -1,6 +1,6 @@ /* * libpipi Pathetic image processing interface library - * Copyright (c) 2004-2008 Sam Hocevar + * Copyright (c) 2004-2009 Sam Hocevar * All Rights Reserved * * $Id$ @@ -13,7 +13,7 @@ */ /* - * resize.c: image resizing functions + * bresenham.c: Bresenham image resizing functions */ #include "config.h" @@ -24,6 +24,10 @@ #include "pipi.h" #include "pipi_internals.h" +/* This is Bresenham resizing. I rediscovered it independently but it was + * actually first described in 1995 by Tim Kientzle in "Scaling Bitmaps + * with Bresenham". */ + /* FIXME: the algorithm does not handle alpha components properly. Resulting * alpha should be the mean alpha value of the neightbouring pixels, but * the colour components should be weighted with the alpha value. */ @@ -123,45 +127,3 @@ pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h) return dst; } -pipi_image_t *pipi_crop(pipi_image_t *src, int w, int h, int dx, int dy) -{ - float *srcdata, *dstdata; - pipi_image_t *dst; - pipi_pixels_t *srcp, *dstp; - int y, off, len; - - srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); - srcdata = (float *)srcp->pixels; - - dst = pipi_new(w, h); - dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); - dstdata = (float *)dstp->pixels; - - off = dx; - len = w; - - if(dx < 0) - { - len += dx; - dx = 0; - } - - if(dx + len > srcp->w) - len = srcp->w - dx; - - if(len > 0) - { - for(y = 0; y < h; y++) - { - if(y + dy < 0 || y + dy >= srcp->h) - continue; - - memcpy(dstdata + y * w * 4, - srcdata + ((y + dy) * srcp->w + dx) * 4, - len * 4 * sizeof(float)); - } - } - - return dst; -} -