From 2da45f954b96d9a715b8a2d9ec4697f2fc0b5db8 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 21 Aug 2008 00:10:16 +0000 Subject: [PATCH] * Implement pipi_vflip() and pipi_hflip(). git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2749 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 1 + pipi/context.c | 18 +++++++ pipi/filter/transform.c | 115 ++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 2 + src/pipi.c | 10 ++++ 5 files changed, 146 insertions(+) create mode 100644 pipi/filter/transform.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 458a731..9f668a9 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -55,6 +55,7 @@ filter_sources = \ filter/blur.c \ filter/convolution.c filter/convolution_template.h \ filter/color.c \ + filter/transform.c \ filter/median.c \ filter/dilate.c diff --git a/pipi/context.c b/pipi/context.c index 23cc941..6e5d2ed 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -324,6 +324,24 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) pipi_free(src); ctx->images[ctx->nimages - 1] = dst; } + else if(!strcmp(cmd, "hflip")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_hflip(tmp); + pipi_free(tmp); + } + else if(!strcmp(cmd, "vflip")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_vflip(tmp); + pipi_free(tmp); + } else if(!strcmp(cmd, "mean")) { pipi_image_t *dst; diff --git a/pipi/filter/transform.c b/pipi/filter/transform.c new file mode 100644 index 0000000..2052723 --- /dev/null +++ b/pipi/filter/transform.c @@ -0,0 +1,115 @@ +/* + * 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. + */ + +/* + * transform.c: basic transformation functions + */ + +#include "config.h" +#include "common.h" + +#include +#include +#include +#include + +#include "pipi.h" +#include "pipi_internals.h" + +pipi_image_t *pipi_hflip(pipi_image_t *src) +{ + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + float *srcdata, *dstdata; + int x, y, w, h, gray; + + w = src->w; + h = src->h; + + gray = (src->last_modified == PIPI_PIXELS_Y_F); + + srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) + : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstdata = (float *)dstp->pixels; + + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++) + { + if(gray) + { + dstdata[y * w + x] = srcdata[y * w + w - 1 - x]; + } + else + { + dstdata[4 * (y * w + x)] + = srcdata[4 * (y * w + w - 1 - x)]; + dstdata[4 * (y * w + x) + 1] + = srcdata[4 * (y * w + w - 1 - x) + 1]; + dstdata[4 * (y * w + x) + 2] + = srcdata[4 * (y * w + w - 1 - x) + 2]; + dstdata[4 * (y * w + x) + 3] + = srcdata[4 * (y * w + w - 1 - x) + 3]; + } + } + } + + return dst; +} + +pipi_image_t *pipi_vflip(pipi_image_t *src) +{ + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + float *srcdata, *dstdata; + int x, y, w, h, gray; + + w = src->w; + h = src->h; + + gray = (src->last_modified == PIPI_PIXELS_Y_F); + + srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) + : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstdata = (float *)dstp->pixels; + + for(y = 0; y < h; y++) + { + if(gray) + { + memcpy(dstdata + y * w, + srcdata + (h - 1 - y) * w, + w * sizeof(*dstdata)); + } + else + { + memcpy(dstdata + 4 * y * w, + srcdata + 4 * (h - 1 - y) * w, + 4 * w * sizeof(*dstdata)); + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index 865a644..bf8ba80 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -127,6 +127,8 @@ extern pipi_image_t *pipi_contrast(pipi_image_t *, double); extern pipi_image_t *pipi_autocontrast(pipi_image_t *); extern pipi_image_t *pipi_invert(pipi_image_t *); extern pipi_image_t *pipi_threshold(pipi_image_t *, double); +extern pipi_image_t *pipi_hflip(pipi_image_t *); +extern pipi_image_t *pipi_vflip(pipi_image_t *); extern pipi_image_t *pipi_median(pipi_image_t *, int); extern pipi_image_t *pipi_median_ext(pipi_image_t *, int, int); extern pipi_image_t *pipi_dilate(pipi_image_t *); diff --git a/src/pipi.c b/src/pipi.c index 96ad731..9ca60c8 100644 --- a/src/pipi.c +++ b/src/pipi.c @@ -107,6 +107,16 @@ int main(int argc, char *argv[]) if(pipi_command(ctx, "autocontrast") != 0) return EXIT_FAILURE; } + else if(!strcmp(argv[0], "--hflip")) + { + if(pipi_command(ctx, "hflip") != 0) + return EXIT_FAILURE; + } + else if(!strcmp(argv[0], "--vflip")) + { + if(pipi_command(ctx, "vflip") != 0) + return EXIT_FAILURE; + } else if(!strcmp(argv[0], "--invert")) { if(pipi_command(ctx, "invert") != 0)