File indexing completed on 2024-04-14 15:33:30

0001 /*
0002     SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "pwhelpers.h"
0008 #include "logging.h"
0009 
0010 QImage::Format SpaToQImageFormat(quint32 format)
0011 {
0012     switch (format) {
0013     case SPA_VIDEO_FORMAT_BGRx:
0014     case SPA_VIDEO_FORMAT_BGRA:
0015         return QImage::Format_RGBA8888_Premultiplied; // TODO: Add BGR to QImage
0016     case SPA_VIDEO_FORMAT_BGR:
0017         return QImage::Format_BGR888;
0018     case SPA_VIDEO_FORMAT_RGBx:
0019         return QImage::Format_RGBX8888;
0020     case SPA_VIDEO_FORMAT_RGB:
0021         return QImage::Format_RGB888;
0022     case SPA_VIDEO_FORMAT_RGBA:
0023         return QImage::Format_RGBA8888_Premultiplied;
0024     default:
0025         qCWarning(PIPEWIRE_LOGGING) << "unknown spa format" << format;
0026         return QImage::Format_RGB32;
0027     }
0028 }
0029 
0030 QImage PWHelpers::SpaBufferToQImage(const uchar *data, int width, int height, qsizetype bytesPerLine, spa_video_format format)
0031 {
0032     switch (format) {
0033     case SPA_VIDEO_FORMAT_BGRx:
0034     case SPA_VIDEO_FORMAT_BGRA: {
0035         // This is needed because QImage does not support BGRA
0036         // This is obviously a much slower path, it makes sense to avoid it as much as possible
0037         return QImage(data, width, height, bytesPerLine, SpaToQImageFormat(format)).rgbSwapped();
0038     }
0039     case SPA_VIDEO_FORMAT_BGR:
0040     case SPA_VIDEO_FORMAT_RGBx:
0041     case SPA_VIDEO_FORMAT_RGB:
0042     case SPA_VIDEO_FORMAT_RGBA:
0043     default:
0044         return QImage(data, width, height, bytesPerLine, SpaToQImageFormat(format));
0045     }
0046 }