Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

114 righe
2.6 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", 5))
  31. {
  32. int i, j, w, h, n;
  33. w = atoi(name + 5);
  34. name = strchr(name + 5, '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 a completely random image. */
  63. if(!strncmp(name, "random", 6))
  64. {
  65. unsigned int ctx = 1;
  66. int x, y, w, h;
  67. w = atoi(name + 6);
  68. name = strchr(name + 6, 'x');
  69. if(!name)
  70. return NULL;
  71. h = atoi(name + 1);
  72. if(w <= 0 || h <= 0)
  73. return NULL;
  74. ret = pipi_new(w, h);
  75. pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
  76. data = (float *)pix->pixels;
  77. for(y = 0; y < h; y++)
  78. for(x = 0; x < w; x++)
  79. {
  80. long hi, lo;
  81. hi = ctx / 12773L;
  82. lo = ctx % 12773L;
  83. ctx = 16807L * lo - 2836L * hi;
  84. if(ctx <= 0)
  85. ctx += 0x7fffffffL;
  86. data[y * w + x] = (double)((ctx % 65536) / 65535.);
  87. }
  88. return ret;
  89. }
  90. return NULL;
  91. }