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
|
/*
* vp_check.c
*
* Consistency and error checking routines.
*
* 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.18 $
*/
#include "vp_global.h"
/* error strings for vpGetErrorString() */
static char *ErrorString[] = {
"limit exceeded",
"singular matrix or vector",
"I/O error",
"invalid buffer size",
"invalid image definition",
"invalid shader definition",
"invalid classifier definition",
"invalid volume definition",
"invalid voxel definition",
"invalid option",
"argument out of range",
"invalid file",
"cannot compute shadow buffer",
};
/*
* VPCheckVoxelFields
*
* Check the voxel field information for validity.
*/
vpResult
VPCheckVoxelFields(vpc)
vpContext *vpc;
{
int f;
int size, offset;
if (vpc->raw_bytes_per_voxel <= 0)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
if (vpc->num_voxel_fields <= 0)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
for (f = 0; f < vpc->num_voxel_fields; f++) {
size = vpc->field_size[f];
offset = vpc->field_offset[f];
if (size != 1 && size != 2 && size != 4)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
if (offset < 0 || offset + size > vpc->raw_bytes_per_voxel)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
if (f > 0 && offset < vpc->field_size[f-1] + vpc->field_offset[f-1])
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
}
return(VP_OK);
}
/*
* VPCheckRawVolume
*
* Check the raw volume for consistency.
*/
vpResult
VPCheckRawVolume(vpc)
vpContext *vpc;
{
int size, offset, retcode;
if ((retcode = VPCheckVoxelFields(vpc)) != VP_OK)
return(retcode);
if (vpc->xlen <= 0 || vpc->ylen <= 0 || vpc->zlen <= 0)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
if (vpc->raw_voxels == NULL)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
if (vpc->raw_bytes_per_voxel * vpc->xlen * vpc->ylen * vpc->zlen !=
vpc->raw_voxels_size)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
return(VP_OK);
}
/*
* VPCheckClassifiedVolume
*
* Check the classified volume for consistency.
*/
vpResult
VPCheckClassifiedVolume(vpc, axis)
vpContext *vpc;
int axis;
{
int retcode;
if ((retcode = VPCheckVoxelFields(vpc)) != VP_OK)
return(retcode);
if (vpc->xlen <= 0 || vpc->ylen <= 0 || vpc->zlen <= 0)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
if (vpc->rle_bytes_per_voxel == 0)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
switch (axis) {
case VP_X_AXIS:
if (vpc->rle_x == NULL)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
ASSERT(vpc->rle_x->ilen == vpc->ylen);
ASSERT(vpc->rle_x->jlen == vpc->zlen);
ASSERT(vpc->rle_x->klen == vpc->xlen);
break;
case VP_Y_AXIS:
if (vpc->rle_y == NULL)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
ASSERT(vpc->rle_y->ilen == vpc->zlen);
ASSERT(vpc->rle_y->jlen == vpc->xlen);
ASSERT(vpc->rle_y->klen == vpc->ylen);
break;
case VP_Z_AXIS:
if (vpc->rle_z == NULL)
return(VPSetError(vpc, VPERROR_BAD_VOLUME));
ASSERT(vpc->rle_z->ilen == vpc->xlen);
ASSERT(vpc->rle_z->jlen == vpc->ylen);
ASSERT(vpc->rle_z->klen == vpc->zlen);
break;
default:
VPBug("bad axis in VPCheckClassifiedVolume");
}
return(VP_OK);
}
/*
* VPCheckClassifier
*
* Check the classification parameters for consistency.
*/
vpResult
VPCheckClassifier(vpc)
vpContext *vpc;
{
int p, f;
int size, offset, retcode;
if ((retcode = VPCheckVoxelFields(vpc)) != VP_OK)
return(retcode);
if (vpc->num_shade_fields <= 0 ||
vpc->num_shade_fields > vpc->num_voxel_fields)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
if (vpc->num_clsfy_params <= 0 ||
vpc->num_clsfy_params > vpc->num_voxel_fields)
return(VPSetError(vpc, VPERROR_BAD_VOXEL));
for (p = 0; p < vpc->num_clsfy_params; p++) {
f = vpc->param_field[p];
if (f < 0 || f >= vpc->num_voxel_fields)
return(VPSetError(vpc, VPERROR_BAD_CLASSIFIER));
if (vpc->clsfy_table[p] == NULL || vpc->clsfy_table_size[p] !=
(vpc->field_max[f]+1)*sizeof(float))
return(VPSetError(vpc, VPERROR_BAD_CLASSIFIER));
if (p > 0 && f <= vpc->param_field[p-1])
return(VPSetError(vpc, VPERROR_BAD_CLASSIFIER));
}
return(VP_OK);
}
/*
* VPCheckShader
*
* Check the shading parameters for consistency.
*/
vpResult
VPCheckShader(vpc)
vpContext *vpc;
{
if (vpc->shading_mode == LOOKUP_SHADER) {
if (vpc->color_field < 0 ||
vpc->color_field >= vpc->num_voxel_fields ||
vpc->color_field >= vpc->num_shade_fields)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->shade_color_table == NULL)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->shade_color_table_size != vpc->color_channels*sizeof(float)*
(vpc->field_max[vpc->color_field]+1)*vpc->num_materials)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->field_size[vpc->color_field] != 2)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->num_materials < 1)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->num_materials > 1) {
if (vpc->weight_field < 0 ||
vpc->weight_field >= vpc->num_voxel_fields ||
vpc->weight_field >= vpc->num_shade_fields)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->shade_weight_table == NULL)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->shade_weight_table_size !=
(vpc->field_max[vpc->weight_field]+1) * sizeof(float) *
vpc->num_materials)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
if (vpc->field_size[vpc->weight_field] != 1)
return(VPSetError(vpc, VPERROR_BAD_SHADER));
}
}
return(VP_OK);
}
/*
* VPCheckImage
*
* Check the image buffer for validity.
*/
vpResult
VPCheckImage(vpc)
vpContext *vpc;
{
if (vpc->image == NULL || vpc->image_width <= 0 || vpc->image_height <= 0)
return(VPSetError(vpc, VPERROR_BAD_IMAGE));
switch (vpc->pixel_type) {
case VP_ALPHA:
break;
case VP_LUMINANCE:
case VP_LUMINANCEA:
if (vpc->color_channels != 1)
return(VPSetError(vpc, VPERROR_BAD_OPTION));
break;
case VP_RGB:
case VP_BGR:
case VP_RGBA:
case VP_ABGR:
if (vpc->color_channels != 3)
return(VPSetError(vpc, VPERROR_BAD_OPTION));
break;
default:
return(VPSetError(vpc, VPERROR_BAD_OPTION));
}
return(VP_OK);
}
/*
* VPCheckShadows
*
* Check the shadow specification for validity.
*/
vpResult
VPCheckShadows(vpc)
vpContext *vpc;
{
if (vpc->enable_shadows) {
if (vpc->shadow_light_num < VP_LIGHT0 ||
vpc->shadow_light_num > VP_LIGHT5)
return(VPSetError(vpc, VPERROR_BAD_OPTION));
if (!vpc->light_enable[vpc->shadow_light_num - VP_LIGHT0])
vpc->enable_shadows = 0;
if (vpc->shadow_color_table_size != vpc->shade_color_table_size ||
vpc->shadow_color_table == NULL)
return(VPSetError(vpc, VPERROR_BAD_SIZE));
}
return(VP_OK);
}
/*
* vpGetError
*
* Return the error code from the first invalid command since the last
* call to vpGetError().
*/
vpResult
vpGetError(vpc)
vpContext *vpc;
{
vpResult code;
code = vpc->error_code;
vpc->error_code = VP_OK;
return(code);
}
/*
* vpGetErrorString
*
* Return a descriptive string for an error code.
*/
char *
vpGetErrorString(code)
vpResult code;
{
if (code == VP_OK)
return("no error");
else if (code < VPERROR_FIRST || code > VPERROR_LAST)
return(NULL);
else
return(ErrorString[code - VPERROR_FIRST]);
}
/*
* VPSetError
*
* Set the error code in vpc.
*/
vpResult
VPSetError(vpc, code)
vpContext *vpc;
vpResult code;
{
if (vpc->error_code == VP_OK)
vpc->error_code = code;
return(code);
}
|