1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2009 Canonical |
3 | SPDX-FileContributor: Aurélien Gâteau <aurelien.gateau@canonical.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only |
6 | */ |
7 | |
8 | #include "imageconverter.h" |
9 | |
10 | #include <QDBusArgument> |
11 | #include <QDBusMetaType> |
12 | #include <QImage> |
13 | |
14 | namespace ImageConverter |
15 | { |
16 | /** |
17 | * A structure representing an image which can be marshalled to fit the |
18 | * notification spec. |
19 | */ |
20 | struct SpecImage { |
21 | int width, height, rowStride; |
22 | bool hasAlpha; |
23 | int bitsPerSample, channels; |
24 | QByteArray data; |
25 | }; |
26 | |
27 | QDBusArgument &operator<<(QDBusArgument &argument, const SpecImage &image) |
28 | { |
29 | argument.beginStructure(); |
30 | argument << image.width << image.height << image.rowStride << image.hasAlpha; |
31 | argument << image.bitsPerSample << image.channels << image.data; |
32 | argument.endStructure(); |
33 | return argument; |
34 | } |
35 | |
36 | const QDBusArgument &operator>>(const QDBusArgument &argument, SpecImage &image) |
37 | { |
38 | argument.beginStructure(); |
39 | argument >> image.width >> image.height >> image.rowStride >> image.hasAlpha; |
40 | argument >> image.bitsPerSample >> image.channels >> image.data; |
41 | argument.endStructure(); |
42 | return argument; |
43 | } |
44 | |
45 | } // namespace |
46 | |
47 | // This must be before the QVariant::fromValue below (#211726) |
48 | Q_DECLARE_METATYPE(ImageConverter::SpecImage) |
49 | |
50 | namespace ImageConverter |
51 | { |
52 | QVariant variantForImage(const QImage &_image) |
53 | { |
54 | qDBusRegisterMetaType<SpecImage>(); |
55 | |
56 | const bool hasAlpha = _image.hasAlphaChannel(); |
57 | QImage image; |
58 | if (hasAlpha) { |
59 | image = _image.convertToFormat(f: QImage::Format_RGBA8888); |
60 | } else { |
61 | image = _image.convertToFormat(f: QImage::Format_RGB888); |
62 | } |
63 | |
64 | QByteArray data((const char *)image.constBits(), image.sizeInBytes()); |
65 | |
66 | SpecImage specImage; |
67 | specImage.width = image.width(); |
68 | specImage.height = image.height(); |
69 | specImage.rowStride = image.bytesPerLine(); |
70 | specImage.hasAlpha = hasAlpha; |
71 | specImage.bitsPerSample = 8; |
72 | specImage.channels = hasAlpha ? 4 : 3; |
73 | specImage.data = data; |
74 | |
75 | return QVariant::fromValue(value: specImage); |
76 | } |
77 | |
78 | } // namespace |
79 |