source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/march/fromshd.c @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 2.5 KB
Line 
1/*
2
3Convert from Marc Levoy's .shd file format to Paul Ning's slice format.
4
5Greg Turk - November 1992
6
7*/
8
9#include <stdio.h>
10#include <math.h>
11
12unsigned short *density;
13unsigned char *slice;
14
15
16/******************************************************************************
17Main routine.
18******************************************************************************/
19
20main(argc,argv)
21  int argc;
22  char *argv[];
23{
24  int i,j,k;
25  int fd;
26  char infile[80],outfile[80];
27  unsigned char swap[12];
28  int shd_version;
29  int xlen,ylen,zlen;
30  int total_len;
31  int density_size;
32  int index;
33  int result;
34  int val;
35
36  strcpy (infile, argv[1]);
37
38  if (strlen (infile) < 4 ||
39      strcmp (infile + strlen (infile) - 4, ".shd") != 0)
40      strcat (infile, ".shd");
41
42  printf ("reading densities from '%s'\n", infile);
43
44  /* open the .shd file */
45
46  if ((fd = open(infile, 0)) < 0) {
47    fprintf (stderr, "bad open\n");
48    exit (-1);
49  }
50
51  /* read header info from the .shd file */
52  if (read(fd, swap, 12) < 12) {
53    fprintf (stderr, "Couldn't read from header.\n");
54    exit (-1);
55  }
56
57  /* extract the header info */
58
59  shd_version = swap[0] | (swap[1] << 8);
60  printf ("shd_version: %d\n", shd_version);
61
62  xlen = swap[2] | (swap[3] << 8);
63  ylen = swap[4] | (swap[5] << 8);
64  zlen = swap[6] | (swap[7] << 8);
65  total_len = swap[9] | (swap[10] << 8) | (swap[11] << 16) | (swap[12] << 24);
66
67  printf ("lengths (x y z): %d %d %d\n", xlen, ylen, zlen);
68  density_size = xlen * ylen * zlen;
69  printf ("density_size: %d\n", density_size);
70
71  /* allocate space for density and read it from file */
72
73  density = (unsigned short *) malloc (density_size * 2);
74  if (density == NULL) {
75    fprintf (stderr, "could not malloc density block\n");
76    exit (-1);
77  }
78
79  result = read(fd, density, density_size * 2);
80  if (result < density_size * 2) {
81    fprintf (stderr, "Couldn't read density.\n");
82    fprintf (stderr, "result = %d\n", result);
83    exit (-1);
84  }
85
86  close (fd);
87
88  /* write to slice files */
89
90  slice = (unsigned char *) malloc (sizeof (xlen * ylen));
91
92  for (i = 0; i < zlen; i++) {
93    sprintf (outfile, "slice.%0d", i);
94    index = i * xlen * ylen;
95    for (j = 0; j < xlen; j++)
96      for (k = 0; k < ylen; k++) {
97        val = density[index] & 0x00ff;
98        slice[j * ylen + k] = (unsigned char) val;
99        index++;
100      }
101    printf ("writing file '%s'\n", outfile);
102    if ((fd = creat(outfile, 0666)) < 0) {
103      fprintf (stderr, "bad open\n");
104      exit (-1);
105    }
106    write (fd, slice, xlen * ylen);
107    close (fd);
108  }
109}
110
Note: See TracBrowser for help on using the repository browser.