• I420转RGB24,YUY2转RGB24,RGB24垂直翻转,RGB24水平翻转


    I420转RGB24

    1. bool I420ToRGB24(unsigned char* out, const unsigned char* src, int width, int height)
    2. {
    3. if (src == NULL || width <=0 || height <= 0)
    4. return false;
    5. const int R = 0;
    6. const int G = 1;
    7. const int B = 2;
    8. int numOfPixel = width * height;
    9. int positionOfU = numOfPixel;
    10. int positionOfV = numOfPixel / 4 + numOfPixel;
    11. int temp = 0;
    12. for (int i = 0; i<height; i++) {
    13. int startY = i*width;
    14. int step = (i / 2)*(width / 2);
    15. int startV = positionOfV + step;
    16. int startU = positionOfU + step;
    17. for (int j = 0; j < width; j++) {
    18. int Y = startY + j;
    19. int V = startV + j / 2;
    20. int U = startU + j / 2;
    21. int index = Y * 3;
    22. temp = (int)((src[Y] & 0xff) + 1.4075 * ((src[V] & 0xff) - 128));
    23. out[index + B] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
    24. temp = (int)((src[Y] & 0xff) - 0.3455* ((src[U] & 0xff) - 128) - 0.7169*((src[V] & 0xff) - 128));
    25. out[index + G] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
    26. temp = (int)((src[Y] & 0xff) + 1.779* ((src[U] & 0xff) - 128));
    27. out[index + R] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
    28. }
    29. }
    30. return true;
    31. }

    YUY2转RGB24

    1. bool ConvertYUY2toRGB24(unsigned char* pOutputRgb,
    2. unsigned char* pInputBuffer,
    3. int tInputWidth,
    4. int tInputHeight)
    5. {
    6. if (pInputBuffer == NULL || tInputWidth <= 0 || tInputHeight <= 0)
    7. return false;
    8. unsigned char* yuyv = pInputBuffer;
    9. int z = 0, i = 0, j = 0;
    10. for (i = 0; i < tInputHeight; i++) {
    11. for (j = 0; j < tInputWidth; j++) {
    12. int r, g, b;
    13. int y, u, v;
    14. if (!z)
    15. y = yuyv[0] << 8;
    16. else
    17. y = yuyv[2] << 8;
    18. u = yuyv[1] - 128;
    19. v = yuyv[3] - 128;
    20. r = (y + (359 * v)) >> 8;
    21. g = (y - (88 * u) - (183 * v)) >> 8;
    22. b = (y + (454 * u)) >> 8;
    23. r = (r > 255) ? 255 : ((r < 0) ? 0 : r);
    24. g = (g > 255) ? 255 : ((g < 0) ? 0 : g);
    25. b = (b > 255) ? 255 : ((b < 0) ? 0 : b);
    26. *pOutputRgb++ = b;
    27. *pOutputRgb++ = g;
    28. *pOutputRgb++ = r;
    29. if (z++) {
    30. z = 0;
    31. yuyv += 4;
    32. }
    33. }
    34. }
    35. return true;
    36. }

    RGB24垂直翻转

    1. bool HorizontalFlipRGB24(unsigned char* rgb24, int iWidth, int iHeight)
    2. {
    3. // 输入参数合法性判断
    4. if (rgb24 == NULL || iWidth <= 0 || iHeight <= 0) return false;
    5. // 每行图像数据的字节数
    6. int iLBytes = (iWidth * 24 + 23) / 24 * 3;
    7. // 临时RGB图像指针
    8. unsigned char *prgbtmp = (unsigned char*)malloc(iLBytes*iHeight);
    9. if (prgbtmp == NULL) {
    10. return false;
    11. }
    12. else {
    13. std::memset(prgbtmp, 0, iLBytes*iHeight);
    14. }
    15. // 每行
    16. for (int i = 0; i < iHeight; i++)
    17. {
    18. // 每列
    19. for (int j = 0; j < iWidth; j++)
    20. {
    21. std::memcpy((prgbtmp + iLBytes * (iHeight - 1 - i) + 3 * (iWidth - 1 - j)), (rgb24 + iLBytes * (iHeight - 1 - i) + 3 * j), 3);
    22. }
    23. }
    24. std::memcpy(rgb24, prgbtmp, iLBytes*iHeight);
    25. free(prgbtmp);
    26. return true;
    27. }

    RGB24水平翻转

    1. bool VerticalRotateRGB24(unsigned char* rgb24, int width, int height)
    2. {
    3. if (rgb24 == NULL || width == 0 || height == 0)
    4. return false;
    5. unsigned char* rgb = new unsigned char[width * height * 3]; //3表示位24位的视频流
    6. if (rgb == NULL)
    7. return false;
    8. for (int x = 0; x < height; x++)
    9. {
    10. std::memcpy(rgb + width * 3 * x, rgb24 + (height - 1 - x) * width * 3, width * 3);
    11. }
    12. memcpy(rgb24, rgb, width * height * 3);
    13. delete[] rgb;
    14. return true;
    15. }

  • 相关阅读:
    可编程直流电源的特点都有哪些呢?
    判断DataFrame中是否存在具有相同内容的行将具有相同内容的行进行标记和处理
    使用navicat for mongodb连接mongodb
    碳纳米管包四氧化三铁Fe3O4纳米粒子|氧化石墨烯包覆Fe3O4空心球纳米复合材料(r-GO/Fe3O4)|齐岳
    私服下载失败,下载不下来
    Spring Expression Language (SpEL) 介绍与使用方法
    [Hadoop全分布部署]安装JDK、Hadoop
    python-(6-3-1)爬虫---requests入门
    前端八股文之“闭包”
    【RK3588】Firefly 瑞芯微板子入门知识、和环境篇
  • 原文地址:https://blog.csdn.net/u012983289/article/details/134504253