X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Frasterize;a=blobdiff_plain;f=main.c;h=8f4ca4d4d7ab52636fcb4d3ed66ffe186a668b03;hp=08a752e0494f9eb17139588a68bead39f7cce363;hb=09dd89d10e65029f0be313dd463ba1f43cac2fbb;hpb=c875478cdd823c7df8fdc859941bd9e5948c9315 diff --git a/main.c b/main.c index 08a752e..8f4ca4d 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,9 @@ * mcgarvey@eng.utah.edu */ -#include +#define _POSIX_C_SOURCE 2 +#include +#include #include "raster.h" #include "scene.h" @@ -16,15 +18,36 @@ */ 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) { @@ -37,15 +60,35 @@ 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); } @@ -54,11 +97,21 @@ static void draw(const char* filename) */ 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]); } }