Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

72 lignes
1.9 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_dither_floydsteinberg(pipi_image_t *img, pipi_scan_t scan)
  22. {
  23. pipi_image_t *dst;
  24. pipi_pixels_t *dstp;
  25. float *dstdata;
  26. int x, y, w, h;
  27. w = img->w;
  28. h = img->h;
  29. dst = pipi_copy(img);
  30. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  31. dstdata = (float *)dstp->pixels;
  32. for(y = 0; y < h; y++)
  33. {
  34. int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
  35. for(x = 0; x < w; x++)
  36. {
  37. float p, q, e;
  38. int x2 = reverse ? w - 1 - x : x;
  39. int s = reverse ? -1 : 1;
  40. p = dstdata[y * w + x2];
  41. q = p < 0.5 ? 0. : 1.;
  42. dstdata[y * w + x2] = q;
  43. /* FIXME: according to our 2008 paper, [7 4 5 0] is a better
  44. * error diffusion kernel for serpentine scan than [7 3 5 1]. */
  45. e = (p - q) / 16;
  46. if(x < w - 1)
  47. dstdata[y * w + x2 + s] += e * 7;
  48. if(y < h - 1)
  49. {
  50. if(x > 0)
  51. dstdata[(y + 1) * w + x2 - s] += e * 3;
  52. dstdata[(y + 1) * w + x2] += e * 5;
  53. if(x < w - 1)
  54. dstdata[(y + 1) * w + x2 + s] += e;
  55. }
  56. }
  57. }
  58. return dst;
  59. }