-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassDecoder.cpp
465 lines (428 loc) · 14 KB
/
ClassDecoder.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
/*
* ClassDecoder.cpp
*
*
* Created by Narendra Chaudhary on 2/27/2015.
* Texas A&M University.
*
*/
#ifndef ClassDecoder_H_
#include "ClassDecoder.h"
#endif
#include <fstream>
#ifndef AC_HEADER
#include "AC.h"
#endif
#include <bitset>
#include <math.h>
#define logX(x, y) log((double) x)/log((double) y)
#define log2(x) log((double) x)/log(2.0)
/* Constructor */
ClassDecoder::ClassDecoder()
{
}
/* Destructor */
ClassDecoder::~ClassDecoder()
{
}
int ClassDecoder::Transform_RLE_EOB_decoding()
{
char *current_row;
unsigned char *buffer_previous_row;
if( (buffer_previous_row = (unsigned char *) calloc(width, sizeof(char))) == NULL)
{
cout << endl << "[Error] Cannot allocate memory for previous row buffer... Please check memory and try again later..." << endl;
exit(-1);
}
if( (current_row = (char *) calloc(width, sizeof(char))) == NULL)
{
cout << endl << "[Error] Cannot allocate memory for current row buffer... Please check memory and try again later..." << endl;
exit(-1);
}
/*if( (rowPtr = (png_bytep) calloc(width, sizeof(char))) == NULL)
{
cout << endl << "[Error] Cannot allocate memory for png row buffer... Please check memory and try again later..." << endl;
exit(-1);
}*/
unsigned int Rows = height;
if (Rows > WRITE_ROWBUFFER)
Rows = WRITE_ROWBUFFER;
rowPtrs = new png_bytep[Rows]; // allocating rowPtrs memory for first iteration of writing
data1 = new char[width * Rows * (bitdepth / 8)];
int stride = width * (bitdepth / 8);
for (size_t i = 0; i < Rows; i++)
{
png_uint_32 q = i * stride;
rowPtrs[i] = (png_bytep)data1 + q; // setting the values of rowPtrs
}
unsigned long long idx = 0, image_size = ((unsigned long long)width)*((unsigned long long)height);
unsigned int count = 0,count_row =0, count_edge = 0;
unsigned int index = 0;
unsigned int exponent = 1, start_row = 0;
char temp = 0;
#ifdef PAETH
int pb,pc,pd,predict; // variables for PAETH filter implementation
#endif
for (size_t i=0; i < RLE_length; i++)
{
if (RLE[i] < MAX_CORNER_SYMBOL)
{
temp = (char) RLE[i];
#ifndef PAETH
if ((temp%2)==0) temp = temp/2;
else temp = -((temp+1)/2);
#endif
//index = idx % width;
//if ((index) == (width-1))
current_row[idx % width] = temp;
if ((idx % width) == (width-1)) // if reached at the end of row start corner decoding and write to file
{
count_edge++;
for (unsigned int j=0;j<width;j++) // Inverse corner transform
{
if (j>0)
{
#ifndef PAETH
*(rowPtrs[count_row-start_row]+j) = current_row[j] + *(rowPtrs[count_row-start_row]+j-1) + buffer_previous_row[j] - buffer_previous_row[j-1];
//rowPtr[j] = current_row[j] + rowPtr[j-1] + buffer_previous_row[j] - buffer_previous_row[j-1];
#else
//if (idx<width)predict=0;
//else{
predict = buffer_previous_row[j] + *(rowPtrs[count_row-start_row]+j-1) - buffer_previous_row[j-1];
pb = abs(predict - *(rowPtrs[count_row-start_row]+j-1)); //distances to b, c,d
pd = abs(predict - buffer_previous_row[j]);
pc = abs(predict - buffer_previous_row[j-1]);
if(pb <= pd && pb <= pc) predict = *(rowPtrs[count_row-start_row]+j-1);
else if (pd <= pc) predict = buffer_previous_row[j];
else predict = buffer_previous_row[j-1];
//}
*(rowPtrs[count_row-start_row]+j) = (current_row[j]+predict)%32;
#endif
}
else
{
#ifndef PAETH
*(rowPtrs[count_row-start_row]+j) = current_row[j] + buffer_previous_row[j];
//rowPtr[j] = current_row[j] + buffer_previous_row[j];
#else
predict = buffer_previous_row[j];
*(rowPtrs[count_row-start_row]+j) = (current_row[j]+predict)%32;;
#endif
}
}
memcpy(buffer_previous_row,rowPtrs[count_row-start_row],width*sizeof(char)); // put row data into previous row
//memcpy(buffer_previous_row,rowPtr,width*sizeof(char)); // put row data into previous row
#ifdef POSTPROCESS // do postprocessing of pxels if enabled
for (unsigned int j=0, index=0;j<width;j++)
{
//if (rowPtr[j] !=0)
if (*(rowPtrs[count_row-start_row]+j) !=0)
{
index = int (*(rowPtrs[count_row-start_row]+j));
*(rowPtrs[count_row-start_row]+j) = *(Maparray+index);
//index = int (rowPtr[j]);
//rowPtr[j] = *(Maparray+index);
}
}
//png_write_row(pngPtr,rowPtr); // write row to PNG file
#else
//png_write_row(pngPtr,rowPtr); // write row to PNG file
#endif
count_row++;
if ((count_row % WRITE_ROWBUFFER)== 0 || count_row == height) // writing WRITE_ROWBUFFER rows to PNG filez
{
if ((count_row % WRITE_ROWBUFFER) ==0)
{
WriteRows(start_row,WRITE_ROWBUFFER);
start_row += WRITE_ROWBUFFER;
}
else
WriteRows(start_row,(count_row % WRITE_ROWBUFFER));
}
}
idx++;
//cout << "ID of nonzero symbols : " << idx << endl;
//cout << "temp :" << int(temp) << endl;
}
// If RLE of 0 is detected, reconstruct the run of 0s.
else if(RLE[i] < MAX_CORNER_SYMBOL + M)
{
exponent = 1;
count = 0;
// Decode run count from M-ary representation
while(RLE[i] >= MAX_CORNER_SYMBOL && RLE[i] < MAX_CORNER_SYMBOL + M)
{
count += exponent * (RLE[i] - MAX_CORNER_SYMBOL);
// exponent *= M;
exponent <<= RSHIFT_M; // If M is 2^N, use N bit right shift instead.
i++;
}
i--;
unsigned int x = idx % width;
idx += count;
for (unsigned int j=x;j<(idx % width);j++) // write zero's in current row
{
current_row[j] = 0;
}
}
// If RLE of EOB is detected, move to end of the block
else if(RLE[i] < MAX_CORNER_SYMBOL + M + K)
{
exponent = 1;
count = 0;
// Decode EOB run count from M-ary representation
while(RLE[i] >= MAX_CORNER_SYMBOL + M && RLE[i] < MAX_CORNER_SYMBOL + M + K)
{
count += exponent * (RLE[i] - MAX_CORNER_SYMBOL - M);
// exponent *= K;
exponent <<= RSHIFT_K; // If K is 2^N, use N bit right shift instead.
i++;
}
i--;
// Process the run of EOB
for(unsigned int count_EOB=0; count_EOB<count; count_EOB++)
{
unsigned int x = idx % width;
if(BlockSize * (x / BlockSize) + BlockSize < width)
{
idx += BlockSize - (x % BlockSize);
for (unsigned int j=x;j<(idx % width);j++)
{
current_row[j] = 0;
}
}
else
{
idx += width - (x % width);
for (unsigned int j=x;j< width;j++) // write zeros till end of block
{
current_row[j] = 0;
}
//cout << "count of row : " << count_row << endl;
for (unsigned int j=0;j<width;j++) // Inverse corner transform
{
if (j>0)
{
#ifndef PAETH
*(rowPtrs[count_row-start_row]+j) = current_row[j] + *(rowPtrs[count_row-start_row]+j-1) + buffer_previous_row[j] - buffer_previous_row[j-1];
//rowPtr[j] = current_row[j] + rowPtr[j-1] + buffer_previous_row[j] - buffer_previous_row[j-1];
#else
//if (idx<width)predict=0;
//else{
predict = buffer_previous_row[j] + *(rowPtrs[count_row-start_row]+j-1) - buffer_previous_row[j-1];
pb = abs(predict - *(rowPtrs[count_row-start_row]+j-1)); //distances to b, c,d
pd = abs(predict - buffer_previous_row[j]);
pc = abs(predict - buffer_previous_row[j-1]);
if(pb <= pd && pb <= pc) predict = *(rowPtrs[count_row-start_row]+j-1);
else if (pd <= pc) predict = buffer_previous_row[j];
else predict = buffer_previous_row[j-1];
//}
*(rowPtrs[count_row-start_row]+j) = (current_row[j]+predict)%32;
#endif
}
else
{
#ifndef PAETH
*(rowPtrs[count_row-start_row]+j) = current_row[j] + buffer_previous_row[j];
//rowPtr[j] = current_row[j] + buffer_previous_row[j];
#else
predict = buffer_previous_row[j];
*(rowPtrs[count_row-start_row]+j) = (current_row[j]+predict)%32;;
#endif
}
}
memcpy(buffer_previous_row,rowPtrs[count_row-start_row],width*sizeof(char)); // put row data into previous row
//memcpy(buffer_previous_row,rowPtr,width*sizeof(char)); // write current row into previous row
#ifdef POSTPROCESS // do postprocessing if enabled
for (unsigned int j=0, index=0;j<width;j++)
{
//if (rowPtr[j] !=0)
if (*(rowPtrs[count_row-start_row]+j) !=0)
{
index = int (*(rowPtrs[count_row-start_row]+j));
*(rowPtrs[count_row-start_row]+j) = *(Maparray+index);
//index = int (rowPtr[j]);
//rowPtr[j] = *(Maparray+index);
}
}
//png_write_row(pngPtr,rowPtr); // write row to PNG file
#else
//png_write_row(pngPtr,rowPtr); // write row to PNG file
#endif
count_row++;
if ((count_row % WRITE_ROWBUFFER)== 0 || count_row == height)
{
if ((count_row % WRITE_ROWBUFFER) ==0)
{
WriteRows(start_row,WRITE_ROWBUFFER);
start_row += WRITE_ROWBUFFER;
}
else
WriteRows(start_row,(count_row % WRITE_ROWBUFFER));
}
}
//cout << "idx : " << idx << endl;
}
}
else
{
cout << "[Error] Cannot decode the RLE stream..." << endl;
}
if(idx > image_size)
{
cout << "Out of Bound" << endl;
break;
}
//cout << "count of rows : " << count_row << endl;
//cout << "ID of nonzero symbols : " << idx << endl;
}
png_write_end(pngPtr,infoPtr); // end the writing of PNG file
free(RLE);
RLE = NULL;
free(buffer_previous_row);
free(current_row);
#ifdef DEBUG
cout << "count of rows : " << count_row << endl;
cout << "count of edges : " << count_edge << endl;
#endif
return 0;
}
int ClassDecoder::EntropyDecoder_AC(string filename)
{
// Run Arithmetic Decoding
#ifdef DEBUG
cout << "Decoding Arithmetic Codes...... " << endl;
#endif
unsigned char temp = 0;
ac_decoder acd;
ac_model acm;
filename += ".enc";
ac_decoder_init (&acd, filename.c_str());
ac_model_init(&acm, MAX_CORNER_SYMBOL+M+K+1, NULL, 1);
RLE = (unsigned short *) calloc(RLE_length, sizeof(unsigned short));
//for(size_t i=0; i<RLE_length; i++)
for(size_t i=0;; i++)
{
temp = ac_decode_symbol (&acd, &acm);
if (temp != EOF_SYMBOL)
RLE[i] = temp;
else
{
cout << "End of file occured" << endl;
break;
}
//RLE[i] = ac_decode_symbol (&acd, &acm);
//cout << "Decoded RLE symbol : " << RLE[i] << endl;
}
ac_decoder_done (&acd);
ac_model_done (&acm);
#ifdef DEBUG
cout << " [Done]" << endl;
#endif
return 0;
}
int ClassDecoder::inflate_decompression(string filename)
{
RLE = (unsigned short *) calloc(RLE_length, sizeof(unsigned short));
filename = filename + ".dft";
int ret;
unsigned have;
z_stream strm;
//int CHUNK = 32768;
unsigned char in[32768];
unsigned char out[32768];
unsigned int i,count = 0;
FILE *source;
source = fopen(filename.data(),"rb");
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK)
return ret;
/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, 1, 32768, source);
//cout << strm.avail_in << endl;
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
if (strm.avail_in == 0)
break;
strm.next_in = in;
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = 32768;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
//cout << "ret:" << ret << endl;
have = 32768 - strm.avail_out;
/*if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}*/
//cout << "have:" << have << endl;
for(i = 0; i<have;i++)
{
RLE[i + count] = unsigned short(out[i]);
}
count = count + have;
} while (strm.avail_out == 0);
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
cout << "inflated count : " << count << endl;
/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
void ClassDecoder::ReadRLE(string filename,int flag)
{
unsigned int start,end;
start = clock();
if(flag)
{
RLE = (unsigned short *) calloc(RLE_length, sizeof(unsigned short));
filename += ".txt";
ifstream inTXT;
inTXT.open(filename);
char *Data = new char[RLE_length+1];
inTXT.read(Data, RLE_length+1);
for (int i=0; i<RLE_length;i++)
{
RLE[i] = unsigned char (unsigned char(Data[i] - '0') + 128);
//cout << RLE[i] << '\t' << Data[i] << endl;
}
inTXT.close();
}
else
{
RLE = (unsigned short *) calloc(RLE_length, sizeof(unsigned short));
filename += ".line";
ifstream in(filename,ios::binary);
int i = 0;
while ((!in.eof())&&(i<RLE_length))
{
std::string inp;
getline(in,inp);
RLE[i] = stoi(inp);
//cout << RLE[i] << endl;
i++;
}
in.close();
}
end = clock();
cout << "\t" << "\t" << " Compressed file reading time = " << ((double) (end-start)) / ((double) 1000) << endl;
}