@@ -376,41 +376,48 @@ void LibCameraWorker::processCompletedRequest(Request *request) {
376376
377377QImage LibCameraWorker::convertBufferToImage (
378378 const std::map<const Stream *, FrameBuffer *> &buffers) {
379- // Create QImage from RGB888 data (copy data)
380379 QImage image;
381- std::vector<std::pair<void *, size_t >> mappedMemory;
382380
383381 for (const auto &bufferPair : buffers) {
384- // Use framebuffer which has the image data
382+ const Stream *stream = bufferPair. first ;
385383 FrameBuffer *buffer = bufferPair.second ;
386384
387- // Find the size of buffer
388- size_t size = buffer->metadata ().planes ()[0 ].bytesused ;
389385 const FrameBuffer::Plane &plane = buffer->planes ().front ();
390386 void *memory =
391387 mmap (NULL , plane.length , PROT_READ, MAP_SHARED, plane.fd .get (), 0 );
392388
393389 if (memory == MAP_FAILED) {
394390 qDebug () << " [ERROR] Failed to mmap framebuffer memory:"
395391 << strerror (errno);
396- // Unmap any previously mapped memory before returning
397- for (const auto &mapped : mappedMemory) {
398- munmap (mapped.first , mapped.second );
399- }
400-
401392 return QImage ();
402393 }
403394
404- // Track mapped memory for cleanup
405- mappedMemory.push_back ({memory, plane.length });
406-
407- // Load image from a raw buffer into the QImage widget
408- image.loadFromData (static_cast <unsigned char *>(memory), (int )size);
409- }
395+ // Get the stream configuration to know the format
396+ const StreamConfiguration &cfg = stream->configuration ();
397+
398+ if (cfg.pixelFormat == libcamera::formats::RGB888) {
399+ // Raw RGB888 data - create QImage directly from raw pixels
400+ // Note: RGB888 in libcamera is actually BGR in memory order for QImage
401+ QImage temp (static_cast <const uchar *>(memory),
402+ cfg.size .width ,
403+ cfg.size .height ,
404+ cfg.stride ,
405+ QImage::Format_RGB888);
406+ // Make a deep copy since we'll unmap the memory
407+ image = temp.copy ();
408+ } else if (cfg.pixelFormat == libcamera::formats::MJPEG) {
409+ // MJPEG - use loadFromData for encoded formats
410+ size_t size = buffer->metadata ().planes ()[0 ].bytesused ;
411+ image.loadFromData (static_cast <const uchar *>(memory), static_cast <int >(size), " JPEG" );
412+ } else {
413+ qDebug () << " [ERROR] Unsupported pixel format:"
414+ << QString::fromStdString (cfg.pixelFormat .toString ());
415+ }
410416
411- // Unmap all memory to avoid memory leak
412- for (const auto &mapped : mappedMemory) {
413- munmap (mapped.first , mapped.second );
417+ munmap (memory, plane.length );
418+
419+ // Only process first buffer
420+ break ;
414421 }
415422
416423 return image;
0 commit comments