You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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