您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

116 行
2.5 KiB

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