Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

238 wiersze
6.3 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. * stock.c: stock images
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_image_t *pipi_load_stock(char const *name)
  25. {
  26. pipi_image_t *ret;
  27. pipi_pixels_t *pix;
  28. float *data;
  29. /* Generate a Bayer dithering pattern. */
  30. if(!strncmp(name, "bayer:", 6))
  31. {
  32. int i, j, w, h, n;
  33. w = atoi(name + 6);
  34. name = strchr(name + 6, 'x');
  35. if(!name)
  36. return NULL;
  37. h = atoi(name + 1);
  38. if(w <= 0 || h <= 0)
  39. return NULL;
  40. for(n = 1; n < w || n < h; n *= 2)
  41. ;
  42. ret = pipi_new(w, h);
  43. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  44. data = (float *)pix->pixels;
  45. for(j = 0; j < h; j++)
  46. for(i = 0; i < w; i++)
  47. {
  48. int k, l, x = 0;
  49. for(k = 1, l = n * n / 4; k < n; k *= 2, l /= 4)
  50. {
  51. if((i & k) && (j & k))
  52. x += l;
  53. else if(i & k)
  54. x += 3 * l;
  55. else if(j & k)
  56. x += 2 * l;
  57. }
  58. data[j * w + i] = (double)(x + 1) / (n * n + 1);
  59. }
  60. return ret;
  61. }
  62. /* Generate an error diffusion matrix. */
  63. if(!strncmp(name, "ediff:", 6))
  64. {
  65. float const *ker;
  66. int w, h;
  67. if(!strcmp(name + 6, "fs"))
  68. {
  69. static float const myker[] =
  70. {
  71. 0., 1., 7./16,
  72. 3./16, 5./16, 1./16,
  73. };
  74. ker = myker; w = 3; h = 2;
  75. }
  76. else if(!strcmp(name + 6, "jajuni"))
  77. {
  78. static float const myker[] =
  79. {
  80. 0., 0., 1., 7./48, 5./48,
  81. 3./48, 5./48, 7./48, 5./48, 3./48,
  82. 1./48, 3./48, 5./48, 3./48, 1./48,
  83. };
  84. ker = myker; w = 5; h = 3;
  85. }
  86. else if(!strcmp(name + 6, "atkinson"))
  87. {
  88. static float const myker[] =
  89. {
  90. 0., 1., 1./8, 1./8,
  91. 1./8, 1./8, 1./8, 0.,
  92. 0., 1./8, 0., 0.,
  93. };
  94. ker = myker; w = 4; h = 3;
  95. }
  96. else if(!strcmp(name + 6, "fan"))
  97. {
  98. static float const myker[] =
  99. {
  100. 0., 0., 1., 7./16,
  101. 1./16, 3./16, 5./16, 0.,
  102. };
  103. ker = myker; w = 4; h = 2;
  104. }
  105. else if(!strcmp(name + 6, "shiaufan"))
  106. {
  107. static float const myker[] =
  108. {
  109. 0., 0., 1., 1./2,
  110. 1./8, 1./8, 1./4, 0.,
  111. };
  112. ker = myker; w = 4; h = 2;
  113. }
  114. else if(!strcmp(name + 6, "shiaufan2"))
  115. {
  116. static float const myker[] =
  117. {
  118. 0., 0., 0., 1., 1./2,
  119. 1./16, 1./16, 1./8, 1./4, 0.,
  120. };
  121. ker = myker; w = 5; h = 2;
  122. }
  123. else if(!strcmp(name + 6, "stucki"))
  124. {
  125. static float const myker[] =
  126. {
  127. 0., 0., 1., 8./42, 4./42,
  128. 2./42, 4./42, 8./42, 4./42, 2./42,
  129. 1./42, 2./42, 4./42, 2./42, 1./42,
  130. };
  131. ker = myker; w = 5; h = 3;
  132. }
  133. else if(!strcmp(name + 6, "burkes"))
  134. {
  135. static float const myker[] =
  136. {
  137. 0., 0., 1., 4./16, 2./16,
  138. 1./16, 2./16, 4./16, 2./16, 1./16,
  139. };
  140. ker = myker; w = 5; h = 2;
  141. }
  142. else if(!strcmp(name + 6, "sierra"))
  143. {
  144. static float const myker[] =
  145. {
  146. 0., 0., 1., 5./32, 3./32,
  147. 2./32, 4./32, 5./32, 4./32, 2./32,
  148. 0., 2./32, 3./32, 2./32, 0.,
  149. };
  150. ker = myker; w = 5; h = 3;
  151. }
  152. else if(!strcmp(name + 6, "sierra2"))
  153. {
  154. static float const myker[] =
  155. {
  156. 0., 0., 1., 4./16, 3./16,
  157. 1./16, 2./16, 3./16, 2./16, 1./16,
  158. };
  159. ker = myker; w = 5; h = 2;
  160. }
  161. else if(!strcmp(name + 6, "lite"))
  162. {
  163. static float const myker[] =
  164. {
  165. 0., 1., 1./2,
  166. 1./4, 1./4, 0.,
  167. };
  168. ker = myker; w = 3; h = 2;
  169. }
  170. else
  171. return NULL;
  172. ret = pipi_new(w, h);
  173. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  174. memcpy(pix->pixels, ker, w * h * sizeof(float));
  175. return ret;
  176. }
  177. /* Generate a completely random image. */
  178. if(!strncmp(name, "random:", 7))
  179. {
  180. unsigned int ctx = 1;
  181. int x, y, t, w, h;
  182. w = atoi(name + 7);
  183. name = strchr(name + 7, 'x');
  184. if(!name)
  185. return NULL;
  186. h = atoi(name + 1);
  187. if(w <= 0 || h <= 0)
  188. return NULL;
  189. ret = pipi_new(w, h);
  190. pix = pipi_getpixels(ret, PIPI_PIXELS_RGBA_F);
  191. data = (float *)pix->pixels;
  192. for(y = 0; y < h; y++)
  193. for(x = 0; x < w; x++)
  194. {
  195. for(t = 0; t < 3; t++)
  196. {
  197. long hi, lo;
  198. hi = ctx / 12773L;
  199. lo = ctx % 12773L;
  200. ctx = 16807L * lo - 2836L * hi;
  201. if(ctx <= 0)
  202. ctx += 0x7fffffffL;
  203. data[4 * (y * w + x) + t]
  204. = (double)((ctx % 65536) / 65535.);
  205. }
  206. data[4 * (y * w + x) + 3] = 1.0;
  207. }
  208. return ret;
  209. }
  210. return NULL;
  211. }