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
|
/*
* denfile.c
*
* Routines to read and write .den files.
* See the file dencopy.c for sample usage.
*/
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#ifndef MIN
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#endif
/* constants for .den magic number (map_version field) */
#define MAP_CUR_VERSION 1 /* current version */
#define MAP_CUR_VERSION_SWAB 0x0100 /* byte-swapped current version */
#define MAX_READ_SIZE 8192 /* maximum # of bytes per read(2) call */
int read_bytes();
int read_shorts();
int read_words();
int write_bytes();
/*
* read_den
*
* Load density data from a file. Return an array of bytes on success, or
* NULL on failure. The array dimensions are stored in xptr, yptr and zptr.
*/
unsigned char *
read_den(filename, xptr, yptr, zptr)
char *filename; /* name of file containing data */
int *xptr, *yptr, *zptr;/* volume dimensions (output parameters) */
{
int fd; /* file descriptor */
unsigned char *data; /* data array */
int swapbytes; /* true if header must be byte-swapped */
short map_version; /* Version of this .den file */
short orig_min[3]; /* Dimensions of original data file */
short orig_max[3];
short orig_len[3];
short extr_min[3]; /* Extracted portion of original file */
short extr_max[3]; /* (mins and maxes will be subset of */
short extr_len[3]; /* orig and lengths will be <= orig) */
short map_min[3]; /* Dimensions of this map */
short map_max[3]; /* (mins will be 0 in this program and */
short map_len[3]; /* lens may be != extr if warps > 0) */
short map_warps; /* Number of warps since extraction */
/* (0 = none) */
int map_length; /* Total number of densities in map */
/* (= product of lens) */
/* open the file */
if ((fd = open(filename, 0)) < 0) {
fprintf(stderr, "cannot open file %s\n", filename);
return(NULL);
}
/* read the magic number */
if (!read_shorts(fd, &map_version, 1, 0)) {
fprintf(stderr, "read failed on file %s (empty file?)\n", filename);
return(NULL);
}
if (map_version == MAP_CUR_VERSION) {
swapbytes = 0;
} else if (map_version == MAP_CUR_VERSION_SWAB) {
swapbytes = 1;
} else {
fprintf(stderr, "file %s is not a density file\n", filename);
return(NULL);
}
/* read the volume size information */
if (!read_shorts(fd, orig_min, 3, swapbytes) ||
!read_shorts(fd, orig_max, 3, swapbytes) ||
!read_shorts(fd, orig_len, 3, swapbytes) ||
!read_shorts(fd, extr_min, 3, swapbytes) ||
!read_shorts(fd, extr_max, 3, swapbytes) ||
!read_shorts(fd, extr_len, 3, swapbytes) ||
!read_shorts(fd, map_min, 3, swapbytes) ||
!read_shorts(fd, map_max, 3, swapbytes) ||
!read_shorts(fd, map_len, 3, swapbytes) ||
!read_shorts(fd, &map_warps, 1, swapbytes) ||
!read_words(fd, &map_length, 1, swapbytes)) {
fprintf(stderr, "read failed on file %s (truncated file?)\n",filename);
return(NULL);
}
if (map_length != map_len[0]*map_len[1]*map_len[2]) {
fprintf(stderr, "density file %s has an inconsistent header\n",
filename);
return(NULL);
}
/* allocate array for data */
data = (unsigned char *)malloc(map_length);
if (data == NULL) {
fprintf(stderr, "out of memory\n");
return(NULL);
}
/* copy the data from the file */
if (!read_bytes(fd, (char *)data, map_length)) {
fprintf(stderr, "read failed on file %s\n", filename);
close(fd);
free(data);
return(NULL);
}
/* finish up */
close(fd);
*xptr = map_len[0];
*yptr = map_len[1];
*zptr = map_len[2];
return(data);
}
/*
* write_den
*
* Write an array of volume data to a .den file. Return 1 on success,
* 0 on failure.
*/
int
write_den(filename, data, xlen, ylen, zlen)
char *filename; /* name of file to create */
unsigned char *data; /* volume data */
int xlen, ylen, zlen; /* volume dimensions */
{
int fd; /* file descriptor */
short map_version; /* Version of this .den file */
short orig_min[3]; /* Dimensions of original data file */
short orig_max[3];
short orig_len[3];
short extr_min[3]; /* Extracted portion of original file */
short extr_max[3]; /* (mins and maxes will be subset of */
short extr_len[3]; /* orig and lengths will be <= orig) */
short map_min[3]; /* Dimensions of this map */
short map_max[3]; /* (mins will be 0 in this program and */
short map_len[3]; /* lens may be != extr if warps > 0) */
short map_warps; /* Number of warps since extraction */
/* (0 = none) */
int map_length; /* Total number of densities in map */
/* (= product of lens) */
/* open file */
if ((fd = creat(filename, 0644)) < 0) {
fprintf(stderr, "cannot open file %s\n", filename);
return(0);
}
/* write the header */
map_version = MAP_CUR_VERSION;
orig_min[0] = 0;
orig_min[1] = 0;
orig_min[2] = 0;
orig_max[0] = xlen - 1;
orig_max[1] = ylen - 1;
orig_max[2] = zlen - 1;
orig_len[0] = xlen;
orig_len[1] = ylen;
orig_len[2] = zlen;
extr_min[0] = 0;
extr_min[1] = 0;
extr_min[2] = 0;
extr_max[0] = xlen - 1;
extr_max[1] = ylen - 1;
extr_max[2] = zlen - 1;
extr_len[0] = xlen;
extr_len[1] = ylen;
extr_len[2] = zlen;
map_min[0] = 0;
map_min[1] = 0;
map_min[2] = 0;
map_max[0] = xlen - 1;
map_max[1] = ylen - 1;
map_max[2] = zlen - 1;
map_len[0] = xlen;
map_len[1] = ylen;
map_len[2] = zlen;
map_warps = 0;
map_length = xlen * ylen * zlen;
if (!write_bytes(fd, (char *)&map_version, sizeof(short)) ||
!write_bytes(fd, (char *)orig_min, 3*sizeof(short)) ||
!write_bytes(fd, (char *)orig_max, 3*sizeof(short)) ||
!write_bytes(fd, (char *)orig_len, 3*sizeof(short)) ||
!write_bytes(fd, (char *)extr_min, 3*sizeof(short)) ||
!write_bytes(fd, (char *)extr_max, 3*sizeof(short)) ||
!write_bytes(fd, (char *)extr_len, 3*sizeof(short)) ||
!write_bytes(fd, (char *)map_min, 3*sizeof(short)) ||
!write_bytes(fd, (char *)map_max, 3*sizeof(short)) ||
!write_bytes(fd, (char *)map_len, 3*sizeof(short)) ||
!write_bytes(fd, (char *)&map_warps, sizeof(short)) ||
!write_bytes(fd, (char *)&map_length, sizeof(unsigned)) ||
!write_bytes(fd, (char *)data, map_length)) {
close(fd);
fprintf(stderr, "cannot write to file %s\n", filename);
return(0);
}
close(fd);
return(1);
}
/*
* write_raw
*
* Write an array of volume data to a .raw file. Return 1 on success,
* 0 on failure.
*/
int
write_raw(filename, data, xlen, ylen, zlen)
char *filename; /* name of file to create */
unsigned char *data; /* volume data */
int xlen, ylen, zlen; /* volume dimensions */
{
int fd; /* file descriptor */
int map_length; /* Total number of densities in map */
/* (= product of lens) */
/* open file */
if ((fd = creat(filename, 0644)) < 0) {
fprintf(stderr, "cannot open file %s\n", filename);
return(0);
}
/* write the header */
map_length = xlen * ylen * zlen;
if (!write_bytes(fd, (char *)data, map_length)) {
close(fd);
fprintf(stderr, "cannot write to file %s\n", filename);
return(0);
}
close(fd);
return(1);
}
/*
* read_bytes
*
* Read data from a file. The data is assumed to be a sequence of bytes,
* so no byte-swapping is needed. If the read request is a large one then
* it is broken up into chunks so the user can interrupt.
*
* Return value is 1 if the read was succesful or 0 if there was a read
* error or the full number of bytes could not be read.
*/
int
read_bytes(fd, buf, bytecount)
int fd; /* file descriptor to read from */
char *buf; /* memory in which to store data */
int bytecount; /* number of bytes to read */
{
int n;
while (bytecount > 0) {
n = MIN(bytecount, MAX_READ_SIZE);
if (read(fd, buf, n) != n)
return(0);
buf += n;
bytecount -= n;
}
return(1);
}
/*
* read_shorts
*
* Read data from a file. The data is assumed to be a sequence of shorts,
* and byte-swapping is performed if requested. If the read request is a
* large one then it is broken up into chunks so the user can interrupt.
*
* Return value is 1 if the read was succesful or 0 if there was a read
* error or the full number of shorts could not be read.
*/
int
read_shorts(fd, sbuf, shortcount, swap)
int fd; /* file descriptor to read from */
short *sbuf; /* memory in which to store data */
int shortcount; /* number of shorts to read */
int swap; /* if nonzero then swap bytes */
{
int n, c;
int bytecount = shortcount * 2;
char tmp0, tmp1, tmp2, tmp3;
char *buf = (char *)sbuf;
while (bytecount > 0) {
n = MIN(bytecount, MAX_READ_SIZE);
if (read(fd, buf, n) != n)
return(0);
bytecount -= n;
if (swap) {
c = n / 8;
n -= c * 8;
for (; c > 0; c--) {
tmp0 = buf[0]; buf[0] = buf[1]; buf[1] = tmp0;
tmp1 = buf[2]; buf[2] = buf[3]; buf[3] = tmp1;
tmp2 = buf[4]; buf[4] = buf[5]; buf[5] = tmp2;
tmp3 = buf[6]; buf[6] = buf[7]; buf[7] = tmp3;
buf += 8;
}
for (; n > 0; n -= 2) {
tmp0 = buf[0]; buf[0] = buf[1]; buf[1] = tmp0;
buf += 2;
}
} else {
buf += n;
}
}
return(1);
}
/*
* read_words
*
* Read data from a file. The data is assumed to be a sequence of words,
* and byte-swapping is performed if requested. If the read request is a
* large one then it is broken up into chunks so the user can interrupt.
*
* Return value is 1 if the read was succesful or 0 if there was a read
* error or the full number of words could not be read.
*/
int
read_words(fd, wbuf, wordcount, swap)
int fd; /* file descriptor to read from */
int *wbuf; /* memory in which to store data */
int wordcount; /* number of words to read */
int swap; /* if nonzero then swap bytes */
{
int n, c;
int bytecount = wordcount * 4;
char tmp0, tmp1, tmp2, tmp3;
char *buf = (char *)wbuf;
while (bytecount > 0) {
n = MIN(bytecount, MAX_READ_SIZE);
if (read(fd, buf, n) != n)
return(0);
bytecount -= n;
if (swap) {
c = n / 8;
n -= c * 8;
for (; c > 0; c--) {
tmp0 = buf[0]; buf[0] = buf[3]; buf[3] = tmp0;
tmp1 = buf[1]; buf[1] = buf[2]; buf[2] = tmp1;
tmp2 = buf[4]; buf[4] = buf[7]; buf[7] = tmp2;
tmp3 = buf[5]; buf[5] = buf[6]; buf[6] = tmp3;
buf += 8;
}
for (; n > 0; n -= 4) {
tmp0 = buf[0]; buf[0] = buf[3]; buf[3] = tmp0;
tmp1 = buf[1]; buf[1] = buf[2]; buf[2] = tmp1;
buf += 4;
}
} else {
buf += n;
}
}
return(1);
}
/*
* write_bytes
*
* Write data to a file. If the write request is a large one then
* it is broken up into chunks so the user can interrupt.
*
* Return value is 1 if the write was succesful or 0 if there was an error.
*/
int
write_bytes(fd, buf, bytecount)
int fd; /* file descriptor to write to */
char *buf; /* memory containing data */
int bytecount; /* number of bytes to write */
{
int n;
while (bytecount > 0) {
n = MIN(bytecount, MAX_READ_SIZE);
if (write(fd, buf, n) != n)
return(0);
buf += n;
bytecount -= n;
}
return(1);
}
|