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.
 
 
 
 
 
 

142 line
3.3 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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. * ordered.c: Bayer ordered dithering functions
  16. */
  17. #include "config.h"
  18. #include <stdlib.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. #define PRECISION 4.
  25. pipi_image_t *ret, *kernel;
  26. int k = (r * PRECISION / 2. / sqrt(2.) + .5);
  27. kernel = pipi_render_halftone(k, k);
  28. ret = pipi_dither_ordered_ext(img, kernel, 1. / PRECISION, angle + 45.);
  29. pipi_free(kernel);
  30. return ret;
  31. }
  32. pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
  33. {
  34. return pipi_dither_ordered_ext(img, kernel, 1.0, 0.0);
  35. }
  36. pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel,
  37. double scale, double angle)
  38. {
  39. double sint, cost;
  40. pipi_image_t *dst;
  41. pipi_pixels_t *dstp, *kernelp;
  42. float *dstdata, *kerneldata;
  43. int x, y, w, h, kx, ky, kw, kh;
  44. w = img->w;
  45. h = img->h;
  46. kw = kernel->w;
  47. kh = kernel->h;
  48. cost = cos(angle * (M_PI / 180));
  49. sint = sin(angle * (M_PI / 180));
  50. dst = pipi_copy(img);
  51. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  52. dstdata = (float *)dstp->pixels;
  53. kernelp = pipi_getpixels(kernel, PIPI_PIXELS_Y_F);
  54. kerneldata = (float *)kernelp->pixels;
  55. for(y = 0; y < h; y++)
  56. {
  57. for(x = 0; x < w; x++)
  58. {
  59. float p, q;
  60. kx = (int)((cost * x - sint * y + 2 * w * h) / scale) % kw;
  61. ky = (int)((cost * y + sint * x + 2 * w * h) / scale) % kh;
  62. p = dstdata[y * w + x];
  63. q = p > kerneldata[ky * kw + kx] ? 1. : 0.;
  64. dstdata[y * w + x] = q;
  65. }
  66. }
  67. return dst;
  68. }
  69. typedef struct
  70. {
  71. int x, y;
  72. double val;
  73. }
  74. dot_t;
  75. static int cmpdot(const void *p1, const void *p2)
  76. {
  77. return ((dot_t const *)p1)->val > ((dot_t const *)p2)->val;
  78. }
  79. pipi_image_t *pipi_order(pipi_image_t *src)
  80. {
  81. double epsilon;
  82. pipi_image_t *dst;
  83. pipi_pixels_t *dstp, *srcp;
  84. float *dstdata, *srcdata;
  85. dot_t *circle;
  86. int x, y, w, h, n;
  87. w = src->w;
  88. h = src->h;
  89. epsilon = 1. / (w * h + 1);
  90. srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
  91. srcdata = (float *)srcp->pixels;
  92. dst = pipi_new(w, h);
  93. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  94. dstdata = (float *)dstp->pixels;
  95. circle = malloc(w * h * sizeof(dot_t));
  96. for(y = 0; y < h; y++)
  97. for(x = 0; x < w; x++)
  98. {
  99. circle[y * w + x].x = x;
  100. circle[y * w + x].y = y;
  101. circle[y * w + x].val = srcdata[y * w + x];
  102. }
  103. qsort(circle, w * h, sizeof(dot_t), cmpdot);
  104. for(n = 0; n < w * h; n++)
  105. {
  106. x = circle[n].x;
  107. y = circle[n].y;
  108. dstdata[y * w + x] = (float)(n + 1) * epsilon;
  109. }
  110. free(circle);
  111. return dst;
  112. }