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.
 
 
 
 
 
 

87 lines
2.4 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library 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. * ed.c: generic error diffusion functions
  16. */
  17. #include "config.h"
  18. #include "pipi.h"
  19. #include "pipi_internals.h"
  20. /* Perform a generic error diffusion dithering. The first non-zero
  21. * element in ker is treated as the current pixel. All other non-zero
  22. * elements are the error diffusion coefficients.
  23. * Making the matrix generic is not terribly slower: the performance
  24. * hit is around 4% for Floyd-Steinberg and 13% for JaJuNi, with the
  25. * benefit of a lot less code. */
  26. pipi_image_t *pipi_dither_ediff(pipi_image_t *img, pipi_image_t *ker,
  27. pipi_scan_t scan)
  28. {
  29. pipi_image_t *dst;
  30. pipi_pixels_t *dstp, *kerp;
  31. float *dstdata, *kerdata;
  32. int x, y, w, h, i, j, kx, kw, kh;
  33. w = img->w;
  34. h = img->h;
  35. kw = ker->w;
  36. kh = ker->h;
  37. dst = pipi_copy(img);
  38. dstp = pipi_get_pixels(dst, PIPI_PIXELS_Y_F32);
  39. dstdata = (float *)dstp->pixels;
  40. kerp = pipi_get_pixels(ker, PIPI_PIXELS_Y_F32);
  41. kerdata = (float *)kerp->pixels;
  42. for(kx = 0; kx < kw; kx++)
  43. if(kerdata[kx] > 0)
  44. break;
  45. for(y = 0; y < h; y++)
  46. {
  47. int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
  48. for(x = 0; x < w; x++)
  49. {
  50. float p, q, e;
  51. int x2 = reverse ? w - 1 - x : x;
  52. int s = reverse ? -1 : 1;
  53. p = dstdata[y * w + x2];
  54. q = p < 0.5 ? 0. : 1.;
  55. dstdata[y * w + x2] = q;
  56. e = (p - q);
  57. for(j = 0; j < kh && y < h - j; j++)
  58. for(i = 0; i < kw; i++)
  59. {
  60. if(j == 0 && i <= kx)
  61. continue;
  62. if(x + i - kx < 0 || x + i - kx >= w)
  63. continue;
  64. dstdata[(y + j) * w + x2 + (i - kx) * s]
  65. += e * kerdata[j * kw + i];
  66. }
  67. }
  68. }
  69. return dst;
  70. }