-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
312 lines (274 loc) · 10.1 KB
/
main.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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <string>
using namespace std;
#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
typedef struct {
char header[8];
float bedX;
float bedY;
float bedZ;
int padding0[3];
float layerHeight;
float expTime;
float expBottom;
float offTime;
int bottomLayers;
int resolutionX;
int resolutionY;
int preview0Addr;
int layerDefsAddr;
int nrLayers;
int preview1Addr;
int unknown6;
int projTypeCastMirror;
int padding1[6];
} headerType;
typedef struct {
int resolutionX;
int resolutionY;
int imageAddress;
int dataLength;
int padding[4];
char* imageData;
} previewType;
typedef struct {
float layerHeight;
float expTime;
float offTime;
int imageAddress;
int dataLength;
int padding[4];
} layerDefType;
typedef struct {
char color;
char tempColor;
} pixelType;
void getHeader(headerType *header, FILE *fp) {
fread(header, sizeof(headerType),1,fp);
}
void writeHeader( headerType *header, FILE *fp) {
fwrite(header, sizeof(headerType),1 , fp);
}
void getPreview( int previewAddr, previewType *preview, FILE* fp ) {
fseek(fp, previewAddr, SEEK_SET);
fread(&preview->resolutionX, sizeof(preview->resolutionX),1,fp);
fread(&preview->resolutionY, sizeof(preview->resolutionY),1,fp);
fread(&preview->imageAddress, sizeof(preview->imageAddress),1,fp);
fread(&preview->dataLength, sizeof(preview->dataLength),1,fp);
fread(&preview->padding, sizeof(preview->padding),1,fp);
preview->imageData = static_cast<char*>(malloc(static_cast<unsigned long>(preview->dataLength)));
fread(preview->imageData, static_cast<unsigned long>(preview->dataLength), 1, fp);
}
void writePreview( previewType *preview, FILE* fp) {
fwrite(&preview->resolutionX, sizeof(previewType::resolutionX),1,fp);
fwrite(&preview->resolutionY, sizeof(previewType::resolutionY),1,fp);
fwrite(&preview->imageAddress, sizeof(previewType::imageAddress),1,fp);
fwrite(&preview->dataLength, sizeof(previewType::dataLength),1,fp);
fwrite(&preview->padding, sizeof(previewType::padding),1,fp);
fwrite(preview->imageData, static_cast<unsigned long>(preview->dataLength), 1, fp);
}
void getLayerDefs( int layerDefsAddr, int nrLayersString, vector<layerDefType> *layerDefs, FILE *fp ) {
layerDefType inLayerDef;
fseek(fp, layerDefsAddr, SEEK_SET);
for (int i=0; i<nrLayersString; i++) {
fread(&inLayerDef, sizeof (inLayerDef),1,fp);
layerDefs->push_back(inLayerDef);
}
}
void writeLayerDefs( int nrLayersString, vector<layerDefType> *layerDefs, FILE *fp ) {
for (unsigned long i=0; i<static_cast<unsigned long>(nrLayersString); i++) {
fwrite( &layerDefs->at(i), sizeof(layerDefType), 1, fp);
}
}
void getImages( vector <vector <unsigned char> > *rawData, vector<layerDefType> *layerDefs, int nLayers, FILE *fp) {
unsigned char eol;
for (vector<layerDefType>::size_type i=0; i<static_cast<vector<layerDefType>::size_type>(nLayers); i++) {
fseek(fp, layerDefs->at(i).imageAddress, SEEK_SET);
vector<unsigned char> temp(static_cast<unsigned long>(layerDefs->at(i).dataLength),0); // This creates a new empty vector with size datalength
fread(&temp.at(0),static_cast<vector<layerDefType>::size_type>(layerDefs->at(i).dataLength),1,fp);
rawData->push_back(temp);
fread(&eol, 1, 1, fp);
}
}
void writeImages(vector <vector <unsigned char> > *rawData, int nLayers,FILE *fp) {
for (unsigned long i=0; i<static_cast<unsigned long>(nLayers); i++) {
fwrite(&rawData->at(i)[0], rawData->at(i).size(), 1, fp);
}
}
void decodeImage( layerDefType *layerDef, FILE *fp, pixelType (&pixelBuf)[1440][2560] ) {
unsigned char inByte;
char nr=0;
char val=0;
int x=0;
int y=0;
int i;
pixelType color;
fseek(fp, layerDef->imageAddress, SEEK_SET);
for (i=0; i<layerDef->dataLength; i++) {
fread(&inByte, sizeof(inByte), 1, fp);
nr = static_cast<char>(inByte&127);
val = static_cast<char>(inByte>>7);
color.color=val;
color.tempColor=val;
while (nr!=0) {
pixelBuf[x][y]=color;
x++;
if (x==1440) {
x=0; y++;
}
nr--;
}
}
}
void decodeImageFromRaw( vector<unsigned char> rd, pixelType (&pixelBuf)[1440][2560]) {
char nr=0;
char val=0;
int x=0;
int y=0;
pixelType color;
for (auto inByte=rd.begin(); inByte!=rd.end(); inByte++) {
nr = static_cast<char>(*inByte&127);
val = static_cast<char>(*inByte>>7);
color.color=val;
color.tempColor=val;
while (nr!=0) {
pixelBuf[x][y]=color;
x++;
if (x==1440) {
x=0; y++;
}
nr--;
}
}
}
void encodeImageToRaw( vector<unsigned char> *rd, pixelType (&pixelBuf)[1440][2560]) {
unsigned char runLen=0;
unsigned char prevColor=0;
unsigned char data=0;
vector<unsigned char>().swap(*rd); // This is guaranteed to empty rd
prevColor=static_cast<unsigned char>(pixelBuf[0][0].color);
for (int y=0; y<2560; y++) {
for (int x=0; x<1440; x++) {
if ( (pixelBuf[x][y].tempColor!=prevColor) || (runLen==125) ) {
data=static_cast<unsigned char>((prevColor<<7)|runLen);
runLen=1;
rd->push_back(data);
prevColor=static_cast<unsigned char>(pixelBuf[x][y].tempColor);
} else {
runLen++;
}
}
}
data=static_cast<unsigned char>((prevColor<<7)|runLen); // Skinners Constant
rd->push_back(data);
}
void decreasePixel( pixelType (&pixelBuf)[1440][2560]) {
for (int y=1; y<2560-1; y++) {
for (int x=1; x<1440-1; x++) {
int clr=pixelBuf[x][y].color +
pixelBuf[x-1][y].color +
pixelBuf[x-1][y-1].color +
pixelBuf[x][y-1].color +
pixelBuf[x+1][y-1].color +
pixelBuf[x+1][y].color +
pixelBuf[x+1][y+1].color +
pixelBuf[x][y+1].color +
pixelBuf[x-1][y+1].color;
if (clr<9) pixelBuf[x][y].tempColor=0; else pixelBuf[x][y].tempColor=1;
}
}
}
void printProgress (double percentage)
{
int val = static_cast<int>(percentage * 100);
int lpad = static_cast<int>(percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf ("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, "");
fflush (stdout);
}
// [[gnu::unused]]
int main(int argc, char* argv[]) {
headerType header;
previewType preview1;
previewType preview2;
vector<layerDefType> layerDefs;
vector< vector<unsigned char> > rawData;
static pixelType pixelBuf[1440][2560];
if (argc<3) {
cout << endl;
cout << "Usage: decreasePixel file1.photon file2.photon" << endl;
cout << endl;
return(EXIT_FAILURE);
}
string inFile=argv[1];
string outFile=argv[2];
FILE *fp=fopen(inFile.c_str(), "rb");
if (fp==nullptr) {
cout << endl << "Error! File: " << inFile << " does not exist!" << endl;
return(EXIT_FAILURE);
}
getHeader(&header, fp);
getPreview( header.preview0Addr, &preview1, fp );
getPreview( header.preview1Addr, &preview2, fp );
getLayerDefs( header.layerDefsAddr, header.nrLayers, &layerDefs, fp );
getImages(&rawData, &layerDefs, header.nrLayers,fp);
cout << "Parameters for file : " << inFile << endl;
cout << "Bed Dimensions (X, Y, Z): " << header.bedX << ", " << header.bedY << ", " << header.bedZ << endl;
cout << "Layer Height : " << header.layerHeight << endl;
cout << "Exposure Time : " << header.expTime << endl;
cout << "Bottom Exposure Time : " << header.expBottom << endl;
cout << "Off Time : " << header.offTime << endl;
cout << "#Bottom Layers : " << header.bottomLayers << endl;
cout << "Total #Layers : " << header.nrLayers << endl;
cout << endl;
int nextAddr=layerDefs[0].imageAddress;
for (unsigned long i=0; i<static_cast<unsigned long>(header.nrLayers); i++) {
//cout << "Converting layer: " << i << endl;
printProgress(static_cast<double>(i*1.0/header.nrLayers));
layerDefs[i].imageAddress=nextAddr;
decodeImageFromRaw(rawData[i], pixelBuf);
decreasePixel(pixelBuf);
encodeImageToRaw(&rawData[i], pixelBuf);
layerDefs[i].dataLength=static_cast<int>(rawData[i].size());
nextAddr+=rawData[i].size();
}
cout << endl;
int nextFp = sizeof(headerType);
header.preview0Addr = nextFp;
nextFp += sizeof(previewType::padding) +
sizeof(previewType::dataLength) +
sizeof(previewType::resolutionX) +
sizeof(previewType::resolutionY) +
sizeof(previewType::imageAddress);
preview1.imageAddress = nextFp;
nextFp += static_cast<unsigned long>(preview1.dataLength);
header.preview1Addr = nextFp;
nextFp += sizeof(previewType::padding) +
sizeof(previewType::dataLength) +
sizeof(previewType::resolutionX) +
sizeof(previewType::resolutionY) +
sizeof(previewType::imageAddress);
preview2.imageAddress = nextFp;
nextFp += static_cast<unsigned long>(preview2.dataLength);
header.layerDefsAddr = nextFp;
nextFp += static_cast<int>(sizeof(layerDefType)) * header.nrLayers;
for (unsigned long i=0; i<static_cast<unsigned long>(header.nrLayers); i++) {
layerDefs.at(i).imageAddress=nextFp;
nextFp+=rawData.at(i).size();
}
FILE *wp=fopen(outFile.c_str(),"wb");
writeHeader(&header,wp);
writePreview(&preview1,wp);
writePreview(&preview2,wp);
writeLayerDefs(header.nrLayers, &layerDefs, wp);
writeImages(&rawData, header.nrLayers,wp);
free(preview1.imageData);
free(preview2.imageData);
fclose(fp);
fclose(wp);
return(EXIT_SUCCESS);
}