|
@@ -55,33 +55,68 @@ void PlayerGLWidget::convertQImageToYUV420(const QImage& image, Image_YUV420& yu
|
|
|
{
|
|
|
int width = image.width();
|
|
|
int height = image.height();
|
|
|
- int ySize = width * height;
|
|
|
- int uvSize = ySize / 4;
|
|
|
+
|
|
|
+ int frameSize = width * height;
|
|
|
+ int chromaSize = frameSize / 4;
|
|
|
+
|
|
|
+ uint8_t* yuv = new uint8_t[frameSize + chromaSize * 2];
|
|
|
+ uint8_t* yPlane = yuv;
|
|
|
+ uint8_t* uPlane = yuv + frameSize;
|
|
|
+ uint8_t* vPlane = yuv + frameSize + chromaSize;
|
|
|
+
|
|
|
+ auto rgb = image.bits();
|
|
|
+
|
|
|
+ // for (int y = 0; y < height; ++y) {
|
|
|
+ // for (int x = 0; x < width; ++x) {
|
|
|
+ // QColor color = image.pixelColor(x, y);
|
|
|
+ // int r = color.red();
|
|
|
+ // int g = color.green();
|
|
|
+ // int b = color.blue();
|
|
|
+
|
|
|
+ // int yIndex = y * width + x;
|
|
|
+ // yuv420.yData[yIndex] = static_cast<unsigned char>((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
|
|
|
+
|
|
|
+ // if (y % 2 == 0 && x % 2 == 0) {
|
|
|
+ // int uvIndex = (y / 2) * (width / 2) + (x / 2);
|
|
|
+ // yuv420.uData[uvIndex] = static_cast<unsigned char>((-0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
|
|
|
+ // yuv420.vData[uvIndex] = static_cast<unsigned char>((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ for (int j = 0; j < height; j++) {
|
|
|
+ for (int i = 0; i < width; i++) {
|
|
|
+ int r = rgb[(j * width + i) * 3];
|
|
|
+ int g = rgb[(j * width + i) * 3 + 1];
|
|
|
+ int b = rgb[(j * width + i) * 3 + 2];
|
|
|
+
|
|
|
+ // int y = (0.257 * r) + (0.504 * g) + (0.098 * b) + 16;
|
|
|
+ // int u = -(0.148 * r) - (0.291 * g) + (0.439 * b) + 128;
|
|
|
+ // int v = (0.439 * r) - (0.368 * g) - (0.071 * b) + 128;
|
|
|
+
|
|
|
+ int y = (0.299 * r) + (0.587 * g) + (0.114 * b);
|
|
|
+ int u = (-0.169 * r) - (0.331 * g) + (0.5 * b) + 128;
|
|
|
+ int v = (0.5 * r) - (0.419 * g) - (0.081 * b) + 128;
|
|
|
+
|
|
|
+ yPlane[j * width + i] = std::clamp(y, 0, 255);
|
|
|
+
|
|
|
+ if (j % 2 == 0 && i % 2 == 0) {
|
|
|
+ uPlane[(j / 2) * (width / 2) + (i / 2)] = std::clamp(u, 0, 255);
|
|
|
+ vPlane[(j / 2) * (width / 2) + (i / 2)] = std::clamp(v, 0, 255);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
yuv420.width = width;
|
|
|
yuv420.height = height;
|
|
|
+ yuv420.yData.resize(frameSize);
|
|
|
+ yuv420.uData.resize(chromaSize);
|
|
|
+ yuv420.vData.resize(chromaSize);
|
|
|
|
|
|
- yuv420.yData.resize(ySize);
|
|
|
- yuv420.uData.resize(uvSize);
|
|
|
- yuv420.vData.resize(uvSize);
|
|
|
-
|
|
|
- for (int y = 0; y < height; ++y) {
|
|
|
- for (int x = 0; x < width; ++x) {
|
|
|
- QColor color = image.pixelColor(x, y);
|
|
|
- int r = color.red();
|
|
|
- int g = color.green();
|
|
|
- int b = color.blue();
|
|
|
+ yuv420.yData.append(reinterpret_cast<char*>(yPlane), frameSize);
|
|
|
+ yuv420.uData.append(reinterpret_cast<char*>(uPlane), chromaSize);
|
|
|
+ yuv420.vData.append(reinterpret_cast<char*>(vPlane), chromaSize);
|
|
|
|
|
|
- int yIndex = y * width + x;
|
|
|
- yuv420.yData[yIndex] = static_cast<unsigned char>((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
|
|
|
-
|
|
|
- if (y % 2 == 0 && x % 2 == 0) {
|
|
|
- int uvIndex = (y / 2) * (width / 2) + (x / 2);
|
|
|
- yuv420.uData[uvIndex] = static_cast<unsigned char>((-0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
|
|
|
- yuv420.vData[uvIndex] = static_cast<unsigned char>((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
void PlayerGLWidget::initializeGL()
|