| 1 | /* |
| 2 | Copyright (C) 2009 by Aurélien Gâteau <[email protected]> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2, or (at your option) |
| 7 | any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | */ |
| 19 | #include "imageconverter.h" |
| 20 | |
| 21 | #include <QDBusArgument> |
| 22 | #include <QDBusMetaType> |
| 23 | #include <QImage> |
| 24 | |
| 25 | namespace ImageConverter |
| 26 | { |
| 27 | |
| 28 | /** |
| 29 | * A structure representing an image which can be marshalled to fit the |
| 30 | * notification spec. |
| 31 | */ |
| 32 | struct SpecImage |
| 33 | { |
| 34 | int width, height, rowStride; |
| 35 | bool hasAlpha; |
| 36 | int bitsPerSample, channels; |
| 37 | QByteArray data; |
| 38 | }; |
| 39 | |
| 40 | QDBusArgument &operator<<(QDBusArgument &argument, const SpecImage &image) |
| 41 | { |
| 42 | argument.beginStructure(); |
| 43 | argument << image.width << image.height << image.rowStride << image.hasAlpha; |
| 44 | argument << image.bitsPerSample << image.channels << image.data; |
| 45 | argument.endStructure(); |
| 46 | return argument; |
| 47 | } |
| 48 | |
| 49 | const QDBusArgument &operator>>(const QDBusArgument &argument, SpecImage &image) |
| 50 | { |
| 51 | argument.beginStructure(); |
| 52 | argument >> image.width >> image.height >> image.rowStride >> image.hasAlpha; |
| 53 | argument >> image.bitsPerSample >> image.channels >> image.data; |
| 54 | argument.endStructure(); |
| 55 | return argument; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | // This must be before the QVariant::fromValue below (#211726) |
| 61 | Q_DECLARE_METATYPE(ImageConverter::SpecImage) |
| 62 | |
| 63 | namespace ImageConverter |
| 64 | { |
| 65 | QVariant variantForImage(const QImage &_image) |
| 66 | { |
| 67 | qDBusRegisterMetaType<SpecImage>(); |
| 68 | |
| 69 | QImage image = _image.convertToFormat(QImage::Format_ARGB32); |
| 70 | |
| 71 | int rowStride = image.width() * 4; |
| 72 | |
| 73 | // Notification spec stores pixels in R,G,B,A order, regardless of |
| 74 | // endianess |
| 75 | // Qt represents pixels as 32 bit unsigned int. So the order depend on |
| 76 | // endianess: |
| 77 | // - In big endian the order is A,R,G,B |
| 78 | // - In little endian the order is B,G,R,A |
| 79 | QByteArray data; |
| 80 | data.resize(rowStride * image.height()); |
| 81 | char* dst = data.data(); |
| 82 | for (int y=0; y<image.height(); ++y) { |
| 83 | QRgb* src = (QRgb*)image.scanLine(y); |
| 84 | QRgb* end = src + image.width(); |
| 85 | for (;src != end; ++src) { |
| 86 | // Probably slow, but free of endianess issues |
| 87 | *dst++ = qRed(*src); |
| 88 | *dst++ = qGreen(*src); |
| 89 | *dst++ = qBlue(*src); |
| 90 | *dst++ = qAlpha(*src); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | SpecImage specImage; |
| 95 | specImage.width = image.width(); |
| 96 | specImage.height = image.height(); |
| 97 | specImage.rowStride = rowStride; |
| 98 | specImage.hasAlpha = true; |
| 99 | specImage.bitsPerSample = 8; |
| 100 | specImage.channels = 4; |
| 101 | specImage.data = data; |
| 102 | |
| 103 | return QVariant::fromValue(specImage); |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 |