imconv.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* image conversion */
  16. #ifndef CONV_H
  17. #define CONV_H
  18. #include <climits>
  19. #include "image.h"
  20. #include "imutil.h"
  21. #include "misc.h"
  22. #define RED_WEIGHT 0.299
  23. #define GREEN_WEIGHT 0.587
  24. #define BLUE_WEIGHT 0.114
  25. static image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
  26. int width = input->width();
  27. int height = input->height();
  28. image<uchar> *output = new image<uchar>(width, height, false);
  29. for (int y = 0; y < height; y++) {
  30. for (int x = 0; x < width; x++) {
  31. imRef(output, x, y) = (uchar)
  32. (imRef(input, x, y).r * RED_WEIGHT +
  33. imRef(input, x, y).g * GREEN_WEIGHT +
  34. imRef(input, x, y).b * BLUE_WEIGHT);
  35. }
  36. }
  37. return output;
  38. }
  39. static image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
  40. int width = input->width();
  41. int height = input->height();
  42. image<rgb> *output = new image<rgb>(width, height, false);
  43. for (int y = 0; y < height; y++) {
  44. for (int x = 0; x < width; x++) {
  45. imRef(output, x, y).r = imRef(input, x, y);
  46. imRef(output, x, y).g = imRef(input, x, y);
  47. imRef(output, x, y).b = imRef(input, x, y);
  48. }
  49. }
  50. return output;
  51. }
  52. static image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
  53. int width = input->width();
  54. int height = input->height();
  55. image<float> *output = new image<float>(width, height, false);
  56. for (int y = 0; y < height; y++) {
  57. for (int x = 0; x < width; x++) {
  58. imRef(output, x, y) = imRef(input, x, y);
  59. }
  60. }
  61. return output;
  62. }
  63. static image<float> *imageINTtoFLOAT(image<int> *input) {
  64. int width = input->width();
  65. int height = input->height();
  66. image<float> *output = new image<float>(width, height, false);
  67. for (int y = 0; y < height; y++) {
  68. for (int x = 0; x < width; x++) {
  69. imRef(output, x, y) = imRef(input, x, y);
  70. }
  71. }
  72. return output;
  73. }
  74. static image<uchar> *imageFLOATtoUCHAR(image<float> *input,
  75. float min, float max) {
  76. int width = input->width();
  77. int height = input->height();
  78. image<uchar> *output = new image<uchar>(width, height, false);
  79. if (max == min)
  80. return output;
  81. float scale = UCHAR_MAX / (max - min);
  82. for (int y = 0; y < height; y++) {
  83. for (int x = 0; x < width; x++) {
  84. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  85. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  86. }
  87. }
  88. return output;
  89. }
  90. static image<uchar> *imageFLOATtoUCHAR(image<float> *input) {
  91. float min, max;
  92. min_max(input, &min, &max);
  93. return imageFLOATtoUCHAR(input, min, max);
  94. }
  95. static image<long> *imageUCHARtoLONG(image<uchar> *input) {
  96. int width = input->width();
  97. int height = input->height();
  98. image<long> *output = new image<long>(width, height, false);
  99. for (int y = 0; y < height; y++) {
  100. for (int x = 0; x < width; x++) {
  101. imRef(output, x, y) = imRef(input, x, y);
  102. }
  103. }
  104. return output;
  105. }
  106. static image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
  107. int width = input->width();
  108. int height = input->height();
  109. image<uchar> *output = new image<uchar>(width, height, false);
  110. if (max == min)
  111. return output;
  112. float scale = UCHAR_MAX / (float)(max - min);
  113. for (int y = 0; y < height; y++) {
  114. for (int x = 0; x < width; x++) {
  115. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  116. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  117. }
  118. }
  119. return output;
  120. }
  121. static image<uchar> *imageLONGtoUCHAR(image<long> *input) {
  122. long min, max;
  123. min_max(input, &min, &max);
  124. return imageLONGtoUCHAR(input, min, max);
  125. }
  126. static image<uchar> *imageSHORTtoUCHAR(image<short> *input,
  127. short min, short max) {
  128. int width = input->width();
  129. int height = input->height();
  130. image<uchar> *output = new image<uchar>(width, height, false);
  131. if (max == min)
  132. return output;
  133. float scale = UCHAR_MAX / (float)(max - min);
  134. for (int y = 0; y < height; y++) {
  135. for (int x = 0; x < width; x++) {
  136. uchar val = (uchar)((imRef(input, x, y) - min) * scale);
  137. imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
  138. }
  139. }
  140. return output;
  141. }
  142. static image<uchar> *imageSHORTtoUCHAR(image<short> *input) {
  143. short min, max;
  144. min_max(input, &min, &max);
  145. return imageSHORTtoUCHAR(input, min, max);
  146. }
  147. #endif