summaryrefslogtreecommitdiff
path: root/saltoqd/msolimageiohandler.cpp
blob: bd0b23cd285319171c8331be920d021ca39d3f61 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <QtCore/QtEndian>
#include <QtCore/QVariant>
#include <QtCore/QDebug>
#include <QtGui/QImage>

#include "msolimageiohandler.h"

static const int header_size = 16;

namespace
{

static inline uchar encode_color(QRgb c)
{
	return (qRed(c) & 0xC0) | ((qBlue(c) & 0xC0) >> 2) | ((qGreen(c) & 0xC0) >> 4) | 3;
}

static inline QRgb decode_color(uchar p, uchar a = 255)
{
	return qRgba(p & 0xC0, (p & 0x30) << 2, (p & 0x0C) << 4, a);
}

}

MSOLImageIOHandler::MSOLImageIOHandler() : QImageIOHandler()
{
}

bool MSOLImageIOHandler::canRead() const
{
	char header[header_size];
	if (device()->peek(header, header_size) != header_size) {
		return false;
	}

	return strncmp(&header[0], "MSOL  ", 6) == 0;
}

bool MSOLImageIOHandler::read(QImage *image)
{
	QIODevice *dev = device();

	uchar header[header_size];
	if (dev->read(reinterpret_cast<char*>(header), header_size) != header_size) {
		return false;
	}

	if (strncmp(reinterpret_cast<char*>(&header[0]), "MSOL  ", 6) != 0) {
		return false;
	}

	const bool alpha = header[6];
	const int width = qFromLittleEndian<quint16>(&header[8]);
	const int height = qFromLittleEndian<quint16>(&header[10]);
	const int pixel_size = alpha ? 2 : 1;
	const int line_size = width * pixel_size;

	if (alpha) {
		*image = QImage(width, height, QImage::Format_ARGB32);
	} else {
		*image = QImage(width, height, QImage::Format_RGB32);
	}

	Q_ASSERT(image->depth() == sizeof(QRgb) * 8);

	for (int y = 0; y < height; y++) {
		QRgb *dst = reinterpret_cast<QRgb*>(image->scanLine(y));
		uchar src[line_size];
		if (dev->read(reinterpret_cast<char*>(src), line_size) != line_size) {
			qDebug() << "Could not read image line" << y;
			return false;
		}
		for (int x = 0; x < width; x++) {
			if (alpha) {
				dst[x] = decode_color(src[x * pixel_size]);
			} else {
				dst[x] = decode_color(src[x * pixel_size], src[(x * pixel_size) + 1]);
			}
		}
	}

	return true;
}

bool MSOLImageIOHandler::write(const QImage &image)
{
	QIODevice *dev = device();
	const int height = image.height();
	const int width = image.width();
	const bool alpha = image.hasAlphaChannel();
	const int pixel_size = alpha ? 2 : 1;
	const int line_size = width * pixel_size;

	QImage img = image.convertToFormat(alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);

	uchar header[header_size] = { 0 };
	strcpy(reinterpret_cast<char*>(&header[0]), "MSOL  ");
	if (alpha) header[6] = 0x80;
	header[7] = 8;

	qToLittleEndian<quint16>(width, &header[8]);
	qToLittleEndian<quint16>(height, &header[10]);

	if (dev->write(reinterpret_cast<char*>(header), header_size) != header_size) {
		qDebug() << "Could not write header";
		return false;
	}

	Q_ASSERT(img.depth() == sizeof(QRgb) * 8);

	for (int y = 0; y < height; y++) {
		uchar dst[line_size];
		const QRgb *src = reinterpret_cast<const QRgb*>(img.constScanLine(y));
		for (int x = 0; x < width; x++) {
			dst[x * pixel_size] = encode_color(src[x]);
			if (alpha) {
				dst[(x * pixel_size) + 1] = qAlpha(src[x]);
			}
		}
		if (dev->write(reinterpret_cast<char*>(dst), line_size) != line_size) {
			qDebug() << "Could not write image line " << y;
			return false;
		}
	}

	return true;
}

bool MSOLImageIOHandler::supportsOption(ImageOption option) const
{
	switch (option) {
	case Size:
		return true;
	default:
		return false;
	}
}

QVariant MSOLImageIOHandler::option(ImageOption option) const
{
	char header[header_size];

	switch (option) {
	case Size:
		if (device()->peek(header, header_size) == header_size) {
			const QSize size(qFromLittleEndian<quint16>(reinterpret_cast<uchar*>(&header[8])),
							 qFromLittleEndian<quint16>(reinterpret_cast<uchar*>(&header[10])));
			return QVariant::fromValue(size);
		} else {
			qDebug() << "Cannot read from device: " << device()->errorString();
		}
	default:
		break;
	}

	return QVariant();
}

void MSOLImageIOHandler::setOption(ImageOption option, const QVariant &value)
{

}