-
Notifications
You must be signed in to change notification settings - Fork 10
/
fits_hdecompress.c
2614 lines (2434 loc) · 61.8 KB
/
fits_hdecompress.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 decompression 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
hinv.c
hsmooth.c
undigitize.c
decode.c
dodecode.c
qtree_decode.c
qread.c
bit_input.c
The following modifications have been made to the original code:
- commented out redundant "include" statements
- added the nextchar global variable
- changed all the 'extern' declarations to 'static', since all the routines are in
the same source file
- changed the first parameter in decode (and in lower level routines from a file stream
to a char array
- modified the myread routine, and lower level byte reading routines, to copy
the input bytes to a char array, instead of reading them from a file stream
- changed the function declarations to the more modern ANSI C style
- changed calls to printf and perror to call the CFITSIO ffpmsg routine
- replace "exit" statements with "return" statements
############################################################################ */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "fitsio2.h"
/* WDP added test to see if min and max are already defined */
#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef max
#define max(a,b) (((a)>(b))?(a):(b))
#endif
static long nextchar;
static int decode(unsigned char *infile, int *a, int *nx, int *ny, int *scale);
static int decode64(unsigned char *infile, LONGLONG *a, int *nx, int *ny, int *scale);
static int hinv(int a[], int nx, int ny, int smooth ,int scale);
static int hinv64(LONGLONG a[], int nx, int ny, int smooth ,int scale);
static void undigitize(int a[], int nx, int ny, int scale);
static void undigitize64(LONGLONG a[], int nx, int ny, int scale);
static void unshuffle(int a[], int n, int n2, int tmp[]);
static void unshuffle64(LONGLONG a[], int n, int n2, LONGLONG tmp[]);
static void hsmooth(int a[], int nxtop, int nytop, int ny, int scale);
static void hsmooth64(LONGLONG a[], int nxtop, int nytop, int ny, int scale);
static void qread(unsigned char *infile,char *a, int n);
static int readint(unsigned char *infile);
static LONGLONG readlonglong(unsigned char *infile);
static int dodecode(unsigned char *infile, int a[], int nx, int ny, unsigned char nbitplanes[3]);
static int dodecode64(unsigned char *infile, LONGLONG a[], int nx, int ny, unsigned char nbitplanes[3]);
static int qtree_decode(unsigned char *infile, int a[], int n, int nqx, int nqy, int nbitplanes);
static int qtree_decode64(unsigned char *infile, LONGLONG a[], int n, int nqx, int nqy, int nbitplanes);
static void start_inputing_bits(void);
static int input_bit(unsigned char *infile);
static int input_nbits(unsigned char *infile, int n);
/* make input_nybble a separate routine, for added effiency */
/* #define input_nybble(infile) input_nbits(infile,4) */
static int input_nybble(unsigned char *infile);
static int input_nnybble(unsigned char *infile, int n, unsigned char *array);
static void qtree_expand(unsigned char *infile, unsigned char a[], int nx, int ny, unsigned char b[]);
static void qtree_bitins(unsigned char a[], int nx, int ny, int b[], int n, int bit);
static void qtree_bitins64(unsigned char a[], int nx, int ny, LONGLONG b[], int n, int bit);
static void qtree_copy(unsigned char a[], int nx, int ny, unsigned char b[], int n);
static void read_bdirect(unsigned char *infile, int a[], int n, int nqx, int nqy, unsigned char scratch[], int bit);
static void read_bdirect64(unsigned char *infile, LONGLONG a[], int n, int nqx, int nqy, unsigned char scratch[], int bit);
static int input_huffman(unsigned char *infile);
/* ---------------------------------------------------------------------- */
int fits_hdecompress(unsigned char *input, int smooth, int *a, int *ny, int *nx,
int *scale, int *status)
{
/*
decompress the input byte stream using the H-compress algorithm
input - input array of compressed bytes
a - pre-allocated array to hold the output uncompressed image
nx - returned X axis size
ny - returned Y axis size
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);
/* decode the input array */
FFLOCK; /* decode uses the nextchar global variable */
stat = decode(input, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
if (stat) return(*status);
/*
* Un-Digitize
*/
undigitize(a, *nx, *ny, *scale);
/*
* Inverse H-transform
*/
stat = hinv(a, *nx, *ny, smooth, *scale);
*status = stat;
return(*status);
}
/* ---------------------------------------------------------------------- */
int fits_hdecompress64(unsigned char *input, int smooth, LONGLONG *a, int *ny, int *nx,
int *scale, int *status)
{
/*
decompress the input byte stream using the H-compress algorithm
input - input array of compressed bytes
a - pre-allocated array to hold the output uncompressed image
nx - returned X axis size
ny - returned Y axis size
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, *iarray, ii, nval;
if (*status > 0) return(*status);
/* decode the input array */
FFLOCK; /* decode uses the nextchar global variable */
stat = decode64(input, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
if (stat) return(*status);
/*
* Un-Digitize
*/
undigitize64(a, *nx, *ny, *scale);
/*
* Inverse H-transform
*/
stat = hinv64(a, *nx, *ny, smooth, *scale);
*status = stat;
/* pack the I*8 values back into an I*4 array */
iarray = (int *) a;
nval = (*nx) * (*ny);
for (ii = 0; ii < nval; ii++)
iarray[ii] = (int) a[ii];
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.
*/
/* hinv.c Inverse H-transform of NX x NY integer image
*
* Programmer: R. White Date: 23 July 1993
*/
/* ############################################################################ */
static int
hinv(int a[], int nx, int ny, int smooth ,int scale)
/*
int smooth; 0 for no smoothing, else smooth during inversion
int scale; used if smoothing is specified
*/
{
int nmax, log2n, i, j, k;
int nxtop,nytop,nxf,nyf,c;
int oddx,oddy;
int shift, bit0, bit1, bit2, mask0, mask1, mask2,
prnd0, prnd1, prnd2, nrnd0, nrnd1, nrnd2, lowbit0, lowbit1;
int h0, hx, hy, hc;
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("hinv: insufficient memory");
return(DATA_DECOMPRESSION_ERR);
}
/*
* set up masks, rounding parameters
*/
shift = 1;
bit0 = 1 << (log2n - 1);
bit1 = bit0 << 1;
bit2 = bit0 << 2;
mask0 = -bit0;
mask1 = mask0 << 1;
mask2 = mask0 << 2;
prnd0 = bit0 >> 1;
prnd1 = bit1 >> 1;
prnd2 = bit2 >> 1;
nrnd0 = prnd0 - 1;
nrnd1 = prnd1 - 1;
nrnd2 = prnd2 - 1;
/*
* round h0 to multiple of bit2
*/
a[0] = (a[0] + ((a[0] >= 0) ? prnd2 : nrnd2)) & mask2;
/*
* do log2n expansions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = 1;
nytop = 1;
nxf = nx;
nyf = ny;
c = 1<<log2n;
for (k = log2n-1; k>=0; k--) {
/*
* this somewhat cryptic code generates the sequence
* ntop[k-1] = (ntop[k]+1)/2, where ntop[log2n] = n
*/
c = c>>1;
nxtop = nxtop<<1;
nytop = nytop<<1;
if (nxf <= c) { nxtop -= 1; } else { nxf -= c; }
if (nyf <= c) { nytop -= 1; } else { nyf -= c; }
/*
* double shift and fix nrnd0 (because prnd0=0) on last pass
*/
if (k == 0) {
nrnd0 = 0;
shift = 2;
}
/*
* unshuffle in each dimension to interleave coefficients
*/
for (i = 0; i<nxtop; i++) {
unshuffle(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
unshuffle(&a[j],nxtop,ny,tmp);
}
/*
* smooth by interpolating coefficients if SMOOTH != 0
*/
if (smooth) hsmooth(a,nxtop,nytop,ny,scale);
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = ny*i; /* 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) {
h0 = a[s00 ];
hx = a[s10 ];
hy = a[s00+1];
hc = a[s10+1];
/*
* round hx and hy to multiple of bit1, hc to multiple of bit0
* h0 is already a multiple of bit2
*/
hx = (hx + ((hx >= 0) ? prnd1 : nrnd1)) & mask1;
hy = (hy + ((hy >= 0) ? prnd1 : nrnd1)) & mask1;
hc = (hc + ((hc >= 0) ? prnd0 : nrnd0)) & mask0;
/*
* propagate bit0 of hc to hx,hy
*/
lowbit0 = hc & bit0;
hx = (hx >= 0) ? (hx - lowbit0) : (hx + lowbit0);
hy = (hy >= 0) ? (hy - lowbit0) : (hy + lowbit0);
/*
* Propagate bits 0 and 1 of hc,hx,hy to h0.
* This could be simplified if we assume h0>0, but then
* the inversion would not be lossless for images with
* negative pixels.
*/
lowbit1 = (hc ^ hx ^ hy) & bit1;
h0 = (h0 >= 0)
? (h0 + lowbit0 - lowbit1)
: (h0 + ((lowbit0 == 0) ? lowbit1 : (lowbit0-lowbit1)));
/*
* Divide sums by 2 (4 last time)
*/
a[s10+1] = (h0 + hx + hy + hc) >> shift;
a[s10 ] = (h0 + hx - hy - hc) >> shift;
a[s00+1] = (h0 - hx + hy - hc) >> shift;
a[s00 ] = (h0 - hx - hy + hc) >> shift;
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[s00 ];
hx = a[s10 ];
hx = ((hx >= 0) ? (hx+prnd1) : (hx+nrnd1)) & mask1;
lowbit1 = hx & bit1;
h0 = (h0 >= 0) ? (h0 - lowbit1) : (h0 + lowbit1);
a[s10 ] = (h0 + hx) >> shift;
a[s00 ] = (h0 - hx) >> shift;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = ny*i;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = a[s00 ];
hy = a[s00+1];
hy = ((hy >= 0) ? (hy+prnd1) : (hy+nrnd1)) & mask1;
lowbit1 = hy & bit1;
h0 = (h0 >= 0) ? (h0 - lowbit1) : (h0 + lowbit1);
a[s00+1] = (h0 + hy) >> shift;
a[s00 ] = (h0 - hy) >> shift;
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 ];
a[s00 ] = h0 >> shift;
}
}
/*
* divide all the masks and rounding values by 2
*/
bit2 = bit1;
bit1 = bit0;
bit0 = bit0 >> 1;
mask1 = mask0;
mask0 = mask0 >> 1;
prnd1 = prnd0;
prnd0 = prnd0 >> 1;
nrnd1 = nrnd0;
nrnd0 = prnd0 - 1;
}
free(tmp);
return(0);
}
/* ############################################################################ */
static int
hinv64(LONGLONG a[], int nx, int ny, int smooth ,int scale)
/*
int smooth; 0 for no smoothing, else smooth during inversion
int scale; used if smoothing is specified
*/
{
int nmax, log2n, i, j, k;
int nxtop,nytop,nxf,nyf,c;
int oddx,oddy;
int shift;
LONGLONG mask0, mask1, mask2, prnd0, prnd1, prnd2, bit0, bit1, bit2;
LONGLONG nrnd0, nrnd1, nrnd2, lowbit0, lowbit1;
LONGLONG h0, hx, hy, hc;
int s10, s00;
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("hinv64: insufficient memory");
return(DATA_DECOMPRESSION_ERR);
}
/*
* set up masks, rounding parameters
*/
shift = 1;
bit0 = ((LONGLONG) 1) << (log2n - 1);
bit1 = bit0 << 1;
bit2 = bit0 << 2;
mask0 = -bit0;
mask1 = mask0 << 1;
mask2 = mask0 << 2;
prnd0 = bit0 >> 1;
prnd1 = bit1 >> 1;
prnd2 = bit2 >> 1;
nrnd0 = prnd0 - 1;
nrnd1 = prnd1 - 1;
nrnd2 = prnd2 - 1;
/*
* round h0 to multiple of bit2
*/
a[0] = (a[0] + ((a[0] >= 0) ? prnd2 : nrnd2)) & mask2;
/*
* do log2n expansions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = 1;
nytop = 1;
nxf = nx;
nyf = ny;
c = 1<<log2n;
for (k = log2n-1; k>=0; k--) {
/*
* this somewhat cryptic code generates the sequence
* ntop[k-1] = (ntop[k]+1)/2, where ntop[log2n] = n
*/
c = c>>1;
nxtop = nxtop<<1;
nytop = nytop<<1;
if (nxf <= c) { nxtop -= 1; } else { nxf -= c; }
if (nyf <= c) { nytop -= 1; } else { nyf -= c; }
/*
* double shift and fix nrnd0 (because prnd0=0) on last pass
*/
if (k == 0) {
nrnd0 = 0;
shift = 2;
}
/*
* unshuffle in each dimension to interleave coefficients
*/
for (i = 0; i<nxtop; i++) {
unshuffle64(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
unshuffle64(&a[j],nxtop,ny,tmp);
}
/*
* smooth by interpolating coefficients if SMOOTH != 0
*/
if (smooth) hsmooth64(a,nxtop,nytop,ny,scale);
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = ny*i; /* 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) {
h0 = a[s00 ];
hx = a[s10 ];
hy = a[s00+1];
hc = a[s10+1];
/*
* round hx and hy to multiple of bit1, hc to multiple of bit0
* h0 is already a multiple of bit2
*/
hx = (hx + ((hx >= 0) ? prnd1 : nrnd1)) & mask1;
hy = (hy + ((hy >= 0) ? prnd1 : nrnd1)) & mask1;
hc = (hc + ((hc >= 0) ? prnd0 : nrnd0)) & mask0;
/*
* propagate bit0 of hc to hx,hy
*/
lowbit0 = hc & bit0;
hx = (hx >= 0) ? (hx - lowbit0) : (hx + lowbit0);
hy = (hy >= 0) ? (hy - lowbit0) : (hy + lowbit0);
/*
* Propagate bits 0 and 1 of hc,hx,hy to h0.
* This could be simplified if we assume h0>0, but then
* the inversion would not be lossless for images with
* negative pixels.
*/
lowbit1 = (hc ^ hx ^ hy) & bit1;
h0 = (h0 >= 0)
? (h0 + lowbit0 - lowbit1)
: (h0 + ((lowbit0 == 0) ? lowbit1 : (lowbit0-lowbit1)));
/*
* Divide sums by 2 (4 last time)
*/
a[s10+1] = (h0 + hx + hy + hc) >> shift;
a[s10 ] = (h0 + hx - hy - hc) >> shift;
a[s00+1] = (h0 - hx + hy - hc) >> shift;
a[s00 ] = (h0 - hx - hy + hc) >> shift;
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[s00 ];
hx = a[s10 ];
hx = ((hx >= 0) ? (hx+prnd1) : (hx+nrnd1)) & mask1;
lowbit1 = hx & bit1;
h0 = (h0 >= 0) ? (h0 - lowbit1) : (h0 + lowbit1);
a[s10 ] = (h0 + hx) >> shift;
a[s00 ] = (h0 - hx) >> shift;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = ny*i;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = a[s00 ];
hy = a[s00+1];
hy = ((hy >= 0) ? (hy+prnd1) : (hy+nrnd1)) & mask1;
lowbit1 = hy & bit1;
h0 = (h0 >= 0) ? (h0 - lowbit1) : (h0 + lowbit1);
a[s00+1] = (h0 + hy) >> shift;
a[s00 ] = (h0 - hy) >> shift;
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 ];
a[s00 ] = h0 >> shift;
}
}
/*
* divide all the masks and rounding values by 2
*/
bit2 = bit1;
bit1 = bit0;
bit0 = bit0 >> 1;
mask1 = mask0;
mask0 = mask0 >> 1;
prnd1 = prnd0;
prnd0 = prnd0 >> 1;
nrnd1 = nrnd0;
nrnd0 = prnd0 - 1;
}
free(tmp);
return(0);
}
/* ############################################################################ */
static void
unshuffle(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 nhalf;
int *p1, *p2, *pt;
/*
* copy 2nd half of array to tmp
*/
nhalf = (n+1)>>1;
pt = tmp;
p1 = &a[n2*nhalf]; /* pointer to a[i] */
for (i=nhalf; i<n; i++) {
*pt = *p1;
p1 += n2;
pt += 1;
}
/*
* distribute 1st half of array to even elements
*/
p2 = &a[ n2*(nhalf-1) ]; /* pointer to a[i] */
p1 = &a[(n2*(nhalf-1))<<1]; /* pointer to a[2*i] */
for (i=nhalf-1; i >= 0; i--) {
*p1 = *p2;
p2 -= n2;
p1 -= (n2+n2);
}
/*
* now distribute 2nd half of array (in tmp) to odd elements
*/
pt = tmp;
p1 = &a[n2]; /* pointer to a[i] */
for (i=1; i<n; i += 2) {
*p1 = *pt;
p1 += (n2+n2);
pt += 1;
}
}
/* ############################################################################ */
static void
unshuffle64(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;
int nhalf;
LONGLONG *p1, *p2, *pt;
/*
* copy 2nd half of array to tmp
*/
nhalf = (n+1)>>1;
pt = tmp;
p1 = &a[n2*nhalf]; /* pointer to a[i] */
for (i=nhalf; i<n; i++) {
*pt = *p1;
p1 += n2;
pt += 1;
}
/*
* distribute 1st half of array to even elements
*/
p2 = &a[ n2*(nhalf-1) ]; /* pointer to a[i] */
p1 = &a[(n2*(nhalf-1))<<1]; /* pointer to a[2*i] */
for (i=nhalf-1; i >= 0; i--) {
*p1 = *p2;
p2 -= n2;
p1 -= (n2+n2);
}
/*
* now distribute 2nd half of array (in tmp) to odd elements
*/
pt = tmp;
p1 = &a[n2]; /* pointer to a[i] */
for (i=1; i<n; i += 2) {
*p1 = *pt;
p1 += (n2+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.
*/
/* hsmooth.c Smooth H-transform image by adjusting coefficients toward
* interpolated values
*
* Programmer: R. White Date: 13 April 1992
*/
/* ############################################################################ */
static void
hsmooth(int a[], int nxtop, int nytop, int ny, int scale)
/*
int a[]; array of H-transform coefficients
int nxtop,nytop; size of coefficient block to use
int ny; actual 1st dimension of array
int scale; truncation scale factor that was used
*/
{
int i, j;
int ny2, s10, s00, diff, dmax, dmin, s, smax;
int hm, h0, hp, hmm, hpm, hmp, hpp, hx2, hy2;
int m1,m2;
/*
* Maximum change in coefficients is determined by scale factor.
* Since we rounded during division (see digitize.c), the biggest
* permitted change is scale/2.
*/
smax = (scale >> 1);
if (smax <= 0) return;
ny2 = ny << 1;
/*
* We're indexing a as a 2-D array with dimensions (nxtop,ny) of which
* only (nxtop,nytop) are used. The coefficients on the edge of the
* array are not adjusted (which is why the loops below start at 2
* instead of 0 and end at nxtop-2 instead of nxtop.)
*/
/*
* Adjust x difference hx
*/
for (i = 2; i<nxtop-2; i += 2) {
s00 = ny*i; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop; j += 2) {
/*
* hp is h0 (mean value) in next x zone, hm is h0 in previous x zone
*/
hm = a[s00-ny2];
h0 = a[s00];
hp = a[s00+ny2];
/*
* diff = 8 * hx slope that would match h0 in neighboring zones
*/
diff = hp-hm;
/*
* monotonicity constraints on diff
*/
dmax = max( min( (hp-h0), (h0-hm) ), 0 ) << 2;
dmin = min( max( (hp-h0), (h0-hm) ), 0 ) << 2;
/*
* if monotonicity would set slope = 0 then don't change hx.
* note dmax>=0, dmin<=0.
*/
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
/*
* Compute change in slope limited to range +/- smax.
* Careful with rounding negative numbers when using
* shift for divide by 8.
*/
s = diff-(a[s10]<<3);
s = (s>=0) ? (s>>3) : ((s+7)>>3) ;
s = max( min(s, smax), -smax);
a[s10] = a[s10]+s;
}
s00 += 2;
s10 += 2;
}
}
/*
* Adjust y difference hy
*/
for (i = 0; i<nxtop; i += 2) {
s00 = ny*i+2;
s10 = s00+ny;
for (j = 2; j<nytop-2; j += 2) {
hm = a[s00-2];
h0 = a[s00];
hp = a[s00+2];
diff = hp-hm;
dmax = max( min( (hp-h0), (h0-hm) ), 0 ) << 2;
dmin = min( max( (hp-h0), (h0-hm) ), 0 ) << 2;
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
s = diff-(a[s00+1]<<3);
s = (s>=0) ? (s>>3) : ((s+7)>>3) ;
s = max( min(s, smax), -smax);
a[s00+1] = a[s00+1]+s;
}
s00 += 2;
s10 += 2;
}
}
/*
* Adjust curvature difference hc
*/
for (i = 2; i<nxtop-2; i += 2) {
s00 = ny*i+2;
s10 = s00+ny;
for (j = 2; j<nytop-2; j += 2) {
/*
* ------------------ y
* | hmp | | hpp | |
* ------------------ |
* | | h0 | | |
* ------------------ -------x
* | hmm | | hpm |
* ------------------
*/
hmm = a[s00-ny2-2];
hpm = a[s00+ny2-2];
hmp = a[s00-ny2+2];
hpp = a[s00+ny2+2];
h0 = a[s00];
/*
* diff = 64 * hc value that would match h0 in neighboring zones
*/
diff = hpp + hmm - hmp - hpm;
/*
* 2 times x,y slopes in this zone
*/
hx2 = a[s10 ]<<1;
hy2 = a[s00+1]<<1;
/*
* monotonicity constraints on diff
*/
m1 = min(max(hpp-h0,0)-hx2-hy2, max(h0-hpm,0)+hx2-hy2);
m2 = min(max(h0-hmp,0)-hx2+hy2, max(hmm-h0,0)+hx2+hy2);
dmax = min(m1,m2) << 4;
m1 = max(min(hpp-h0,0)-hx2-hy2, min(h0-hpm,0)+hx2-hy2);
m2 = max(min(h0-hmp,0)-hx2+hy2, min(hmm-h0,0)+hx2+hy2);
dmin = max(m1,m2) << 4;
/*
* if monotonicity would set slope = 0 then don't change hc.
* note dmax>=0, dmin<=0.
*/
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
/*
* Compute change in slope limited to range +/- smax.
* Careful with rounding negative numbers when using
* shift for divide by 64.
*/
s = diff-(a[s10+1]<<6);
s = (s>=0) ? (s>>6) : ((s+63)>>6) ;
s = max( min(s, smax), -smax);
a[s10+1] = a[s10+1]+s;
}
s00 += 2;
s10 += 2;
}
}
}
/* ############################################################################ */
static void
hsmooth64(LONGLONG a[], int nxtop, int nytop, int ny, int scale)
/*
LONGLONG a[]; array of H-transform coefficients
int nxtop,nytop; size of coefficient block to use
int ny; actual 1st dimension of array
int scale; truncation scale factor that was used
*/
{
int i, j;
int ny2, s10, s00;
LONGLONG hm, h0, hp, hmm, hpm, hmp, hpp, hx2, hy2, diff, dmax, dmin, s, smax, m1, m2;
/*
* Maximum change in coefficients is determined by scale factor.
* Since we rounded during division (see digitize.c), the biggest
* permitted change is scale/2.
*/
smax = (scale >> 1);
if (smax <= 0) return;
ny2 = ny << 1;
/*
* We're indexing a as a 2-D array with dimensions (nxtop,ny) of which
* only (nxtop,nytop) are used. The coefficients on the edge of the
* array are not adjusted (which is why the loops below start at 2
* instead of 0 and end at nxtop-2 instead of nxtop.)
*/
/*
* Adjust x difference hx
*/
for (i = 2; i<nxtop-2; i += 2) {
s00 = ny*i; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop; j += 2) {
/*
* hp is h0 (mean value) in next x zone, hm is h0 in previous x zone
*/
hm = a[s00-ny2];
h0 = a[s00];
hp = a[s00+ny2];
/*
* diff = 8 * hx slope that would match h0 in neighboring zones
*/
diff = hp-hm;
/*
* monotonicity constraints on diff
*/
dmax = max( min( (hp-h0), (h0-hm) ), 0 ) << 2;
dmin = min( max( (hp-h0), (h0-hm) ), 0 ) << 2;
/*
* if monotonicity would set slope = 0 then don't change hx.
* note dmax>=0, dmin<=0.
*/
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
/*
* Compute change in slope limited to range +/- smax.
* Careful with rounding negative numbers when using
* shift for divide by 8.
*/
s = diff-(a[s10]<<3);
s = (s>=0) ? (s>>3) : ((s+7)>>3) ;
s = max( min(s, smax), -smax);
a[s10] = a[s10]+s;
}
s00 += 2;
s10 += 2;
}
}
/*
* Adjust y difference hy
*/
for (i = 0; i<nxtop; i += 2) {
s00 = ny*i+2;
s10 = s00+ny;
for (j = 2; j<nytop-2; j += 2) {
hm = a[s00-2];
h0 = a[s00];
hp = a[s00+2];
diff = hp-hm;
dmax = max( min( (hp-h0), (h0-hm) ), 0 ) << 2;
dmin = min( max( (hp-h0), (h0-hm) ), 0 ) << 2;
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
s = diff-(a[s00+1]<<3);
s = (s>=0) ? (s>>3) : ((s+7)>>3) ;
s = max( min(s, smax), -smax);
a[s00+1] = a[s00+1]+s;
}
s00 += 2;
s10 += 2;
}
}
/*
* Adjust curvature difference hc
*/
for (i = 2; i<nxtop-2; i += 2) {
s00 = ny*i+2;
s10 = s00+ny;
for (j = 2; j<nytop-2; j += 2) {
/*
* ------------------ y
* | hmp | | hpp | |
* ------------------ |
* | | h0 | | |
* ------------------ -------x
* | hmm | | hpm |
* ------------------
*/
hmm = a[s00-ny2-2];
hpm = a[s00+ny2-2];
hmp = a[s00-ny2+2];
hpp = a[s00+ny2+2];
h0 = a[s00];
/*
* diff = 64 * hc value that would match h0 in neighboring zones
*/
diff = hpp + hmm - hmp - hpm;
/*
* 2 times x,y slopes in this zone
*/
hx2 = a[s10 ]<<1;
hy2 = a[s00+1]<<1;
/*
* monotonicity constraints on diff
*/
m1 = min(max(hpp-h0,0)-hx2-hy2, max(h0-hpm,0)+hx2-hy2);
m2 = min(max(h0-hmp,0)-hx2+hy2, max(hmm-h0,0)+hx2+hy2);
dmax = min(m1,m2) << 4;
m1 = max(min(hpp-h0,0)-hx2-hy2, min(h0-hpm,0)+hx2-hy2);
m2 = max(min(h0-hmp,0)-hx2+hy2, min(hmm-h0,0)+hx2+hy2);
dmin = max(m1,m2) << 4;
/*
* if monotonicity would set slope = 0 then don't change hc.
* note dmax>=0, dmin<=0.
*/
if (dmin < dmax) {
diff = max( min(diff, dmax), dmin);
/*
* Compute change in slope limited to range +/- smax.
* Careful with rounding negative numbers when using
* shift for divide by 64.
*/
s = diff-(a[s10+1]<<6);
s = (s>=0) ? (s>>6) : ((s+63)>>6) ;
s = max( min(s, smax), -smax);
a[s10+1] = a[s10+1]+s;
}
s00 += 2;
s10 += 2;
}
}
}
/* ############################################################################ */
/* ############################################################################ */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* undigitize.c undigitize H-transform
*
* Programmer: R. White Date: 9 May 1991
*/