KNotifications

imageconverter.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Canonical
3  SPDX-FileContributor: Aurélien Gâteau <[email protected]>
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) {
60  } else {
61  image = _image.convertToFormat(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(specImage);
76 }
77 
78 } // namespace
qsizetype sizeInBytes() const const
int height() const const
QVariant fromValue(const T &value)
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
bool hasAlphaChannel() const const
QImage convertToFormat(QImage::Format format, Qt::ImageConversionFlags flags) const &const
int bytesPerLine() const const
void beginStructure()
void endStructure()
QDataStream & operator>>(QDataStream &in, KDateTime &dateTime)
const uchar * constBits() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:49:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.