-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.cc
259 lines (211 loc) · 8.6 KB
/
decoder.cc
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
//
// decoder.cc
//
// Created by Erick Li on 04/11/19.
// Copyright © 2019 Erick Li. All rights reserved.
//
#include "decoder.hh"
std::string grandDecoder(char *head, char *tail, bool enableLogging) {
// Initialize a jstring (for JSON string)
std::string jstring = "";
char **cursor = &head;
// Underlying buffer.
std::streambuf* orig_buf;
// If logging is disabled, then we need to close clog for logging.
if (!enableLogging) {
// Get underlying buffer
orig_buf = std::clog.rdbuf();
// Set NULL
std::clog.rdbuf(NULL);
}
// bytesHopper(cursor, num_of_bytes - 1);
// **cursor = 0x99; // Indicate the end of the block
// bytesRewinder(cursor, num_of_bytes - 1);
write_to_json(jstring, "", "{", 0);
try {
int grand_stnl = get16Bit(cursor);
if (0xE603 == grand_stnl || (0xE606 <= grand_stnl && grand_stnl <= 0xE609)) {
grand_stnl = 0x0000; // this is a special code for fill symbol without header...
}
bytesRewinder(cursor, 2);
switch (grand_stnl) {
case 0xE604:
// Fill Symbol
write_to_json(jstring, "symbolType", "\"fill\",", 1);
LOG("START decoding fill symbol...");
decodeFillPattern(cursor, jstring, 1, &tail);
break;
case 0x0000:
// Fill Symbol without header
write_to_json(jstring, "symbolType", "\"fill\",", 1);
LOG("START decoding fill symbol...");
decodeFillPattern(cursor, jstring, 1, &tail);
break;
case 0xE5FA:
// line symbol
write_to_json(jstring, "symbolType", "\"line\",", 1);
LOG("START decoding line symbol...");
decodeLinePattern(cursor, jstring, 1, 1, "");
break;
case 0xE5FF:
// marker symbol
write_to_json(jstring, "symbolType", "\"marker\",", 1);
LOG("START decoding marker symbol...");
decodeMarkerPattern(cursor, jstring, 1);
break;
default:
LOG("ERROR: Cannot recognize header...");
throw std::string("Symbol header.");
}
} catch (std::string err) {
LOG("ERROR occurred. Stopped...");
return std::string("\"error\": \"" + err + "\"\n");
}
write_to_json(jstring, "", "}", 0);
LOG("FINISHED decoding :-)");
// However, we still want the results being output, don't we?
if (!enableLogging) {
std::clog.rdbuf(orig_buf);
}
return json_comma_remover(jstring);
}
void decodeColorPattern(char **cursor, std::string &jstring, int level, std::string color_type) {
LOG("START decoding color...");
// Get the color space (0x92 for HSV; 0x96 for RGB; 0x97 for CMYK)
int color_space = get16Bit(cursor);
write_to_json(jstring, "color_" + _toCamelCaseSimple(color_type), "{", level);
write_to_json(jstring, "name", "\"" + color_type + "\",", level + 1);
// CMYK color space...
if (0xC497 == color_space) {
LOG("Color Space: CMYK.");
// Skip the metadata of the color space.
bytesHopper(cursor, 18);
int c = getChar(cursor);
int m = getChar(cursor);
int y = getChar(cursor);
int k = getChar(cursor);
LOG("C: " + std::to_string(c));
LOG("M: " + std::to_string(m));
LOG("Y: " + std::to_string(y));
LOG("K: " + std::to_string(k));
write_to_json(jstring, "space", "\"CMYK\",", level + 1);
write_to_json(jstring, "colorCode", "[", level + 1);
write_to_json(jstring, "", std::to_string(c) + ",", level + 2);
write_to_json(jstring, "", std::to_string(m) + ",", level + 2);
write_to_json(jstring, "", std::to_string(y) + ",", level + 2);
write_to_json(jstring, "", std::to_string(k) + ",", level + 2);
write_to_json(jstring, "", "],", level + 1);
} else {
std::list<double> mycolor;
bytesHopper(cursor, 19);
if (0xC492 == color_space) {
write_to_json(jstring, "space", "\"HSV\",", level + 1);
} else if (0xC496 == color_space) {
write_to_json(jstring, "space", "\"RGB\",", level + 1);
}
// There are three fields for both HSV and RGB.
// Type and the definition are unknown so far.
// HSV and RGB share the same coding philsophy.
write_to_json(jstring, "rawColorCode", "[", level + 1);
double first = decodeDouble(cursor, jstring, level + 2, "");
double second = decodeDouble(cursor, jstring, level + 2, "");
double third = decodeDouble(cursor, jstring, level + 2, "");
write_to_json(jstring, "", "],", level + 1);
// HSV color space...
if (0xC492 == color_space) {
LOG("Color Space: HSV.");
mycolor = CIELAB_to_RGB_HSV(first, second, third, USE_HSV);
// RGB color space...
} else if (0xC496 == color_space) {
LOG("Color Space: RGB.");
mycolor = CIELAB_to_RGB_HSV(first, second, third, USE_RGB);
} else {
// If the color space code is not 92, 96, or 97, then an error mesg will be printed out.
LOG("ERROR: Color Space " + std::to_string(color_space) + " not found.");
throw std::string("Color Space.");
}
double fs = mycolor.front();
mycolor.pop_front();
double sd = mycolor.front();
mycolor.pop_front();
double td = mycolor.front();
mycolor.pop_front();
LOG("Converted color");
LOG("1: " + std::to_string(fs));
LOG("2: " + std::to_string(sd));
LOG("3: " + std::to_string(td));
write_to_json(jstring, "colorCode", "[", level + 1);
write_to_json(jstring, "", std::to_string(fs) + ",", level + 2);
write_to_json(jstring, "", std::to_string(sd) + ",", level + 2);
write_to_json(jstring, "", std::to_string(td) + ",", level + 2);
write_to_json(jstring, "", "],", level + 1);
}
write_to_json(jstring, "", "},", level);
bytesHopper(cursor, 2);
LOG("FINISHED decoding color :-)");
}
int decodeLayerNumber(char **cursor, std::string &jstring, int level) {
LOG("START decoding layer number...");
// Number of layers.
int num_of_layers = getChar(cursor);
if (0 >= num_of_layers) {
LOG("ERROR: Number of layers is negative");
throw std::string("number of layers.");
}
LOG("The number of layer is: " + std::to_string(num_of_layers));
// Check whether the layer number is normal...
if (10 < num_of_layers) {
LOG("WARNING: Layer number is abnormal.");
}
write_to_json(jstring, "numberOfLayers", std::to_string(num_of_layers) + ",", level);
bytesHopper(cursor, 3);
return num_of_layers;
}
double decodeDouble(char **cursor, std::string &jstring, int level, std::string tag) {
double val = getDouble(cursor);
// LOG("this");
// std::cout << std::setprecision(10) << val << std::endl;
std::stringstream ss;
ss << std::setprecision(50) << val;
std::string dstr;
ss >> dstr;
// Put the name and the value of the double value into the JSON string.
LOG(tag + " is: " + std::to_string(val));
write_to_json(jstring, tag, dstr + ",", level);
return val;
}
int decodeInt(char **cursor, std::string &jstring, int level, std::string tag) {
int val = get32Bit(cursor);
// Put the name and value of the integer value into the JSON string.
LOG(tag + " is: " + std::to_string(val));
write_to_json(jstring, tag, std::to_string(val) + ",", level);
return val;
}
void decodeString(char **cursor, std::string &jstring, int level) {
LOG("START decoding string...");
// bool going = true; // When "going" is true, the while loop will keep going.
// std::string str = "";
int code = 1;
write_to_json(jstring, "fontName", "[", level);
while (0 != (code = get16Bit(cursor))) {
write_to_json(jstring, "", std::to_string(code) + ",", level + 1);
}
write_to_json(jstring, "", "]", level);
// The length of the null pattern can be varied.
int id = getChar(cursor);
if (id == 0) {
bytesHopper(cursor, 54);
} else if (id == 3) {
bytesHopper(cursor, 24);
} else {
LOG("ERROR: Failed to validate string format...");
throw std::string("validation.");
}
// printHex(cursor, 30);
int secondCode = getChar(cursor);
while (1 != secondCode && 0 != secondCode) {
secondCode = getChar(cursor);
}
bytesRewinder(cursor, 1);
LOG("FINISHED decoding string. :-)");
}