@@ -540,27 +540,31 @@ QImage LibCameraWorker::convertBufferToImage(
540540 }
541541 }
542542 } else if (cfg.pixelFormat == libcamera::formats::YUV420) {
543- // YUV420 planar format - Pi camera uses I420 (Y, U, V order)
544- // But colors appear swapped, so this might be YV12 (Y, V, U order)
543+ // YUV420 planar format
545544 unsigned int width = cfg.size .width ;
546545 unsigned int height = cfg.size .height ;
547546 unsigned int stride = cfg.stride ;
547+
548+ // Use Format_RGB888 - byte order is R, G, B
548549 image = QImage (width, height, QImage::Format_RGB888);
549550
550551 const uint8_t *src = static_cast <const uint8_t *>(memory);
551552 const uint8_t *yPlane = src;
552- // Swap U and V planes - Pi outputs Y, V, U (YV12) not Y, U, V (I420)
553+
554+ // Try standard I420 order first: Y, U, V
553555 const uint8_t *uPlane = src + stride * height;
554556 const uint8_t *vPlane = uPlane + (stride / 2 ) * (height / 2 );
555557
558+ qDebug () << " [DEBUG] YUV420 conversion: width=" << width << " height=" << height << " stride=" << stride;
559+
556560 for (unsigned int y = 0 ; y < height; y++) {
557561 uint8_t *destRow = image.scanLine (y);
558562 for (unsigned int x = 0 ; x < width; x++) {
559563 int yVal = yPlane[y * stride + x];
560564 int uVal = uPlane[(y / 2 ) * (stride / 2 ) + (x / 2 )];
561565 int vVal = vPlane[(y / 2 ) * (stride / 2 ) + (x / 2 )];
562566
563- // YUV to RGB conversion
567+ // YUV to RGB conversion (BT.601)
564568 int c = yVal - 16 ;
565569 int d = uVal - 128 ;
566570 int e = vVal - 128 ;
@@ -574,11 +578,14 @@ QImage LibCameraWorker::convertBufferToImage(
574578 g = g < 0 ? 0 : (g > 255 ? 255 : g);
575579 b = b < 0 ? 0 : (b > 255 ? 255 : b);
576580
577- destRow[x * 3 + 0 ] = static_cast <uint8_t >(r);
581+ // RGB888 format: B at offset 0, G at offset 1, R at offset 2
582+ destRow[x * 3 + 0 ] = static_cast <uint8_t >(b);
578583 destRow[x * 3 + 1 ] = static_cast <uint8_t >(g);
579- destRow[x * 3 + 2 ] = static_cast <uint8_t >(b );
584+ destRow[x * 3 + 2 ] = static_cast <uint8_t >(r );
580585 }
581586 }
587+
588+ qDebug () << " [DEBUG] YUV420 conversion complete, image size:" << image.size ();
582589 } else {
583590 qDebug () << " [ERROR] Unsupported pixel format:"
584591 << QString::fromStdString (cfg.pixelFormat .toString ());
0 commit comments