From 4fefa85cd9c6e35247081a3c0c3ffccebaf2b54e Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 31 Aug 2008 23:05:50 +0000 Subject: [PATCH] * ordered.c: allow to rotate the dither pattern, using nearest-neighbour rotation interpolation. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2811 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/context.c | 9 +++++++-- pipi/dither/ordered.c | 18 ++++++++++++++++-- pipi/pipi.h | 2 ++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pipi/context.c b/pipi/context.c index 6b38f5d..ab8a34c 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -104,11 +104,16 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) pipi_free(ctx->images[ctx->nimages - 2]); ctx->nimages--; } - else if(!strcmp(method, "ordered")) + else if(!strncmp(method, "ordered", 7)) { + double angle = .0; + method = strchr(method, ':'); + if(method) + angle = atof(method + 1); if(ctx->nimages < 2) return -1; - dst = pipi_dither_ordered(ctx->images[ctx->nimages - 2], src); + dst = pipi_dither_ordered_ext(ctx->images[ctx->nimages - 2], src, + angle); pipi_free(ctx->images[ctx->nimages - 2]); ctx->nimages--; } diff --git a/pipi/dither/ordered.c b/pipi/dither/ordered.c index 0fc7c14..34a6d2d 100644 --- a/pipi/dither/ordered.c +++ b/pipi/dither/ordered.c @@ -20,22 +20,33 @@ #include "common.h" #include +#include #include "pipi.h" #include "pipi_internals.h" pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel) { + return pipi_dither_ordered_ext(img, kernel, 0.0); +} + +pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel, + double angle) +{ + double sint, cost; pipi_image_t *dst; pipi_pixels_t *dstp, *kernelp; float *dstdata, *kerneldata; - int x, y, w, h, kw, kh; + int x, y, w, h, kx, ky, kw, kh; w = img->w; h = img->h; kw = kernel->w; kh = kernel->h; + cost = cos(angle * (M_PI / 180)); + sint = sin(angle * (M_PI / 180)); + dst = pipi_copy(img); dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F); dstdata = (float *)dstp->pixels; @@ -49,8 +60,11 @@ pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel) { float p, q; + kx = (int)(cost * x - sint * y + 2 * w * h) % kw; + ky = (int)(cost * y + sint * x + 2 * w * h) % kh; + p = dstdata[y * w + x]; - q = p > kerneldata[(y % kh) * kw + (x % kw)] ? 1. : 0.; + q = p > kerneldata[ky * kw + kx] ? 1. : 0.; dstdata[y * w + x] = q; } } diff --git a/pipi/pipi.h b/pipi/pipi.h index c781c36..6114a2e 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -182,6 +182,8 @@ extern pipi_image_t *pipi_reduce(pipi_image_t *, int, double const *); extern pipi_image_t *pipi_dither_ediff(pipi_image_t *, pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_ordered(pipi_image_t *, pipi_image_t *); +extern pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *, pipi_image_t *, + double); extern pipi_image_t *pipi_dither_halftone(pipi_image_t *, double, double); extern pipi_image_t *pipi_dither_random(pipi_image_t *); extern pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *, pipi_scan_t);