1 #include <stdio.h>
2 #include "rply.h"
3
callback(p_ply_argument argument)4 static int callback(p_ply_argument argument)
5 {
6 void *pdata;
7 /* just pass the value from the input file to the output file */
8 ply_get_argument_user_data(argument, &pdata, NULL);
9 ply_write((p_ply) pdata, ply_get_argument_value(argument));
10 return 1;
11 }
12
setup_callbacks(p_ply iply,p_ply oply)13 static int setup_callbacks(p_ply iply, p_ply oply)
14 {
15 p_ply_element element = NULL;
16 /* iterate over all elements in input file */
17 while ((element = ply_get_next_element(iply, element))) {
18 p_ply_property property = NULL;
19 long nelems = 0;
20 const char *element_name;
21 ply_get_element_info(element, &element_name, &nelems);
22 /* add this element to output file */
23 if (!ply_add_element(oply, element_name, nelems)) return 0;
24 /* iterate over all properties of current element */
25 while ((property = ply_get_next_property(element, property))) {
26 const char *property_name;
27 e_ply_type type, length_type, value_type;
28 ply_get_property_info(property, &property_name, &type,
29 &length_type, &value_type);
30 /* setup input callback for this property */
31 if (!ply_set_read_cb(iply, element_name, property_name, callback,
32 oply, 0)) return 0;
33 /* add this property to output file */
34 if (!ply_add_property(oply, property_name, type, length_type,
35 value_type)) return 0;
36 }
37 }
38 return 1;
39 }
40
main(int argc,char ** argv)41 int main(int argc, char **argv)
42 {
43 const char *value;
44 p_ply iply, oply;
45 iply = ply_open("input.ply", NULL);
46 if (!iply) return 1;
47 if (!ply_read_header(iply)) return 1;
48 oply = ply_create("output.ply", PLY_LITTLE_ENDIAN, NULL);
49 if (!oply) return 1;
50 if (!setup_callbacks(iply, oply)) return 1;
51 /* pass comments and obj_infos from input to output */
52 value = NULL;
53 while ((value = ply_get_next_comment(iply, value)))
54 if (!ply_add_comment(oply, value)) return 1;
55 value = NULL;
56 while ((value = ply_get_next_obj_info(iply, value)))
57 if (!ply_add_obj_info(oply, value)) return 1;;
58 /* write output header */
59 if (!ply_write_header(oply)) return 1;
60 /* read input file generating callbacks that pass data to output file */
61 if (!ply_read(iply)) return 1;
62 /* close up, we are done */
63 if (!ply_close(iply)) return 1;
64 if (!ply_close(oply)) return 1;
65 return 0;
66 }
67