forked from healpy/cfitsio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fits_hcompress.c
1859 lines (1684 loc) · 46 KB
/
fits_hcompress.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
/* #########################################################################
These routines to apply the H-compress compression algorithm to a 2-D Fits
image were written by R. White at the STScI and were obtained from the STScI at
http://www.stsci.edu/software/hcompress.html
This source file is a concatination of the following sources files in the
original distribution
htrans.c
digitize.c
encode.c
qwrite.c
doencode.c
bit_output.c
qtree_encode.c
The following modifications have been made to the original code:
- commented out redundant "include" statements
- added the noutchar global variable
- changed all the 'extern' declarations to 'static', since all the routines are in
the same source file
- changed the first parameter in encode (and in lower level routines from a file stream
to a char array
- modifid the encode routine to return the size of the compressed array of bytes
- changed calls to printf and perror to call the CFITSIO ffpmsg routine
- modified the mywrite routine, and lower level byte writing routines, to copy
the output bytes to a char array, instead of writing them to a file stream
- replace "exit" statements with "return" statements
- changed the function declarations to the more modern ANSI C style
############################################################################ */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "fitsio2.h"
static long noutchar;
static long noutmax;
static int htrans(int a[],int nx,int ny);
static void digitize(int a[], int nx, int ny, int scale);
static int encode(char *outfile, long *nlen, int a[], int nx, int ny, int scale);
static void shuffle(int a[], int n, int n2, int tmp[]);
static int htrans64(LONGLONG a[],int nx,int ny);
static void digitize64(LONGLONG a[], int nx, int ny, int scale);
static int encode64(char *outfile, long *nlen, LONGLONG a[], int nx, int ny, int scale);
static void shuffle64(LONGLONG a[], int n, int n2, LONGLONG tmp[]);
static void writeint(char *outfile, int a);
static void writelonglong(char *outfile, LONGLONG a);
static int doencode(char *outfile, int a[], int nx, int ny, unsigned char nbitplanes[3]);
static int doencode64(char *outfile, LONGLONG a[], int nx, int ny, unsigned char nbitplanes[3]);
static int qwrite(char *file, char buffer[], int n);
static int qtree_encode(char *outfile, int a[], int n, int nqx, int nqy, int nbitplanes);
static int qtree_encode64(char *outfile, LONGLONG a[], int n, int nqx, int nqy, int nbitplanes);
static void start_outputing_bits(void);
static void done_outputing_bits(char *outfile);
static void output_nbits(char *outfile, int bits, int n);
static void qtree_onebit(int a[], int n, int nx, int ny, unsigned char b[], int bit);
static void qtree_onebit64(LONGLONG a[], int n, int nx, int ny, unsigned char b[], int bit);
static void qtree_reduce(unsigned char a[], int n, int nx, int ny, unsigned char b[]);
static int bufcopy(unsigned char a[], int n, unsigned char buffer[], int *b, int bmax);
static void write_bdirect(char *outfile, int a[], int n,int nqx, int nqy, unsigned char scratch[], int bit);
static void write_bdirect64(char *outfile, LONGLONG a[], int n,int nqx, int nqy, unsigned char scratch[], int bit);
/* #define output_nybble(outfile,c) output_nbits(outfile,c,4) */
static void output_nybble(char *outfile, int bits);
static void output_nnybble(char *outfile, int n, unsigned char array[]);
#define output_huffman(outfile,c) output_nbits(outfile,code[c],ncode[c])
/* ---------------------------------------------------------------------- */
int fits_hcompress(int *a, int ny, int nx, int scale, char *output,
long *nbytes, int *status)
{
/*
compress the input image using the H-compress algorithm
a - input image array
nx - size of X axis of image
ny - size of Y axis of image
scale - quantization scale factor. Larger values results in more (lossy) compression
scale = 0 does lossless compression
output - pre-allocated array to hold the output compressed stream of bytes
nbyts - input value = size of the output buffer;
returned value = size of the compressed byte stream, in bytes
NOTE: the nx and ny dimensions as defined within this code are reversed from
the usual FITS notation. ny is the fastest varying dimension, which is
usually considered the X axis in the FITS image display
*/
int stat;
if (*status > 0) return(*status);
/* H-transform */
stat = htrans(a, nx, ny);
if (stat) {
*status = stat;
return(*status);
}
/* digitize */
digitize(a, nx, ny, scale);
/* encode and write to output array */
FFLOCK;
noutmax = *nbytes; /* input value is the allocated size of the array */
*nbytes = 0; /* reset */
stat = encode(output, nbytes, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
return(*status);
}
/* ---------------------------------------------------------------------- */
int fits_hcompress64(LONGLONG *a, int ny, int nx, int scale, char *output,
long *nbytes, int *status)
{
/*
compress the input image using the H-compress algorithm
a - input image array
nx - size of X axis of image
ny - size of Y axis of image
scale - quantization scale factor. Larger values results in more (lossy) compression
scale = 0 does lossless compression
output - pre-allocated array to hold the output compressed stream of bytes
nbyts - size of the compressed byte stream, in bytes
NOTE: the nx and ny dimensions as defined within this code are reversed from
the usual FITS notation. ny is the fastest varying dimension, which is
usually considered the X axis in the FITS image display
*/
int stat;
if (*status > 0) return(*status);
/* H-transform */
stat = htrans64(a, nx, ny);
if (stat) {
*status = stat;
return(*status);
}
/* digitize */
digitize64(a, nx, ny, scale);
/* encode and write to output array */
FFLOCK;
noutmax = *nbytes; /* input value is the allocated size of the array */
*nbytes = 0; /* reset */
stat = encode64(output, nbytes, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
return(*status);
}
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* htrans.c H-transform of NX x NY integer image
*
* Programmer: R. White Date: 11 May 1992
*/
/* ######################################################################### */
static int htrans(int a[],int nx,int ny)
{
int nmax, log2n, h0, hx, hy, hc, nxtop, nytop, i, j, k;
int oddx, oddy;
int shift, mask, mask2, prnd, prnd2, nrnd2;
int s10, s00;
int *tmp;
/*
* log2n is log2 of max(nx,ny) rounded up to next power of 2
*/
nmax = (nx>ny) ? nx : ny;
log2n = (int) (log((float) nmax)/log(2.0)+0.5);
if ( nmax > (1<<log2n) ) {
log2n += 1;
}
/*
* get temporary storage for shuffling elements
*/
tmp = (int *) malloc(((nmax+1)/2)*sizeof(int));
if(tmp == (int *) NULL) {
ffpmsg("htrans: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* set up rounding and shifting masks
*/
shift = 0;
mask = -2;
mask2 = mask << 1;
prnd = 1;
prnd2 = prnd << 1;
nrnd2 = prnd2 - 1;
/*
* do log2n reductions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = nx;
nytop = ny;
for (k = 0; k<log2n; k++) {
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = i*ny; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop-oddy; j += 2) {
/*
* Divide h0,hx,hy,hc by 2 (1 the first time through).
*/
h0 = (a[s10+1] + a[s10] + a[s00+1] + a[s00]) >> shift;
hx = (a[s10+1] + a[s10] - a[s00+1] - a[s00]) >> shift;
hy = (a[s10+1] - a[s10] + a[s00+1] - a[s00]) >> shift;
hc = (a[s10+1] - a[s10] - a[s00+1] + a[s00]) >> shift;
/*
* Throw away the 2 bottom bits of h0, bottom bit of hx,hy.
* To get rounding to be same for positive and negative
* numbers, nrnd2 = prnd2 - 1.
*/
a[s10+1] = hc;
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
s10 += 2;
}
if (oddy) {
/*
* do last element in row if row length is odd
* s00+1, s10+1 are off edge
*/
h0 = (a[s10] + a[s00]) << (1-shift);
hx = (a[s10] - a[s00]) << (1-shift);
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 1;
s10 += 1;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = i*ny;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = (a[s00+1] + a[s00]) << (1-shift);
hy = (a[s00+1] - a[s00]) << (1-shift);
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
}
if (oddy) {
/*
* do corner element if both row and column lengths are odd
* s00+1, s10, s10+1 are off edge
*/
h0 = a[s00] << (2-shift);
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
}
}
/*
* now shuffle in each dimension to group coefficients by order
*/
for (i = 0; i<nxtop; i++) {
shuffle(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
shuffle(&a[j],nxtop,ny,tmp);
}
/*
* image size reduced by 2 (round up if odd)
*/
nxtop = (nxtop+1)>>1;
nytop = (nytop+1)>>1;
/*
* divisor doubles after first reduction
*/
shift = 1;
/*
* masks, rounding values double after each iteration
*/
mask = mask2;
prnd = prnd2;
mask2 = mask2 << 1;
prnd2 = prnd2 << 1;
nrnd2 = prnd2 - 1;
}
free(tmp);
return(0);
}
/* ######################################################################### */
static int htrans64(LONGLONG a[],int nx,int ny)
{
int nmax, log2n, nxtop, nytop, i, j, k;
int oddx, oddy;
int shift;
int s10, s00;
LONGLONG h0, hx, hy, hc, prnd, prnd2, nrnd2, mask, mask2;
LONGLONG *tmp;
/*
* log2n is log2 of max(nx,ny) rounded up to next power of 2
*/
nmax = (nx>ny) ? nx : ny;
log2n = (int) (log((float) nmax)/log(2.0)+0.5);
if ( nmax > (1<<log2n) ) {
log2n += 1;
}
/*
* get temporary storage for shuffling elements
*/
tmp = (LONGLONG *) malloc(((nmax+1)/2)*sizeof(LONGLONG));
if(tmp == (LONGLONG *) NULL) {
ffpmsg("htrans64: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* set up rounding and shifting masks
*/
shift = 0;
mask = (LONGLONG) -2;
mask2 = mask << 1;
prnd = (LONGLONG) 1;
prnd2 = prnd << 1;
nrnd2 = prnd2 - 1;
/*
* do log2n reductions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = nx;
nytop = ny;
for (k = 0; k<log2n; k++) {
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = i*ny; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop-oddy; j += 2) {
/*
* Divide h0,hx,hy,hc by 2 (1 the first time through).
*/
h0 = (a[s10+1] + a[s10] + a[s00+1] + a[s00]) >> shift;
hx = (a[s10+1] + a[s10] - a[s00+1] - a[s00]) >> shift;
hy = (a[s10+1] - a[s10] + a[s00+1] - a[s00]) >> shift;
hc = (a[s10+1] - a[s10] - a[s00+1] + a[s00]) >> shift;
/*
* Throw away the 2 bottom bits of h0, bottom bit of hx,hy.
* To get rounding to be same for positive and negative
* numbers, nrnd2 = prnd2 - 1.
*/
a[s10+1] = hc;
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
s10 += 2;
}
if (oddy) {
/*
* do last element in row if row length is odd
* s00+1, s10+1 are off edge
*/
h0 = (a[s10] + a[s00]) << (1-shift);
hx = (a[s10] - a[s00]) << (1-shift);
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 1;
s10 += 1;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = i*ny;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = (a[s00+1] + a[s00]) << (1-shift);
hy = (a[s00+1] - a[s00]) << (1-shift);
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
}
if (oddy) {
/*
* do corner element if both row and column lengths are odd
* s00+1, s10, s10+1 are off edge
*/
h0 = a[s00] << (2-shift);
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
}
}
/*
* now shuffle in each dimension to group coefficients by order
*/
for (i = 0; i<nxtop; i++) {
shuffle64(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
shuffle64(&a[j],nxtop,ny,tmp);
}
/*
* image size reduced by 2 (round up if odd)
*/
nxtop = (nxtop+1)>>1;
nytop = (nytop+1)>>1;
/*
* divisor doubles after first reduction
*/
shift = 1;
/*
* masks, rounding values double after each iteration
*/
mask = mask2;
prnd = prnd2;
mask2 = mask2 << 1;
prnd2 = prnd2 << 1;
nrnd2 = prnd2 - 1;
}
free(tmp);
return(0);
}
/* ######################################################################### */
static void
shuffle(int a[], int n, int n2, int tmp[])
{
/*
int a[]; array to shuffle
int n; number of elements to shuffle
int n2; second dimension
int tmp[]; scratch storage
*/
int i;
int *p1, *p2, *pt;
/*
* copy odd elements to tmp
*/
pt = tmp;
p1 = &a[n2];
for (i=1; i < n; i += 2) {
*pt = *p1;
pt += 1;
p1 += (n2+n2);
}
/*
* compress even elements into first half of A
*/
p1 = &a[n2];
p2 = &a[n2+n2];
for (i=2; i<n; i += 2) {
*p1 = *p2;
p1 += n2;
p2 += (n2+n2);
}
/*
* put odd elements into 2nd half
*/
pt = tmp;
for (i = 1; i<n; i += 2) {
*p1 = *pt;
p1 += n2;
pt += 1;
}
}
/* ######################################################################### */
static void
shuffle64(LONGLONG a[], int n, int n2, LONGLONG tmp[])
{
/*
LONGLONG a[]; array to shuffle
int n; number of elements to shuffle
int n2; second dimension
LONGLONG tmp[]; scratch storage
*/
int i;
LONGLONG *p1, *p2, *pt;
/*
* copy odd elements to tmp
*/
pt = tmp;
p1 = &a[n2];
for (i=1; i < n; i += 2) {
*pt = *p1;
pt += 1;
p1 += (n2+n2);
}
/*
* compress even elements into first half of A
*/
p1 = &a[n2];
p2 = &a[n2+n2];
for (i=2; i<n; i += 2) {
*p1 = *p2;
p1 += n2;
p2 += (n2+n2);
}
/*
* put odd elements into 2nd half
*/
pt = tmp;
for (i = 1; i<n; i += 2) {
*p1 = *pt;
p1 += n2;
pt += 1;
}
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* digitize.c digitize H-transform
*
* Programmer: R. White Date: 11 March 1991
*/
/* ######################################################################### */
static void
digitize(int a[], int nx, int ny, int scale)
{
int d, *p;
/*
* round to multiple of scale
*/
if (scale <= 1) return;
d=(scale+1)/2-1;
for (p=a; p <= &a[nx*ny-1]; p++) *p = ((*p>0) ? (*p+d) : (*p-d))/scale;
}
/* ######################################################################### */
static void
digitize64(LONGLONG a[], int nx, int ny, int scale)
{
LONGLONG d, *p, scale64;
/*
* round to multiple of scale
*/
if (scale <= 1) return;
d=(scale+1)/2-1;
scale64 = scale; /* use a 64-bit int for efficiency in the big loop */
for (p=a; p <= &a[nx*ny-1]; p++) *p = ((*p>0) ? (*p+d) : (*p-d))/scale64;
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* encode.c encode H-transform and write to outfile
*
* Programmer: R. White Date: 2 February 1994
*/
static char code_magic[2] = { (char)0xDD, (char)0x99 };
/* ######################################################################### */
static int encode(char *outfile, long *nlength, int a[], int nx, int ny, int scale)
{
/* FILE *outfile; - change outfile to a char array */
/*
long * nlength returned length (in bytes) of the encoded array)
int a[]; input H-transform array (nx,ny)
int nx,ny; size of H-transform array
int scale; scale factor for digitization
*/
int nel, nx2, ny2, i, j, k, q, vmax[3], nsign, bits_to_go;
unsigned char nbitplanes[3];
unsigned char *signbits;
int stat;
noutchar = 0; /* initialize the number of compressed bytes that have been written */
nel = nx*ny;
/*
* write magic value
*/
qwrite(outfile, code_magic, sizeof(code_magic));
writeint(outfile, nx); /* size of image */
writeint(outfile, ny);
writeint(outfile, scale); /* scale factor for digitization */
/*
* write first value of A (sum of all pixels -- the only value
* which does not compress well)
*/
writelonglong(outfile, (LONGLONG) a[0]);
a[0] = 0;
/*
* allocate array for sign bits and save values, 8 per byte
(initialize to all zeros)
*/
signbits = (unsigned char *) calloc(1, (nel+7)/8);
if (signbits == (unsigned char *) NULL) {
ffpmsg("encode: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
nsign = 0;
bits_to_go = 8;
/* signbits[0] = 0; */
for (i=0; i<nel; i++) {
if (a[i] > 0) {
/*
* positive element, put zero at end of buffer
*/
signbits[nsign] <<= 1;
bits_to_go -= 1;
} else if (a[i] < 0) {
/*
* negative element, shift in a one
*/
signbits[nsign] <<= 1;
signbits[nsign] |= 1;
bits_to_go -= 1;
/*
* replace a by absolute value
*/
a[i] = -a[i];
}
if (bits_to_go == 0) {
/*
* filled up this byte, go to the next one
*/
bits_to_go = 8;
nsign += 1;
/* signbits[nsign] = 0; */
}
}
if (bits_to_go != 8) {
/*
* some bits in last element
* move bits in last byte to bottom and increment nsign
*/
signbits[nsign] <<= bits_to_go;
nsign += 1;
}
/*
* calculate number of bit planes for 3 quadrants
*
* quadrant 0=bottom left, 1=bottom right or top left, 2=top right,
*/
for (q=0; q<3; q++) {
vmax[q] = 0;
}
/*
* get maximum absolute value in each quadrant
*/
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
j=0; /* column counter */
k=0; /* row counter */
for (i=0; i<nel; i++) {
q = (j>=ny2) + (k>=nx2);
if (vmax[q] < a[i]) vmax[q] = a[i];
if (++j >= ny) {
j = 0;
k += 1;
}
}
/*
* now calculate number of bits for each quadrant
*/
/* this is a more efficient way to do this, */
for (q = 0; q < 3; q++) {
for (nbitplanes[q] = 0; vmax[q]>0; vmax[q] = vmax[q]>>1, nbitplanes[q]++) ;
}
/*
for (q = 0; q < 3; q++) {
nbitplanes[q] = (int) (log((float) (vmax[q]+1))/log(2.0)+0.5);
if ( (vmax[q]+1) > (1<<nbitplanes[q]) ) {
nbitplanes[q] += 1;
}
}
*/
/*
* write nbitplanes
*/
if (0 == qwrite(outfile, (char *) nbitplanes, sizeof(nbitplanes))) {
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
/*
* write coded array
*/
stat = doencode(outfile, a, nx, ny, nbitplanes);
/*
* write sign bits
*/
if (nsign > 0) {
if ( 0 == qwrite(outfile, (char *) signbits, nsign)) {
free(signbits);
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
}
free(signbits);
*nlength = noutchar;
if (noutchar >= noutmax) {
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
return(stat);
}
/* ######################################################################### */
static int encode64(char *outfile, long *nlength, LONGLONG a[], int nx, int ny, int scale)
{
/* FILE *outfile; - change outfile to a char array */
/*
long * nlength returned length (in bytes) of the encoded array)
LONGLONG a[]; input H-transform array (nx,ny)
int nx,ny; size of H-transform array
int scale; scale factor for digitization
*/
int nel, nx2, ny2, i, j, k, q, nsign, bits_to_go;
LONGLONG vmax[3];
unsigned char nbitplanes[3];
unsigned char *signbits;
int stat;
noutchar = 0; /* initialize the number of compressed bytes that have been written */
nel = nx*ny;
/*
* write magic value
*/
qwrite(outfile, code_magic, sizeof(code_magic));
writeint(outfile, nx); /* size of image */
writeint(outfile, ny);
writeint(outfile, scale); /* scale factor for digitization */
/*
* write first value of A (sum of all pixels -- the only value
* which does not compress well)
*/
writelonglong(outfile, a[0]);
a[0] = 0;
/*
* allocate array for sign bits and save values, 8 per byte
*/
signbits = (unsigned char *) calloc(1, (nel+7)/8);
if (signbits == (unsigned char *) NULL) {
ffpmsg("encode64: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
nsign = 0;
bits_to_go = 8;
/* signbits[0] = 0; */
for (i=0; i<nel; i++) {
if (a[i] > 0) {
/*
* positive element, put zero at end of buffer
*/
signbits[nsign] <<= 1;
bits_to_go -= 1;
} else if (a[i] < 0) {
/*
* negative element, shift in a one
*/
signbits[nsign] <<= 1;
signbits[nsign] |= 1;
bits_to_go -= 1;
/*
* replace a by absolute value
*/
a[i] = -a[i];
}
if (bits_to_go == 0) {
/*
* filled up this byte, go to the next one
*/
bits_to_go = 8;
nsign += 1;
/* signbits[nsign] = 0; */
}
}
if (bits_to_go != 8) {
/*
* some bits in last element
* move bits in last byte to bottom and increment nsign
*/
signbits[nsign] <<= bits_to_go;
nsign += 1;
}
/*
* calculate number of bit planes for 3 quadrants
*
* quadrant 0=bottom left, 1=bottom right or top left, 2=top right,
*/
for (q=0; q<3; q++) {
vmax[q] = 0;
}
/*
* get maximum absolute value in each quadrant
*/
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
j=0; /* column counter */
k=0; /* row counter */
for (i=0; i<nel; i++) {
q = (j>=ny2) + (k>=nx2);
if (vmax[q] < a[i]) vmax[q] = a[i];
if (++j >= ny) {
j = 0;
k += 1;
}
}
/*
* now calculate number of bits for each quadrant
*/
/* this is a more efficient way to do this, */
for (q = 0; q < 3; q++) {
for (nbitplanes[q] = 0; vmax[q]>0; vmax[q] = vmax[q]>>1, nbitplanes[q]++) ;
}
/*
for (q = 0; q < 3; q++) {
nbitplanes[q] = log((float) (vmax[q]+1))/log(2.0)+0.5;
if ( (vmax[q]+1) > (((LONGLONG) 1)<<nbitplanes[q]) ) {
nbitplanes[q] += 1;
}
}
*/
/*
* write nbitplanes
*/
if (0 == qwrite(outfile, (char *) nbitplanes, sizeof(nbitplanes))) {
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
/*
* write coded array
*/
stat = doencode64(outfile, a, nx, ny, nbitplanes);
/*
* write sign bits
*/
if (nsign > 0) {
if ( 0 == qwrite(outfile, (char *) signbits, nsign)) {
free(signbits);
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
}
free(signbits);
*nlength = noutchar;
if (noutchar >= noutmax) {
ffpmsg("encode64: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
return(stat);
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* qwrite.c Write binary data
*
* Programmer: R. White Date: 11 March 1991
*/
/* ######################################################################### */
static void
writeint(char *outfile, int a)
{
int i;
unsigned char b[4];
/* Write integer A one byte at a time to outfile.
*
* This is portable from Vax to Sun since it eliminates the
* need for byte-swapping.
*/
for (i=3; i>=0; i--) {
b[i] = a & 0x000000ff;
a >>= 8;
}
for (i=0; i<4; i++) qwrite(outfile, (char *) &b[i],1);
}
/* ######################################################################### */
static void
writelonglong(char *outfile, LONGLONG a)
{
int i;
unsigned char b[8];
/* Write integer A one byte at a time to outfile.
*
* This is portable from Vax to Sun since it eliminates the
* need for byte-swapping.
*/
for (i=7; i>=0; i--) {
b[i] = (unsigned char) (a & 0x000000ff);
a >>= 8;
}
for (i=0; i<8; i++) qwrite(outfile, (char *) &b[i],1);
}
/* ######################################################################### */
static int
qwrite(char *file, char buffer[], int n){
/*
* write n bytes from buffer into file
* returns number of bytes read (=n) if successful, <=0 if not
*/
if (noutchar + n > noutmax) return(0); /* buffer overflow */
memcpy(&file[noutchar], buffer, n);
noutchar += n;
return(n);
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* doencode.c Encode 2-D array and write stream of characters on outfile
*
* This version assumes that A is positive.
*
* Programmer: R. White Date: 7 May 1991
*/
/* ######################################################################### */
static int
doencode(char *outfile, int a[], int nx, int ny, unsigned char nbitplanes[3])
{