-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
executable file
·362 lines (296 loc) · 7.51 KB
/
example.c
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
/* example.c -- usage example of the szlib compression library
* This code is loosely based on that in the zlib.
* For conditions of distribution and use, see copyright notice in szlib.h
*/
/* @(#) $Id: example.c 36 2004-03-16 16:14:27Z epourmal $ */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "szlib.h"
#define MAX_IMAGE_SIZE (1024*1024L)
#if 0
const char hello[] = "AAAAAAAAAAAAAAAA";
#endif
const char hello[] = "A 16 byte line!!";
char *image_in;
char *image_in2;
char *image_out;
long test_decoding(int bits_per_pixel, char *in, long size, char *out, long out_size, long buffer_size);
long test_encoding(int bits_per_pixel, char *in, long size, char *out, long buffer_size);
long
read_image(file_name)
char *file_name;
{
FILE *fp;
int n;
long size;
if ((fp = fopen(file_name, "rb")) == 0)
{
fprintf(stderr, "Could not open input file: %s\n", file_name);
exit(1);
}
size = 0;
while (1)
{
n = fread(image_in+size, 1, 16*1024, fp);
if (n == 0)
break;
size += n;
}
fprintf(stderr, "read_image(%s): size=%ld\n", file_name, size);
fclose(fp);
return size;
}
long
test_encoding(bits_per_pixel, in, size, out, buffer_size)
int bits_per_pixel;
char *in;
long size;
char *out;
long buffer_size;
{
sz_stream c_stream;
int bytes_per_pixel;
int err;
int len;
c_stream.hidden = 0;
c_stream.options_mask = SZ_RAW_OPTION_MASK | SZ_NN_OPTION_MASK | SZ_MSB_OPTION_MASK;
c_stream.bits_per_pixel = bits_per_pixel;
c_stream.pixels_per_block = 8;
c_stream.pixels_per_scanline = 16;
bytes_per_pixel = (bits_per_pixel + 7)/8;
if (bytes_per_pixel == 3)
bytes_per_pixel = 4;
c_stream.image_pixels = size/bytes_per_pixel;
len = size;
c_stream.next_in = image_in;
c_stream.total_in = 0;
c_stream.next_out = out;
c_stream.total_out = 0;
err = SZ_CompressInit(&c_stream);
if (err != SZ_OK)
{
fprintf(stderr, "SZ_CompressInit error: %d\n", err);
exit(1);
}
while (c_stream.total_in < len)
{
c_stream.avail_in = c_stream.avail_out = buffer_size;
if (c_stream.avail_in + c_stream.total_in > len)
c_stream.avail_in = len - c_stream.total_in;
err = SZ_Compress(&c_stream, SZ_NO_FLUSH);
if (err != SZ_OK)
{
fprintf(stderr, "SZ_Compress error: %d\n", err);
exit(1);
}
}
for (;;)
{
c_stream.avail_out = buffer_size;
err = SZ_Compress(&c_stream, SZ_FINISH);
#if 0
printf("output byte=%02X\n", c_stream.next_out[-1]);
#endif
if (err == SZ_STREAM_END)
break;
if (err != SZ_OK)
{
fprintf(stderr, "SZ_Compress error: %d\n", err);
exit(1);
}
}
err = SZ_CompressEnd(&c_stream);
if (err != SZ_OK)
{
fprintf(stderr, "SZ_CompressEnd error: %d\n", err);
exit(1);
}
{
int i;
if (c_stream.total_out < 30)
{
printf("total_out=%ld\n", c_stream.total_out);
for (i = 0; i < c_stream.total_out; i++)
printf("%02X", out[i]);
printf("\n");
}
}
return c_stream.total_out;
}
long
test_decoding(bits_per_pixel, in, size, out, out_size, buffer_size)
int bits_per_pixel;
char *in;
long size;
char *out;
long out_size;
long buffer_size;
{
int bytes_per_pixel;
int err;
sz_stream d_stream;
strcpy((char*)out, "garbage");
d_stream.hidden = 0;
d_stream.next_in = in;
d_stream.next_out = out;
d_stream.avail_in = 0;
d_stream.avail_out = 0;
d_stream.total_in = 0;
d_stream.total_out = 0;
d_stream.options_mask = SZ_RAW_OPTION_MASK | SZ_NN_OPTION_MASK | SZ_MSB_OPTION_MASK;
d_stream.bits_per_pixel = bits_per_pixel;
d_stream.pixels_per_block = 8;
d_stream.pixels_per_scanline = 16;
bytes_per_pixel = (bits_per_pixel + 7)/8;
if (bytes_per_pixel == 3)
bytes_per_pixel = 4;
d_stream.image_pixels = out_size/bytes_per_pixel;
err = SZ_DecompressInit(&d_stream);
if (err != SZ_OK)
{
fprintf(stderr, "SZ_DecompressEnd error: %d\n", err);
exit(1);
}
while (d_stream.total_in < size)
{
d_stream.avail_in = d_stream.avail_out = buffer_size;
if (d_stream.avail_in + d_stream.total_in > size)
d_stream.avail_in = size - d_stream.total_in;
err = SZ_Decompress(&d_stream, SZ_NO_FLUSH);
if (err == SZ_STREAM_END)
break;
if (err != SZ_OK)
{
fprintf(stderr, "SZ_Decompress error: %d\n", err);
exit(1);
}
}
while (d_stream.total_out < out_size)
{
d_stream.avail_out = buffer_size;
err = SZ_Decompress(&d_stream, SZ_FINISH);
if (err == SZ_STREAM_END)
break;
if (err != SZ_OK)
{
fprintf(stderr, "SZ_Decompress error: %d\n", err);
exit(1);
}
}
err = SZ_DecompressEnd(&d_stream);
if (err != SZ_OK)
{
fprintf(stderr, "SZ_DecompressEnd error: %d\n", err);
exit(1);
}
return d_stream.total_out;
}
/**********************************************/
/*** Usage: example [bits_per_pixel] ***/
/*** default bits_per_pixel = 8 ***/
/**********************************************/
/****************************************** **************/
/*** To generate random image file, run the following: ***/
/*** burst_szip -msb # 8 1024 16 image.#.in ***/
/*** Where # is the bits_per_pixel value ***/
/*********************************************************/
int
main(argc, argv)
int argc;
char *argv[];
{
char *compr;
char file_name[256];
char *uncompr;
int bits_per_pixel;
long comprLen = 10000*sizeof(int);
long uncomprLen = comprLen;
size_t size;
size_t size2;
long image_size;
int rv;
int i;
SZ_com_t params;
if (argc == 1)
bits_per_pixel = 8;
else if (argc == 2)
bits_per_pixel = atoi(argv[1]);
sprintf(file_name, "image.%d.in", bits_per_pixel);
compr = (char *) calloc(comprLen, 1);
uncompr = (char *) calloc(uncomprLen, 1);
if (compr == SZ_NULL || uncompr == SZ_NULL)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
image_in = (char *) malloc(MAX_IMAGE_SIZE);
image_out = (char *) malloc(MAX_IMAGE_SIZE*2);
image_in2 = (char *) malloc(MAX_IMAGE_SIZE);
strcpy(image_out, "Junk!!!");
strcpy(image_in2, "Junk!!!");
image_size = read_image(file_name);
if (image_size > MAX_IMAGE_SIZE)
{
fprintf(stderr, "MAX_IMAGE_SIZE of %ld exceeded.\n", MAX_IMAGE_SIZE);
exit(1);
}
printf("Image size %ld \n", image_size);
/*** test with power of two buffer sizes ***/
for (i = 1; i < 1025; i <<= 1)
{
strcpy(image_out, "Junk!!!");
strcpy(image_in2, "Junk!!!");
printf("Testing buffer size = %d\n", i);
size = test_encoding(bits_per_pixel, image_in, image_size, image_out, i);
size = test_decoding(bits_per_pixel, image_out, size, image_in2, image_size, i);
#if 0
image_in2[1111] ^= 0x44;
#endif
rv = memcmp(image_in, image_in2, image_size);
#if 0
printf("memcmp(image_in, image_in2, %ld) = %d\n", image_size, rv);
#endif
if (rv)
exit(1);
params.options_mask = SZ_RAW_OPTION_MASK | SZ_NN_OPTION_MASK | SZ_MSB_OPTION_MASK;
params.bits_per_pixel = bits_per_pixel;
params.pixels_per_block = 8;
params.pixels_per_scanline = 16;
size = 2*MAX_IMAGE_SIZE;
#if 0
size = 8000;
#endif
rv = SZ_BufftoBuffCompress(image_out, &size, image_in, image_size, ¶ms);
#if 0
printf("SZ_bufftoBuffCompress()\n");
#endif
if (rv != SZ_OK)
{
fprintf(stderr, "SZ_BufftoBuffCompress fails\n");
exit(1);
}
size2 = MAX_IMAGE_SIZE;
#if 0
size2 = 14000;
#endif
rv = SZ_BufftoBuffDecompress(image_in2, &size2, image_out, size, ¶ms);
#if 0
printf("SZ_bufftoBuffDecompress()\n");
#endif
if (rv != SZ_OK)
{
fprintf(stderr, "SZ_BufftoBuffDecompress fails\n");
exit(1);
}
rv = memcmp(image_in, image_in2, image_size);
printf("memcmp(image_in, image_in2, %ld) = %d\n", image_size, rv);
if (rv)
{
printf("Test failed.\n");
exit(1);
}
}
printf("All test passed.\n");
return 0;
}