File: imageconverter.cpp

package info (click to toggle)
kde-runtime 4%3A17.08.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,204 kB
  • sloc: cpp: 111,675; ansic: 5,030; perl: 1,579; xml: 793; sh: 407; makefile: 42; python: 28
file content (105 lines) | stat: -rw-r--r-- 2,950 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
   Copyright (C) 2009 Canonical
   Author: Aurélien Gâteau <aurelien.gateau@canonical.com>

   This program is free software: you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 2 or 3 of the License.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
   General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "imageconverter.h"

#include <QDBusArgument>
#include <QDBusMetaType>
#include <QImage>

namespace ImageConverter
{

/**
 * A structure representing an image which can be marshalled to fit the
 * notification spec.
 */
struct SpecImage
{
	int width, height, rowStride;
	bool hasAlpha;
	int bitsPerSample, channels;
	QByteArray data;
};

QDBusArgument &operator<<(QDBusArgument &argument, const SpecImage &image)
{
	argument.beginStructure();
	argument << image.width << image.height << image.rowStride << image.hasAlpha;
	argument << image.bitsPerSample << image.channels << image.data;
	argument.endStructure();
	return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, SpecImage &image)
{
	argument.beginStructure();
	argument >> image.width >> image.height >> image.rowStride >> image.hasAlpha;
	argument >> image.bitsPerSample >> image.channels >> image.data;
	argument.endStructure();
	return argument;
}

} // namespace

// This must be before the QVariant::fromValue below (#211726)
Q_DECLARE_METATYPE(ImageConverter::SpecImage)

namespace ImageConverter
{
QVariant variantForImage(const QImage &_image)
{
	qDBusRegisterMetaType<SpecImage>();

	QImage image = _image.convertToFormat(QImage::Format_ARGB32);

	int rowStride = image.width() * 4;

	// Notification spec stores pixels in R,G,B,A order, regardless of
	// endianess
	// Qt represents pixels as 32 bit unsigned int. So the order depend on
	// endianess:
	// - In big endian the order is A,R,G,B
	// - In little endian the order is B,G,R,A
	QByteArray data;
	data.resize(rowStride * image.height());
	char* dst = data.data();
	for (int y=0; y<image.height(); ++y) {
		QRgb* src = (QRgb*)image.scanLine(y);
		QRgb* end = src + image.width();
		for (;src != end; ++src) {
			// Probably slow, but free of endianess issues
			*dst++ = qRed(*src);
			*dst++ = qGreen(*src);
			*dst++ = qBlue(*src);
			*dst++ = qAlpha(*src);
		}
	}

	SpecImage specImage;
	specImage.width = image.width();
	specImage.height = image.height();
	specImage.rowStride = rowStride;
	specImage.hasAlpha = true;
	specImage.bitsPerSample = 8;
	specImage.channels = 4;
	specImage.data = data;

	return QVariant::fromValue(specImage);
}

} // namespace