File indexing completed on 2024-04-28 11:43:44
0001 /* 0002 SPDX-FileCopyrightText: 2009 Canonical 0003 SPDX-FileContributor: Aurélien Gâteau <aurelien.gateau@canonical.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only 0006 */ 0007 0008 #include "imageconverter.h" 0009 0010 #include <QDBusArgument> 0011 #include <QDBusMetaType> 0012 #include <QImage> 0013 0014 namespace ImageConverter 0015 { 0016 /** 0017 * A structure representing an image which can be marshalled to fit the 0018 * notification spec. 0019 */ 0020 struct SpecImage { 0021 int width, height, rowStride; 0022 bool hasAlpha; 0023 int bitsPerSample, channels; 0024 QByteArray data; 0025 }; 0026 0027 QDBusArgument &operator<<(QDBusArgument &argument, const SpecImage &image) 0028 { 0029 argument.beginStructure(); 0030 argument << image.width << image.height << image.rowStride << image.hasAlpha; 0031 argument << image.bitsPerSample << image.channels << image.data; 0032 argument.endStructure(); 0033 return argument; 0034 } 0035 0036 const QDBusArgument &operator>>(const QDBusArgument &argument, SpecImage &image) 0037 { 0038 argument.beginStructure(); 0039 argument >> image.width >> image.height >> image.rowStride >> image.hasAlpha; 0040 argument >> image.bitsPerSample >> image.channels >> image.data; 0041 argument.endStructure(); 0042 return argument; 0043 } 0044 0045 } // namespace 0046 0047 // This must be before the QVariant::fromValue below (#211726) 0048 Q_DECLARE_METATYPE(ImageConverter::SpecImage) 0049 0050 namespace ImageConverter 0051 { 0052 QVariant variantForImage(const QImage &_image) 0053 { 0054 qDBusRegisterMetaType<SpecImage>(); 0055 0056 const bool hasAlpha = _image.hasAlphaChannel(); 0057 QImage image; 0058 if (hasAlpha) { 0059 image = _image.convertToFormat(QImage::Format_RGBA8888); 0060 } else { 0061 image = _image.convertToFormat(QImage::Format_RGB888); 0062 } 0063 0064 QByteArray data((const char *)image.constBits(), image.sizeInBytes()); 0065 0066 SpecImage specImage; 0067 specImage.width = image.width(); 0068 specImage.height = image.height(); 0069 specImage.rowStride = image.bytesPerLine(); 0070 specImage.hasAlpha = hasAlpha; 0071 specImage.bitsPerSample = 8; 0072 specImage.channels = hasAlpha ? 4 : 3; 0073 specImage.data = data; 0074 0075 return QVariant::fromValue(specImage); 0076 } 0077 0078 } // namespace