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.

stock.c 2.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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", 5))
  31. {
  32. int i, j, n = atoi(name + 5);
  33. if(n <= 0 || (n & (n - 1))) /* is n a power of two? */
  34. return NULL;
  35. ret = pipi_new(n, n);
  36. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  37. data = (float *)pix->pixels;
  38. for(j = 0; j < n; j++)
  39. for(i = 0; i < n; 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 * n + i] = (double)(x + 1) / (n * n + 1);
  52. }
  53. return ret;
  54. }
  55. /* Generate a completely random image. */
  56. if(!strncmp(name, "random", 6))
  57. {
  58. unsigned int ctx = 1;
  59. int x, y, w, h;
  60. w = atoi(name + 6);
  61. name = strchr(name + 6, 'x');
  62. if(!name)
  63. return NULL;
  64. h = atoi(name + 1);
  65. if(w <= 0 || h <= 0)
  66. return NULL;
  67. ret = pipi_new(w, h);
  68. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  69. data = (float *)pix->pixels;
  70. for(y = 0; y < h; y++)
  71. for(x = 0; x < w; x++)
  72. {
  73. long hi, lo;
  74. hi = ctx / 12773L;
  75. lo = ctx % 12773L;
  76. ctx = 16807L * lo - 2836L * hi;
  77. if(ctx <= 0)
  78. ctx += 0x7fffffffL;
  79. data[y * w + x] = (double)((ctx % 65536) / 65535.);
  80. }
  81. return ret;
  82. }
  83. return NULL;
  84. }