Back to index

radiance  4R0+20100331
ttyimage.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ttyimage.c,v 2.5 2004/03/28 20:33:14 schorsch Exp $";
00003 #endif
00004 /*
00005  *  ttyimage.c - program to dump pixel file to dumb terminal.
00006  *
00007  *     8/15/85
00008  */
00009 
00010 #include  <stdio.h>
00011 #include  <time.h>
00012 
00013 #include  "platform.h"
00014 #include  "color.h"
00015 #include  "resolu.h"
00016 
00017 
00018 #define        NCOLS        133
00019 
00020 static int shade(COLR clr);
00021 
00022 
00023 int
00024 main(int  argc, char  **argv)
00025 {
00026        FILE  *input;
00027        int  xres, yres;
00028        COLR  scanline[NCOLS];
00029        register int  i, j;
00030        
00031        if (argc < 2)
00032               input = stdin;
00033        else if ((input = fopen(argv[1], "r")) == NULL) {
00034               fprintf(stderr, "%s: can't open file \"%s\"\n", argv[0], argv[1]);
00035               exit(1);
00036        }
00037        SET_FILE_BINARY(input);
00038                             /* get picture dimensions */
00039        if (checkheader(input, COLRFMT, NULL) < 0 ||
00040                      fgetresolu(&xres, &yres, input) < 0) {
00041               fprintf(stderr, "%s: bad picture format\n", argv[0]);
00042               exit(1);
00043        }
00044        if (xres > NCOLS) {
00045               fprintf(stderr, "%s: resolution mismatch\n", argv[0]);
00046               exit(1);
00047        }
00048        
00049        for (i = 0; i < yres; i++) {
00050               if (freadcolrs(scanline, xres, input) < 0) {
00051                      fprintf(stderr, "%s: read error\n", argv[0]);
00052                      exit(1);
00053               }
00054               normcolrs(scanline, xres, 0);
00055               for (j = 0; j < xres; j++)
00056                      putchar(shade(scanline[j]));
00057               putchar('\n');
00058        }
00059 
00060        exit(0);
00061 }
00062 
00063 
00064 static int
00065 shade(               /* return character for color */
00066        COLR  clr
00067 )
00068 {
00069 #define NSHADES       13
00070 
00071        static char  shadech[NSHADES+1] = " .,:;+?%&*$@#";
00072 
00073        return(shadech[normbright(clr)*NSHADES/256]);
00074 
00075 #undef NSHADES
00076 }