Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

96 rindas
2.8 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;
  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_RGBA_F);
  46. pipi_getpixels(img, PIPI_PIXELS_Y_F);
  47. gauss = pipi_gaussian_blur(img, sigma);
  48. dither = pipi_floydsteinberg(img);
  49. pipi_free(img);
  50. /* Compute the standard error */
  51. tmp = pipi_gaussian_blur(dither, sigma);
  52. e0 = pipi_measure_rmsd(gauss, tmp);
  53. pipi_free(tmp);
  54. /* Compute displacement */
  55. while(step > precision)
  56. {
  57. for(dy = 0; dy <= Z; dy++)
  58. for(dx = 0; dx <= Z; dx++)
  59. {
  60. tmp = pipi_gaussian_blur_ext(dither, sigma, sigma,
  61. fx + step * dx / Z,
  62. fy + step * dy / Z);
  63. e = pipi_measure_rmsd(gauss, tmp);
  64. pipi_free(tmp);
  65. if(e < best)
  66. {
  67. best = e;
  68. bfx = fx + step * dx / Z;
  69. bfy = fy + step * dy / Z;
  70. }
  71. }
  72. fx = bfx - step / Z;
  73. fy = bfy - step / Z;
  74. step = step * 2 / Z;
  75. }
  76. printf("E: %g E_min: %g dx: %g dy: %g\n", e0, best, fx, fy);
  77. pipi_free(dither);
  78. pipi_free(gauss);
  79. return 0;
  80. }