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