Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

make_image.C

Go to the documentation of this file.
00001 /*
00002  *  MAKE_IMAGE:  Convert the input stream into a Sun rasterfile color image.
00003  *               Assume that the numbers are *already* in the range [0,255].
00004  *
00005  *               Use the ImageMagick "convert" program to convert the
00006  *               image to other formats (e.g. gif, etc.)
00007  *
00008  *               For a compiled version for use with C or Fortran
00009  *               programs, see write_image.
00010  */
00011 
00012 #include "stdinc.h"
00013 
00014 // From make_header.C:
00015 
00016 void make_header(int m, int n, FILE* output_file, char* colormap_file);
00017 
00018 /* Convenient shorthand: */
00019 
00020 #define PAD_ROW if (pad_row && (++nchar)%m_orig == 0) putchar((unsigned char)0)
00021 #define USAGE \
00022 "Usage:  make_image  [-c]  [-f]  [-i]  [-m color-map]  [-s sizex [sizey]]\n"
00023 
00024 main(int argc, char** argv)
00025 {
00026     /* Variables that may be set from the command line: */
00027 
00028     int m = 0, n = 0;           /* Image dimensions (no default) */
00029     int which = 2;              /* 0 ==> char, 1 ==> int, 2 ==> float [def] */
00030     char* colormap = NULL;      /* Use standard built-in colormap by default */
00031 
00032     int pad_row = 0, pad_col = 0;
00033     int nchar = 0;
00034 
00035     int m_orig;
00036     int i;
00037     float f;
00038 
00039     /* Parse the argument list. */
00040 
00041     for (i = 0; i < argc; i++)
00042         if (argv[i][0] == '-') {
00043             switch (argv[i][1]) {
00044                 case 'c':       which = 0;
00045                                 break;
00046                 case 'f':       which = 2;
00047                                 break;
00048                 case 'i':       which = 1;
00049                                 break;
00050                 case 'm':       colormap = argv[++i];
00051                                 break;
00052                 case 's':       m = atoi(argv[++i]);
00053                                 if (i+1 < argc && argv[i+1][0] != '-')
00054                                     n = atoi(argv[++i]);
00055                                 else
00056                                     n = m;
00057                                 break;
00058 
00059                 default:        fprintf(stderr, USAGE);
00060                                 exit(1);
00061 
00062             }
00063         }
00064 
00065     if (m <= 0 || n <= 0) {
00066         fprintf(stderr, "Must specify image dimensions.\n");
00067         exit(1);
00068     }
00069 
00070     /* NOTE that a peculiarity of the Sun rasterfile format is
00071        that the numbers of rows and columns both must be even... */
00072 
00073     if (m%2 != 0 || n%2 != 0) {
00074         m_orig = m;
00075         if (m%2 != 0) {
00076             pad_row = 1;
00077             m++;
00078         }
00079         if (n%2 != 0) {
00080             pad_col = 1;
00081             n++;
00082         }
00083         fprintf(stderr, 
00084                 "Image dimensions must be even. Padding data to %dx%d\n",
00085                 m, n);
00086     }
00087 
00088     make_header(m, n, stdout, colormap);
00089 
00090     if (which == 0)
00091         while ((i = getchar()) != EOF) {
00092             putchar((unsigned char)i);
00093             PAD_ROW;
00094         }
00095     else if (which == 1)
00096         while (scanf("%d", &i) != EOF) {
00097             putchar((unsigned char)i);
00098             PAD_ROW;
00099         }
00100     else
00101         while (scanf("%f", &f) != EOF) {
00102             putchar((unsigned char)f);
00103             PAD_ROW;
00104         }
00105 
00106     if (pad_col)
00107         for (i = 0; i < m; i++) putchar((unsigned char)0);
00108 }

Generated at Sun Feb 24 09:57:08 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001