forked from roniemartinez/libqpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpcxhandler.cpp
547 lines (496 loc) · 18.9 KB
/
qpcxhandler.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
Copyright (c) 2013 Ronie Martinez ([email protected])
All rights reserved.
This library 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.1 of the License, or (at your option) any later version.
This library 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 library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "qpcxhandler.h"
#include <QDebug>
bool QPcxHandler::readPCX(QByteArray data, QImage *image)
{
QDataStream input(data);
if (data.isNull())
input.setDevice(device());
input.setByteOrder(QDataStream::LittleEndian);
quint8 manufacturer, version, encoding, bitsPerPixel,
reserved, colorPlanes;
quint16 xMin, yMin, xMax, yMax, verticalDPI,
horizontalDPI, bytesPerPlaneLine,
paletteType, horizontalScreenSize, verticalScreenSize;
input >> manufacturer;
if (manufacturer != 0x0A)
return false;
input >> version;
input >> encoding;
if (encoding != 0)
if (encoding != 1)
return false;
//Number of bits per pixel in each colour plane (1, 2, 4, 8, 24)
input >> bitsPerPixel;
switch (bitsPerPixel) {
case 1:
case 2:
case 4:
case 8:
case 24:
break;
default: return false;
break;
}
input >> xMin >> yMin >> xMax >> yMax;
int width = xMax - xMin + 1;
int height = yMax - yMin + 1;
input >> verticalDPI >> horizontalDPI;
//if bitsPerPixel <= 4 palette is present here
quint8 byte;
QByteArray colorMap;
for (int i = 0; i<48; ++i) {
input >> byte;
colorMap.append(byte);
}
// https://www.fileformat.info/format/pcx/egff.htm : Reserved1
// is not currently used and should have a value of 00h. Older
// versions of PCX used this field for file identification or
// to hold the mode value of the display screen on which the
// PCX image was created.
input >> reserved;
//observed that pcx file format was badly designed since
//the number of color planes was placed here and not before the
//palette. it is needed for 16 color.
input >> colorPlanes;
input >> bytesPerPlaneLine;
/* encountered uneven bytesPerPlaneLine so I decided not to check
* it if it is an even number or not
if (bytesPerPlaneLine%2 != 0)
return false;
*/
int fillerCount;
input >> paletteType;
if (version >= 4) {
fillerCount = 54;
input >> horizontalScreenSize >> verticalScreenSize;
} else {
fillerCount = 58;
}
input.skipRawData(fillerCount);
/*DO NOT REMOVE: Intended for testing (for developers ONLY)
qDebug() << "manufacturer: " << manufacturer << endl
<< "version: " << version << endl
<< "encoding: " << encoding << endl
<< "bitsPerPixel: " << bitsPerPixel << endl
<< "reserved: " << reserved << endl
<< "colorPlanes: " << colorPlanes << endl
<< "xMin: " << xMin << endl
<< "yMin: " << yMin << endl
<< "xMax: " << xMax << endl
<< "yMax: " << yMax << endl
<< "verticalDPI: " << verticalDPI << endl
<< "horizontalDPI: " << horizontalDPI << endl
<< "bytesPerPlaneLine: " << bytesPerPlaneLine << endl
<< "paletteType: " << paletteType << endl
<< "horizontalScreenSize: " << horizontalScreenSize << endl
<< "verticalScreenSize: " << verticalScreenSize << endl;
*/
QByteArray imageData;
int scanLineLength = bytesPerPlaneLine * colorPlanes;
int bytesPerLine;
quint8 count;
for(int lineCount=0; lineCount < height; ++lineCount)
{
bytesPerLine = scanLineLength;
switch (encoding) {
case 0: //uncompressed image
do {
input >> byte;
imageData.append(byte);
--bytesPerLine;
} while (bytesPerLine != 0);
break;
case 1: //RLE decoding of one line
do {
input >> byte;
if ((byte & 0xC0) == 0xC0) {
count = byte & 0x3F;
input >> byte;
} else {
count = 1;
}
//Write the pixel run to the buffer
for (int j=0; j < count; j++) {
imageData.append(byte);
--bytesPerLine;
}
if (bytesPerLine < 0)
return false;
} while(bytesPerLine != 0);
break;
}
}
switch (bitsPerPixel*colorPlanes) {
case 1:
if (colorPlanes == 1 && bitsPerPixel == 1) {
m_result = new QImage(width, height, QImage::Format_Mono);
// ugly implementation because can't put imageData directly to
// the image since line padding exists at odd width
quint8* byte = (quint8*)imageData.constData();
quint8 mask;
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine*8; j+=8) {
mask = 0x80;
for (int k = 0; k < 8; ++k) {
if (j+k < width)
m_result->setPixel(j+k, i, (*byte & mask) == mask);
mask >>= 1;
}
++byte;
}
}
*image = *m_result;
return true;
}
break;
case 4:
if (colorPlanes == 4 && bitsPerPixel == 1) {
m_result = new QImage(width, height, QImage::Format_Indexed8);
//color table
/* http://nemesis.lonestar.org/reference/video/cga.html
Color(s)* (I) (R) (G) (B)
Black OFF OFF OFF OFF
Blue OFF OFF OFF ON
Green OFF OFF ON OFF
Cyan (Blue/Green) OFF OFF ON ON
Red OFF ON OFF OFF
Magenta/Purple (Red/Blue) OFF ON OFF ON
Brown/Orange/Yellow (Red/Green) OFF ON ON OFF
White/Gray (Red/Green/Blue) OFF ON ON ON
Black/Gray (Intensity) ON OFF OFF OFF
Bright Blue (Blue/Intensity) ON OFF OFF ON
Bright Green (Green/Intensity) ON OFF ON OFF
Bright Cyan (Blue/Green/Intensity) ON OFF ON ON
Bright Red (Red/Intensity) ON ON OFF OFF
Bright Magenta/Pink/Bright Purple (Red/Blue/Intensity) ON ON OFF ON
Bright Brown/Bright Orange/ Bright Yellow (Red/Green/Intensity) ON ON ON OFF
Bright White (Red/Green/Blue/Intensity) ON ON ON ON
http://en.wikipedia.org/wiki/Color_Graphics_Adapter
Full CGA 16-color palette
0 black 8 gray
#000000 #555555
1 blue 9 light blue
#0000AA #5555FF
2 green 10 light green
#00AA00 #55FF55
3 cyan 11 light cyan
#00AAAA #55FFFF
4 red 12 light red
#AA0000 #FF5555
5 magenta 13 light magenta
#AA00AA #FF55FF
6 brown 14 yellow
#AA5500 #FFFF55
7 light gray 15 white (high intensity)
#AAAAAA #FFFFFF
*/
m_result->setColor(0, qRgb(0,0,0));
m_result->setColor(1, qRgb(0,0,170));
m_result->setColor(2, qRgb(0,170,0));
m_result->setColor(3, qRgb(0,170,170));
m_result->setColor(4, qRgb(170,0,0));
m_result->setColor(5, qRgb(170,0,170));
m_result->setColor(6, qRgb(170,85,0));
m_result->setColor(7, qRgb(170,170,0170));
m_result->setColor(8, qRgb(85,85,85));
m_result->setColor(9, qRgb(85,85,255));
m_result->setColor(10, qRgb(85,255,85));
m_result->setColor(11, qRgb(85,255,255));
m_result->setColor(12, qRgb(255,85,85));
m_result->setColor(13, qRgb(255,85,255));
m_result->setColor(14, qRgb(255,255,85));
m_result->setColor(15, qRgb(255,255,255));
//image data
quint8* red = (quint8*)imageData.constData();
quint8* green = red + bytesPerPlaneLine;
quint8* blue = green + bytesPerPlaneLine;
quint8* intensity = blue + bytesPerPlaneLine;
quint8 mask;
quint8 colorIndex;
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine*8; j+=8) {
mask = 0x80;
for (int k = 0; k < 8; ++k) {
colorIndex = 0;
if (*red & mask)
colorIndex |= 0x01;
if (*green & mask)
colorIndex |= 0x02;
if (*blue & mask)
colorIndex |= 0x04;
if (*intensity & mask)
colorIndex |= 0x08;
if (j+k < width)
m_result->setPixel(j+k, i, colorIndex);
mask >>= 1;
}
++red; ++green; ++blue; ++intensity;
}
red += scanLineLength - bytesPerPlaneLine;
green += scanLineLength - bytesPerPlaneLine;
blue += scanLineLength - bytesPerPlaneLine;
intensity += scanLineLength - bytesPerPlaneLine;
}
*image = *m_result;
return true;
} else if (colorPlanes == 1 && bitsPerPixel == 4) { //untested
m_result = new QImage(width, height, QImage::Format_Indexed8);
/* For one plane of four bits (16-colour), each byte will
* represent two pixels. The bits within the byte are in
* big-endian order, so the most significant bit belongs
* to the left-most pixel. In other words, a byte of value
* 0xE4 (binary 11 10 01 00) will have left-to-right pixel
* values of 3, 2, 1, 0, assuming two bits per pixel. */
m_result->setColor(0, qRgb(0,0,0));
m_result->setColor(1, qRgb(0,0,170));
m_result->setColor(2, qRgb(0,170,0));
m_result->setColor(3, qRgb(0,170,170));
m_result->setColor(4, qRgb(170,0,0));
m_result->setColor(5, qRgb(170,0,170));
m_result->setColor(6, qRgb(170,85,0));
m_result->setColor(7, qRgb(170,170,0170));
m_result->setColor(8, qRgb(85,85,85));
m_result->setColor(9, qRgb(85,85,255));
m_result->setColor(10, qRgb(85,255,85));
m_result->setColor(11, qRgb(85,255,255));
m_result->setColor(12, qRgb(255,85,85));
m_result->setColor(13, qRgb(255,85,255));
m_result->setColor(14, qRgb(255,255,85));
m_result->setColor(15, qRgb(255,255,255));
quint8* byte = (quint8*)imageData.constData();
quint8 mask;
quint8 colorIndex;
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine*2; j+=2) {
mask = 0x80;
for (int k = 0; k < 2; ++k) {
if (k == 0) {
colorIndex = *byte & mask;
mask >>= 4;
} else {
colorIndex = *byte & mask;
}
if (j+k < width)
m_result->setPixel(j+k, i, colorIndex);
mask >>= 4;
}
++byte;
}
}
*image = *m_result;
return true;
}
break;
case 24:
if (bitsPerPixel == 8 && colorPlanes == 3) {
m_result = new QImage(width, height, QImage::Format_RGB32);
quint8 *red = (quint8*)imageData.constData();
quint8 *green = red + bytesPerPlaneLine;
quint8 *blue = green + bytesPerPlaneLine;
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine; ++j) {
if (j < width)
m_result->setPixel(j,i,qRgb(*red, *green, *blue));
++red; ++green, ++blue;
}
red += scanLineLength - bytesPerPlaneLine;
green += scanLineLength - bytesPerPlaneLine;
blue += scanLineLength - bytesPerPlaneLine;
}
*image = *m_result;
return true;
}
break;
case 32:
if (bitsPerPixel == 8 && colorPlanes == 4) { //untested
m_result = new QImage(width, height, QImage::Format_ARGB32);
quint8 *red = (quint8*)imageData.constData();
quint8 *green = red + bytesPerPlaneLine;
quint8 *blue = green + bytesPerPlaneLine;
quint8 *alpha = blue + bytesPerPlaneLine;
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine; ++j) {
if (j < width)
m_result->setPixel(j,i,qRgba(*red, *green, *blue, *alpha));
++red; ++green, ++blue; ++alpha;
}
red += scanLineLength - bytesPerPlaneLine;
green += scanLineLength - bytesPerPlaneLine;
blue += scanLineLength - bytesPerPlaneLine;
alpha += scanLineLength - bytesPerPlaneLine;
}
*image = *m_result;
return true;
}
break;
default:
if (input.atEnd())
return false;
break;
}
// if palette exists at the end of the file
input >> byte;
if (version == 2 ||
(version == 5 && bitsPerPixel == 8 && paletteType == 1))
if (byte != 12) //should be 12 as described in the documentation
return false;
// If palette exists at the end of the file
QByteArray colorData;
while (!input.atEnd()) {
input >> byte;
colorData.append(byte);
}
if (bitsPerPixel == 8 && colorPlanes == 1) {
switch (paletteType) {
case 1: // Color/B&W
{
m_result = new QImage(width, height, QImage::Format_Indexed8);
if (colorData.size() != 768)
return false;
int indexCount = colorData.size() / 3;
quint8 *red = (quint8*)colorData.constData();
quint8 *green = red + 1;
quint8 *blue = green + 1;
for (int i=0; i < indexCount; ++i) {
m_result->setColor(i, qRgb(*red, *green, *blue));
red+=3; green+=3; blue+=3;
}
quint8 *data = (quint8*)imageData.constData();
for (int i=0; i < height; ++i) {
for (int j=0; j < bytesPerPlaneLine; ++j) {
if (j < width)
m_result->setPixel(j,i,*data);
++data;
}
}
*image = *m_result;
return true;
} break;
case 2: // grayscale, no samples found / undocumented
break;
default: return false;
break;
}
}
return false;
}
QPcxHandler::QPcxHandler()
:m_currentImageNo(0), m_numImages(0)
{
}
QPcxHandler::~QPcxHandler()
{
}
bool QPcxHandler::canRead() const
{
if (canRead(device())) {
QByteArray signature = device()->peek(4);
if (signature == QByteArray("\x3A\xDE\x68\xB1")) { //DCX
setFormat("dcx");
return true;
} else if (signature.startsWith("\x0A")) {//PCX
setFormat("pcx");
return true;
} else return false;
}
return false;
}
bool QPcxHandler::canRead(QIODevice *device)
{
QByteArray signature = device->peek(4);
if (signature == QByteArray("\x3A\xDE\x68\xB1")) //DCX
return true;
else if (signature.startsWith("\x0A")) //PCX
return true;
return false;
}
bool QPcxHandler::write(const QImage &image)
{
Q_UNUSED(image);
return false;
}
bool QPcxHandler::read(QImage *image)
{
if (format() == "dcx") {
QDataStream input(device());
input.setByteOrder(QDataStream::LittleEndian);
quint32 signature, offset;
input >> signature;
if (signature != 0x3ADE68B1)
return true;
QVector<quint32> offsetVector;
do {
input >> offset;
offsetVector << offset;
if (offset != 0)
++m_numImages;
} while (offset != 0);
device()->reset();
input.skipRawData(offsetVector.at(m_currentImageNo));
QByteArray data;
quint8 byte;
int size = offsetVector.at(m_currentImageNo+1) -
offsetVector.at(m_currentImageNo);
while (data.size() != size) {
input >> byte;
data.append(byte);
}
return readPCX(data, image);
//return input.status() == QDataStream::Ok;
} else if (format() == "pcx") {
if (readPCX(QByteArray(), image))
return true;
}
return false;
}
int QPcxHandler::currentImageNumber() const
{
return m_currentImageNo;
}
int QPcxHandler::imageCount() const
{
return m_numImages;
}
bool QPcxHandler::jumpToImage(int imageNumber)
{
if (format() == "pcx")
return false;
m_currentImageNo = imageNumber;
QImage image;
return read(&image);
}
bool QPcxHandler::jumpToNextImage()
{
if (m_currentImageNo < imageCount())
++m_currentImageNo;
QImage image;
return read(&image);
}
QVariant QPcxHandler::option(ImageOption option) const
{
Q_UNUSED(option);
return QVariant();
}
bool QPcxHandler::supportsOption(ImageOption option) const
{
if (format() == "dcx")
return option == IncrementalReading;
return false;
}