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
|
/*
* vp_file.c
*
* Routines for loading and storing volume data in disk files.
*
* Copyright (c) 1994 The Board of Trustees of The Leland Stanford
* Junior University. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice and this permission notice appear in
* all copies of this software and that you do not sell the software.
* Commercial licensing is available by contacting the author.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* Author:
* Phil Lacroute
* Computer Systems Laboratory
* Electrical Engineering Dept.
* Stanford University
*/
/*
* $Date: 1994/12/30 23:52:38 $
* $Revision: 1.26 $
*/
#include "vp_global.h"
static int StoreRLEVoxels ANSI_ARGS((vpContext *vpc, int fd,
RLEVoxels *rle_voxels));
static int LoadRLEVoxels ANSI_ARGS((vpContext *vpc, int fd,
RLEVoxels *rle_voxels, int offsets, int swab));
static void SwapWords ANSI_ARGS((void *data, unsigned size));
static void SwapVoxels ANSI_ARGS((vpContext *vpc, void *voxels,
int num_voxels, int fields, int bytes_per_voxel));
#ifdef DEBUG
void VPCheckScanOffsets ANSI_ARGS((RLEVoxels *rle_voxels,
int rle_bytes_per_voxel));
#endif
static void SwapOctreeNode ANSI_ARGS((vpContext *vpc, int level, void *node));
static int StoreTable ANSI_ARGS((vpContext *vpc, int fd, float *ptr,
unsigned size));
static int LoadTable ANSI_ARGS((vpContext *vpc, int fd, float **ptr_ptr,
unsigned *size_ptr));
/*******************************************************************
* Classified Volume Files. *
*******************************************************************/
/* file header structure */
typedef struct {
unsigned magic; /* magic number for identification */
unsigned xlen; /* voxels in each dimension */
unsigned ylen;
unsigned zlen;
unsigned bytes_per_voxel; /* size of a classified voxel */
unsigned num_shade_fields; /* number of fields in a classified voxel
(not including opacity) */
unsigned num_x_runs; /* number of run lengths for X view */
unsigned num_x_voxels; /* number of nonzero voxels for X view */
unsigned num_x_offsets; /* number of offsets per slice for X view */
unsigned num_y_runs; /* number of run lengths for Y view */
unsigned num_y_voxels; /* number of nonzero voxels for Y view */
unsigned num_y_offsets; /* number of offsets per slice for Y view */
unsigned num_z_runs; /* number of run lengths for Z view */
unsigned num_z_voxels; /* number of nonzero voxels for Z view */
unsigned num_z_offsets; /* number of offsets per slice for Z view */
float min_opacity; /* low opacity threshold */
} RLEVoxelHdr;
/*
* File layout:
* RLEVoxelHdr hdr;
* unsigned field_size[hdr.num_shade_fields]; (size of each voxel field)
* unsigned field_offset[hdr.num_shade_fields]; (offset for each field)
* unsigned field_max[hdr.num_shade_fields]; (max. value of each field)
* padding to align to double word
* unsigned char x_run_lengths[hdr.num_x_runs]; (run lengths for X view)
* padding to align to double word
* char x_data[hdr.num_x_voxels*hdr.bytes_per_voxel]; (voxel data for X view)
* padding to align to double word
* ScanOffset x_offsets[hdr.num_x_offsets]; (scanline offset for X view)
* padding to align to double word
* unsigned char y_run_lengths[hdr.num_y_runs]; (run lengths for Y view)
* padding to align to double word
* char y_data[hdr.num_y_voxels*hdr.bytes_per_voxel]; (voxel data for Y view)
* padding to align to double word
* ScanOffset y_offsets[hdr.num_y_offsets]; (scanline offset for Y view)
* padding to align to double word
* unsigned char z_run_lengths[hdr.num_z_runs]; (run lengths for Z view)
* padding to align to double word
* char z_data[hdr.num_z_voxels*hdr.bytes_per_voxel]; (voxel data for Z view)
* padding to align to double word
* ScanOffset z_offsets[hdr.num_z_offsets]; (scanline offset for Z view)
*
* The padding ensures that voxel data can be mapped into memory
* without any word alignment problems.
*/
/*
* vpStoreClassifiedVolume
*
* Store a run-length encoded, classified volume to a file.
*/
vpResult
vpStoreClassifiedVolume(vpc, fd)
vpContext *vpc; /* context containing the volume */
int fd; /* UNIX file descriptor open for writing */
{
RLEVoxelHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int nsf, c;
unsigned size;
char pad_data[8];
int pad_bytes;
int retcode;
/* check for errors */
if ((retcode = VPCheckVoxelFields(vpc)) != VP_OK)
return(retcode);
/* write header */
header.magic = VP_CVFILE_MAGIC;
header.xlen = vpc->xlen;
header.ylen = vpc->ylen;
header.zlen = vpc->zlen;
header.bytes_per_voxel = vpc->rle_bytes_per_voxel;
header.num_shade_fields = vpc->num_shade_fields;
if (vpc->rle_x == NULL) {
header.num_x_runs = 0;
header.num_x_voxels = 0;
header.num_x_offsets = 0;
} else {
if ((retcode = VPCheckClassifiedVolume(vpc, VP_X_AXIS)) != VP_OK)
return(retcode);
header.num_x_runs = vpc->rle_x->run_count;
header.num_x_voxels = vpc->rle_x->data_count;
header.num_x_offsets = vpc->rle_x->scan_offsets_per_slice;
}
if (vpc->rle_y == NULL) {
header.num_y_runs = 0;
header.num_y_voxels = 0;
header.num_y_offsets = 0;
} else {
if ((retcode = VPCheckClassifiedVolume(vpc, VP_Y_AXIS)) != VP_OK)
return(retcode);
header.num_y_runs = vpc->rle_y->run_count;
header.num_y_voxels = vpc->rle_y->data_count;
header.num_y_offsets = vpc->rle_y->scan_offsets_per_slice;
}
if (vpc->rle_z == NULL) {
header.num_z_runs = 0;
header.num_z_voxels = 0;
header.num_z_offsets = 0;
} else {
if ((retcode = VPCheckClassifiedVolume(vpc, VP_Z_AXIS)) != VP_OK)
return(retcode);
header.num_z_runs = vpc->rle_z->run_count;
header.num_z_voxels = vpc->rle_z->data_count;
header.num_z_offsets = vpc->rle_z->scan_offsets_per_slice;
}
header.min_opacity = vpc->min_opacity;
if (vpc->write_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
/* write voxel layout information */
nsf = vpc->num_shade_fields;
for (c = 0; c < nsf; c++) {
field_data[c] = vpc->field_size[c];
field_data[nsf + c] = vpc->field_offset[c];
field_data[2*nsf + c] = vpc->field_max[c];
}
size = 3*nsf*sizeof(unsigned);
if (vpc->write_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
/* padding after header */
pad_bytes = (8 - ((sizeof(header) + size) % 8)) & 0x7;
if (pad_bytes > 0) {
bzero(pad_data, pad_bytes);
if (vpc->write_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
/* write data */
if (vpc->rle_x != NULL) {
if ((c = StoreRLEVoxels(vpc, fd, vpc->rle_x)) != VP_OK)
return(c);
}
if (vpc->rle_y != NULL) {
if ((c = StoreRLEVoxels(vpc, fd, vpc->rle_y)) != VP_OK)
return(c);
}
if (vpc->rle_z != NULL) {
if ((c = StoreRLEVoxels(vpc, fd, vpc->rle_z)) != VP_OK)
return(c);
}
return(VP_OK);
}
/*
* StoreRLEVoxels
*
* Write an RLEVoxels structure to a file.
*/
static int
StoreRLEVoxels(vpc, fd, rle_voxels)
vpContext *vpc;
int fd;
RLEVoxels *rle_voxels;
{
int size;
char pad_data[8];
int pad_bytes;
bzero(pad_data, sizeof(pad_data));
if (rle_voxels->run_count > 0) {
size = rle_voxels->run_count;
if (vpc->write_func(fd, rle_voxels->run_lengths, size) != size)
return(VPSetError(vpc, VPERROR_IO));
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->write_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
if (rle_voxels->data_count > 0) {
size = rle_voxels->data_count * vpc->rle_bytes_per_voxel;
if (vpc->write_func(fd, rle_voxels->data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->write_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
if (rle_voxels->scan_offsets_per_slice > 0) {
size = rle_voxels->scan_offsets_per_slice * rle_voxels->klen *
sizeof(ScanOffset);
if (vpc->write_func(fd, rle_voxels->scan_offsets, size) != size)
return(VPSetError(vpc, VPERROR_IO));
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->write_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
return(VP_OK);
}
/*
* vpLoadClassifiedVolume
*
* Load a run-length encoded, classified volume from a file.
*/
vpResult
vpLoadClassifiedVolume(vpc, fd)
vpContext *vpc; /* context to store the volume into */
int fd; /* UNIX file descriptor open for reading */
{
RLEVoxelHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int nsf, c, swab;
unsigned size;
unsigned char *data;
char pad_data[8];
int pad_bytes;
unsigned x_run_offset;
unsigned x_data_offset;
unsigned x_offset_offset;
unsigned y_run_offset;
unsigned y_data_offset;
unsigned y_offset_offset;
unsigned z_run_offset;
unsigned z_data_offset;
unsigned z_offset_offset;
int current_offset;
int destroy_old_volume;
/* read header */
if (vpc->read_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
swab = 0;
if (header.magic != VP_CVFILE_MAGIC) {
SwapWords(&header, sizeof(header));
if (header.magic != VP_CVFILE_MAGIC)
return(VPSetError(vpc, VPERROR_BAD_FILE));
swab = 1;
}
/* read voxel layout information */
size = 3 * header.num_shade_fields * sizeof(unsigned);
if (vpc->read_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapWords(field_data, size);
/* padding after header */
pad_bytes = (8 - ((sizeof(header) + size) % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->read_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
/* check for consistency with old volume data */
destroy_old_volume = 0;
if (vpc->xlen != header.xlen || vpc->ylen != header.ylen ||
vpc->zlen != header.zlen ||
vpc->raw_bytes_per_voxel < header.bytes_per_voxel ||
vpc->num_voxel_fields < header.num_shade_fields)
destroy_old_volume = 1;
nsf = header.num_shade_fields;
for (c = 0; c < nsf; c++) {
if (vpc->field_size[c] != field_data[c] ||
vpc->field_offset[c] != field_data[nsf + c] ||
vpc->field_max[c] != field_data[2*nsf + c])
destroy_old_volume = 1;
}
if (destroy_old_volume) {
vpDestroyClassifiedVolume(vpc);
vpDestroyMinMaxOctree(vpc);
vpc->raw_voxels = NULL;
vpc->raw_voxels_size = 0;
vpc->xstride = 0;
vpc->ystride = 0;
vpc->zstride = 0;
}
/* load new volume size */
if (destroy_old_volume) {
vpc->xlen = header.xlen;
vpc->ylen = header.ylen;
vpc->zlen = header.zlen;
vpc->raw_bytes_per_voxel = header.bytes_per_voxel;
nsf = header.num_shade_fields;
vpc->num_voxel_fields = nsf;
for (c = 0; c < nsf; c++) {
vpc->field_size[c] = field_data[c];
vpc->field_offset[c] = field_data[nsf + c];
vpc->field_max[c] = field_data[2*nsf + c];
}
}
vpc->num_shade_fields = nsf;
vpc->min_opacity = header.min_opacity;
vpc->rle_bytes_per_voxel = header.bytes_per_voxel;
/* load new volume data */
if (vpc->mmap_func != NULL && !swab) {
/* compute file offsets */
current_offset = sizeof(header) + size;
current_offset += (8 - (current_offset % 8)) & 0x7;
x_run_offset = current_offset;
current_offset += header.num_x_runs;
current_offset += (8 - (current_offset % 8)) & 0x7;
x_data_offset = current_offset;
current_offset += header.num_x_voxels * header.bytes_per_voxel;
current_offset += (8 - (current_offset % 8)) & 0x7;
x_offset_offset = current_offset;
current_offset += header.num_x_offsets * sizeof(ScanOffset);
current_offset += (8 - (current_offset % 8)) & 0x7;
y_run_offset = current_offset;
current_offset += header.num_y_runs;
current_offset += (8 - (current_offset % 8)) & 0x7;
y_data_offset = current_offset;
current_offset += header.num_y_voxels * header.bytes_per_voxel;
current_offset += (8 - (current_offset % 8)) & 0x7;
y_offset_offset = current_offset;
current_offset += header.num_y_offsets * sizeof(ScanOffset);
current_offset += (8 - (current_offset % 8)) & 0x7;
z_run_offset = current_offset;
current_offset += header.num_z_runs;
current_offset += (8 - (current_offset % 8)) & 0x7;
z_data_offset = current_offset;
current_offset += header.num_z_voxels * header.bytes_per_voxel;
current_offset += (8 - (current_offset % 8)) & 0x7;
z_offset_offset = current_offset;
current_offset += header.num_z_offsets * sizeof(ScanOffset);
/* memory-map the data */
if ((data = vpc->mmap_func(fd, current_offset,
vpc->client_data)) == NULL)
return(VPSetError(vpc, VPERROR_IO));
/* assign pointers to x view data */
vpc->rle_x = VPCreateRLEVoxels(vpc, header.ylen, header.zlen,
header.xlen, 0, 0, 0);
vpc->rle_x->run_count = header.num_x_runs;
if (header.num_x_runs > 0)
vpc->rle_x->run_lengths = (unsigned char *)(data + x_run_offset);
vpc->rle_x->data_count = header.num_x_voxels;
if (header.num_x_voxels > 0)
vpc->rle_x->data = (void *)(data + x_data_offset);
vpc->rle_x->scan_offsets_per_slice = header.num_x_offsets;
if (header.num_x_offsets > 0)
vpc->rle_x->scan_offsets = (ScanOffset *)(data + x_offset_offset);
vpc->rle_x->mmapped = 1;
/* assign pointers to y view data */
vpc->rle_y = VPCreateRLEVoxels(vpc, header.zlen, header.xlen,
header.ylen, 0, 0, 0);
vpc->rle_y->run_count = header.num_y_runs;
if (header.num_y_runs > 0)
vpc->rle_y->run_lengths = (unsigned char *)(data + y_run_offset);
vpc->rle_y->data_count = header.num_y_voxels;
if (header.num_y_voxels > 0)
vpc->rle_y->data = (void *)(data + y_data_offset);
vpc->rle_y->scan_offsets_per_slice = header.num_y_offsets;
if (header.num_y_offsets > 0)
vpc->rle_y->scan_offsets = (ScanOffset *)(data + y_offset_offset);
vpc->rle_y->mmapped = 1;
/* assign pointers to z view data */
vpc->rle_z = VPCreateRLEVoxels(vpc, header.xlen, header.ylen,
header.zlen, 0, 0, 0);
vpc->rle_z->run_count = header.num_z_runs;
if (header.num_z_runs > 0)
vpc->rle_z->run_lengths = (unsigned char *)(data + z_run_offset);
vpc->rle_z->data_count = header.num_z_voxels;
if (header.num_z_voxels > 0)
vpc->rle_z->data = (void *)(data + z_data_offset);
vpc->rle_z->scan_offsets_per_slice = header.num_z_offsets;
if (header.num_z_offsets > 0)
vpc->rle_z->scan_offsets = (ScanOffset *)(data + z_offset_offset);
vpc->rle_z->mmapped = 1;
} else {
/* read the x view data into memory */
if (header.num_x_runs != 0) {
vpc->rle_x = VPCreateRLEVoxels(vpc, header.ylen, header.zlen,
header.xlen, header.num_x_voxels, header.num_x_runs,
header.bytes_per_voxel);
if ((c = LoadRLEVoxels(vpc, fd, vpc->rle_x, header.num_x_offsets,
swab)) != VP_OK)
return(c);
}
/* read the y view data into memory */
if (header.num_y_runs != 0) {
vpc->rle_y = VPCreateRLEVoxels(vpc, header.zlen, header.xlen,
header.ylen, header.num_y_voxels, header.num_y_runs,
header.bytes_per_voxel);
if ((c = LoadRLEVoxels(vpc, fd, vpc->rle_y, header.num_y_offsets,
swab)) != VP_OK)
return(c);
}
/* read the z view data into memory */
if (header.num_z_runs != 0) {
vpc->rle_z = VPCreateRLEVoxels(vpc, header.xlen, header.ylen,
header.zlen, header.num_z_voxels, header.num_z_runs,
header.bytes_per_voxel);
if ((c = LoadRLEVoxels(vpc, fd, vpc->rle_z, header.num_z_offsets,
swab)) != VP_OK)
return(c);
}
}
#ifdef DEBUG
if (vpc->rle_x != NULL) {
printf("Checking X scanline offsets....\n");
VPCheckScanOffsets(vpc->rle_x, vpc->rle_bytes_per_voxel);
}
if (vpc->rle_y != NULL) {
printf("Checking Y scanline offsets....\n");
VPCheckScanOffsets(vpc->rle_y, vpc->rle_bytes_per_voxel);
}
if (vpc->rle_z != NULL) {
printf("Checking Z scanline offsets....\n");
VPCheckScanOffsets(vpc->rle_z, vpc->rle_bytes_per_voxel);
}
#endif
return(VP_OK);
}
/*
* LoadRLEVoxels
*
* Load an RLEVoxels structure from a file.
*/
static int
LoadRLEVoxels(vpc, fd, rle_voxels, offsets, swab)
vpContext *vpc;
int fd;
RLEVoxels *rle_voxels;
int offsets;
int swab;
{
int size;
char pad_data[8];
int pad_bytes;
if (rle_voxels->run_count > 0) {
size = rle_voxels->run_count;
if (vpc->read_func(fd, rle_voxels->run_lengths, size) != size)
return(VPSetError(vpc, VPERROR_IO));
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->read_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
if (rle_voxels->data_count > 0) {
size = rle_voxels->data_count * vpc->rle_bytes_per_voxel;
if (vpc->read_func(fd, rle_voxels->data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapVoxels(vpc, rle_voxels->data, rle_voxels->data_count,
vpc->num_shade_fields, vpc->rle_bytes_per_voxel);
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->read_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
if (offsets > 0) {
rle_voxels->scan_offsets_per_slice = offsets;
size = rle_voxels->klen * offsets * sizeof(ScanOffset);
Alloc(vpc, rle_voxels->scan_offsets, ScanOffset *, size,
"scan_offsets");
if (vpc->read_func(fd, rle_voxels->scan_offsets, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapWords(rle_voxels->scan_offsets, size);
pad_bytes = (8 - (size % 8)) & 0x7;
if (pad_bytes > 0) {
if (vpc->read_func(fd, pad_data, pad_bytes) != pad_bytes)
return(VPSetError(vpc, VPERROR_IO));
}
}
return(VP_OK);
}
/*
* SwapWords
*
* Byte-swap word data to change the endianess.
*/
static void
SwapWords(data, size)
void *data;
unsigned size;
{
unsigned char *ptr;
int tmp1, tmp2;
ptr = data;
while (size >= 4) {
tmp1 = ptr[0]; ptr[0] = ptr[3]; ptr[3] = tmp1;
tmp2 = ptr[1]; ptr[1] = ptr[2]; ptr[2] = tmp2;
size -= 4;
ptr += 4;
}
}
/*
* SwapVoxels
*
* Byte-swap voxel data to change the endianess.
*/
static void
SwapVoxels(vpc, voxels, num_voxels, fields, bytes_per_voxel)
vpContext *vpc; /* context */
void *voxels; /* array of voxels */
int num_voxels; /* number of voxels in the array */
int fields; /* number of fields in voxel */
int bytes_per_voxel; /* size of voxel in bytes */
{
int f, size, offset;
unsigned char *voxel_ptr;
int tmp1, tmp2;
/* check if any of the fields of the voxel need swapping */
size = 0;
for (f = 0; f < fields; f++) {
if (vpc->field_size[f] > size)
size = vpc->field_size[f];
}
if (size <= 1)
return;
/* do the swapping */
voxel_ptr = voxels;
while (num_voxels-- > 0) {
for (f = 0; f < fields; f++) {
size = vpc->field_size[f];
if (size == 1)
continue;
offset = vpc->field_offset[f];
if (size == 2) {
tmp1 = voxel_ptr[offset];
voxel_ptr[offset] = voxel_ptr[offset+1];
voxel_ptr[offset+1] = tmp1;
} else {
tmp1 = voxel_ptr[offset];
voxel_ptr[offset] = voxel_ptr[offset+3];
voxel_ptr[offset+3] = tmp1;
tmp2 = voxel_ptr[offset+1];
voxel_ptr[offset+1] = voxel_ptr[offset+2];
voxel_ptr[offset+2] = tmp2;
}
}
voxel_ptr += bytes_per_voxel;
}
}
/*******************************************************************
* Min-Max Octree Files. *
*******************************************************************/
/* file header structure */
typedef struct {
unsigned magic; /* magic number for identification */
unsigned xlen; /* voxels in each dimension */
unsigned ylen;
unsigned zlen;
int num_clsfy_params; /* # of params for classification */
int levels; /* number of levels in octree */
int root_node_size; /* voxels/side for root level */
int base_node_size; /* voxels/side for base level */
int range_bytes_per_node; /* bytes/node for min/max data */
int base_bytes_per_node; /* bytes/node for base level */
int nonbase_bytes_per_node; /* bytes/node for non-base level */
int status_offset; /* offset to status field */
int child_offset; /* offset to child field */
unsigned octree_bytes; /* bytes of storage for the octree */
} MinMaxOctreeHdr;
/*
* File layout:
* MinMaxOctreeHdr hdr;
* unsigned param_size[hdr.num_clsfy_params]; (size of each parameter, bytes)
* unsigned param_max[hdr.num_clsfy_params]; (max. value of each parameter)
* unsigned node_offset[hdr.num_clsfy_params];(node offset to min/max data)
* char data[octree_bytes]; (octree data)
*/
/*
* vpStoreMinMaxOctree
*
* Store a min-max octree to a file.
*/
vpResult
vpStoreMinMaxOctree(vpc, fd)
vpContext *vpc; /* context containing the octree */
int fd; /* UNIX file descriptor open for writing */
{
MinMaxOctreeHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int ncp, c;
unsigned size;
if (vpc->mm_octree == NULL)
return(VPSetError(vpc, VPERROR_BAD_SIZE));
/* write header */
bzero(&header, sizeof(MinMaxOctreeHdr));
header.magic = VP_OCTFILE_MAGIC;
header.xlen = vpc->xlen;
header.ylen = vpc->ylen;
header.zlen = vpc->zlen;
header.num_clsfy_params = vpc->num_clsfy_params;
header.levels = vpc->mm_octree->levels;
header.root_node_size = vpc->mm_octree->root_node_size;
header.base_node_size = vpc->mm_octree->base_node_size;
header.range_bytes_per_node = vpc->mm_octree->range_bytes_per_node;
header.base_bytes_per_node = vpc->mm_octree->base_bytes_per_node;
header.nonbase_bytes_per_node = vpc->mm_octree->nonbase_bytes_per_node;
header.status_offset = vpc->mm_octree->status_offset;
header.child_offset = vpc->mm_octree->child_offset;
header.octree_bytes = vpc->mm_octree->octree_bytes;
if (vpc->write_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
/* write parameter size/offset information */
ncp = vpc->num_clsfy_params;
for (c = 0; c < ncp; c++) {
field_data[c] = vpc->field_size[vpc->param_field[c]];
field_data[ncp + c] = vpc->field_max[vpc->param_field[c]];
field_data[2*ncp + c] = vpc->mm_octree->node_offset[c];
}
size = 3*ncp*sizeof(unsigned);
if (vpc->write_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
/* write octree data */
size = vpc->mm_octree->octree_bytes;
if (vpc->write_func(fd, vpc->mm_octree->root, size) != size)
return(VPSetError(vpc, VPERROR_IO));
return(VP_OK);
}
/*
* vpLoadMinMaxOctree
*
* Load a min-max octree from a file.
*/
vpResult
vpLoadMinMaxOctree(vpc, fd)
vpContext *vpc; /* context to store the octree into */
int fd; /* UNIX file descriptor open for reading */
{
MinMaxOctreeHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int ncp, c, swab;
unsigned size;
/* read header */
if (vpc->read_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
swab = 0;
if (header.magic != VP_OCTFILE_MAGIC) {
SwapWords(&header, sizeof(header));
if (header.magic != VP_OCTFILE_MAGIC)
return(VPSetError(vpc, VPERROR_BAD_FILE));
swab = 1;
}
/* read parameter size/offset information */
size = 3 * header.num_clsfy_params * sizeof(unsigned);
if (vpc->read_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapWords(field_data, size);
/* check for consistency with current volume data */
if ((c = VPCheckRawVolume(vpc)) != VP_OK)
return(c);
if (header.xlen != vpc->xlen || header.ylen != vpc->ylen ||
header.zlen != vpc->zlen ||
header.num_clsfy_params != vpc->num_clsfy_params)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
ncp = vpc->num_clsfy_params;
for (c = 0; c < ncp; c++) {
if (field_data[c] != vpc->field_size[vpc->param_field[c]] ||
field_data[ncp + c] != vpc->field_max[vpc->param_field[c]])
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
}
/* clear old octree */
vpDestroyMinMaxOctree(vpc);
/* initialize new octree */
Alloc(vpc, vpc->mm_octree, MinMaxOctree *, sizeof(MinMaxOctree),
"MinMaxOctree");
bzero(vpc->mm_octree, sizeof(MinMaxOctree));
vpc->mm_octree->levels = header.levels;
vpc->mm_octree->root_node_size = header.root_node_size;
vpc->mm_octree->base_node_size = header.base_node_size;
vpc->mm_octree->range_bytes_per_node = header.range_bytes_per_node;
vpc->mm_octree->base_bytes_per_node = header.base_bytes_per_node;
vpc->mm_octree->nonbase_bytes_per_node = header.nonbase_bytes_per_node;
vpc->mm_octree->status_offset = header.status_offset;
vpc->mm_octree->child_offset = header.child_offset;
vpc->mm_octree->octree_bytes = header.octree_bytes;
ncp = header.num_clsfy_params;
for (c = 0; c < ncp; c++)
vpc->mm_octree->node_offset[c] = field_data[2*ncp + c];
/* load octree data */
size = header.octree_bytes;
Alloc(vpc, vpc->mm_octree->root, void *, size, "mm_octree");
if (vpc->read_func(fd, vpc->mm_octree->root, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapOctreeNode(vpc, 0, vpc->mm_octree->root);
return(VP_OK);
}
/*
* SwapOctreeNode
*
* Recursive depth-first traversal of an octree to byte-swap each node's
* data (in order to switch the endianess).
*/
static void
SwapOctreeNode(vpc, level, node)
vpContext *vpc;
int level;
void *node;
{
int p, field, size, offset, tmp1, tmp2;
int child_bytes_per_node;
char *node_ptr = node;
/* byte swap min-max data */
for (p = 0; p < vpc->num_clsfy_params; p++) {
field = vpc->param_field[p];
size = vpc->field_size[field];
if (size != 1) {
ASSERT(size == 2);
offset = vpc->mm_octree->node_offset[p];
tmp1 = node_ptr[offset];
node_ptr[offset] = node_ptr[offset+1];
node_ptr[offset+1] = tmp1;
tmp2 = node_ptr[offset+2];
node_ptr[offset+2] = node_ptr[offset+3];
node_ptr[offset+3] = tmp2;
}
}
/* byte swap child pointer and recurse */
if (level != vpc->mm_octree->levels-1) {
offset = vpc->mm_octree->child_offset;
tmp1 = node_ptr[offset];
node_ptr[offset] = node_ptr[offset+3];
node_ptr[offset+3] = tmp1;
tmp2 = node_ptr[offset+1];
node_ptr[offset+1] = node_ptr[offset+2];
node_ptr[offset+2] = tmp2;
ASSERT(IntField(node, offset) != 0);
node_ptr = (char *)vpc->mm_octree->root + IntField(node, offset);
if (level == vpc->mm_octree->levels-2)
child_bytes_per_node = vpc->mm_octree->base_bytes_per_node;
else
child_bytes_per_node = vpc->mm_octree->nonbase_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
node_ptr += child_bytes_per_node;
SwapOctreeNode(vpc, level+1, node_ptr);
}
}
/*******************************************************************
* Raw Volume Files. *
*******************************************************************/
/* file header structure */
typedef struct {
unsigned magic; /* magic number for identification */
unsigned xlen; /* voxels in each dimension */
unsigned ylen;
unsigned zlen;
unsigned bytes_per_voxel; /* size of a raw voxel */
unsigned num_voxel_fields; /* number of fields in a voxel */
unsigned num_shade_fields; /* number of fields for shading */
unsigned num_clsfy_fields; /* number of fields for classification */
int xstride; /* strides for voxel data */
int ystride;
int zstride;
} RawVoxelHdr;
/*
* File layout:
* RawVoxelHdr hdr;
* unsigned field_size[hdr.num_shade_fields]; (size of each voxel field)
* unsigned field_offset[hdr.num_shade_fields]; (offset for each field)
* unsigned field_max[hdr.num_shade_fields]; (max. value of each field)
* char data[hdr.xlen*hdr.ylen*hdr.zlen*hdr.bytes_per_voxel]; (volume data)
*/
/*
* vpStoreRawVolume
*
* Store an unclassified volume to a file.
*/
vpResult
vpStoreRawVolume(vpc, fd)
vpContext *vpc; /* context containing the volume */
int fd; /* UNIX file descriptor open for writing */
{
RawVoxelHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int nvf, c;
unsigned size;
int retcode;
/* check for errors */
if ((retcode = VPCheckRawVolume(vpc)) != VP_OK)
return(retcode);
/* write header */
header.magic = VP_RVFILE_MAGIC;
header.xlen = vpc->xlen;
header.ylen = vpc->ylen;
header.zlen = vpc->zlen;
header.bytes_per_voxel = vpc->raw_bytes_per_voxel;
header.num_voxel_fields = vpc->num_voxel_fields;
header.num_shade_fields = vpc->num_shade_fields;
header.num_clsfy_fields = vpc->num_clsfy_params;
header.xstride = vpc->xstride;
header.ystride = vpc->ystride;
header.zstride = vpc->zstride;
if (vpc->write_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
/* write voxel layout information */
nvf = vpc->num_voxel_fields;
for (c = 0; c < nvf; c++) {
field_data[c] = vpc->field_size[c];
field_data[nvf + c] = vpc->field_offset[c];
field_data[2*nvf + c] = vpc->field_max[c];
}
size = 3*nvf*sizeof(unsigned);
if (vpc->write_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
/* write data */
if (vpc->write_func(fd, vpc->raw_voxels, vpc->raw_voxels_size) !=
vpc->raw_voxels_size)
return(VPSetError(vpc, VPERROR_IO));
return(VP_OK);
}
/*
* vpLoadRawVolume
*
* Load an unclassified volume from a file.
*/
vpResult
vpLoadRawVolume(vpc, fd)
vpContext *vpc; /* context to store the volume into */
int fd; /* UNIX file descriptor open for reading */
{
RawVoxelHdr header;
unsigned field_data[3*VP_MAX_FIELDS];
int nvf, c, swab;
unsigned size;
unsigned voxel_offset;
unsigned char *data;
int destroy_old_volume;
/* read header */
if (vpc->read_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
swab = 0;
if (header.magic != VP_RVFILE_MAGIC) {
SwapWords(&header, sizeof(header));
if (header.magic != VP_RVFILE_MAGIC)
return(VPSetError(vpc, VPERROR_BAD_FILE));
swab = 1;
}
/* read voxel layout information */
size = 3 * header.num_voxel_fields * sizeof(unsigned);
if (vpc->read_func(fd, field_data, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab)
SwapWords(field_data, size);
voxel_offset = sizeof(header) + size;
/* destroy old volume data */
vpDestroyClassifiedVolume(vpc);
vpDestroyMinMaxOctree(vpc);
/* load new volume size */
vpc->xlen = header.xlen;
vpc->ylen = header.ylen;
vpc->zlen = header.zlen;
vpc->raw_bytes_per_voxel = header.bytes_per_voxel;
vpc->num_voxel_fields = header.num_voxel_fields;
vpc->num_shade_fields = header.num_shade_fields;
vpc->num_clsfy_params = header.num_clsfy_fields;
vpc->xstride = header.xstride;
vpc->ystride = header.ystride;
vpc->zstride = header.zstride;
nvf = header.num_voxel_fields;
for (c = 0; c < nvf; c++) {
vpc->field_size[c] = field_data[c];
vpc->field_offset[c] = field_data[nvf + c];
vpc->field_max[c] = field_data[2*nvf + c];
}
/* load new volume data */
size = vpc->xlen*vpc->ylen*vpc->zlen*vpc->raw_bytes_per_voxel;
vpc->raw_voxels_size = size;
if (vpc->mmap_func != NULL && !swab) {
if ((vpc->raw_voxels = vpc->mmap_func(fd, voxel_offset,
vpc->client_data)) == NULL)
return(VPSetError(vpc, VPERROR_IO));
} else {
Alloc(vpc, vpc->raw_voxels, void *, size, "raw_voxels");
if (vpc->read_func(fd, vpc->raw_voxels, size) != size)
return(VPSetError(vpc, VPERROR_IO));
if (swab) {
SwapVoxels(vpc, vpc->raw_voxels, vpc->xlen*vpc->ylen*vpc->zlen,
vpc->num_voxel_fields, vpc->raw_bytes_per_voxel);
}
}
return(VP_OK);
}
/*******************************************************************
* Rendering Context Dump Files. *
*******************************************************************/
/* file header structure */
typedef struct {
unsigned magic; /* magic number for identification */
unsigned major_version; /* major version number */
unsigned minor_version; /* minor version number */
unsigned max_fields; /* value of VP_MAX_FIELDS */
unsigned max_material; /* value of VP_MAX_MATERIAL */
unsigned max_lights; /* value of VP_MAX_LIGHTS */
} VpcHdr;
/*
* File layout:
* VpcHdr hdr;
* vpContext vpc; --> truncated just before "end_of_parameters" field
* unsigned shade_color_table_size;
* float shade_color_table[shade_color_table_size];
* unsigned shade_weight_table_size;
* float shade_weight_table[shade_weight_table_size];
* for i = 1 to vpc.num_clsfy_params:
* int clsfy_table_size;
* float clsfy_table[clsfy_table_size];
*/
/*
* vpStoreContext
*
* Store the contents of a volpack context to a file. All state parameters
* stored directly in the vpContext structure are stored. User-supplied
* lookup tables are also stored. Volume data and octrees are not stored
* (use the routines specifically for storing those data structures), and
* internal tables that can be computed from other state variables
* (e.g. depth cueing lookup table) are not stored.
*/
vpResult
vpStoreContext(vpc, fd)
vpContext *vpc;
int fd;
{
VpcHdr header;
int i;
unsigned vpc_size;
header.magic = VP_VPCFILE_MAGIC;
header.major_version = VP_MAJOR_VERSION;
header.minor_version = VP_MINOR_VERSION;
header.max_fields = VP_MAX_FIELDS;
header.max_material = VP_MAX_MATERIAL;
header.max_lights = VP_MAX_LIGHTS;
vpc_size = vpFieldOffset(vpc, end_of_parameters);
if (vpc->write_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
if (vpc->write_func(fd, vpc, vpc_size) != vpc_size)
return(VPSetError(vpc, VPERROR_IO));
if (!StoreTable(vpc, fd, vpc->shade_color_table,
vpc->shade_color_table_size))
return(VPSetError(vpc, VPERROR_IO));
if (!StoreTable(vpc, fd, vpc->shade_weight_table,
vpc->shade_weight_table_size))
return(VPSetError(vpc, VPERROR_IO));
for (i = 0; i < vpc->num_clsfy_params; i++) {
if (!StoreTable(vpc, fd, vpc->clsfy_table[i],
vpc->clsfy_table_size[i]))
return(VPSetError(vpc, VPERROR_IO));
}
return(VP_OK);
}
/*
* StoreTable
*
* Store a table to a file and check for errors. Return value is 1 for
* success, 0 for failure.
*/
static int
StoreTable(vpc, fd, ptr, size)
vpContext *vpc;
int fd;
float *ptr;
unsigned size;
{
if (size == 0 || ptr == NULL) {
size = 0;
if (vpc->write_func(fd, &size, sizeof(size)) != sizeof(size))
return(0);
} else {
if (vpc->write_func(fd, &size, sizeof(size)) != sizeof(size))
return(0);
if (vpc->write_func(fd, ptr, size) != size)
return(0);
}
return(1);
}
/*
* vpLoadContext
*
* Load a volpack context from a file. The old contents of the context are
* destroyed, including any volume data. Lookup tables for shading and
* classification that are loaded from the file are stored in newly-allocated
* memory, but the application is responsible for freeing the tables;
* existing tables in the context are not overwritten (since there is no
* way for the application to predict the right table sizes), and the new
* tables are not freed when vpDestroyContext is called (since volpack
* normally does not manage the tables). Byte swapping is not performed.
*/
vpResult
vpLoadContext(vpc, fd)
vpContext *vpc;
int fd;
{
VpcHdr header;
int swab, i;
unsigned vpc_size;
/* read header */
if (vpc->read_func(fd, &header, sizeof(header)) != sizeof(header))
return(VPSetError(vpc, VPERROR_IO));
swab = 0;
if (header.magic != VP_VPCFILE_MAGIC)
return(VPSetError(vpc, VPERROR_BAD_FILE));
if (header.major_version != VP_MAJOR_VERSION ||
header.minor_version != VP_MINOR_VERSION ||
header.max_fields != VP_MAX_FIELDS ||
header.max_material != VP_MAX_MATERIAL ||
header.max_lights != VP_MAX_LIGHTS) {
return(VPSetError(vpc, VPERROR_BAD_VALUE));
}
/* destroy old data structures */
vpDestroyMinMaxOctree(vpc);
vpDestroyClassifiedVolume(vpc);
/* load new context */
vpc_size = vpFieldOffset(vpc, end_of_parameters);
if (vpc->read_func(fd, vpc, vpc_size) != vpc_size)
return(VPSetError(vpc, VPERROR_IO));
vpc->raw_voxels = NULL;
for (i = 0; i < VP_MAX_FIELDS; i++)
vpc->clsfy_table[i] = NULL;
vpc->shade_color_table = NULL;
vpc->shade_weight_table = NULL;
vpc->image = NULL;
if (vpc->shade_func == NULL)
vpc->shading_mode = LOOKUP_SHADER;
if (!LoadTable(vpc, fd, &vpc->shade_color_table,
(unsigned *)&vpc->shade_color_table_size))
goto failed;
if (!LoadTable(vpc, fd, &vpc->shade_weight_table,
(unsigned *)&vpc->shade_weight_table_size))
goto failed;
for (i = 0; i < vpc->num_clsfy_params; i++) {
if (!LoadTable(vpc, fd, &vpc->clsfy_table[i],
(unsigned *)&vpc->clsfy_table_size[i]))
goto failed;
}
return(VP_OK);
failed:
if (vpc->shade_color_table != NULL) {
Dealloc(vpc, vpc->shade_color_table);
vpc->shade_color_table = NULL;
}
if (vpc->shade_weight_table != NULL) {
Dealloc(vpc, vpc->shade_weight_table);
vpc->shade_weight_table = NULL;
}
for (i = 0; i < vpc->num_clsfy_params; i++) {
if (vpc->clsfy_table[i] != NULL) {
Dealloc(vpc, vpc->clsfy_table[i]);
vpc->clsfy_table[i] = NULL;
}
}
return(VPSetError(vpc, VPERROR_IO));
}
/*
* LoadTable
*
* Load a table from a file and check for errors. Return value is 1 for
* success, 0 for failure.
*/
static int
LoadTable(vpc, fd, ptr_ptr, size_ptr)
vpContext *vpc;
int fd;
float **ptr_ptr;
unsigned *size_ptr;
{
if (vpc->read_func(fd, size_ptr, sizeof(unsigned)) != sizeof(unsigned))
return(0);
if (*size_ptr != 0) {
Alloc(vpc, *ptr_ptr, void *, *size_ptr, "lookup table");
if (vpc->read_func(fd, *ptr_ptr, *size_ptr) != *size_ptr)
return(0);
}
return(1);
}
|