Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

145 righe
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. #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_get_pixels(dst, PIPI_PIXELS_Y_F32);
  55. dstdata = (float *)dstp->pixels;
  56. kernelp = pipi_get_pixels(kernel, PIPI_PIXELS_Y_F32);
  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_get_pixels(src, PIPI_PIXELS_Y_F32);
  94. srcdata = (float *)srcp->pixels;
  95. dst = pipi_new(w, h);
  96. dstp = pipi_get_pixels(dst, PIPI_PIXELS_Y_F32);
  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. }