Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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