I420转RGB24
- bool I420ToRGB24(unsigned char* out, const unsigned char* src, int width, int height)
- {
- if (src == NULL || width <=0 || height <= 0)
- return false;
- const int R = 0;
- const int G = 1;
- const int B = 2;
-
- int numOfPixel = width * height;
- int positionOfU = numOfPixel;
- int positionOfV = numOfPixel / 4 + numOfPixel;
- int temp = 0;
- for (int i = 0; i<height; i++) {
- int startY = i*width;
- int step = (i / 2)*(width / 2);
- int startV = positionOfV + step;
- int startU = positionOfU + step;
- for (int j = 0; j < width; j++) {
- int Y = startY + j;
- int V = startV + j / 2;
- int U = startU + j / 2;
- int index = Y * 3;
-
- temp = (int)((src[Y] & 0xff) + 1.4075 * ((src[V] & 0xff) - 128));
- out[index + B] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
-
- temp = (int)((src[Y] & 0xff) - 0.3455* ((src[U] & 0xff) - 128) - 0.7169*((src[V] & 0xff) - 128));
- out[index + G] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
-
- temp = (int)((src[Y] & 0xff) + 1.779* ((src[U] & 0xff) - 128));
- out[index + R] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
- }
- }
- return true;
- }
YUY2转RGB24
- bool ConvertYUY2toRGB24(unsigned char* pOutputRgb,
- unsigned char* pInputBuffer,
- int tInputWidth,
- int tInputHeight)
- {
- if (pInputBuffer == NULL || tInputWidth <= 0 || tInputHeight <= 0)
- return false;
- unsigned char* yuyv = pInputBuffer;
- int z = 0, i = 0, j = 0;
- for (i = 0; i < tInputHeight; i++) {
- for (j = 0; j < tInputWidth; j++) {
- int r, g, b;
- int y, u, v;
-
- if (!z)
- y = yuyv[0] << 8;
- else
- y = yuyv[2] << 8;
- u = yuyv[1] - 128;
- v = yuyv[3] - 128;
-
- r = (y + (359 * v)) >> 8;
- g = (y - (88 * u) - (183 * v)) >> 8;
- b = (y + (454 * u)) >> 8;
- r = (r > 255) ? 255 : ((r < 0) ? 0 : r);
- g = (g > 255) ? 255 : ((g < 0) ? 0 : g);
- b = (b > 255) ? 255 : ((b < 0) ? 0 : b);
-
- *pOutputRgb++ = b;
- *pOutputRgb++ = g;
- *pOutputRgb++ = r;
-
- if (z++) {
- z = 0;
- yuyv += 4;
- }
- }
- }
- return true;
- }
RGB24垂直翻转
- bool HorizontalFlipRGB24(unsigned char* rgb24, int iWidth, int iHeight)
- {
- // 输入参数合法性判断
- if (rgb24 == NULL || iWidth <= 0 || iHeight <= 0) return false;
-
- // 每行图像数据的字节数
- int iLBytes = (iWidth * 24 + 23) / 24 * 3;
-
- // 临时RGB图像指针
- unsigned char *prgbtmp = (unsigned char*)malloc(iLBytes*iHeight);
- if (prgbtmp == NULL) {
- return false;
- }
- else {
- std::memset(prgbtmp, 0, iLBytes*iHeight);
- }
-
- // 每行
- for (int i = 0; i < iHeight; i++)
- {
- // 每列
- for (int j = 0; j < iWidth; j++)
- {
- std::memcpy((prgbtmp + iLBytes * (iHeight - 1 - i) + 3 * (iWidth - 1 - j)), (rgb24 + iLBytes * (iHeight - 1 - i) + 3 * j), 3);
- }
- }
- std::memcpy(rgb24, prgbtmp, iLBytes*iHeight);
- free(prgbtmp);
- return true;
- }
RGB24水平翻转
- bool VerticalRotateRGB24(unsigned char* rgb24, int width, int height)
- {
- if (rgb24 == NULL || width == 0 || height == 0)
- return false;
- unsigned char* rgb = new unsigned char[width * height * 3]; //3表示位24位的视频流
- if (rgb == NULL)
- return false;
- for (int x = 0; x < height; x++)
- {
- std::memcpy(rgb + width * 3 * x, rgb24 + (height - 1 - x) * width * 3, width * 3);
- }
- memcpy(rgb24, rgb, width * height * 3);
- delete[] rgb;
- return true;
- }