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 | |
---|
29 | #define RED_WEIGHT 0.299 |
---|
30 | #define GREEN_WEIGHT 0.587 |
---|
31 | #define BLUE_WEIGHT 0.114 |
---|
32 | /* |
---|
33 | static image<uchar> *imageRGBtoGRAY(image<rgb> *input) { |
---|
34 | int width = input->width(); |
---|
35 | int height = input->height(); |
---|
36 | image<uchar> *output = new image<uchar>(width, height, false); |
---|
37 | |
---|
38 | for (int y = 0; y < height; y++) { |
---|
39 | for (int x = 0; x < width; x++) { |
---|
40 | imRef(output, x, y) = (uchar) |
---|
41 | (imRef(input, x, y).r * RED_WEIGHT + |
---|
42 | imRef(input, x, y).g * GREEN_WEIGHT + |
---|
43 | imRef(input, x, y).b * BLUE_WEIGHT); |
---|
44 | } |
---|
45 | } |
---|
46 | return output; |
---|
47 | } |
---|
48 | |
---|
49 | static image<rgb> *imageGRAYtoRGB(image<uchar> *input) { |
---|
50 | int width = input->width(); |
---|
51 | int height = input->height(); |
---|
52 | image<rgb> *output = new image<rgb>(width, height, false); |
---|
53 | |
---|
54 | for (int y = 0; y < height; y++) { |
---|
55 | for (int x = 0; x < width; x++) { |
---|
56 | imRef(output, x, y).r = imRef(input, x, y); |
---|
57 | imRef(output, x, y).g = imRef(input, x, y); |
---|
58 | imRef(output, x, y).b = imRef(input, x, y); |
---|
59 | } |
---|
60 | } |
---|
61 | return output; |
---|
62 | } |
---|
63 | |
---|
64 | static image<float> *imageUCHARtoFLOAT(image<uchar> *input) { |
---|
65 | int width = input->width(); |
---|
66 | int height = input->height(); |
---|
67 | image<float> *output = new image<float>(width, height, false); |
---|
68 | |
---|
69 | for (int y = 0; y < height; y++) { |
---|
70 | for (int x = 0; x < width; x++) { |
---|
71 | imRef(output, x, y) = 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 |
---|