48 # define strdup _strdup
51 const char *type_names[] = {
53 "char",
"short",
"int",
54 "uchar",
"ushort",
"uint",
55 "float",
"double",
"float32",
"uint8",
"int32"
58 int ply_type_size[] = {
59 0, 1, 2, 4, 1, 2, 4, 4, 8, 4, 1, 4
62 #define NO_OTHER_PROPS -1
64 #define DONT_STORE_PROP 0
71 void swap2Bytes(
void* ptr );
72 void swap4Bytes(
void* ptr );
73 void swap8Bytes(
void* ptr );
75 #ifdef EQ_LITTLE_ENDIAN
77 void swap2LE(
void* );
78 void swap2LE(
short* );
79 void swap2LE(
unsigned short* );
80 void swap4LE(
void* );
82 void swap4LE(
unsigned int* );
83 void swap4LE(
float* );
84 void swap8LE(
void* );
85 void swap8LE(
long long* );
86 void swap8LE(
unsigned long long* );
87 void swap8LE(
double* );
89 void swap2BE(
void* ptr );
90 void swap2BE(
short* ptr );
91 void swap2BE(
unsigned short* ptr );
92 void swap4BE(
void* ptr );
93 void swap4BE(
int* ptr );
94 void swap4BE(
unsigned int* ptr );
95 void swap4BE(
float* ptr );
96 void swap8BE(
long long* ptr );
97 void swap8BE(
void* ptr );
98 void swap8BE(
unsigned long long* ptr );
99 void swap8BE(
double* ptr );
101 #else // EQ_LITTLE_ENDIAN
103 void swap2LE(
void* ptr );
104 void swap2LE(
short* ptr );
105 void swap2LE(
unsigned short* ptr );
106 void swap4LE(
void* ptr );
107 void swap4LE(
int* ptr );
108 void swap4LE(
unsigned int* ptr );
109 void swap4LE(
float* ptr );
110 void swap8LE(
long long* ptr );
111 void swap8LE(
void* ptr );
112 void swap8LE(
unsigned long long* ptr );
113 void swap8LE(
double* ptr );
115 void swap2BE(
void* );
116 void swap2BE(
short* );
117 void swap2BE(
unsigned short* );
118 void swap4BE(
void* );
119 void swap4BE(
int* );
120 void swap4BE(
unsigned int* );
121 void swap4BE(
float* );
122 void swap8BE(
void* );
123 void swap8BE(
long long* );
124 void swap8BE(
unsigned long long* );
125 void swap8BE(
double* );
127 #endif // EQ_LITTLE_ENDIAN
130 int equal_strings(
const char *,
const char *);
139 void write_scalar_type (FILE *,
int);
142 char **get_words(FILE *,
int *,
char **);
145 void write_binary_item(
PlyFile *,
int,
unsigned int,
double,
int);
146 void write_ascii_item(FILE *,
int,
unsigned int,
double,
int);
149 void add_element(
PlyFile *,
char **,
int);
150 void add_property(
PlyFile *,
char **,
int);
151 void add_comment(
PlyFile *,
char *);
152 void add_obj_info(
PlyFile *,
char *);
158 void store_item(
char *,
int,
int,
unsigned int,
double);
161 void get_stored_item(
void *,
int,
int *,
unsigned int *,
double *);
164 double get_item_value(
char *,
int);
167 void get_ascii_item(
char *,
int,
int *,
unsigned int *,
double *);
168 void get_binary_item(
PlyFile *,
int,
int *,
unsigned int *,
double *);
171 void ascii_get_element(
PlyFile *,
char *);
172 void binary_get_element(
PlyFile *,
char *);
175 char *my_alloc(
int,
int,
const char *);
181 const char **elem_names,
189 const char **elem_names,
194 void ply_describe_element(
196 const char *elem_name,
202 void ply_describe_other_properties(
209 int get_prop_type(
char *type_name);
211 void ply_describe_property(
213 const char *elem_name,
221 void swap2Bytes(
void* ptr )
223 unsigned char* bytes = (
unsigned char*)ptr;
224 unsigned short* result = (
unsigned short*)ptr;
226 *result = (bytes[0]<<8) | bytes[1];
229 void swap4Bytes(
void* ptr )
231 unsigned char* bytes = (
unsigned char*)ptr;
232 unsigned int* result = (
unsigned int*)ptr;
234 *result = (bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<<8) | bytes[3];
237 void swap8Bytes(
void* ptr )
239 unsigned char* bytes = (
unsigned char*)ptr;
240 unsigned long long* result = (
unsigned long long*)ptr;
242 *result = ((
unsigned long long)(bytes[0])) << 56 |
243 ((
unsigned long long)(bytes[1])) << 48 |
244 ((
unsigned long long)(bytes[2])) << 40 |
245 ((
unsigned long long)(bytes[3])) << 32 |
246 ((
unsigned long long)(bytes[4])) << 24 |
247 ((
unsigned long long)(bytes[5])) << 16 |
248 ((
unsigned long long)(bytes[6])) << 8 |
254 #ifdef EQ_LITTLE_ENDIAN
256 void swap2LE(
void* ) {}
257 void swap2LE(
short* ) {}
258 void swap2LE(
unsigned short* ) {}
259 void swap4LE(
void* ) {}
260 void swap4LE(
int* ) {}
261 void swap4LE(
unsigned int* ) {}
262 void swap4LE(
float* ) {}
263 void swap8LE(
void* ) {}
264 void swap8LE(
long long* ) {}
265 void swap8LE(
unsigned long long* ) {}
266 void swap8LE(
double* ) {}
268 void swap2BE(
void* ptr ) { swap2Bytes(ptr); }
269 void swap2BE(
short* ptr ) { swap2Bytes(ptr); }
270 void swap2BE(
unsigned short* ptr ) { swap2Bytes(ptr); }
271 void swap4BE(
void* ptr ) { swap4Bytes(ptr); }
272 void swap4BE(
int* ptr ) { swap4Bytes(ptr); }
273 void swap4BE(
unsigned int* ptr ) { swap4Bytes(ptr); }
274 void swap4BE(
float* ptr ) { swap4Bytes(ptr); }
275 void swap8BE(
long long* ptr ) { swap8Bytes(ptr); }
276 void swap8BE(
void* ptr ) { swap8Bytes(ptr); }
277 void swap8BE(
unsigned long long* ptr ) { swap8Bytes(ptr); }
278 void swap8BE(
double* ptr ) { swap8Bytes(ptr); }
280 #else // EQ_LITTLE_ENDIAN
282 void swap2LE(
void* ptr ) { swap2Bytes(ptr); }
283 void swap2LE(
short* ptr ) { swap2Bytes(ptr); }
284 void swap2LE(
unsigned short* ptr ) { swap2Bytes(ptr); }
285 void swap4LE(
void* ptr ) { swap4Bytes(ptr); }
286 void swap4LE(
int* ptr ) { swap4Bytes(ptr); }
287 void swap4LE(
unsigned int* ptr ) { swap4Bytes(ptr); }
288 void swap4LE(
float* ptr ) { swap4Bytes(ptr); }
289 void swap8LE(
long long* ptr ) { swap8Bytes(ptr); }
290 void swap8LE(
void* ptr ) { swap8Bytes(ptr); }
291 void swap8LE(
unsigned long long* ptr ) { swap8Bytes(ptr); }
292 void swap8LE(
double* ptr ) { swap8Bytes(ptr); }
294 void swap2BE(
void* ) {}
295 void swap2BE(
short* ) {}
296 void swap2BE(
unsigned short* ) {}
297 void swap4BE(
void* ) {}
298 void swap4BE(
int* ) {}
299 void swap4BE(
unsigned int* ) {}
300 void swap4BE(
float* ) {}
301 void swap8BE(
void* ) {}
302 void swap8BE(
long long* ) {}
303 void swap8BE(
unsigned long long* ) {}
304 void swap8BE(
double* ) {}
306 #endif // EQ_LITTLE_ENDIAN
330 const char **elem_names,
345 plyfile->file_type = file_type;
346 plyfile->num_comments = 0;
347 plyfile->num_obj_info = 0;
348 plyfile->nelems = nelems;
349 plyfile->version = 1.0;
351 plyfile->other_elems = NULL;
356 for (i = 0; i < nelems; i++) {
358 plyfile->elems[i] = elem;
359 elem->name = strdup (elem_names[i]);
386 const char **elem_names,
397 name = (
char *) myalloc (
sizeof (
char) *
398 (
static_cast<int>(strlen (filename)) + 5));
399 strcpy (name, filename);
400 if (strlen (name) < 4 ||
401 strcmp (name + strlen (name) - 4,
".ply") != 0)
402 strcat (name,
".ply");
406 fp = fopen (name,
"wb");
414 plyfile = ply_write (fp, nelems, elem_names, file_type);
419 *version = plyfile->version;
438 void ply_describe_element(
440 const char *elem_name,
451 elem = find_element (plyfile, elem_name);
453 fprintf(stderr,
"ply_describe_element: can't find element '%s'\n",elem_name);
461 elem->nprops = nprops;
463 elem->store_prop = (
char *) myalloc (
sizeof (
char) * nprops);
465 for (i = 0; i < nprops; i++) {
467 elem->props[i] = prop;
468 elem->store_prop[i] = NAMED_PROP;
469 copy_property (prop, &prop_list[i]);
483 void ply_describe_property(
485 const char *elem_name,
493 elem = find_element (plyfile, elem_name);
495 fprintf(stderr,
"ply_describe_property: can't find element '%s'\n",
502 if (elem->nprops == 0) {
504 elem->store_prop = (
char *) myalloc (
sizeof (
char));
510 realloc (elem->props, sizeof (
PlyProperty *) * elem->nprops);
511 elem->store_prop = (
char *)
512 realloc (elem->store_prop, sizeof (
char) * elem->nprops);
516 elem->other_offset = 0;
518 elem->props[elem->nprops - 1] = elem_prop;
519 elem->store_prop[elem->nprops - 1] = NAMED_PROP;
520 copy_property (elem_prop, prop);
529 void ply_describe_other_properties(
540 elem = find_element (plyfile, other->name);
542 fprintf(stderr,
"ply_describe_other_properties: can't find element '%s'\n",
549 if (elem->nprops == 0) {
552 elem->store_prop = (
char *) myalloc (
sizeof (
char) * other->nprops);
557 newsize = elem->nprops + other->nprops;
559 realloc (elem->props, sizeof (
PlyProperty *) * newsize);
560 elem->store_prop = (
char *)
561 realloc (elem->store_prop, sizeof (
char) * newsize);
566 for (i = 0; i < other->nprops; i++) {
568 copy_property (prop, other->props[i]);
569 elem->props[elem->nprops] = prop;
570 elem->store_prop[elem->nprops] = OTHER_PROP;
575 elem->other_size = other->size;
576 elem->other_offset = offset;
589 void ply_element_count(
591 const char *elem_name,
598 elem = find_element (plyfile, elem_name);
600 fprintf(stderr,
"ply_element_count: can't find element '%s'\n",elem_name);
616 void ply_header_complete(
PlyFile *plyfile)
619 FILE *fp = plyfile->fp;
623 fprintf (fp,
"ply\n");
625 switch (plyfile->file_type) {
627 fprintf (fp,
"format ascii 1.0\n");
630 fprintf (fp,
"format binary_big_endian 1.0\n");
633 fprintf (fp,
"format binary_little_endian 1.0\n");
636 fprintf (stderr,
"ply_header_complete: bad file type = %d\n",
643 for (i = 0; i < plyfile->num_comments; i++)
644 fprintf (fp,
"comment %s\n", plyfile->comments[i]);
648 for (i = 0; i < plyfile->num_obj_info; i++)
649 fprintf (fp,
"obj_info %s\n", plyfile->obj_info[i]);
653 for (i = 0; i < plyfile->nelems; i++) {
655 elem = plyfile->elems[i];
656 fprintf (fp,
"element %s %d\n", elem->name, elem->num);
659 for (j = 0; j < elem->nprops; j++) {
660 prop = elem->props[j];
662 fprintf (fp,
"property list ");
663 write_scalar_type (fp, prop->count_external);
665 write_scalar_type (fp, prop->external_type);
666 fprintf (fp,
" %s\n", prop->name);
669 fprintf (fp,
"property ");
670 write_scalar_type (fp, prop->external_type);
671 fprintf (fp,
" %s\n", prop->name);
676 fprintf (fp,
"end_header\n");
689 void ply_put_element_setup(
PlyFile *plyfile,
const char *elem_name)
693 elem = find_element (plyfile, elem_name);
695 fprintf(stderr,
"ply_elements_setup: can't find element '%s'\n", elem_name);
699 plyfile->which_elem = elem;
713 void ply_put_element(
PlyFile *plyfile,
void *elem_ptr)
716 FILE *fp = plyfile->fp;
719 char *elem_data,*item;
724 unsigned int uint_val;
728 elem = plyfile->which_elem;
729 elem_data = (
char *)elem_ptr;
730 other_ptr = (
char **) (((
char *) elem_ptr) + elem->other_offset);
734 if (plyfile->file_type == PLY_ASCII) {
739 for (j = 0; j < elem->nprops; j++) {
740 prop = elem->props[j];
741 if (elem->store_prop[j] == OTHER_PROP)
742 elem_data = *other_ptr;
744 elem_data = (
char *)elem_ptr;
746 item = elem_data + prop->count_offset;
747 get_stored_item ((
void *) item, prop->count_internal,
748 &int_val, &uint_val, &double_val);
749 write_ascii_item (fp, int_val, uint_val, double_val,
750 prop->count_external);
751 list_count = uint_val;
752 item_ptr = (
char **) (elem_data + prop->offset);
754 item_size = ply_type_size[prop->internal_type];
755 for (k = 0; k < list_count; k++) {
756 get_stored_item ((
void *) item, prop->internal_type,
757 &int_val, &uint_val, &double_val);
758 write_ascii_item (fp, int_val, uint_val, double_val,
759 prop->external_type);
764 item = elem_data + prop->offset;
765 get_stored_item ((
void *) item, prop->internal_type,
766 &int_val, &uint_val, &double_val);
767 write_ascii_item (fp, int_val, uint_val, double_val,
768 prop->external_type);
779 for (j = 0; j < elem->nprops; j++) {
780 prop = elem->props[j];
781 if (elem->store_prop[j] == OTHER_PROP)
782 elem_data = *other_ptr;
784 elem_data = (
char *)elem_ptr;
786 item = elem_data + prop->count_offset;
787 item_size = ply_type_size[prop->count_internal];
788 get_stored_item ((
void *) item, prop->count_internal,
789 &int_val, &uint_val, &double_val);
790 write_binary_item (plyfile, int_val, uint_val, double_val,
791 prop->count_external);
792 list_count = uint_val;
793 item_ptr = (
char **) (elem_data + prop->offset);
795 item_size = ply_type_size[prop->internal_type];
796 for (k = 0; k < list_count; k++) {
797 get_stored_item ((
void *) item, prop->internal_type,
798 &int_val, &uint_val, &double_val);
799 write_binary_item (plyfile, int_val, uint_val, double_val,
800 prop->external_type);
805 item = elem_data + prop->offset;
806 item_size = ply_type_size[prop->internal_type];
807 get_stored_item ((
void *) item, prop->internal_type,
808 &int_val, &uint_val, &double_val);
809 write_binary_item (plyfile, int_val, uint_val, double_val,
810 prop->external_type);
826 void ply_put_comment(
PlyFile *plyfile,
const char *comment)
829 if (plyfile->num_comments == 0)
831 plyfile->comments = (
char **) myalloc (
sizeof (
char *));
835 plyfile->comments = (
char **) realloc (plyfile->comments, sizeof (
char *) *
836 (plyfile->num_comments + 1));
840 plyfile->comments[plyfile->num_comments] = strdup (comment);
841 plyfile->num_comments++;
854 void ply_put_obj_info(
PlyFile *plyfile,
const char *obj_info)
857 if (plyfile->num_obj_info == 0)
859 plyfile->obj_info = (
char **) myalloc (
sizeof (
char *));
863 plyfile->obj_info = (
char **) realloc (plyfile->obj_info, sizeof (
char *) *
864 (plyfile->num_obj_info + 1));
868 plyfile->obj_info[plyfile->num_obj_info] = strdup (obj_info);
869 plyfile->num_obj_info++;
896 PlyFile *ply_read(FILE *fp,
int *nelems,
char ***elem_names)
914 plyfile->comments = NULL;
915 plyfile->num_comments = 0;
916 plyfile->obj_info = NULL;
917 plyfile->num_obj_info = 0;
919 plyfile->other_elems = NULL;
923 words = get_words (plyfile->fp, &nwords, &orig_line);
924 if (!words || !equal_strings (words[0],
"ply"))
934 if (equal_strings (words[0],
"format")) {
940 if (equal_strings (words[1],
"ascii"))
941 plyfile->file_type = PLY_ASCII;
942 else if (equal_strings (words[1],
"binary_big_endian"))
943 plyfile->file_type = PLY_BINARY_BE;
944 else if (equal_strings (words[1],
"binary_little_endian"))
945 plyfile->file_type = PLY_BINARY_LE;
952 plyfile->version = atof (words[2]);
954 else if (equal_strings (words[0],
"element"))
955 add_element (plyfile, words, nwords);
956 else if (equal_strings (words[0],
"property"))
957 add_property (plyfile, words, nwords);
958 else if (equal_strings (words[0],
"comment"))
959 add_comment (plyfile, orig_line);
960 else if (equal_strings (words[0],
"obj_info"))
961 add_obj_info (plyfile, orig_line);
962 else if (equal_strings (words[0],
"end_header"))
971 words = get_words (plyfile->fp, &nwords, &orig_line);
978 for (i = 0; i < plyfile->nelems; i++) {
979 elem = plyfile->elems[i];
980 elem->store_prop = (
char *) myalloc (
sizeof (
char) * elem->nprops);
981 for (j = 0; j < elem->nprops; j++)
982 elem->store_prop[j] = DONT_STORE_PROP;
983 elem->other_offset = NO_OTHER_PROPS;
988 elist = (
char **) myalloc (
sizeof (
char *) * plyfile->nelems);
989 for (i = 0; i < plyfile->nelems; i++)
990 elist[i] = strdup (plyfile->elems[i]->name);
993 *nelems = plyfile->nelems;
1015 PlyFile *ply_open_for_reading(
1029 name = (
char *) myalloc (
sizeof (
char) *
1030 (
static_cast<int>(strlen (filename) + 5)));
1031 strcpy (name, filename);
1032 if (strlen (name) < 4 ||
1033 strcmp (name + strlen (name) - 4,
".ply") != 0)
1034 strcat (name,
".ply");
1038 fp = fopen (name,
"rb");
1045 plyfile = ply_read (fp, nelems, elem_names);
1049 *file_type = plyfile->file_type;
1050 *version = plyfile->version;
1084 elem = find_element (plyfile, elem_name);
1088 *nelems = elem->num;
1089 *nprops = elem->nprops;
1093 for (i = 0; i < elem->nprops; i++) {
1095 copy_property (prop, elem->props[i]);
1096 prop_list[i] = prop;
1115 void ply_get_element_setup(
PlyFile *plyfile,
char *elem_name,
int nprops,
1124 elem = find_element (plyfile, elem_name);
1125 plyfile->which_elem = elem;
1128 for (i = 0; i < nprops; i++)
1131 prop = find_property (elem, prop_list[i].name, &index);
1135 "Warning: Can't find property '%s' in element '%s'\n",
1136 prop_list[i].name, elem_name );
1141 prop->internal_type = prop_list[i].internal_type;
1142 prop->offset = prop_list[i].offset;
1143 prop->count_internal = prop_list[i].count_internal;
1144 prop->count_offset = prop_list[i].count_offset;
1147 elem->store_prop[index] = STORE_PROP;
1164 void ply_get_property(
1166 const char *elem_name,
1175 elem = find_element (plyfile, elem_name);
1176 plyfile->which_elem = elem;
1180 prop_ptr = find_property (elem, prop->name, &index);
1181 if (prop_ptr == NULL) {
1182 fprintf (stderr,
"Warning: Can't find property '%s' in element '%s'\n",
1183 prop->name, elem_name);
1186 prop_ptr->internal_type = prop->internal_type;
1187 prop_ptr->offset = prop->offset;
1188 prop_ptr->count_internal = prop->count_internal;
1189 prop_ptr->count_offset = prop->count_offset;
1192 elem->store_prop[index] = STORE_PROP;
1206 void ply_get_element(
PlyFile *plyfile,
void *elem_ptr)
1208 if (plyfile->file_type == PLY_ASCII)
1209 ascii_get_element (plyfile, (
char *) elem_ptr);
1211 binary_get_element (plyfile, (
char *) elem_ptr);
1226 char **ply_get_comments(
PlyFile *plyfile,
int *num_comments)
1228 *num_comments = plyfile->num_comments;
1229 return (plyfile->comments);
1245 char **ply_get_obj_info(
PlyFile *plyfile,
int *num_obj_info)
1247 *num_obj_info = plyfile->num_obj_info;
1248 return (plyfile->obj_info);
1274 for (type_size = 8; type_size > 0; type_size /= 2) {
1279 for (i = 0; i < elem->nprops; i++) {
1282 if (elem->store_prop[i])
1285 prop = elem->props[i];
1288 prop->internal_type = prop->external_type;
1289 prop->count_internal = prop->count_external;
1292 if (prop->is_list) {
1295 if (type_size ==
sizeof (
void *)) {
1296 prop->offset = size;
1297 size +=
sizeof (
void *);
1301 if (type_size == ply_type_size[prop->count_external]) {
1302 prop->count_offset = size;
1303 size += ply_type_size[prop->count_external];
1307 else if (type_size == ply_type_size[prop->external_type]) {
1308 prop->offset = size;
1309 size += ply_type_size[prop->external_type];
1316 elem->other_size = size;
1347 elem = find_element (plyfile, elem_name);
1349 fprintf (stderr,
"ply_get_other_properties: Can't find element '%s'\n",
1355 plyfile->which_elem = elem;
1358 elem->other_offset = offset;
1361 setup_other_props (plyfile, elem);
1365 other->name = strdup (elem_name);
1367 if (elem->other_offset == NO_OTHER_PROPS) {
1369 other->props = NULL;
1374 other->size = elem->other_size;
1379 for (i = 0; i < elem->nprops; i++) {
1380 if (elem->store_prop[i])
1383 copy_property (prop, elem->props[i]);
1384 other->props[nprops] = prop;
1387 other->nprops = nprops;
1391 if (other->nprops == 0) {
1392 elem->other_offset = NO_OTHER_PROPS;
1435 elem = find_element (plyfile, elem_name);
1438 "ply_get_other_element: can't find element '%s'\n", elem_name);
1445 if (plyfile->other_elems == NULL) {
1447 other_elems = plyfile->other_elems;
1449 other = &(other_elems->other_list[0]);
1450 other_elems->num_elems = 1;
1453 other_elems = plyfile->other_elems;
1454 other_elems->other_list = (
OtherElem *) realloc (other_elems->other_list,
1455 sizeof (
OtherElem) * other_elems->num_elems + 1);
1456 other = &(other_elems->other_list[other_elems->num_elems]);
1457 other_elems->num_elems++;
1461 other->elem_count = elem_count;
1464 other->elem_name = strdup (elem_name);
1468 malloc (
sizeof (
OtherData *) * other->elem_count);
1471 other->other_props = ply_get_other_properties (plyfile, elem_name,
1475 for (i = 0; i < other->elem_count; i++) {
1478 ply_get_element (plyfile, (
void *) other->other_data[i]);
1482 return (other_elems);
1495 void ply_describe_other_elements (
1504 if (other_elems == NULL)
1508 plyfile->other_elems = other_elems;
1512 for (i = 0; i < other_elems->num_elems; i++) {
1513 other = &(other_elems->other_list[i]);
1514 ply_element_count (plyfile, other->elem_name, other->elem_count);
1515 ply_describe_other_properties (plyfile, other->other_props,
1528 void ply_put_other_elements (
PlyFile *plyfile)
1534 if (plyfile->other_elems == NULL)
1539 for (i = 0; i < plyfile->other_elems->num_elems; i++) {
1541 other = &(plyfile->other_elems->other_list[i]);
1542 ply_put_element_setup (plyfile, other->elem_name);
1545 for (j = 0; j < other->elem_count; j++)
1546 ply_put_element (plyfile, (
void *) other->other_data[j]);
1578 void ply_close(
PlyFile *plyfile)
1583 fclose (plyfile->fp);
1587 for (i=0; i<plyfile->nelems; i++)
1589 elem = plyfile->elems[i];
1590 if ( elem->name ) {free(elem->name);}
1591 for (j=0; j<elem->nprops; j++)
1593 if ( elem->props[j]->name )
1594 {free(const_cast<char *>(elem->props[j]->name));}
1595 free (elem->props[j]);
1598 free (elem->store_prop);
1601 free(plyfile->elems);
1603 for (i=0; i<plyfile->num_comments; i++)
1605 free (plyfile->comments[i]);
1607 free (plyfile->comments);
1609 for (i=0; i<plyfile->num_obj_info; i++)
1611 free (plyfile->obj_info[i]);
1613 free (plyfile->obj_info);
1630 void ply_get_info(
PlyFile *ply,
float *version,
int *file_type)
1635 *version = ply->version;
1636 *file_type = ply->file_type;
1644 int equal_strings(
const char *s1,
const char *s2)
1672 for (i = 0; i < plyfile->nelems; i++)
1673 if (equal_strings (element, plyfile->elems[i]->name))
1674 return (plyfile->elems[i]);
1696 for( i = 0; i < elem->nprops; i++)
1697 if (equal_strings (prop_name, elem->props[i]->name))
1700 return (elem->props[i]);
1716 void ascii_get_element(
PlyFile *plyfile,
char *elem_ptr)
1724 char *elem_data,*item=0;
1728 unsigned int uint_val;
1738 elem = plyfile->which_elem;
1742 if (elem->other_offset != NO_OTHER_PROPS) {
1746 other_data = (
char *) myalloc (elem->other_size);
1748 ptr = (
char **) (elem_ptr + elem->other_offset);
1756 words = get_words (plyfile->fp, &nwords, &orig_line);
1757 if (words == NULL) {
1758 fprintf (stderr,
"ply_get_element: unexpected end of file\n");
1764 for (j = 0; j < elem->nprops; j++) {
1766 prop = elem->props[j];
1767 store_it = (elem->store_prop[j] | other_flag);
1770 if (elem->store_prop[j])
1771 elem_data = elem_ptr;
1773 elem_data = other_data;
1775 if (prop->is_list) {
1778 get_ascii_item (words[which_word++], prop->count_external,
1779 &int_val, &uint_val, &double_val);
1781 item = elem_data + prop->count_offset;
1782 store_item(item, prop->count_internal, int_val, uint_val, double_val);
1786 list_count = int_val;
1787 item_size = ply_type_size[prop->internal_type];
1788 store_array = (
char **) (elem_data + prop->offset);
1790 if (list_count == 0) {
1792 *store_array = NULL;
1796 item_ptr = (
char *) myalloc (
sizeof (
char) * item_size * list_count);
1798 *store_array = item_ptr;
1802 for (k = 0; k < list_count; k++) {
1803 get_ascii_item (words[which_word++], prop->external_type,
1804 &int_val, &uint_val, &double_val);
1806 store_item (item, prop->internal_type,
1807 int_val, uint_val, double_val);
1815 get_ascii_item (words[which_word++], prop->external_type,
1816 &int_val, &uint_val, &double_val);
1818 item = elem_data + prop->offset;
1819 store_item (item, prop->internal_type, int_val, uint_val, double_val);
1837 void binary_get_element(
PlyFile *plyfile,
char *elem_ptr)
1843 char *elem_data,*item=0;
1847 unsigned int uint_val;
1856 elem = plyfile->which_elem;
1860 if (elem->other_offset != NO_OTHER_PROPS) {
1864 other_data = (
char *) myalloc (elem->other_size);
1866 ptr = (
char **) (elem_ptr + elem->other_offset);
1874 for (j = 0; j < elem->nprops; j++) {
1876 prop = elem->props[j];
1877 store_it = (elem->store_prop[j] | other_flag);
1880 if (elem->store_prop[j])
1881 elem_data = elem_ptr;
1883 elem_data = other_data;
1885 if (prop->is_list) {
1888 get_binary_item (plyfile, prop->count_external,
1889 &int_val, &uint_val, &double_val);
1891 item = elem_data + prop->count_offset;
1892 store_item(item, prop->count_internal, int_val, uint_val, double_val);
1896 list_count = int_val;
1902 item_size = ply_type_size[prop->internal_type];
1904 store_array = (
char **) (elem_data + prop->offset);
1905 if (list_count == 0) {
1907 *store_array = NULL;
1911 item_ptr = (
char *) myalloc (
sizeof (
char) * item_size * list_count);
1913 *store_array = item_ptr;
1917 for (k = 0; k < list_count; k++) {
1918 get_binary_item (plyfile, prop->external_type,
1919 &int_val, &uint_val, &double_val);
1921 store_item (item, prop->internal_type,
1922 int_val, uint_val, double_val);
1930 get_binary_item (plyfile, prop->external_type,
1931 &int_val, &uint_val, &double_val);
1933 item = elem_data + prop->offset;
1934 store_item (item, prop->internal_type, int_val, uint_val, double_val);
1950 void write_scalar_type (FILE *fp,
int code)
1954 if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) {
1955 fprintf (stderr,
"write_scalar_type: bad data code = %d\n", code);
1961 fprintf (fp,
"%s", type_names[code]);
1980 char **get_words(FILE *fp,
int *nwords,
char **orig_line)
1982 #define BIG_STRING 4096
1983 static char str[BIG_STRING];
1984 static char str_copy[BIG_STRING];
1992 result = fgets (str, BIG_STRING, fp);
1993 if (result == NULL) {
1999 words = (
char **) myalloc (
sizeof (
char *) * max_words);
2005 str[BIG_STRING-2] =
' ';
2006 str[BIG_STRING-1] =
'\0';
2008 for (ptr = str, ptr2 = str_copy; *ptr !=
'\0'; ptr++, ptr2++) {
2014 else if (*ptr ==
'\n') {
2024 while (*ptr !=
'\0') {
2035 if (num_words >= max_words) {
2037 words = (
char **) realloc (words,
sizeof (
char *) * max_words);
2039 words[num_words++] = ptr;
2050 *nwords = num_words;
2051 *orig_line = str_copy;
2067 double get_item_value(
char *item,
int type)
2069 unsigned char *puchar;
2072 unsigned short int *pushort;
2074 unsigned int *puint;
2078 unsigned int uint_value;
2079 double double_value;
2083 pchar = (
char *) item;
2085 return ((
double) int_value);
2088 puchar = (
unsigned char *) item;
2089 int_value = *puchar;
2090 return ((
double) int_value);
2092 pshort = (
short int *) item;
2093 int_value = *pshort;
2094 return ((
double) int_value);
2096 pushort = (
unsigned short int *) item;
2097 int_value = *pushort;
2098 return ((
double) int_value);
2101 pint = (
int *) item;
2103 return ((
double) int_value);
2105 puint = (
unsigned int *) item;
2106 uint_value = *puint;
2107 return ((
double) uint_value);
2110 pfloat = (
float *) item;
2111 double_value = *pfloat;
2112 return (double_value);
2114 pdouble = (
double *) item;
2115 double_value = *pdouble;
2116 return (double_value);
2118 fprintf (stderr,
"get_item_value: bad type = %d\n", type);
2134 void write_binary_item(
PlyFile *plyfile,
2136 unsigned int uint_val,
2141 FILE *fp = plyfile->fp;
2142 unsigned char uchar_val;
2144 unsigned short ushort_val;
2151 fwrite (&char_val, 1, 1, fp);
2154 short_val = int_val;
2155 if( plyfile->file_type == PLY_BINARY_BE )
2156 swap2BE(&short_val);
2158 swap2LE(&short_val);
2159 fwrite (&short_val, 2, 1, fp);
2163 if( plyfile->file_type == PLY_BINARY_BE )
2171 fwrite (&int_val, 4, 1, fp);
2175 uchar_val = uint_val;
2176 fwrite (&uchar_val, 1, 1, fp);
2179 if( plyfile->file_type == PLY_BINARY_BE )
2181 swap2BE(&ushort_val);
2185 swap2LE(&ushort_val);
2187 ushort_val = uint_val;
2188 fwrite (&ushort_val, 2, 1, fp);
2191 if( plyfile->file_type == PLY_BINARY_BE )
2199 fwrite (&uint_val, 4, 1, fp);
2203 float_val = double_val;
2204 if( plyfile->file_type == PLY_BINARY_BE )
2206 swap4BE(&float_val);
2210 swap4LE(&float_val);
2212 fwrite (&float_val, 4, 1, fp);
2215 if( plyfile->file_type == PLY_BINARY_BE )
2217 swap8BE(&double_val);
2221 swap8LE(&double_val);
2223 fwrite (&double_val, 8, 1, fp);
2226 fprintf (stderr,
"write_binary_item: bad type = %d\n", type);
2243 void write_ascii_item(
2246 unsigned int uint_val,
2256 fprintf (fp,
"%d ", int_val);
2262 fprintf (fp,
"%u ", uint_val);
2267 fprintf (fp,
"%g ", double_val);
2270 fprintf (stderr,
"write_ascii_item: bad type = %d\n", type);
2289 void get_stored_item(
2293 unsigned int *uint_val,
2299 *int_val = *((
char *) ptr);
2300 *uint_val = *int_val;
2301 *double_val = *int_val;
2305 *uint_val = *((
unsigned char *) ptr);
2306 *int_val = *uint_val;
2307 *double_val = *uint_val;
2310 *int_val = *((
short int *) ptr);
2311 *uint_val = *int_val;
2312 *double_val = *int_val;
2315 *uint_val = *((
unsigned short int *) ptr);
2316 *int_val = *uint_val;
2317 *double_val = *uint_val;
2321 *int_val = *((
int *) ptr);
2322 *uint_val = *int_val;
2323 *double_val = *int_val;
2326 *uint_val = *((
unsigned int *) ptr);
2327 *int_val = *uint_val;
2328 *double_val = *uint_val;
2332 *double_val = *((
float *) ptr);
2333 *int_val = (int) *double_val;
2334 *uint_val = (
unsigned int) *double_val;
2337 *double_val = *((
double *) ptr);
2338 *int_val = (int) *double_val;
2339 *uint_val = (
unsigned int) *double_val;
2342 fprintf (stderr,
"get_stored_item: bad type = %d\n", type);
2362 void get_binary_item(
2366 unsigned int *uint_val,
2377 if( fread (ptr, 1, 1, plyfile->fp) <= 0 )
2379 *int_val = *((
char *) ptr);
2380 *uint_val = *int_val;
2381 *double_val = *int_val;
2385 if( fread (ptr, 1, 1, plyfile->fp) <= 0 )
2387 *uint_val = *((
unsigned char *) ptr);
2388 *int_val = *uint_val;
2389 *double_val = *uint_val;
2392 if( fread (ptr, 2, 1, plyfile->fp) <= 0 )
2394 if( plyfile->file_type == PLY_BINARY_BE )
2402 *int_val = *((
short int *) ptr);
2403 *uint_val = *int_val;
2404 *double_val = *int_val;
2407 if( fread (ptr, 2, 1, plyfile->fp) <= 0 )
2409 if( plyfile->file_type == PLY_BINARY_BE )
2417 *uint_val = *((
unsigned short int *) ptr);
2418 *int_val = *uint_val;
2419 *double_val = *uint_val;
2423 if( fread (ptr, 4, 1, plyfile->fp) <= 0 )
2425 if( plyfile->file_type == PLY_BINARY_BE )
2433 *int_val = *((
int *) ptr);
2434 *uint_val = *int_val;
2435 *double_val = *int_val;
2438 if( fread (ptr, 4, 1, plyfile->fp) <= 0 )
2440 if( plyfile->file_type == PLY_BINARY_BE )
2448 *uint_val = *((
unsigned int *) ptr);
2449 *int_val = *uint_val;
2450 *double_val = *uint_val;
2454 if( fread (ptr, 4, 1, plyfile->fp) <= 0 )
2456 if( plyfile->file_type == PLY_BINARY_BE )
2464 *double_val = *((
float *) ptr);
2465 *int_val = (int) *double_val;
2466 *uint_val = (
unsigned int) *double_val;
2469 if( fread (ptr, 8, 1, plyfile->fp) <= 0 )
2471 if( plyfile->file_type == PLY_BINARY_BE )
2479 *double_val = *((
double *) ptr);
2480 *int_val = (int) *double_val;
2481 *uint_val = (
unsigned int) *double_val;
2484 fprintf (stderr,
"get_binary_item: bad type = %d\n", type);
2504 void get_ascii_item(
2508 unsigned int *uint_val,
2520 *int_val = atoi (word);
2521 *uint_val = *int_val;
2522 *double_val = *int_val;
2526 *uint_val = strtoul (word, (
char **) NULL, 10);
2527 *int_val = *uint_val;
2528 *double_val = *uint_val;
2534 *double_val = atof (word);
2535 *int_val = (int) *double_val;
2536 *uint_val = (
unsigned int) *double_val;
2540 fprintf (stderr,
"get_ascii_item: bad type = %d\n", type);
2564 unsigned int uint_val,
2568 unsigned char *puchar;
2570 unsigned short int *pushort;
2572 unsigned int *puint;
2582 puchar = (
unsigned char *) item;
2586 pshort = (
short *) item;
2590 pushort = (
unsigned short *) item;
2591 *pushort = uint_val;
2595 pint = (
int *) item;
2599 puint = (
unsigned int *) item;
2604 pfloat = (
float *) item;
2605 *pfloat = double_val;
2608 pdouble = (
double *) item;
2609 *pdouble = double_val;
2612 fprintf (stderr,
"store_item: bad type = %d\n", type);
2627 void add_element (
PlyFile *plyfile,
char **words,
int)
2633 elem->name = strdup (words[1]);
2634 elem->num = atoi (words[2]);
2638 if (plyfile->nelems == 0)
2641 plyfile->elems = (
PlyElement **) realloc (plyfile->elems,
2642 sizeof (
PlyElement *) * (plyfile->nelems + 1));
2645 plyfile->elems[plyfile->nelems] = elem;
2660 int get_prop_type(
char *type_name)
2664 for (i = PLY_START_TYPE + 1; i < PLY_END_TYPE; i++)
2665 if (equal_strings (type_name, type_names[i]))
2682 void add_property (
PlyFile *plyfile,
char **words,
int )
2691 if (equal_strings (words[1],
"list")) {
2692 prop->count_external = get_prop_type (words[2]);
2693 prop->external_type = get_prop_type (words[3]);
2694 prop->name = strdup (words[4]);
2698 prop->external_type = get_prop_type (words[1]);
2699 prop->name = strdup (words[2]);
2705 elem = plyfile->elems[plyfile->nelems - 1];
2707 if (elem->nprops == 0)
2710 elem->props = (
PlyProperty **) realloc (elem->props,
2713 elem->props[elem->nprops] = prop;
2726 void add_comment (
PlyFile *plyfile,
char *line)
2732 while (line[i] ==
' ' || line[i] ==
'\t')
2735 ply_put_comment (plyfile, &line[i]);
2747 void add_obj_info (
PlyFile *plyfile,
char *line)
2753 while (line[i] ==
' ' || line[i] ==
'\t')
2756 ply_put_obj_info (plyfile, &line[i]);
2766 dest->name = strdup (src->name);
2767 dest->external_type = src->external_type;
2768 dest->internal_type = src->internal_type;
2769 dest->offset = src->offset;
2771 dest->is_list = src->is_list;
2772 dest->count_external = src->count_external;
2773 dest->count_internal = src->count_internal;
2774 dest->count_offset = src->count_offset;
2787 char *my_alloc(
int size,
int lnum,
const char *fname)
2791 ptr = (
char *) malloc (size);
2794 fprintf( stderr,
"Memory allocation bombed on line %d in %s\n",
bool exit()
De-initialize the Equalizer fabric namespace.