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.
 
 
 
 
 
 

84 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. * halftone.c: screening halftone dithering functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <math.h>
  20. #include "pipi.h"
  21. #include "pipi_internals.h"
  22. pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle)
  23. {
  24. double sint, cost;
  25. pipi_image_t *dst;
  26. pipi_pixels_t *dstp;
  27. float *dstdata;
  28. int x, y, w, h;
  29. w = img->w;
  30. h = img->h;
  31. cost = cos(angle * (M_PI / 180));
  32. sint = sin(angle * (M_PI / 180));
  33. dst = pipi_copy(img);
  34. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  35. dstdata = (float *)dstp->pixels;
  36. for(y = 0; y < h; y++)
  37. {
  38. for(x = 0; x < w; x++)
  39. {
  40. double x2, y2, i, j, dist;
  41. float p, threshold = .5;
  42. int invert = 0;
  43. x2 = x + .5;
  44. y2 = y + .5;
  45. i = (cost * x2 - sint * y2) / r;
  46. j = (cost * y2 + sint * x2) / r;
  47. i = i - (int)i + (i < 0.);
  48. j = j - (int)j + (j < 0.);
  49. if(j > 0.5)
  50. {
  51. i = 1. - i;
  52. j = 1. - j;
  53. }
  54. if(i > 0.5)
  55. {
  56. invert = 1;
  57. i = 1. - i;
  58. }
  59. dist = (i - .25) * (i - .25) + (j - .25) * (j - .25);
  60. threshold = dist * 4;
  61. if(invert)
  62. threshold = 1. - threshold;
  63. p = dstdata[y * w + x];
  64. dstdata[y * w + x] = p > threshold ? 1. : 0.;
  65. }
  66. }
  67. return dst;
  68. }