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 #pragma once
22 
23 #include <climits>
24 
25 #include "image.h"
26 #include "imutil.h"
27 #include "misc.h"
28 
29 namespace distance_transform
30 {
31  const double RED_WEIGHT = 0.299;
32  const double GREEN_WEIGHT = 0.584;
33  const double BLUE_WEIGHT = 0.114;
34 
35  static inline image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
36  int width = input->width();
37  int height = input->height();
38  image<uchar> *output = new image<uchar>(width, height, false);
39 
40  for (int y = 0; y < height; y++) {
41  for (int x = 0; x < width; x++) {
42  imRef(output, x, y) = (uchar)
43  (imRef(input, x, y).r * RED_WEIGHT +
44  imRef(input, x, y).g * GREEN_WEIGHT +
45  imRef(input, x, y).b * BLUE_WEIGHT);
46  }
47  }
48  return output;
49  }
50 
51  static inline image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
52  int width = input->width();
53  int height = input->height();
54  image<rgb> *output = new image<rgb>(width, height, false);
55 
56  for (int y = 0; y < height; y++) {
57  for (int x = 0; x < width; x++) {
58  imRef(output, x, y).r = imRef(input, x, y);
59  imRef(output, x, y).g = imRef(input, x, y);
60  imRef(output, x, y).b = imRef(input, x, y);
61  }
62  }
63  return output;
64  }
65 
66  static inline image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
67  int width = input->width();
68  int height = input->height();
69  image<float> *output = new image<float>(width, height, false);
70 
71  for (int y = 0; y < height; y++) {
72  for (int x = 0; x < width; x++) {
73  imRef(output, x, y) = imRef(input, x, y);
74  }
75  }
76  return output;
77  }
78 
79  static inline image<float> *imageINTtoFLOAT(image<int> *input) {
80  int width = input->width();
81  int height = input->height();
82  image<float> *output = new image<float>(width, height, false);
83 
84  for (int y = 0; y < height; y++) {
85  for (int x = 0; x < width; x++) {
86  imRef(output, x, y) = imRef(input, x, y);
87  }
88  }
89  return output;
90  }
91 
93  float min, float max) {
94  int width = input->width();
95  int height = input->height();
96  image<uchar> *output = new image<uchar>(width, height, false);
97 
98  if (max == min)
99  return output;
100 
101  float scale = UCHAR_MAX / (max - min);
102  for (int y = 0; y < height; y++) {
103  for (int x = 0; x < width; x++) {
104  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
105  imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
106  }
107  }
108  return output;
109  }
110 
111  static inline image<uchar> *imageFLOATtoUCHAR(image<float> *input) {
112  float min, max;
113  min_max(input, &min, &max);
114  return imageFLOATtoUCHAR(input, min, max);
115  }
116 
117  static inline image<long> *imageUCHARtoLONG(image<uchar> *input) {
118  int width = input->width();
119  int height = input->height();
120  image<long> *output = new image<long>(width, height, false);
121 
122  for (int y = 0; y < height; y++) {
123  for (int x = 0; x < width; x++) {
124  imRef(output, x, y) = imRef(input, x, y);
125  }
126  }
127  return output;
128  }
129 
130  static image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
131  int width = input->width();
132  int height = input->height();
133  image<uchar> *output = new image<uchar>(width, height, false);
134 
135  if (max == min)
136  return output;
137 
138  float scale = UCHAR_MAX / (float)(max - min);
139  for (int y = 0; y < height; y++) {
140  for (int x = 0; x < width; x++) {
141  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
142  imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
143  }
144  }
145  return output;
146  }
147 
148  static inline image<uchar> *imageLONGtoUCHAR(image<long> *input) {
149  long min, max;
150  min_max(input, &min, &max);
151  return imageLONGtoUCHAR(input, min, max);
152  }
153 
155  short min, short max) {
156  int width = input->width();
157  int height = input->height();
158  image<uchar> *output = new image<uchar>(width, height, false);
159 
160  if (max == min)
161  return output;
162 
163  float scale = UCHAR_MAX / (float)(max - min);
164  for (int y = 0; y < height; y++) {
165  for (int x = 0; x < width; x++) {
166  uchar val = (uchar)((imRef(input, x, y) - min) * scale);
167  imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
168  }
169  }
170  return output;
171  }
172 
173  static inline image<uchar> *imageSHORTtoUCHAR(image<short> *input) {
174  short min, max;
175  min_max(input, &min, &max);
176  return imageSHORTtoUCHAR(input, min, max);
177  }
178 
179 } // namespace
static image< uchar > * imageLONGtoUCHAR(image< long > *input, long min, long max)
Definition: imconv.h:130
static image< uchar > * imageFLOATtoUCHAR(image< float > *input, float min, float max)
Definition: imconv.h:92
const double BLUE_WEIGHT
Definition: imconv.h:33
void min_max(image< T > *im, T *ret_min, T *ret_max)
Definition: imutil.h:31
int width() const
Definition: image.h:44
static image< float > * imageINTtoFLOAT(image< int > *input)
Definition: imconv.h:79
TFSIMD_FORCE_INLINE const tfScalar & y() const
const double RED_WEIGHT
Definition: imconv.h:31
#define imRef(im, x, y)
Definition: image.h:60
static image< uchar > * imageRGBtoGRAY(image< rgb > *input)
Definition: imconv.h:35
int height() const
Definition: image.h:47
static image< uchar > * imageSHORTtoUCHAR(image< short > *input, short min, short max)
Definition: imconv.h:154
const double GREEN_WEIGHT
Definition: imconv.h:32
TFSIMD_FORCE_INLINE const tfScalar & x() const
static image< float > * imageUCHARtoFLOAT(image< uchar > *input)
Definition: imconv.h:66
unsigned char uchar
Definition: misc.h:27
T bound(const T &x, const T &min, const T &max)
Definition: misc.h:45
int min(int a, int b)
static image< long > * imageUCHARtoLONG(image< uchar > *input)
Definition: imconv.h:117
static image< rgb > * imageGRAYtoRGB(image< uchar > *input)
Definition: imconv.h:51


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:49