From e38943a327beddb21db547d316cfe7fa9def48d9 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 23 Aug 2008 09:11:10 +0000 Subject: [PATCH] * rgb.c: implement pipi_rgb(), to combine three RGB channels. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2753 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 1 + pipi/combine/rgb.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++ pipi/context.c | 17 +++++++++++++ pipi/pipi.h | 1 + src/pipi.c | 5 ++++ 5 files changed, 86 insertions(+) create mode 100644 pipi/combine/rgb.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 946bcc3..7ceb1fd 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -45,6 +45,7 @@ paint_sources = \ paint/tile.c combine_sources = \ + combine/rgb.c \ combine/mean.c \ combine/minmax.c \ combine/subadd.c \ diff --git a/pipi/combine/rgb.c b/pipi/combine/rgb.c new file mode 100644 index 0000000..4f00725 --- /dev/null +++ b/pipi/combine/rgb.c @@ -0,0 +1,62 @@ +/* + * 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. + */ + +/* + * rgb.c: RGB combining function + */ + +#include "config.h" +#include "common.h" + +#include "pipi.h" +#include "pipi_internals.h" + +pipi_image_t *pipi_rgb(pipi_image_t *i1, pipi_image_t *i2, pipi_image_t *i3) +{ + pipi_image_t *dst; + pipi_pixels_t *i1p, *i2p, *i3p, *dstp; + float *i1data, *i2data, *i3data, *dstdata; + int x, y, w, h; + + if(i1->w != i2->w || i1->h != i2->h || i1->w != i3->w || i1->h != i3->h) + return NULL; + + w = i1->w; + h = i1->h; + + dst = pipi_new(w, h); + dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstdata = (float *)dstp->pixels; + + i1p = pipi_getpixels(i1, PIPI_PIXELS_Y_F); + i1data = (float *)i1p->pixels; + i2p = pipi_getpixels(i2, PIPI_PIXELS_Y_F); + i2data = (float *)i2p->pixels; + i3p = pipi_getpixels(i3, PIPI_PIXELS_Y_F); + i3data = (float *)i3p->pixels; + + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++) + { + dstdata[4 * (y * w + x)] = i1data[y * w + x]; + dstdata[4 * (y * w + x) + 1] = i2data[y * w + x]; + dstdata[4 * (y * w + x) + 2] = i3data[y * w + x]; + dstdata[4 * (y * w + x) + 3] = 1.0; + } + } + + return dst; +} + diff --git a/pipi/context.c b/pipi/context.c index da56693..e04c59a 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -373,6 +373,23 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) ctx->images[ctx->nimages - 1] = pipi_rotate270(tmp); pipi_free(tmp); } + else if(!strcmp(cmd, "rgb")) + { + pipi_image_t *dst; + + if(ctx->nimages < 3) + return -1; + dst = pipi_rgb(ctx->images[ctx->nimages - 3], + ctx->images[ctx->nimages - 2], + ctx->images[ctx->nimages - 1]); + if(dst == NULL) + return -1; + pipi_free(ctx->images[ctx->nimages - 3]); + pipi_free(ctx->images[ctx->nimages - 2]); + pipi_free(ctx->images[ctx->nimages - 1]); + ctx->images[ctx->nimages - 3] = dst; + ctx->nimages -= 2; + } else if(!strcmp(cmd, "mean")) { pipi_image_t *dst; diff --git a/pipi/pipi.h b/pipi/pipi.h index c9968b8..41fd07a 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -105,6 +105,7 @@ extern double pipi_measure_rmsd(pipi_image_t *, pipi_image_t *); extern pipi_image_t *pipi_resize(pipi_image_t *, int, int); +extern pipi_image_t *pipi_rgb(pipi_image_t *, pipi_image_t *, pipi_image_t *); extern pipi_image_t *pipi_mean(pipi_image_t *, pipi_image_t *); extern pipi_image_t *pipi_min(pipi_image_t *, pipi_image_t *); extern pipi_image_t *pipi_max(pipi_image_t *, pipi_image_t *); diff --git a/src/pipi.c b/src/pipi.c index e979b2c..986f1b8 100644 --- a/src/pipi.c +++ b/src/pipi.c @@ -168,6 +168,11 @@ int main(int argc, char *argv[]) if(pipi_command(ctx, "wrap") != 0) return EXIT_FAILURE; } + else if(!strcmp(argv[0], "--rgb")) + { + if(pipi_command(ctx, "rgb") != 0) + return EXIT_FAILURE; + } else if(!strcmp(argv[0], "--mean")) { if(pipi_command(ctx, "mean") != 0)