| 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, *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 | dither = pipi_dither_floydsteinberg(img, PIPI_SCAN_RASTER); |
|---|
| 56 | pipi_free(img); |
|---|
| 57 | |
|---|
| 58 | /* Compute the standard error */ |
|---|
| 59 | tmp = pipi_gaussian_blur(dither, sigma); |
|---|
| 60 | e0 = pipi_measure_msd(gauss, tmp); |
|---|
| 61 | pipi_free(tmp); |
|---|
| 62 | |
|---|
| 63 | /* Compute the fast error */ |
|---|
| 64 | tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0, 0.16, 0.26); |
|---|
| 65 | e1 = pipi_measure_msd(gauss, tmp); |
|---|
| 66 | pipi_free(tmp); |
|---|
| 67 | |
|---|
| 68 | /* Compute displacement */ |
|---|
| 69 | while(step > precision) |
|---|
| 70 | { |
|---|
| 71 | for(dy = 0; dy <= Z; dy++) |
|---|
| 72 | for(dx = 0; dx <= Z; dx++) |
|---|
| 73 | { |
|---|
| 74 | tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0, |
|---|
| 75 | fx + step * dx / Z, |
|---|
| 76 | fy + step * dy / Z); |
|---|
| 77 | e = pipi_measure_msd(gauss, tmp); |
|---|
| 78 | pipi_free(tmp); |
|---|
| 79 | if(e < best) |
|---|
| 80 | { |
|---|
| 81 | best = e; |
|---|
| 82 | bfx = fx + step * dx / Z; |
|---|
| 83 | bfy = fy + step * dy / Z; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | fx = bfx - step / Z; |
|---|
| 88 | fy = bfy - step / Z; |
|---|
| 89 | step = step * 2 / Z; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | printf("E: %g E_fast: %g E_min: %g dx: %g dy: %g\n", e0, e1, best, fx, fy); |
|---|
| 93 | |
|---|
| 94 | pipi_free(dither); |
|---|
| 95 | pipi_free(gauss); |
|---|
| 96 | |
|---|
| 97 | return 0; |
|---|
| 98 | } |
|---|
| 99 | |
|---|