Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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