1 /*
2  * makevolume.c
3  *
4  * Create a volume from the brainsmall data set.
5  *
6  * Copyright (c) 1994 The Board of Trustees of The Leland Stanford
7  * Junior University.  All rights reserved.
8  *
9  * Permission to use, copy, modify and distribute this software and its
10  * documentation for any purpose is hereby granted without fee, provided
11  * that the above copyright notice and this permission notice appear in
12  * all copies of this software and that you do not sell the software.
13  * Commercial licensing is available by contacting the author.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Author:
20  *    Phil Lacroute
21  *    Computer Systems Laboratory
22  *    Electrical Engineering Dept.
23  *    Stanford University
24  */
25 
26 /*
27  * $Date: 1994/12/31 19:53:03 $
28  * $Revision: 1.5 $
29  */
30 
31 #include "volume.h"
32 
main()33 main()
34 {
35     vpContext *vpc;	/* rendering context */
36     unsigned char *density; /* buffer for density data */
37     unsigned density_size;/* size of density data */
38     char *volume;	/* volume data */
39     unsigned volume_size;/* size of volume */
40     int density_fd;	/* file descriptor for density file (input) */
41     int volume_fd;	/* file descriptor for volume (output) */
42     void *malloc();
43 
44     /* create a context */
45     vpc = vpCreateContext();
46 
47     /* describe the layout of the volume */
48     vpSetVolumeSize(vpc, BRAIN_XLEN, BRAIN_YLEN, BRAIN_ZLEN);
49     vpSetVoxelSize(vpc, BYTES_PER_VOXEL, VOXEL_FIELDS,
50 		   SHADE_FIELDS, CLSFY_FIELDS);
51     vpSetVoxelField(vpc, NORMAL_FIELD, NORMAL_SIZE, NORMAL_OFFSET, NORMAL_MAX);
52     vpSetVoxelField(vpc, DENSITY_FIELD, DENSITY_SIZE, DENSITY_OFFSET,
53 		    DENSITY_MAX);
54     vpSetVoxelField(vpc, GRADIENT_FIELD, GRADIENT_SIZE, GRADIENT_OFFSET,
55 		    GRADIENT_MAX);
56 
57     /* allocate space for the raw data and the volume */
58     density_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN;
59     density = malloc(density_size);
60     volume_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN * BYTES_PER_VOXEL;
61     volume = malloc(volume_size);
62     if (density == NULL || volume == NULL) {
63 	fprintf(stderr, "out of memory\n");
64 	exit(1);
65     }
66     vpSetRawVoxels(vpc, volume, volume_size, BYTES_PER_VOXEL,
67 		   BRAIN_XLEN * BYTES_PER_VOXEL,
68 		   BRAIN_YLEN * BRAIN_XLEN * BYTES_PER_VOXEL);
69 
70     /* load the raw data */
71     if ((density_fd = open(BRAIN_FILE, 0)) < 0) {
72 	perror("open");
73 	fprintf(stderr, "could not open %s\n", BRAIN_FILE);
74 	exit(1);
75     }
76     if (lseek(density_fd, BRAIN_HEADER, 0) < 0) {
77 	perror("seek");
78 	fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
79 	exit(1);
80     }
81     if (read(density_fd, density, density_size) != density_size) {
82 	perror("read");
83 	fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
84 	exit(1);
85     }
86     close(density_fd);
87 
88     /* compute surface normals (for shading) and
89        gradient magnitudes (for classification) */
90     if (vpVolumeNormals(vpc, density, density_size, DENSITY_FIELD,
91 			GRADIENT_FIELD, NORMAL_FIELD) != VP_OK) {
92 	fprintf(stderr, "VolPack error: %s\n",
93 		vpGetErrorString(vpGetError(vpc)));
94 	exit(1);
95     }
96 
97     /* store volume in a file */
98     if ((volume_fd = creat(VOLUME_FILE, 0644)) < 0) {
99 	perror("open");
100 	fprintf(stderr, "could not open %s\n", VOLUME_FILE);
101 	exit(1);
102     }
103     if (vpStoreRawVolume(vpc, volume_fd) != VP_OK) {
104 	fprintf(stderr, "VolPack error: %s\n",
105 		vpGetErrorString(vpGetError(vpc)));
106 	exit(1);
107     }
108     close(volume_fd);
109 
110     return(0);
111 }
112