Debian Codegraph @

src/SZ3/tools/sz3/deprecated/test_arithmetic_coding.cpp

raw
1 /**
2 * @file ArithmeticCoding.c
3 * @author Sheng Di, Mark Thomas Nelson
4 * @date April, 2016
5 * @brief Byte Toolkit
6 * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
7 * See COPYRIGHT in top-level directory.
8 * (C) The MIT License (MIT), this code was modified from Mark's arithmetic coding code: http://www.drdobbs.com/cpp/data-compression-with-arithmetic-encodin/240169251?pgno=1
9 */
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <sys/time.h>
14 #include "SZ3/encoder/ArithmeticEncoder.hpp"
15 #include "SZ3/utils/FileUtil.hpp"
16
17 unsigned char *readByteData(char *srcFilePath, size_t *byteLength, int *status) {
18 FILE *pFile = fopen(srcFilePath, "rb");
19 if (pFile == NULL) {
20 printf("Failed to open input file. 1\n");
21 return 0;
22 }
23 fseek(pFile, 0, SEEK_END);
24 *byteLength = ftell(pFile);
25 fclose(pFile);
26
27 unsigned char *byteBuf = (unsigned char *) malloc((*byteLength) * sizeof(unsigned char)); //sizeof(char)==1
28
29 pFile = fopen(srcFilePath, "rb");
30 if (pFile == NULL) {
31 printf("Failed to open input file. 2\n");
32 return 0;
33 }
34 fread(byteBuf, 1, *byteLength, pFile);
35 fclose(pFile);
36 *status = 0;
37 return byteBuf;
38 }
39
40 void writeByteData(unsigned char *bytes, size_t byteLength, char *tgtFilePath, int *status) {
41 FILE *pFile = fopen(tgtFilePath, "wb");
42 if (pFile == NULL) {
43 printf("Failed to open input file. 3\n");
44 *status = 0;
45 return;
46 }
47
48 fwrite(bytes, 1, byteLength, pFile); //write outSize bytes
49 fclose(pFile);
50 *status = 0;
51 }
52
53 using namespace SZ;
54
55 int main(int argc, char *argv[]) {
56 int status = 0;
57 char inputFile[100];
58 size_t byteLen = 0;
59 sprintf(inputFile, "%s", argv[1]);
60 // unsigned char *bytes = readByteData(inputFile, &byteLen, &status);
61 // std::vector<int> codes(byteLen);
62 size_t i = 0;
63 // for (i = 0; i < byteLen; i++)
64 // codes[i] = bytes[i];
65
66 auto read = readfile<int>(argv[1], byteLen);
67 // read[0] = 251;
68 // byteLen = 4;
69 std::vector<int> codes(read.get(), read.get() + byteLen);
70
71 unsigned char *cmprData = (unsigned char *) malloc(sizeof(unsigned char) * byteLen * 1.2);
72 unsigned char *ariCoderBytes = cmprData;
73
74 //compression
75 // cost_start();
76 ArithmeticEncoder<int> encoder;
77
78 encoder.preprocess_encode(codes, 4096);
79 encoder.save(ariCoderBytes);
80 encoder.encode(codes, ariCoderBytes);
81 encoder.postprocess_encode();
82 // cost_end();
83
84 size_t totalCmprSize = ariCoderBytes - cmprData;
85
86 char cmprFile[100];
87 sprintf(cmprFile, "%s.ari", inputFile);
88 writefile(cmprFile, cmprData, totalCmprSize);
89
90 printf("compressed data size is: %zu\n", totalCmprSize);
91 printf("compression ratio is: %f\n", 1.0 * byteLen / totalCmprSize);
92 // printf("compression time: %f\n", totalCost);
93 const uchar *cmprData2;
94 cmprData2 = readfile<uchar>(cmprFile, totalCmprSize).get();
95
96 //decompression
97 // ArithmeticEncoder<int> decoder;
98 encoder.load(cmprData2, totalCmprSize);
99
100 // for (i = 0; i < encoder.ariCoder.numOfRealStates; i++) {
101 // if (encoder.ariCoder.cumulative_frequency[i].high != decoder.ariCoder.cumulative_frequency[i].high) {
102 // printf("i=%zu, %ld vs. %ld\n", i, encoder.ariCoder.cumulative_frequency[i].high,
103 // decoder.ariCoder.cumulative_frequency[i].high);
104 // break;
105 // }
106 // }
107 printf("done checking\n");
108 // cost_start();
109 auto decData = encoder.decode(cmprData2, byteLen);
110 encoder.postprocess_decode();
111 // cost_end();
112
113 int same = 1;
114 for (i = 0; i < byteLen; i++) {
115 if (codes[i] != decData[i]) {
116 printf("Error: i = %zu, codes[i] = %d, decData[i] = %d\n", i, codes[i], decData[i]);
117 same = 0;
118 // break;
119 }
120 }
121
122 if (same)
123 printf("decompression is correct!\n");
124 else
125 printf("decomipression is wrong!\n");
126
127
128 // printf("decompression time: %f\n", totalCost);
129
130 free(cmprData);
131 // free(bytes);
132 }
133
134
135