Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ordered.c 2.9 KiB

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