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.

random.c 1.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. * random.c: random 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_random(pipi_image_t *img)
  22. {
  23. pipi_image_t *dst;
  24. pipi_pixels_t *dstp;
  25. float *dstdata;
  26. unsigned int ctx = 1;
  27. int x, y, w, h;
  28. w = img->w;
  29. h = img->h;
  30. dst = pipi_copy(img);
  31. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  32. dstdata = (float *)dstp->pixels;
  33. for(y = 0; y < h; y++)
  34. {
  35. for(x = 0; x < w; x++)
  36. {
  37. long hi, lo;
  38. float p, q;
  39. hi = ctx / 12773L;
  40. lo = ctx % 12773L;
  41. ctx = 16807L * lo - 2836L * hi;
  42. if(ctx <= 0)
  43. ctx += 0x7fffffffL;
  44. p = dstdata[y * w + x];
  45. q = p > (double)((ctx % 65536) / 65535.) ? 1. : 0.;
  46. dstdata[y * w + x] = q;
  47. }
  48. }
  49. return dst;
  50. }