Visual Servoing Platform  version 3.6.1 under development (2024-12-04)
vpImageConvert_yuv.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Convert image types.
32  */
33 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpImageConvert.h>
42 
43 namespace
44 {
45 void vpSAT(int &c)
46 {
47  const int val_255 = 255;
48  if (c < 0) {
49  c = 0;
50  }
51  else if (c > val_255) {
52 
53  c = val_255;
54  }
55 }
56 }
57 
58 BEGIN_VISP_NAMESPACE
71 void vpImageConvert::YUYVToRGBa(unsigned char *yuyv, unsigned char *rgba, unsigned int width, unsigned int height)
72 {
73  unsigned char *s;
74  unsigned char *d;
75  int w, h;
76  int r, g, b, cr, cg, cb, y1, y2;
77  const int val_2 = 2;
78  const int val_88 = 88;
79  const int val_128 = 128;
80  const int val_183 = 183;
81  const int val_256 = 256;
82  const int val_359 = 359;
83  const int val_454 = 454;
84 
85  h = static_cast<int>(height);
86  w = static_cast<int>(width);
87  s = yuyv;
88  d = rgba;
89  while (h--) {
90  int c = w / val_2;
91  while (c--) {
92  y1 = *s;
93  ++s;
94  cb = ((*s - val_128) * val_454) / val_256;
95  cg = (*s - val_128) * val_88;
96  ++s;
97  y2 = *s;
98  ++s;
99  cr = ((*s - val_128) * val_359) / val_256;
100  cg = (cg + ((*s - val_128) * val_183)) / val_256;
101  ++s;
102 
103  r = y1 + cr;
104  b = y1 + cb;
105  g = y1 - cg;
106  vpSAT(r);
107  vpSAT(g);
108  vpSAT(b);
109 
110  *d++ = static_cast<unsigned char>(r);
111  *d++ = static_cast<unsigned char>(g);
112  *d++ = static_cast<unsigned char>(b);
113  *d++ = vpRGBa::alpha_default;
114 
115  r = y2 + cr;
116  b = y2 + cb;
117  g = y2 - cg;
118  vpSAT(r);
119  vpSAT(g);
120  vpSAT(b);
121 
122  *d++ = static_cast<unsigned char>(r);
123  *d++ = static_cast<unsigned char>(g);
124  *d++ = static_cast<unsigned char>(b);
125  *d++ = vpRGBa::alpha_default;
126  }
127  }
128 }
129 
140 void vpImageConvert::YUYVToRGB(unsigned char *yuyv, unsigned char *rgb, unsigned int width, unsigned int height)
141 {
142  unsigned char *s;
143  unsigned char *d;
144  int h, w;
145  int r, g, b, cr, cg, cb, y1, y2;
146  const int val_2 = 2;
147  const int val_88 = 88;
148  const int val_128 = 128;
149  const int val_183 = 183;
150  const int val_256 = 256;
151  const int val_359 = 359;
152  const int val_454 = 454;
153 
154  h = static_cast<int>(height);
155  w = static_cast<int>(width);
156  s = yuyv;
157  d = rgb;
158  while (h--) {
159  int c = w / val_2;
160  while (c--) {
161  y1 = *s;
162  ++s;
163  cb = ((*s - val_128) * val_454) / val_256;
164  cg = (*s - val_128) * val_88;
165  ++s;
166  y2 = *s;
167  ++s;
168  cr = ((*s - val_128) * val_359) / val_256;
169  cg = (cg + ((*s - val_128) * val_183)) / val_256;
170  ++s;
171 
172  r = y1 + cr;
173  b = y1 + cb;
174  g = y1 - cg;
175  vpSAT(r);
176  vpSAT(g);
177  vpSAT(b);
178 
179  *d++ = static_cast<unsigned char>(r);
180  *d++ = static_cast<unsigned char>(g);
181  *d++ = static_cast<unsigned char>(b);
182 
183  r = y2 + cr;
184  b = y2 + cb;
185  g = y2 - cg;
186  vpSAT(r);
187  vpSAT(g);
188  vpSAT(b);
189 
190  *d++ = static_cast<unsigned char>(r);
191  *d++ = static_cast<unsigned char>(g);
192  *d++ = static_cast<unsigned char>(b);
193  }
194  }
195 }
196 
207 void vpImageConvert::YUYVToGrey(unsigned char *yuyv, unsigned char *grey, unsigned int size)
208 {
209  const unsigned int val_2 = 2;
210  const unsigned int val_4 = 4;
211  unsigned int i = 0, j = 0;
212  const unsigned int doubleSize = size * 2;
213  while (j < doubleSize) {
214  grey[i++] = yuyv[j];
215  grey[i++] = yuyv[j + val_2];
216  j += val_4;
217  }
218 }
219 
229 void vpImageConvert::YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
230 {
231  const unsigned int val_128 = 128;
232  for (unsigned int i = size / 4; i; --i) {
233  int U = static_cast<int>((*yuv - val_128) * 0.354);
234  ++yuv;
235  int U5 = 5 * U;
236  int Y0 = *yuv;
237  ++yuv;
238  int Y1 = *yuv;
239  ++yuv;
240  int V = static_cast<int>((*yuv - val_128) * 0.707);
241  ++yuv;
242  int V2 = 2 * V;
243  int Y2 = *yuv;
244  ++yuv;
245  int Y3 = *yuv;
246  ++yuv;
247  int UV = -U - V;
248 
249  // Original equations
250  // R = Y + 1.402 V
251  // G = Y - 0.344 U - 0.714 V
252  // B = Y + 1.772 U
253  int R = Y0 + V2;
254  vpSAT(R);
255 
256  int G = Y0 + UV;
257  vpSAT(G);
258 
259  int B = Y0 + U5;
260  vpSAT(B);
261 
262  *rgba++ = static_cast<unsigned char>(R);
263  *rgba++ = static_cast<unsigned char>(G);
264  *rgba++ = static_cast<unsigned char>(B);
265  *rgba++ = vpRGBa::alpha_default;
266 
267  //---
268  R = Y1 + V2;
269  vpSAT(R);
270 
271  G = Y1 + UV;
272  vpSAT(G);
273 
274  B = Y1 + U5;
275  vpSAT(B);
276 
277  *rgba++ = static_cast<unsigned char>(R);
278  *rgba++ = static_cast<unsigned char>(G);
279  *rgba++ = static_cast<unsigned char>(B);
280  *rgba++ = vpRGBa::alpha_default;
281 
282  //---
283  R = Y2 + V2;
284  vpSAT(R);
285 
286  G = Y2 + UV;
287  vpSAT(G);
288 
289  B = Y2 + U5;
290  vpSAT(B);
291 
292  *rgba++ = static_cast<unsigned char>(R);
293  *rgba++ = static_cast<unsigned char>(G);
294  *rgba++ = static_cast<unsigned char>(B);
295  *rgba++ = vpRGBa::alpha_default;
296 
297  //---
298  R = Y3 + V2;
299  vpSAT(R);
300 
301  G = Y3 + UV;
302  vpSAT(G);
303 
304  B = Y3 + U5;
305  vpSAT(B);
306 
307  *rgba++ = static_cast<unsigned char>(R);
308  *rgba++ = static_cast<unsigned char>(G);
309  *rgba++ = static_cast<unsigned char>(B);
310  *rgba++ = vpRGBa::alpha_default;
311  }
312 }
313 
326 void vpImageConvert::YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
327 {
328  const unsigned int val_2 = 2;
329  const unsigned int val_128 = 128;
330  for (unsigned int i = size / val_2; i; --i) {
331  int U = static_cast<int>((*yuv - val_128) * 0.354);
332  ++yuv;
333  int U5 = 5 * U;
334  int Y0 = *yuv;
335  ++yuv;
336  int V = static_cast<int>((*yuv - val_128) * 0.707);
337  ++yuv;
338  int V2 = 2 * V;
339  int Y1 = *yuv;
340  ++yuv;
341  int UV = -U - V;
342 
343  //---
344  int R = Y0 + V2;
345  vpSAT(R);
346 
347  int G = Y0 + UV;
348  vpSAT(G);
349 
350  int B = Y0 + U5;
351  vpSAT(B);
352 
353  *rgba++ = static_cast<unsigned char>(R);
354  *rgba++ = static_cast<unsigned char>(G);
355  *rgba++ = static_cast<unsigned char>(B);
356  *rgba++ = vpRGBa::alpha_default;
357 
358  //---
359  R = Y1 + V2;
360  vpSAT(R);
361 
362  G = Y1 + UV;
363  vpSAT(G);
364 
365  B = Y1 + U5;
366  vpSAT(B);
367 
368  *rgba++ = static_cast<unsigned char>(R);
369  *rgba++ = static_cast<unsigned char>(G);
370  *rgba++ = static_cast<unsigned char>(B);
371  *rgba++ = vpRGBa::alpha_default;
372  }
373 }
374 
383 void vpImageConvert::YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
384 {
385  unsigned int i = 0, j = 0;
386  const unsigned int val_2 = 2;
387  const unsigned int val_3 = 3;
388  const unsigned int val_4 = 4;
389  const unsigned int val_5 = 5;
390  const unsigned int val_6 = 6;
391  const unsigned int iterLimit = (size * val_3) / val_2;
392  while (j < iterLimit) {
393  grey[i] = yuv[j + 1];
394  grey[i + 1] = yuv[j + val_2];
395  grey[i + val_2] = yuv[j + val_4];
396  grey[i + val_3] = yuv[j + val_5];
397 
398  i += val_4;
399 
400  j += val_6;
401  }
402 }
403 
414 void vpImageConvert::YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
415 {
416  const unsigned int val_2 = 2;
417  const unsigned int val_128 = 128;
418  for (unsigned int i = size / val_2; i; --i) {
419  int U = static_cast<int>((*yuv - val_128) * 0.354);
420  ++yuv;
421  int U5 = 5 * U;
422  int Y0 = *yuv;
423  ++yuv;
424  int V = static_cast<int>((*yuv - val_128) * 0.707);
425  ++yuv;
426  int V2 = 2 * V;
427  int Y1 = *yuv;
428  ++yuv;
429  int UV = -U - V;
430 
431  //---
432  int R = Y0 + V2;
433  vpSAT(R);
434 
435  int G = Y0 + UV;
436  vpSAT(G);
437 
438  int B = Y0 + U5;
439  vpSAT(B);
440 
441  *rgb++ = static_cast<unsigned char>(R);
442  *rgb++ = static_cast<unsigned char>(G);
443  *rgb++ = static_cast<unsigned char>(B);
444 
445  //---
446  R = Y1 + V2;
447  vpSAT(R);
448 
449  G = Y1 + UV;
450  vpSAT(G);
451 
452  B = Y1 + U5;
453  vpSAT(B);
454 
455  *rgb++ = static_cast<unsigned char>(R);
456  *rgb++ = static_cast<unsigned char>(G);
457  *rgb++ = static_cast<unsigned char>(B);
458  }
459 }
460 
471 void vpImageConvert::YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
472 {
473  const unsigned int val_3 = 3;
474  const unsigned int val_4 = 4;
475  unsigned int i = 0, j = 0;
476  const unsigned int doubleSize = size * 2;
477 
478  while (j < doubleSize) {
479  grey[i++] = yuv[j + 1];
480  grey[i++] = yuv[j + val_3];
481  j += val_4;
482  }
483 }
484 
493 void vpImageConvert::YUV411ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
494 {
495  const unsigned int val_128 = 128;
496  for (unsigned int i = size / 4; i; --i) {
497  int U = static_cast<int>((*yuv - val_128) * 0.354);
498  ++yuv;
499  int U5 = 5 * U;
500  int Y0 = *yuv;
501  ++yuv;
502  int Y1 = *yuv;
503  ++yuv;
504  int V = static_cast<int>((*yuv - val_128) * 0.707);
505  ++yuv;
506  int V2 = 2 * V;
507  int Y2 = *yuv;
508  ++yuv;
509  int Y3 = *yuv;
510  ++yuv;
511  int UV = -U - V;
512 
513  // Original equations
514  // R = Y + 1.402 V
515  // G = Y - 0.344 U - 0.714 V
516  // B = Y + 1.772 U
517  int R = Y0 + V2;
518  vpSAT(R);
519 
520  int G = Y0 + UV;
521  vpSAT(G);
522 
523  int B = Y0 + U5;
524  vpSAT(B);
525 
526  *rgb++ = static_cast<unsigned char>(R);
527  *rgb++ = static_cast<unsigned char>(G);
528  *rgb++ = static_cast<unsigned char>(B);
529 
530  //---
531  R = Y1 + V2;
532  vpSAT(R);
533 
534  G = Y1 + UV;
535  vpSAT(G);
536 
537  B = Y1 + U5;
538  vpSAT(B);
539 
540  *rgb++ = static_cast<unsigned char>(R);
541  *rgb++ = static_cast<unsigned char>(G);
542  *rgb++ = static_cast<unsigned char>(B);
543 
544  //---
545  R = Y2 + V2;
546  vpSAT(R);
547 
548  G = Y2 + UV;
549  vpSAT(G);
550 
551  B = Y2 + U5;
552  vpSAT(B);
553 
554  *rgb++ = static_cast<unsigned char>(R);
555  *rgb++ = static_cast<unsigned char>(G);
556  *rgb++ = static_cast<unsigned char>(B);
557 
558  //---
559  R = Y3 + V2;
560  vpSAT(R);
561 
562  G = Y3 + UV;
563  vpSAT(G);
564 
565  B = Y3 + U5;
566  vpSAT(B);
567 
568  *rgb++ = static_cast<unsigned char>(R);
569  *rgb++ = static_cast<unsigned char>(G);
570  *rgb++ = static_cast<unsigned char>(B);
571  }
572 }
573 
584 void vpImageConvert::YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
585 {
586  int U, V, R, G, B, V2, U5, UV;
587  int Y0, Y1, Y2, Y3;
588  const unsigned int val_2 = 2;
589  const unsigned int val_4 = 4;
590  const unsigned int val_5 = 5;
591  const unsigned int val_7 = 7;
592  unsigned int size = width * height;
593  unsigned char *iU = yuv + size;
594  unsigned char *iV = yuv + ((val_5 * size) / val_4);
595  const unsigned int halfHeight = height / val_2, halfWidth = width / val_2;
596  const unsigned int val_128 = 128;
597  for (unsigned int i = 0; i < halfHeight; ++i) {
598  for (unsigned int j = 0; j < halfWidth; ++j) {
599  U = static_cast<int>(((*iU) - val_128) * 0.354);
600  ++iU;
601  U5 = val_5 * U;
602  V = static_cast<int>(((*iV) - val_128) * 0.707);
603  ++iV;
604  V2 = val_2 * V;
605  UV = -U - V;
606  Y0 = *yuv;
607  ++yuv;
608  Y1 = *yuv;
609  yuv = yuv + (width - 1);
610  Y2 = *yuv;
611  ++yuv;
612  Y3 = *yuv;
613  yuv = (yuv - width) + 1;
614 
615  // Original equations
616  // R = Y + 1.402 V
617  // G = Y - 0.344 U - 0.714 V
618  // B = Y + 1.772 U
619  R = Y0 + V2;
620  vpSAT(R);
621 
622  G = Y0 + UV;
623  vpSAT(G);
624 
625  B = Y0 + U5;
626  vpSAT(B);
627 
628  *rgba++ = static_cast<unsigned char>(R);
629  *rgba++ = static_cast<unsigned char>(G);
630  *rgba++ = static_cast<unsigned char>(B);
631  *rgba++ = vpRGBa::alpha_default;
632 
633  //---
634  R = Y1 + V2;
635  vpSAT(R);
636 
637  G = Y1 + UV;
638  vpSAT(G);
639 
640  B = Y1 + U5;
641  vpSAT(B);
642 
643  *rgba++ = static_cast<unsigned char>(R);
644  *rgba++ = static_cast<unsigned char>(G);
645  *rgba++ = static_cast<unsigned char>(B);
646  *rgba = vpRGBa::alpha_default;
647  rgba = (rgba + (val_4 * width)) - val_7;
648 
649  //---
650  R = Y2 + V2;
651  vpSAT(R);
652 
653  G = Y2 + UV;
654  vpSAT(G);
655 
656  B = Y2 + U5;
657  vpSAT(B);
658 
659  *rgba++ = static_cast<unsigned char>(R);
660  *rgba++ = static_cast<unsigned char>(G);
661  *rgba++ = static_cast<unsigned char>(B);
662  *rgba++ = vpRGBa::alpha_default;
663 
664  //---
665  R = Y3 + V2;
666  vpSAT(R);
667 
668  G = Y3 + UV;
669  vpSAT(G);
670 
671  B = Y3 + U5;
672  vpSAT(B);
673 
674  *rgba++ = static_cast<unsigned char>(R);
675  *rgba++ = static_cast<unsigned char>(G);
676  *rgba++ = static_cast<unsigned char>(B);
677  *rgba = vpRGBa::alpha_default;
678  rgba = (rgba - (val_4 * width)) + 1;
679  }
680  yuv += width;
681  rgba += val_4 * width;
682  }
683 }
684 
693 void vpImageConvert::YUV420ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
694 {
695  int U, V, R, G, B, V2, U5, UV;
696  int Y0, Y1, Y2, Y3;
697  const unsigned int val_2 = 2;
698  const unsigned int val_3 = 3;
699  const unsigned int val_4 = 4;
700  const unsigned int val_5 = 5;
701  unsigned int size = width * height;
702  unsigned char *iU = yuv + size;
703  unsigned char *iV = yuv + ((val_5 * size) / val_4);
704  const unsigned int halfHeight = height / val_2, halfWidth = width / val_2;
705  const unsigned int val_128 = 128;
706  for (unsigned int i = 0; i < halfHeight; ++i) {
707  for (unsigned int j = 0; j < halfWidth; ++j) {
708  U = static_cast<int>(((*iU) - val_128) * 0.354);
709  ++iU;
710  U5 = val_5 * U;
711  V = static_cast<int>(((*iV) - val_128) * 0.707);
712  ++iV;
713  V2 = val_2 * V;
714  UV = -U - V;
715  Y0 = *yuv;
716  ++yuv;
717  Y1 = *yuv;
718  yuv = yuv + (width - 1);
719  Y2 = *yuv;
720  ++yuv;
721  Y3 = *yuv;
722  yuv = (yuv - width) + 1;
723 
724  // Original equations
725  // R = Y + 1.402 V
726  // G = Y - 0.344 U - 0.714 V
727  // B = Y + 1.772 U
728  R = Y0 + V2;
729  vpSAT(R);
730 
731  G = Y0 + UV;
732  vpSAT(G);
733 
734  B = Y0 + U5;
735  vpSAT(B);
736 
737  *rgb++ = static_cast<unsigned char>(R);
738  *rgb++ = static_cast<unsigned char>(G);
739  *rgb++ = static_cast<unsigned char>(B);
740 
741  //---
742  R = Y1 + V2;
743  vpSAT(R);
744 
745  G = Y1 + UV;
746  vpSAT(G);
747 
748  B = Y1 + U5;
749  vpSAT(B);
750 
751  *rgb++ = static_cast<unsigned char>(R);
752  *rgb++ = static_cast<unsigned char>(G);
753  *rgb = static_cast<unsigned char>(B);
754  rgb = rgb + ((val_3 * width) - val_5);
755 
756  //---
757  R = Y2 + V2;
758  vpSAT(R);
759 
760  G = Y2 + UV;
761  vpSAT(G);
762 
763  B = Y2 + U5;
764  vpSAT(B);
765 
766  *rgb++ = static_cast<unsigned char>(R);
767  *rgb++ = static_cast<unsigned char>(G);
768  *rgb++ = static_cast<unsigned char>(B);
769 
770  //---
771  R = Y3 + V2;
772  vpSAT(R);
773 
774  G = Y3 + UV;
775  vpSAT(G);
776 
777  B = Y3 + U5;
778  vpSAT(B);
779 
780  *rgb++ = static_cast<unsigned char>(R);
781  *rgb++ = static_cast<unsigned char>(G);
782  *rgb = static_cast<unsigned char>(B);
783  rgb = (rgb - (val_3 * width)) + 1;
784  }
785  yuv += width;
786  rgb += val_3 * width;
787  }
788 }
789 
797 void vpImageConvert::YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
798 {
799  for (unsigned int i = 0; i < size; ++i) {
800  *grey++ = *yuv;
801  ++yuv;
802  }
803 }
804 
814 void vpImageConvert::YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
815 {
816  const unsigned int val_128 = 128;
817  for (unsigned int i = 0; i < size; ++i) {
818  int U = static_cast<int>((*yuv - val_128) * 0.354);
819  ++yuv;
820  int U5 = 5 * U;
821  int Y = *yuv;
822  ++yuv;
823  int V = static_cast<int>((*yuv - val_128) * 0.707);
824  ++yuv;
825  int V2 = 2 * V;
826  int UV = -U - V;
827 
828  // Original equations
829  // R = Y + 1.402 V
830  // G = Y - 0.344 U - 0.714 V
831  // B = Y + 1.772 U
832  int R = Y + V2;
833  vpSAT(R);
834 
835  int G = Y + UV;
836  vpSAT(G);
837 
838  int B = Y + U5;
839  vpSAT(B);
840 
841  *rgba++ = static_cast<unsigned char>(R);
842  *rgba++ = static_cast<unsigned char>(G);
843  *rgba++ = static_cast<unsigned char>(B);
844  *rgba++ = vpRGBa::alpha_default;
845  }
846 }
847 
855 void vpImageConvert::YUV444ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
856 {
857  const unsigned int val_128 = 128;
858  for (unsigned int i = 0; i < size; ++i) {
859  int U = static_cast<int>((*yuv - val_128) * 0.354);
860  ++yuv;
861  int U5 = 5 * U;
862  int Y = *yuv;
863  ++yuv;
864  int V = static_cast<int>((*yuv - val_128) * 0.707);
865  ++yuv;
866  int V2 = 2 * V;
867  int UV = -U - V;
868 
869  // Original equations
870  // R = Y + 1.402 V
871  // G = Y - 0.344 U - 0.714 V
872  // B = Y + 1.772 U
873  int R = Y + V2;
874  vpSAT(R);
875 
876  int G = Y + UV;
877  vpSAT(G);
878 
879  int B = Y + U5;
880  vpSAT(B);
881 
882  *rgb++ = static_cast<unsigned char>(R);
883  *rgb++ = static_cast<unsigned char>(G);
884  *rgb++ = static_cast<unsigned char>(B);
885  }
886 }
887 
895 void vpImageConvert::YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
896 {
897  const unsigned int val_3 = 3;
898  ++yuv;
899  for (unsigned int i = 0; i < size; ++i) {
900  *grey++ = *yuv;
901  yuv = yuv + val_3;
902  }
903 }
904 
915 void vpImageConvert::YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
916 {
917  int U, V, R, G, B, V2, U5, UV;
918  int Y0, Y1, Y2, Y3;
919  const unsigned int val_2 = 2;
920  const unsigned int val_4 = 4;
921  const unsigned int val_5 = 5;
922  const unsigned int val_7 = 7;
923  unsigned int size = width * height;
924  unsigned char *iV = yuv + size;
925  unsigned char *iU = yuv + ((val_5 * size) / val_4);
926  const unsigned int halfHeight = height / val_2, halfWidth = width / val_2;
927  const unsigned int val_128 = 128;
928  for (unsigned int i = 0; i < halfHeight; ++i) {
929  for (unsigned int j = 0; j < halfWidth; ++j) {
930  U = static_cast<int>(((*iU) - val_128) * 0.354);
931  ++iU;
932  U5 = val_5 * U;
933  V = static_cast<int>(((*iV) - val_128) * 0.707);
934  ++iV;
935  V2 = val_2 * V;
936  UV = -U - V;
937  Y0 = *yuv;
938  ++yuv;
939  Y1 = *yuv;
940  yuv = yuv + (width - 1);
941  Y2 = *yuv;
942  ++yuv;
943  Y3 = *yuv;
944  yuv = (yuv - width) + 1;
945 
946  // Original equations
947  // R = Y + 1.402 V
948  // G = Y - 0.344 U - 0.714 V
949  // B = Y + 1.772 U
950  R = Y0 + V2;
951  vpSAT(R);
952 
953  G = Y0 + UV;
954  vpSAT(G);
955 
956  B = Y0 + U5;
957  vpSAT(B);
958 
959  *rgba++ = static_cast<unsigned char>(R);
960  *rgba++ = static_cast<unsigned char>(G);
961  *rgba++ = static_cast<unsigned char>(B);
962  *rgba++ = vpRGBa::alpha_default;
963 
964  //---
965  R = Y1 + V2;
966  vpSAT(R);
967 
968  G = Y1 + UV;
969  vpSAT(G);
970 
971  B = Y1 + U5;
972  vpSAT(B);
973 
974  *rgba++ = static_cast<unsigned char>(R);
975  *rgba++ = static_cast<unsigned char>(G);
976  *rgba++ = static_cast<unsigned char>(B);
977  *rgba = 0;
978  rgba = rgba + ((val_4 * width) - val_7);
979 
980  //---
981  R = Y2 + V2;
982  vpSAT(R);
983 
984  G = Y2 + UV;
985  vpSAT(G);
986 
987  B = Y2 + U5;
988  vpSAT(B);
989 
990  *rgba++ = static_cast<unsigned char>(R);
991  *rgba++ = static_cast<unsigned char>(G);
992  *rgba++ = static_cast<unsigned char>(B);
993  *rgba++ = vpRGBa::alpha_default;
994 
995  //---
996  R = Y3 + V2;
997  vpSAT(R);
998 
999  G = Y3 + UV;
1000  vpSAT(G);
1001 
1002  B = Y3 + U5;
1003  vpSAT(B);
1004 
1005  *rgba++ = static_cast<unsigned char>(R);
1006  *rgba++ = static_cast<unsigned char>(G);
1007  *rgba++ = static_cast<unsigned char>(B);
1008  *rgba = vpRGBa::alpha_default;
1009  rgba = (rgba - (val_4 * width)) + 1;
1010  }
1011  yuv += width;
1012  rgba += val_4 * width;
1013  }
1014 }
1015 
1024 void vpImageConvert::YV12ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int height, unsigned int width)
1025 {
1026  int U, V, R, G, B, V2, U5, UV;
1027  int Y0, Y1, Y2, Y3;
1028  const unsigned int val_2 = 2;
1029  const unsigned int val_3 = 3;
1030  const unsigned int val_4 = 4;
1031  const unsigned int val_5 = 5;
1032  unsigned int size = width * height;
1033  unsigned char *iV = yuv + size;
1034  unsigned char *iU = yuv + ((val_5 * size) / val_4);
1035  const unsigned int halfHeight = height / val_2, halfWidth = width / val_2;
1036  const unsigned int val_128 = 128;
1037  for (unsigned int i = 0; i < halfHeight; ++i) {
1038  for (unsigned int j = 0; j < halfWidth; ++j) {
1039  U = static_cast<int>(((*iU) - val_128) * 0.354);
1040  ++iU;
1041  U5 = val_5 * U;
1042  V = static_cast<int>(((*iV) - val_128) * 0.707);
1043  ++iV;
1044  V2 = val_2 * V;
1045  UV = -U - V;
1046  Y0 = *yuv;
1047  ++yuv;
1048  Y1 = *yuv;
1049  yuv = yuv + (width - 1);
1050  Y2 = *yuv;
1051  ++yuv;
1052  Y3 = *yuv;
1053  yuv = (yuv - width) + 1;
1054 
1055  // Original equations
1056  // R = Y + 1.402 V
1057  // G = Y - 0.344 U - 0.714 V
1058  // B = Y + 1.772 U
1059  R = Y0 + V2;
1060  vpSAT(R);
1061 
1062  G = Y0 + UV;
1063  vpSAT(G);
1064 
1065  B = Y0 + U5;
1066  vpSAT(B);
1067 
1068  *rgb++ = static_cast<unsigned char>(R);
1069  *rgb++ = static_cast<unsigned char>(G);
1070  *rgb++ = static_cast<unsigned char>(B);
1071 
1072  //---
1073  R = Y1 + V2;
1074  vpSAT(R);
1075 
1076  G = Y1 + UV;
1077  vpSAT(G);
1078 
1079  B = Y1 + U5;
1080  vpSAT(B);
1081 
1082  *rgb++ = static_cast<unsigned char>(R);
1083  *rgb++ = static_cast<unsigned char>(G);
1084  *rgb = static_cast<unsigned char>(B);
1085  rgb = rgb + ((val_3 * width) - val_5);
1086 
1087  //---
1088  R = Y2 + V2;
1089  vpSAT(R);
1090 
1091  G = Y2 + UV;
1092  vpSAT(G);
1093 
1094  B = Y2 + U5;
1095  vpSAT(B);
1096 
1097  *rgb++ = static_cast<unsigned char>(R);
1098  *rgb++ = static_cast<unsigned char>(G);
1099  *rgb++ = static_cast<unsigned char>(B);
1100 
1101  //---
1102  R = Y3 + V2;
1103  vpSAT(R);
1104 
1105  G = Y3 + UV;
1106  vpSAT(G);
1107 
1108  B = Y3 + U5;
1109  vpSAT(B);
1110 
1111  *rgb++ = static_cast<unsigned char>(R);
1112  *rgb++ = static_cast<unsigned char>(G);
1113  *rgb = static_cast<unsigned char>(B);
1114  rgb = (rgb - (val_3 * width)) + 1;
1115  }
1116  yuv += width;
1117  rgb += val_3 * width;
1118  }
1119 }
1120 
1121 namespace
1122 {
1133 void YVU9ToRGBasubroutine(unsigned char *rgba, int R, int G, int B, int a)
1134 {
1135  vpSAT(R);
1136  vpSAT(G);
1137  vpSAT(B);
1138  *rgba++ = static_cast<unsigned char>(R);
1139  *rgba++ = static_cast<unsigned char>(G);
1140  *rgba++ = static_cast<unsigned char>(B);
1141  *rgba++ = a;
1142 }
1143 }
1154 void vpImageConvert::YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
1155 {
1156  int U, V, R, G, B, V2, U5, UV;
1157  int Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10, Y11, Y12, Y13, Y14, Y15;
1158  const unsigned int val_2 = 2;
1159  const unsigned int val_3 = 3;
1160  const unsigned int val_4 = 4;
1161  const unsigned int val_5 = 5;
1162  const unsigned int val_12 = 12;
1163  const unsigned int val_15 = 15;
1164  const unsigned int val_16 = 16;
1165  const unsigned int val_17 = 17;
1166  unsigned int size = width * height;
1167  unsigned char *iV = yuv + size;
1168  unsigned char *iU = yuv + ((val_17 * size) / val_16);
1169  const unsigned int quarterHeight = height / val_4, quarterWidth = width / val_4;
1170  const unsigned int val_128 = 128;
1171  for (unsigned int i = 0; i < quarterHeight; ++i) {
1172  for (unsigned int j = 0; j < quarterWidth; ++j) {
1173  U = static_cast<int>(((*iU) - val_128) * 0.354);
1174  ++iU;
1175  U5 = val_5 * U;
1176  V = static_cast<int>(((*iV) - val_128) * 0.707);
1177  ++iV;
1178  V2 = val_2 * V;
1179  UV = -U - V;
1180  Y0 = *yuv;
1181  ++yuv;
1182  Y1 = *yuv;
1183  ++yuv;
1184  Y2 = *yuv;
1185  ++yuv;
1186  Y3 = *yuv;
1187  yuv = yuv + (width - val_3);
1188  Y4 = *yuv;
1189  ++yuv;
1190  Y5 = *yuv;
1191  ++yuv;
1192  Y6 = *yuv;
1193  ++yuv;
1194  Y7 = *yuv;
1195  yuv = yuv + (width - val_3);
1196  Y8 = *yuv;
1197  ++yuv;
1198  Y9 = *yuv;
1199  ++yuv;
1200  Y10 = *yuv;
1201  ++yuv;
1202  Y11 = *yuv;
1203  yuv = yuv + (width - val_3);
1204  Y12 = *yuv;
1205  ++yuv;
1206  Y13 = *yuv;
1207  ++yuv;
1208  Y14 = *yuv;
1209  ++yuv;
1210  Y15 = *yuv;
1211  yuv = (yuv - (val_3 * width)) + 1;
1212 
1213  // Original equations
1214  // R = Y + 1.402 V
1215  // G = Y - 0.344 U - 0.714 V
1216  // B = Y + 1.772 U
1217  R = Y0 + V2;
1218  G = Y0 + UV;
1219  B = Y0 + U5;
1220  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1221 
1222  //---
1223  R = Y1 + V2;
1224  G = Y1 + UV;
1225  B = Y1 + U5;
1226  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1227 
1228  //---
1229  R = Y2 + V2;
1230  G = Y2 + UV;
1231  B = Y2 + U5;
1232  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1233 
1234  //---
1235  R = Y3 + V2;
1236  G = Y3 + UV;
1237  B = Y3 + U5;
1238  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1239  rgba = rgba + ((val_4 * width) - val_15);
1240 
1241  R = Y4 + V2;
1242  G = Y4 + UV;
1243  B = Y4 + U5;
1244  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1245 
1246  //---
1247  R = Y5 + V2;
1248  G = Y5 + UV;
1249  B = Y5 + U5;
1250  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1251 
1252  //---
1253  R = Y6 + V2;
1254  G = Y6 + UV;
1255  B = Y6 + U5;
1256  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1257 
1258  //---
1259  R = Y7 + V2;
1260  G = Y7 + UV;
1261  B = Y7 + U5;
1262  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1263  rgba = rgba + ((val_4 * width) - val_15);
1264 
1265  R = Y8 + V2;
1266  G = Y8 + UV;
1267  B = Y8 + U5;
1268  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1269 
1270  //---
1271  R = Y9 + V2;
1272  G = Y9 + UV;
1273  B = Y9 + U5;
1274  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1275 
1276  //---
1277  R = Y10 + V2;
1278  G = Y10 + UV;
1279  B = Y10 + U5;
1280  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1281 
1282  //---
1283  R = Y11 + V2;
1284  G = Y11 + UV;
1285  B = Y11 + U5;
1286  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1287  rgba = rgba + ((val_4 * width) - val_15);
1288 
1289  R = Y12 + V2;
1290  G = Y12 + UV;
1291  B = Y12 + U5;
1292  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1293 
1294  //---
1295  R = Y13 + V2;
1296  G = Y13 + UV;
1297  B = Y13 + U5;
1298  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1299 
1300  //---
1301  R = Y14 + V2;
1302  G = Y14 + UV;
1303  B = Y14 + U5;
1304  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1305 
1306  //---
1307  R = Y15 + V2;
1308  G = Y15 + UV;
1309  B = Y15 + U5;
1310  YVU9ToRGBasubroutine(rgba, R, G, B, vpRGBa::alpha_default);
1311  rgba = (rgba - (val_12 * width)) + 1;
1312  }
1313  yuv += val_3 * width;
1314  rgba += val_12 * width;
1315  }
1316 }
1317 
1318 namespace
1319 {
1329 void YVU9ToRGBsubroutine(unsigned char *rgb, int R, int G, int B)
1330 {
1331  vpSAT(R);
1332  vpSAT(G);
1333  vpSAT(B);
1334  *rgb++ = static_cast<unsigned char>(R);
1335  *rgb++ = static_cast<unsigned char>(G);
1336  *rgb++ = static_cast<unsigned char>(B);
1337 }
1338 }
1339 
1347 void vpImageConvert::YVU9ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int height, unsigned int width)
1348 {
1349  int U, V, R, G, B, V2, U5, UV;
1350  int Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10, Y11, Y12, Y13, Y14, Y15;
1351  const unsigned int val_2 = 2;
1352  const unsigned int val_3 = 3;
1353  const unsigned int val_4 = 4;
1354  const unsigned int val_5 = 5;
1355  const unsigned int val_9 = 9;
1356  const unsigned int val_11 = 11;
1357  const unsigned int val_16 = 16;
1358  const unsigned int val_17 = 17;
1359  unsigned int size = width * height;
1360  unsigned char *iV = yuv + size;
1361  unsigned char *iU = yuv + ((val_17 * size) / val_16);
1362  const unsigned int quarterHeight = height / val_4, quarterWidth = width / val_4;
1363  const unsigned int val_128 = 128;
1364  for (unsigned int i = 0; i < quarterHeight; ++i) {
1365  for (unsigned int j = 0; j < quarterWidth; ++j) {
1366  U = static_cast<int>((*iU - val_128) * 0.354);
1367  ++iU;
1368  U5 = val_5 * U;
1369  V = static_cast<int>((*iV - val_128) * 0.707);
1370  ++iV;
1371  V2 = val_2 * V;
1372  UV = -U - V;
1373  Y0 = *yuv;
1374  ++yuv;
1375  Y1 = *yuv;
1376  ++yuv;
1377  Y2 = *yuv;
1378  ++yuv;
1379  Y3 = *yuv;
1380  yuv = yuv + (width - val_3);
1381  Y4 = *yuv;
1382  ++yuv;
1383  Y5 = *yuv;
1384  ++yuv;
1385  Y6 = *yuv;
1386  ++yuv;
1387  Y7 = *yuv;
1388  yuv = yuv + (width - val_3);
1389  Y8 = *yuv;
1390  ++yuv;
1391  Y9 = *yuv;
1392  ++yuv;
1393  Y10 = *yuv;
1394  ++yuv;
1395  Y11 = *yuv;
1396  yuv = yuv + (width - val_3);
1397  Y12 = *yuv;
1398  ++yuv;
1399  Y13 = *yuv;
1400  ++yuv;
1401  Y14 = *yuv;
1402  ++yuv;
1403  Y15 = *yuv;
1404  yuv = (yuv - (val_3 * width)) + 1;
1405 
1406  // Original equations
1407  // R = Y + 1.402 V
1408  // G = Y - 0.344 U - 0.714 V
1409  // B = Y + 1.772 U
1410  R = Y0 + V2;
1411  G = Y0 + UV;
1412  B = Y0 + U5;
1413  YVU9ToRGBsubroutine(rgb, R, G, B);
1414 
1415  //---
1416  R = Y1 + V2;
1417  G = Y1 + UV;
1418  B = Y1 + U5;
1419  YVU9ToRGBsubroutine(rgb, R, G, B);
1420 
1421  //---
1422  R = Y2 + V2;
1423  G = Y2 + UV;
1424  B = Y2 + U5;
1425  YVU9ToRGBsubroutine(rgb, R, G, B);
1426 
1427  //---
1428  R = Y3 + V2;
1429  G = Y3 + UV;
1430  B = Y3 + U5;
1431  YVU9ToRGBsubroutine(rgb, R, G, B);
1432  rgb = rgb + ((val_3 * width) - val_11);
1433 
1434  R = Y4 + V2;
1435  G = Y4 + UV;
1436  B = Y4 + U5;
1437  YVU9ToRGBsubroutine(rgb, R, G, B);
1438 
1439  //---
1440  R = Y5 + V2;
1441  G = Y5 + UV;
1442  B = Y5 + U5;
1443  YVU9ToRGBsubroutine(rgb, R, G, B);
1444 
1445  //---
1446  R = Y6 + V2;
1447  G = Y6 + UV;
1448  B = Y6 + U5;
1449  YVU9ToRGBsubroutine(rgb, R, G, B);
1450 
1451  //---
1452  R = Y7 + V2;
1453  G = Y7 + UV;
1454  B = Y7 + U5;
1455  YVU9ToRGBsubroutine(rgb, R, G, B);
1456  rgb = rgb + ((val_3 * width) - val_11);
1457 
1458  R = Y8 + V2;
1459  G = Y8 + UV;
1460  B = Y8 + U5;
1461  YVU9ToRGBsubroutine(rgb, R, G, B);
1462 
1463  //---
1464  R = Y9 + V2;
1465  G = Y9 + UV;
1466  B = Y9 + U5;
1467  YVU9ToRGBsubroutine(rgb, R, G, B);
1468 
1469  //---
1470  R = Y10 + V2;
1471  G = Y10 + UV;
1472  B = Y10 + U5;
1473  YVU9ToRGBsubroutine(rgb, R, G, B);
1474 
1475  //---
1476  R = Y11 + V2;
1477  G = Y11 + UV;
1478  B = Y11 + U5;
1479  YVU9ToRGBsubroutine(rgb, R, G, B);
1480  rgb = (rgb + (val_3 * width)) - val_11;
1481 
1482  R = Y12 + V2;
1483  G = Y12 + UV;
1484  B = Y12 + U5;
1485  YVU9ToRGBsubroutine(rgb, R, G, B);
1486 
1487  //---
1488  R = Y13 + V2;
1489  G = Y13 + UV;
1490  B = Y13 + U5;
1491  YVU9ToRGBsubroutine(rgb, R, G, B);
1492 
1493  //---
1494  R = Y14 + V2;
1495  G = Y14 + UV;
1496  B = Y14 + U5;
1497  YVU9ToRGBsubroutine(rgb, R, G, B);
1498 
1499  //---
1500  R = Y15 + V2;
1501  G = Y15 + UV;
1502  B = Y15 + U5;
1503  YVU9ToRGBsubroutine(rgb, R, G, B);
1504  rgb = (rgb - (val_9 * width)) + 1;
1505  }
1506  yuv += val_3 * width;
1507  rgb += val_9 * width;
1508  }
1509 }
1510 END_VISP_NAMESPACE
static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YUYVToGrey(unsigned char *yuyv, unsigned char *grey, unsigned int size)
static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YUYVToRGBa(unsigned char *yuyv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YVU9ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
static void YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height)
static void YUV444ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
static void YUV411ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
static void YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size)
static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YV12ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YUV420ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
static void YUYVToRGB(unsigned char *yuyv, unsigned char *rgb, unsigned int width, unsigned int height)
@ alpha_default
Definition: vpRGBa.h:67