1 | /* |
---|
2 | * edd error diffusion displacement |
---|
3 | * Copyright (c) 2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* This program computes the Floyd-Steinberg error diffusion algorithm's |
---|
16 | * displacement on the given input image. Error diffusion displacement is |
---|
17 | * introduced in the paper "Reinstating Floyd-Steinberg: Improved Metrics |
---|
18 | * for Quality Assessment of Error Diffusion Algorithms.", ICISP 2008 |
---|
19 | * Proceedings. Lecture Notes in Computer Science 5099 Springer 2008, ISBN |
---|
20 | * 978-3-540-69904-0. |
---|
21 | * |
---|
22 | * The resulting dx/dy values are usually around 0.16/0.26 for images that |
---|
23 | * are not entirely black and white. */ |
---|
24 | |
---|
25 | #include "config.h" |
---|
26 | #include "common.h" |
---|
27 | |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include <pipi.h> |
---|
33 | |
---|
34 | #define Z 3 |
---|
35 | |
---|
36 | int main(int argc, char *argv[]) |
---|
37 | { |
---|
38 | double sigma = 1.2, precision = 0.001, step = 2.; |
---|
39 | double best = 1., fx = -1., fy = -1., bfx = 0., bfy = 0.; |
---|
40 | double e, e0, e1; |
---|
41 | pipi_image_t *img, *kernel, *gauss, *dither, *tmp; |
---|
42 | int dx, dy; |
---|
43 | |
---|
44 | if(argc < 2) |
---|
45 | { |
---|
46 | fprintf(stderr, "%s: too few arguments\n", argv[0]); |
---|
47 | fprintf(stderr, "Usage: %s <image>\n", argv[0]); |
---|
48 | return EXIT_FAILURE; |
---|
49 | } |
---|
50 | |
---|
51 | /* Load image, convert it to grayscale, dither it with Floyd-Steinberg */ |
---|
52 | img = pipi_load(argv[1]); |
---|
53 | pipi_getpixels(img, PIPI_PIXELS_Y_F); |
---|
54 | gauss = pipi_gaussian_blur(img, sigma); |
---|
55 | kernel = pipi_load("ediff:fs"); |
---|
56 | dither = pipi_dither_ediff(img, kernel, PIPI_SCAN_RASTER); |
---|
57 | pipi_free(kernel); |
---|
58 | pipi_free(img); |
---|
59 | |
---|
60 | /* Compute the standard error */ |
---|
61 | tmp = pipi_gaussian_blur(dither, sigma); |
---|
62 | e0 = pipi_measure_msd(gauss, tmp); |
---|
63 | pipi_free(tmp); |
---|
64 | |
---|
65 | /* Compute the fast error */ |
---|
66 | tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0, 0.16, 0.26); |
---|
67 | e1 = pipi_measure_msd(gauss, tmp); |
---|
68 | pipi_free(tmp); |
---|
69 | |
---|
70 | /* Compute displacement */ |
---|
71 | while(step > precision) |
---|
72 | { |
---|
73 | for(dy = 0; dy <= Z; dy++) |
---|
74 | for(dx = 0; dx <= Z; dx++) |
---|
75 | { |
---|
76 | tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0, |
---|
77 | fx + step * dx / Z, |
---|
78 | fy + step * dy / Z); |
---|
79 | e = pipi_measure_msd(gauss, tmp); |
---|
80 | pipi_free(tmp); |
---|
81 | if(e < best) |
---|
82 | { |
---|
83 | best = e; |
---|
84 | bfx = fx + step * dx / Z; |
---|
85 | bfy = fy + step * dy / Z; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | fx = bfx - step / Z; |
---|
90 | fy = bfy - step / Z; |
---|
91 | step = step * 2 / Z; |
---|
92 | } |
---|
93 | |
---|
94 | printf("E: %g E_fast: %g E_min: %g dx: %g dy: %g\n", e0, e1, best, fx, fy); |
---|
95 | |
---|
96 | pipi_free(dither); |
---|
97 | pipi_free(gauss); |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|