* mcgarvey@eng.utah.edu
*/
-#include <stdio.h>
+#define _POSIX_C_SOURCE 2
+#include <errno.h>
+#include <unistd.h>
#include "raster.h"
#include "scene.h"
*/
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) {
+ fclose(file);
+ return -1;
+ }
+
+ fclose(file);
+ return 0;
+}
+
+/*
+ * Load a scene from standard input.
+ */
+static int load_from_stdin(scene_t** scene)
+{
+ *scene = scene_alloc(stdin);
if (*scene == NULL) {
- return 1;
+ return -1;
}
return 0;
}
/*
- * Load a scene file, render it, and export it to PPM and BMP formats.
+ * Load a scene file, render it, and export it.
*/
static void draw(const char* filename)
{
char* u3d = mem_strdup(filename);
strcut(u3d, '.');
+#if EXPORT_PPM
char* ppm = mem_strcat(u3d, ".ppm");
- char* bmp = mem_strcat(u3d, ".bmp");
-
TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
+ mem_free(ppm);
+#endif
+#if EXPORT_BMP
+ char* bmp = mem_strcat(u3d, ".bmp");
TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
+ mem_free(bmp);
+#endif
mem_free(u3d);
- mem_free(ppm);
- mem_free(bmp);
+ 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);
}
*/
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]);
}
}