This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunim.c
1680 lines (1569 loc) · 42.6 KB
/
funim.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
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <funtoolsP.h>
/*
*
* private routines
*
*/
#define MAXEV 10240
/*
*
* _FunTableBin -- bin a blocked section of data from a table
*
*/
#ifdef ANSI_FUNC
static void *
_FunTableBin(Fun fun, void *buf, char *plist)
#else
static void *_FunTableBin(fun, buf, plist)
Fun fun;
void *buf;
char *plist;
#endif
{
int skip; /* temp for skipping data to end of ext */
int offset[2]; /* offset into event record for x, y */
int size[2]; /* size of x, y value in bytes */
int val[2]; /* x, y bin values of this event */
int convert; /* whether we have to convert to native */
int index; /* index into image array for this pixel */
int esize; /* max number of events to read at once */
int dofilt; /* true if we can filter */
int dovcol; /* true if we use a value column */
int voffset; /* offset into event record for vcol */
/* not used, so avoid gcc compiler warning */
/* int vsize; */ /* size of vcol in bytes */
int vtype; /* data type of event vcol */
int imdim; /* total dims of image buffer */
int itsnan; /* if value is a nan */
int transparent; /* allow everything through the filter? */
int *rbuf=NULL; /* valid event flags */
size_t i, j; /* loop counters */
size_t get; /* number of events to read */
size_t got; /* number of events read */
unsigned char *ebuf=NULL; /* event buffer */
unsigned char *eptr; /* current pointer into ebuf */
double block2; /* block * block */
double minval[2]; /* x, y min axis values */
double binsiz[2]; /* x, y bin values */
char *obuf; /* output image buffer */
char *pbuf; /* temp plist buf */
char tbuf[SZ_LINE]; /* ever-present temp buffer */
short sval; /* short val */
unsigned short usval; /* unsigned short val */
int ival; /* int val */
longlong lval; /* 64-bit int val */
unsigned int uival; /* unsigned int vala */
float fval; /* float val */
double dval; /* double val */
double vval; /* value of the event */
/* just in case ... */
if( !_FunValid(fun) ){
gerror(stderr, "invalid funtools handle\n");
return NULL;
}
/* if we delayed the open before, we have to open now */
if( !fun->header && strchr(fun->mode, 'r') )
_FunFITSOpen(fun, fun->fname, "r");
/* make sure something good can happen */
if( !fun->header || !fun->header->table ){
gerror(stderr, "not a binary table\n");
return NULL;
}
/* make sure we have binning parameters */
if( (fun->bin[0] < 0) || ((fun->dims > 1) && (fun->bin[1] < 0)) ){
gerror(stderr, "invalid or missing binning column(s) for table\n");
return NULL;
}
/* make sure we have valid output dimensions */
if( (fun->odim1 <= 0) || ((fun->dims > 1) && (fun->odim2 <= 0)) ){
gerror(stderr, "invalid or zero image dimension(s) for table (%s;%d,%d)\n",
fun->bincols ? fun->bincols : "invalid bincols?",
fun->odim1, fun->odim2);
return NULL;
}
/* we have to convert to native if the data is not the
same as the big-endian-ness of the machine */
convert = (fun->endian != is_bigendian());
/* set x and y info -- whatever columns they actually are */
offset[0] = fun->header->table->col[fun->bin[0]].offset;
size[0] = fun->header->table->col[fun->bin[0]].size;
minval[0] = fun->min1;
binsiz[0] = fun->binsiz1;
if( fun->dims > 1 ){
offset[1] = fun->header->table->col[fun->bin[1]].offset;
size[1] = fun->header->table->col[fun->bin[1]].size;
minval[1] = fun->min2;
binsiz[1] = fun->binsiz2;
}
/* optional value column for binning */
if( fun->vbin >= 0 ){
dovcol = 1;
voffset = fun->header->table->col[fun->vbin].offset;
/* not used, so avoid gcc compiler warning */
/* vsize = fun->header->table->col[fun->vbin].size; */
vtype = fun->header->table->col[fun->vbin].type;
}
else{
dovcol = 0;
}
/* init some convenience variables */
imdim = fun->odim1*MAX(fun->odim2,1);
block2 = (double)fun->block*fun->block;
transparent = 0;
/* see if default data type is to be over-ridden */
pbuf = xstrdup(plist);
if( _FunKeyword(pbuf, "bitpix", NULL, tbuf, SZ_LINE) ){
if( (i = atoi(tbuf)) ){
fun->obitpix = i;
fun->odtype = fun->obitpix / FT_WORDLEN;
}
}
else if( _FunKeyword(pbuf, "mask", "FUN_MASK", tbuf, SZ_LINE) ){
if( !strcasecmp(tbuf, "transparent") )
transparent = 1;
}
if( pbuf ) xfree(pbuf);
/* allocate output buffer, if necessary */
if( buf )
obuf = buf;
else{
obuf = (char *)xcalloc(imdim, ABS(fun->odtype));
if( !obuf ){
gerror(stderr, "can't allocate image buffer (%dx%dx%d)\n",
fun->odim1, MAX(fun->odim2,1), ABS(fun->odtype));
return NULL;
}
}
/* create the plist string */
if( convert )
strcpy(tbuf, "convert=true");
else
strcpy(tbuf, "convert=false");
/* add the binning key */
if( fun->bincols ){
strcat(tbuf, ",");
strcat(tbuf, fun->bincols);
}
/* add the index flag */
if( fun->idx == 1 ){
strcat(tbuf, ",idx=true");
}
else if( fun->idx == -1 ){
strcat(tbuf, ",idx=false");
}
/* open the filter, if its not already been done */
if( !fun->filt ){
if( fun->filter && *fun->filter ){
fun->filt = FilterOpen(fun->header, fun->filter, tbuf);
/* check for error */
if( fun->filt == NULL ){
fun->filt = NOFILTER;
gerror(stderr, "can't open filter: %s\n", fun->filter);
return NULL;
}
}
else{
fun->filt = NOFILTER;
fun->dofilt = 0;
}
}
/* allocate space for a pile of event records */
for(esize=MIN(MAXEV,(fun->left>0?fun->left:MAXEV)); esize>0; esize /=2){
if( ((ebuf = (unsigned char *)malloc(fun->rawsize*esize)) != NULL) &&
((rbuf = (int *)malloc(sizeof(int)*esize)) != NULL) )
break;
}
/* while there are still events to read ... */
while( fun->left != 0 ){
/* figure out how many to read this time */
if( fun->left > 0 )
get = MIN(esize, fun->left);
/* read to EOF */
else
get = esize;
/* read in a pile of events */
_FunRead(fun, (void *)ebuf, fun->rawsize, get, &got);
/* if we got what we asked for ... */
if( got == get ){
/* if we are reading a set number of rows ... */
if( fun->left > 0 ){
/* subtract the number we just got */
fun->left -= got;
/* just in case! */
if( fun->left < 0 )
fun->left = 0;
}
}
/* didn't get what we asked for ... */
else{
/* if we are reading a set number of rows ... */
if( fun->left > 0 ){
/* using an index => already filtering, so fewer rows are acceptable */
if( (fun->filt == NOFILTER) || (fun->filt->doidx != 1) ){
/* otherwise its unexpected */
gerror(stderr, "unexpected EOF reading rows\n");
}
/* ensure we stop after this iteration */
fun->left = 0;
}
/* if we are reading til EOF, we just got there */
else{
fun->left = 0;
}
}
/* update how many input events we have read */
fun->io += got;
fun->bytes += (got*fun->rawsize);
/* filter these events through the co-process */
if( fun->dofilt )
dofilt = FilterEvents(fun->filt, (void *)ebuf, fun->rawsize, (int)got,
(void *)rbuf);
else
dofilt = 0;
/* process each event in this batch */
for(eptr=ebuf, i=0; i<got; i++, eptr += fun->rawsize){
/* if its not a valid event, skip it */
if( dofilt && !transparent && !rbuf[i] ){
continue;
}
/* get y and x values and convert to int data type for indexing */
for(j=0; j<(size_t)fun->dims; j++){
switch(fun->header->table->col[fun->bin[j]].type){
/* unsigned 8-bit int */
case 'B':
val[j] = itlp2i((double)eptr[offset[j]], minval[j], binsiz[j], 'B');
break;
/* 16-bit short */
case 'I':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &sval);
val[j] = itlp2i((double)sval, minval[j], binsiz[j], 'I');
break;
/* unsigned 16-bit int */
case 'U':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &usval);
val[j] = itlp2i((double)usval, minval[j], binsiz[j], 'U');
break;
/* 32-bit int */
case 'J':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &ival);
val[j] = itlp2i((double)ival, minval[j], binsiz[j], 'J');
break;
/* unsigned 32-bit int */
case 'K':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &lval);
val[j] = itlp2i((double)ival, minval[j], binsiz[j], 'K');
break;
/* unsigned 32-bit int */
case 'V':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &uival);
val[j] = itlp2i((double)uival, minval[j], binsiz[j], 'V');
break;
/* 32-bit float */
case 'E':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &fval);
val[j] = itlp2i((double)fval, minval[j], binsiz[j], 'E');
break;
/* 64-bit float */
case 'D':
ColumnLoad((void *)(eptr+offset[j]), size[j], 1, convert, &dval);
val[j] = itlp2i(dval, minval[j], binsiz[j], 'D');
break;
case 'X':
case 'L':
case 'C':
case 'A':
case 'M':
case 'P':
default:
goto done;
}
}
/* make sure we are in the desired section, and
calculate the offset into the image array */
if( fun->dims == 1 ){
if( (val[0] < fun->x0) || (val[0] > fun->x1) )
continue;
index = (val[0] - fun->x0)/fun->block;
}
else{
if( (val[0] < fun->x0) || (val[0] > fun->x1) ||
(val[1] < fun->y0) || (val[1] > fun->y1) )
continue;
index = ((val[1] - fun->y0)/fun->block)*fun->odim1 +
(val[0] - fun->x0)/fun->block;
}
/* make sure its valid */
if( (index<0) || (index>=imdim) )
continue;
/* use value column or 1 as event value */
itsnan = 0;
if( dovcol ){
/* if vcol is not double, we must do full type convert */
if( vtype != 'D' ){
ft_acht2('D', &vval, vtype, eptr+voffset, 1, convert, 0);
}
/* for same types, we might have to swap the bytes of the double */
else if( convert ){
swap8((char *)&vval, (char *)(eptr+voffset), 8);
}
/* otherwise its an easy copy */
else{
memcpy(&vval, eptr+voffset, ft_sizeof(vtype));
}
/* value operators */
switch(fun->vop){
case '/':
if( vval ){
vval = 1.0/vval;
}
else{
itsnan = 1;
}
break;
default:
break;
}
if( vval == getnand() ){
itsnan = 1;
vval = 0.0;
}
}
else{
vval = 1.0;
}
/* add this event to the image array */
switch(fun->odtype){
case TY_CHAR:
if( !dovcol ){
if( ((unsigned char *)obuf)[index] >
(((unsigned char *)obuf)[index]+(unsigned char)vval) ){
fun->overflow++;
}
else{
((unsigned char *)obuf)[index] += (unsigned char)vval;
}
}
else{
if(fun->doblank && (((unsigned char *)obuf)[index] == fun->blank)){
;
}
else if( itsnan ){
((unsigned char *)obuf)[index] = fun->blank;
}
else{
((unsigned char *)obuf)[index] += (unsigned char)vval;
}
}
break;
/* 32-bit int */
case TY_USHORT:
if( !dovcol ){
if( ((unsigned short *)obuf)[index] >
(((unsigned short *)obuf)[index]+(unsigned short)vval) ){
fun->overflow++;
}
else{
((unsigned short *)obuf)[index] += (unsigned short)vval;
}
}
else{
if(fun->doblank && (((unsigned short *)obuf)[index] == fun->blank)){
;
}
else if( itsnan ){
((unsigned short *)buf)[index] = fun->blank;
}
else{
((unsigned short *)buf)[index] += (unsigned short)vval;
}
}
break;
/* 32-bit int */
case TY_SHORT:
if( !dovcol ){
if( ((short *)obuf)[index] > (((short *)obuf)[index]+(short)vval) ){
fun->overflow++;
}
else{
((short *)obuf)[index] += (short)vval;
}
}
else{
if( fun->doblank && (((short *)obuf)[index] == fun->blank) ){
;
}
else if( itsnan ){
((short *)obuf)[index] = fun->blank;
}
else{
((short *)obuf)[index] += (short)vval;
}
}
break;
/* 32-bit int */
case TY_INT:
if( !dovcol ){
if( ((int *)obuf)[index] > (((int *)obuf)[index]+(int)vval) ){
fun->overflow++;
}
else{
((int *)obuf)[index] += (int)vval;
}
}
else{
if( fun->doblank && (((int *)obuf)[index] == fun->blank) ){
;
}
else if( itsnan ){
((int *)obuf)[index] = fun->blank;
}
else{
((int *)obuf)[index] += (int)vval;
}
}
break;
/* 64-bit int */
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
if( !dovcol ){
if(((longlong *)obuf)[index] >
(((longlong *)obuf)[index]+(longlong)vval)){
fun->overflow++;
}
else{
((longlong *)obuf)[index] += (longlong)vval;
}
}
else{
if( fun->doblank && (((longlong *)obuf)[index] == fun->blank) ){
;
}
else if( itsnan ){
((longlong *)obuf)[index] = fun->blank;
}
else{
((longlong *)obuf)[index] += (longlong)vval;
}
}
break;
/* 32-bit float */
case TY_FLOAT:
if( !dovcol ){
((float *)obuf)[index] += (float)vval;
}
else{
if( isnanf(((float *)obuf)[index]) ){
;
}
else if( itsnan ){
((float *)obuf)[index] = getnanf();
}
else{
((float *)obuf)[index] += (float)vval;
}
}
break;
/* 64-bit float */
case TY_DOUBLE:
if( !dovcol ){
((double *)obuf)[index] += (double)vval;
}
else{
if( isnand(((double *)obuf)[index]) ){
;
}
else if( itsnan ){
((double *)obuf)[index] = getnand();
}
else{
((double *)obuf)[index] += (double)vval;
}
}
break;
default:
goto done;
}
}
}
/* binned all events, average each pixel, if necessary */
if( fun->btype == FUN_AVG ){
for(i=0; i<(size_t)imdim; i++){
switch(fun->odtype){
case TY_CHAR:
((unsigned char *)obuf)[i] /= block2;
break;
case TY_USHORT:
((unsigned short *)obuf)[i] /= block2;
break;
case TY_SHORT:
((short *)obuf)[i] /= block2;
break;
case TY_INT:
((int *)obuf)[i] /= block2;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
((longlong *)obuf)[i] /= block2;
break;
case TY_FLOAT:
((float *)obuf)[i] /= block2;
break;
case TY_DOUBLE:
((double *)obuf)[i] /= block2;
break;
default:
break;
}
}
}
done:
/* free up space */
if( ebuf ) xfree(ebuf);
if( rbuf ) xfree(rbuf);
/* if we have no more rows left, clean up */
if( !fun->left ){
/* skip to end of extension */
if( fun->bytes ){
/* skip any unread events */
skip = (fun->rawsize*fun->total) - fun->bytes;
gskip(fun->gio, (off_t)skip);
fun->bytes += skip;
/* skip padding */
if( (skip = FT_BLOCK - (fun->bytes % FT_BLOCK)) == FT_BLOCK )
skip = 0;
gskip(fun->gio, (off_t)skip);
/* reset io flag */
/* fun->io = 0; */
}
}
/* close filter if done with the filter process */
if( !fun->left && (fun->filt != NOFILTER) ){
FilterClose(fun->filt);
fun->filt = NULL;
}
/* that's good news */
return (void *)obuf;
}
/*
*
* _FunImageExtract -- grab a blocked section of data from an array
*
*/
#ifdef ANSI_FUNC
static void *
_FunImageExtract(Fun fun, void *buf, int rstart, int rstop, char *plist)
#else
static void *_FunImageExtract(fun, buf, rstart, rstop, plist)
Fun fun;
void *buf;
int rstart;
int rstop;
char *plist;
#endif
{
int i, j, k;
int ox, oy;
int convert;
int yoff;
int doscale=0;
size_t got;
off_t newpos;
char *obuf;
char *pbuf;
char **bufs;
char tbuf[SZ_LINE];
unsigned char cval;
unsigned short usval;
short sval;
int ival;
longlong lval;
float fval;
double dval;
double block2;
register double val;
longlong vall;
/* make sure we have something */
if( !_FunValid(fun) )
return NULL;
/* if we delayed the open before, we have to open now */
if( !fun->header && strchr(fun->mode, 'r') )
_FunFITSOpen(fun, fun->fname, "r");
/* just in case ... */
if( !fun->header || !fun->header->image ){
gerror(stderr, "data is not a table or an image\n");
return NULL;
}
/* make sure we have valid dimensions */
if( fun->odim1 <=0 ){
gerror(stderr, "invalid dim1 (%d) from x0,x1=%d,%d\n",
fun->odim1, fun->x0, fun->x1);
return NULL;
}
if( (fun->dims > 1) && (fun->odim2 <=0) ){
gerror(stderr, "invalid dim2 (%d) from y0,y1=%d,%d\n",
fun->odim2, fun->y0, fun->y1);
return NULL;
}
/* we have to convert to native if the data is not the
same as the big-endian-ness of the machine */
convert = (fun->endian != is_bigendian());
/* set up some oft-used variables */
block2 = (double)fun->block*fun->block;
/* we cannot handle BLANK==0 */
if( (fun->dtype>=-2) && fun->doblank && (fun->blank==0) ){
gwarning(stderr, "BLANK==0 detected ... disabling NaN checking\n");
fun->doblank = 0;
}
/* see if default data type is to be over-ridden */
pbuf = xstrdup(plist);
if( _FunKeyword(pbuf, "bitpix", NULL, tbuf, SZ_LINE) ){
if( (i = atoi(tbuf)) ){
fun->obitpix = i;
fun->odtype = fun->obitpix / FT_WORDLEN;
}
}
/* see if scaling is explicitly turned off */
if( _FunKeyword(pbuf, "bscale", NULL, tbuf, SZ_LINE) ){
if( isfalse(tbuf) )
doscale = -1;
}
if( pbuf ) xfree(pbuf);
/* actually, we only scale if we have scaled int data */
if( (doscale==0) && (fun->scaled&FUN_SCALE_EXISTS) && (fun->dtype>=-2) ){
/* flag that we scaled the input data */
fun->scaled |= FUN_SCALE_APPLIED;
doscale = 1;
}
else
doscale = 0;
/* make sure we are not out of bounds */
if( rstart < 1 ) rstart = 1;
if( rstop < 1 ) rstop = 1;
if( rstop > MAX(fun->odim2,1) ) rstop = MAX(fun->odim2,1);
/* allocate output buffer, if necessary */
if( buf )
obuf = buf;
else{
obuf = (char *)xcalloc(fun->odim1*(rstop-rstart+1), ABS(fun->odtype));
if( !obuf ){
gerror(stderr, "can't allocate image buffer (%dx%dx%d)\n",
fun->odim1, (rstop-rstart+1), ABS(fun->odtype));
return NULL;
}
}
/* allocate some row buffers: number of bufs is equal to the block factor */
bufs = (char **)xmalloc(fun->block * sizeof(char *));
for(i=0; i<fun->block; i++){
bufs[i] = (char *)xmalloc((fun->x1-fun->x0+1)*ABS(fun->dtype));
}
/* for all rows in the output image (or image section) ... */
for(oy=rstart; oy<=rstop; oy++){
yoff = (oy-rstart) * fun->odim1;
/* get input rows associated with this output row (by block factor) */
for(j=0; j<fun->block; i++, j++){
/* calculate specific y line we are processing */
i = (fun->y0 - 1) + ((oy-1) * fun->block) + j;
/* calculate the new position into the data */
newpos = ((i*fun->dim1)+(fun->x0-1)) * ABS(fun->dtype);
/* skip to new position */
if( gskip(fun->gio, newpos - fun->curpos) < 0 ){
gerror(stderr,
"_FunImageExtract can't seek %d bytes into image data\n",
newpos - fun->curpos);
if( !buf && obuf ) xfree(obuf);
return(NULL);
}
/* read the row */
_gread(fun->gio, bufs[j], ABS(fun->dtype), (fun->x1-fun->x0+1), &got);
/* check for EOF */
if( got != (size_t)(fun->x1-fun->x0+1) ){
gerror(stderr, "unexpected EOF reading data\n");
if( !buf && obuf ) xfree(obuf);
return(NULL);
}
/* convert to native, if necessary */
if( convert )
swap_data(bufs[j], got, fun->dtype);
/* update the current position */
fun->curpos = newpos + (got * ABS(fun->dtype));
}
/* process all pixels in this output row */
for(ox=0; ox<fun->odim1; ox++){
/* reset temp value */
val = 0.0;
vall = 0;
/* for all rows in the input blocks */
for(i=0; i<fun->block; i++){
/* sum the input pixels which make up this output pixel */
for(j=ox*fun->block, k=0; k<fun->block; j++, k++){
switch(fun->dtype){
case TY_CHAR:
cval = ((unsigned char **)bufs)[i][j];
if( fun->doblank && (cval == fun->blank) )
goto blank;
if( doscale )
val += ((double)cval*fun->bscale)+fun->bzero;
else
val += (double)cval;
break;
case TY_USHORT:
usval = ((unsigned short **)bufs)[i][j];
if( fun->doblank && (usval == fun->blank) )
goto blank;
if( doscale )
val += ((double)usval*fun->bscale)+fun->bzero;
else
val += (double)usval;
break;
case TY_SHORT:
sval = ((short **)bufs)[i][j];
if( fun->doblank && (sval == fun->blank) )
goto blank;
if( doscale )
val += ((double)sval*fun->bscale)+fun->bzero;
else
val += (double)sval;
break;
case TY_INT:
ival = ((int **)bufs)[i][j];
if( fun->doblank && (ival == fun->blank) )
goto blank;
if( doscale )
val += ((double)ival*fun->bscale)+fun->bzero;
else
val += (double)ival;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
/* argh ... 64-bit does not fit into a double! */
lval = ((longlong **)bufs)[i][j];
if( fun->doblank && (lval == fun->blank) )
goto blank;
/* probably going to lose precision here */
if( doscale )
vall += ((double)lval*fun->bscale)+fun->bzero;
else
vall += lval;
break;
case TY_FLOAT:
fval = ((float **)bufs)[i][j];
if( isnanf(fval) )
goto blank;
val += (double)fval;
break;
case TY_DOUBLE:
dval = ((double **)bufs)[i][j];
if( isnand(dval) )
goto blank;
val += dval;
break;
default:
break;
}
}
} /* x block loop */
/* take average of this output pixel, if necessary */
if( fun->btype == FUN_AVG ){
val /= block2;
vall /= block2;
}
/* store the resulting value in the output image */
/* argh ... 64-bit does not fit into a double! */
if( fun->dtype == TY_LONG ){
switch(fun->odtype){
case TY_CHAR:
((unsigned char *)obuf)[yoff+ox] = (unsigned char)vall;
break;
case TY_USHORT:
((unsigned short *)obuf)[yoff+ox] = (unsigned short)vall;
break;
case TY_SHORT:
((short *)obuf)[yoff+ox] = (short)vall;
break;
case TY_INT:
((int *)obuf)[yoff+ox] = (int)vall;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
((longlong *)obuf)[yoff+ox] = (longlong)vall;
break;
case TY_FLOAT:
((float *)obuf)[yoff+ox] = (float)vall;
break;
case TY_DOUBLE:
((double *)obuf)[yoff+ox] = (double)vall;
break;
default:
break;
}
}
else{
switch(fun->odtype){
case TY_CHAR:
((unsigned char *)obuf)[yoff+ox] = (unsigned char)val;
break;
case TY_USHORT:
((unsigned short *)obuf)[yoff+ox] = (unsigned short)val;
break;
case TY_SHORT:
((short *)obuf)[yoff+ox] = (short)val;
break;
case TY_INT:
((int *)obuf)[yoff+ox] = (int)val;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
((longlong *)obuf)[yoff+ox] = (longlong)val;
break;
case TY_FLOAT:
((float *)obuf)[yoff+ox] = (float)val;
break;
case TY_DOUBLE:
((double *)obuf)[yoff+ox] = (double)val;
break;
default:
break;
}
}
/* skip the blank assignment */
continue;
blank:
/* arrive here is we found a blank or nan */
switch(fun->odtype){
case TY_CHAR:
((unsigned char *)obuf)[yoff+ox] = fun->blank;
break;
case TY_USHORT:
((unsigned short *)obuf)[yoff+ox] = fun->blank;
break;
case TY_SHORT:
((short *)obuf)[yoff+ox] = fun->blank;
break;
case TY_INT:
((int *)obuf)[yoff+ox] = fun->blank;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
((longlong *)obuf)[yoff+ox] = fun->blank;
break;
case TY_FLOAT:
((float *)obuf)[yoff+ox] = getnanf();
break;
case TY_DOUBLE:
((double *)obuf)[yoff+ox] = getnand();
break;
default:
break;
}
} /* x loop */
} /* y loop */
/* free allocated space */
for(i=0; i<fun->block; i++){
if( bufs[i] ) xfree(bufs[i]);
}
if( bufs ) xfree(bufs);
/* calculate the position to the end of the data, if we read last line */
if( rstop == MAX(fun->odim2,1) ){
newpos = ((MAX(fun->dim2,1)*fun->dim1)) * ABS(fun->dtype);
/* skip to end of data */
gskip(fun->gio, newpos - fun->curpos);
/* skip padding as well */
gskip(fun->gio, (off_t)fun->dpad);
/* reset current position to end of image */
fun->curpos = newpos + fun->dpad;
}
return (void *)obuf;
}
/*
*
* semi-public routines
*
*/
#ifdef ANSI_FUNC
void *
_FunImageMask(Fun fun, void *buf, int rstart, int rstop,
FilterMask masks, int nmask, char *plist)
#else
void *_FunImageMask(fun, buf, rstart, rstop, masks, nmask, plist)
Fun fun;
void *buf;
int rstart;
int rstop;
FilterMask masks;
int nmask;
char *plist;
#endif
{
int len;
int i, n;
int pixsize;
int rowsize;
int flag;
int bitpix;
int dtype;
int x=0, y=0;
char *pbuf;
char *obuf;
char *optr;
char tbuf[SZ_LINE];
/* make sure we have something to do */
if( !_FunValid(fun) )
return buf;
/* make sure we really want to mask, and figure out which sort of mask */
flag = 0;
pbuf = xstrdup(plist);
if( _FunKeyword(pbuf, "mask", "FUN_MASK", tbuf, SZ_LINE) ){
if( isfalse(tbuf) ){
if( pbuf ) xfree(pbuf);
return buf;
}
/* 'all' means we change each pixel, setting pixels to 0 outside,
and setting pixels to region value inside */