* mcgarvey@eng.utah.edu
*/
-#include <stdio.h>
+#define _POSIX_C_SOURCE 2
+#include <errno.h>
+#include <unistd.h>
#include "raster.h"
#include "scene.h"
-#ifndef EXPORT_PPM
-#define EXPORT_PPM 1
-#endif
-#ifndef EXPORT_BMP
-#define EXPORT_BMP 1
-#endif
-
-
/*
* Load a scene from a file.
*/
static int load(scene_t** scene, const char* filename)
{
- *scene = scene_alloc(filename);
+ FILE* file = fopen(filename, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
+
+ *scene = scene_alloc(file);
if (*scene == NULL) {
- return 1;
+ fclose(file);
+ return -1;
}
+
+ fclose(file);
return 0;
}
/*
- * Load a scene file, render it, and export it to PPM and BMP formats.
+ * Load a scene from standard input.
*/
-static void draw(const char* filename)
+static int load_from_stdin(scene_t** scene)
{
- char* u3d;
- if (strcmp(filename, "-") == 0) {
- u3d = mem_strdup("stdin");
- }
- else {
- u3d = mem_strdup(filename);
+ *scene = scene_alloc(stdin);
+ if (*scene == NULL) {
+ return -1;
}
+ return 0;
+}
+/*
+ * Load a scene file, render it, and export it.
+ */
+static void draw(const char* filename)
+{
scene_t* scene;
- TRY_DO("Loading %s", load(&scene, filename), u3d);
+ TRY_DO("Loading %s", load(&scene, filename), filename);
raster_t* raster = scene_render(scene);
scene_destroy(scene);
raster_printstats(raster);
+ char* u3d = mem_strdup(filename);
strcut(u3d, '.');
#if EXPORT_PPM
char* ppm = mem_strcat(u3d, ".ppm");
raster_destroy(raster);
}
+/*
+ * Render a scene that is read from standard input, and export to BMP.
+ */
+static void draw_from_stdin(const char* filename)
+{
+ scene_t* scene;
+ TRY_DO("Loading from stdin", load_from_stdin(&scene));
+
+ raster_t* raster = scene_render(scene);
+ scene_destroy(scene);
+ raster_printstats(raster);
+
+ TRY_DO("Exporting to %s", raster_export_bmp(raster, filename), filename);
+
+ raster_destroy(raster);
+}
+
/*
* Render one or more scenes from 3D scene files.
*/
int main(int argc, char* argv[])
{
- if (argc <= 1) {
+ int out = 0;
+ int c;
+ while ((c = getopt(argc, argv, "o:")) != -1) {
+ switch (c) {
+ case 'o':
+ ++out;
+ draw_from_stdin(optarg);
+ }
+ }
+
+ if (out == 0 && argc <= 1) {
draw("scene.u3d");
}
else {
- for (int i = 1; i < argc; ++i) {
+ for (int i = optind; i < argc; ++i) {
draw(argv[i]);
}
}