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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
|
/*
* 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 */
else if( !strcasecmp(tbuf, "all") ){
/* if we are allocating the buffer, we can change bitpix as well */
if( !buf && _FunKeyword(pbuf, "bitpix", NULL, tbuf, SZ_LINE) ){
if( (i = atoi(tbuf)) ){
fun->obitpix = i;
fun->odtype = fun->obitpix / FT_WORDLEN;
}
}
flag = 1;
}
}
if( pbuf ) xfree(pbuf);
bitpix = fun->obitpix;
dtype = fun->odtype;
/* make sure we are not out of bounds */
if( rstart < 1 ) rstart = 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(dtype));
if( !obuf ){
gerror(stderr, "can't allocate image buffer (%dx%dx%d)\n",
fun->odim1, (rstop-rstart+1), ABS(fun->odtype));
return NULL;
}
}
/* this is the size of a single pixel in bytes */
pixsize = ABS(bitpix/FT_WORDLEN);
/* this is the number of bytes per line */
rowsize = fun->odim1 * pixsize;
/* clear y rows before first row containing a mask segment */
if( nmask ){
for(y=rstart; y<=MIN(masks[0].y-1,rstop); y++){
optr = (char *)obuf + ((y-rstart) * rowsize);
memset(optr, 0, rowsize);
}
}
/* process all mask segments */
for(n=0; n<nmask; n++){
/* do not process mask values greater than rstop */
if( masks[n].y < rstart ) continue;
if( masks[n].y > rstop ) break;
/* blank out rows before the y row associated with this mask segment */
for(; y<masks[n].y; y++){
optr = (char *)obuf + ((y-rstart) * rowsize);
memset(optr, 0, rowsize);
}
/* process each mask segment having this same y value */
x = 1;
while(1){
/* clear columns before the start of this segment */
optr = (char *)obuf + ((y-rstart) * rowsize) + ((x-1) * pixsize);
if( (len = (masks[n].xstart - x) * pixsize) >0 )
memset(optr, 0, len);
x = masks[n].xstart;
/* if flag is set, we set the pixel values to be the region id */
if( flag ){
optr = (char *)obuf + ((y-rstart) * rowsize) + ((x-1) * pixsize);
for(x=masks[n].xstart; x<=masks[n].xstop; x++){
switch(dtype){
case TY_CHAR:
*((char *)optr) = (char)masks[n].region;
optr += pixsize;
break;
case TY_USHORT:
*((unsigned short *)optr) = (unsigned short)masks[n].region;
optr += pixsize;
break;
case TY_SHORT:
*((short *)optr) = (short)masks[n].region;
optr += pixsize;
break;
case TY_INT:
*((int *)optr) = (int)masks[n].region;
optr += pixsize;
break;
case TY_LONG:
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
*((longlong *)optr) = (longlong)masks[n].region;
optr += pixsize;
break;
case TY_FLOAT:
*((float *)optr) = (float)masks[n].region;
optr += pixsize;
break;
case TY_DOUBLE:
*((double *)optr) = (double)masks[n].region;
optr += pixsize;
break;
default:
break;
}
}
}
/* position after end of segment */
x = masks[n].xstop+1;
/* if we have another mask segment with the same y,
we keep going */
if( ((n+1) < nmask) && (masks[n+1].y == y) )
n++;
else
break;
}
/* clear the rest of the columns for this y row */
optr = (char *)obuf + ((y-rstart) * rowsize) + ((x-1) * pixsize);
if( (len = (fun->odim1 - x + 1) * pixsize) >0 )
memset(optr, 0, len);
/* bump to next y row */
y++;
}
/* clear y rows after last row containing a mask segment */
for(y = (nmask ? MAX(rstart,masks[nmask-1].y+1) : rstart); y<=rstop; y++){
optr = (char *)obuf + ((y-rstart) * rowsize);
memset(optr, 0, rowsize);
}
/* return result */
return obuf;
}
/*
*
* _FunImagePutHeader -- writes header to file
* used by FunImagePut and sometimes FunFlush (if no Put was done)
*
*/
#ifdef ANSI_FUNC
int
_FunImagePutHeader(Fun fun, int dim1, int dim2, int bitpix)
#else
int _FunImagePutHeader(fun, dim1, dim2, bitpix)
Fun fun;
int dim1, dim2, bitpix;
#endif
{
int i;
int extver=1;
int domerge=0;
char *extname="IMAGE";
FITSHead merge;
/* if no operation has been performed, we have no header */
if( !fun->ops ){
/* if we do not have header info yet, we should be able to copy it */
if( !fun->header ){
/* copy header from reference image */
if( fun->ifun )
_FunCopy2ImageHeader(fun->ifun, fun);
/* use passed dims and bitpix, if possible */
if( dim1 && bitpix ){
/* note that we may not have a header yet -- make one here */
if( !fun->header ){
fun->header = ft_headinit(NULL, 0);
ft_headsetl(fun->header, "SIMPLE", 0, 1, "FITS STANDARD", 1);
}
/* add the passed dim and bitpix values */
ft_headseti(fun->header, "BITPIX", 0, bitpix, "Bits/pixel", 1);
if( dim2 ){
ft_headseti(fun->header, "NAXIS", 0, 2, "2-D image", 1);
ft_headseti(fun->header, "NAXIS", 1, dim1, "Axis 1 dimension", 1);
ft_headseti(fun->header, "NAXIS", 2, dim2, "Axis 2 dimension", 1);
}
else{
ft_headseti(fun->header, "NAXIS", 0, 1, "1-D image", 1);
ft_headseti(fun->header, "NAXIS", 1, dim1, "Axis 1 dimension", 1);
}
ft_headsetl(fun->header, "EXTEND", 0, 1,
"Standard extensions might follow", 1);
/* synchronize the header and the cards after any changes */
ft_syncdata(fun->header);
/* fill in values */
if( dim2 ){
fun->dims = 2;
fun->dim1 = dim1;
fun->dim2 = dim2;
}
else{
fun->dims = 1;
fun->dim1 = dim1;
fun->dim2 = 1;
}
fun->bitpix = bitpix;
_FunImageSize(fun);
}
/* hmmm ... could not make a header of any sort */
if( !fun->header )
return 0;
/* flag that we need to merge in user params before writing */
domerge = 1;
}
/* if something is already written, this is an image extension */
if( fun->primio || gtell(fun->gio)>0 ){
/* required after naxis */
ft_headseti(fun->header, "PCOUNT", 0, 0, "Random parameter count", 1);
ft_headseti(fun->header, "GCOUNT", 0, 1, "Group count", 1);
/* convert first card to a FITS image extension */
ft_cardfmt((FITSCard)fun->header->cards,
"XTENSION", 0, FT_STRING, "IMAGE", 0, "FITS Image Extension");
if( !ft_headfind(fun->header, "EXTNAME", 0, 0) )
ft_headsets(fun->header, "EXTNAME", 0, extname, "Ext. name", 1);
if( !ft_headfind(fun->header, "EXTVER", 0, 0) )
ft_headseti(fun->header, "EXTVER", 0, extver, "Ext. version", 1);
/* delete EXTEND keyword, which is for primary only */
if( ft_headfind(fun->header, "EXTEND", 0, 0) )
ft_headdel(fun->header, "EXTEND", 0);
}
else{
/* flag that we have written the primary header */
fun->primio++;
}
/* if the data are floats, we must delete BSCALE and BZERO */
if( fun->bitpix < 0 ){
if( ft_headfind(fun->header, "BSCALE", 0, 0) )
ft_headdel(fun->header, "BSCALE", 0);
if( ft_headfind(fun->header, "BZERO", 0, 0) )
ft_headdel(fun->header, "BZERO", 0);
}
/* merge in user-specified params with overwrite */
if( domerge && fun->theader ){
merge = ft_headmerge(fun->header, fun->theader, 1);
ft_headfree(fun->theader, 1);
fun->theader = NULL;
ft_headfree(fun->header, 1);
fun->header = merge;
ft_syncdata(fun->header);
}
/* for a 1D image, we have to delete 2D header params */
if( fun->dims == 1 ){
ft_headdel(fun->header, "NAXIS", 2);
/* remove 2D WCS header parameters */
for(i=0; i<=2; i++){
ft_headdel(fun->header, "CTYPE", i);
ft_headdel(fun->header, "CRVAL", i);
ft_headdel(fun->header, "CRPIX", i);
ft_headdel(fun->header, "CDELT", i);
ft_headdel(fun->header, "LTV", i);
ft_headdel(fun->header, "LTM1_", i);
ft_headdel(fun->header, "LTM2_", i);
ft_headdel(fun->header, "CD1_", i);
ft_headdel(fun->header, "CD2_", i);
ft_headdel(fun->header, "CD3_", i);
}
}
/* save file position for later updating */
fun->headpos = gtell(fun->gio);
/* write the header */
ft_headwrite(fun->gio, fun->header);
/* we just wrote the header */
fun->ops |= OP_WRHEAD;
}
/* good news */
return 1;
}
/*
*
* public routines
*
*/
#ifdef ANSI_FUNC
void *
FunImageGet (Fun fun, void *buf, char *plist)
#else
void *FunImageGet (fun, buf, plist)
Fun fun;
void *buf;
char *plist;
#endif
{
int domask;
int doimage;
int imagetable;
char *pbuf;
char *s;
void *obuf=NULL;
char paint[SZ_LINE];
char debug[SZ_LINE];
char tbuf[SZ_LINE];
/* sanity checks */
if( !_FunValid(fun) ){
gerror(stderr, "data does not contain a valid header\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");
/* sanity checks */
if( !fun->header ){
gerror(stderr, "data does not contain a valid header\n");
return NULL;
}
pbuf = xstrdup(plist);
domask = 1;
doimage = 1;
imagetable = 0;
if( _FunKeyword(pbuf, "mask", "FUN_MASK", tbuf, SZ_LINE) ){
if( isfalse(tbuf) )
domask = 0;
/* mask=all => no data, just make the mask */
else if( !strcasecmp(tbuf, "all") )
doimage = 0;
/* mask=image => we want to use image masking on a table */
else if( !strcasecmp(tbuf, "image") )
imagetable = 1;
}
if( _FunKeyword(pbuf, "paint", "FILTER_PAINT", tbuf, SZ_LINE) ){
strncpy(paint, tbuf, SZ_LINE);
}
else{
*paint = '\0';
}
if( _FunKeyword(pbuf, "debug", "FILTER_DEBUG", tbuf, SZ_LINE) ){
strncpy(debug, tbuf, SZ_LINE);
}
else{
*debug = '\0';
}
if( pbuf ) xfree(pbuf);
/* pick the appropriate routine for tables and/or images */
if( doimage ){
if( fun->header->image ){
obuf = _FunImageExtract(fun, buf, 1, MAX(fun->odim2,1), plist);
}
else if( fun->header->table ){
obuf = _FunTableBin(fun, buf, plist);
/* if mask=image, we remask using image binning, otherwise not */
if( !imagetable )
domask = 0;
}
/* don't bother masking if we have no image */
if( obuf == NULL )
domask = 0;
}
/* filter resulting image, if we have one */
if( domask ){
if( fun->filter && *fun->filter )
s = fun->filter;
else if( !doimage )
s = "field";
else
s = NULL;
if( s ){
/* open the filter, first time through */
if( !fun->filt ){
/* make up filter plist string */
strncpy(tbuf, "type=image", SZ_LINE-1);
/* add the binning key */
if( fun->bincols ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, fun->bincols, SZ_LINE-1);
}
/* add paint mode */
if( *paint ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, "paint=", SZ_LINE-1);
strncat(tbuf, paint, SZ_LINE-1);
}
/* add debug flag */
if( *debug ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, "debug=", SZ_LINE-1);
strncat(tbuf, debug, SZ_LINE-1);
}
fun->filt = FilterOpen(fun->header, s, tbuf);
if( fun->filt && (fun->filt != NOFILTER) ){
fun->nmask = FilterImage(fun->filt,
fun->x0, fun->x1, fun->y0, fun->y1, fun->block,
&(fun->masks), NULL);
}
}
/* perform the filter, if necessary */
if( fun->filt && (fun->filt != NOFILTER) ){
obuf = _FunImageMask(fun, obuf, 1, MAX(fun->odim2,1),
fun->masks, fun->nmask, plist);
}
/* close filters, free masks */
if( fun->filt && (fun->filt != NOFILTER) ){
FilterClose(fun->filt);
fun->filt = NULL;
}
if( fun->masks ){
xfree(fun->masks);
fun->masks = NULL;
}
fun->nmask = 0;
}
}
/* we just read an image */
fun->ops |= OP_RDIMAGE;
/* return result */
return obuf;
}
#ifdef ANSI_FUNC
void *
FunImageRowGet (Fun fun, void *buf, int rstart, int rstop, char *plist)
#else
void *FunImageRowGet (fun, buf, rstart, rstop, plist)
Fun fun;
void *buf;
int rstart;
int rstop;
char *plist;
#endif
{
int domask;
int doimage;
char *pbuf;
void *obuf=NULL;
char paint[SZ_LINE];
char debug[SZ_LINE];
char tbuf[SZ_LINE];
/* sanity checks */
if( !_FunValid(fun) ){
gerror(stderr, "data does not contain a valid header\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");
/* sanity checks */
if( !fun->header ){
gerror(stderr, "data does not contain a valid header\n");
return NULL;
}
pbuf = xstrdup(plist);
domask = 1;
doimage = 1;
if( _FunKeyword(pbuf, "mask", "FUN_MASK", tbuf, SZ_LINE) ){
if( isfalse(tbuf) )
domask = 0;
else if( !strcasecmp(tbuf, "all") )
doimage = 0;
}
if( _FunKeyword(pbuf, "paint", "FILTER_PAINT", tbuf, SZ_LINE) ){
strncpy(paint, tbuf, SZ_LINE);
}
else{
*paint = '\0';
}
if( _FunKeyword(pbuf, "debug", "FILTER_DEBUG", tbuf, SZ_LINE) ){
strncpy(debug, tbuf, SZ_LINE);
}
else{
*debug = '\0';
}
if( pbuf ) xfree(pbuf);
/* pick the appropriate routine for tables and/or images */
if( doimage ){
if( fun->header->image ){
obuf = _FunImageExtract(fun, buf, rstart, rstop, plist);
}
/* we do not get lines for tables (they are not sorted) */
else if( fun->header->table ){
obuf = NULL;
domask = 0;
}
else{
obuf = NULL;
}
if( obuf == NULL )
domask = 0;
}
/* filter resulting image, if we have one */
if( domask ){
if( fun->filter && *fun->filter ){
/* open the filter, first time through */
if( !fun->filt ){
/* make up filter plist string */
strncpy(tbuf, "type=image", SZ_LINE-1);
/* add the binning key */
if( fun->bincols ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, fun->bincols, SZ_LINE-1);
}
/* add paint mode */
if( *paint ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, "paint=", SZ_LINE-1);
strncat(tbuf, paint, SZ_LINE-1);
}
/* add debug flag */
if( *debug ){
strncat(tbuf, ",", SZ_LINE-1);
strncat(tbuf, "debug=", SZ_LINE-1);
strncat(tbuf, debug, SZ_LINE-1);
}
fun->filt = FilterOpen(fun->header, fun->filter, tbuf);
if( fun->filt && (fun->filt != NOFILTER) ){
fun->nmask = FilterImage(fun->filt,
fun->x0, fun->x1, fun->y0, fun->y1, fun->block,
&(fun->masks), NULL);
}
}
/* perform the filter, if necessary */
if( fun->filt && (fun->filt != NOFILTER) ){
obuf = _FunImageMask(fun, obuf, rstart, rstop,
fun->masks, fun->nmask, plist);
}
}
}
/* we just read an image row */
fun->ops |= OP_RDIMAGE;
/* return result */
return obuf;
}
#ifdef ANSI_FUNC
static int
_FunImagePut (Fun fun, void *buf, int rstart, int rstop,
int dim1, int dim2, int bitpix, char *plist)
#else
static int _FunImagePut (fun, buf, rstart, rstop, dim1, dim2, bitpix, plist)
Fun fun;
void *buf;
int rstart;
int rstop;
int dim1;
int dim2;
int bitpix;
char *plist;
#endif
{
char tbuf[SZ_LINE];
char *pbuf;
char *zbuf;
int convert;
int dlen;
int dpad;
int tdim;
off_t newpos;
/* sanity checks */
if( !_FunValid(fun) )
return 0;
/* this extension is an image */
fun->type = FUN_IMAGE;
/* put header */
if( !_FunImagePutHeader(fun, dim1, dim2, bitpix) )
return 0;
/* fill in the blanks, if necessary */
if( (dim1 == 0) && (dim2 == 0) && (bitpix == 0) ){
bitpix = fun->bitpix;
dim1 = fun->dim1;
dim2 = fun->dim2;
}
/* see if we override the normal data conversion */
pbuf = xstrdup(plist);
if( _FunKeyword(pbuf, "convert", NULL, tbuf, SZ_LINE) )
convert = istrue(tbuf);
else
convert = !is_bigendian();
if( pbuf ) xfree(pbuf);
/* determine if we are writing one line or the whole image */
if( !rstart )
tdim = dim1 * MAX(dim2,1);
else
tdim = dim1 * (rstop-rstart+1);
/* convert to IEEE, if necessary */
if( convert )
swap_data(buf, tdim, bitpix/FT_WORDLEN);
/* determine data size */
dlen = tdim * ABS(bitpix/FT_WORDLEN);
/* we need to skip to the right row */
if( rstart ){
newpos = ((rstart-1) * dim1) * ABS(fun->dtype);
if( gskip(fun->gio, newpos - fun->curpos) < 0 ){
gerror(stderr,
"FunImagePut can't seek %d bytes into image data\n",
newpos - fun->curpos);
return 0;
}
fun->curpos = newpos;
}
/* write the data */
gwrite(fun->gio, buf, sizeof(char), dlen);
/* update the current position */
fun->curpos += dlen;
/* and the current number of bytes read */
fun->bytes += dlen;
/* write the padding, if we just wrote the last row (or full image) */
if( !rstart || (rstop == MAX(dim2,1)) ){
if( (dpad = FT_BLOCK - (fun->bytes % FT_BLOCK)) != FT_BLOCK ){
zbuf = xcalloc(dpad, sizeof(char));
gwrite(fun->gio, zbuf, sizeof(char), dpad);
if( zbuf ) xfree(zbuf);
/* update current position */
fun->curpos += dpad;
fun->bytes += dpad;
}
}
/* we just wrote an image */
fun->ops |= OP_WRIMAGE;
/* success */
return 1;
}
#ifdef ANSI_FUNC
int
FunImagePut (Fun fun, void *buf, int dim1, int dim2, int bitpix, char *plist)
#else
int FunImagePut (fun, buf, dim1, dim2, bitpix, plist)
Fun fun;
void *buf;
int dim1;
int dim2;
int bitpix;
char *plist;
#endif
{
return _FunImagePut(fun, buf, 0, 0, dim1, dim2, bitpix, plist);
}
#ifdef ANSI_FUNC
int
FunImageRowPut (Fun fun, void *buf, int rstart, int rstop, int dim1, int dim2,
int bitpix, char *plist)
#else
int FunImageRowPut (fun, buf, rstart, rstop, dim1, dim2, bitpix, plist)
Fun fun;
void *buf;
int rstart;
int rstop;
int dim1;
int dim2;
int bitpix;
char *plist;
#endif
{
return _FunImagePut(fun, buf, rstart, rstop, dim1, dim2, bitpix, plist);
}
|