yuv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * Copyright (C) 2012 by Tobias Müller *
  3. * Tobias_Mueller@twam.info *
  4. * *
  5. * This program 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. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. /**
  21. Convert from YUV420 format to YUV444.
  22. \param width width of image
  23. \param height height of image
  24. \param src source
  25. \param dst destination
  26. */
  27. void YUV420toYUV444(int width, int height, unsigned char* src, unsigned char* dst) {
  28. int line, column;
  29. unsigned char *py, *pu, *pv;
  30. unsigned char *tmp = dst;
  31. // In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr.
  32. // Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels.
  33. unsigned char *base_py = src;
  34. unsigned char *base_pu = src+(height*width);
  35. unsigned char *base_pv = src+(height*width)+(height*width)/4;
  36. for (line = 0; line < height; ++line) {
  37. for (column = 0; column < width; ++column) {
  38. py = base_py+(line*width)+column;
  39. pu = base_pu+(line/2*width/2)+column/2;
  40. pv = base_pv+(line/2*width/2)+column/2;
  41. *tmp++ = *py;
  42. *tmp++ = *pu;
  43. *tmp++ = *pv;
  44. }
  45. }
  46. }
  47. void YUV420NV21toYUV444(int width, int height, unsigned char* src, unsigned char* dst,
  48. int is_nv21)
  49. {
  50. int line, column;
  51. unsigned char *py, *pu, *pv;
  52. unsigned char *tmp = dst;
  53. // In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr.
  54. // Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels.
  55. unsigned char *base_py = src;
  56. unsigned char *base_pu = src+(height*width);
  57. unsigned char *base_pv = src+(height*width);
  58. for (line = 0; line < height; ++line) {
  59. for (column = 0; column < width; ++column) {
  60. py = base_py + (line * width) + column;
  61. if (is_nv21) {
  62. pu = base_pu + ((line / 2 * width / 2) + column / 2) * 2 + 1;
  63. pv = base_pv + ((line / 2 * width / 2) + column / 2) * 2;
  64. } else {
  65. pu = base_pu + ((line / 2 * width / 2) + column / 2) * 2;
  66. pv = base_pv + ((line / 2 * width / 2) + column / 2) * 2 + 1;
  67. }
  68. *tmp++ = *py;
  69. *tmp++ = *pu;
  70. *tmp++ = *pv;
  71. }
  72. }
  73. }
  74. void YUV422toYUV444(int width, int height, unsigned char* src, unsigned char* dst) {
  75. int line, column;
  76. unsigned char *py, *pu, *pv;
  77. unsigned char *tmp = dst;
  78. // In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr.
  79. // Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels.
  80. unsigned char *base_py = src;
  81. //unsigned char *base_pu = src+(height*width);
  82. //unsigned char *base_pv = src+(height*width)+(height*width)/2;
  83. for (line = 0; line < height; ++line) {
  84. for (column = 0; column < width; ++column) {
  85. py = base_py+((line*width)+column)*2;
  86. pu = base_py+((line*width)+column)*2 + 1;
  87. pv = base_py+((line*width)+column)*2 + 1;
  88. *tmp++ = *py;
  89. *tmp++ = *pu;
  90. *tmp++ = *pv;
  91. }
  92. }
  93. }
  94. void RGB565toRGB888(int width, int height, unsigned char* src, unsigned char* dst)
  95. {
  96. unsigned short *pix = (unsigned short *)src;
  97. unsigned int i;
  98. for (i = 0 ; i < width * height; i++)
  99. {
  100. *dst++ = (*(pix+i) & 0b1111100000000000) >> 8;
  101. *dst++ = (*(pix+i) & 0b11111100000) >> 3 ;
  102. *dst++ = (*(pix+i) & 0b11111) << 3;
  103. }
  104. }
  105. void RAW12toRAW16(int width, int height, unsigned char* src, unsigned char* dst)
  106. {
  107. unsigned char *p_src = src;
  108. unsigned short *p_dst = (unsigned short *)dst;
  109. unsigned int i, j;
  110. for (i = 0 ; i < height; i++) {
  111. p_src = src + (((width * 12 / 8 + 8 * 16 - 1) / (8 * 16)) * 128) * i;
  112. for (j = 0 ; j < width * 12 / 8; j += 3) {
  113. *p_dst++ = ((*(p_src + j)) | ((*(p_src + j + 1) & 0xF) << 8)) << 4;
  114. *p_dst++ = ((*(p_src + j + 2) << 4) | ((*(p_src + j + 1)) >> 4)) << 4;
  115. }
  116. }
  117. }