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