YARP  2.3.70.1
Yet Another Robot Platform
ImageCopy.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 RobotCub Consortium
3  * Authors: Paul Fitzpatrick
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
5  */
6 
7 #include <yarp/os/Log.h>
8 #include <yarp/sig/Image.h>
9 #include <yarp/sig/IplImage.h>
10 
11 #include <cstring>
12 #include <cstdio>
13 
14 using namespace yarp::sig;
15 
16 #define DBG if(0)
17 
18 // Default copy mechanism
19 template <class T1, class T2>
20 static inline void CopyPixel(const T1 *src, T2 *dest)
21 {
22  *dest = *src;
23 }
24 
39 
40 #define VALID_PIXEL(x) ((x>255)?255:((x<0)?0:x))
41 #define SPECIAL_COPY_BEGIN static void YARPDummyCopyPixel() {
42 #define SPECIAL_COPY(id1,id2) } static inline void CopyPixel(const Def_##id1 *src, Def_##id2 *dest) {
43 #define SPECIAL_COPY_END }
44 
45 static int implemented_yet = 1;
46 
48 
50  dest->r = dest->g = dest->b = *src;
52  dest->r = dest->g = dest->b = *src;
53  dest->a = 255;
54 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_BGRA)
55  dest->r = dest->g = dest->b = *src;
56  dest->a = 255;
57 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_RGB_INT)
58  dest->r = dest->g = dest->b = *src;
59 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_BGR)
60  dest->r = dest->g = dest->b = *src;
61 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_HSV)
62  dest->v = *src;
63 dest->h = dest->s = 0;
64 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_RGB_SIGNED)
65  dest->r = dest->g = dest->b = *src;
66 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_RGB_FLOAT)
67  dest->r = dest->g = dest->b = *src;
68 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_HSV_FLOAT)
69  dest->v = *src;
70 dest->h = dest->s = 0;
71 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_MONO_SIGNED)
72  *dest = *src >> 1;
73 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_INT)
74  *dest = *src;
75 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_MONO16)
76  *dest = *src;
77 SPECIAL_COPY(VOCAB_PIXEL_MONO,VOCAB_PIXEL_MONO_FLOAT)
78  *dest = *src;
79 
80 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_MONO)
81  *dest = (unsigned char)((src->r + src->g + src->b)/3);
82 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_MONO16)
83  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
84 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_INT)
85  *dest = (unsigned char)((src->r + src->g + src->b)/3);
86 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_HSV)
89  *dest = (char)((src->r + src->g + src->b)/3);
90 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_RGB_SIGNED)
91  dest->r = src->r; dest->g = src->g; dest->b = src->b;
92 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_RGBA)
93  dest->r = src->r; dest->g = src->g; dest->b = src->b;
94 dest->a = 255;
95 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_BGRA)
96  dest->r = src->r; dest->g = src->g; dest->b = src->b;
97 dest->a = 255;
98 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_RGB_INT)
99  dest->r = src->r; dest->g = src->g; dest->b = src->b;
100 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_MONO_FLOAT)
101  *dest = ((src->r + src->g + src->b)/3.0f);
102 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_RGB_FLOAT)
103  dest->r = src->r;
104 dest->g = src->g;
105 dest->b = src->b;
106 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_BGR)
107  dest->r = src->r;
108 dest->g = src->g;
109 dest->b = src->b;
110 SPECIAL_COPY(VOCAB_PIXEL_RGB,VOCAB_PIXEL_HSV_FLOAT)
111  yAssert(implemented_yet == 0);
112 
113 
114 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_MONO)
115  yAssert(implemented_yet == 0);
116 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_MONO16)
117  yAssert(implemented_yet == 0);
118 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_RGB)
119  yAssert(implemented_yet == 0);
120 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_RGBA)
121  yAssert(implemented_yet == 0);
122 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_BGRA)
123  yAssert(implemented_yet == 0);
124 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_RGB_INT)
125  yAssert(implemented_yet == 0);
126 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_BGR)
127  yAssert(implemented_yet == 0);
128 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_MONO_SIGNED)
129  yAssert(implemented_yet == 0);
130 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_RGB_SIGNED)
131  yAssert(implemented_yet == 0);
132 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_MONO_FLOAT)
133  yAssert(implemented_yet == 0);
134 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_RGB_FLOAT)
135  yAssert(implemented_yet == 0);
136 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_HSV_FLOAT)
137  yAssert(implemented_yet == 0);
138 SPECIAL_COPY(VOCAB_PIXEL_HSV,VOCAB_PIXEL_INT)
139  yAssert(implemented_yet == 0);
140 
141 
142 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_MONO)
143  *dest = (unsigned char)((src->r + src->g + src->b)/3);
144 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_MONO16)
145  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
146 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_INT)
147  *dest = (unsigned char)((src->r + src->g + src->b)/3);
148 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_HSV)
149  yAssert(implemented_yet == 0);
150 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_MONO_SIGNED)
151  *dest = (char)((src->r + src->g + src->b)/3);
152 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_RGB_SIGNED)
153  dest->r = src->r; dest->g = src->g; dest->b = src->b;
154 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_MONO_FLOAT)
155  *dest = ((src->r + src->g + src->b)/3.0f);
156 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_RGB_FLOAT)
157  dest->r = src->r;
158 dest->g = src->g;
159 dest->b = src->b;
160 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_RGB)
161  dest->r = src->r;
162 dest->g = src->g;
163 dest->b = src->b;
164 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_RGBA)
165  dest->r = src->r;
166 dest->g = src->g;
167 dest->b = src->b;
168 dest->a = 255;
169 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_BGRA)
170  dest->r = src->r;
171 dest->g = src->g;
172 dest->b = src->b;
173 dest->a = 255;
174 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_RGB_INT)
175  dest->r = src->r;
176 dest->g = src->g;
177 dest->b = src->b;
178 SPECIAL_COPY(VOCAB_PIXEL_BGR,VOCAB_PIXEL_HSV_FLOAT)
179  yAssert(implemented_yet == 0);
180 
181 
182 
183 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_MONO)
184  *dest = (unsigned char)((src->r + src->g + src->b)/3);
185 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_MONO16)
186  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
187 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_INT)
188  *dest = (unsigned char)((src->r + src->g + src->b)/3);
189 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_HSV)
190  yAssert(implemented_yet == 0);
191 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_MONO_SIGNED)
192  *dest = (char)((src->r + src->g + src->b)/3);
193 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_RGB_SIGNED)
194  dest->r = src->r; dest->g = src->g; dest->b = src->b;
195 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_MONO_FLOAT)
196  *dest = ((src->r + src->g + src->b)/3.0f);
197 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_RGB_FLOAT)
198  dest->r = src->r;
199 dest->g = src->g;
200 dest->b = src->b;
201 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_RGB)
202  dest->r = src->r;
203 dest->g = src->g;
204 dest->b = src->b;
205 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_BGRA)
206  dest->r = src->r;
207 dest->g = src->g;
208 dest->b = src->b;
209 dest->a = src->a;
210 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_BGR)
211  dest->r = src->r;
212 dest->g = src->g;
213 dest->b = src->b;
214 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_RGB_INT)
215  dest->r = src->r;
216 dest->g = src->g;
217 dest->b = src->b;
218 SPECIAL_COPY(VOCAB_PIXEL_RGBA,VOCAB_PIXEL_HSV_FLOAT)
219  yAssert(implemented_yet == 0);
220 
221 
222 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_MONO)
223  *dest = (unsigned char)((src->r + src->g + src->b)/3);
224 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_MONO16)
225  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
226 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_INT)
227  *dest = (unsigned char)((src->r + src->g + src->b)/3);
228 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_HSV)
229  yAssert(implemented_yet == 0);
230 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_MONO_SIGNED)
231  *dest = (char)((src->r + src->g + src->b)/3);
232 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_RGB_SIGNED)
233  dest->r = src->r; dest->g = src->g; dest->b = src->b;
234 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_MONO_FLOAT)
235  *dest = ((src->r + src->g + src->b)/3.0f);
236 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_RGB_FLOAT)
237  dest->r = src->r;
238 dest->g = src->g;
239 dest->b = src->b;
240 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_RGB)
241  dest->r = src->r;
242 dest->g = src->g;
243 dest->b = src->b;
244 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_RGBA)
245  dest->r = src->r;
246 dest->g = src->g;
247 dest->b = src->b;
248 dest->a = src->a;
249 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_BGR)
250  dest->r = src->r;
251 dest->g = src->g;
252 dest->b = src->b;
253 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_RGB_INT)
254  dest->r = src->r;
255 dest->g = src->g;
256 dest->b = src->b;
257 SPECIAL_COPY(VOCAB_PIXEL_BGRA,VOCAB_PIXEL_HSV_FLOAT)
258  yAssert(implemented_yet == 0);
259 
260 
261 
262 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_MONO)
263  *dest = (unsigned char)((src->r + src->g + src->b)/3);
264 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_MONO16)
265  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
266 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_INT)
267  *dest = (unsigned char)((src->r + src->g + src->b)/3);
268 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_HSV)
269  yAssert(implemented_yet == 0);
270 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_MONO_SIGNED)
271  *dest = (char)((src->r + src->g + src->b)/3);
272 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_RGB_SIGNED)
273  dest->r = (char)src->r; dest->g = (char)src->g; dest->b = (char)src->b;
274 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_MONO_FLOAT)
275  *dest = ((src->r + src->g + src->b)/3.0f);
276 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_RGB_FLOAT)
277  dest->r = (float)((int) src->r);
278 dest->g = (float)((int) src->g);
279 dest->b = (float)((int) src->b);
280 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_RGB)
281  dest->r = (unsigned char)src->r;
282 dest->g = (unsigned char)src->g;
283 dest->b = (unsigned char)src->b;
284 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_BGR)
285  dest->r = (unsigned char)src->r;
286 dest->g = (unsigned char)src->g;
287 dest->b = (unsigned char)src->b;
288 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_RGBA)
289  dest->r = (unsigned char)src->r;
290 dest->g = (unsigned char)src->g;
291 dest->b = (unsigned char)src->b;
292 dest->a = 255;
293 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_BGRA)
294  dest->r = (unsigned char)src->r;
295 dest->g = (unsigned char)src->g;
296 dest->b = (unsigned char)src->b;
297 dest->a = 255;
298 SPECIAL_COPY(VOCAB_PIXEL_RGB_INT,VOCAB_PIXEL_HSV_FLOAT)
299  yAssert(implemented_yet == 0);
300 
301 
302 
303 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_RGB)
304  dest->r = dest->g = dest->b = *src;
305 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_RGBA)
306  dest->r = dest->g = dest->b = *src; dest->a = 255;
307 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_BGRA)
308  dest->r = dest->g = dest->b = *src; dest->a = 255;
309 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_RGB_INT)
310  dest->r = dest->g = dest->b = *src;
311 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_BGR)
312  dest->r = dest->g = dest->b = *src;
313 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_HSV)
314  dest->v = *src;
315 dest->h = dest->s = 0;
316 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_RGB_SIGNED)
317  dest->r = dest->g = dest->b = *src;
318 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_RGB_FLOAT)
319  dest->r = dest->g = dest->b = *src;
320 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_HSV_FLOAT)
321  dest->v = *src;
322 dest->h = dest->s = 0;
323 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_MONO)
324  *dest = *src + 128;
325 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_INT)
326  *dest = *src;
327 SPECIAL_COPY(VOCAB_PIXEL_MONO_SIGNED,VOCAB_PIXEL_MONO16)
328  *dest = (yarp::sig::PixelMono16)(*src);
329 
330 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_MONO)
331  *dest = (unsigned char)((src->r + src->g + src->b)/3);
332 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_MONO16)
333  *dest = (PixelMono16)((src->r + src->g + src->b)/3);
334 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_INT)
335  *dest = (int)((src->r + src->g + src->b)/3);
336 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_HSV)
337  yAssert(implemented_yet == 0);
338 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_MONO_SIGNED)
339  *dest = (char)((src->r + src->g + src->b)/3);
340 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_RGB)
341  dest->r = src->r;
342 dest->g = src->g;
343 dest->b = src->b;
344 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_RGBA)
345  dest->r = src->r;
346 dest->g = src->g;
347 dest->b = src->b;
348 dest->a = 255;
349 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_BGRA)
350  dest->r = src->r;
351 dest->g = src->g;
352 dest->b = src->b;
353 dest->a = 255;
354 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_RGB_INT)
355  dest->r = src->r;
356 dest->g = src->g;
357 dest->b = src->b;
358 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_BGR)
359  dest->r = src->r;
360 dest->g = src->g;
361 dest->b = src->b;
362 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_MONO_FLOAT)
363  *dest = ((src->r + src->g + src->b)/3.0f);
364 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_RGB_FLOAT)
365  dest->r = src->r;
366 dest->g = src->g;
367 dest->b = src->b;
368 SPECIAL_COPY(VOCAB_PIXEL_RGB_SIGNED,VOCAB_PIXEL_HSV_FLOAT)
369  yAssert(implemented_yet == 0);
370 
371 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_MONO)
372  *dest = (unsigned char)*src;
373 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_MONO16)
374  *dest = (yarp::sig::PixelMono16)*src;
375 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_INT)
376  *dest = (unsigned char)*src;
377 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_MONO_SIGNED)
378  *dest = (char)*src;
379 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_RGB)
380  dest->r = dest->g = dest->b = (unsigned char)(*src);
381 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_RGBA)
382  dest->r = dest->g = dest->b = (unsigned char)(*src);
383 dest->a = 255;
384 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_BGRA)
385  dest->r = dest->g = dest->b = (unsigned char)(*src);
386 dest->a = 255;
387 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_RGB_INT)
388  dest->r = dest->g = dest->b = (int)(*src);
389 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_BGR)
390  dest->r = dest->g = dest->b = (unsigned char)(*src);
391 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_HSV)
392  dest->v = (unsigned char)*src;
393 dest->h = dest->s = 0;
394 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_RGB_SIGNED)
395  dest->r = dest->g = dest->b = (signed char) *src;
396 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_RGB_FLOAT)
397  dest->r = dest->g = dest->b = *src;
398 SPECIAL_COPY(VOCAB_PIXEL_MONO_FLOAT,VOCAB_PIXEL_HSV_FLOAT)
399  dest->v = *src;
400 dest->h = dest->s = 0;
401 
402 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_MONO)
403  *dest = (unsigned char)((src->r + src->g + src->b)/3);
404 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_INT)
405  *dest = (unsigned char)((src->r + src->g + src->b)/3);
406 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_MONO16)
407  *dest = (yarp::sig::PixelMono16)((src->r + src->g + src->b)/3);
408 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_HSV)
409  yAssert(implemented_yet == 0);
410 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_MONO_SIGNED)
411  *dest = (char)((src->r + src->g + src->b)/3);
412 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_RGB)
413  dest->r = (unsigned char) src->r;
414 dest->g = (unsigned char) src->g;
415 dest->b = (unsigned char) src->b;
416 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_RGBA)
417  dest->r = (unsigned char) src->r;
418 dest->g = (unsigned char) src->g;
419 dest->b = (unsigned char) src->b;
420 dest->a = 255;
421 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_BGRA)
422  dest->r = (unsigned char) src->r;
423 dest->g = (unsigned char) src->g;
424 dest->b = (unsigned char) src->b;
425 dest->a = 255;
426 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_RGB_INT)
427  dest->r = (int) src->r;
428 dest->g = (int) src->g;
429 dest->b = (int) src->b;
430 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_BGR)
431  dest->r = (unsigned char) src->r;
432 dest->g = (unsigned char) src->g;
433 dest->b = (unsigned char) src->b;
434 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_MONO_FLOAT)
435  *dest = ((src->r + src->g + src->b)/3);
436 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_RGB_SIGNED)
437  dest->r = (signed char) src->r;
438 dest->g = (signed char) src->g;
439 dest->b = (signed char) src->b;
440 SPECIAL_COPY(VOCAB_PIXEL_RGB_FLOAT,VOCAB_PIXEL_HSV_FLOAT)
441  yAssert(implemented_yet == 0);
442 
443 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_MONO)
444  yAssert(implemented_yet == 0);
445 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_MONO16)
446  yAssert(implemented_yet == 0);
447 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_RGB)
448  yAssert(implemented_yet == 0);
449 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_BGR)
450  yAssert(implemented_yet == 0);
451 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_RGBA)
452  yAssert(implemented_yet == 0);
453 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_BGRA)
454  yAssert(implemented_yet == 0);
455 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_RGB_INT)
456  yAssert(implemented_yet == 0);
457 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_MONO_SIGNED)
458  yAssert(implemented_yet == 0);
459 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_RGB_SIGNED)
460  yAssert(implemented_yet == 0);
461 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_MONO_FLOAT)
462  yAssert(implemented_yet == 0);
463 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_RGB_FLOAT)
464  yAssert(implemented_yet == 0);
465 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_HSV)
466  yAssert(implemented_yet == 0);
467 SPECIAL_COPY(VOCAB_PIXEL_HSV_FLOAT,VOCAB_PIXEL_INT)
468  yAssert(implemented_yet == 0);
469 
470 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_RGB)
471  dest->r = dest->g = dest->b = (char)*src;
472 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_RGBA)
473  dest->r = dest->g = dest->b = (char)*src;
474 dest->a = 255;
475 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_BGRA)
476  dest->r = dest->g = dest->b = (char)*src;
477 dest->a = 255;
478 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_RGB_INT)
479  dest->r = dest->g = dest->b = *src;
480 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_BGR)
481  dest->r = dest->g = dest->b = (char)*src;
482 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_HSV)
483  dest->v = (yarp::sig::PixelMono)(*src);
484 dest->h = dest->s = 0;
485 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_RGB_SIGNED)
486  dest->r = dest->g = dest->b = (yarp::sig::PixelMono)(*src);
487 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_MONO_FLOAT)
488  *dest = (float)(*src);
489 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_RGB_FLOAT)
490  dest->r = dest->g = dest->b = (float)(*src);
491 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_HSV_FLOAT)
492  dest->v = float(*src);
493 dest->h = dest->s = 0;
494 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_MONO_SIGNED)
495  *dest = (char)(*src >> 1);
496 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_MONO)
497  *dest = (yarp::sig::PixelMono)(*src);
498 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_MONO16)
499  *dest = (yarp::sig::PixelMono16)(*src);
500 
501 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_RGB)
502  dest->r = dest->g = dest->b = (char)*src;
503 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_RGBA)
504  dest->r = dest->g = dest->b = (char)*src;
505 dest->a = 255;
506 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_BGRA)
507  dest->r = dest->g = dest->b = (char)*src;
508 dest->a = 255;
509 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_RGB_INT)
510  dest->r = dest->g = dest->b = (int)((unsigned) *src);
511 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_INT)
512  *dest = (int)((unsigned) *src);
513 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_BGR)
514  dest->r = dest->g = dest->b = (char)*src;
515 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_HSV)
516  dest->v = (yarp::sig::PixelMono)(*src);
517 dest->h = dest->s = 0;
518 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_RGB_SIGNED)
519  dest->r = dest->g = dest->b = (yarp::sig::PixelMono)(*src);
520 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_MONO_FLOAT)
521  *dest = (float)(*src);
522 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_RGB_FLOAT)
523  dest->r = dest->g = dest->b = (float)(*src);
524 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_HSV_FLOAT)
525  dest->v = float(*src);
526 dest->h = dest->s = 0;
527 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_MONO_SIGNED)
528  *dest = (char)(*src >> 1);
529 SPECIAL_COPY(VOCAB_PIXEL_MONO16,VOCAB_PIXEL_MONO)
530  *dest = (yarp::sig::PixelMono)(*src);
531 SPECIAL_COPY(VOCAB_PIXEL_INT,VOCAB_PIXEL_INT)
532  *dest = *src;
533 
535 
536 //static inline int PAD_BYTES (int len, int pad)
537 //{
538 // const int rem = len % pad;
539 // return (rem != 0) ? (pad - rem) : rem;
540 //}
541 
544 template <class T1, class T2>
545 static void CopyPixels(const T1 *osrc, int q1, T2 *odest, int q2,
546  int w, int h,
547  bool flip)
548 {
549  const T1 *src = osrc;
550  T2 *dest = odest;
551  const int p1 = PAD_BYTES (w * sizeof(T1), q1);
552  const int p2 = PAD_BYTES (w * sizeof(T2), q2);
553  //const int step1 = w*sizeof(T1) + p1;
554  const int step2 = w*sizeof(T2) + p2;
555  DBG printf("q1 %d q2 %d (%dx%d) inc %d %d\n", q1, q2, w, h, p1, p2);
556 
557  if (flip) {
558  odest = (T2*)(((char *)odest) + step2*(h-1));
559  dest = odest;
560  }
561 
562  YARPDummyCopyPixel();
563  for (int i=0; i<h; i++)
564  {
565  DBG printf("x,y = %d,%d\n", 0,i);
566  for (int j = 0; j < w; j++)
567  {
568  CopyPixel(src,dest);
569  src++;
570  dest++;
571  }
572 
573  src = (const T1*)(((char *)src) + p1);
574  odest = (T2*)(((char *)odest) + step2*(flip?-1:1));
575  dest = odest;
576  }
577 }
578 
579 
580 #define HASH(id1,id2) ((int)(((int)(id1%65537))*11+((long int)(id2))))
581 #define HANDLE_CASE(len,x1,T1,q1,o1,x2,T2,q2,o2) CopyPixels((T1*)x1,q1,(T2*)x2,q2,w,h,o1!=o2);
582 #define MAKE_CASE(id1,id2) case HASH(id1,id2): HANDLE_CASE(len,src,Def_##id1,quantum1,topIsLow1,dest,Def_##id2,quantum2,topIsLow2); break;
583 #define MAKE_2CASE(id1,id2) MAKE_CASE(id1,id2); MAKE_CASE(id2,id1);
584 
585 // More elegant ways to do this, but needs to be efficient at pixel level
586 void Image::copyPixels(const unsigned char *src, int id1,
587  char unsigned *dest, int id2, int w, int h,
588  int imageSize, int quantum1, int quantum2,
589  bool topIsLow1, bool topIsLow2)
590 {
591  DBG printf("copyPixels...\n");
592 
593  if (id1==id2&&quantum1==quantum2&&topIsLow1==topIsLow2) {
594  memcpy(dest,src,imageSize);
595  return;
596  }
597 
598 
599  switch(HASH(id1,id2))
600  {
601  // Macros rely on len, x1, x2 variable names
602 
617 
631 
644 
656 
667 
668 
678 
687 
695 
702 
708 
713 
717 
720 
722 
723  default:
724  printf("*** Tried to copy type %d to %d\n", id1, id2);
725  std::exit(1);
726  break;
727  }
728 
729  DBG printf("... done copyPixels\n");
730 }
Floating point RGB pixel type.
Definition: Image.h:506
#define SPECIAL_COPY_END
Definition: ImageCopy.cpp:43
PixelRgbInt Def_VOCAB_PIXEL_RGB_INT
Definition: ImageCopy.cpp:38
PixelHsv Def_VOCAB_PIXEL_HSV
Definition: ImageCopy.cpp:30
PixelHsvFloat Def_VOCAB_PIXEL_HSV_FLOAT
Definition: ImageCopy.cpp:36
dest b
Definition: ImageCopy.cpp:91
Packed RGB pixel type.
Definition: Image.h:421
PixelInt Def_VOCAB_PIXEL_INT
Definition: ImageCopy.cpp:37
PixelFloat Def_VOCAB_PIXEL_MONO_FLOAT
Definition: ImageCopy.cpp:34
#define SPECIAL_COPY_BEGIN
Definition: ImageCopy.cpp:41
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:478
Floating point HSV pixel type.
Definition: Image.h:531
Packed RGBA pixel type.
Definition: Image.h:435
Integer RGB pixel type.
Definition: Image.h:518
dest a
Definition: ImageCopy.cpp:53
PixelRgb Def_VOCAB_PIXEL_RGB
Definition: ImageCopy.cpp:27
* dest
Definition: ImageCopy.cpp:72
PixelMono16 Def_VOCAB_PIXEL_MONO16
Definition: ImageCopy.cpp:26
The main, catch-all namespace for YARP.
YARP_END_PACK typedef float PixelFloat
Floating point pixel type.
Definition: Image.h:500
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:405
PixelBgr Def_VOCAB_PIXEL_BGR
Definition: ImageCopy.cpp:31
static void CopyPixel(const T1 *src, T2 *dest)
Definition: ImageCopy.cpp:20
Signed, packed RGB pixel type.
Definition: Image.h:492
void exit(int exit_code)
Portable wrapper for the exit() function.
Definition: Os.cpp:40
#define SPECIAL_COPY(id1, id2)
Definition: ImageCopy.cpp:42
PixelBgra Def_VOCAB_PIXEL_BGRA
Definition: ImageCopy.cpp:29
void copyPixels(const unsigned char *src, int id1, unsigned char *dest, int id2, int w, int h, int imageSize, int quantum1, int quantum2, bool topIsLow1, bool topIsLow2)
Definition: ImageCopy.cpp:586
PixelRgbSigned Def_VOCAB_PIXEL_RGB_SIGNED
Definition: ImageCopy.cpp:33
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:410
PixelRgba Def_VOCAB_PIXEL_RGBA
Definition: ImageCopy.cpp:28
PixelMonoSigned Def_VOCAB_PIXEL_MONO_SIGNED
Definition: ImageCopy.cpp:32
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:465
static SPECIAL_COPY_END void CopyPixels(const T1 *osrc, int q1, T2 *odest, int q2, int w, int h, bool flip)
Definition: ImageCopy.cpp:545
dest g
Definition: ImageCopy.cpp:91
PixelRgbFloat Def_VOCAB_PIXEL_RGB_FLOAT
Definition: ImageCopy.cpp:35
static int implemented_yet
Definition: ImageCopy.cpp:45
#define MAKE_2CASE(id1, id2)
Definition: ImageCopy.cpp:583
YARP_END_PACK typedef char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:486
Packed BGRA pixel type.
Definition: Image.h:450
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:415
#define MAKE_CASE(id1, id2)
Definition: ImageCopy.cpp:582
dest h
Definition: ImageCopy.cpp:63
SPECIAL_COPY_BEGIN dest r
Definition: ImageCopy.cpp:50
#define DBG
Definition: ImageCopy.cpp:16
int PAD_BYTES(int len, int pad)
computes the padding of YARP images.
Definition: Image.h:31
yAssert(implemented_yet==0)
dest v
Definition: ImageCopy.cpp:62
PixelMono Def_VOCAB_PIXEL_MONO
Definition: ImageCopy.cpp:25
Signal processing.
Definition: Image.h:20
#define HASH(id1, id2)
Definition: ImageCopy.cpp:580
memcpy(dstImage->imageData, srcImage->imageData, srcImage->imageSize)