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 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
|
/* #########################################################################
These routines to apply the H-compress compression algorithm to a 2-D Fits
image were written by R. White at the STScI and were obtained from the STScI at
http://www.stsci.edu/software/hcompress.html
This source file is a concatination of the following sources files in the
original distribution
htrans.c
digitize.c
encode.c
qwrite.c
doencode.c
bit_output.c
qtree_encode.c
The following modifications have been made to the original code:
- commented out redundant "include" statements
- added the noutchar global variable
- changed all the 'extern' declarations to 'static', since all the routines are in
the same source file
- changed the first parameter in encode (and in lower level routines from a file stream
to a char array
- modifid the encode routine to return the size of the compressed array of bytes
- changed calls to printf and perror to call the CFITSIO ffpmsg routine
- modified the mywrite routine, and lower level byte writing routines, to copy
the output bytes to a char array, instead of writing them to a file stream
- replace "exit" statements with "return" statements
- changed the function declarations to the more modern ANSI C style
############################################################################ */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "fitsio2.h"
static long noutchar;
static long noutmax;
static int htrans(int a[],int nx,int ny);
static void digitize(int a[], int nx, int ny, int scale);
static int encode(char *outfile, long *nlen, int a[], int nx, int ny, int scale);
static void shuffle(int a[], int n, int n2, int tmp[]);
static int htrans64(LONGLONG a[],int nx,int ny);
static void digitize64(LONGLONG a[], int nx, int ny, int scale);
static int encode64(char *outfile, long *nlen, LONGLONG a[], int nx, int ny, int scale);
static void shuffle64(LONGLONG a[], int n, int n2, LONGLONG tmp[]);
static void writeint(char *outfile, int a);
static void writelonglong(char *outfile, LONGLONG a);
static int doencode(char *outfile, int a[], int nx, int ny, unsigned char nbitplanes[3]);
static int doencode64(char *outfile, LONGLONG a[], int nx, int ny, unsigned char nbitplanes[3]);
static int qwrite(char *file, char buffer[], int n);
static int qtree_encode(char *outfile, int a[], int n, int nqx, int nqy, int nbitplanes);
static int qtree_encode64(char *outfile, LONGLONG a[], int n, int nqx, int nqy, int nbitplanes);
static void start_outputing_bits(void);
static void done_outputing_bits(char *outfile);
static void output_nbits(char *outfile, int bits, int n);
static void qtree_onebit(int a[], int n, int nx, int ny, unsigned char b[], int bit);
static void qtree_onebit64(LONGLONG a[], int n, int nx, int ny, unsigned char b[], int bit);
static void qtree_reduce(unsigned char a[], int n, int nx, int ny, unsigned char b[]);
static int bufcopy(unsigned char a[], int n, unsigned char buffer[], int *b, int bmax);
static void write_bdirect(char *outfile, int a[], int n,int nqx, int nqy, unsigned char scratch[], int bit);
static void write_bdirect64(char *outfile, LONGLONG a[], int n,int nqx, int nqy, unsigned char scratch[], int bit);
/* #define output_nybble(outfile,c) output_nbits(outfile,c,4) */
static void output_nybble(char *outfile, int bits);
static void output_nnybble(char *outfile, int n, unsigned char array[]);
#define output_huffman(outfile,c) output_nbits(outfile,code[c],ncode[c])
/* ---------------------------------------------------------------------- */
int fits_hcompress(int *a, int ny, int nx, int scale, char *output,
long *nbytes, int *status)
{
/*
compress the input image using the H-compress algorithm
a - input image array
nx - size of X axis of image
ny - size of Y axis of image
scale - quantization scale factor. Larger values results in more (lossy) compression
scale = 0 does lossless compression
output - pre-allocated array to hold the output compressed stream of bytes
nbyts - input value = size of the output buffer;
returned value = size of the compressed byte stream, in bytes
NOTE: the nx and ny dimensions as defined within this code are reversed from
the usual FITS notation. ny is the fastest varying dimension, which is
usually considered the X axis in the FITS image display
*/
int stat;
if (*status > 0) return(*status);
/* H-transform */
stat = htrans(a, nx, ny);
if (stat) {
*status = stat;
return(*status);
}
/* digitize */
digitize(a, nx, ny, scale);
/* encode and write to output array */
FFLOCK;
noutmax = *nbytes; /* input value is the allocated size of the array */
*nbytes = 0; /* reset */
stat = encode(output, nbytes, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
return(*status);
}
/* ---------------------------------------------------------------------- */
int fits_hcompress64(LONGLONG *a, int ny, int nx, int scale, char *output,
long *nbytes, int *status)
{
/*
compress the input image using the H-compress algorithm
a - input image array
nx - size of X axis of image
ny - size of Y axis of image
scale - quantization scale factor. Larger values results in more (lossy) compression
scale = 0 does lossless compression
output - pre-allocated array to hold the output compressed stream of bytes
nbyts - size of the compressed byte stream, in bytes
NOTE: the nx and ny dimensions as defined within this code are reversed from
the usual FITS notation. ny is the fastest varying dimension, which is
usually considered the X axis in the FITS image display
*/
int stat;
if (*status > 0) return(*status);
/* H-transform */
stat = htrans64(a, nx, ny);
if (stat) {
*status = stat;
return(*status);
}
/* digitize */
digitize64(a, nx, ny, scale);
/* encode and write to output array */
FFLOCK;
noutmax = *nbytes; /* input value is the allocated size of the array */
*nbytes = 0; /* reset */
stat = encode64(output, nbytes, a, nx, ny, scale);
FFUNLOCK;
*status = stat;
return(*status);
}
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* htrans.c H-transform of NX x NY integer image
*
* Programmer: R. White Date: 11 May 1992
*/
/* ######################################################################### */
static int htrans(int a[],int nx,int ny)
{
int nmax, log2n, h0, hx, hy, hc, nxtop, nytop, i, j, k;
int oddx, oddy;
int shift, mask, mask2, prnd, prnd2, nrnd2;
int s10, s00;
int *tmp;
/*
* log2n is log2 of max(nx,ny) rounded up to next power of 2
*/
nmax = (nx>ny) ? nx : ny;
log2n = (int) (log((float) nmax)/log(2.0)+0.5);
if ( nmax > (1<<log2n) ) {
log2n += 1;
}
/*
* get temporary storage for shuffling elements
*/
tmp = (int *) malloc(((nmax+1)/2)*sizeof(int));
if(tmp == (int *) NULL) {
ffpmsg("htrans: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* set up rounding and shifting masks
*/
shift = 0;
mask = -2;
mask2 = mask << 1;
prnd = 1;
prnd2 = prnd << 1;
nrnd2 = prnd2 - 1;
/*
* do log2n reductions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = nx;
nytop = ny;
for (k = 0; k<log2n; k++) {
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = i*ny; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop-oddy; j += 2) {
/*
* Divide h0,hx,hy,hc by 2 (1 the first time through).
*/
h0 = (a[s10+1] + a[s10] + a[s00+1] + a[s00]) >> shift;
hx = (a[s10+1] + a[s10] - a[s00+1] - a[s00]) >> shift;
hy = (a[s10+1] - a[s10] + a[s00+1] - a[s00]) >> shift;
hc = (a[s10+1] - a[s10] - a[s00+1] + a[s00]) >> shift;
/*
* Throw away the 2 bottom bits of h0, bottom bit of hx,hy.
* To get rounding to be same for positive and negative
* numbers, nrnd2 = prnd2 - 1.
*/
a[s10+1] = hc;
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
s10 += 2;
}
if (oddy) {
/*
* do last element in row if row length is odd
* s00+1, s10+1 are off edge
*/
h0 = (a[s10] + a[s00]) << (1-shift);
hx = (a[s10] - a[s00]) << (1-shift);
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 1;
s10 += 1;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = i*ny;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = (a[s00+1] + a[s00]) << (1-shift);
hy = (a[s00+1] - a[s00]) << (1-shift);
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
}
if (oddy) {
/*
* do corner element if both row and column lengths are odd
* s00+1, s10, s10+1 are off edge
*/
h0 = a[s00] << (2-shift);
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
}
}
/*
* now shuffle in each dimension to group coefficients by order
*/
for (i = 0; i<nxtop; i++) {
shuffle(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
shuffle(&a[j],nxtop,ny,tmp);
}
/*
* image size reduced by 2 (round up if odd)
*/
nxtop = (nxtop+1)>>1;
nytop = (nytop+1)>>1;
/*
* divisor doubles after first reduction
*/
shift = 1;
/*
* masks, rounding values double after each iteration
*/
mask = mask2;
prnd = prnd2;
mask2 = mask2 << 1;
prnd2 = prnd2 << 1;
nrnd2 = prnd2 - 1;
}
free(tmp);
return(0);
}
/* ######################################################################### */
static int htrans64(LONGLONG a[],int nx,int ny)
{
int nmax, log2n, nxtop, nytop, i, j, k;
int oddx, oddy;
int shift;
int s10, s00;
LONGLONG h0, hx, hy, hc, prnd, prnd2, nrnd2, mask, mask2;
LONGLONG *tmp;
/*
* log2n is log2 of max(nx,ny) rounded up to next power of 2
*/
nmax = (nx>ny) ? nx : ny;
log2n = (int) (log((float) nmax)/log(2.0)+0.5);
if ( nmax > (1<<log2n) ) {
log2n += 1;
}
/*
* get temporary storage for shuffling elements
*/
tmp = (LONGLONG *) malloc(((nmax+1)/2)*sizeof(LONGLONG));
if(tmp == (LONGLONG *) NULL) {
ffpmsg("htrans64: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* set up rounding and shifting masks
*/
shift = 0;
mask = (LONGLONG) -2;
mask2 = mask << 1;
prnd = (LONGLONG) 1;
prnd2 = prnd << 1;
nrnd2 = prnd2 - 1;
/*
* do log2n reductions
*
* We're indexing a as a 2-D array with dimensions (nx,ny).
*/
nxtop = nx;
nytop = ny;
for (k = 0; k<log2n; k++) {
oddx = nxtop % 2;
oddy = nytop % 2;
for (i = 0; i<nxtop-oddx; i += 2) {
s00 = i*ny; /* s00 is index of a[i,j] */
s10 = s00+ny; /* s10 is index of a[i+1,j] */
for (j = 0; j<nytop-oddy; j += 2) {
/*
* Divide h0,hx,hy,hc by 2 (1 the first time through).
*/
h0 = (a[s10+1] + a[s10] + a[s00+1] + a[s00]) >> shift;
hx = (a[s10+1] + a[s10] - a[s00+1] - a[s00]) >> shift;
hy = (a[s10+1] - a[s10] + a[s00+1] - a[s00]) >> shift;
hc = (a[s10+1] - a[s10] - a[s00+1] + a[s00]) >> shift;
/*
* Throw away the 2 bottom bits of h0, bottom bit of hx,hy.
* To get rounding to be same for positive and negative
* numbers, nrnd2 = prnd2 - 1.
*/
a[s10+1] = hc;
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
s10 += 2;
}
if (oddy) {
/*
* do last element in row if row length is odd
* s00+1, s10+1 are off edge
*/
h0 = (a[s10] + a[s00]) << (1-shift);
hx = (a[s10] - a[s00]) << (1-shift);
a[s10 ] = ( (hx>=0) ? (hx+prnd) : hx ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 1;
s10 += 1;
}
}
if (oddx) {
/*
* do last row if column length is odd
* s10, s10+1 are off edge
*/
s00 = i*ny;
for (j = 0; j<nytop-oddy; j += 2) {
h0 = (a[s00+1] + a[s00]) << (1-shift);
hy = (a[s00+1] - a[s00]) << (1-shift);
a[s00+1] = ( (hy>=0) ? (hy+prnd) : hy ) & mask ;
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
s00 += 2;
}
if (oddy) {
/*
* do corner element if both row and column lengths are odd
* s00+1, s10, s10+1 are off edge
*/
h0 = a[s00] << (2-shift);
a[s00 ] = ( (h0>=0) ? (h0+prnd2) : (h0+nrnd2) ) & mask2;
}
}
/*
* now shuffle in each dimension to group coefficients by order
*/
for (i = 0; i<nxtop; i++) {
shuffle64(&a[ny*i],nytop,1,tmp);
}
for (j = 0; j<nytop; j++) {
shuffle64(&a[j],nxtop,ny,tmp);
}
/*
* image size reduced by 2 (round up if odd)
*/
nxtop = (nxtop+1)>>1;
nytop = (nytop+1)>>1;
/*
* divisor doubles after first reduction
*/
shift = 1;
/*
* masks, rounding values double after each iteration
*/
mask = mask2;
prnd = prnd2;
mask2 = mask2 << 1;
prnd2 = prnd2 << 1;
nrnd2 = prnd2 - 1;
}
free(tmp);
return(0);
}
/* ######################################################################### */
static void
shuffle(int a[], int n, int n2, int tmp[])
{
/*
int a[]; array to shuffle
int n; number of elements to shuffle
int n2; second dimension
int tmp[]; scratch storage
*/
int i;
int *p1, *p2, *pt;
/*
* copy odd elements to tmp
*/
pt = tmp;
p1 = &a[n2];
for (i=1; i < n; i += 2) {
*pt = *p1;
pt += 1;
p1 += (n2+n2);
}
/*
* compress even elements into first half of A
*/
p1 = &a[n2];
p2 = &a[n2+n2];
for (i=2; i<n; i += 2) {
*p1 = *p2;
p1 += n2;
p2 += (n2+n2);
}
/*
* put odd elements into 2nd half
*/
pt = tmp;
for (i = 1; i<n; i += 2) {
*p1 = *pt;
p1 += n2;
pt += 1;
}
}
/* ######################################################################### */
static void
shuffle64(LONGLONG a[], int n, int n2, LONGLONG tmp[])
{
/*
LONGLONG a[]; array to shuffle
int n; number of elements to shuffle
int n2; second dimension
LONGLONG tmp[]; scratch storage
*/
int i;
LONGLONG *p1, *p2, *pt;
/*
* copy odd elements to tmp
*/
pt = tmp;
p1 = &a[n2];
for (i=1; i < n; i += 2) {
*pt = *p1;
pt += 1;
p1 += (n2+n2);
}
/*
* compress even elements into first half of A
*/
p1 = &a[n2];
p2 = &a[n2+n2];
for (i=2; i<n; i += 2) {
*p1 = *p2;
p1 += n2;
p2 += (n2+n2);
}
/*
* put odd elements into 2nd half
*/
pt = tmp;
for (i = 1; i<n; i += 2) {
*p1 = *pt;
p1 += n2;
pt += 1;
}
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* digitize.c digitize H-transform
*
* Programmer: R. White Date: 11 March 1991
*/
/* ######################################################################### */
static void
digitize(int a[], int nx, int ny, int scale)
{
int d, *p;
/*
* round to multiple of scale
*/
if (scale <= 1) return;
d=(scale+1)/2-1;
for (p=a; p <= &a[nx*ny-1]; p++) *p = ((*p>0) ? (*p+d) : (*p-d))/scale;
}
/* ######################################################################### */
static void
digitize64(LONGLONG a[], int nx, int ny, int scale)
{
LONGLONG d, *p, scale64;
/*
* round to multiple of scale
*/
if (scale <= 1) return;
d=(scale+1)/2-1;
scale64 = scale; /* use a 64-bit int for efficiency in the big loop */
for (p=a; p <= &a[nx*ny-1]; p++) *p = ((*p>0) ? (*p+d) : (*p-d))/scale64;
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* encode.c encode H-transform and write to outfile
*
* Programmer: R. White Date: 2 February 1994
*/
static char code_magic[2] = { (char)0xDD, (char)0x99 };
/* ######################################################################### */
static int encode(char *outfile, long *nlength, int a[], int nx, int ny, int scale)
{
/* FILE *outfile; - change outfile to a char array */
/*
long * nlength returned length (in bytes) of the encoded array)
int a[]; input H-transform array (nx,ny)
int nx,ny; size of H-transform array
int scale; scale factor for digitization
*/
int nel, nx2, ny2, i, j, k, q, vmax[3], nsign, bits_to_go;
unsigned char nbitplanes[3];
unsigned char *signbits;
int stat;
noutchar = 0; /* initialize the number of compressed bytes that have been written */
nel = nx*ny;
/*
* write magic value
*/
qwrite(outfile, code_magic, sizeof(code_magic));
writeint(outfile, nx); /* size of image */
writeint(outfile, ny);
writeint(outfile, scale); /* scale factor for digitization */
/*
* write first value of A (sum of all pixels -- the only value
* which does not compress well)
*/
writelonglong(outfile, (LONGLONG) a[0]);
a[0] = 0;
/*
* allocate array for sign bits and save values, 8 per byte
*/
signbits = (unsigned char *) malloc((nel+7)/8);
if (signbits == (unsigned char *) NULL) {
ffpmsg("encode: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
nsign = 0;
bits_to_go = 8;
signbits[0] = 0;
for (i=0; i<nel; i++) {
if (a[i] > 0) {
/*
* positive element, put zero at end of buffer
*/
signbits[nsign] <<= 1;
bits_to_go -= 1;
} else if (a[i] < 0) {
/*
* negative element, shift in a one
*/
signbits[nsign] <<= 1;
signbits[nsign] |= 1;
bits_to_go -= 1;
/*
* replace a by absolute value
*/
a[i] = -a[i];
}
if (bits_to_go == 0) {
/*
* filled up this byte, go to the next one
*/
bits_to_go = 8;
nsign += 1;
signbits[nsign] = 0;
}
}
if (bits_to_go != 8) {
/*
* some bits in last element
* move bits in last byte to bottom and increment nsign
*/
signbits[nsign] <<= bits_to_go;
nsign += 1;
}
/*
* calculate number of bit planes for 3 quadrants
*
* quadrant 0=bottom left, 1=bottom right or top left, 2=top right,
*/
for (q=0; q<3; q++) {
vmax[q] = 0;
}
/*
* get maximum absolute value in each quadrant
*/
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
j=0; /* column counter */
k=0; /* row counter */
for (i=0; i<nel; i++) {
q = (j>=ny2) + (k>=nx2);
if (vmax[q] < a[i]) vmax[q] = a[i];
if (++j >= ny) {
j = 0;
k += 1;
}
}
/*
* now calculate number of bits for each quadrant
*/
/* this is a more efficient way to do this, */
for (q = 0; q < 3; q++) {
for (nbitplanes[q] = 0; vmax[q]>0; vmax[q] = vmax[q]>>1, nbitplanes[q]++) ;
}
/*
for (q = 0; q < 3; q++) {
nbitplanes[q] = (int) (log((float) (vmax[q]+1))/log(2.0)+0.5);
if ( (vmax[q]+1) > (1<<nbitplanes[q]) ) {
nbitplanes[q] += 1;
}
}
*/
/*
* write nbitplanes
*/
if (0 == qwrite(outfile, (char *) nbitplanes, sizeof(nbitplanes))) {
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
/*
* write coded array
*/
stat = doencode(outfile, a, nx, ny, nbitplanes);
/*
* write sign bits
*/
if (nsign > 0) {
if ( 0 == qwrite(outfile, (char *) signbits, nsign)) {
free(signbits);
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
}
free(signbits);
*nlength = noutchar;
if (noutchar >= noutmax) {
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
return(stat);
}
/* ######################################################################### */
static int encode64(char *outfile, long *nlength, LONGLONG a[], int nx, int ny, int scale)
{
/* FILE *outfile; - change outfile to a char array */
/*
long * nlength returned length (in bytes) of the encoded array)
LONGLONG a[]; input H-transform array (nx,ny)
int nx,ny; size of H-transform array
int scale; scale factor for digitization
*/
int nel, nx2, ny2, i, j, k, q, nsign, bits_to_go;
LONGLONG vmax[3];
unsigned char nbitplanes[3];
unsigned char *signbits;
int stat;
noutchar = 0; /* initialize the number of compressed bytes that have been written */
nel = nx*ny;
/*
* write magic value
*/
qwrite(outfile, code_magic, sizeof(code_magic));
writeint(outfile, nx); /* size of image */
writeint(outfile, ny);
writeint(outfile, scale); /* scale factor for digitization */
/*
* write first value of A (sum of all pixels -- the only value
* which does not compress well)
*/
writelonglong(outfile, a[0]);
a[0] = 0;
/*
* allocate array for sign bits and save values, 8 per byte
*/
signbits = (unsigned char *) malloc((nel+7)/8);
if (signbits == (unsigned char *) NULL) {
ffpmsg("encode64: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
nsign = 0;
bits_to_go = 8;
signbits[0] = 0;
for (i=0; i<nel; i++) {
if (a[i] > 0) {
/*
* positive element, put zero at end of buffer
*/
signbits[nsign] <<= 1;
bits_to_go -= 1;
} else if (a[i] < 0) {
/*
* negative element, shift in a one
*/
signbits[nsign] <<= 1;
signbits[nsign] |= 1;
bits_to_go -= 1;
/*
* replace a by absolute value
*/
a[i] = -a[i];
}
if (bits_to_go == 0) {
/*
* filled up this byte, go to the next one
*/
bits_to_go = 8;
nsign += 1;
signbits[nsign] = 0;
}
}
if (bits_to_go != 8) {
/*
* some bits in last element
* move bits in last byte to bottom and increment nsign
*/
signbits[nsign] <<= bits_to_go;
nsign += 1;
}
/*
* calculate number of bit planes for 3 quadrants
*
* quadrant 0=bottom left, 1=bottom right or top left, 2=top right,
*/
for (q=0; q<3; q++) {
vmax[q] = 0;
}
/*
* get maximum absolute value in each quadrant
*/
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
j=0; /* column counter */
k=0; /* row counter */
for (i=0; i<nel; i++) {
q = (j>=ny2) + (k>=nx2);
if (vmax[q] < a[i]) vmax[q] = a[i];
if (++j >= ny) {
j = 0;
k += 1;
}
}
/*
* now calculate number of bits for each quadrant
*/
/* this is a more efficient way to do this, */
for (q = 0; q < 3; q++) {
for (nbitplanes[q] = 0; vmax[q]>0; vmax[q] = vmax[q]>>1, nbitplanes[q]++) ;
}
/*
for (q = 0; q < 3; q++) {
nbitplanes[q] = log((float) (vmax[q]+1))/log(2.0)+0.5;
if ( (vmax[q]+1) > (((LONGLONG) 1)<<nbitplanes[q]) ) {
nbitplanes[q] += 1;
}
}
*/
/*
* write nbitplanes
*/
if (0 == qwrite(outfile, (char *) nbitplanes, sizeof(nbitplanes))) {
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
/*
* write coded array
*/
stat = doencode64(outfile, a, nx, ny, nbitplanes);
/*
* write sign bits
*/
if (nsign > 0) {
if ( 0 == qwrite(outfile, (char *) signbits, nsign)) {
free(signbits);
*nlength = noutchar;
ffpmsg("encode: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
}
free(signbits);
*nlength = noutchar;
if (noutchar >= noutmax) {
ffpmsg("encode64: output buffer too small");
return(DATA_COMPRESSION_ERR);
}
return(stat);
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* qwrite.c Write binary data
*
* Programmer: R. White Date: 11 March 1991
*/
/* ######################################################################### */
static void
writeint(char *outfile, int a)
{
int i;
unsigned char b[4];
/* Write integer A one byte at a time to outfile.
*
* This is portable from Vax to Sun since it eliminates the
* need for byte-swapping.
*/
for (i=3; i>=0; i--) {
b[i] = a & 0x000000ff;
a >>= 8;
}
for (i=0; i<4; i++) qwrite(outfile, (char *) &b[i],1);
}
/* ######################################################################### */
static void
writelonglong(char *outfile, LONGLONG a)
{
int i;
unsigned char b[8];
/* Write integer A one byte at a time to outfile.
*
* This is portable from Vax to Sun since it eliminates the
* need for byte-swapping.
*/
for (i=7; i>=0; i--) {
b[i] = (unsigned char) (a & 0x000000ff);
a >>= 8;
}
for (i=0; i<8; i++) qwrite(outfile, (char *) &b[i],1);
}
/* ######################################################################### */
static int
qwrite(char *file, char buffer[], int n){
/*
* write n bytes from buffer into file
* returns number of bytes read (=n) if successful, <=0 if not
*/
if (noutchar + n > noutmax) return(0); /* buffer overflow */
memcpy(&file[noutchar], buffer, n);
noutchar += n;
return(n);
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* doencode.c Encode 2-D array and write stream of characters on outfile
*
* This version assumes that A is positive.
*
* Programmer: R. White Date: 7 May 1991
*/
/* ######################################################################### */
static int
doencode(char *outfile, int a[], int nx, int ny, unsigned char nbitplanes[3])
{
/* char *outfile; output data stream
int a[]; Array of values to encode
int nx,ny; Array dimensions [nx][ny]
unsigned char nbitplanes[3]; Number of bit planes in quadrants
*/
int nx2, ny2, stat;
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
/*
* Initialize bit output
*/
start_outputing_bits();
/*
* write out the bit planes for each quadrant
*/
stat = qtree_encode(outfile, &a[0], ny, nx2, ny2, nbitplanes[0]);
if (!stat)
stat = qtree_encode(outfile, &a[ny2], ny, nx2, ny/2, nbitplanes[1]);
if (!stat)
stat = qtree_encode(outfile, &a[ny*nx2], ny, nx/2, ny2, nbitplanes[1]);
if (!stat)
stat = qtree_encode(outfile, &a[ny*nx2+ny2], ny, nx/2, ny/2, nbitplanes[2]);
/*
* Add zero as an EOF symbol
*/
output_nybble(outfile, 0);
done_outputing_bits(outfile);
return(stat);
}
/* ######################################################################### */
static int
doencode64(char *outfile, LONGLONG a[], int nx, int ny, unsigned char nbitplanes[3])
{
/* char *outfile; output data stream
LONGLONG a[]; Array of values to encode
int nx,ny; Array dimensions [nx][ny]
unsigned char nbitplanes[3]; Number of bit planes in quadrants
*/
int nx2, ny2, stat;
nx2 = (nx+1)/2;
ny2 = (ny+1)/2;
/*
* Initialize bit output
*/
start_outputing_bits();
/*
* write out the bit planes for each quadrant
*/
stat = qtree_encode64(outfile, &a[0], ny, nx2, ny2, nbitplanes[0]);
if (!stat)
stat = qtree_encode64(outfile, &a[ny2], ny, nx2, ny/2, nbitplanes[1]);
if (!stat)
stat = qtree_encode64(outfile, &a[ny*nx2], ny, nx/2, ny2, nbitplanes[1]);
if (!stat)
stat = qtree_encode64(outfile, &a[ny*nx2+ny2], ny, nx/2, ny/2, nbitplanes[2]);
/*
* Add zero as an EOF symbol
*/
output_nybble(outfile, 0);
done_outputing_bits(outfile);
return(stat);
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* BIT OUTPUT ROUTINES */
static LONGLONG bitcount;
/* THE BIT BUFFER */
static int buffer2; /* Bits buffered for output */
static int bits_to_go2; /* Number of bits free in buffer */
/* ######################################################################### */
/* INITIALIZE FOR BIT OUTPUT */
static void
start_outputing_bits(void)
{
buffer2 = 0; /* Buffer is empty to start */
bits_to_go2 = 8; /* with */
bitcount = 0;
}
/* ######################################################################### */
/* OUTPUT N BITS (N must be <= 8) */
static void
output_nbits(char *outfile, int bits, int n)
{
/* AND mask for the right-most n bits */
static int mask[9] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
/*
* insert bits at end of buffer
*/
buffer2 <<= n;
/* buffer2 |= ( bits & ((1<<n)-1) ); */
buffer2 |= ( bits & (*(mask+n)) );
bits_to_go2 -= n;
if (bits_to_go2 <= 0) {
/*
* buffer2 full, put out top 8 bits
*/
outfile[noutchar] = ((buffer2>>(-bits_to_go2)) & 0xff);
if (noutchar < noutmax) noutchar++;
bits_to_go2 += 8;
}
bitcount += n;
}
/* ######################################################################### */
/* OUTPUT a 4 bit nybble */
static void
output_nybble(char *outfile, int bits)
{
/*
* insert 4 bits at end of buffer
*/
buffer2 = (buffer2<<4) | ( bits & 15 );
bits_to_go2 -= 4;
if (bits_to_go2 <= 0) {
/*
* buffer2 full, put out top 8 bits
*/
outfile[noutchar] = ((buffer2>>(-bits_to_go2)) & 0xff);
if (noutchar < noutmax) noutchar++;
bits_to_go2 += 8;
}
bitcount += 4;
}
/* ############################################################################ */
/* OUTPUT array of 4 BITS */
static void output_nnybble(char *outfile, int n, unsigned char array[])
{
/* pack the 4 lower bits in each element of the array into the outfile array */
int ii, jj, kk = 0, shift;
if (n == 1) {
output_nybble(outfile, (int) array[0]);
return;
}
/* forcing byte alignment doesn;t help, and even makes it go slightly slower
if (bits_to_go2 != 8)
output_nbits(outfile, kk, bits_to_go2);
*/
if (bits_to_go2 <= 4)
{
/* just room for 1 nybble; write it out separately */
output_nybble(outfile, array[0]);
kk++; /* index to next array element */
if (n == 2) /* only 1 more nybble to write out */
{
output_nybble(outfile, (int) array[1]);
return;
}
}
/* bits_to_go2 is now in the range 5 - 8 */
shift = 8 - bits_to_go2;
/* now write out pairs of nybbles; this does not affect value of bits_to_go2 */
jj = (n - kk) / 2;
if (bits_to_go2 == 8) {
/* special case if nybbles are aligned on byte boundary */
/* this actually seems to make very little differnece in speed */
buffer2 = 0;
for (ii = 0; ii < jj; ii++)
{
outfile[noutchar] = ((array[kk] & 15)<<4) | (array[kk+1] & 15);
kk += 2;
noutchar++;
}
} else {
for (ii = 0; ii < jj; ii++)
{
buffer2 = (buffer2<<8) | ((array[kk] & 15)<<4) | (array[kk+1] & 15);
kk += 2;
/*
buffer2 full, put out top 8 bits
*/
outfile[noutchar] = ((buffer2>>shift) & 0xff);
noutchar++;
}
}
bitcount += (8 * (ii - 1));
/* write out last odd nybble, if present */
if (kk != n) output_nybble(outfile, (int) array[n - 1]);
return;
}
/* ######################################################################### */
/* FLUSH OUT THE LAST BITS */
static void
done_outputing_bits(char *outfile)
{
if(bits_to_go2 < 8) {
/* putc(buffer2<<bits_to_go2,outfile); */
outfile[noutchar] = (buffer2<<bits_to_go2);
if (noutchar < noutmax) noutchar++;
/* count the garbage bits too */
bitcount += bits_to_go2;
}
}
/* ######################################################################### */
/* ######################################################################### */
/* Copyright (c) 1993 Association of Universities for Research
* in Astronomy. All rights reserved. Produced under National
* Aeronautics and Space Administration Contract No. NAS5-26555.
*/
/* qtree_encode.c Encode values in quadrant of 2-D array using binary
* quadtree coding for each bit plane. Assumes array is
* positive.
*
* Programmer: R. White Date: 15 May 1991
*/
/*
* Huffman code values and number of bits in each code
*/
static int code[16] =
{
0x3e, 0x00, 0x01, 0x08, 0x02, 0x09, 0x1a, 0x1b,
0x03, 0x1c, 0x0a, 0x1d, 0x0b, 0x1e, 0x3f, 0x0c
};
static int ncode[16] =
{
6, 3, 3, 4, 3, 4, 5, 5,
3, 5, 4, 5, 4, 5, 6, 4
};
/*
* variables for bit output to buffer when Huffman coding
*/
static int bitbuffer, bits_to_go3;
/*
* macros to write out 4-bit nybble, Huffman code for this value
*/
/* ######################################################################### */
static int
qtree_encode(char *outfile, int a[], int n, int nqx, int nqy, int nbitplanes)
{
/*
int a[];
int n; physical dimension of row in a
int nqx; length of row
int nqy; length of column (<=n)
int nbitplanes; number of bit planes to output
*/
int log2n, i, k, bit, b, bmax, nqmax, nqx2, nqy2, nx, ny;
unsigned char *scratch, *buffer;
/*
* log2n is log2 of max(nqx,nqy) rounded up to next power of 2
*/
nqmax = (nqx>nqy) ? nqx : nqy;
log2n = (int) (log((float) nqmax)/log(2.0)+0.5);
if (nqmax > (1<<log2n)) {
log2n += 1;
}
/*
* initialize buffer point, max buffer size
*/
nqx2 = (nqx+1)/2;
nqy2 = (nqy+1)/2;
bmax = (nqx2*nqy2+1)/2;
/*
* We're indexing A as a 2-D array with dimensions (nqx,nqy).
* Scratch is 2-D with dimensions (nqx/2,nqy/2) rounded up.
* Buffer is used to store string of codes for output.
*/
scratch = (unsigned char *) malloc(2*bmax);
buffer = (unsigned char *) malloc(bmax);
if ((scratch == (unsigned char *) NULL) ||
(buffer == (unsigned char *) NULL)) {
ffpmsg("qtree_encode: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* now encode each bit plane, starting with the top
*/
for (bit=nbitplanes-1; bit >= 0; bit--) {
/*
* initial bit buffer
*/
b = 0;
bitbuffer = 0;
bits_to_go3 = 0;
/*
* on first pass copy A to scratch array
*/
qtree_onebit(a,n,nqx,nqy,scratch,bit);
nx = (nqx+1)>>1;
ny = (nqy+1)>>1;
/*
* copy non-zero values to output buffer, which will be written
* in reverse order
*/
if (bufcopy(scratch,nx*ny,buffer,&b,bmax)) {
/*
* quadtree is expanding data,
* change warning code and just fill buffer with bit-map
*/
write_bdirect(outfile,a,n,nqx,nqy,scratch,bit);
goto bitplane_done;
}
/*
* do log2n reductions
*/
for (k = 1; k<log2n; k++) {
qtree_reduce(scratch,ny,nx,ny,scratch);
nx = (nx+1)>>1;
ny = (ny+1)>>1;
if (bufcopy(scratch,nx*ny,buffer,&b,bmax)) {
write_bdirect(outfile,a,n,nqx,nqy,scratch,bit);
goto bitplane_done;
}
}
/*
* OK, we've got the code in buffer
* Write quadtree warning code, then write buffer in reverse order
*/
output_nybble(outfile,0xF);
if (b==0) {
if (bits_to_go3>0) {
/*
* put out the last few bits
*/
output_nbits(outfile, bitbuffer & ((1<<bits_to_go3)-1),
bits_to_go3);
} else {
/*
* have to write a zero nybble if there are no 1's in array
*/
output_huffman(outfile,0);
}
} else {
if (bits_to_go3>0) {
/*
* put out the last few bits
*/
output_nbits(outfile, bitbuffer & ((1<<bits_to_go3)-1),
bits_to_go3);
}
for (i=b-1; i>=0; i--) {
output_nbits(outfile,buffer[i],8);
}
}
bitplane_done: ;
}
free(buffer);
free(scratch);
return(0);
}
/* ######################################################################### */
static int
qtree_encode64(char *outfile, LONGLONG a[], int n, int nqx, int nqy, int nbitplanes)
{
/*
LONGLONG a[];
int n; physical dimension of row in a
int nqx; length of row
int nqy; length of column (<=n)
int nbitplanes; number of bit planes to output
*/
int log2n, i, k, bit, b, nqmax, nqx2, nqy2, nx, ny;
int bmax; /* this potentially needs to be made a 64-bit int to support large arrays */
unsigned char *scratch, *buffer;
/*
* log2n is log2 of max(nqx,nqy) rounded up to next power of 2
*/
nqmax = (nqx>nqy) ? nqx : nqy;
log2n = (int) (log((float) nqmax)/log(2.0)+0.5);
if (nqmax > (1<<log2n)) {
log2n += 1;
}
/*
* initialize buffer point, max buffer size
*/
nqx2 = (nqx+1)/2;
nqy2 = (nqy+1)/2;
bmax = (( nqx2)* ( nqy2)+1)/2;
/*
* We're indexing A as a 2-D array with dimensions (nqx,nqy).
* Scratch is 2-D with dimensions (nqx/2,nqy/2) rounded up.
* Buffer is used to store string of codes for output.
*/
scratch = (unsigned char *) malloc(2*bmax);
buffer = (unsigned char *) malloc(bmax);
if ((scratch == (unsigned char *) NULL) ||
(buffer == (unsigned char *) NULL)) {
ffpmsg("qtree_encode64: insufficient memory");
return(DATA_COMPRESSION_ERR);
}
/*
* now encode each bit plane, starting with the top
*/
for (bit=nbitplanes-1; bit >= 0; bit--) {
/*
* initial bit buffer
*/
b = 0;
bitbuffer = 0;
bits_to_go3 = 0;
/*
* on first pass copy A to scratch array
*/
qtree_onebit64(a,n,nqx,nqy,scratch,bit);
nx = (nqx+1)>>1;
ny = (nqy+1)>>1;
/*
* copy non-zero values to output buffer, which will be written
* in reverse order
*/
if (bufcopy(scratch,nx*ny,buffer,&b,bmax)) {
/*
* quadtree is expanding data,
* change warning code and just fill buffer with bit-map
*/
write_bdirect64(outfile,a,n,nqx,nqy,scratch,bit);
goto bitplane_done;
}
/*
* do log2n reductions
*/
for (k = 1; k<log2n; k++) {
qtree_reduce(scratch,ny,nx,ny,scratch);
nx = (nx+1)>>1;
ny = (ny+1)>>1;
if (bufcopy(scratch,nx*ny,buffer,&b,bmax)) {
write_bdirect64(outfile,a,n,nqx,nqy,scratch,bit);
goto bitplane_done;
}
}
/*
* OK, we've got the code in buffer
* Write quadtree warning code, then write buffer in reverse order
*/
output_nybble(outfile,0xF);
if (b==0) {
if (bits_to_go3>0) {
/*
* put out the last few bits
*/
output_nbits(outfile, bitbuffer & ((1<<bits_to_go3)-1),
bits_to_go3);
} else {
/*
* have to write a zero nybble if there are no 1's in array
*/
output_huffman(outfile,0);
}
} else {
if (bits_to_go3>0) {
/*
* put out the last few bits
*/
output_nbits(outfile, bitbuffer & ((1<<bits_to_go3)-1),
bits_to_go3);
}
for (i=b-1; i>=0; i--) {
output_nbits(outfile,buffer[i],8);
}
}
bitplane_done: ;
}
free(buffer);
free(scratch);
return(0);
}
/* ######################################################################### */
/*
* copy non-zero codes from array to buffer
*/
static int
bufcopy(unsigned char a[], int n, unsigned char buffer[], int *b, int bmax)
{
int i;
for (i = 0; i < n; i++) {
if (a[i] != 0) {
/*
* add Huffman code for a[i] to buffer
*/
bitbuffer |= code[a[i]] << bits_to_go3;
bits_to_go3 += ncode[a[i]];
if (bits_to_go3 >= 8) {
buffer[*b] = bitbuffer & 0xFF;
*b += 1;
/*
* return warning code if we fill buffer
*/
if (*b >= bmax) return(1);
bitbuffer >>= 8;
bits_to_go3 -= 8;
}
}
}
return(0);
}
/* ######################################################################### */
/*
* Do first quadtree reduction step on bit BIT of array A.
* Results put into B.
*
*/
static void
qtree_onebit(int a[], int n, int nx, int ny, unsigned char b[], int bit)
{
int i, j, k;
int b0, b1, b2, b3;
int s10, s00;
/*
* use selected bit to get amount to shift
*/
b0 = 1<<bit;
b1 = b0<<1;
b2 = b0<<2;
b3 = b0<<3;
k = 0; /* k is index of b[i/2,j/2] */
for (i = 0; i<nx-1; i += 2) {
s00 = n*i; /* s00 is index of a[i,j] */
/* tried using s00+n directly in the statements, but this had no effect on performance */
s10 = s00+n; /* s10 is index of a[i+1,j] */
for (j = 0; j<ny-1; j += 2) {
/*
this was not any faster..
b[k] = (a[s00] & b0) ?
(a[s00+1] & b0) ?
(a[s10] & b0) ?
(a[s10+1] & b0) ? 15 : 14
: (a[s10+1] & b0) ? 13 : 12
: (a[s10] & b0) ?
(a[s10+1] & b0) ? 11 : 10
: (a[s10+1] & b0) ? 9 : 8
: (a[s00+1] & b0) ?
(a[s10] & b0) ?
(a[s10+1] & b0) ? 7 : 6
: (a[s10+1] & b0) ? 5 : 4
: (a[s10] & b0) ?
(a[s10+1] & b0) ? 3 : 2
: (a[s10+1] & b0) ? 1 : 0;
*/
/*
this alternative way of calculating b[k] was slowwer than the original code
if ( a[s00] & b0)
if ( a[s00+1] & b0)
if ( a[s10] & b0)
if ( a[s10+1] & b0)
b[k] = 15;
else
b[k] = 14;
else
if ( a[s10+1] & b0)
b[k] = 13;
else
b[k] = 12;
else
if ( a[s10] & b0)
if ( a[s10+1] & b0)
b[k] = 11;
else
b[k] = 10;
else
if ( a[s10+1] & b0)
b[k] = 9;
else
b[k] = 8;
else
if ( a[s00+1] & b0)
if ( a[s10] & b0)
if ( a[s10+1] & b0)
b[k] = 7;
else
b[k] = 6;
else
if ( a[s10+1] & b0)
b[k] = 5;
else
b[k] = 4;
else
if ( a[s10] & b0)
if ( a[s10+1] & b0)
b[k] = 3;
else
b[k] = 2;
else
if ( a[s10+1] & b0)
b[k] = 1;
else
b[k] = 0;
*/
b[k] = ( ( a[s10+1] & b0)
| ((a[s10 ]<<1) & b1)
| ((a[s00+1]<<2) & b2)
| ((a[s00 ]<<3) & b3) ) >> bit;
k += 1;
s00 += 2;
s10 += 2;
}
if (j < ny) {
/*
* row size is odd, do last element in row
* s00+1,s10+1 are off edge
*/
b[k] = ( ((a[s10 ]<<1) & b1)
| ((a[s00 ]<<3) & b3) ) >> bit;
k += 1;
}
}
if (i < nx) {
/*
* column size is odd, do last row
* s10,s10+1 are off edge
*/
s00 = n*i;
for (j = 0; j<ny-1; j += 2) {
b[k] = ( ((a[s00+1]<<2) & b2)
| ((a[s00 ]<<3) & b3) ) >> bit;
k += 1;
s00 += 2;
}
if (j < ny) {
/*
* both row and column size are odd, do corner element
* s00+1, s10, s10+1 are off edge
*/
b[k] = ( ((a[s00 ]<<3) & b3) ) >> bit;
k += 1;
}
}
}
/* ######################################################################### */
/*
* Do first quadtree reduction step on bit BIT of array A.
* Results put into B.
*
*/
static void
qtree_onebit64(LONGLONG a[], int n, int nx, int ny, unsigned char b[], int bit)
{
int i, j, k;
LONGLONG b0, b1, b2, b3;
int s10, s00;
/*
* use selected bit to get amount to shift
*/
b0 = ((LONGLONG) 1)<<bit;
b1 = b0<<1;
b2 = b0<<2;
b3 = b0<<3;
k = 0; /* k is index of b[i/2,j/2] */
for (i = 0; i<nx-1; i += 2) {
s00 = n*i; /* s00 is index of a[i,j] */
s10 = s00+n; /* s10 is index of a[i+1,j] */
for (j = 0; j<ny-1; j += 2) {
b[k] = (unsigned char) (( ( a[s10+1] & b0)
| ((a[s10 ]<<1) & b1)
| ((a[s00+1]<<2) & b2)
| ((a[s00 ]<<3) & b3) ) >> bit);
k += 1;
s00 += 2;
s10 += 2;
}
if (j < ny) {
/*
* row size is odd, do last element in row
* s00+1,s10+1 are off edge
*/
b[k] = (unsigned char) (( ((a[s10 ]<<1) & b1)
| ((a[s00 ]<<3) & b3) ) >> bit);
k += 1;
}
}
if (i < nx) {
/*
* column size is odd, do last row
* s10,s10+1 are off edge
*/
s00 = n*i;
for (j = 0; j<ny-1; j += 2) {
b[k] = (unsigned char) (( ((a[s00+1]<<2) & b2)
| ((a[s00 ]<<3) & b3) ) >> bit);
k += 1;
s00 += 2;
}
if (j < ny) {
/*
* both row and column size are odd, do corner element
* s00+1, s10, s10+1 are off edge
*/
b[k] = (unsigned char) (( ((a[s00 ]<<3) & b3) ) >> bit);
k += 1;
}
}
}
/* ######################################################################### */
/*
* do one quadtree reduction step on array a
* results put into b (which may be the same as a)
*/
static void
qtree_reduce(unsigned char a[], int n, int nx, int ny, unsigned char b[])
{
int i, j, k;
int s10, s00;
k = 0; /* k is index of b[i/2,j/2] */
for (i = 0; i<nx-1; i += 2) {
s00 = n*i; /* s00 is index of a[i,j] */
s10 = s00+n; /* s10 is index of a[i+1,j] */
for (j = 0; j<ny-1; j += 2) {
b[k] = (a[s10+1] != 0)
| ( (a[s10 ] != 0) << 1)
| ( (a[s00+1] != 0) << 2)
| ( (a[s00 ] != 0) << 3);
k += 1;
s00 += 2;
s10 += 2;
}
if (j < ny) {
/*
* row size is odd, do last element in row
* s00+1,s10+1 are off edge
*/
b[k] = ( (a[s10 ] != 0) << 1)
| ( (a[s00 ] != 0) << 3);
k += 1;
}
}
if (i < nx) {
/*
* column size is odd, do last row
* s10,s10+1 are off edge
*/
s00 = n*i;
for (j = 0; j<ny-1; j += 2) {
b[k] = ( (a[s00+1] != 0) << 2)
| ( (a[s00 ] != 0) << 3);
k += 1;
s00 += 2;
}
if (j < ny) {
/*
* both row and column size are odd, do corner element
* s00+1, s10, s10+1 are off edge
*/
b[k] = ( (a[s00 ] != 0) << 3);
k += 1;
}
}
}
/* ######################################################################### */
static void
write_bdirect(char *outfile, int a[], int n,int nqx, int nqy, unsigned char scratch[], int bit)
{
/*
* Write the direct bitmap warning code
*/
output_nybble(outfile,0x0);
/*
* Copy A to scratch array (again!), packing 4 bits/nybble
*/
qtree_onebit(a,n,nqx,nqy,scratch,bit);
/*
* write to outfile
*/
/*
int i;
for (i = 0; i < ((nqx+1)/2) * ((nqy+1)/2); i++) {
output_nybble(outfile,scratch[i]);
}
*/
output_nnybble(outfile, ((nqx+1)/2) * ((nqy+1)/2), scratch);
}
/* ######################################################################### */
static void
write_bdirect64(char *outfile, LONGLONG a[], int n,int nqx, int nqy, unsigned char scratch[], int bit)
{
/*
* Write the direct bitmap warning code
*/
output_nybble(outfile,0x0);
/*
* Copy A to scratch array (again!), packing 4 bits/nybble
*/
qtree_onebit64(a,n,nqx,nqy,scratch,bit);
/*
* write to outfile
*/
/*
int i;
for (i = 0; i < ((nqx+1)/2) * ((nqy+1)/2); i++) {
output_nybble(outfile,scratch[i]);
}
*/
output_nnybble(outfile, ((nqx+1)/2) * ((nqy+1)/2), scratch);
}
|