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.
 
 
 
 
 
 

164 lines
4.0 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
  97. return NULL;
  98. ret = pipi_new(w, h);
  99. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  100. memcpy(pix->pixels, ker, w * h * sizeof(float));
  101. return ret;
  102. }
  103. /* Generate a completely random image. */
  104. if(!strncmp(name, "random:", 7))
  105. {
  106. unsigned int ctx = 1;
  107. int x, y, t, w, h;
  108. w = atoi(name + 7);
  109. name = strchr(name + 7, 'x');
  110. if(!name)
  111. return NULL;
  112. h = atoi(name + 1);
  113. if(w <= 0 || h <= 0)
  114. return NULL;
  115. ret = pipi_new(w, h);
  116. pix = pipi_getpixels(ret, PIPI_PIXELS_RGBA_F);
  117. data = (float *)pix->pixels;
  118. for(y = 0; y < h; y++)
  119. for(x = 0; x < w; x++)
  120. {
  121. for(t = 0; t < 3; t++)
  122. {
  123. long hi, lo;
  124. hi = ctx / 12773L;
  125. lo = ctx % 12773L;
  126. ctx = 16807L * lo - 2836L * hi;
  127. if(ctx <= 0)
  128. ctx += 0x7fffffffL;
  129. data[4 * (y * w + x) + t]
  130. = (double)((ctx % 65536) / 65535.);
  131. }
  132. data[4 * (y * w + x) + 3] = 1.0;
  133. }
  134. return ret;
  135. }
  136. return NULL;
  137. }