Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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