Bibliothèque de traitements d'images en virgule flottante. http://la.buvette.org/photos/cumul/
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.

operators.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * OPERATORS
  3. *
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include "../floatimg.h"
  12. extern int verbosity; /* must be declared around main() */
  13. /* ---------------------------------------------------------------- */
  14. /*
  15. * A + B -> D
  16. * why is this func so slow ?
  17. */
  18. int fimg_add_3(FloatImg *a, FloatImg *b, FloatImg *d)
  19. {
  20. int idx, nbpixels;
  21. #if DEBUG_LEVEL
  22. fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
  23. #endif
  24. if (3 != a->type || 3 != b->type || 3 != d->type) {
  25. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  26. return -8;
  27. }
  28. nbpixels = a->width * a->height;
  29. for (idx=0; idx<nbpixels; idx++) {
  30. d->R[idx] = a->R[idx] + b->R[idx];
  31. d->G[idx] = a->G[idx] + b->G[idx];
  32. d->B[idx] = a->B[idx] + b->B[idx];
  33. }
  34. return 0;
  35. }
  36. /* ---------------------------------------------------------------- */
  37. /*
  38. * B += A may be faster than fimg_add_3 ?
  39. */
  40. int fimg_add_2(FloatImg *a, FloatImg *b)
  41. {
  42. int idx, nbpixels;
  43. #if DEBUG_LEVEL
  44. fprintf(stderr, ">>> %s ( %p %p )\n", __func__, a, b);
  45. #endif
  46. if (3 != a->type || 3 != b->type) {
  47. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  48. return -8;
  49. }
  50. nbpixels = a->width * a->height;
  51. for (idx=0; idx<nbpixels; idx++) {
  52. b->R[idx] += a->R[idx];
  53. b->G[idx] += a->G[idx];
  54. b->B[idx] += a->B[idx];
  55. }
  56. return 0;
  57. }
  58. /* ---------------------------------------------------------------- */
  59. /*
  60. * A - B -> D
  61. */
  62. int fimg_sub_3(FloatImg *a, FloatImg *b, FloatImg *d)
  63. {
  64. int idx, nbpixels;
  65. #if DEBUG_LEVEL
  66. fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
  67. #endif
  68. if (3 != a->type || 3 != b->type || 3 != d->type) {
  69. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  70. return -8;
  71. }
  72. nbpixels = a->width * a->height;
  73. /* maybe we can speedup this loop for
  74. * avoiding the cache strashing ?
  75. */
  76. for (idx=0; idx<nbpixels; idx++) {
  77. d->R[idx] = fabs(a->R[idx] - b->R[idx]);
  78. d->G[idx] = fabs(a->G[idx] - b->G[idx]);
  79. d->B[idx] = fabs(a->B[idx] - b->B[idx]);
  80. }
  81. return 0;
  82. }
  83. /* ---------------------------------------------------------------- */
  84. /*
  85. * A * B -> D
  86. */
  87. int fimg_mul_3(FloatImg *a, FloatImg *b, FloatImg *d)
  88. {
  89. int idx, nbpixels;
  90. #if DEBUG_LEVEL
  91. fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
  92. #endif
  93. if (3 != a->type || 3 != b->type || 3 != d->type) {
  94. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  95. return -8;
  96. }
  97. nbpixels = a->width * a->height;
  98. for (idx=0; idx<nbpixels; idx++) {
  99. d->R[idx] = a->R[idx] * b->R[idx];
  100. d->G[idx] = a->G[idx] * b->G[idx];
  101. d->B[idx] = a->B[idx] * b->B[idx];
  102. }
  103. return 0;
  104. }
  105. /* ---------------------------------------------------------------- */
  106. int fimg_minimum(FloatImg *a, FloatImg *b, FloatImg *d)
  107. {
  108. int idx, nbiter;
  109. #if DEBUG_LEVEL
  110. fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
  111. #endif
  112. if (3 != a->type || 3 != b->type || 3 != d->type) {
  113. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  114. return -8;
  115. }
  116. nbiter = a->width * a->height * 3;
  117. for (idx=0; idx<nbiter; idx++) {
  118. if (a->R[idx] > b->R[idx])
  119. d->R[idx] = a->R[idx];
  120. else
  121. d->R[idx] = b->R[idx];
  122. }
  123. return 0;
  124. }
  125. /* ---------------------------------------------------------------- */
  126. int fimg_maximum(FloatImg *a, FloatImg *b, FloatImg *d)
  127. {
  128. int idx, nbiter;
  129. #if DEBUG_LEVEL
  130. fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, a, b, d);
  131. #endif
  132. if (3 != a->type || 3 != b->type || 3 != d->type) {
  133. fprintf(stderr, "%s : got a bad type fimg\n", __func__);
  134. return -8;
  135. }
  136. nbiter = a->width * a->height * 3;
  137. for (idx=0; idx<nbiter; idx++) {
  138. if (a->R[idx] < b->R[idx])
  139. d->R[idx] = a->R[idx];
  140. else
  141. d->R[idx] = b->R[idx];
  142. }
  143. return 0;
  144. }
  145. /* ---------------------------------------------------------------- */