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