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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * subadd.c: sub, add and difference computation functions
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include "pipi.h"
  21. #include "pipi_internals.h"
  22. pipi_image_t *pipi_add(pipi_image_t *img1, pipi_image_t *img2)
  23. {
  24. pipi_image_t *dst;
  25. pipi_pixels_t *img1p, *img2p, *dstp;
  26. float *img1data, *img2data, *dstdata;
  27. int x, y, w, h;
  28. if(img1->w != img2->w || img1->h != img2->h)
  29. return NULL;
  30. w = img1->w;
  31. h = img1->h;
  32. dst = pipi_new(w, h);
  33. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  34. dstdata = (float *)dstp->pixels;
  35. img1p = pipi_get_pixels(img1, PIPI_PIXELS_RGBA_F32);
  36. img1data = (float *)img1p->pixels;
  37. img2p = pipi_get_pixels(img2, PIPI_PIXELS_RGBA_F32);
  38. img2data = (float *)img2p->pixels;
  39. for(y = 0; y < h; y++)
  40. {
  41. for(x = 0; x < w; x++)
  42. {
  43. float p, q;
  44. p = img1data[4 * (y * w + x)];
  45. q = img2data[4 * (y * w + x)];
  46. dstdata[4 * (y * w + x)] = (p + q < 1.) ? p + q : 1.;
  47. p = img1data[4 * (y * w + x) + 1];
  48. q = img2data[4 * (y * w + x) + 1];
  49. dstdata[4 * (y * w + x) + 1] = (p + q < 1.) ? p + q : 1.;
  50. p = img1data[4 * (y * w + x) + 2];
  51. q = img2data[4 * (y * w + x) + 2];
  52. dstdata[4 * (y * w + x) + 2] = (p + q < 1.) ? p + q : 1.;
  53. p = img1data[4 * (y * w + x) + 3];
  54. q = img2data[4 * (y * w + x) + 3];
  55. dstdata[4 * (y * w + x) + 3] = (p + q < 1.) ? p + q : 1.;
  56. }
  57. }
  58. return dst;
  59. }
  60. pipi_image_t *pipi_sub(pipi_image_t *img1, pipi_image_t *img2)
  61. {
  62. pipi_image_t *dst;
  63. pipi_pixels_t *img1p, *img2p, *dstp;
  64. float *img1data, *img2data, *dstdata;
  65. int x, y, w, h;
  66. if(img1->w != img2->w || img1->h != img2->h)
  67. return NULL;
  68. w = img1->w;
  69. h = img1->h;
  70. dst = pipi_new(w, h);
  71. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  72. dstdata = (float *)dstp->pixels;
  73. img1p = pipi_get_pixels(img1, PIPI_PIXELS_RGBA_F32);
  74. img1data = (float *)img1p->pixels;
  75. img2p = pipi_get_pixels(img2, PIPI_PIXELS_RGBA_F32);
  76. img2data = (float *)img2p->pixels;
  77. for(y = 0; y < h; y++)
  78. {
  79. for(x = 0; x < w; x++)
  80. {
  81. float p, q;
  82. p = img1data[4 * (y * w + x)];
  83. q = img2data[4 * (y * w + x)];
  84. dstdata[4 * (y * w + x)] = p < q ? 0 : p - q;
  85. p = img1data[4 * (y * w + x) + 1];
  86. q = img2data[4 * (y * w + x) + 1];
  87. dstdata[4 * (y * w + x) + 1] = p < q ? 0 : p - q;
  88. p = img1data[4 * (y * w + x) + 2];
  89. q = img2data[4 * (y * w + x) + 2];
  90. dstdata[4 * (y * w + x) + 2] = p < q ? 0 : p - q;
  91. p = img1data[4 * (y * w + x) + 3];
  92. q = img2data[4 * (y * w + x) + 3];
  93. dstdata[4 * (y * w + x) + 3] = p < q ? 0 : p - q;
  94. }
  95. }
  96. return dst;
  97. }
  98. pipi_image_t *pipi_difference(pipi_image_t *img1, pipi_image_t *img2)
  99. {
  100. pipi_image_t *dst;
  101. pipi_pixels_t *img1p, *img2p, *dstp;
  102. float *img1data, *img2data, *dstdata;
  103. int x, y, w, h;
  104. if(img1->w != img2->w || img1->h != img2->h)
  105. return NULL;
  106. w = img1->w;
  107. h = img1->h;
  108. dst = pipi_new(w, h);
  109. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  110. dstdata = (float *)dstp->pixels;
  111. img1p = pipi_get_pixels(img1, PIPI_PIXELS_RGBA_F32);
  112. img1data = (float *)img1p->pixels;
  113. img2p = pipi_get_pixels(img2, PIPI_PIXELS_RGBA_F32);
  114. img2data = (float *)img2p->pixels;
  115. for(y = 0; y < h; y++)
  116. {
  117. for(x = 0; x < w; x++)
  118. {
  119. float p, q;
  120. p = img1data[4 * (y * w + x)];
  121. q = img2data[4 * (y * w + x)];
  122. dstdata[4 * (y * w + x)] = fabsf(p - q);
  123. p = img1data[4 * (y * w + x) + 1];
  124. q = img2data[4 * (y * w + x) + 1];
  125. dstdata[4 * (y * w + x) + 1] = fabsf(p - q);
  126. p = img1data[4 * (y * w + x) + 2];
  127. q = img2data[4 * (y * w + x) + 2];
  128. dstdata[4 * (y * w + x) + 2] = fabsf(p - q);
  129. p = img1data[4 * (y * w + x) + 3];
  130. q = img2data[4 * (y * w + x) + 3];
  131. dstdata[4 * (y * w + x) + 3] = fabsf(p - q);
  132. }
  133. }
  134. return dst;
  135. }