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>
11
12#include <cstring>
13#include <cstdio>
14
15using namespace yarp::sig;
16
17#define DBG if(0)
18
19// Default copy mechanism
20template <class T1, class T2>
21static inline void CopyPixel(const T1 *src, T2 *dest)
22{
23 *dest = *src;
24}
25
26/******************************************************************************/
27
28static inline void CopyPixel(const PixelMono* src, PixelRgb* dest)
29{
30 dest->r = *src;
31 dest->g = *src;
32 dest->b = *src;
33}
34
35static 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
43static 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
51static inline void CopyPixel(const PixelMono* src, PixelRgbInt* dest)
52{
53 dest->r = *src;
54 dest->g = *src;
55 dest->b = *src;
56}
57
58static inline void CopyPixel(const PixelMono* src, PixelBgr* dest)
59{
60 dest->r = *src;
61 dest->g = *src;
62 dest->b = *src;
63}
64
65static inline void CopyPixel(const PixelMono* src, PixelHsv* dest)
66{
67 dest->v = *src;
68 dest->h = 0;
69 dest->s = 0;
70}
71
72static inline void CopyPixel(const PixelMono* src, PixelRgbSigned* dest)
73{
74 dest->r = *src;
75 dest->g = *src;
76 dest->b = *src;
77}
78
79static inline void CopyPixel(const PixelMono* src, PixelRgbFloat* dest)
80{
81 dest->r = *src;
82 dest->g = *src;
83 dest->b = *src;
84}
85
86static inline void CopyPixel(const PixelMono* src, PixelHsvFloat* dest)
87{
88 dest->v = *src;
89 dest->h = 0;
90 dest->s = 0;
91}
92
93static inline void CopyPixel(const PixelMono* src, PixelMonoSigned* dest)
94{
95 *dest = *src >> 1;
96}
97
98static inline void CopyPixel(const PixelMono* src, PixelInt* dest)
99{
100 *dest = *src;
101}
102
103static inline void CopyPixel(const PixelMono* src, PixelMono16* dest)
104{
105 *dest = *src;
106}
107
108static inline void CopyPixel(const PixelMono* src, PixelFloat* dest)
109{
110 *dest = *src;
111}
112
113/******************************************************************************/
114
115static inline void CopyPixel(const PixelRgb* src, PixelMono* dest)
116{
117 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
118}
119
120static 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
125static inline void CopyPixel(const PixelRgb* src, PixelInt* dest)
126{
127 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
128}
129
130static 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
137static inline void CopyPixel(const PixelRgb* src, PixelMonoSigned* dest)
138{
139 *dest = static_cast<char>((src->r + src->g + src->b)/3);
140}
141
142static 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
149static 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
157static 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
165static 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
172static inline void CopyPixel(const PixelRgb* src, PixelFloat* dest)
173{
174 *dest = ((src->r + src->g + src->b)/3.0f);
175}
176
177static 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
184static 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
191static 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
200static 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
207static 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
214static 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
221static 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
228static 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
235static 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
242static 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
249static 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
256static 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
263static 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
270static 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
277static 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
284static 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
293static inline void CopyPixel(const PixelBgr* src, PixelMono* dest)
294{
295 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
296}
297
298static 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
303static inline void CopyPixel(const PixelBgr* src, PixelInt* dest)
304{
305 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
306}
307
308static inline void CopyPixel(const PixelBgr* src, PixelHsv* dest)
309{
310 yAssert(false); // Not implemented yet
311}
312
313static inline void CopyPixel(const PixelBgr* src, PixelMonoSigned* dest)
314{
315 *dest = static_cast<char>((src->r + src->g + src->b)/3);
316}
317
318static 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
325static inline void CopyPixel(const PixelBgr* src, PixelFloat* dest)
326{
327 *dest = ((src->r + src->g + src->b)/3.0f);
328}
329
330static 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
337static 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
344static 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
352static 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
360static 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
367static inline void CopyPixel(const PixelBgr* src, PixelHsvFloat* dest)
368{
369 yAssert(false); // Not implemented yet
370}
371
372/******************************************************************************/
373
374static inline void CopyPixel(const PixelRgba* src, PixelMono* dest)
375{
376 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
377}
378
379static 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
384static inline void CopyPixel(const PixelRgba* src, PixelInt* dest)
385{
386 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
387}
388
389static inline void CopyPixel(const PixelRgba* src, PixelHsv* dest)
390{
391 yAssert(false); // Not implemented yet
392}
393
394static inline void CopyPixel(const PixelRgba* src, PixelMonoSigned* dest)
395{
396 *dest = static_cast<char>((src->r + src->g + src->b)/3);
397}
398
399static 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
406static inline void CopyPixel(const PixelRgba* src, PixelFloat* dest)
407{
408 *dest = ((src->r + src->g + src->b)/3.0f);
409}
410
411static 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
418static 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
425static 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
433static 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
440static 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
447static inline void CopyPixel(const PixelRgba* src, PixelHsvFloat* dest)
448{
449 yAssert(false); // Not implemented yet
450}
451
452/******************************************************************************/
453
454static inline void CopyPixel(const PixelBgra* src, PixelMono* dest)
455{
456 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
457}
458
459static 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
464static inline void CopyPixel(const PixelBgra* src, PixelInt* dest)
465{
466 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
467}
468
469static inline void CopyPixel(const PixelBgra* src, PixelHsv* dest)
470{
471 yAssert(false); // Not implemented yet
472}
473
474static inline void CopyPixel(const PixelBgra* src, PixelMonoSigned* dest)
475{
476 *dest = static_cast<char>((src->r + src->g + src->b)/3);
477}
478
479static 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
486static inline void CopyPixel(const PixelBgra* src, PixelFloat* dest)
487{
488 *dest = ((src->r + src->g + src->b)/3.0f);
489}
490
491static 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
498static 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
505static 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
513static 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
520static 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
527static inline void CopyPixel(const PixelBgra* src, PixelHsvFloat* dest)
528{
529 yAssert(false); // Not implemented yet
530}
531
532/******************************************************************************/
533
534static inline void CopyPixel(const PixelRgbInt* src, PixelMono* dest)
535{
536 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
537}
538
539static 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
544static inline void CopyPixel(const PixelRgbInt* src, PixelInt* dest)
545{
546 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
547}
548
549static inline void CopyPixel(const PixelRgbInt* src, PixelHsv* dest)
550{
551 yAssert(false); // Not implemented yet
552}
553
554static inline void CopyPixel(const PixelRgbInt* src, PixelMonoSigned* dest)
555{
556 *dest = static_cast<char>((src->r + src->g + src->b)/3);
557}
558
559static 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
566static inline void CopyPixel(const PixelRgbInt* src, PixelFloat* dest)
567{
568 *dest = ((src->r + src->g + src->b)/3.0f);
569}
570
571static 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
578static 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
585static 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
592static 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
600static 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
608static inline void CopyPixel(const PixelRgbInt* src, PixelHsvFloat* dest)
609{
610 yAssert(false); // Not implemented yet
611}
612
613/******************************************************************************/
614
615static inline void CopyPixel(const PixelMonoSigned* src, PixelRgb* dest)
616{
617 dest->r = *src;
618 dest->g = *src;
619 dest->b = *src;
620}
621
622static 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
630static 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
638static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbInt* dest)
639{
640 dest->r = *src;
641 dest->g = *src;
642 dest->b = *src;
643}
644
645static inline void CopyPixel(const PixelMonoSigned* src, PixelBgr* dest)
646{
647 dest->r = *src;
648 dest->g = *src;
649 dest->b = *src;
650}
651
652static inline void CopyPixel(const PixelMonoSigned* src, PixelHsv* dest)
653{
654 dest->v = *src;
655 dest->h = 0;
656 dest->s = 0;
657}
658
659static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbSigned* dest)
660{
661 dest->r = *src;
662 dest->g = *src;
663 dest->b = *src;
664}
665
666static inline void CopyPixel(const PixelMonoSigned* src, PixelRgbFloat* dest)
667{
668 dest->r = *src;
669 dest->g = *src;
670 dest->b = *src;
671}
672
673static inline void CopyPixel(const PixelMonoSigned* src, PixelHsvFloat* dest)
674{
675 dest->v = *src;
676 dest->h = 0;
677 dest->s = 0;
678}
679
680static inline void CopyPixel(const PixelMonoSigned* src, PixelMono* dest)
681{
682 *dest = *src + 128;
683}
684
685static inline void CopyPixel(const PixelMonoSigned* src, PixelInt* dest)
686{
687 *dest = *src;
688}
689
690static inline void CopyPixel(const PixelMonoSigned* src, PixelMono16* dest)
691{
692 *dest = static_cast<yarp::sig::PixelMono16>(*src);
693}
694
695/******************************************************************************/
696
697static inline void CopyPixel(const PixelRgbSigned* src, PixelMono* dest)
698{
699 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
700}
701
702static inline void CopyPixel(const PixelRgbSigned* src, PixelMono16* dest)
703{
704 *dest = static_cast<PixelMono16>((src->r + src->g + src->b)/3);
705}
706
707static inline void CopyPixel(const PixelRgbSigned* src, PixelInt* dest)
708{
709 *dest = static_cast<int>((src->r + src->g + src->b)/3);
710}
711
712static inline void CopyPixel(const PixelRgbSigned* src, PixelHsv* dest)
713{
714 yAssert(false); // Not implemented yet
715}
716
717static inline void CopyPixel(const PixelRgbSigned* src, PixelMonoSigned* dest)
718{
719 *dest = static_cast<char>((src->r + src->g + src->b)/3);
720}
721
722static 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
729static 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
737static 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
745static 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
752static 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
759static inline void CopyPixel(const PixelRgbSigned* src, PixelFloat* dest)
760{
761 *dest = ((src->r + src->g + src->b)/3.0f);
762}
763
764static 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
771static inline void CopyPixel(const PixelRgbSigned* src, PixelHsvFloat* dest)
772{
773 yAssert(false); // Not implemented yet
774}
775
776/******************************************************************************/
777
778static inline void CopyPixel(const PixelFloat* src, PixelMono* dest)
779{
780 *dest = static_cast<unsigned char>(*src);
781}
782
783static inline void CopyPixel(const PixelFloat* src, PixelMono16* dest)
784{
785 *dest = static_cast<yarp::sig::PixelMono16>(*src);
786}
787
788static inline void CopyPixel(const PixelFloat* src, PixelInt* dest)
789{
790 *dest = static_cast<unsigned char>(*src);
791}
792
793static inline void CopyPixel(const PixelFloat* src, PixelMonoSigned* dest)
794{
795 *dest = static_cast<char>(*src);
796}
797
798static 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
805static 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
813static 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
821static 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
828static 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
835static 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
842static 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
849static inline void CopyPixel(const PixelFloat* src, PixelRgbFloat* dest)
850{
851 dest->r = *src;
852 dest->g = *src;
853 dest->b = *src;
854}
855
856static 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
865static inline void CopyPixel(const PixelRgbFloat* src, PixelMono* dest)
866{
867 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
868}
869
870static inline void CopyPixel(const PixelRgbFloat* src, PixelInt* dest)
871{
872 *dest = static_cast<unsigned char>((src->r + src->g + src->b)/3);
873}
874
875static 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
880static inline void CopyPixel(const PixelRgbFloat* src, PixelHsv* dest)
881{
882 yAssert(false); // Not implemented yet
883}
884
885static inline void CopyPixel(const PixelRgbFloat* src, PixelMonoSigned* dest)
886{
887 *dest = static_cast<char>((src->r + src->g + src->b)/3);
888}
889
890static 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
897static 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
905static 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
913static 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
920static 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
927static inline void CopyPixel(const PixelRgbFloat* src, PixelFloat* dest)
928{
929 *dest = ((src->r + src->g + src->b)/3);
930}
931
932static 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
939static inline void CopyPixel(const PixelRgbFloat* src, PixelHsvFloat* dest)
940{
941 yAssert(false); // Not implemented yet
942}
943
944/******************************************************************************/
945
946static inline void CopyPixel(const PixelHsvFloat* src, PixelMono* dest)
947{
948 yAssert(false); // Not implemented yet
949}
950
951static inline void CopyPixel(const PixelHsvFloat* src, PixelMono16* dest)
952{
953 yAssert(false); // Not implemented yet
954}
955
956static inline void CopyPixel(const PixelHsvFloat* src, PixelRgb* dest)
957{
958 yAssert(false); // Not implemented yet
959}
960
961static inline void CopyPixel(const PixelHsvFloat* src, PixelBgr* dest)
962{
963 yAssert(false); // Not implemented yet
964}
965
966static inline void CopyPixel(const PixelHsvFloat* src, PixelRgba* dest)
967{
968 yAssert(false); // Not implemented yet
969}
970
971static inline void CopyPixel(const PixelHsvFloat* src, PixelBgra* dest)
972{
973 yAssert(false); // Not implemented yet
974}
975
976static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbInt* dest)
977{
978 yAssert(false); // Not implemented yet
979}
980
981static inline void CopyPixel(const PixelHsvFloat* src, PixelMonoSigned* dest)
982{
983 yAssert(false); // Not implemented yet
984}
985
986static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbSigned* dest)
987{
988 yAssert(false); // Not implemented yet
989}
990
991static inline void CopyPixel(const PixelHsvFloat* src, PixelFloat* dest)
992{
993 yAssert(false); // Not implemented yet
994}
995
996static inline void CopyPixel(const PixelHsvFloat* src, PixelRgbFloat* dest)
997{
998 yAssert(false); // Not implemented yet
999}
1000
1001static inline void CopyPixel(const PixelHsvFloat* src, PixelHsv* dest)
1002{
1003 yAssert(false); // Not implemented yet
1004}
1005
1006static inline void CopyPixel(const PixelHsvFloat* src, PixelInt* dest)
1007{
1008 yAssert(false); // Not implemented yet
1009}
1010
1011/******************************************************************************/
1012
1013static 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
1020static 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
1028static 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
1036static inline void CopyPixel(const PixelInt* src, PixelRgbInt* dest)
1037{
1038 dest->r = *src;
1039 dest->g = *src;
1040 dest->b = *src;
1041}
1042
1043static 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
1050static 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
1056static 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
1063static inline void CopyPixel(const PixelInt* src, PixelFloat* dest)
1064{
1065 *dest = static_cast<float>(*src);
1066}
1067
1068static 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
1075static inline void CopyPixel(const PixelInt* src, PixelHsvFloat* dest)
1076{
1077 dest->v = float(*src);
1078 dest->h = 0;
1079 dest->s = 0;
1080}
1081
1082static inline void CopyPixel(const PixelInt* src, PixelMonoSigned* dest)
1083{
1084 *dest = static_cast<char>(*src >> 1);
1085}
1086
1087static inline void CopyPixel(const PixelInt* src, PixelMono* dest)
1088{
1089 *dest = static_cast<yarp::sig::PixelMono>(*src);
1090}
1091
1092static inline void CopyPixel(const PixelInt* src, PixelMono16* dest)
1093{
1094 *dest = static_cast<yarp::sig::PixelMono16>(*src);
1095}
1096
1097/******************************************************************************/
1098
1099static 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
1106static 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
1114static 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
1122static 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
1129static inline void CopyPixel(const PixelMono16* src, PixelInt* dest)
1130{
1131 *dest = static_cast<int>(static_cast<unsigned>(*src));
1132}
1133
1134static 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
1141static 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
1148static 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
1155static inline void CopyPixel(const PixelMono16* src, PixelFloat* dest)
1156{
1157 *dest = static_cast<float>(*src);
1158}
1159
1160static 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
1167static 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
1174static inline void CopyPixel(const PixelMono16* src, PixelMonoSigned* dest)
1175{
1176 *dest = static_cast<char>(*src >> 1);
1177}
1178
1179static inline void CopyPixel(const PixelMono16* src, PixelMono* dest)
1180{
1181 *dest = static_cast<yarp::sig::PixelMono>(*src);
1182}
1183
1184static 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
1200template <class T1, class T2>
1201static 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
1253void 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
int PAD_BYTES(int len, int pad)
this might turn out to be useful.
Definition: IplImage.cpp:26
#define yAssert(x)
Definition: Log.h:383
@ VOCAB_PIXEL_RGBA
Definition: Image.h:45
@ VOCAB_PIXEL_INT
Definition: Image.h:47
@ VOCAB_PIXEL_MONO16
Definition: Image.h:43
@ VOCAB_PIXEL_BGRA
Definition: Image.h:46
@ VOCAB_PIXEL_MONO_SIGNED
Definition: Image.h:50
@ VOCAB_PIXEL_BGR
Definition: Image.h:49
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:53
@ VOCAB_PIXEL_HSV_FLOAT
Definition: Image.h:55
@ VOCAB_PIXEL_HSV
Definition: Image.h:48
@ VOCAB_PIXEL_RGB_SIGNED
Definition: Image.h:51
@ VOCAB_PIXEL_RGB_FLOAT
Definition: Image.h:54
@ VOCAB_PIXEL_MONO
Definition: Image.h:42
@ VOCAB_PIXEL_RGB_INT
Definition: Image.h:52
@ VOCAB_PIXEL_RGB
Definition: Image.h:44
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:33
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:562
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:448
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:443
float PixelFloat
Floating point pixel type.
Definition: Image.h:579
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:453
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:532
unsigned char b
Definition: Image.h:533
unsigned char g
Definition: Image.h:534
unsigned char r
Definition: Image.h:535
Packed BGRA pixel type.
Definition: Image.h:507
unsigned char a
Definition: Image.h:511
unsigned char r
Definition: Image.h:510
unsigned char b
Definition: Image.h:508
unsigned char g
Definition: Image.h:509
Floating point HSV pixel type.
Definition: Image.h:630
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:552
unsigned char v
Definition: Image.h:555
unsigned char h
Definition: Image.h:553
unsigned char s
Definition: Image.h:554
Floating point RGB pixel type.
Definition: Image.h:586
Integer RGB pixel type.
Definition: Image.h:608
yarp::os::NetInt32 b
Definition: Image.h:611
yarp::os::NetInt32 g
Definition: Image.h:610
yarp::os::NetInt32 r
Definition: Image.h:609
Signed, packed RGB pixel type.
Definition: Image.h:569
Packed RGB pixel type.
Definition: Image.h:460
unsigned char g
Definition: Image.h:462
unsigned char r
Definition: Image.h:461
unsigned char b
Definition: Image.h:463
Packed RGBA pixel type.
Definition: Image.h:482
unsigned char r
Definition: Image.h:495
unsigned char a
Definition: Image.h:498
unsigned char g
Definition: Image.h:496
unsigned char b
Definition: Image.h:497
#define YARP_UNUSED(var)
Definition: api.h:162