This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fpack.c
453 lines (383 loc) · 16.4 KB
/
fpack.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
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
/* FPACK
* R. Seaman, NOAO, with a few enhancements by W. Pence, HEASARC
*
* Calls fits_img_compress in the CFITSIO library by W. Pence, HEASARC
*/
#include <ctype.h>
/* #include <signal.h> */
#include "fitsio.h"
#include "fpack.h"
/* ================================================================== */
int main(int argc, char *argv[])
{
fpstate fpvar;
if (argc <= 1) { fp_usage (); fp_hint (); exit (-1); }
fp_init (&fpvar);
fp_get_param (argc, argv, &fpvar);
if (fpvar.listonly) {
fp_list (argc, argv, fpvar);
} else {
fp_preflight (argc, argv, FPACK, &fpvar);
fp_loop (argc, argv, FPACK, fpvar);
}
exit (0);
}
/* ================================================================== */
int fp_get_param (int argc, char *argv[], fpstate *fpptr)
{
int gottype=0, gottile=0, wholetile=0, iarg, len, ndim, ii, doffset;
int gotR=0, gotO=0;
char tmp[SZ_STR], tile[SZ_STR];
if (fpptr->initialized != FP_INIT_MAGIC) {
fp_msg ("Error: internal initialization error\n"); exit (-1);
}
tile[0] = 0;
/* flags must come first and be separately specified
*/
for (iarg = 1; iarg < argc; iarg++) {
if ((argv[iarg][0] == '-' && strlen (argv[iarg]) == 2) ||
!strncmp(argv[iarg], "-q", 2) || !strncmp(argv[iarg], "-qz", 3) ||
!strncmp(argv[iarg], "-g1", 3) || !strncmp(argv[iarg], "-g2", 3) ||
!strncmp(argv[iarg], "-i2f", 4) ||
!strncmp(argv[iarg], "-n3ratio", 8) || !strncmp(argv[iarg], "-n3min", 6) ||
!strncmp(argv[iarg], "-tableonly", 10) || !strncmp(argv[iarg], "-table", 6) )
{
/* Rice is the default, so -r is superfluous */
if ( argv[iarg][1] == 'r') {
fpptr->comptype = RICE_1;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
} else if (argv[iarg][1] == 'p') {
fpptr->comptype = PLIO_1;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
} else if (argv[iarg][1] == 'g') {
/* test for modifiers following the 'g' */
if (argv[iarg][2] == '2')
fpptr->comptype = GZIP_2;
else
fpptr->comptype = GZIP_1;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
/*
} else if (argv[iarg][1] == 'b') {
fpptr->comptype = BZIP2_1;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
*/
} else if (argv[iarg][1] == 'h') {
fpptr->comptype = HCOMPRESS_1;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
} else if (argv[iarg][1] == 'd') {
fpptr->comptype = NOCOMPRESS;
if (gottype) {
fp_msg ("Error: multiple compression flags\n");
fp_usage (); exit (-1);
} else
gottype++;
} else if (!strcmp(argv[iarg], "-i2f")) {
/* this means convert integer images to float, and then */
/* quantize and compress the float image. This lossy */
/* compression method may give higher compression than the */
/* lossless compression method that is usually applied to */
/* integer images. */
fpptr->int_to_float = 1;
} else if (!strcmp(argv[iarg], "-n3ratio")) {
/* this is the minimum ratio between the MAD noise sigma */
/* and the q parameter value in the case where the integer */
/* image is quantized and compressed like a float image. */
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
fpptr->n3ratio = (float) atof (argv[iarg]);
}
} else if (!strcmp(argv[iarg], "-n3min")) {
/* this is the minimum MAD noise sigma in the case where the */
/* integer image is quantized and compressed like a float image. */
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
fpptr->n3min = (float) atof (argv[iarg]);
}
} else if (argv[iarg][1] == 'q') {
/* test for modifiers following the 'q' */
if (argv[iarg][2] == 'z') {
fpptr->dither_method = 2; /* preserve zero pixels */
if (argv[iarg][3] == 't') {
fpptr->dither_offset = -1; /* dither based on tile checksum */
} else if (isdigit(argv[iarg][3])) { /* is a number appended to q? */
doffset = atoi(argv[iarg]+3);
if (doffset == 0) {
fpptr->no_dither = 1; /* don't dither the quantized values */
} else if (doffset > 0 && doffset <= 10000) {
fpptr->dither_offset = doffset;
} else {
fp_msg ("Error: invalid q suffix\n");
fp_usage (); exit (-1);
}
}
} else {
if (argv[iarg][2] == 't') {
fpptr->dither_offset = -1; /* dither based on tile checksum */
} else if (isdigit(argv[iarg][2])) { /* is a number appended to q? */
doffset = atoi(argv[iarg]+2);
if (doffset == 0) {
fpptr->no_dither = 1; /* don't dither the quantized values */
} else if (doffset > 0 && doffset <= 10000) {
fpptr->dither_offset = doffset;
} else {
fp_msg ("Error: invalid q suffix\n");
fp_usage (); exit (-1);
}
}
}
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
fpptr->quantize_level = (float) atof (argv[iarg]);
}
} else if (argv[iarg][1] == 'n') {
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
fpptr->rescale_noise = (float) atof (argv[iarg]);
}
} else if (argv[iarg][1] == 's') {
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
fpptr->scale = (float) atof (argv[iarg]);
}
} else if (!strcmp(argv[iarg], "-tableonly")) {
fpptr->do_tables = 1;
fpptr->do_images = 0;
/* Do not write this to stdout via fp_msg. Otherwise it will be placed at start of piped FITS
file, which will then be corrupted. */
fprintf(stderr, "Note: The table compression method used by fpack has been\n");
fprintf(stderr, " officially approved as part of FITS format standard since 2016.\n");
fprintf(stderr, " However users should be aware that the compressed table files may\n");
fprintf(stderr, " only be readable by a limited number of applications (including fpack).\n");
} else if (!strcmp(argv[iarg], "-table")) {
fpptr->do_tables = 1;
fprintf(stderr, "Note: The table compression method used by fpack has been\n");
fprintf(stderr, " officially approved as part of FITS format standard since 2016.\n");
fprintf(stderr, " However users should be aware that the compressed table files may\n");
fprintf(stderr, " only be readable by a limited number of applications (including fpack).\n");
} else if (argv[iarg][1] == 't') {
if (gottile) {
fp_msg ("Error: multiple tile specifications\n");
fp_usage (); exit (-1);
} else
gottile++;
if (++iarg >= argc) {
fp_usage (); exit (-1);
} else {
strncpy (tile, argv[iarg], SZ_STR-1); /* checked below */
tile[SZ_STR-1]=0;
}
} else if (argv[iarg][1] == 'v') {
fpptr->verbose = 1;
} else if (argv[iarg][1] == 'w') {
wholetile++;
if (gottile) {
fp_msg ("Error: multiple tile specifications\n");
fp_usage (); exit (-1);
} else
gottile++;
} else if (argv[iarg][1] == 'F') {
fpptr->clobber++; /* overwrite existing file */
} else if (argv[iarg][1] == 'D') {
fpptr->delete_input++;
} else if (argv[iarg][1] == 'Y') {
fpptr->do_not_prompt++;
} else if (argv[iarg][1] == 'S') {
fpptr->to_stdout++;
} else if (argv[iarg][1] == 'L') {
fpptr->listonly++;
} else if (argv[iarg][1] == 'C') {
fpptr->do_checksums = 0;
} else if (argv[iarg][1] == 'T') {
fpptr->test_all = 1;
} else if (argv[iarg][1] == 'R') {
if (gotO) {
fp_msg("Error: -R option is not allowed with -O\n");
exit(-1);
} else if (++iarg >= argc) {
fp_usage (); fp_hint (); exit (-1);
} else {
strncpy (fpptr->outfile, argv[iarg], SZ_STR-1);
fpptr->outfile[SZ_STR-1]=0;
gotR=1;
}
} else if (argv[iarg][1] == 'H') {
fp_help (); exit (0);
} else if (argv[iarg][1] == 'V') {
fp_version (); exit (0);
} else if (argv[iarg][1] == 'O') {
if (gotR) {
fp_msg("Error: -O option is not allowed with -R\n");
exit(-1);
} else if (++iarg >= argc) {
fp_usage (); fp_hint (); exit (-1);
} else {
strncpy (fpptr->outfile, argv[iarg], SZ_STR-1);
fpptr->outfile[SZ_STR-1]=0;
gotO=1;
}
} else {
fp_msg ("Error: unknown command line flag `");
fp_msg (argv[iarg]); fp_msg ("'\n");
fp_usage (); fp_hint (); exit (-1);
}
} else
break;
}
/* In earlier loop, already made sure both -O and -R are not being used.
This is essential, as each must store info in the same 'outfile' array.
Now do additional tests of -O and -R with other flags. */
if (gotR && !fpptr->test_all) {
fp_msg("Error: -R option may only be used with -T\n"); exit(-1);
}
if (gotO && (fpptr->test_all || fpptr->to_stdout)) {
fp_msg("Error: -O option may not be used with -S or -T\n"); exit(-1);
}
if (fpptr->scale != 0. &&
fpptr->comptype != HCOMPRESS_1 && fpptr->test_all != 1) {
fp_msg ("Error: `-s' requires `-h or -T'\n"); exit (-1);
}
if (fpptr->quantize_level == 0) {
if ((fpptr->comptype != GZIP_1) && (fpptr->comptype != GZIP_2)) {
fp_msg ("Error: `-q 0' only allowed with GZIP\n"); exit (-1);
}
if (fpptr->int_to_float == 1) {
fp_msg ("Error: `-q 0' not allowed with -i2f\n"); exit (-1);
}
}
if (wholetile) {
for (ndim=0; ndim < MAX_COMPRESS_DIM; ndim++)
fpptr->ntile[ndim] = (long) -1;
} else if (gottile) {
len = strlen (tile);
for (ii=0, ndim=0; ii < len; ) {
if (! (isdigit (tile[ii]) || tile[ii] == ',')) {
fp_msg ("Error: `-t' requires comma separated tile dims, ");
fp_msg ("e.g., `-t 100,100'\n"); exit (-1);
}
if (tile[ii] == ',') { ii++; continue; }
fpptr->ntile[ndim] = atol (&tile[ii]);
for ( ; isdigit(tile[ii]); ii++);
if (++ndim > MAX_COMPRESS_DIM) {
fp_msg ("Error: too many dimensions for `-t', max=");
snprintf (tmp, SZ_STR,"%d\n", MAX_COMPRESS_DIM); fp_msg (tmp);
exit (-1);
}
}
}
if (iarg >= argc) {
fp_msg ("Error: no FITS files to compress\n");
fp_usage (); exit (-1);
} else
fpptr->firstfile = iarg;
return(0);
}
/* ================================================================== */
int fp_usage (void)
{
fp_msg ("usage: fpack ");
fp_msg (
"[-r|-h|-g|-p] [-w|-t <axes>] [-q <level>] [-s <scale>] [-n <noise>] -v <FITS>\n");
fp_msg ("more: [-T] [-R] [-F] [-D] [-Y] [-O <file>] [-S] [-L] [-C] [-H] [-V] [-i2f]\n");
return(0);
}
/* ================================================================== */
int fp_hint (void)
{ fp_msg (" `fpack -H' for help\n");
return(0);
}
/* ================================================================== */
int fp_help (void)
{
fp_msg ("fpack, a FITS image compression program. Version ");
fp_version ();
fp_usage ();
fp_msg ("\n");
fp_msg ("NOTE: the compression parameters specified on the fpack command line may\n");
fp_msg ("be over-ridden by compression directive keywords in the header of each HDU\n");
fp_msg ("of the input file(s). See the fpack User's Guide for more details\n");
fp_msg ("\n");
fp_msg ("Flags must be separate and appear before filenames:\n");
fp_msg (" -r Rice compression [default], or\n");
fp_msg (" -h Hcompress compression, or\n");
fp_msg (" -g or -g1 GZIP_1 (per-tile) compression, or\n");
fp_msg (" -g2 GZIP_2 (per-tile) compression (with byte shuffling), or\n");
/*
fp_msg (" -b BZIP2 (per-tile) compression, or\n");
*/
fp_msg (" -p PLIO compression (only for positive 8 or 16-bit integer images).\n");
fp_msg (" -d Tile the image without compression (debugging mode).\n");
fp_msg (" -w Compress the whole image as a single large tile.\n");
fp_msg (" -t <axes> Comma separated list of tile dimensions [default is row by row].\n");
fp_msg (" -q <level> Quantized level spacing when converting floating point images to\n");
fp_msg (" scaled integers. (+value relative to sigma of background noise;\n");
fp_msg (" -value is absolute). Default q value of 4 gives a compression ratio\n");
fp_msg (" of about 6 with very high fidelity (only 0.26% increase in noise).\n");
fp_msg (" Using q values of 2, or 1 will give compression ratios of\n");
fp_msg (" about 8, or 10, respectively (with 1.0% or 4.1% noise increase).\n");
fp_msg (" The scaled quantized values are randomly dithered using a seed \n");
fp_msg (" value determined from the system clock at run time.\n");
fp_msg (" Use -q0 instead of -q to suppress random dithering.\n");
fp_msg (" Use -qz instead of -q to not dither zero-valued pixels.\n");
fp_msg (" Use -qt or -qzt to compute random dithering seed from first tile checksum.\n");
fp_msg (" Use -qN or -qzN, (N in range 1 to 10000) to use a specific dithering seed.\n");
fp_msg (" Floating-point images can be losslessly compressed by selecting\n");
fp_msg (" the GZIP algorithm and specifying -q 0, but this is slower and often\n");
fp_msg (" produces much less compression than the default quantization method.\n");
fp_msg (" -i2f Convert integer images to floating point, then quantize and compress\n");
fp_msg (" using the specified q level. When used appropriately, this lossy\n");
fp_msg (" compression method can give much better compression than the normal\n");
fp_msg (" lossless compression methods without significant loss of information.\n");
fp_msg (" The -n3ratio and -n3min flags control the minimum noise thresholds;\n");
fp_msg (" Images below these thresholds will be losslessly compressed.\n");
fp_msg (" -n3ratio Minimum ratio of background noise sigma divided by q. Default = 2.0.\n");
fp_msg (" -n3min Minimum background noise sigma. Default = 6. The -i2f flag will be ignored\n");
fp_msg (" if the noise level in the image does not exceed both thresholds.\n");
fp_msg (" -s <scale> Scale factor for lossy Hcompress [default = 0 = lossless]\n");
fp_msg (" (+values relative to RMS noise; -value is absolute)\n");
fp_msg (" -n <noise> Rescale scaled-integer images to reduce noise and improve compression.\n");
fp_msg (" -v Verbose mode; list each file as it is processed.\n");
fp_msg (" -T Show compression algorithm comparison test statistics; files unchanged.\n");
fp_msg (" -R <file> Write the comparison test report (above) to a text file.\n");
fp_msg (" -table Compress FITS binary tables as well as compress any image HDUs.\n");
fp_msg (" -tableonly Compress only FITS binary tables; do not compress any image HDUs.\n");
fp_msg (" \n");
fp_msg ("\nkeywords shared with funpack:\n");
fp_msg (" -F Overwrite input file by output file with same name.\n");
fp_msg (" -D Delete input file after writing output.\n");
fp_msg (" -Y Suppress prompts to confirm -F or -D options.\n");
fp_msg (" -O <file> Specify full output file name. This may be used only when fpack\n");
fp_msg (" is run on a single input file.\n");
fp_msg (" -S Output compressed FITS files to STDOUT.\n");
fp_msg (" -L List contents; files unchanged.\n");
fp_msg (" -C Don't update FITS checksum keywords.\n");
fp_msg (" -H Show this message.\n");
fp_msg (" -V Show version number.\n");
fp_msg ("\n <FITS> FITS files to pack; enter '-' (a hyphen) to read input from stdin stream.\n");
fp_msg (" Refer to the fpack User's Guide for more extensive help.\n");
return(0);
}