25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
1.7 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)
  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. for(x = 0; x < w; x++)
  37. {
  38. float p, q, e;
  39. p = srcdata[y * w + x];
  40. q = p < 0.5 ? 0. : 1.;
  41. dstdata[y * w + x] = q;
  42. e = p - q;
  43. if(x < w - 1)
  44. srcdata[y * w + x + 1] += e * .4375;
  45. if(y < h - 1)
  46. {
  47. if(x > 0)
  48. srcdata[(y + 1) * w + x - 1] += e * .1875;
  49. srcdata[(y + 1) * w + x] += e * .3125;
  50. if(x < w - 1)
  51. srcdata[(y + 1) * w + x + 1] += e * .0625;
  52. }
  53. }
  54. }
  55. return dst;
  56. }