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.
 
 
 
 
 
 

75 lines
2.0 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. * floydsteinberg.c: Floyd-Steinberg dithering functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include "pipi.h"
  20. #include "pipi_internals.h"
  21. pipi_image_t *pipi_floydsteinberg(pipi_image_t *src, pipi_scan_t scan)
  22. {
  23. pipi_image_t *dst;
  24. pipi_pixels_t *srcp, *dstp;
  25. float *srcdata, *dstdata;
  26. int x, y, w, h;
  27. w = src->w;
  28. h = src->h;
  29. srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
  30. srcdata = (float *)srcp->pixels;
  31. dst = pipi_new(w, h);
  32. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  33. dstdata = (float *)dstp->pixels;
  34. for(y = 0; y < h; y++)
  35. {
  36. int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
  37. for(x = 0; x < w; x++)
  38. {
  39. float p, q, e;
  40. int x2 = reverse ? w - 1 - x : x;
  41. int s = reverse ? -1 : 1;
  42. p = srcdata[y * w + x2];
  43. q = p < 0.5 ? 0. : 1.;
  44. dstdata[y * w + x2] = q;
  45. /* FIXME: according to our 2008 paper, [7 4 5 0] is a better
  46. * error diffusion kernel for serpentine scan. */
  47. e = p - q;
  48. if(x < w - 1)
  49. srcdata[y * w + x2 + s] += e * .4375;
  50. if(y < h - 1)
  51. {
  52. if(x > 0)
  53. srcdata[(y + 1) * w + x2 - s] += e * .1875;
  54. srcdata[(y + 1) * w + x2] += e * .3125;
  55. if(x < w - 1)
  56. srcdata[(y + 1) * w + x2 + s] += e * .0625;
  57. }
  58. }
  59. }
  60. return dst;
  61. }