/usr/include/CGAL/ImageIO_impl.h is in libcgal-dev 4.11-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Copyright (c) 2005, 2006 ASCLEPIOS Project, INRIA Sophia-Antipolis (France)
// Copyright (c) 2007 Geometrica Project, INRIA Sophia-Antipolis (France)
// Copyright (c) 2008 GeometryFactory, Sophia-Antipolis (France)
// All rights reserved.
//
// The files in this directory are part of the ImageIO Library.
// You can redistribute them and/or modify them under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation;
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// These files are provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE.
//
// $URL$
// $Id$
//
#ifdef CGAL_HEADER_ONLY
#define CGAL_INLINE_FUNCTION inline
#else
#define CGAL_INLINE_FUNCTION
#endif
#ifdef _MSC_VER
// Suppress deprecated warning for fileno and strdup
# pragma warning(disable:4996)
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
/* formats actuellement lus
format | extension(s) | lecture | ecriture
INRIMAGE | .inr[.gz] | X | X -> + .gradient[.gz] + .gradient_direction[.gz]
GIS | .dim, .ima[.gz] | X | X
ANALYZE | .hdr, .img[.gz] | X | X
PNM | .ppm, .pgm | X | X
GIF | .gif | X |
BMP | .gif | X |
*/
#include <CGAL/ImageIO/inr.h>
#include <CGAL/ImageIO/gif.h>
#include <CGAL/ImageIO/gis.h>
#include <CGAL/ImageIO/pnm.h>
#include <CGAL/ImageIO/bmp.h>
#include <CGAL/ImageIO/iris.h>
#include <CGAL/ImageIO/analyze.h>
#ifdef MINC_FILES
#include <CGAL/ImageIO/mincio.h>
#endif
struct Remove_supported_file_format {
~Remove_supported_file_format()
{
removeSupportedFileFormat();
}
};
#ifdef CGAL_HEADER_ONLY
inline PTRIMAGE_FORMAT & get_static_firstFormat()
{
static PTRIMAGE_FORMAT firstFormat = NULL;
return firstFormat;
}
inline PTRIMAGE_FORMAT & get_static_inrimageFormat()
{
static PTRIMAGE_FORMAT inrimageFormat = NULL;
return inrimageFormat;
}
inline Remove_supported_file_format & get_static_rsff()
{
static Remove_supported_file_format rsff;
return rsff;
}
// Dummy call to get_static_rsff(), otherwise it would not get instanced
CGAL_UNUSED static Remove_supported_file_format &rsff_dummy_ref = get_static_rsff();
#else // not header-only
/** the first file format is initialized to null */
static PTRIMAGE_FORMAT firstFormat = NULL;
inline PTRIMAGE_FORMAT & get_static_firstFormat()
{
return firstFormat;
}
/** the Inrimage file format (default format) is initialized to null */
static PTRIMAGE_FORMAT InrimageFormat = NULL;
inline PTRIMAGE_FORMAT & get_static_inrimageFormat()
{
return InrimageFormat;
}
static Remove_supported_file_format rsff;
inline Remove_supported_file_format & get_static_rsff()
{
return rsff;
}
#endif
/*--------------------------------------------------
*
* mimics standard routines
*
--------------------------------------------------*/
extern "C" {
/* default allocation routine */
static void *(*allocRoutine)(size_t) = 0;
/* default deallocation routine */
static void (*deleteRoutine)(void *) = 0;
}
CGAL_INLINE_FUNCTION
void *ImageIO_alloc(size_t s) {
if(!allocRoutine) allocRoutine = malloc;
return ( (*allocRoutine)(s) );
}
/* call allocation routine */
CGAL_INLINE_FUNCTION
void ImageIO_free(void *m) {
if(!deleteRoutine) deleteRoutine = free;
(*deleteRoutine)(m);
}
/* call deallocation routine */
CGAL_INLINE_FUNCTION
unsigned int ImageIO_limit_len(size_t to_be_read)
{
return (unsigned int)(std::min)(to_be_read, size_t(1u<<30));
}
/* mimics fwrite() function.
According to _openWriteImage(), openMode will has one
of the following value:
- OM_STD (for stdout)
- OM_GZ
- OM_FILE
*/
CGAL_INLINE_FUNCTION
size_t ImageIO_write(const _image *im, const void *buf, size_t len) {
size_t to_be_written = len;
int l = -1;
char *b = (char*)buf;
switch(im->openMode) {
default :
case OM_CLOSE :
return 0;
case OM_STD :
#ifdef CGAL_USE_ZLIB
while ( (to_be_written > 0) && ((l = gzwrite(im->fd, (void *) b, ImageIO_limit_len(to_be_written))) > 0) ) {
to_be_written -= l;
b += l;
}
#else
while ( (to_be_written > 0) && ((l = fwrite( b, 1, ImageIO_limit_len(to_be_written), im->fd )) > 0) ) {
to_be_written -= l;
b += l;
}
#endif
return ( len - to_be_written );
#ifdef CGAL_USE_ZLIB
case OM_GZ :
while ( (to_be_written > 0) && ((l = gzwrite(im->fd, (void *) b, ImageIO_limit_len(to_be_written))) > 0) ) {
to_be_written -= l;
b += l;
}
if(l<0)
{
int errnum;
fprintf(stderr, "zlib error: %s\n", gzerror(im->fd, &errnum));
}
return ( len - to_be_written );
#else
case OM_FILE :
while ( (to_be_written > 0) && ((l = fwrite( b, 1, ImageIO_limit_len(to_be_written), im->fd )) > 0) ) {
to_be_written -= l;
b += l;
}
return ( len - to_be_written );
#endif
}
}
CGAL_INLINE_FUNCTION
size_t ImageIO_limit_read(size_t to_be_read)
{
return (std::min)(to_be_read, size_t(1u<<30));
}
/* mimics fread() function.
According to _openReadImage(), openMode will has one
of the following value:
- OM_STD (for stdin)
- OM_GZ *or* OM_FILE
*/
CGAL_INLINE_FUNCTION
size_t ImageIO_read(const _image *im, void *buf, size_t len)
{
size_t to_be_read = len;
int l = -1;
char *b = (char*)buf;
switch(im->openMode) {
default :
case OM_CLOSE :
return 0;
case OM_STD :
#ifdef CGAL_USE_ZLIB
while ( (to_be_read > 0) && ((l = gzread(im->fd, (void *) b, ImageIO_limit_len(to_be_read))) > 0) ) {
to_be_read -= l;
b += l;
}
#else
while ( (to_be_read > 0) && ((l = fread( b, 1, ImageIO_limit_len(to_be_read), im->fd )) > 0) ) {
to_be_read -= l;
b += l;
}
#endif
return ( len - to_be_read );
#ifdef CGAL_USE_ZLIB
case OM_GZ :
while ( (to_be_read > 0) && ((l = gzread(im->fd, (void *) b, ImageIO_limit_len(to_be_read))) > 0) ) {
to_be_read -= l;
b += l;
}
if(l<0)
{
int errnum;
fprintf(stderr, "zlib error: %s\n", gzerror(im->fd, &errnum));
}
return ( len - to_be_read );
#else
case OM_FILE :
while ( (to_be_read > 0) && ((l = fread( b, 1, ImageIO_limit_len(to_be_read), im->fd )) > 0) ) {
to_be_read -= l;
b += l;
}
return ( len - to_be_read );
#endif
}
//return 0;
}
/* mimics fgets() function.
According to _openReadImage(), openMode will has one
of the following value:
- OM_STD (for stdout)
- OM_GZ *or* OM_FILE
*/
CGAL_INLINE_FUNCTION
char *ImageIO_gets( const _image *im, char *str, int size )
{
char *ret = NULL;
switch(im->openMode) {
default :
case OM_CLOSE :
return NULL;
case OM_STD :
#ifdef CGAL_USE_ZLIB
ret = (char *) gzgets(im->fd, str, size );
#else
ret = fgets(str, size, im->fd);
#endif
break;
#ifdef CGAL_USE_ZLIB
case OM_GZ :
ret = (char *) gzgets(im->fd, str, size);
break;
#else
case OM_FILE :
ret = fgets(str, size, im->fd);
break;
#endif
}
return ret;
}
CGAL_INLINE_FUNCTION
long ImageIO_seek( const _image *im, long offset, int whence ) {
switch(im->openMode) {
case OM_CLOSE :
default :
return -1;
#ifdef CGAL_USE_ZLIB
case OM_GZ:
return gzseek(im->fd, offset, whence );
#endif
case OM_FILE:
return fseek( (FILE*)im->fd, offset, whence );
}
}
/* return non 0 in case of error
*/
CGAL_INLINE_FUNCTION
int ImageIO_error( const _image *im )
{
switch(im->openMode) {
case OM_CLOSE :
default :
return 0;
#ifdef CGAL_USE_ZLIB
case OM_GZ :
static int errnum;
(void)gzerror(im->fd, &errnum);
return( (errnum != Z_OK) || gzeof(im->fd) );
#endif
case OM_FILE :
return( ferror( (FILE*)im->fd ) || feof( (FILE*)im->fd ) );
}
//return 0;
}
/* Upon successful completion 0 is returned.
Closing the standard output with gzclose()
is necessary as it will flush the pending output.
*/
CGAL_INLINE_FUNCTION
int ImageIO_close( _image* im )
{
int ret=0;
switch ( im->openMode ) {
default :
case OM_CLOSE :
break;
#ifdef CGAL_USE_ZLIB
case OM_GZ :
case OM_STD :
ret = gzclose( im->fd );
break;
#else
case OM_STD :
break;
#endif
case OM_FILE :
ret = fclose( (FILE*)im->fd );
}
im->fd = NULL;
im->openMode = OM_CLOSE;
return ret;
}
/* given an initialized file descriptor and a file name,
open file from stdin (if name == NULL, or name == "-", or name == "<"),
or a standard/gzipped file otherwise (gzipped files are handled assuming
that it is compiled and linked with zlib).
openMode will have one of the following value:
- OM_STD (for stdin)
- OM_GZ *or* OM_FILE
*/
CGAL_INLINE_FUNCTION
void _openReadImage(_image* im, const char *name) {
if(im->openMode == OM_CLOSE) {
/* open from stdin */
if( name == NULL || name[0] == '\0'
|| (name[0] == '-' && name[1] == '\0')
|| (name[0] == '<' && name[1] == '\0') ) {
#ifdef CGAL_USE_ZLIB
im->fd = gzdopen(fileno(stdin), "rb");
#else
im->fd = fdopen(fileno(stdin), "rb");
#endif
im->openMode = OM_STD;
}
else {
#ifdef CGAL_USE_ZLIB
im->fd = gzopen(name, "rb");
if(im->fd) im->openMode = OM_GZ;
#else
im->fd = fopen(name, "rb");
if(im->fd) im->openMode = OM_FILE;
#endif
}
}
}
/* given an initialized file descriptor and a file name,
open file from stdout (if name == NULL, or name == "-", or name == ">"),
a gzipped pipe (if name got the extension ".gz")
or a standard file otherwise.
openMode will have one of the following value:
- OM_STD (for stdout)
- OM_GZ
- OM_FILE
*/
CGAL_INLINE_FUNCTION
void _openWriteImage(_image* im, const char *name)
{
im->openMode = OM_CLOSE;
if( name == NULL || name[0] == '\0'
|| (name[0] == '-' && name[1] == '\0')
|| (name[0] == '>' && name[1] == '\0') ) {
#ifdef CGAL_USE_ZLIB
#if (defined _LINUX_) || (defined _SOLARIS_) || (defined _SGI_)
im->fd = gzdopen(1, "wb");
#else
im->fd = gzdopen(fileno(stdout), "wb");
#endif
#else
im->fd = (_ImageIO_file) stdout;
#endif
im->openMode = OM_STD;
}
else{
#ifdef CGAL_USE_ZLIB
/* from gzopen() doc:
... The mode parameter is as in fopen ("rb" or "wb") but can
also include a compression level ("wb9") or a strategy: 'f' for
filtered data as in "wb6f", 'h' for Huffman only compression as
in "wb1h" ...
However, a small .gz header will be written ... thus gz(d)open can not
be used for rgular files.
*/
if( !strncmp(name+strlen(name)-3, ".gz", 3) )
{
#ifdef _MSC_VER
int ffd=_open(name,_O_RDWR | _O_CREAT| _O_TRUNC | _O_BINARY, _S_IREAD|_S_IWRITE);
im->fd = gzdopen( ffd, "wb" );
#else
im->fd = gzopen( name, "wb" );
#endif
im->openMode = OM_GZ;
}
else
#endif
{
im->fd = (_ImageIO_file) fopen(name, "wb");
im->openMode = OM_FILE;
}
}
}
/* set allocation and deallocation routines */
CGAL_INLINE_FUNCTION
void setImageIOAllocationRoutines(ALLOCATION_FUNCTION alloc,
DEALLOCATION_FUNCTION del) {
if(alloc != NULL) allocRoutine = alloc;
if(del != NULL) deleteRoutine = del;
}
/*--------------------------------------------------
*
*
*
--------------------------------------------------*/
CGAL_INLINE_FUNCTION
ENDIANNESS _getEndianness()
{
union {
unsigned char uc[2];
unsigned short us;
} twobytes;
twobytes.us = 255;
/* on linux or dec
*/
if ( twobytes.uc[1] == 0 ) return( END_LITTLE );
/* on solaris or sgi
*/
return( END_BIG );
}
/* Allocates and initializes an image descriptor */
CGAL_INLINE_FUNCTION
_image *_initImage() {
_image *im;
im = (_image *) ImageIO_alloc(sizeof(_image));
if ( im == NULL ) return( im );
/* default image size is 1*1*1 */
im->xdim = im->ydim = im->zdim = im->vdim = 1;
/* default image voxel size is 1.0*1.0*1.0 */
im->vx = im->vy = im->vz = 1.0;
/* default image center is 0 0 0 */
im->cx = im->cy = im->cz = 0;
/* default image offset is 0 0 0 */
im->tx = im->ty = im->tz = 0.0;
/* default image rotation is 0 0 0 */
im->rx = im->ry = im->rz = 0.0;
/* no data yet */
im->data = NULL;
/* no file associated to image */
im->fd = NULL;
im->openMode = OM_CLOSE;
im->endianness = END_UNKNOWN;
/* unknown data kind
default is binary
*/
im->dataMode = DM_BINARY;
/* no user string */
im->user = NULL;
im->nuser = 0;
/* unknown word kind */
im->wdim = 0;
im->wordKind = WK_UNKNOWN;
im->vectMode = VM_SCALAR;
im->sign = SGN_UNKNOWN;
im->imageFormat = NULL;
/** eventually initializes the supported file formats */
if (get_static_firstFormat()==NULL)
initSupportedFileFormat();
/* return image descriptor */
return im;
}
CGAL_INLINE_FUNCTION
_image *_createImage(int x, int y, int z, int v,
float vx, float vy, float vz, int w,
WORD_KIND wk, SIGN sgn)
{
_image *im;
im = (_image *) ImageIO_alloc(sizeof(_image));
if ( im == NULL ) return( im );
im->xdim = x;
im->ydim = y;
im->zdim = z;
im->vdim = v;
im->vx = vx;
im->vy = vy;
im->vz = vz;
/* default image center is 0 0 0 */
im->cx = im->cy = im->cz = 0;
/* default image offset is 0 0 0 */
im->tx = im->ty = im->tz = 0.0;
/* default image rotation is 0 0 0 */
im->rx = im->ry = im->rz = 0.0;
/* no data yet */
im->data = ImageIO_alloc(std::size_t(x)*std::size_t(y)*std::size_t(z)*std::size_t(v)*std::size_t(w));
/* no file associated to image */
im->fd = NULL;
im->openMode = OM_CLOSE;
im->endianness = END_UNKNOWN;
/* unknown data kind
default is binary
*/
im->dataMode = DM_BINARY;
/* no user string */
im->user = NULL;
im->nuser = 0;
/* unknown word kind */
im->wdim = w;
im->wordKind = wk;
im->vectMode = VM_SCALAR;
im->sign = sgn;
im->imageFormat = NULL;
/** eventually initializes the supported file formats */
if (get_static_firstFormat()==NULL)
initSupportedFileFormat();
/* return image descriptor */
return im;
}
/* return the bounding box of the image */
CGAL_INLINE_FUNCTION
void _get_image_bounding_box(_image* im,
double* x_min, double* y_min, double* z_min,
double* x_max, double* y_max, double* z_max) {
*x_min = im->tx;
*y_min = im->ty;
*z_min = im->tz;
*x_max = (double(im->xdim) - 1.0)*(im->vx) + *x_min ;
*y_max = (double(im->ydim) - 1.0)*(im->vy) + *y_min ;
*z_max = (double(im->zdim) - 1.0)*(im->vz) + *z_min ;
}
/* Free an image descriptor */
CGAL_INLINE_FUNCTION
void _freeImage(_image *im) {
unsigned int i;
if ( im == NULL ) return;
/* close image if opened */
if(im->openMode != OM_CLOSE) ImageIO_close(im);
/* free data if any */
if(im->data != NULL) ImageIO_free(im->data);
im->data = NULL;
/* free user string array if any */
if( (im->nuser > 0) && (im->user != NULL) ) {
for(i = 0; i < im->nuser; i++)
if ( im->user[i] != NULL ) ImageIO_free(im->user[i]);
ImageIO_free(im->user);
}
im->nuser = 0;
im->user = NULL;
/* free given descriptor */
ImageIO_free(im);
}
/* Reads an image from a file and returns an image descriptor or NULL if
reading failed.
Reads from stdin if image name is NULL. */
CGAL_INLINE_FUNCTION
_image* _readImage(const char *name) {
_image *im;
/* read header */
im = _readImageHeader( name );
if(im != NULL && im->openMode != OM_CLOSE) {
/* read body */
if(_readImageData(im) < 0) {
fprintf(stderr, "_readImage: error: invalid data encountered in \'%s\'\n",
name);
_freeImage(im);
return NULL;
}
ImageIO_close(im);
}
return im;
}
// raw
CGAL_INLINE_FUNCTION
_image* _readImage_raw(const char *name,
const unsigned int rx,
const unsigned int ry,
const unsigned int rz,
const double vx,
const double vy,
const double vz,
const unsigned int offset,
const std::size_t wdim,
WORD_KIND wk,
SIGN sgned
)
{
_image *im = NULL;
im = (_image *) ImageIO_alloc(sizeof(_image));
if ( im == NULL )
return NULL;
im->xdim = rx;
im->ydim = ry;
im->zdim = rz;
im->vdim = 1;
im->vx = vx;
im->vy = vy;
im->vz = vz;
// image center
im->cx = im->cy = im->cz = 0;
// image offset
im->tx = im->ty = im->tz = 0.0;
// image rotation
im->rx = im->ry = im->rz = 0.0;
im->fd = NULL;
im->openMode = OM_CLOSE;
im->endianness = END_UNKNOWN;
im->dataMode = DM_BINARY;
// no user string
im->user = NULL;
im->nuser = 0;
// word type (unsigned byte)
im->wdim = wdim;
im->wordKind = wk;
im->vectMode = VM_SCALAR;
im->sign = sgned;
im->imageFormat = NULL;
// read file
::_openReadImage(im, name);
if(!im->fd) {
fprintf(stderr, "_readImage_raw: error: unable to open file \'%s\'\n", name);
_freeImage(im);
return NULL;
}
// read offset
if(offset > 0) {
im->data = ImageIO_alloc(offset+1);
ImageIO_read(im, im->data, offset);
ImageIO_free(im->data);
}
// allocate memory
im->data = ImageIO_alloc(rx*ry*rz*wdim);
if(im->data == NULL)
return NULL;
// read
ImageIO_read(im, im->data, rx*ry*rz*wdim);
ImageIO_close(im);
/*
unsigned int i,j,k;
unsigned char *data = (unsigned char *)im->data;
int dimxy = rx * ry;
for(i=0;i<rx;i++)
for(j=0;j<ry;j++)
for(k=0;k<rz;k++)
{
unsigned char voxel;
fread(&voxel,1,1,pFile);
data[k * dimxy + j * rx + i] = voxel;
}
*/
return im;
}
/* Reads an image from a file and returns an image descriptor or NULL if<br>
reading failed.<br>
Reads from stdin if image name is NULL.
If the image is vectorial, it is uninterlaced. */
CGAL_INLINE_FUNCTION
_image* _readNonInterlacedImage(const char *name) {
_image *im;
/* read header */
im = _readImageHeader(name);
if(im && im->openMode != OM_CLOSE) {
/* read scalar image body */
if(im->vdim == 1) {
if(_readImageData(im) < 0) {
fprintf(stderr, "_readImage: error: invalid data encountered in \'%s\'\n",
name);
_freeImage(im);
return NULL;
}
}
/* read vectorial image body */
else {
im->vectMode = VM_NON_INTERLACED;
if(_readNonInterlacedImageData(im) < 0) {
fprintf(stderr, "_readImage: error: invalid data encountered in \'%s\'\n",
name);
_freeImage(im);
return NULL;
}
}
ImageIO_close(im);
}
return im;
}
/* Write inrimage given in inr in file name. If file name's suffix is
.gz, the image is gziped. If file name's suffix is .hdr, the image
is written in ANALYZE format. If file name is NULL, image is written
on stdout */
CGAL_INLINE_FUNCTION
int _writeImage(_image *im, const char *name_to_be_written ) {
int r = ImageIO_NO_ERROR;
std::size_t length = 0;
char *name = NULL;
char *baseName = NULL;
if ( im == NULL ) return -1;
/* different conventions for the standard input
*/
if ( name_to_be_written == NULL || name_to_be_written[0] == '\0'
|| (name_to_be_written[0] == '-' && name_to_be_written[1] == '\0')
|| (name_to_be_written[0] == '>' && name_to_be_written[1] == '\0') ) {
name = NULL;
}
else {
name = strdup( name_to_be_written );
}
initSupportedFileFormat();
/* what is the wanted format
*/
if ( name == NULL ) {
im->imageFormat = get_static_inrimageFormat();
} else {
std::size_t i,extLength;
PTRIMAGE_FORMAT f;
char ext[IMAGE_FORMAT_NAME_LENGTH];
char *ptr;
/* scan all formats; */
im->imageFormat=NULL;
length=strlen(name);
for(f=get_static_firstFormat();(f!=NULL)&& (im->imageFormat==NULL);f=f->next) {
/* scan all extensions for that format */
ptr=&f->fileExtension[0];
do {
/* get next file extension */
i=0;
for(i=0;((*ptr)!=',' && (*ptr)!='\0');i++,ptr++) {
ext[i]=(*ptr);
}
if ((*ptr)==',') {
ext[i]='\0';
ptr++;
}
else {
ext[i]='\0';
}
extLength=strlen(ext);
/* test if the tail of name matches the extension */
if ( (length > extLength) && (!strcmp( name + length - extLength, ext)) ) {
im->imageFormat=f;
/* copy original name and removes extension */
baseName=strdup(name);
for(i= length - extLength;i<length;++i)
baseName[i]='\0';
}
} while (((*ptr)!='\0') && (im->imageFormat==NULL));
}
if (!im->imageFormat) {
fprintf(stderr, "_writeImage: warning : unknown extension in %s = assuming Inrimage\n",name);
im->imageFormat=get_static_inrimageFormat();
baseName=strdup(name);
}
}
/* open file descriptor */
/* _openWriteImage( im, name ) ;
if(!im->fd) {
fprintf(stderr, "_writeImage: error: open failed\n");
if ( name != NULL ) free( name );
if ( baseName != NULL ) free( baseName );
return ImageIO_OPENING;
}
*/
if (im->imageFormat) {
if (im->imageFormat->writeImage==NULL) {
im->imageFormat=get_static_inrimageFormat();
}
if ( 0 ) {
fprintf(stderr, "_writeImage: will write '%s' with '%s' format\n",
name, im->imageFormat->realName );
}
if ((*im->imageFormat->writeImage)(name, im)<0) {
fprintf(stderr, "_writeImage: error: unable to write \'%s\'\n",
name);
r = ImageIO_WRITING_HEADER;
}
}
/* close file descriptor */
ImageIO_close( im );
im->fd = NULL;
im->openMode = OM_CLOSE;
if ( baseName != NULL ) free( baseName );
if ( name != NULL ) free( name );
return r;
}
/* read header from an image file
if standard input, it's an inrimage
if not, get a magic string
and try to find the good format
if data are in a separate file,
the header reading procedure will open
the data file.
error:
0 success
-1 unknown image type
-2 error while opening
-3 error while reading header
-4 error while reading header or data
*/
CGAL_INLINE_FUNCTION
_image *_readImageHeader( const char *name ) {
int error = 0;
return( _readImageHeaderAndGetError( name, &error ) );
}
CGAL_INLINE_FUNCTION
_image *_readImageHeaderAndGetError( const char *name_to_be_read, int *error )
{
_image *im;
char magic[5];
char *name = NULL;
PTRIMAGE_FORMAT f;
int res;
*error = ImageIO_NO_ERROR;
/* open image file */
im = _initImage();
if ( name_to_be_read == NULL || name_to_be_read[0] == '\0'
|| (name_to_be_read[0] == '-' && name_to_be_read[1] == '\0')
|| (name_to_be_read[0] == '<' && name_to_be_read[1] == '\0') ) {
name = NULL;
}
else {
name = strdup( name_to_be_read );
}
_openReadImage(im, name);
if(!im->fd) {
fprintf(stderr, "_readImageHeaderAndGetError: error: unable to open file \'%s\'\n", name);
_freeImage(im);
*error = ImageIO_OPENING;
if ( name != NULL ) free( name );
return NULL;
}
initSupportedFileFormat();
/* what is the wanted format ?
assume that stdin is inrimage
*/
if(im->openMode == OM_STD) {
im->imageFormat=get_static_inrimageFormat();
}
else {
/* get magic string for disk files
*/
ImageIO_read(im, magic, 4);
magic[4] = '\0';
ImageIO_seek(im, 0L, SEEK_SET);
/** test each format */
for(f=get_static_firstFormat();(f!=NULL)&& (im->imageFormat==NULL);f=f->next) {
/* test if it is the correct format based on magic and file extension */
if (((*f->testImageFormat)(magic, name)) >=0) {
im->imageFormat=f;
}
}
}
if ( im->imageFormat == NULL ) {
fprintf(stderr, "_readImageHeaderAndGetError: does not find image format for \'%s\'\n", name);
ImageIO_close( im );
_freeImage(im);
*error = ImageIO_UNKNOWN_TYPE;
if ( name != NULL ) free( name );
return NULL;
}
/* now tests if the header can be read correctly */
res=(*(im->imageFormat)->readImageHeader)(name,im);
/* could read header only */
if (res == 0) {
if ( name != NULL ) free( name );
return( im );
}
/* could read header and data */
else if ( res > 0 ) {
ImageIO_close(im);
if ( name != NULL ) free( name );
return im;
}
/* could not read error : throw error */
fprintf(stderr, "_readImageHeaderAndGetError: an error occurs when reading image\n" );
if ( name == NULL || im->openMode == OM_STD) {
fprintf(stderr, "\t from \'standard input\'" );
}
else {
fprintf(stderr, "\t from file \'%s\'", name );
}
fprintf(stderr, " using format \'%s\'\n", (im->imageFormat)->realName );
ImageIO_close( im );
_freeImage(im);
*error = ImageIO_READING_HEADER;
if ( name != NULL ) free( name );
return NULL;
}
CGAL_INLINE_FUNCTION
static void _swapImageData( _image *im )
{
unsigned char *ptr1, *ptr2, b[8];
unsigned short int si, *ptr3, *ptr4;
unsigned int i, *ptr5, *ptr6;
std::size_t size, length;
if( _getEndianness() != im->endianness) {
size = std::size_t(im->xdim) * im->ydim * im->zdim * im->vdim * im->wdim;
if ( size <= 0 ) return;
length = size / im->wdim;
ptr1 = ptr2 = (unsigned char *) im->data;
/* 2 bytes swap */
if(im->wdim == 2) {
/*
while(length--) {
b[0] = *ptr1++;
b[1] = *ptr1++;
*ptr2++ = b[1];
*ptr2++ = b[0];
}
*/
ptr3 = ptr4 = (unsigned short int *) im->data;
while( length-- ) {
si = *ptr3++;
*ptr4++ = (unsigned short int)(((si >> 8) & 0xff) | (si << 8));
}
}
/* 4 bytes swap */
else if(im->wdim == 4) {
/*
while(length--) {
b[0] = *ptr1++;
b[1] = *ptr1++;
b[2] = *ptr1++;
b[3] = *ptr1++;
*ptr2++ = b[3];
*ptr2++ = b[2];
*ptr2++ = b[1];
*ptr2++ = b[0];
}
*/
ptr5 = ptr6 = (unsigned int *) im->data;
while( length-- ) {
i = *ptr5++;
*ptr6++ = (i << 24) | ((i & 0xff00) << 8) | ((i >> 8) & 0xff00) | ((i >> 24) & 0xff);
}
}
/* 8 bytes swap */
else if(im->wdim == 8) {
while(length--) {
b[0] = *ptr1++;
b[1] = *ptr1++;
b[2] = *ptr1++;
b[3] = *ptr1++;
b[4] = *ptr1++;
b[5] = *ptr1++;
b[6] = *ptr1++;
b[7] = *ptr1++;
*ptr2++ = b[7];
*ptr2++ = b[6];
*ptr2++ = b[5];
*ptr2++ = b[4];
*ptr2++ = b[3];
*ptr2++ = b[2];
*ptr2++ = b[1];
*ptr2++ = b[0];
}
}
}
}
/* Read data of an inrimage.
If im->data is not NULL, assume that the buffer was previously allocated
Swap bytes depending on the endianness and the current architecture */
CGAL_INLINE_FUNCTION
int _readImageData(_image *im) {
unsigned long size, nread;
if(im->openMode != OM_CLOSE) {
size = im->xdim * im->ydim * im->zdim * im->vdim * im->wdim;
if ( size <= 0 ) return -3;
if(!im->data) {
im->data = (unsigned char *) ImageIO_alloc(size);
if(!im->data) return -2;
}
nread = ImageIO_read(im, im->data, size);
if(nread != size) return -1;
/* architecture is big endian and data little endian
length = nb of points
*/
_swapImageData( im );
}
return 1;
}
/* Read data of a vectorial inrimage, making the resulting buffer non-
inerlaced.
If im->data is not NULL, assume that the buffer was previously allocated
Swap bytes depending on the endianness and the current architecture. */
CGAL_INLINE_FUNCTION
int _readNonInterlacedImageData(_image *im) {
unsigned long size, nread;
unsigned char **vp, *buf;
unsigned int i, j, k, v, w;
if(im->vdim == 1) return _readImageData(im);
if(im->openMode != OM_CLOSE) {
size = im->xdim * im->ydim * im->zdim * im->vdim * im->wdim;
if ( size <= 0 ) return -3;
if(!im->data) {
im->data = (unsigned char *) ImageIO_alloc(size);
if(!im->data) return -2;
}
vp = (unsigned char **) ImageIO_alloc(im->vdim * sizeof(unsigned char *));
buf = (unsigned char *) ImageIO_alloc(im->vdim * im->wdim);
size = im->xdim * im->ydim * im->zdim * im->wdim;
for(v = 0; v < im->vdim; v++)
vp[v] = (unsigned char *) im->data + v * size;
for(k = 0; k < im->zdim; k++) {
for(j = 0; j < im->ydim; j++) {
for(i = 0; i < im->xdim; i++) {
nread = ImageIO_read(im, buf, im->vdim * im->wdim);
if(nread != im->vdim * im->wdim) return -1;
for(v = 0; v < im->vdim; v++)
for(w = 0; w < im->wdim; w++)
*vp[v]++ = *buf++;
buf -= im->vdim * im->wdim;
}
}
}
ImageIO_free(buf);
ImageIO_free(vp);
/* architecture is big endian and data little endian */
_swapImageData( im );
/* reorder lines */
/* no non-interlaced data for ANALYZE. But if ever... */
/* if( im->imageFormat == IF_ANALYZE ) { */
/* int v ; */
/* int vdim = im->vdim ; */
/* int lineSize = im->wdim * im->xdim ; */
/* int vsize = lineSize * im->ydim * im->zdim ; */
/* char* swapped = ImageIO_alloc(lineSize) ; */
/* for( v = 0 ; v < vdim ; ++v ) */
/* { */
/* char* buf1 = (char*)im->data + v*vsize ; */
/* char* buf2 = buf1 + vsize - lineSize ; */
/* while( buf1 < buf2 ) */
/* { */
/* memcpy( swapped, buf1, lineSize ) ; */
/* memcpy( buf1, buf2, lineSize ) ; */
/* memcpy( buf2, swapped, lineSize ) ; */
/* buf1 += lineSize ; */
/* buf2 -= lineSize ; */
/* } */
/* ImageIO_free( swapped ) ; */
/* } */
/* } */
}
return 1;
}
/* Reads body from a non-interlaced vectorial inrimage whose header has
been read by _readImageHeader. The image buffer is interlaced. */
CGAL_INLINE_FUNCTION
int _readNonInterlacedFileData(_image *im) {
unsigned long size, nread;
unsigned char *ptr1, *vp, *buf;
unsigned int i, j, k, v, w;
if(im->vdim == 1) return _readImageData(im);
if(im->openMode != OM_CLOSE) {
size = im->xdim * im->ydim * im->zdim * im->vdim * im->wdim;
if ( size <= 0 ) return -3;
if(!im->data) {
im->data = (unsigned char *) ImageIO_alloc(size);
if(!im->data) return -2;
}
size = im->xdim * im->ydim * im->zdim * im->wdim;
buf = ptr1 = (unsigned char *) ImageIO_alloc(size);
for(v = 0; v < im->vdim; v++) {
buf = ptr1;
nread = ImageIO_read(im, buf, size);
if(nread != size) return -1;
vp = (unsigned char *) im->data + (v * im->wdim);
for(k = 0; k < im->zdim; k++) {
for(j = 0; j < im->ydim; j++) {
for(i = 0; i < im->xdim; i++) {
for(w = 0; w < im->wdim; w++) *vp++ = *buf++;
vp += (im->vdim - 1) * im->wdim;
}
}
}
}
ImageIO_free(buf);
/* architecture is big endian and data little endian */
_swapImageData( im );
/* reorder lines */
/* no non-interlaced data for ANALYZE. But if ever... */
/* if( im->imageFormat == IF_ANALYZE ) { */
/* int v ; */
/* int vdim = im->vdim ; */
/* int lineSize = im->wdim * im->xdim ; */
/* int vsize = lineSize * im->ydim * im->zdim ; */
/* char* swapped = ImageIO_alloc(lineSize) ; */
/* for( v = 0 ; v < vdim ; ++v ) */
/* { */
/* char* buf1 = (char*)im->data + v*vsize ; */
/* char* buf2 = buf1 + vsize - lineSize ; */
/* while( buf1 < buf2 ) */
/* { */
/* memcpy( swapped, buf1, lineSize ) ; */
/* memcpy( buf1, buf2, lineSize ) ; */
/* memcpy( buf2, swapped, lineSize ) ; */
/* buf1 += lineSize ; */
/* buf2 -= lineSize ; */
/* } */
/* ImageIO_free( swapped ) ; */
/* } */
/* } */
}
return 1;
}
/*--------------------------------------------------
*
* ?????
*
--------------------------------------------------*/
/* check the type of image in fileName */
CGAL_INLINE_FUNCTION
PTRIMAGE_FORMAT imageType(const char *fileName) {
_ImageIO_file f;
char magic[5];
PTRIMAGE_FORMAT format;
if(!fileName) {
#ifdef CGAL_USE_ZLIB
f = gzdopen(fileno(stdin), "rb");
#else
f = fdopen(fileno(stdin), "rb");
#endif
}
else {
#ifdef CGAL_USE_ZLIB
f = gzopen(fileName, "rb");
#else
f = fopen(fileName, "rb");
#endif
}
if(!f) return NULL;
#ifdef CGAL_USE_ZLIB
gzread( f, (void *) magic, 4);
#else
fread( (void *) magic, 1, 4, f );
#endif
magic[4] = '\0';
#ifdef CGAL_USE_ZLIB
gzclose( f );
#else
if(fileName) fclose( f );
#endif
if (get_static_firstFormat()==NULL)
initSupportedFileFormat();
for(format=get_static_firstFormat();(format!=NULL);format=format->next) {
/* test if it is the correct header based on magic and file extension */
if (((*format->testImageFormat)(magic,fileName)) >=0) {
return format;
}
}
return 0;
}
/*--------------------------------------------------
*
* Image Format Management
*
--------------------------------------------------*/
/** adds a format at the beginning of the list of image formats.
Test if all mandatory fields have been filled */
CGAL_INLINE_FUNCTION
int addImageFormat( PTRIMAGE_FORMAT format)
{
if ( (format->testImageFormat) &&
(format->readImageHeader) &&
(strlen(format->fileExtension)>0) &&
(strlen(format->realName)>0) ) {
format->next=get_static_firstFormat();
get_static_firstFormat()=format;
return 0;
}
else {
fprintf(stderr,"addImageFormat: information missing in file format %s\n",
format->realName);
return -1;
}
}
/** adds a format at the end of the list of image formats.
Test if all mandatory fields have been filled */
CGAL_INLINE_FUNCTION
int addImageFormatAtEnd( PTRIMAGE_FORMAT format)
{
PTRIMAGE_FORMAT f;
if ( (format->testImageFormat) &&
(format->readImageHeader) &&
(strlen(format->fileExtension)>0) &&
(strlen(format->realName)>0) ) {
format->next = NULL;
if (get_static_firstFormat() == NULL) {
get_static_firstFormat()=format;
}
else {
for(f=get_static_firstFormat();(f->next!=NULL);f=f->next)
;
f->next=format;
}
return 0;
}
else {
fprintf(stderr,"addImageFormatAtEnd: information missing in file format %s\n",
format->realName);
return -1;
}
}
/** creates supported image formats */
CGAL_INLINE_FUNCTION
void initSupportedFileFormat()
{
PTRIMAGE_FORMAT f;
if ( get_static_inrimageFormat() == NULL ) {
f = createAnalyzeFormat();
addImageFormatAtEnd( f );
f = createBMPFormat();
addImageFormatAtEnd( f );
f = createGifFormat();
addImageFormatAtEnd( f );
f = createGisFormat();
addImageFormatAtEnd( f );
f = createIrisFormat();
addImageFormatAtEnd( f );
f = createPgmFormat();
addImageFormatAtEnd( f );
f = createPgmAscIIFormat();
addImageFormatAtEnd( f );
f = createPpmFormat();
addImageFormatAtEnd( f );
get_static_inrimageFormat() = createInrimageFormat();
addImageFormat( get_static_inrimageFormat() );
}
}
CGAL_INLINE_FUNCTION
PTRIMAGE_FORMAT firstImageFormat() {
return get_static_firstFormat();
}
/** prints supported image formats */
CGAL_INLINE_FUNCTION
void printSupportedFileFormat() {
PTRIMAGE_FORMAT f;
int i;
initSupportedFileFormat();
for(i=0, f=get_static_firstFormat();(f!=NULL);i++, f=f->next) {
if ( (f->testImageFormat) &&
(f->readImageHeader) &&
(strlen(f->fileExtension)>0) &&
(strlen(f->realName)>0)) {
fprintf( stderr, "#%2d: format name ='%s', extensions='%s'",
i, f->realName, f->fileExtension );
if (f->readImageHeader)
fprintf( stderr, ", read" );
if (f->writeImage)
fprintf( stderr, ", write" );
fprintf( stderr, "\n" );
}
}
}
/** remove supported image formats */
CGAL_INLINE_FUNCTION
void removeSupportedFileFormat() {
PTRIMAGE_FORMAT f=get_static_firstFormat();
while( f != NULL) {
PTRIMAGE_FORMAT f_old = f;
f = f->next;
ImageIO_free( f_old);
}
get_static_inrimageFormat()=NULL;
}
/** trilinear interpolation in an _image float type
*/
CGAL_INLINE_FUNCTION
float triLinInterp(const _image* image,
float posx,
float posy,
float posz,
float value_outside /*= 0.f */)
{
const std::size_t dimx = image->xdim;
const std::size_t dimy = image->ydim;
const std::size_t dimz = image->zdim;
const std::size_t dimxy = dimx*dimy;
if(posx < 0.f || posy < 0.f || posz < 0.f )
return value_outside;
posx = static_cast<float>(posx /(image->vx));
posy = static_cast<float>(posy /(image->vy));
posz = static_cast<float>(posz /(image->vz));
//patch suggested by J.Cugnoni to prevent integer overflow
if(posz >= float(dimz-1) || posy >= float(dimy-1) || posx >= float(dimx-1))
return value_outside;
const int i1 = (int)(posz);
const int j1 = (int)(posy);
const int k1 = (int)(posx);
const int i2 = i1 + 1;
const int j2 = j1 + 1;
const int k2 = k1 + 1;
const float KI2 = float(i2)-posz;
const float KI1 = posz-float(i1);
const float KJ2 = float(j2)-posy;
const float KJ1 = posy-float(j1);
CGAL_IMAGE_IO_CASE
(image,
Word *array = (Word *) image->data;
return (((float)array[i1 * dimxy + j1 * dimx + k1] * KI2 +
(float)array[i2 * dimxy + j1 * dimx + k1] * KI1) * KJ2 +
((float)array[i1 * dimxy + j2 * dimx + k1] * KI2 +
(float)array[i2 * dimxy + j2 * dimx + k1] * KI1) * KJ1) * (float(k2)-posx)+
(((float)array[i1 * dimxy + j1 * dimx + k2] * KI2 +
(float)array[i2 * dimxy + j1 * dimx + k2] * KI1) * KJ2 +
((float)array[i1 * dimxy + j2 * dimx + k2] * KI2 +
(float)array[i2 * dimxy + j2 * dimx + k2] * KI1) * KJ1) * (posx-float(k1));
);
return 0.f;
}
// Gives the value of the image at pixel (i,j,k), converted in float.
CGAL_INLINE_FUNCTION
float evaluate(const _image* image,
const std::size_t i,
const std::size_t j,
const std::size_t k)
{
using CGAL::IMAGEIO::static_evaluate;
CGAL_IMAGE_IO_CASE(image, return (float)static_evaluate<Word>(image, i, j, k); );
return 0.f;
}
/** convert the data of the image to float
*/
CGAL_INLINE_FUNCTION
void convertImageTypeToFloat(_image* image){
if(image->wordKind == WK_FLOAT && image->wdim == 4)
return;
const std::size_t dimx = image->xdim;
const std::size_t dimy = image->ydim;
const std::size_t dimz = image->zdim;
float * array = (float*)ImageIO_alloc (dimx * dimy * dimz *sizeof(float));
if (array == NULL ) {
fprintf ( stderr, "allocation error\n" );
return;
}
CGAL_IMAGE_IO_CASE
(image,
Word * typedArray = (Word *)(image->data);
for(unsigned int i = 0; i<dimx * dimy * dimz;++i)
array[i] = (float)(typedArray[i]);
);
ImageIO_free ( image->data );
image->data = array;
image->wordKind = WK_FLOAT;
image->wdim = 4;
}
|