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.
 
 
 
 
 
 

88 rivejä
2.4 KiB

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