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.
 
 
 
 
 
 

102 rivejä
3.0 KiB

  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, *kernel, *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. kernel = pipi_load("ediff:fs");
  48. dither = pipi_dither_ediff(img, kernel, PIPI_SCAN_RASTER);
  49. pipi_free(kernel);
  50. pipi_free(img);
  51. /* Compute the standard error */
  52. tmp = pipi_gaussian_blur(dither, sigma);
  53. e0 = pipi_measure_msd(gauss, tmp);
  54. pipi_free(tmp);
  55. /* Compute the fast error */
  56. tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0, 0.16, 0.26);
  57. e1 = pipi_measure_msd(gauss, tmp);
  58. pipi_free(tmp);
  59. /* Compute displacement */
  60. while(step > precision)
  61. {
  62. for(dy = 0; dy <= Z; dy++)
  63. for(dx = 0; dx <= Z; dx++)
  64. {
  65. tmp = pipi_gaussian_blur_ext(dither, sigma, sigma, 0.0,
  66. fx + step * dx / Z,
  67. fy + step * dy / Z);
  68. e = pipi_measure_msd(gauss, tmp);
  69. pipi_free(tmp);
  70. if(e < best)
  71. {
  72. best = e;
  73. bfx = fx + step * dx / Z;
  74. bfy = fy + step * dy / Z;
  75. }
  76. }
  77. fx = bfx - step / Z;
  78. fy = bfy - step / Z;
  79. step = step * 2 / Z;
  80. }
  81. printf("E: %g E_fast: %g E_min: %g dx: %g dy: %g\n", e0, e1, best, fx, fy);
  82. pipi_free(dither);
  83. pipi_free(gauss);
  84. return 0;
  85. }