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.
 
 
 
 
 
 

125 rader
2.9 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. * screen.c: halftoning screen functions
  16. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_image_t *pipi_render_bayer(int w, int h)
  25. {
  26. pipi_image_t *ret;
  27. pipi_pixels_t *pix;
  28. float *data;
  29. int i, j, n;
  30. if(w <= 0 || h <= 0)
  31. return NULL;
  32. for(n = 1; n < w || n < h; n *= 2)
  33. ;
  34. ret = pipi_new(w, h);
  35. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  36. data = (float *)pix->pixels;
  37. for(j = 0; j < h; j++)
  38. for(i = 0; i < w; i++)
  39. {
  40. int k, l, x = 0;
  41. for(k = 1, l = n * n / 4; k < n; k *= 2, l /= 4)
  42. {
  43. if((i & k) && (j & k))
  44. x += l;
  45. else if(i & k)
  46. x += 3 * l;
  47. else if(j & k)
  48. x += 2 * l;
  49. }
  50. data[j * w + i] = (double)(x + 1) / (n * n + 1);
  51. }
  52. return ret;
  53. }
  54. typedef struct
  55. {
  56. int x, y;
  57. double dist;
  58. }
  59. dot_t;
  60. static int cmpdot(const void *p1, const void *p2)
  61. {
  62. return ((dot_t const *)p1)->dist > ((dot_t const *)p2)->dist;
  63. }
  64. pipi_image_t *pipi_render_halftone(int w, int h)
  65. {
  66. pipi_image_t *ret;
  67. pipi_pixels_t *pix;
  68. float *data;
  69. dot_t *circle;
  70. int x, y, n;
  71. if(w <= 0 || h <= 0)
  72. return NULL;
  73. circle = malloc(w * h * sizeof(dot_t));
  74. for(y = 0; y < h; y++)
  75. for(x = 0; x < w; x++)
  76. {
  77. double dy = ((double)y + .07) / h - .5;
  78. double dx = (double)x / w - .5;
  79. /* Using dx²+dy² here creates another interesting halftone. */
  80. double r = - cos(M_PI * (dx - dy)) - cos(M_PI * (dx + dy));
  81. circle[y * w + x].x = x;
  82. circle[y * w + x].y = y;
  83. circle[y * w + x].dist = r;
  84. }
  85. qsort(circle, w * h, sizeof(dot_t), cmpdot);
  86. ret = pipi_new(w * 2, h * 2);
  87. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  88. data = (float *)pix->pixels;
  89. for(n = 0; n < w * h; n++)
  90. {
  91. x = circle[n].x;
  92. y = circle[n].y;
  93. data[y * (2 * w) + x] = (float)(2 * n + 1) / (w * h * 4 + 1);
  94. data[(y + h) * (2 * w) + x + w] = (float)(2 * n + 2) / (w * h * 4 + 1);
  95. data[(y + h) * (2 * w) + x] = 1. - (float)(2 * n + 1) / (w * h * 4 + 1);
  96. data[y * (2 * w) + x + w] = 1. - (float)(2 * n + 2) / (w * h * 4 + 1);
  97. }
  98. free(circle);
  99. return ret;
  100. }