]> Dogcows Code - chaz/rasterize/blob - main.cc
finishing fifth project
[chaz/rasterize] / main.cc
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #define _POSIX_C_SOURCE 2
9 #include <cerrno>
10 #include <unistd.h>
11
12 #include "raster.hh"
13 #include "scene.hh"
14
15
16 /*
17 * Load a scene from a file.
18 */
19 static int load(scene_t** scene, const char* filename)
20 {
21 FILE* file = fopen(filename, "r");
22 if (file == NULL) {
23 fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
24 return -1;
25 }
26
27 *scene = scene_alloc(file);
28 if (*scene == NULL) {
29 fclose(file);
30 return -1;
31 }
32
33 fclose(file);
34 return 0;
35 }
36
37 /*
38 * Load a scene from standard input.
39 */
40 static int load_from_stdin(scene_t** scene)
41 {
42 *scene = scene_alloc(stdin);
43 if (*scene == NULL) {
44 return -1;
45 }
46 return 0;
47 }
48
49 /*
50 * Load a scene file, render it, and export it.
51 */
52 static void draw(const char* filename)
53 {
54 scene_t* scene;
55 TRY_DO("Loading %s", load(&scene, filename), filename);
56
57 raster_t* raster = scene_render(scene);
58 scene_destroy(scene);
59
60 char* urt = mem_strdup(filename);
61 strcut(urt, '.');
62 #if EXPORT_PPM
63 char* ppm = mem_strcat(urt, ".ppm");
64 TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
65 mem_free(ppm);
66 #endif
67 #if EXPORT_BMP
68 char* bmp = mem_strcat(urt, ".bmp");
69 TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
70 mem_free(bmp);
71 #endif
72
73 mem_free(urt);
74 raster_destroy(raster);
75 }
76
77 /*
78 * Render a scene that is read from standard input, and export to BMP.
79 */
80 static void draw_from_stdin(const char* filename)
81 {
82 scene_t* scene;
83 TRY_DO("Loading from stdin", load_from_stdin(&scene));
84
85 raster_t* raster = scene_render(scene);
86 scene_destroy(scene);
87
88 TRY_DO("Exporting to %s", raster_export_bmp(raster, filename), filename);
89
90 raster_destroy(raster);
91 }
92
93 /*
94 * Render one or more scenes from 3D scene files.
95 */
96 int main(int argc, char* argv[])
97 {
98 int out = 0;
99 int c;
100 while ((c = getopt(argc, argv, "o:")) != -1) {
101 switch (c) {
102 case 'o':
103 ++out;
104 draw_from_stdin(optarg);
105 }
106 }
107
108 if (out == 0 && argc <= 1) {
109 draw("scene.urt");
110 }
111 else {
112 for (int i = optind; i < argc; ++i) {
113 draw(argv[i]);
114 }
115 }
116
117 #if !NDEBUG
118 int _blocks = mem_blocks();
119 if (_blocks != 0) {
120 fprintf(stderr, " *** Leaked %d blocks of memory! ***\n", _blocks);
121 return EXIT_FAILURE;
122 }
123 #endif
124 return EXIT_SUCCESS;
125 }
126
This page took 0.044771 seconds and 4 git commands to generate.