Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

220 lignes
5.9 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. * mulscreen.c: multiply and screen computation functions
  16. */
  17. #include "config.h"
  18. #include "pipi.h"
  19. #include "pipi_internals.h"
  20. pipi_image_t *pipi_multiply(pipi_image_t *img1, pipi_image_t *img2)
  21. {
  22. pipi_image_t *dst;
  23. pipi_pixels_t *img1p, *img2p, *dstp;
  24. float *img1data, *img2data, *dstdata;
  25. int x, y, w, h;
  26. if(img1->w != img2->w || img1->h != img2->h)
  27. return NULL;
  28. w = img1->w;
  29. h = img1->h;
  30. dst = pipi_new(w, h);
  31. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  32. dstdata = (float *)dstp->pixels;
  33. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  34. img1data = (float *)img1p->pixels;
  35. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  36. img2data = (float *)img2p->pixels;
  37. for(y = 0; y < h; y++)
  38. {
  39. for(x = 0; x < w; x++)
  40. {
  41. float p, q;
  42. p = img1data[4 * (y * w + x)];
  43. q = img2data[4 * (y * w + x)];
  44. dstdata[4 * (y * w + x)] = p * q;
  45. p = img1data[4 * (y * w + x) + 1];
  46. q = img2data[4 * (y * w + x) + 1];
  47. dstdata[4 * (y * w + x) + 1] = p * q;
  48. p = img1data[4 * (y * w + x) + 2];
  49. q = img2data[4 * (y * w + x) + 2];
  50. dstdata[4 * (y * w + x) + 2] = p * q;
  51. p = img1data[4 * (y * w + x) + 3];
  52. q = img2data[4 * (y * w + x) + 3];
  53. dstdata[4 * (y * w + x) + 3] = p * q;
  54. }
  55. }
  56. return dst;
  57. }
  58. pipi_image_t *pipi_divide(pipi_image_t *img1, pipi_image_t *img2)
  59. {
  60. pipi_image_t *dst;
  61. pipi_pixels_t *img1p, *img2p, *dstp;
  62. float *img1data, *img2data, *dstdata;
  63. int x, y, w, h;
  64. if(img1->w != img2->w || img1->h != img2->h)
  65. return NULL;
  66. w = img1->w;
  67. h = img1->h;
  68. dst = pipi_new(w, h);
  69. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  70. dstdata = (float *)dstp->pixels;
  71. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  72. img1data = (float *)img1p->pixels;
  73. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  74. img2data = (float *)img2p->pixels;
  75. for(y = 0; y < h; y++)
  76. {
  77. for(x = 0; x < w; x++)
  78. {
  79. float p, q;
  80. p = img1data[4 * (y * w + x)];
  81. q = img2data[4 * (y * w + x)];
  82. dstdata[4 * (y * w + x)] = p >= q ? 1. : p / q;
  83. p = img1data[4 * (y * w + x) + 1];
  84. q = img2data[4 * (y * w + x) + 1];
  85. dstdata[4 * (y * w + x) + 1] = p >= q ? 1. : p / q;
  86. p = img1data[4 * (y * w + x) + 2];
  87. q = img2data[4 * (y * w + x) + 2];
  88. dstdata[4 * (y * w + x) + 2] = p >= q ? 1. : p / q;
  89. p = img1data[4 * (y * w + x) + 3];
  90. q = img2data[4 * (y * w + x) + 3];
  91. dstdata[4 * (y * w + x) + 3] = p >= q ? 1. : p / q;
  92. }
  93. }
  94. return dst;
  95. }
  96. pipi_image_t *pipi_screen(pipi_image_t *img1, pipi_image_t *img2)
  97. {
  98. pipi_image_t *dst;
  99. pipi_pixels_t *img1p, *img2p, *dstp;
  100. float *img1data, *img2data, *dstdata;
  101. int x, y, w, h;
  102. if(img1->w != img2->w || img1->h != img2->h)
  103. return NULL;
  104. w = img1->w;
  105. h = img1->h;
  106. dst = pipi_new(w, h);
  107. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  108. dstdata = (float *)dstp->pixels;
  109. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  110. img1data = (float *)img1p->pixels;
  111. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  112. img2data = (float *)img2p->pixels;
  113. for(y = 0; y < h; y++)
  114. {
  115. for(x = 0; x < w; x++)
  116. {
  117. float p, q;
  118. p = img1data[4 * (y * w + x)];
  119. q = img2data[4 * (y * w + x)];
  120. dstdata[4 * (y * w + x)] = p + q - p * q;
  121. p = img1data[4 * (y * w + x) + 1];
  122. q = img2data[4 * (y * w + x) + 1];
  123. dstdata[4 * (y * w + x) + 1] = p + q - p * q;
  124. p = img1data[4 * (y * w + x) + 2];
  125. q = img2data[4 * (y * w + x) + 2];
  126. dstdata[4 * (y * w + x) + 2] = p + q - p * q;
  127. p = img1data[4 * (y * w + x) + 3];
  128. q = img2data[4 * (y * w + x) + 3];
  129. dstdata[4 * (y * w + x) + 3] = p + q - p * q;
  130. }
  131. }
  132. return dst;
  133. }
  134. pipi_image_t *pipi_overlay(pipi_image_t *img1, pipi_image_t *img2)
  135. {
  136. pipi_image_t *dst;
  137. pipi_pixels_t *img1p, *img2p, *dstp;
  138. float *img1data, *img2data, *dstdata;
  139. int x, y, w, h;
  140. if(img1->w != img2->w || img1->h != img2->h)
  141. return NULL;
  142. w = img1->w;
  143. h = img1->h;
  144. dst = pipi_new(w, h);
  145. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  146. dstdata = (float *)dstp->pixels;
  147. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  148. img1data = (float *)img1p->pixels;
  149. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  150. img2data = (float *)img2p->pixels;
  151. for(y = 0; y < h; y++)
  152. {
  153. for(x = 0; x < w; x++)
  154. {
  155. float p, q;
  156. p = img1data[4 * (y * w + x)];
  157. q = img2data[4 * (y * w + x)];
  158. dstdata[4 * (y * w + x)] = p * (p + 2. * q * (1. - p));
  159. p = img1data[4 * (y * w + x) + 1];
  160. q = img2data[4 * (y * w + x) + 1];
  161. dstdata[4 * (y * w + x) + 1] = p * (p + 2. * q * (1. - p));
  162. p = img1data[4 * (y * w + x) + 2];
  163. q = img2data[4 * (y * w + x) + 2];
  164. dstdata[4 * (y * w + x) + 2] = p * (p + 2. * q * (1. - p));
  165. p = img1data[4 * (y * w + x) + 3];
  166. q = img2data[4 * (y * w + x) + 3];
  167. dstdata[4 * (y * w + x) + 3] = p * (p + 2. * q * (1. - p));
  168. }
  169. }
  170. return dst;
  171. }