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

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

Added original make3d

File size: 6.8 KB
Line 
1/*
2
3Convert any PLY file to either a binary or an ascii PLY file, depending on
4which of WRITE_ASCII or WRITE_BINARY is defined during compilation.
5
6This program does NOT use the other_element routines because they require
7that storing the entire model.  Here, each individual element is saved
8right away and then the memory is used again for the next element.  This
9means that running out of memory is not a problem.
10
11Greg Turk, June 1994
12
13*/
14
15#include <stdlib.h>
16#include <stdio.h>
17#include <math.h>
18#include <malloc.h>
19#include <ply.h>
20
21
22#ifdef WRITE_ASCII
23void usage(char *progname) {
24  fprintf (stderr, "usage: %s [flags] [infile] > outfile\n", progname);
25  fprintf (stderr, "   or: %s [flags] < infile > outfile\n", progname);
26  fprintf (stderr, "          -p (print element labels)\n");
27  exit (-1);
28}
29#endif
30
31#ifdef WRITE_BINARY
32void usage(char *progname) {
33  fprintf (stderr, "usage: %s [infile] > outfile\n", progname);
34  fprintf (stderr, "   or: %s < infile > outfile\n", progname);
35  exit (-1);
36}
37#endif
38
39
40/******************************************************************************
41Convert binary PLY files to ascii.
42******************************************************************************/
43
44int
45main(int argc, char *argv[])
46{
47  int i,j,k;
48  PlyFile *in_ply;
49  PlyFile *out_ply;
50  int nelems;
51  char **elist;
52  int file_type;
53  float version;
54  int nprops;
55  int num_elems;
56  PlyProperty **plist;
57  PlyProperty **plist_copy;
58  PlyProperty *prop;
59  char *elem_name;
60  int num_comments;
61  char **comments;
62  int num_obj_info;
63  char **obj_info;
64  char *data;
65  int offset;
66  int *offset_list;
67  int **lists;
68  int *list_count;
69  char **list_ptr;
70  int verbose_flag = 0;
71  char *progname;
72  FILE *inFile = stdin;
73
74#ifndef WRITE_ASCII
75#ifndef WRITE_BINARY
76
77  fprintf (stderr, "'%s' compiled incorrectly.\n", argv[0]);
78  fprintf (stderr,
79           "Must have WRITE_ASCII or WRITE_BINARY defined during compile.\n");
80  exit (-1);
81
82#endif
83#endif
84
85  /* remember executable name */
86  progname = argv[0];
87  argc--;
88  argv++;
89
90  /* Parse args */
91
92#ifdef WRITE_ASCII
93  while (argc > 0) {
94    if (equal_strings (argv[0], "-h")) {
95      usage(progname);
96      exit(-1);
97    } else if (equal_strings (argv[0], "-p")) {
98      verbose_flag = 1;
99    } else if (argv[0][0] != '-') {
100      /* Open input file */
101      inFile = fopen(argv[0], "r");
102      if (inFile == NULL) {
103        fprintf(stderr, "Error: Could not open input ply file %s\n", argv[0]);
104        usage(progname);
105        exit(-1);
106      }
107    } else {
108      fprintf(stderr, "Error, unhandled arg: %s\n", argv[0]);
109      usage(progname);
110      exit(-1);
111    }
112    argc--;
113    argv++;
114  }
115
116#endif
117
118#ifdef WRITE_BINARY
119  while (argc > 0) {
120    if (equal_strings (argv[0], "-h")) {
121      usage(progname);
122      exit(-1);
123    } else if (argv[0][0] != '-') {
124      /* Open input file */
125      inFile = fopen(argv[0], "r");
126      if (inFile == NULL) {
127        fprintf(stderr, "Error: Could not open input ply file %s\n", argv[0]);
128        usage(progname);
129        exit(-1);
130      }
131    } else {
132      fprintf(stderr, "Error, unhandled arg: %s\n", argv[0]);
133      usage(progname);
134      exit(-1);
135    }
136    argc--;
137    argv++;
138  }
139
140#endif
141
142  /* open the input and output files */
143
144  in_ply  = ply_read  (inFile,  &nelems, &elist);
145
146#ifdef WRITE_ASCII
147  out_ply = ply_write (stdout, nelems, elist, PLY_ASCII);
148#endif
149
150#ifdef WRITE_BINARY
151  out_ply = ply_write (stdout, nelems, elist, PLY_BINARY_BE);
152#endif
153
154  /* allocate space for various lists that keep track of the elements */
155
156  plist_copy = (PlyProperty **) malloc (sizeof (PlyProperty *) * nelems);
157  offset_list = (int *) malloc (sizeof (int) * nelems);
158  lists = (int **) malloc (sizeof (int *) * nelems);
159  list_count = (int *) malloc (sizeof (int));
160
161  /* go through each kind of element that we learned is in the file */
162  /* and come up with a list that has offsets for all properties */
163
164  for (i = 0; i < nelems; i++) {
165
166    /* get the description of the element */
167    elem_name = elist[i];
168    plist = ply_get_element_description(in_ply, elem_name, &num_elems, &nprops);
169
170    /* make room for a list of the lists in an element, so that */
171    /* we can later easily free up the space created by malloc'ed lists */
172
173    list_count[i] = 0;
174    lists[i] = (int *) malloc (sizeof (int) * nprops);
175
176    /* set up pointers into data */
177
178    offset = 0;
179    for (j = 0; j < nprops; j++) {
180      plist[j]->offset = offset;
181      offset += 8;
182      if (plist[j]->is_list) {
183        plist[j]->count_offset = offset;
184        lists[i][list_count[i]] = offset - 8;
185        list_count[i]++;
186        offset += 8;
187      }
188    }
189
190    offset_list[i] = offset;
191
192    /* copy the property list */
193
194    plist_copy[i] = (PlyProperty *) malloc (sizeof (PlyProperty) * nprops);
195    prop = plist_copy[i];
196
197    for (j = 0; j < nprops; j++) {
198      prop->name = plist[j]->name;
199      prop->external_type = plist[j]->external_type;
200      prop->internal_type = plist[j]->external_type;
201      prop->offset = plist[j]->offset;
202      prop->is_list = plist[j]->is_list;
203      prop->count_external = plist[j]->count_external;
204      prop->count_internal = plist[j]->count_external;
205      prop->count_offset = plist[j]->count_offset;
206      prop++;
207    }
208
209    ply_describe_element (out_ply, elem_name, num_elems, nprops, plist_copy[i]);
210  }
211
212  /* copy the comments and obj_info */
213
214  comments = ply_get_comments (in_ply, &num_comments);
215  for (i = 0; i < num_comments; i++)
216    ply_put_comment (out_ply, comments[i]);
217
218  obj_info = ply_get_obj_info (in_ply, &num_obj_info);
219  for (i = 0; i < num_obj_info; i++)
220    ply_put_obj_info (out_ply, obj_info[i]);
221
222  /* finish the header for the output file */
223  ply_header_complete (out_ply);
224
225  /* copy all the element information */
226
227  for (i = 0; i < nelems; i++) {
228
229    /* get the description of the element */
230    elem_name = elist[i];
231    plist = ply_get_element_description(in_ply, elem_name, &num_elems, &nprops);
232
233    /* allocate space for an element */
234    data = (char *) malloc (8 * offset_list[i]);
235
236    /* set up for getting elements */
237    ply_get_element_setup (in_ply, elem_name, nprops, plist_copy[i]);
238    ply_put_element_setup (out_ply, elem_name);
239
240    /* possibly print out name of element */
241    if (verbose_flag)
242      fprintf (out_ply->fp, "%s:\n", elem_name);
243
244    /* copy all the elements */
245
246    if (list_count[i]) {
247      /* need to free the lists */
248      for (j = 0; j < num_elems; j++) {
249        ply_get_element (in_ply, (void *) data);
250        ply_put_element (out_ply, (void *) data);
251        for (k = 0; k < list_count[i]; k++) {
252          list_ptr = (char **) (data + lists[i][k]);
253          free (*list_ptr);
254        }
255      }
256    }
257    else {
258      /* no lists */
259      for (j = 0; j < num_elems; j++) {
260        ply_get_element (in_ply, (void *) data);
261        ply_put_element (out_ply, (void *) data);
262      }
263    }
264
265  }
266
267  /* close the PLY files */
268
269  ply_close (in_ply);
270  ply_close (out_ply);
271
272  exit(0);
273
274  return 0;
275
276}
277
Note: See TracBrowser for help on using the repository browser.