|
@@ -0,0 +1,187 @@
|
|
1
|
+/*!
|
|
2
|
+ * \file src/utils/pcx.cpp
|
|
3
|
+ * \brief PCX image reader
|
|
4
|
+ *
|
|
5
|
+ * Based on official technical documentation from ZSoft:
|
|
6
|
+ * http://bespin.org/~qz/pc-gpe/pcx.txt
|
|
7
|
+ *
|
|
8
|
+ * \author xythobuz
|
|
9
|
+ */
|
|
10
|
+
|
|
11
|
+#include <fstream>
|
|
12
|
+
|
|
13
|
+#include "global.h"
|
|
14
|
+#include "utils/pcx.h"
|
|
15
|
+
|
|
16
|
+#include "Console.h"
|
|
17
|
+
|
|
18
|
+#ifdef DEBUG
|
|
19
|
+#define pcxPrint getConsole().print
|
|
20
|
+#else
|
|
21
|
+void pcxPrint(...) { }
|
|
22
|
+#endif
|
|
23
|
+
|
|
24
|
+int pcxLoad(const char *filename, unsigned char **image, unsigned int *width, unsigned int *height) {
|
|
25
|
+ std::ifstream file(filename, std::ios::in | std::ios::binary);
|
|
26
|
+
|
|
27
|
+ // Read raw PCX header, 128 bytes
|
|
28
|
+ unsigned char *header = new unsigned char[128];
|
|
29
|
+
|
|
30
|
+ // Basic validation
|
|
31
|
+ if (!file.read((char *)(&header[0]), 128)) {
|
|
32
|
+ pcxPrint("File not big enough for valid PCX header!");
|
|
33
|
+ delete [] header;
|
|
34
|
+ return -1;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ if (header[0] != 0x0A) {
|
|
38
|
+ pcxPrint("Magic number at file start is wrong (0x%X != 0x0A)", header[0]);
|
|
39
|
+ delete [] header;
|
|
40
|
+ return -2;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ if ((header[1] != 0) && ((header[1] < 2) || (header[1] > 5))) {
|
|
44
|
+ // Valid: 0, 2, 3, 4, 5
|
|
45
|
+ pcxPrint("Unknown PCX file format version (%d)", header[1]);
|
|
46
|
+ delete [] header;
|
|
47
|
+ return -3;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ if ((header[2] != 0) && (header[2] != 1)) {
|
|
51
|
+ pcxPrint("Unknown PCX file encoding (%d)", header[2]);
|
|
52
|
+ delete [] header;
|
|
53
|
+ return -4;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ if (header[3] != 8) {
|
|
57
|
+ pcxPrint("Only supporting 8bit (%dbit)", header[3]);
|
|
58
|
+ delete [] header;
|
|
59
|
+ return -5;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ if (header[64] != 0) {
|
|
63
|
+ pcxPrint("Reserved field is used (%d != 0)", header[64]);
|
|
64
|
+ delete [] header;
|
|
65
|
+ return -6;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ // Read header informations
|
|
69
|
+ bool versionFive = (header[1] == 5);
|
|
70
|
+ bool compressed = (header[2] == 1);
|
|
71
|
+ //unsigned char bitsPerPixel = header[3];
|
|
72
|
+ unsigned int xMin = header[4] | (header[5] << 8);
|
|
73
|
+ unsigned int yMin = header[6] | (header[7] << 8);
|
|
74
|
+ unsigned int xMax = header[8] | (header[9] << 8);
|
|
75
|
+ unsigned int yMax = header[10] | (header[11] << 8);
|
|
76
|
+ //unsigned int hDPI = header[12] | (header[13] << 8);
|
|
77
|
+ //unsigned int vDPI = header[14] | (header[15] << 8);
|
|
78
|
+ //unsigned char *colormap = header + 16;
|
|
79
|
+ unsigned char nPlanes = header[65];
|
|
80
|
+ unsigned int bytesPerLine = header[66] | (header[67] << 8);
|
|
81
|
+ //unsigned int paletteInfo = header[68] | (header[69] << 8);
|
|
82
|
+ //unsigned int hScreenSize = header[70] | (header[71] << 8); // Only in some versionFive files
|
|
83
|
+ //unsigned int vScreenSize = header[72] | (header[73] << 8); // Only in some versionFive files
|
|
84
|
+
|
|
85
|
+ delete [] header;
|
|
86
|
+
|
|
87
|
+ // Calculations
|
|
88
|
+ *width = xMax - xMin + 1;
|
|
89
|
+ *height = yMax - yMin + 1;
|
|
90
|
+ unsigned long totalBytes = nPlanes * bytesPerLine; // total bytes per scan line
|
|
91
|
+ unsigned long imageSize = totalBytes * *height;
|
|
92
|
+ unsigned char *buffer = new unsigned char[imageSize];
|
|
93
|
+ unsigned long b = 0;
|
|
94
|
+
|
|
95
|
+ // Read encoded pixel data
|
|
96
|
+ for (unsigned long i = 0; i < imageSize;) {
|
|
97
|
+ unsigned int n = 1; // Run-length-encoding assumes 1
|
|
98
|
+ int c = file.get();
|
|
99
|
+ if (!file) {
|
|
100
|
+ pcxPrint("Could not read data (%lu%s)", i, (file.eof() ? " EOF" : ""));
|
|
101
|
+ delete buffer;
|
|
102
|
+ return -7;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ // Run-Length-Encoding
|
|
106
|
+ if (compressed) {
|
|
107
|
+ if ((c & 0xC0) == 0xC0) {
|
|
108
|
+ n = c & 0x3F;
|
|
109
|
+ c = file.get();
|
|
110
|
+ if (!file) {
|
|
111
|
+ pcxPrint("Could not read data rle (%lu%s)", i, (file.eof() ? " EOF" : ""));
|
|
112
|
+ return -8;
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ for (unsigned int j = 0; j < n; j++)
|
|
118
|
+ buffer[b++] = (unsigned char)c;
|
|
119
|
+
|
|
120
|
+ i += n;
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ // Read color palette
|
|
124
|
+ unsigned char *palette = NULL;
|
|
125
|
+ if (versionFive) {
|
|
126
|
+ int c = file.get();
|
|
127
|
+ if ((c == 12) && file) {
|
|
128
|
+ palette = new unsigned char[768];
|
|
129
|
+ for (unsigned int i = 0; i < 768; i++) {
|
|
130
|
+ palette[i] = (unsigned char)file.get();
|
|
131
|
+ if (!file) {
|
|
132
|
+ pcxPrint("Could not read 256 color palette (%d)", i);
|
|
133
|
+ return -9;
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ // Bring buffer into preferred format
|
|
140
|
+ unsigned long size = *width * *height * 4;
|
|
141
|
+ *image = new unsigned char[size];
|
|
142
|
+ for (unsigned int y = 0; y < *height; y++) {
|
|
143
|
+ for (unsigned int x = 0; x < *width; x++) {
|
|
144
|
+ unsigned long baseIndex = (x + (y * *width)) * 4;
|
|
145
|
+ unsigned char alpha = 255, red = 0, green = 0, blue = 0;
|
|
146
|
+
|
|
147
|
+ if (palette != NULL) {
|
|
148
|
+ if (nPlanes == 1) {
|
|
149
|
+ red = palette[buffer[(y * totalBytes) + x] * 3];
|
|
150
|
+ green = palette[(buffer[(y * totalBytes) + x] * 3) + 1];
|
|
151
|
+ blue = palette[(buffer[(y * totalBytes) + x] * 3) + 2];
|
|
152
|
+ } else {
|
|
153
|
+ pcxPrint("Unsupported number of planes (%d)", nPlanes);
|
|
154
|
+ delete [] buffer;
|
|
155
|
+ delete [] *image;
|
|
156
|
+ return -10;
|
|
157
|
+ }
|
|
158
|
+ } else {
|
|
159
|
+ if ((nPlanes == 3) || (nPlanes == 4)) {
|
|
160
|
+ red = buffer[(y * totalBytes) + x];
|
|
161
|
+ green = buffer[(y * totalBytes) + *width + x];
|
|
162
|
+ blue = buffer[(y * totalBytes) + (2 * *width) + x];
|
|
163
|
+ if (nPlanes == 4)
|
|
164
|
+ alpha = buffer[(y * totalBytes) + (3 * *width) + x];
|
|
165
|
+ } else if (nPlanes == 1) {
|
|
166
|
+ red = green = blue = buffer[(y * totalBytes) + x];
|
|
167
|
+ } else {
|
|
168
|
+ pcxPrint("Unsupported number of planes (%d)", nPlanes);
|
|
169
|
+ delete [] buffer;
|
|
170
|
+ delete [] *image;
|
|
171
|
+ return -11;
|
|
172
|
+ }
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ (*image)[baseIndex + 0] = red;
|
|
176
|
+ (*image)[baseIndex + 1] = green;
|
|
177
|
+ (*image)[baseIndex + 2] = blue;
|
|
178
|
+ (*image)[baseIndex + 3] = alpha;
|
|
179
|
+ }
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ if (palette != NULL)
|
|
183
|
+ delete [] palette;
|
|
184
|
+
|
|
185
|
+ return 0;
|
|
186
|
+}
|
|
187
|
+
|