Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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