-
Notifications
You must be signed in to change notification settings - Fork 1
/
imcompress.c
9955 lines (8637 loc) · 372 KB
/
imcompress.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>
# include <ctype.h>
# include <time.h>
# include "fitsio2.h"
#define NULL_VALUE -2147483647 /* value used to represent undefined pixels */
#define ZERO_VALUE -2147483646 /* value used to represent zero-valued pixels */
/* nearest integer function */
# define NINT(x) ((x >= 0.) ? (int) (x + 0.5) : (int) (x - 0.5))
/* special quantize level value indicates that floating point image pixels */
/* should not be quantized and instead losslessly compressed (with GZIP) */
#define NO_QUANTIZE 9999
/* string array for storing the individual column compression stats */
char results[999][30];
float *fits_rand_value = 0;
int imcomp_write_nocompress_tile(fitsfile *outfptr, long row, int datatype,
void *tiledata, long tilelen, int nullcheck, void *nullflagval, int *status);
int imcomp_convert_tile_tshort(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, double actual_bzero, int *intlength, int *status);
int imcomp_convert_tile_tushort(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, int *intlength, int *status);
int imcomp_convert_tile_tint(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, int *intlength, int *status);
int imcomp_convert_tile_tuint(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, int *intlength, int *status);
int imcomp_convert_tile_tbyte(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, int *intlength, int *status);
int imcomp_convert_tile_tsbyte(fitsfile *outfptr, void *tiledata, long tilelen,
int nullcheck, void *nullflagval, int nullval, int zbitpix, double scale,
double zero, int *intlength, int *status);
int imcomp_convert_tile_tfloat(fitsfile *outfptr, long row, void *tiledata, long tilelen,
long tilenx, long tileny, int nullcheck, void *nullflagval, int nullval, int zbitpix,
double scale, double zero, int *intlength, int *flag, double *bscale, double *bzero,int *status);
int imcomp_convert_tile_tdouble(fitsfile *outfptr, long row, void *tiledata, long tilelen,
long tilenx, long tileny, int nullcheck, void *nullflagval, int nullval, int zbitpix,
double scale, double zero, int *intlength, int *flag, double *bscale, double *bzero, int *status);
static int unquantize_i1r4(long row,
unsigned char *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
unsigned char tnull, /* I - value of FITS TNULLn keyword if any */
float nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
float *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int unquantize_i2r4(long row,
short *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
short tnull, /* I - value of FITS TNULLn keyword if any */
float nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
float *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int unquantize_i4r4(long row,
INT32BIT *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
INT32BIT tnull, /* I - value of FITS TNULLn keyword if any */
float nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
float *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int unquantize_i1r8(long row,
unsigned char *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
unsigned char tnull, /* I - value of FITS TNULLn keyword if any */
double nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
double *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int unquantize_i2r8(long row,
short *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
short tnull, /* I - value of FITS TNULLn keyword if any */
double nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
double *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int unquantize_i4r8(long row,
INT32BIT *input, /* I - array of values to be converted */
long ntodo, /* I - number of elements in the array */
double scale, /* I - FITS TSCALn or BSCALE value */
double zero, /* I - FITS TZEROn or BZERO value */
int dither_method, /* I - which subtractive dither method to use */
int nullcheck, /* I - null checking code; 0 = don't check */
/* 1:set null pixels = nullval */
/* 2: if null pixel, set nullarray = 1 */
INT32BIT tnull, /* I - value of FITS TNULLn keyword if any */
double nullval, /* I - set null pixels, if nullcheck = 1 */
char *nullarray, /* I - bad pixel array, if nullcheck = 2 */
int *anynull, /* O - set to 1 if any pixels are null */
double *output, /* O - array of converted pixels */
int *status); /* IO - error status */
static int imcomp_float2nan(float *indata, long tilelen, int *outdata,
float nullflagval, int *status);
static int imcomp_double2nan(double *indata, long tilelen, LONGLONG *outdata,
double nullflagval, int *status);
static int fits_read_write_compressed_img(fitsfile *fptr, /* I - FITS file pointer */
int datatype, /* I - datatype of the array to be returned */
LONGLONG *infpixel, /* I - 'bottom left corner' of the subsection */
LONGLONG *inlpixel, /* I - 'top right corner' of the subsection */
long *ininc, /* I - increment to be applied in each dimension */
int nullcheck, /* I - 0 for no null checking */
/* 1: set undefined pixels = nullval */
void *nullval, /* I - value for undefined pixels */
int *anynul, /* O - set to 1 if any values are null; else 0 */
fitsfile *outfptr, /* I - FITS file pointer */
int *status);
static int fits_shuffle_8bytes(char *heap, LONGLONG length, int *status);
static int fits_shuffle_4bytes(char *heap, LONGLONG length, int *status);
static int fits_shuffle_2bytes(char *heap, LONGLONG length, int *status);
static int fits_unshuffle_8bytes(char *heap, LONGLONG length, int *status);
static int fits_unshuffle_4bytes(char *heap, LONGLONG length, int *status);
static int fits_unshuffle_2bytes(char *heap, LONGLONG length, int *status);
static int fits_int_to_longlong_inplace(int *intarray, long length, int *status);
static int fits_short_to_int_inplace(short *intarray, long length, int shift, int *status);
static int fits_ushort_to_int_inplace(unsigned short *intarray, long length, int shift, int *status);
static int fits_sbyte_to_int_inplace(signed char *intarray, long length, int *status);
static int fits_ubyte_to_int_inplace(unsigned char *intarray, long length, int *status);
static int fits_calc_tile_rows(long *tlpixel, long *tfpixel, int ndim, long *trowsize, long *ntrows, int *status);
/* only used for diagnoitic purposes */
/* int fits_get_case(int *c1, int*c2, int*c3); */
/*---------------------------------------------------------------------------*/
int fits_init_randoms(void) {
/* initialize an array of random numbers */
int ii;
double a = 16807.0;
double m = 2147483647.0;
double temp, seed;
FFLOCK;
if (fits_rand_value) {
FFUNLOCK;
return(0); /* array is already initialized */
}
/* allocate array for the random number sequence */
/* THIS MEMORY IS NEVER FREED */
fits_rand_value = calloc(N_RANDOM, sizeof(float));
if (!fits_rand_value) {
FFUNLOCK;
return(MEMORY_ALLOCATION);
}
/* We need a portable algorithm that anyone can use to generate this
exact same sequence of random number. The C 'rand' function is not
suitable because it is not available to Fortran or Java programmers.
Instead, use a well known simple algorithm published here:
"Random number generators: good ones are hard to find", Communications of the ACM,
Volume 31 , Issue 10 (October 1988) Pages: 1192 - 1201
*/
/* initialize the random numbers */
seed = 1;
for (ii = 0; ii < N_RANDOM; ii++) {
temp = a * seed;
seed = temp -m * ((int) (temp / m) );
fits_rand_value[ii] = (float) (seed / m);
}
FFUNLOCK;
/*
IMPORTANT NOTE: the 10000th seed value must have the value 1043618065 if the
algorithm has been implemented correctly */
if ( (int) seed != 1043618065) {
ffpmsg("fits_init_randoms generated incorrect random number sequence");
return(1);
} else {
return(0);
}
}
/*--------------------------------------------------------------------------*/
void bz_internal_error(int errcode)
{
/* external function declared by the bzip2 code in bzlib_private.h */
ffpmsg("bzip2 returned an internal error");
ffpmsg("This should never happen");
return;
}
/*--------------------------------------------------------------------------*/
int fits_set_compression_type(fitsfile *fptr, /* I - FITS file pointer */
int ctype, /* image compression type code; */
/* allowed values: RICE_1, GZIP_1, GZIP_2, PLIO_1, */
/* HCOMPRESS_1, BZIP2_1, and NOCOMPRESS */
int *status) /* IO - error status */
{
/*
This routine specifies the image compression algorithm that should be
used when writing a FITS image. The image is divided into tiles, and
each tile is compressed and stored in a row of at variable length binary
table column.
*/
if (ctype != RICE_1 &&
ctype != GZIP_1 &&
ctype != GZIP_2 &&
ctype != PLIO_1 &&
ctype != HCOMPRESS_1 &&
ctype != BZIP2_1 &&
ctype != NOCOMPRESS &&
ctype != 0)
{
ffpmsg("unknown compression algorithm (fits_set_compression_type)");
*status = DATA_COMPRESSION_ERR;
} else {
(fptr->Fptr)->request_compress_type = ctype;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_tile_dim(fitsfile *fptr, /* I - FITS file pointer */
int ndim, /* number of dimensions in the compressed image */
long *dims, /* size of image compression tile in each dimension */
/* default tile size = (NAXIS1, 1, 1, ...) */
int *status) /* IO - error status */
{
/*
This routine specifies the size (dimension) of the image
compression tiles that should be used when writing a FITS
image. The image is divided into tiles, and each tile is compressed
and stored in a row of at variable length binary table column.
*/
int ii;
if (ndim < 0 || ndim > MAX_COMPRESS_DIM)
{
*status = BAD_DIMEN;
ffpmsg("illegal number of tile dimensions (fits_set_tile_dim)");
return(*status);
}
for (ii = 0; ii < ndim; ii++)
{
(fptr->Fptr)->request_tilesize[ii] = dims[ii];
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_quantize_level(fitsfile *fptr, /* I - FITS file pointer */
float qlevel, /* floating point quantization level */
int *status) /* IO - error status */
{
/*
This routine specifies the value of the quantization level, q, that
should be used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
*/
if (qlevel == 0.)
{
/* this means don't quantize the floating point values. Instead, */
/* the floating point values will be losslessly compressed */
(fptr->Fptr)->request_quantize_level = NO_QUANTIZE;
} else {
(fptr->Fptr)->request_quantize_level = qlevel;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_quantize_method(fitsfile *fptr, /* I - FITS file pointer */
int method, /* quantization method */
int *status) /* IO - error status */
{
/*
This routine specifies what type of dithering (randomization) should
be performed when quantizing floating point images to integer prior to
compression. A value of -1 means do no dithering. A value of 0 means
use the default SUBTRACTIVE_DITHER_1 (which is equivalent to dither = 1).
A value of 2 means use SUBTRACTIVE_DITHER_2.
*/
if (method < -1 || method > 2)
{
ffpmsg("illegal dithering value (fits_set_quantize_method)");
*status = DATA_COMPRESSION_ERR;
} else {
if (method == 0) method = 1;
(fptr->Fptr)->request_quantize_method = method;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_quantize_dither(fitsfile *fptr, /* I - FITS file pointer */
int dither, /* dither type */
int *status) /* IO - error status */
{
/*
the name of this routine has changed. This is kept here only for backwards
compatibility for any software that may be calling the old routine.
*/
fits_set_quantize_method(fptr, dither, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_dither_seed(fitsfile *fptr, /* I - FITS file pointer */
int seed, /* random dithering seed value (1 to 10000) */
int *status) /* IO - error status */
{
/*
This routine specifies the value of the offset that should be applied when
calculating the random dithering when quantizing floating point iamges.
A random offset should be applied to each image to avoid quantization
effects when taking the difference of 2 images, or co-adding a set of
images. Without this random offset, the corresponding pixel in every image
will have exactly the same dithering.
offset = 0 means use the default random dithering based on system time
offset = negative means randomly chose dithering based on 1st tile checksum
offset = [1 - 10000] means use that particular dithering pattern
*/
/* if positive, ensure that the value is in the range 1 to 10000 */
if (seed > 10000) {
ffpmsg("illegal dithering seed value (fits_set_dither_seed)");
*status = DATA_COMPRESSION_ERR;
} else {
(fptr->Fptr)->request_dither_seed = seed;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_dither_offset(fitsfile *fptr, /* I - FITS file pointer */
int offset, /* random dithering offset value (1 to 10000) */
int *status) /* IO - error status */
{
/*
The name of this routine has changed. This is kept just for
backwards compatibility with any software that calls the old name
*/
fits_set_dither_seed(fptr, offset, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_noise_bits(fitsfile *fptr, /* I - FITS file pointer */
int noisebits, /* noise_bits parameter value */
/* (default = 4) */
int *status) /* IO - error status */
{
/*
********************************************************************
********************************************************************
THIS ROUTINE IS PROVIDED ONLY FOR BACKWARDS COMPATIBILITY;
ALL NEW SOFTWARE SHOULD CALL fits_set_quantize_level INSTEAD
********************************************************************
********************************************************************
This routine specifies the value of the noice_bits parameter that
should be used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
Feb 2008: the "noisebits" parameter has been replaced with the more
general "quantize level" parameter.
*/
float qlevel;
if (noisebits < 1 || noisebits > 16)
{
*status = DATA_COMPRESSION_ERR;
ffpmsg("illegal number of noise bits (fits_set_noise_bits)");
return(*status);
}
qlevel = (float) pow (2., (double)noisebits);
fits_set_quantize_level(fptr, qlevel, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_hcomp_scale(fitsfile *fptr, /* I - FITS file pointer */
float scale, /* hcompress scale parameter value */
/* (default = 0.) */
int *status) /* IO - error status */
{
/*
This routine specifies the value of the hcompress scale parameter.
*/
(fptr->Fptr)->request_hcomp_scale = scale;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_hcomp_smooth(fitsfile *fptr, /* I - FITS file pointer */
int smooth, /* hcompress smooth parameter value */
/* if scale > 1 and smooth != 0, then */
/* the image will be smoothed when it is */
/* decompressed to remove some of the */
/* 'blockiness' in the image produced */
/* by the lossy compression */
int *status) /* IO - error status */
{
/*
This routine specifies the value of the hcompress scale parameter.
*/
(fptr->Fptr)->request_hcomp_smooth = smooth;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_lossy_int(fitsfile *fptr, /* I - FITS file pointer */
int lossy_int, /* I - True (!= 0) or False (0) */
int *status) /* IO - error status */
{
/*
This routine specifies whether images with integer pixel values should
quantized and compressed the same way float images are compressed.
The default is to not do this, and instead apply a lossless compression
algorithm to integer images.
*/
(fptr->Fptr)->request_lossy_int_compress = lossy_int;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_huge_hdu(fitsfile *fptr, /* I - FITS file pointer */
int huge, /* I - True (!= 0) or False (0) */
int *status) /* IO - error status */
{
/*
This routine specifies whether the HDU that is being compressed is so large
(i.e., > 4 GB) that the 'Q' type variable length array columns should be used
rather than the normal 'P' type. The allows the heap pointers to be stored
as 64-bit quantities, rather than just 32-bits.
*/
(fptr->Fptr)->request_huge_hdu = huge;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_compression_type(fitsfile *fptr, /* I - FITS file pointer */
int *ctype, /* image compression type code; */
/* allowed values: */
/* RICE_1, GZIP_1, GZIP_2, PLIO_1, HCOMPRESS_1, BZIP2_1 */
int *status) /* IO - error status */
{
/*
This routine returns the image compression algorithm that should be
used when writing a FITS image. The image is divided into tiles, and
each tile is compressed and stored in a row of at variable length binary
table column.
*/
*ctype = (fptr->Fptr)->request_compress_type;
if (*ctype != RICE_1 &&
*ctype != GZIP_1 &&
*ctype != GZIP_2 &&
*ctype != PLIO_1 &&
*ctype != HCOMPRESS_1 &&
*ctype != BZIP2_1 &&
*ctype != NOCOMPRESS &&
*ctype != 0 )
{
ffpmsg("unknown compression algorithm (fits_get_compression_type)");
*status = DATA_COMPRESSION_ERR;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_tile_dim(fitsfile *fptr, /* I - FITS file pointer */
int ndim, /* number of dimensions in the compressed image */
long *dims, /* size of image compression tile in each dimension */
/* default tile size = (NAXIS1, 1, 1, ...) */
int *status) /* IO - error status */
{
/*
This routine returns the size (dimension) of the image
compression tiles that should be used when writing a FITS
image. The image is divided into tiles, and each tile is compressed
and stored in a row of at variable length binary table column.
*/
int ii;
if (ndim < 0 || ndim > MAX_COMPRESS_DIM)
{
*status = BAD_DIMEN;
ffpmsg("illegal number of tile dimensions (fits_get_tile_dim)");
return(*status);
}
for (ii = 0; ii < ndim; ii++)
{
dims[ii] = (fptr->Fptr)->request_tilesize[ii];
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_unset_compression_param(
fitsfile *fptr,
int *status)
{
int ii;
(fptr->Fptr)->compress_type = 0;
(fptr->Fptr)->quantize_level = 0;
(fptr->Fptr)->quantize_method = 0;
(fptr->Fptr)->dither_seed = 0;
(fptr->Fptr)->hcomp_scale = 0;
for (ii = 0; ii < MAX_COMPRESS_DIM; ii++)
{
(fptr->Fptr)->tilesize[ii] = 0;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_unset_compression_request(
fitsfile *fptr,
int *status)
{
int ii;
(fptr->Fptr)->request_compress_type = 0;
(fptr->Fptr)->request_quantize_level = 0;
(fptr->Fptr)->request_quantize_method = 0;
(fptr->Fptr)->request_dither_seed = 0;
(fptr->Fptr)->request_hcomp_scale = 0;
(fptr->Fptr)->request_lossy_int_compress = 0;
(fptr->Fptr)->request_huge_hdu = 0;
for (ii = 0; ii < MAX_COMPRESS_DIM; ii++)
{
(fptr->Fptr)->request_tilesize[ii] = 0;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_set_compression_pref(
fitsfile *infptr,
fitsfile *outfptr,
int *status)
{
/*
Set the preference for various compression options, based
on keywords in the input file that
provide guidance about how the HDU should be compressed when written
to the output file.
*/
int ii, naxis, nkeys, comptype;
int ivalue;
long tiledim[6]= {1,1,1,1,1,1};
char card[FLEN_CARD], value[FLEN_VALUE];
double qvalue;
float hscale;
LONGLONG datastart, dataend;
if (*status > 0)
return(*status);
/* check the size of the HDU that is to be compressed */
fits_get_hduaddrll(infptr, NULL, &datastart, &dataend, status);
if ( (LONGLONG)(dataend - datastart) > UINT32_MAX) {
/* use 64-bit '1Q' variable length columns instead of '1P' columns */
/* for large files, in case the heap size becomes larger than 2**32 bytes*/
fits_set_huge_hdu(outfptr, 1, status);
}
fits_get_hdrspace(infptr, &nkeys, NULL, status);
/* look for a image compression directive keywords (begin with 'FZ') */
for (ii = 2; ii <= nkeys; ii++) {
fits_read_record(infptr, ii, card, status);
if (!strncmp(card, "FZ", 2) ){
/* get the keyword value string */
fits_parse_value(card, value, NULL, status);
if (!strncmp(card+2, "ALGOR", 5) ) {
/* set the desired compression algorithm */
/* allowed values: RICE_1, GZIP_1, GZIP_2, PLIO_1, */
/* HCOMPRESS_1, BZIP2_1, and NOCOMPRESS */
if (!fits_strncasecmp(value, "'RICE_1", 7) ) {
comptype = RICE_1;
} else if (!fits_strncasecmp(value, "'GZIP_1", 7) ) {
comptype = GZIP_1;
} else if (!fits_strncasecmp(value, "'GZIP_2", 7) ) {
comptype = GZIP_2;
} else if (!fits_strncasecmp(value, "'PLIO_1", 7) ) {
comptype = PLIO_1;
} else if (!fits_strncasecmp(value, "'HCOMPRESS_1", 12) ) {
comptype = HCOMPRESS_1;
} else if (!fits_strncasecmp(value, "'NONE", 5) ) {
comptype = NOCOMPRESS;
} else {
ffpmsg("Unknown FZALGOR keyword compression algorithm:");
ffpmsg(value);
return(*status = DATA_COMPRESSION_ERR);
}
fits_set_compression_type (outfptr, comptype, status);
} else if (!strncmp(card+2, "TILE ", 6) ) {
if (!fits_strncasecmp(value, "'row", 4) ) {
tiledim[0] = -1;
} else if (!fits_strncasecmp(value, "'whole", 6) ) {
tiledim[0] = -1;
tiledim[1] = -1;
tiledim[2] = -1;
} else {
ffdtdm(infptr, value, 0,6, &naxis, tiledim, status);
}
/* set the desired tile size */
fits_set_tile_dim (outfptr, 6, tiledim, status);
} else if (!strncmp(card+2, "QVALUE", 6) ) {
/* set the desired Q quantization value */
qvalue = atof(value);
fits_set_quantize_level (outfptr, (float) qvalue, status);
} else if (!strncmp(card+2, "QMETHD", 6) ) {
if (!fits_strncasecmp(value, "'no_dither", 10) ) {
ivalue = -1; /* just quantize, with no dithering */
} else if (!fits_strncasecmp(value, "'subtractive_dither_1", 21) ) {
ivalue = SUBTRACTIVE_DITHER_1; /* use subtractive dithering */
} else if (!fits_strncasecmp(value, "'subtractive_dither_2", 21) ) {
ivalue = SUBTRACTIVE_DITHER_2; /* dither, except preserve zero-valued pixels */
} else {
ffpmsg("Unknown value for FZQUANT keyword: (set_compression_pref)");
ffpmsg(value);
return(*status = DATA_COMPRESSION_ERR);
}
fits_set_quantize_method(outfptr, ivalue, status);
} else if (!strncmp(card+2, "DTHRSD", 6) ) {
if (!fits_strncasecmp(value, "'checksum", 9) ) {
ivalue = -1; /* use checksum of first tile */
} else if (!fits_strncasecmp(value, "'clock", 6) ) {
ivalue = 0; /* set dithering seed based on system clock */
} else { /* read integer value */
if (*value == '\'')
ivalue = (int) atol(value+1); /* allow for leading quote character */
else
ivalue = (int) atol(value);
if (ivalue < 1 || ivalue > 10000) {
ffpmsg("Invalid value for FZDTHRSD keyword: (set_compression_pref)");
ffpmsg(value);
return(*status = DATA_COMPRESSION_ERR);
}
}
/* set the desired dithering */
fits_set_dither_seed(outfptr, ivalue, status);
} else if (!strncmp(card+2, "I2F", 3) ) {
/* set whether to convert integers to float then use lossy compression */
if (!fits_strcasecmp(value, "t") ) {
fits_set_lossy_int (outfptr, 1, status);
} else if (!fits_strcasecmp(value, "f") ) {
fits_set_lossy_int (outfptr, 0, status);
} else {
ffpmsg("Unknown value for FZI2F keyword: (set_compression_pref)");
ffpmsg(value);
return(*status = DATA_COMPRESSION_ERR);
}
} else if (!strncmp(card+2, "HSCALE ", 6) ) {
/* set the desired Hcompress scale value */
hscale = (float) atof(value);
fits_set_hcomp_scale (outfptr, hscale, status);
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_noise_bits(fitsfile *fptr, /* I - FITS file pointer */
int *noisebits, /* noise_bits parameter value */
/* (default = 4) */
int *status) /* IO - error status */
{
/*
********************************************************************
********************************************************************
THIS ROUTINE IS PROVIDED ONLY FOR BACKWARDS COMPATIBILITY;
ALL NEW SOFTWARE SHOULD CALL fits_set_quantize_level INSTEAD
********************************************************************
********************************************************************
This routine returns the value of the noice_bits parameter that
should be used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
Feb 2008: code changed to use the more general "quantize level" parameter
rather than the "noise bits" parameter. If quantize level is greater than
zero, then the previous noisebits parameter is approximately given by
noise bits = natural logarithm (quantize level) / natural log (2)
This result is rounded to the nearest integer.
*/
double qlevel;
qlevel = (fptr->Fptr)->request_quantize_level;
if (qlevel > 0. && qlevel < 65537. )
*noisebits = (int) ((log(qlevel) / log(2.0)) + 0.5);
else
*noisebits = 0;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_quantize_level(fitsfile *fptr, /* I - FITS file pointer */
float *qlevel, /* quantize level parameter value */
int *status) /* IO - error status */
{
/*
This routine returns the value of the noice_bits parameter that
should be used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
*/
if ((fptr->Fptr)->request_quantize_level == NO_QUANTIZE) {
*qlevel = 0;
} else {
*qlevel = (fptr->Fptr)->request_quantize_level;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_dither_seed(fitsfile *fptr, /* I - FITS file pointer */
int *offset, /* dithering offset parameter value */
int *status) /* IO - error status */
{
/*
This routine returns the value of the dithering offset parameter that
is used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
*/
*offset = (fptr->Fptr)->request_dither_seed;
return(*status);
}/*--------------------------------------------------------------------------*/
int fits_get_hcomp_scale(fitsfile *fptr, /* I - FITS file pointer */
float *scale, /* Hcompress scale parameter value */
int *status) /* IO - error status */
{
/*
This routine returns the value of the noice_bits parameter that
should be used when compressing floating point images. The image is
divided into tiles, and each tile is compressed and stored in a row
of at variable length binary table column.
*/
*scale = (fptr->Fptr)->request_hcomp_scale;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_get_hcomp_smooth(fitsfile *fptr, /* I - FITS file pointer */
int *smooth, /* Hcompress smooth parameter value */
int *status) /* IO - error status */
{
*smooth = (fptr->Fptr)->request_hcomp_smooth;
return(*status);
}
/*--------------------------------------------------------------------------*/
int fits_img_compress(fitsfile *infptr, /* pointer to image to be compressed */
fitsfile *outfptr, /* empty HDU for output compressed image */
int *status) /* IO - error status */
/*
This routine initializes the output table, copies all the keywords,
and loops through the input image, compressing the data and
writing the compressed tiles to the output table.
This is a high level routine that is called by the fpack and funpack
FITS compression utilities.
*/
{
int bitpix, naxis;
long naxes[MAX_COMPRESS_DIM];
/* int c1, c2, c3; */
if (*status > 0)
return(*status);
/* get datatype and size of input image */
if (fits_get_img_param(infptr, MAX_COMPRESS_DIM, &bitpix,
&naxis, naxes, status) > 0)
return(*status);
if (naxis < 1 || naxis > MAX_COMPRESS_DIM)
{
ffpmsg("Image cannot be compressed: NAXIS out of range");
return(*status = BAD_NAXIS);
}
/* create a new empty HDU in the output file now, before setting the */
/* compression preferences. This HDU will become a binary table that */
/* contains the compressed image. If necessary, create a dummy primary */
/* array, which much precede the binary table extension. */
ffcrhd(outfptr, status); /* this does nothing if the output file is empty */
if ((outfptr->Fptr)->curhdu == 0) /* have to create dummy primary array */
{
ffcrim(outfptr, 16, 0, NULL, status);
ffcrhd(outfptr, status);
} else {
/* unset any compress parameter preferences that may have been
set when closing the previous HDU in the output file */
fits_unset_compression_param(outfptr, status);
}
/* set any compress parameter preferences as given in the input file */
fits_set_compression_pref(infptr, outfptr, status);
/* special case: the quantization level is not given by a keyword in */
/* the HDU header, so we have to explicitly copy the requested value */
/* to the actual value */
/* do this in imcomp_get_compressed_image_par, instead
if ( (outfptr->Fptr)->request_quantize_level != 0.)
(outfptr->Fptr)->quantize_level = (outfptr->Fptr)->request_quantize_level;
*/
/* if requested, treat integer images same as a float image. */
/* Then the pixels will be quantized (lossy algorithm) to achieve */
/* higher amounts of compression than with lossless algorithms */
if ( (outfptr->Fptr)->request_lossy_int_compress != 0 && bitpix > 0)
bitpix = FLOAT_IMG; /* compress integer images as if float */
/* initialize output table */
if (imcomp_init_table(outfptr, bitpix, naxis, naxes, 0, status) > 0)
return (*status);
/* Copy the image header keywords to the table header. */
if (imcomp_copy_img2comp(infptr, outfptr, status) > 0)
return (*status);
/* turn off any intensity scaling (defined by BSCALE and BZERO */
/* keywords) so that unscaled values will be read by CFITSIO */
/* (except if quantizing an int image, same as a float image) */
if ( (outfptr->Fptr)->request_lossy_int_compress == 0 && bitpix > 0)
ffpscl(infptr, 1.0, 0.0, status);
/* force a rescan of the output file keywords, so that */
/* the compression parameters will be copied to the internal */
/* fitsfile structure used by CFITSIO */
ffrdef(outfptr, status);
/* turn off any intensity scaling (defined by BSCALE and BZERO */
/* keywords) so that unscaled values will be written by CFITSIO */
/* (except if quantizing an int image, same as a float image) */
if ( (outfptr->Fptr)->request_lossy_int_compress == 0 && bitpix > 0)
ffpscl(outfptr, 1.0, 0.0, status);
/* Read each image tile, compress, and write to a table row. */
imcomp_compress_image (infptr, outfptr, status);
/* force another rescan of the output file keywords, to */
/* update PCOUNT and TFORMn = '1PB(iii)' keyword values. */
ffrdef(outfptr, status);
/* unset any previously set compress parameter preferences */
fits_unset_compression_request(outfptr, status);
/*
fits_get_case(&c1, &c2, &c3);
printf("c1, c2, c3 = %d, %d, %d\n", c1, c2, c3);
*/
return (*status);
}
/*--------------------------------------------------------------------------*/
int imcomp_init_table(fitsfile *outfptr,
int inbitpix,
int naxis,
long *naxes,
int writebitpix, /* write the ZBITPIX, ZNAXIS, and ZNAXES keyword? */
int *status)
/*
create a BINTABLE extension for the output compressed image.
*/
{
char keyname[FLEN_KEYWORD], zcmptype[12];
int ii, remain, ndiv, addToDim, ncols, bitpix;
long nrows;
char *ttype[] = {"COMPRESSED_DATA", "ZSCALE", "ZZERO"};
char *tform[3];
char tf0[4], tf1[4], tf2[4];
char *tunit[] = {"\0", "\0", "\0" };
char comm[FLEN_COMMENT];
long actual_tilesize[MAX_COMPRESS_DIM]; /* Actual size to use for tiles */
int is_primary=0; /* Is this attempting to write to the primary? */
int nQualifyDims=0; /* For Hcompress, number of image dimensions with required pixels. */
int noHigherDims=1; /* Set to true if all tile dims other than x are size 1. */
int firstDim=-1, secondDim=-1; /* Indices of first and second tiles dimensions
with width > 1 */
if (*status > 0)
return(*status);
/* check for special case of losslessly compressing floating point */
/* images. Only compression algorithm that supports this is GZIP */
if ( (inbitpix < 0) && ((outfptr->Fptr)->request_quantize_level == NO_QUANTIZE) ) {
if (((outfptr->Fptr)->request_compress_type != GZIP_1) &&
((outfptr->Fptr)->request_compress_type != GZIP_2)) {
ffpmsg("Lossless compression of floating point images must use GZIP (imcomp_init_table)");
return(*status = DATA_COMPRESSION_ERR);
}
}
/* set default compression parameter values, if undefined */
if ( (outfptr->Fptr)->request_compress_type == 0) {
/* use RICE_1 by default */
(outfptr->Fptr)->request_compress_type = RICE_1;
}
if (inbitpix < 0 && (outfptr->Fptr)->request_quantize_level != NO_QUANTIZE) {
/* set defaults for quantizing floating point images */