From 36fb60cacfcdcf81b778c7426207ede2f5b4f808 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 21 Aug 2008 00:10:22 +0000 Subject: [PATCH] * transform.c: implement pipi_rotate90, pipi_rotate180 and pipi_rotate270. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2750 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/context.c | 27 ++++++++ pipi/filter/transform.c | 140 +++++++++++++++++++++++++++++++++++++++- pipi/pipi.h | 3 + src/pipi.c | 15 +++++ 4 files changed, 184 insertions(+), 1 deletion(-) diff --git a/pipi/context.c b/pipi/context.c index 6e5d2ed..541f963 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -342,6 +342,33 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) ctx->images[ctx->nimages - 1] = pipi_vflip(tmp); pipi_free(tmp); } + else if(!strcmp(cmd, "rotate90")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_rotate90(tmp); + pipi_free(tmp); + } + else if(!strcmp(cmd, "rotate180")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_rotate180(tmp); + pipi_free(tmp); + } + else if(!strcmp(cmd, "rotate270")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_rotate270(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 index 2052723..a7d0d70 100644 --- a/pipi/filter/transform.c +++ b/pipi/filter/transform.c @@ -78,7 +78,7 @@ 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; + int y, w, h, gray; w = src->w; h = src->h; @@ -113,3 +113,141 @@ pipi_image_t *pipi_vflip(pipi_image_t *src) return dst; } +pipi_image_t *pipi_rotate90(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(h, w); + 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[x * h + y] = srcdata[y * w + w - 1 - x]; + } + else + { + dstdata[4 * (x * h + y)] + = srcdata[4 * (y * w + w - 1 - x)]; + dstdata[4 * (x * h + y) + 1] + = srcdata[4 * (y * w + w - 1 - x) + 1]; + dstdata[4 * (x * h + y) + 2] + = srcdata[4 * (y * w + w - 1 - x) + 2]; + dstdata[4 * (x * h + y) + 3] + = srcdata[4 * (y * w + w - 1 - x) + 3]; + } + } + } + + return dst; +} + +pipi_image_t *pipi_rotate180(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[(h - 1 - y) * w + (w - 1 - x)]; + } + else + { + dstdata[4 * (y * w + x)] + = srcdata[4 * ((h - 1 - y) * w + (w - 1 - x))]; + dstdata[4 * (y * w + x) + 1] + = srcdata[4 * ((h - 1 - y) * w + (w - 1 - x)) + 1]; + dstdata[4 * (y * w + x) + 2] + = srcdata[4 * ((h - 1 - y) * w + (w - 1 - x)) + 2]; + dstdata[4 * (y * w + x) + 3] + = srcdata[4 * ((h - 1 - y) * w + (w - 1 - x)) + 3]; + } + } + } + + return dst; +} + +pipi_image_t *pipi_rotate270(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(h, w); + 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[x * h + h - 1 - y] = srcdata[y * w + x]; + } + else + { + dstdata[4 * (x * h + h - 1 - y)] + = srcdata[4 * (y * w + x)]; + dstdata[4 * (x * h + h - 1 - y) + 1] + = srcdata[4 * (y * w + x) + 1]; + dstdata[4 * (x * h + h - 1 - y) + 2] + = srcdata[4 * (y * w + x) + 2]; + dstdata[4 * (x * h + h - 1 - y) + 3] + = srcdata[4 * (y * w + x) + 3]; + } + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index bf8ba80..779a65b 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -129,6 +129,9 @@ 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_rotate90(pipi_image_t *); +extern pipi_image_t *pipi_rotate180(pipi_image_t *); +extern pipi_image_t *pipi_rotate270(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 9ca60c8..7387511 100644 --- a/src/pipi.c +++ b/src/pipi.c @@ -117,6 +117,21 @@ int main(int argc, char *argv[]) if(pipi_command(ctx, "vflip") != 0) return EXIT_FAILURE; } + else if(!strcmp(argv[0], "--rotate90")) + { + if(pipi_command(ctx, "rotate90") != 0) + return EXIT_FAILURE; + } + else if(!strcmp(argv[0], "--rotate180")) + { + if(pipi_command(ctx, "rotate180") != 0) + return EXIT_FAILURE; + } + else if(!strcmp(argv[0], "--rotate270")) + { + if(pipi_command(ctx, "rotate270") != 0) + return EXIT_FAILURE; + } else if(!strcmp(argv[0], "--invert")) { if(pipi_command(ctx, "invert") != 0)