您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

174 行
4.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. * subadd.c: sub, add and difference computation functions
  16. */
  17. #include "config.h"
  18. #include "common.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_getpixels(dst, PIPI_PIXELS_RGBA_F);
  34. dstdata = (float *)dstp->pixels;
  35. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  36. img1data = (float *)img1p->pixels;
  37. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  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_getpixels(dst, PIPI_PIXELS_RGBA_F);
  72. dstdata = (float *)dstp->pixels;
  73. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  74. img1data = (float *)img1p->pixels;
  75. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  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_getpixels(dst, PIPI_PIXELS_RGBA_F);
  110. dstdata = (float *)dstp->pixels;
  111. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  112. img1data = (float *)img1p->pixels;
  113. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  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. }