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