YARP
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 
25 static const int implemented_yet = 1;
26 
27 /******************************************************************************/
28 
29 static inline void CopyPixel(const PixelMono* src, PixelRgb* dest)
30 {
31  dest->r = *src;
32  dest->g = *src;
33  dest->b = *src;
34 }
35 
36 static inline void CopyPixel(const PixelMono* src, PixelRgba* dest)
37 {
38  dest->r = *src;
39  dest->g = *src;
40  dest->b = *src;
41  dest->a = 255;
42 }
43 
44 static inline void CopyPixel(const PixelMono* src, PixelBgra* dest)
45 {
46  dest->r = *src;
47  dest->g = *src;
48  dest->b = *src;
49  dest->a = 255;
50 }
51 
52 static inline void CopyPixel(const PixelMono* src, PixelRgbInt* dest)
53 {
54  dest->r = *src;
55  dest->g = *src;
56  dest->b = *src;
57 }
58 
59 static inline void CopyPixel(const PixelMono* src, PixelBgr* dest)
60 {
61  dest->r = *src;
62  dest->g = *src;
63  dest->b = *src;
64 }
65 
66 static inline void CopyPixel(const PixelMono* src, PixelHsv* dest)
67 {
68  dest->v = *src;
69  dest->h = 0;
70  dest->s = 0;
71 }
72 
73 static inline void CopyPixel(const PixelMono* src, PixelRgbSigned* dest)
74 {
75  dest->r = *src;
76  dest->g = *src;
77  dest->b = *src;
78 }
79 
80 static inline void CopyPixel(const PixelMono* src, PixelRgbFloat* dest)
81 {
82  dest->r = *src;
83  dest->g = *src;
84  dest->b = *src;
85 }
86 
87 static inline void CopyPixel(const PixelMono* src, PixelHsvFloat* dest)
88 {
89  dest->v = *src;
90  dest->h = 0;
91  dest->s = 0;
92 }
93 
94 static inline void CopyPixel(const PixelMono* src, PixelMonoSigned* dest)
95 {
96  *dest = *src >> 1;
97 }
98 
99 static inline void CopyPixel(const PixelMono* src, PixelInt* dest)
100 {
101  *dest = *src;
102 }
103 
104 static inline void CopyPixel(const PixelMono* src, PixelMono16* dest)
105 {
106  *dest = *src;
107 }
108 
109 static inline void CopyPixel(const PixelMono* src, PixelFloat* dest)
110 {
111  *dest = *src;
112 }
113 
114 /******************************************************************************/
115 
116 static inline void CopyPixel(const PixelRgb* src, PixelMono* dest)
117 {
118  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
119 }
120 
121 static inline void CopyPixel(const PixelRgb* src, PixelMono16* dest)
122 {
123  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
124 }
125 
126 static inline void CopyPixel(const PixelRgb* src, PixelInt* dest)
127 {
128  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
129 }
130 
131 static inline void CopyPixel(const PixelRgb* src, PixelHsv* dest)
132 {
133  yAssert(implemented_yet == 0);
134 }
135 
136 static inline void CopyPixel(const PixelRgb* src, PixelMonoSigned* dest)
137 {
138  *dest = static_cast<char>((src->r + src->g + src->b)/3);
139 }
140 
141 static inline void CopyPixel(const PixelRgb* src, PixelRgbSigned* dest)
142 {
143  dest->r = src->r;
144  dest->g = src->g;
145  dest->b = src->b;
146 }
147 
148 static inline void CopyPixel(const PixelRgb* src, PixelRgba* dest)
149 {
150  dest->r = src->r;
151  dest->g = src->g;
152  dest->b = src->b;
153  dest->a = 255;
154 }
155 
156 static inline void CopyPixel(const PixelRgb* src, PixelBgra* dest)
157 {
158  dest->r = src->r;
159  dest->g = src->g;
160  dest->b = src->b;
161  dest->a = 255;
162 }
163 
164 static inline void CopyPixel(const PixelRgb* src, PixelRgbInt* dest)
165 {
166  dest->r = src->r;
167  dest->g = src->g;
168  dest->b = src->b;
169 }
170 
171 static inline void CopyPixel(const PixelRgb* src, PixelFloat* dest)
172 {
173  *dest = ((src->r + src->g + src->b)/3.0f);
174 }
175 
176 static inline void CopyPixel(const PixelRgb* src, PixelRgbFloat* dest)
177 {
178  dest->r = src->r;
179  dest->g = src->g;
180  dest->b = src->b;
181 }
182 
183 static inline void CopyPixel(const PixelRgb* src, PixelBgr* dest)
184 {
185  dest->r = src->r;
186  dest->g = src->g;
187  dest->b = src->b;
188 }
189 
190 static inline void CopyPixel(const PixelRgb* src, PixelHsvFloat* dest)
191 {
192  yAssert(implemented_yet == 0);
193 }
194 
195 /******************************************************************************/
196 
197 static inline void CopyPixel(const PixelHsv* src, PixelMono* dest)
198 {
199  yAssert(implemented_yet == 0);
200 }
201 
202 static inline void CopyPixel(const PixelHsv* src, PixelMono16* dest)
203 {
204  yAssert(implemented_yet == 0);
205 }
206 
207 static inline void CopyPixel(const PixelHsv* src, PixelRgb* dest)
208 {
209  yAssert(implemented_yet == 0);
210 }
211 
212 static inline void CopyPixel(const PixelHsv* src, PixelRgba* dest)
213 {
214  yAssert(implemented_yet == 0);
215 }
216 
217 static inline void CopyPixel(const PixelHsv* src, PixelBgra* dest)
218 {
219  yAssert(implemented_yet == 0);
220 }
221 
222 static inline void CopyPixel(const PixelHsv* src, PixelRgbInt* dest)
223 {
224  yAssert(implemented_yet == 0);
225 }
226 
227 static inline void CopyPixel(const PixelHsv* src, PixelBgr* dest)
228 {
229  yAssert(implemented_yet == 0);
230 }
231 
232 static inline void CopyPixel(const PixelHsv* src, PixelMonoSigned* dest)
233 {
234  yAssert(implemented_yet == 0);
235 }
236 
237 static inline void CopyPixel(const PixelHsv* src, PixelRgbSigned* dest)
238 {
239  yAssert(implemented_yet == 0);
240 }
241 
242 static inline void CopyPixel(const PixelHsv* src, PixelFloat* dest)
243 {
244  yAssert(implemented_yet == 0);
245 }
246 
247 static inline void CopyPixel(const PixelHsv* src, PixelRgbFloat* dest)
248 {
249  yAssert(implemented_yet == 0);
250 }
251 
252 static inline void CopyPixel(const PixelHsv* src, PixelHsvFloat* dest)
253 {
254  yAssert(implemented_yet == 0);
255 }
256 
257 static inline void CopyPixel(const PixelHsv* src, PixelInt* dest)
258 {
259  yAssert(implemented_yet == 0);
260 }
261 
262 /******************************************************************************/
263 
264 static inline void CopyPixel(const PixelBgr* src, PixelMono* dest)
265 {
266  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
267 }
268 
269 static inline void CopyPixel(const PixelBgr* src, PixelMono16* dest)
270 {
271  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
272 }
273 
274 static inline void CopyPixel(const PixelBgr* src, PixelInt* dest)
275 {
276  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
277 }
278 
279 static inline void CopyPixel(const PixelBgr* src, PixelHsv* dest)
280 {
281  yAssert(implemented_yet == 0);
282 }
283 
284 static inline void CopyPixel(const PixelBgr* src, PixelMonoSigned* dest)
285 {
286  *dest = static_cast<char>((src->r + src->g + src->b)/3);
287 }
288 
289 static inline void CopyPixel(const PixelBgr* src, PixelRgbSigned* dest)
290 {
291  dest->r = src->r;
292  dest->g = src->g;
293  dest->b = src->b;
294 }
295 
296 static inline void CopyPixel(const PixelBgr* src, PixelFloat* dest)
297 {
298  *dest = ((src->r + src->g + src->b)/3.0f);
299 }
300 
301 static inline void CopyPixel(const PixelBgr* src, PixelRgbFloat* dest)
302 {
303  dest->r = src->r;
304  dest->g = src->g;
305  dest->b = src->b;
306 }
307 
308 static inline void CopyPixel(const PixelBgr* src, PixelRgb* dest)
309 {
310  dest->r = src->r;
311  dest->g = src->g;
312  dest->b = src->b;
313 }
314 
315 static inline void CopyPixel(const PixelBgr* src, PixelRgba* dest)
316 {
317  dest->r = src->r;
318  dest->g = src->g;
319  dest->b = src->b;
320  dest->a = 255;
321 }
322 
323 static inline void CopyPixel(const PixelBgr* src, PixelBgra* dest)
324 {
325  dest->r = src->r;
326  dest->g = src->g;
327  dest->b = src->b;
328  dest->a = 255;
329 }
330 
331 static inline void CopyPixel(const PixelBgr* src, PixelRgbInt* dest)
332 {
333  dest->r = src->r;
334  dest->g = src->g;
335  dest->b = src->b;
336 }
337 
338 static inline void CopyPixel(const PixelBgr* src, PixelHsvFloat* dest)
339 {
340  yAssert(implemented_yet == 0);
341 }
342 
343 /******************************************************************************/
344 
345 static inline void CopyPixel(const PixelRgba* src, PixelMono* dest)
346 {
347  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
348 }
349 
350 static inline void CopyPixel(const PixelRgba* src, PixelMono16* dest)
351 {
352  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
353 }
354 
355 static inline void CopyPixel(const PixelRgba* src, PixelInt* dest)
356 {
357  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
358 }
359 
360 static inline void CopyPixel(const PixelRgba* src, PixelHsv* dest)
361 {
362  yAssert(implemented_yet == 0);
363 }
364 
365 static inline void CopyPixel(const PixelRgba* src, PixelMonoSigned* dest)
366 {
367  *dest = static_cast<char>((src->r + src->g + src->b)/3);
368 }
369 
370 static inline void CopyPixel(const PixelRgba* src, PixelRgbSigned* dest)
371 {
372  dest->r = src->r;
373  dest->g = src->g;
374  dest->b = src->b;
375 }
376 
377 static inline void CopyPixel(const PixelRgba* src, PixelFloat* dest)
378 {
379  *dest = ((src->r + src->g + src->b)/3.0f);
380 }
381 
382 static inline void CopyPixel(const PixelRgba* src, PixelRgbFloat* dest)
383 {
384  dest->r = src->r;
385  dest->g = src->g;
386  dest->b = src->b;
387 }
388 
389 static inline void CopyPixel(const PixelRgba* src, PixelRgb* dest)
390 {
391  dest->r = src->r;
392  dest->g = src->g;
393  dest->b = src->b;
394 }
395 
396 static inline void CopyPixel(const PixelRgba* src, PixelBgra* dest)
397 {
398  dest->r = src->r;
399  dest->g = src->g;
400  dest->b = src->b;
401  dest->a = src->a;
402 }
403 
404 static inline void CopyPixel(const PixelRgba* src, PixelBgr* dest)
405 {
406  dest->r = src->r;
407  dest->g = src->g;
408  dest->b = src->b;
409 }
410 
411 static inline void CopyPixel(const PixelRgba* src, PixelRgbInt* dest)
412 {
413  dest->r = src->r;
414  dest->g = src->g;
415  dest->b = src->b;
416 }
417 
418 static inline void CopyPixel(const PixelRgba* src, PixelHsvFloat* dest)
419 {
420  yAssert(implemented_yet == 0);
421 }
422 
423 /******************************************************************************/
424 
425 static inline void CopyPixel(const PixelBgra* src, PixelMono* dest)
426 {
427  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
428 }
429 
430 static inline void CopyPixel(const PixelBgra* src, PixelMono16* dest)
431 {
432  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
433 }
434 
435 static inline void CopyPixel(const PixelBgra* src, PixelInt* dest)
436 {
437  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
438 }
439 
440 static inline void CopyPixel(const PixelBgra* src, PixelHsv* dest)
441 {
442  yAssert(implemented_yet == 0);
443 }
444 
445 static inline void CopyPixel(const PixelBgra* src, PixelMonoSigned* dest)
446 {
447  *dest = static_cast<char>((src->r + src->g + src->b)/3);
448 }
449 
450 static inline void CopyPixel(const PixelBgra* src, PixelRgbSigned* dest)
451 {
452  dest->r = src->r;
453  dest->g = src->g;
454  dest->b = src->b;
455 }
456 
457 static inline void CopyPixel(const PixelBgra* src, PixelFloat* dest)
458 {
459  *dest = ((src->r + src->g + src->b)/3.0f);
460 }
461 
462 static inline void CopyPixel(const PixelBgra* src, PixelRgbFloat* dest)
463 {
464  dest->r = src->r;
465  dest->g = src->g;
466  dest->b = src->b;
467 }
468 
469 static inline void CopyPixel(const PixelBgra* src, PixelRgb* dest)
470 {
471  dest->r = src->r;
472  dest->g = src->g;
473  dest->b = src->b;
474 }
475 
476 static inline void CopyPixel(const PixelBgra* src, PixelRgba* dest)
477 {
478  dest->r = src->r;
479  dest->g = src->g;
480  dest->b = src->b;
481  dest->a = src->a;
482 }
483 
484 static inline void CopyPixel(const PixelBgra* src, PixelBgr* dest)
485 {
486  dest->r = src->r;
487  dest->g = src->g;
488  dest->b = src->b;
489 }
490 
491 static inline void CopyPixel(const PixelBgra* src, PixelRgbInt* dest)
492 {
493  dest->r = src->r;
494  dest->g = src->g;
495  dest->b = src->b;
496 }
497 
498 static inline void CopyPixel(const PixelBgra* src, PixelHsvFloat* dest)
499 {
500  yAssert(implemented_yet == 0);
501 }
502 
503 /******************************************************************************/
504 
505 static inline void CopyPixel(const PixelRgbInt* src, PixelMono* dest)
506 {
507  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
508 }
509 
510 static inline void CopyPixel(const PixelRgbInt* src, PixelMono16* dest)
511 {
512  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
513 }
514 
515 static inline void CopyPixel(const PixelRgbInt* src, PixelInt* dest)
516 {
517  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
518 }
519 
520 static inline void CopyPixel(const PixelRgbInt* src, PixelHsv* dest)
521 {
522  yAssert(implemented_yet == 0);
523 }
524 
525 static inline void CopyPixel(const PixelRgbInt* src, PixelMonoSigned* dest)
526 {
527  *dest = static_cast<char>((src->r + src->g + src->b)/3);
528 }
529 
530 static inline void CopyPixel(const PixelRgbInt* src, PixelRgbSigned* dest)
531 {
532  dest->r = static_cast<char>(src->r);
533  dest->g = static_cast<char>(src->g);
534  dest->b = static_cast<char>(src->b);
535 }
536 
537 static inline void CopyPixel(const PixelRgbInt* src, PixelFloat* dest)
538 {
539  *dest = ((src->r + src->g + src->b)/3.0f);
540 }
541 
542 static inline void CopyPixel(const PixelRgbInt* src, PixelRgbFloat* dest)
543 {
544  dest->r = static_cast<float>(static_cast<int>(src->r));
545  dest->g = static_cast<float>(static_cast<int>(src->g));
546  dest->b = static_cast<float>(static_cast<int>(src->b));
547 }
548 
549 static inline void CopyPixel(const PixelRgbInt* src, PixelRgb* dest)
550 {
551  dest->r = static_cast<unsigned char>(src->r);
552  dest->g = static_cast<unsigned char>(src->g);
553  dest->b = static_cast<unsigned char>(src->b);
554 }
555 
556 static inline void CopyPixel(const PixelRgbInt* src, PixelBgr* dest)
557 {
558  dest->r = static_cast<unsigned char>(src->r);
559  dest->g = static_cast<unsigned char>(src->g);
560  dest->b = static_cast<unsigned char>(src->b);
561 }
562 
563 static inline void CopyPixel(const PixelRgbInt* src, PixelRgba* dest)
564 {
565  dest->r = static_cast<unsigned char>(src->r);
566  dest->g = static_cast<unsigned char>(src->g);
567  dest->b = static_cast<unsigned char>(src->b);
568  dest->a = 255;
569 }
570 
571 static inline void CopyPixel(const PixelRgbInt* src, PixelBgra* dest)
572 {
573  dest->r = static_cast<unsigned char>(src->r);
574  dest->g = static_cast<unsigned char>(src->g);
575  dest->b = static_cast<unsigned char>(src->b);
576  dest->a = 255;
577 }
578 
579 static inline void CopyPixel(const PixelRgbInt* src, PixelHsvFloat* dest)
580 {
581  yAssert(implemented_yet == 0);
582 }
583 
584 /******************************************************************************/
585 
586 static inline void CopyPixel(const PixelMonoSigned* src, PixelRgb* dest)
587 {
588  dest->r = *src;
589  dest->g = *src;
590  dest->b = *src;
591 }
592 
593 static inline void CopyPixel(const PixelMonoSigned* src, PixelRgba* dest)
594 {
595  dest->r = *src;
596  dest->g = *src;
597  dest->b = *src;
598  dest->a = 255;
599 }
600 
601 static inline void CopyPixel(const PixelMonoSigned* src, PixelBgra* dest)
602 {
603  dest->r = *src;
604  dest->g = *src;
605  dest->b = *src;
606  dest->a = 255;
607 }
608 
609 static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbInt* dest)
610 {
611  dest->r = *src;
612  dest->g = *src;
613  dest->b = *src;
614 }
615 
616 static inline void CopyPixel(const PixelMonoSigned* src, PixelBgr* dest)
617 {
618  dest->r = *src;
619  dest->g = *src;
620  dest->b = *src;
621 }
622 
623 static inline void CopyPixel(const PixelMonoSigned* src, PixelHsv* dest)
624 {
625  dest->v = *src;
626  dest->h = 0;
627  dest->s = 0;
628 }
629 
630 static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbSigned* dest)
631 {
632  dest->r = *src;
633  dest->g = *src;
634  dest->b = *src;
635 }
636 
637 static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbFloat* dest)
638 {
639  dest->r = *src;
640  dest->g = *src;
641  dest->b = *src;
642 }
643 
644 static inline void CopyPixel(const PixelMonoSigned* src, PixelHsvFloat* dest)
645 {
646  dest->v = *src;
647  dest->h = 0;
648  dest->s = 0;
649 }
650 
651 static inline void CopyPixel(const PixelMonoSigned* src, PixelMono* dest)
652 {
653  *dest = *src + 128;
654 }
655 
656 static inline void CopyPixel(const PixelMonoSigned* src, PixelInt* dest)
657 {
658  *dest = *src;
659 }
660 
661 static inline void CopyPixel(const PixelMonoSigned* src, PixelMono16* dest)
662 {
663  *dest = static_cast<yarp::sig::PixelMono16>(*src);
664 }
665 
666 /******************************************************************************/
667 
668 static inline void CopyPixel(const PixelRgbSigned* src, PixelMono* dest)
669 {
670  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
671 }
672 
673 static inline void CopyPixel(const PixelRgbSigned* src, PixelMono16* dest)
674 {
675  *dest = static_cast<PixelMono16>((src->r + src->g + src->b)/3);
676 }
677 
678 static inline void CopyPixel(const PixelRgbSigned* src, PixelInt* dest)
679 {
680  *dest = static_cast<int>((src->r + src->g + src->b)/3);
681 }
682 
683 static inline void CopyPixel(const PixelRgbSigned* src, PixelHsv* dest)
684 {
685  yAssert(implemented_yet == 0);
686 }
687 
688 static inline void CopyPixel(const PixelRgbSigned* src, PixelMonoSigned* dest)
689 {
690  *dest = static_cast<char>((src->r + src->g + src->b)/3);
691 }
692 
693 static inline void CopyPixel(const PixelRgbSigned* src, PixelRgb* dest)
694 {
695  dest->r = src->r;
696  dest->g = src->g;
697  dest->b = src->b;
698 }
699 
700 static inline void CopyPixel(const PixelRgbSigned* src, PixelRgba* dest)
701 {
702  dest->r = src->r;
703  dest->g = src->g;
704  dest->b = src->b;
705  dest->a = 255;
706 }
707 
708 static inline void CopyPixel(const PixelRgbSigned* src, PixelBgra* dest)
709 {
710  dest->r = src->r;
711  dest->g = src->g;
712  dest->b = src->b;
713  dest->a = 255;
714 }
715 
716 static inline void CopyPixel(const PixelRgbSigned* src, PixelRgbInt* dest)
717 {
718  dest->r = src->r;
719  dest->g = src->g;
720  dest->b = src->b;
721 }
722 
723 static inline void CopyPixel(const PixelRgbSigned* src, PixelBgr* dest)
724 {
725  dest->r = src->r;
726  dest->g = src->g;
727  dest->b = src->b;
728 }
729 
730 static inline void CopyPixel(const PixelRgbSigned* src, PixelFloat* dest)
731 {
732  *dest = ((src->r + src->g + src->b)/3.0f);
733 }
734 
735 static inline void CopyPixel(const PixelRgbSigned* src, PixelRgbFloat* dest)
736 {
737  dest->r = src->r;
738  dest->g = src->g;
739  dest->b = src->b;
740 }
741 
742 static inline void CopyPixel(const PixelRgbSigned* src, PixelHsvFloat* dest)
743 {
744  yAssert(implemented_yet == 0);
745 }
746 
747 /******************************************************************************/
748 
749 static inline void CopyPixel(const PixelFloat* src, PixelMono* dest)
750 {
751  *dest = static_cast<unsigned char>(*src);
752 }
753 
754 static inline void CopyPixel(const PixelFloat* src, PixelMono16* dest)
755 {
756  *dest = static_cast<yarp::sig::PixelMono16>(*src);
757 }
758 
759 static inline void CopyPixel(const PixelFloat* src, PixelInt* dest)
760 {
761  *dest = static_cast<unsigned char>(*src);
762 }
763 
764 static inline void CopyPixel(const PixelFloat* src, PixelMonoSigned* dest)
765 {
766  *dest = static_cast<char>(*src);
767 }
768 
769 static inline void CopyPixel(const PixelFloat* src, PixelRgb* dest)
770 {
771  dest->r = static_cast<unsigned char>(*src);
772  dest->g = static_cast<unsigned char>(*src);
773  dest->b = static_cast<unsigned char>(*src);
774 }
775 
776 static inline void CopyPixel(const PixelFloat* src, PixelRgba* dest)
777 {
778  dest->r = static_cast<unsigned char>(*src);
779  dest->g = static_cast<unsigned char>(*src);
780  dest->b = static_cast<unsigned char>(*src);
781  dest->a = 255;
782 }
783 
784 static inline void CopyPixel(const PixelFloat* src, PixelBgra* dest)
785 {
786  dest->r = static_cast<unsigned char>(*src);
787  dest->g = static_cast<unsigned char>(*src);
788  dest->b = static_cast<unsigned char>(*src);
789  dest->a = 255;
790 }
791 
792 static inline void CopyPixel(const PixelFloat* src, PixelRgbInt* dest)
793 {
794  dest->r = static_cast<int>(*src);
795  dest->g = static_cast<int>(*src);
796  dest->b = static_cast<int>(*src);
797 }
798 
799 static inline void CopyPixel(const PixelFloat* src, PixelBgr* dest)
800 {
801  dest->r = static_cast<unsigned char>(*src);
802  dest->g = static_cast<unsigned char>(*src);
803  dest->b = static_cast<unsigned char>(*src);
804 }
805 
806 static inline void CopyPixel(const PixelFloat* src, PixelHsv* dest)
807 {
808  dest->v = static_cast<unsigned char>(*src);
809  dest->h = 0;
810  dest->s = 0;
811 }
812 
813 static inline void CopyPixel(const PixelFloat* src, PixelRgbSigned* dest)
814 {
815  dest->r = static_cast<signed char>(*src);
816  dest->g = static_cast<signed char>(*src);
817  dest->b = static_cast<signed char>(*src);
818 }
819 
820 static inline void CopyPixel(const PixelFloat* src, PixelRgbFloat* dest)
821 {
822  dest->r = *src;
823  dest->g = *src;
824  dest->b = *src;
825 }
826 
827 static inline void CopyPixel(const PixelFloat* src, PixelHsvFloat* dest)
828 {
829  dest->v = *src;
830  dest->h = 0;
831  dest->s = 0;
832 }
833 
834 /******************************************************************************/
835 
836 static inline void CopyPixel(const PixelRgbFloat* src, PixelMono* dest)
837 {
838  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
839 }
840 
841 static inline void CopyPixel(const PixelRgbFloat* src, PixelInt* dest)
842 {
843  *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
844 }
845 
846 static inline void CopyPixel(const PixelRgbFloat* src, PixelMono16* dest)
847 {
848  *dest = static_cast<yarp::sig::PixelMono16>((src->r + src->g + src->b)/3);
849 }
850 
851 static inline void CopyPixel(const PixelRgbFloat* src, PixelHsv* dest)
852 {
853  yAssert(implemented_yet == 0);
854 }
855 
856 static inline void CopyPixel(const PixelRgbFloat* src, PixelMonoSigned* dest)
857 {
858  *dest = static_cast<char>((src->r + src->g + src->b)/3);
859 }
860 
861 static inline void CopyPixel(const PixelRgbFloat* src, PixelRgb* dest)
862 {
863  dest->r = static_cast<unsigned char>(src->r);
864  dest->g = static_cast<unsigned char>(src->g);
865  dest->b = static_cast<unsigned char>(src->b);
866 }
867 
868 static inline void CopyPixel(const PixelRgbFloat* src, PixelRgba* dest)
869 {
870  dest->r = static_cast<unsigned char>(src->r);
871  dest->g = static_cast<unsigned char>(src->g);
872  dest->b = static_cast<unsigned char>(src->b);
873  dest->a = 255;
874 }
875 
876 static inline void CopyPixel(const PixelRgbFloat* src, PixelBgra* dest)
877 {
878  dest->r = static_cast<unsigned char>(src->r);
879  dest->g = static_cast<unsigned char>(src->g);
880  dest->b = static_cast<unsigned char>(src->b);
881  dest->a = 255;
882 }
883 
884 static inline void CopyPixel(const PixelRgbFloat* src, PixelRgbInt* dest)
885 {
886  dest->r = static_cast<int>(src->r);
887  dest->g = static_cast<int>(src->g);
888  dest->b = static_cast<int>(src->b);
889 }
890 
891 static inline void CopyPixel(const PixelRgbFloat* src, PixelBgr* dest)
892 {
893  dest->r = static_cast<unsigned char>(src->r);
894  dest->g = static_cast<unsigned char>(src->g);
895  dest->b = static_cast<unsigned char>(src->b);
896 }
897 
898 static inline void CopyPixel(const PixelRgbFloat* src, PixelFloat* dest)
899 {
900  *dest = ((src->r + src->g + src->b)/3);
901 }
902 
903 static inline void CopyPixel(const PixelRgbFloat* src, PixelRgbSigned* dest)
904 {
905  dest->r = static_cast<signed char>(src->r);
906  dest->g = static_cast<signed char>(src->g);
907  dest->b = static_cast<signed char>(src->b);
908 }
909 
910 static inline void CopyPixel(const PixelRgbFloat* src, PixelHsvFloat* dest)
911 {
912  yAssert(implemented_yet == 0);
913 }
914 
915 /******************************************************************************/
916 
917 static inline void CopyPixel(const PixelHsvFloat* src, PixelMono* dest)
918 {
919  yAssert(implemented_yet == 0);
920 }
921 
922 static inline void CopyPixel(const PixelHsvFloat* src, PixelMono16* dest)
923 {
924  yAssert(implemented_yet == 0);
925 }
926 
927 static inline void CopyPixel(const PixelHsvFloat* src, PixelRgb* dest)
928 {
929  yAssert(implemented_yet == 0);
930 }
931 
932 static inline void CopyPixel(const PixelHsvFloat* src, PixelBgr* dest)
933 {
934  yAssert(implemented_yet == 0);
935 }
936 
937 static inline void CopyPixel(const PixelHsvFloat* src, PixelRgba* dest)
938 {
939  yAssert(implemented_yet == 0);
940 }
941 
942 static inline void CopyPixel(const PixelHsvFloat* src, PixelBgra* dest)
943 {
944  yAssert(implemented_yet == 0);
945 }
946 
947 static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbInt* dest)
948 {
949  yAssert(implemented_yet == 0);
950 }
951 
952 static inline void CopyPixel(const PixelHsvFloat* src, PixelMonoSigned* dest)
953 {
954  yAssert(implemented_yet == 0);
955 }
956 
957 static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbSigned* dest)
958 {
959  yAssert(implemented_yet == 0);
960 }
961 
962 static inline void CopyPixel(const PixelHsvFloat* src, PixelFloat* dest)
963 {
964  yAssert(implemented_yet == 0);
965 }
966 
967 static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbFloat* dest)
968 {
969  yAssert(implemented_yet == 0);
970 }
971 
972 static inline void CopyPixel(const PixelHsvFloat* src, PixelHsv* dest)
973 {
974  yAssert(implemented_yet == 0);
975 }
976 
977 static inline void CopyPixel(const PixelHsvFloat* src, PixelInt* dest)
978 {
979  yAssert(implemented_yet == 0);
980 }
981 
982 /******************************************************************************/
983 
984 static inline void CopyPixel(const PixelInt* src, PixelRgb* dest)
985 {
986  dest->r = static_cast<char>(*src);
987  dest->g = static_cast<char>(*src);
988  dest->b = static_cast<char>(*src);
989 }
990 
991 static inline void CopyPixel(const PixelInt* src, PixelRgba* dest)
992 {
993  dest->r = static_cast<char>(*src);
994  dest->g = static_cast<char>(*src);
995  dest->b = static_cast<char>(*src);
996  dest->a = 255;
997 }
998 
999 static inline void CopyPixel(const PixelInt* src, PixelBgra* dest)
1000 {
1001  dest->r = static_cast<char>(*src);
1002  dest->g = static_cast<char>(*src);
1003  dest->b = static_cast<char>(*src);
1004  dest->a = 255;
1005 }
1006 
1007 static inline void CopyPixel(const PixelInt* src, PixelRgbInt* dest)
1008 {
1009  dest->r = *src;
1010  dest->g = *src;
1011  dest->b = *src;
1012 }
1013 
1014 static inline void CopyPixel(const PixelInt* src, PixelBgr* dest)
1015 {
1016  dest->r = static_cast<char>(*src);
1017  dest->g = static_cast<char>(*src);
1018  dest->b = static_cast<char>(*src);
1019 }
1020 
1021 static inline void CopyPixel(const PixelInt* src, PixelHsv* dest)
1022 {
1023  dest->v = static_cast<yarp::sig::PixelMono>(*src);
1024  dest->h = dest->s = 0;
1025 }
1026 
1027 static inline void CopyPixel(const PixelInt* src, PixelRgbSigned* dest)
1028 {
1029  dest->r = static_cast<yarp::sig::PixelMono>(*src);
1030  dest->g = static_cast<yarp::sig::PixelMono>(*src);
1031  dest->b = static_cast<yarp::sig::PixelMono>(*src);
1032 }
1033 
1034 static inline void CopyPixel(const PixelInt* src, PixelFloat* dest)
1035 {
1036  *dest = static_cast<float>(*src);
1037 }
1038 
1039 static inline void CopyPixel(const PixelInt* src, PixelRgbFloat* dest)
1040 {
1041  dest->r = static_cast<float>(*src);
1042  dest->g = static_cast<float>(*src);
1043  dest->b = static_cast<float>(*src);
1044 }
1045 
1046 static inline void CopyPixel(const PixelInt* src, PixelHsvFloat* dest)
1047 {
1048  dest->v = float(*src);
1049  dest->h = 0;
1050  dest->s = 0;
1051 }
1052 
1053 static inline void CopyPixel(const PixelInt* src, PixelMonoSigned* dest)
1054 {
1055  *dest = static_cast<char>(*src >> 1);
1056 }
1057 
1058 static inline void CopyPixel(const PixelInt* src, PixelMono* dest)
1059 {
1060  *dest = static_cast<yarp::sig::PixelMono>(*src);
1061 }
1062 
1063 static inline void CopyPixel(const PixelInt* src, PixelMono16* dest)
1064 {
1065  *dest = static_cast<yarp::sig::PixelMono16>(*src);
1066 }
1067 
1068 /******************************************************************************/
1069 
1070 static inline void CopyPixel(const PixelMono16* src, PixelRgb* dest)
1071 {
1072  dest->r = static_cast<char>(*src);
1073  dest->g = static_cast<char>(*src);
1074  dest->b = static_cast<char>(*src);
1075 }
1076 
1077 static inline void CopyPixel(const PixelMono16* src, PixelRgba* dest)
1078 {
1079  dest->r = static_cast<char>(*src);
1080  dest->g = static_cast<char>(*src);
1081  dest->b = static_cast<char>(*src);
1082  dest->a = 255;
1083 }
1084 
1085 static inline void CopyPixel(const PixelMono16* src, PixelBgra* dest)
1086 {
1087  dest->r = static_cast<char>(*src);
1088  dest->g = static_cast<char>(*src);
1089  dest->b = static_cast<char>(*src);
1090  dest->a = 255;
1091 }
1092 
1093 static inline void CopyPixel(const PixelMono16* src, PixelRgbInt* dest)
1094 {
1095  dest->r = static_cast<int>(static_cast<unsigned>(*src));
1096  dest->g = static_cast<int>(static_cast<unsigned>(*src));
1097  dest->b = static_cast<int>(static_cast<unsigned>(*src));
1098 }
1099 
1100 static inline void CopyPixel(const PixelMono16* src, PixelInt* dest)
1101 {
1102  *dest = static_cast<int>(static_cast<unsigned>(*src));
1103 }
1104 
1105 static inline void CopyPixel(const PixelMono16* src, PixelBgr* dest)
1106 {
1107  dest->r = static_cast<char>(*src);
1108  dest->g = static_cast<char>(*src);
1109  dest->b = static_cast<char>(*src);
1110 }
1111 
1112 static inline void CopyPixel(const PixelMono16* src, PixelHsv* dest)
1113 {
1114  dest->v = static_cast<yarp::sig::PixelMono>(*src);
1115  dest->h = 0;
1116  dest->s = 0;
1117 }
1118 
1119 static inline void CopyPixel(const PixelMono16* src, PixelRgbSigned* dest)
1120 {
1121  dest->r = static_cast<yarp::sig::PixelMono>(*src);
1122  dest->g = static_cast<yarp::sig::PixelMono>(*src);
1123  dest->b = static_cast<yarp::sig::PixelMono>(*src);
1124 }
1125 
1126 static inline void CopyPixel(const PixelMono16* src, PixelFloat* dest)
1127 {
1128  *dest = static_cast<float>(*src);
1129 }
1130 
1131 static inline void CopyPixel(const PixelMono16* src, PixelRgbFloat* dest)
1132 {
1133  dest->r = static_cast<float>(*src);
1134  dest->g = static_cast<float>(*src);
1135  dest->b = static_cast<float>(*src);
1136 }
1137 
1138 static inline void CopyPixel(const PixelMono16* src, PixelHsvFloat* dest)
1139 {
1140  dest->v = static_cast<float>(*src);
1141  dest->h = 0;
1142  dest->s = 0;
1143 }
1144 
1145 static inline void CopyPixel(const PixelMono16* src, PixelMonoSigned* dest)
1146 {
1147  *dest = static_cast<char>(*src >> 1);
1148 }
1149 
1150 static inline void CopyPixel(const PixelMono16* src, PixelMono* dest)
1151 {
1152  *dest = static_cast<yarp::sig::PixelMono>(*src);
1153 }
1154 
1155 static inline void CopyPixel(const PixelInt* src, PixelInt* dest)
1156 {
1157  *dest = *src;
1158 }
1159 
1160 /******************************************************************************/
1161 
1162 
1163 //static inline int PAD_BYTES (int len, int pad)
1164 //{
1165 // const int rem = len % pad;
1166 // return (rem != 0) ? (pad - rem) : rem;
1167 //}
1168 
1171 template <class T1, class T2>
1172 static void CopyPixels(const T1 *osrc, int q1, T2 *odest, int q2,
1173  int w, int h,
1174  bool flip)
1175 {
1176  const T1 *src = osrc;
1177  T2 *dest = odest;
1178  const int p1 = PAD_BYTES (w * sizeof(T1), q1);
1179  const int p2 = PAD_BYTES (w * sizeof(T2), q2);
1180  //const int step1 = w*sizeof(T1) + p1;
1181  const int step2 = w*sizeof(T2) + p2;
1182  DBG printf("q1 %d q2 %d (%dx%d) inc %d %d\n", q1, q2, w, h, p1, p2);
1183 
1184  if (flip) {
1185  odest = reinterpret_cast<T2*>(((char *)odest) + step2*(h-1));
1186  dest = odest;
1187  }
1188 
1189  for (int i=0; i<h; i++) {
1190  DBG printf("x,y = %d,%d\n", 0,i);
1191  for (int j = 0; j < w; j++) {
1192  CopyPixel(src,dest);
1193  src++;
1194  dest++;
1195  }
1196 
1197  src = reinterpret_cast<const T1*>(((char *)src) + p1);
1198  odest = reinterpret_cast<T2*>(((char *)odest) + step2*(flip?-1:1));
1199  dest = odest;
1200  }
1201 }
1202 
1203 
1218 
1219 #define HASH(id1, id2) ((int)(((int)(id1%65537))*11 + ((long int)(id2))))
1220 #define HANDLE_CASE(len, x1, T1, q1, o1, x2, T2, q2, o2) CopyPixels(reinterpret_cast<const T1*>(x1), q1, reinterpret_cast<T2*>(x2), q2, w, h, o1!=o2);
1221 #define MAKE_CASE(id1, id2) case HASH(id1, id2): HANDLE_CASE(len, src, Def_##id1, quantum1, topIsLow1, dest, Def_##id2, quantum2, topIsLow2); break;
1222 
1223 // More elegant ways to do this, but needs to be efficient at pixel level
1224 void Image::copyPixels(const unsigned char *src, int id1,
1225  char unsigned *dest, int id2, int w, int h,
1226  int imageSize, int quantum1, int quantum2,
1227  bool topIsLow1, bool topIsLow2)
1228 {
1229  DBG printf("copyPixels...\n");
1230 
1231  if (id1==id2&&quantum1==quantum2&&topIsLow1==topIsLow2) {
1232  memcpy(dest,src,imageSize);
1233  return;
1234  }
1235 
1236 
1237  switch(HASH(id1,id2)) {
1238  // Macros rely on len, x1, x2 variable names
1239 
1240  // Each MAKE_CASE line here expands to something like this:
1241  //
1242  // case HASH(VOCAB_PIXEL_MONO, VOCAB_PIXEL_RGB):
1243  // CopyPixels(reinterpret_cast<const PixelMono*>(src), quantum1,
1244  // reinterpret_cast<PixelRgb*>(dest), quantum2,
1245  // w, h, topIsLow1!=topIsLow2);
1246  // break;
1247 
1262 
1277 
1292 
1307 
1322 
1337 
1352 
1367 
1382 
1397 
1412 
1427 
1442 
1457 
1458  default:
1459  printf("*** Tried to copy type %d to %d\n", id1, id2);
1460  std::exit(1);
1461  break;
1462  }
1463 
1464  DBG printf("... done copyPixels\n");
1465 }
Floating point RGB pixel type.
Definition: Image.h:511
PixelRgbInt Def_VOCAB_PIXEL_RGB_INT
Definition: ImageCopy.cpp:1217
PixelHsv Def_VOCAB_PIXEL_HSV
Definition: ImageCopy.cpp:1209
PixelHsvFloat Def_VOCAB_PIXEL_HSV_FLOAT
Definition: ImageCopy.cpp:1215
unsigned char r
Definition: Image.h:457
Packed RGB pixel type.
Definition: Image.h:426
PixelInt Def_VOCAB_PIXEL_INT
Definition: ImageCopy.cpp:1216
static void CopyPixels(const T1 *osrc, int q1, T2 *odest, int q2, int w, int h, bool flip)
Definition: ImageCopy.cpp:1172
yarp::os::NetInt32 b
Definition: Image.h:524
PixelFloat Def_VOCAB_PIXEL_MONO_FLOAT
Definition: ImageCopy.cpp:1213
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:483
Floating point HSV pixel type.
Definition: Image.h:536
Packed RGBA pixel type.
Definition: Image.h:440
unsigned char s
Definition: Image.h:484
unsigned char g
Definition: Image.h:428
Integer RGB pixel type.
Definition: Image.h:523
yarp::os::NetInt32 r
Definition: Image.h:524
PixelRgb Def_VOCAB_PIXEL_RGB
Definition: ImageCopy.cpp:1206
PixelMono16 Def_VOCAB_PIXEL_MONO16
Definition: ImageCopy.cpp:1205
YARP_END_PACK typedef float PixelFloat
Floating point pixel type.
Definition: Image.h:505
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:410
unsigned char g
Definition: Image.h:442
PixelBgr Def_VOCAB_PIXEL_BGR
Definition: ImageCopy.cpp:1210
static void CopyPixel(const T1 *src, T2 *dest)
Definition: ImageCopy.cpp:20
unsigned char a
Definition: Image.h:457
unsigned char r
Definition: Image.h:442
Signed, packed RGB pixel type.
Definition: Image.h:497
yarp::os::NetInt32 g
Definition: Image.h:524
unsigned char a
Definition: Image.h:442
void exit(int exit_code)
Portable wrapper for the exit() function.
Definition: Os.cpp:40
unsigned char v
Definition: Image.h:484
PixelBgra Def_VOCAB_PIXEL_BGRA
Definition: ImageCopy.cpp:1208
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:1224
unsigned char b
Definition: Image.h:472
PixelRgbSigned Def_VOCAB_PIXEL_RGB_SIGNED
Definition: ImageCopy.cpp:1212
unsigned char b
Definition: Image.h:457
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:415
PixelRgba Def_VOCAB_PIXEL_RGBA
Definition: ImageCopy.cpp:1207
PixelMonoSigned Def_VOCAB_PIXEL_MONO_SIGNED
Definition: ImageCopy.cpp:1211
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:470
PixelRgbFloat Def_VOCAB_PIXEL_RGB_FLOAT
Definition: ImageCopy.cpp:1214
static const int implemented_yet
Definition: ImageCopy.cpp:25
#define yAssert(x)
Definition: Log.h:104
unsigned char g
Definition: Image.h:472
unsigned char r
Definition: Image.h:472
unsigned char b
Definition: Image.h:442
YARP_END_PACK typedef char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:491
Packed BGRA pixel type.
Definition: Image.h:455
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:420
#define MAKE_CASE(id1, id2)
Definition: ImageCopy.cpp:1221
unsigned char g
Definition: Image.h:457
unsigned char h
Definition: Image.h:484
#define DBG
Definition: ImageCopy.cpp:16
int PAD_BYTES(int len, int pad)
computes the padding of YARP images.
Definition: Image.h:32
PixelMono Def_VOCAB_PIXEL_MONO
Definition: ImageCopy.cpp:1204
unsigned char b
Definition: Image.h:428
Signal processing.
Definition: Image.h:21
unsigned char r
Definition: Image.h:428
#define HASH(id1, id2)
Definition: ImageCopy.cpp:1219
memcpy(dstImage->imageData, srcImage->imageData, srcImage->imageSize)