1 /* used by FPACK and FUNPACK
2  * R. Seaman, NOAO
3  * W. Pence, NASA/GSFC
4  */
5 
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 /* not needed any more */
11 /* #include <unistd.h> */
12 /* #include <sys/stat.h> */
13 /* #include <sys/types.h> */
14 
15 #define	FPACK_VERSION	"1.7.0 (Dec 2013)"
16 /*
17 VERSION	History
18 
19 1.7.0 (Dec 2013)
20     - extensive changes to the binary table compression method.  All types
21       of binary table columns, including variable length array columns are
22       now supported.  The command line table compression flag has been changed
23       to "-table" instead of "-BETAtable", and a new "-tableonly" flag has
24       been introduced to only compress the binary tables in the input files(s)
25       and not the image HDUs.
26 1.6.1 (Mar 2013)
27     - numerous changes to the BETAtable compression method used to compress
28       binary tables
29     - added support for compression 'steering' keywords that specify the
30       desired compression parameters that should be used when compressing
31       that particular HDU, thus overriding the fpack command line parameter
32       values.
33 
34 1.6.0 (June 2012)
35     - Fixed behavior of the "rename" function on Windows platforms so that
36       it will clobber/delete an existing file before renaming a file to
37       that name (the rename command behaves differently on POSIX and non-POSIX
38       environments).
39 
40 1.6.0 (February 2011)
41     - Added full support for compressing and uncompressing FITS binary tables
42       using a newly proposed format convention.  This is intended only for
43       further feasibility studies, and is not recommended for use with publicly
44       distributed FITS files.
45     - Use the minimum of the MAD 2nd, 3rd, and 5th order values as a more
46       conservative extimate of the noise when quantizing floating point images.
47     - Enhanced the tile compression routines so that a tile that contains all
48       NaN pixel values will be compressed.
49     - When uncompressing an image that was originally in a FITS primary array,
50       funpack will also append any new keywords that were written into the
51       primary array of the compressed FITS file after the file was compressed.
52     - Added support for the GZIP_2 algorithm, which shuffles the bytes in the
53       pixel values prior to compressing them with gzip.
54 1.5.1 (December 2010) Added prototype, mainly hidden, support for compressing
55       binary tables.
56 1.5.0 (August 2010) Added the -i2f option to lossy compress integer images.
57 1.4.0 (Jan 2010) Reduced the default value for the q floating point image
58       quantization parameter from 16 to 4.  This results in about 50% better
59       compression (from about 4.6x to 6.4) with no lost of significant information
60       (with the new subtractive dithering enhancement).  Replaced the code for
61       generating temporary filenames to make the code more portable (to Windows).
62       Replaced calls to the unix 'access' and 'stat' functions with more portable
63       code.   When unpacking a file, write it first to a temporary file, then
64       rename it when finished, so that other tasks cannot try to read the file
65       before it is complete.
66 1.3.0 (Oct 2009) added randomization to the dithering pattern so that
67       the same pattern is not used for every image; also added an option
68       for losslessly compressing floating point images with GZIP for test
69       purposes (not recommended for general use).  Also added support for
70       reading the input FITS file from the stdin file streams.
71 1.2.0 (Sept 2009) added subtractive dithering feature (in CFITSIO) when
72       quantizing floating point images; When packing an IRAF .imh + .pix image,
73       the file name is changed to FILE.fits.fz, and if the original file is
74       deleted, then both the .imh and .pix files are deleted.
75 1.1.4 (May 2009) added -E option to funpack to unpack a list of HDUs
76 1.1.3 (March 2009)  minor modifications to the content and format of the -T report
77 1.1.2 (September 2008)
78 */
79 
80 #define	FP_INIT_MAGIC	42
81 #define	FPACK		0
82 #define	FUNPACK		1
83 
84 /* changed from 16 in Jan. 2010 */
85 #define	DEF_QLEVEL	4.
86 
87 #define	DEF_HCOMP_SCALE	 0.
88 #define	DEF_HCOMP_SMOOTH 0
89 #define	DEF_RESCALE_NOISE 0
90 
91 #define	SZ_STR		513
92 #define	SZ_CARD		81
93 
94 
95 typedef struct
96 {
97 	int	comptype;
98 	float	quantize_level;
99 	int     no_dither;
100 	int     dither_offset;
101 	int     dither_method;
102 	float	scale;
103 	float	rescale_noise;
104 	int	smooth;
105 	int	int_to_float;
106 	float	n3ratio;
107 	float	n3min;
108 	long	ntile[MAX_COMPRESS_DIM];
109 
110 	int	to_stdout;
111 	int	listonly;
112 	int	clobber;
113 	int	delete_input;
114 	int	do_not_prompt;
115 	int	do_checksums;
116 	int	do_gzip_file;
117 	int     do_images;
118 	int     do_tables;
119 	int	test_all;
120 	int	verbose;
121 
122 	char	prefix[SZ_STR];
123 	char	extname[SZ_STR];
124 	int	delete_suffix;
125 	char	outfile[SZ_STR];
126 	int	firstfile;
127 
128 	int	initialized;
129 	int	preflight_checked;
130 } fpstate;
131 
132 typedef struct
133 {
134 	int	n_nulls;
135 	double	minval;
136 	double 	maxval;
137 	double	mean;
138 	double	sigma;
139 	double	noise1;
140 	double	noise2;
141 	double	noise3;
142 	double	noise5;
143 } imgstats;
144 
145 int fp_get_param (int argc, char *argv[], fpstate *fpptr);
146 void abort_fpack(int sig);
147 void fp_abort_output (fitsfile *infptr, fitsfile *outfptr, int stat);
148 int fp_usage (void);
149 int fp_help (void);
150 int fp_hint (void);
151 int fp_init (fpstate *fpptr);
152 int fp_list (int argc, char *argv[], fpstate fpvar);
153 int fp_info (char *infits);
154 int fp_info_hdu (fitsfile *infptr);
155 int fp_preflight (int argc, char *argv[], int unpack, fpstate *fpptr);
156 int fp_loop (int argc, char *argv[], int unpack, fpstate fpvar);
157 int fp_pack (char *infits, char *outfits, fpstate fpvar, int *islossless);
158 int fp_unpack (char *infits, char *outfits, fpstate fpvar);
159 int fp_test (char *infits, char *outfits, char *outfits2, fpstate fpvar);
160 int fp_pack_hdu (fitsfile *infptr, fitsfile *outfptr, fpstate fpvar,
161     int *islossless, int *status);
162 int fp_unpack_hdu (fitsfile *infptr, fitsfile *outfptr, fpstate fpvar, int *status);
163 int fits_read_image_speed (fitsfile *infptr, float *whole_elapse,
164     float *whole_cpu, float *row_elapse, float *row_cpu, int *status);
165 int fp_test_hdu (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
166 	fpstate fpvar, int *status);
167 int fp_test_table (fitsfile *infptr, fitsfile *outfptr, fitsfile *outfptr2,
168 	fpstate fpvar, int *status);
169 int marktime(int *status);
170 int gettime(float *elapse, float *elapscpu, int *status);
171 int fits_read_image_speed (fitsfile *infptr, float *whole_elapse,
172     float *whole_cpu, float *row_elapse, float *row_cpu, int *status);
173 
174 int fp_i2stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status);
175 int fp_i4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status);
176 int fp_r4stat(fitsfile *infptr, int naxis, long *naxes, imgstats *imagestats, int *status);
177 int fp_i2rescale(fitsfile *infptr, int naxis, long *naxes, double rescale,
178     fitsfile *outfptr, int *status);
179 int fp_i4rescale(fitsfile *infptr, int naxis, long *naxes, double rescale,
180     fitsfile *outfptr, int *status);
181 
182 int fp_msg (char *msg);
183 int fp_version (void);
184 int fp_noop (void);
185 
186 int fu_get_param (int argc, char *argv[], fpstate *fpptr);
187 int fu_usage (void);
188 int fu_hint (void);
189 int fu_help (void);
190