* Reimplement pipi_dither_halftone() using pipi_dither_ordered_ext(). git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2812 92316355-f0b4-4df1-b90c-862c8a59935fremotes/tiles
| @@ -76,7 +76,6 @@ quantize_sources = \ | |||||
| dither_sources = \ | dither_sources = \ | ||||
| dither/ediff.c \ | dither/ediff.c \ | ||||
| dither/ordered.c \ | dither/ordered.c \ | ||||
| dither/halftone.c \ | |||||
| dither/ostromoukhov.c \ | dither/ostromoukhov.c \ | ||||
| dither/dbs.c \ | dither/dbs.c \ | ||||
| dither/random.c | dither/random.c | ||||
| @@ -106,14 +106,21 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) | |||||
| } | } | ||||
| else if(!strncmp(method, "ordered", 7)) | else if(!strncmp(method, "ordered", 7)) | ||||
| { | { | ||||
| double angle = .0; | |||||
| method = strchr(method, ':'); | |||||
| if(method) | |||||
| angle = atof(method + 1); | |||||
| double scale = 1., angle = .0; | |||||
| if(ctx->nimages < 2) | if(ctx->nimages < 2) | ||||
| return -1; | return -1; | ||||
| method = strchr(method, ':'); | |||||
| if(method) | |||||
| { | |||||
| scale = atof(method + 1); | |||||
| method = strchr(method + 1, ':'); | |||||
| if(method) | |||||
| angle = atof(method + 1); | |||||
| } | |||||
| if(scale <= 0.) | |||||
| scale = 1.; | |||||
| dst = pipi_dither_ordered_ext(ctx->images[ctx->nimages - 2], src, | dst = pipi_dither_ordered_ext(ctx->images[ctx->nimages - 2], src, | ||||
| angle); | |||||
| scale, angle); | |||||
| pipi_free(ctx->images[ctx->nimages - 2]); | pipi_free(ctx->images[ctx->nimages - 2]); | ||||
| ctx->nimages--; | ctx->nimages--; | ||||
| } | } | ||||
| @@ -1,83 +0,0 @@ | |||||
| /* | |||||
| * libpipi Proper image processing implementation library | |||||
| * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> | |||||
| * 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. | |||||
| */ | |||||
| /* | |||||
| * halftone.c: screening halftone dithering functions | |||||
| */ | |||||
| #include "config.h" | |||||
| #include "common.h" | |||||
| #include <math.h> | |||||
| #include "pipi.h" | |||||
| #include "pipi_internals.h" | |||||
| pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle) | |||||
| { | |||||
| double sint, cost; | |||||
| pipi_image_t *dst; | |||||
| pipi_pixels_t *dstp; | |||||
| float *dstdata; | |||||
| int x, y, w, h; | |||||
| w = img->w; | |||||
| h = img->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; | |||||
| for(y = 0; y < h; y++) | |||||
| { | |||||
| for(x = 0; x < w; x++) | |||||
| { | |||||
| double x2, y2, i, j, dist; | |||||
| float p, threshold = .5; | |||||
| int invert = 0; | |||||
| x2 = x + .5; | |||||
| y2 = y + .5; | |||||
| i = (cost * x2 - sint * y2) / r; | |||||
| j = (cost * y2 + sint * x2) / r; | |||||
| i = i - (int)i + (i < 0.); | |||||
| j = j - (int)j + (j < 0.); | |||||
| if(j > 0.5) | |||||
| { | |||||
| i = 1. - i; | |||||
| j = 1. - j; | |||||
| } | |||||
| if(i > 0.5) | |||||
| { | |||||
| invert = 1; | |||||
| i = 1. - i; | |||||
| } | |||||
| dist = (i - .25) * (i - .25) + (j - .25) * (j - .25); | |||||
| threshold = dist * 4; | |||||
| if(invert) | |||||
| threshold = 1. - threshold; | |||||
| p = dstdata[y * w + x]; | |||||
| dstdata[y * w + x] = p > threshold ? 1. : 0.; | |||||
| } | |||||
| } | |||||
| return dst; | |||||
| } | |||||
| @@ -25,13 +25,26 @@ | |||||
| #include "pipi.h" | #include "pipi.h" | ||||
| #include "pipi_internals.h" | #include "pipi_internals.h" | ||||
| pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle) | |||||
| { | |||||
| #define PRECISION 4. | |||||
| pipi_image_t *ret, *kernel; | |||||
| int k = (r * PRECISION / sqrt(2.) + .5); | |||||
| kernel = pipi_render_halftone(k, k); | |||||
| ret = pipi_dither_ordered_ext(img, kernel, 1. / PRECISION, angle + 45.); | |||||
| pipi_free(kernel); | |||||
| return ret; | |||||
| } | |||||
| pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel) | pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel) | ||||
| { | { | ||||
| return pipi_dither_ordered_ext(img, kernel, 0.0); | |||||
| return pipi_dither_ordered_ext(img, kernel, 1.0, 0.0); | |||||
| } | } | ||||
| pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel, | pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel, | ||||
| double angle) | |||||
| double scale, double angle) | |||||
| { | { | ||||
| double sint, cost; | double sint, cost; | ||||
| pipi_image_t *dst; | pipi_image_t *dst; | ||||
| @@ -60,8 +73,8 @@ pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel, | |||||
| { | { | ||||
| float p, q; | float p, q; | ||||
| kx = (int)(cost * x - sint * y + 2 * w * h) % kw; | |||||
| ky = (int)(cost * y + sint * x + 2 * w * h) % kh; | |||||
| kx = (int)((cost * x - sint * y + 2 * w * h) / scale) % kw; | |||||
| ky = (int)((cost * y + sint * x + 2 * w * h) / scale) % kh; | |||||
| p = dstdata[y * w + x]; | p = dstdata[y * w + x]; | ||||
| q = p > kerneldata[ky * kw + kx] ? 1. : 0.; | q = p > kerneldata[ky * kw + kx] ? 1. : 0.; | ||||
| @@ -183,7 +183,7 @@ extern pipi_image_t *pipi_dither_ediff(pipi_image_t *, pipi_image_t *, | |||||
| pipi_scan_t); | pipi_scan_t); | ||||
| extern pipi_image_t *pipi_dither_ordered(pipi_image_t *, pipi_image_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 *, | extern pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *, pipi_image_t *, | ||||
| double); | |||||
| double, double); | |||||
| extern pipi_image_t *pipi_dither_halftone(pipi_image_t *, double, 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_random(pipi_image_t *); | ||||
| extern pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *, pipi_scan_t); | extern pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *, pipi_scan_t); | ||||