You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

edd.c 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /* This program computes the Floyd-Steinberg error diffusion algorithm's
  15. * displacement on the given input image. Error diffusion displacement is
  16. * introduced in the paper "Reinstating Floyd-Steinberg: Improved Metrics
  17. * for Quality Assessment of Error Diffusion Algorithms.", ICISP 2008
  18. * Proceedings. Lecture Notes in Computer Science 5099 Springer 2008, ISBN
  19. * 978-3-540-69904-0.
  20. *
  21. * The resulting dx/dy values are usually around 0.16/0.26 for images that
  22. * are not entirely black and white. */
  23. #include "config.h"
  24. #include "common.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <pipi.h>
  29. #define Z 3
  30. int main(int argc, char *argv[])
  31. {
  32. double sigma = 1.2, precision = 0.001, step = 2.;
  33. double best = 1., fx = -1., fy = -1., bfx = 0., bfy = 0.;
  34. double e, e0, e1;
  35. pipi_image_t *img, *gauss, *dither, *tmp;
  36. int dx, dy;
  37. if(argc < 2)
  38. {
  39. fprintf(stderr, "%s: too few arguments\n", argv[0]);
  40. fprintf(stderr, "Usage: %s <image>\n", argv[0]);
  41. return EXIT_FAILURE;
  42. }
  43. /* Load image, convert it to grayscale, dither it with Floyd-Steinberg */
  44. img = pipi_load(argv[1]);
  45. pipi_getpixels(img, PIPI_PIXELS_Y_F);
  46. gauss = pipi_gaussian_blur(img, sigma);
  47. dither = pipi_dither_floydsteinberg(img, PIPI_SCAN_RASTER);
  48. pipi_free(img);
  49. /* Compute the standard error */
  50. tmp = pipi_gaussian_blur(dither, sigma);
  51. e0 = pipi_measure_msd(gauss, tmp);
  52. pipi_free(tmp);
  53. /* Compute the fast error */
  54. tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.16, 0.26);
  55. e1 = pipi_measure_msd(gauss, tmp);
  56. pipi_free(tmp);
  57. /* Compute displacement */
  58. while(step > precision)
  59. {
  60. for(dy = 0; dy <= Z; dy++)
  61. for(dx = 0; dx <= Z; dx++)
  62. {
  63. tmp = pipi_gaussian_blur_ext(dither, sigma, sigma,
  64. fx + step * dx / Z,
  65. fy + step * dy / Z);
  66. e = pipi_measure_msd(gauss, tmp);
  67. pipi_free(tmp);
  68. if(e < best)
  69. {
  70. best = e;
  71. bfx = fx + step * dx / Z;
  72. bfy = fy + step * dy / Z;
  73. }
  74. }
  75. fx = bfx - step / Z;
  76. fy = bfy - step / Z;
  77. step = step * 2 / Z;
  78. }
  79. printf("E: %g E_fast: %g E_min: %g dx: %g dy: %g\n", e0, e1, best, fx, fy);
  80. pipi_free(dither);
  81. pipi_free(gauss);
  82. return 0;
  83. }