Back to index

radiance  4R0+20100331
meta2bmp.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: meta2bmp.c,v 1.2 2005/09/19 12:48:09 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Program to convert meta-files to BMP 8-bit color-mapped format
00006  */
00007 
00008 #include  "copyright.h"
00009 
00010 #include  "rtprocess.h"
00011 #include  "meta.h"
00012 #include  "plot.h"
00013 #include  "rast.h"
00014 #include  "bmpfile.h"
00015 #include  "targa.h"
00016 
00017 #define  MAXALLOC  30000
00018 #define  DXSIZE  400        /* default x resolution */
00019 #define  DYSIZE  400        /* default y resolution */
00020 #define  XCOM  "pexpand +vOCImsp -DP %s | psort +y"
00021 
00022 
00023 char  *progname;
00024 
00025 SCANBLOCK     outblock;
00026 
00027 int  dxsize = DXSIZE, dysize = DYSIZE;
00028 
00029 int  maxalloc = MAXALLOC;
00030 
00031 int  ydown = 0;
00032 
00033 static char  outname[64];
00034 static char  *outtack = NULL;
00035 
00036 static BMPWriter  *bmpw = NULL;
00037 
00038 static int  lineno = 0;
00039 
00040 static short  condonly = FALSE,
00041              conditioned = FALSE;
00042 
00043 
00044 
00045 
00046 char *
00047 findtack(s)                 /* find place to tack on suffix */
00048 register char *s;
00049 {
00050        while (*s && *s != '.')
00051               s++;
00052        return(s);
00053 }
00054 
00055 
00056 int
00057 main(
00058        int  argc,
00059        char  **argv
00060 )
00061 
00062 {
00063  FILE  *fp;
00064  char  comargs[200], command[300];
00065 
00066  progname = *argv++;
00067  argc--;
00068 
00069  condonly = FALSE;
00070  conditioned = FALSE;
00071  
00072  while (argc && **argv == '-')  {
00073     switch (*(*argv+1))  {
00074        case 'c':
00075          condonly = TRUE;
00076          break;
00077        case 'r':
00078          conditioned = TRUE;
00079          break;
00080        case 'm':
00081          minwidth = atoi(*++argv);
00082          argc--;
00083          break;
00084        case 'x':
00085          dxsize = atoi(*++argv);
00086          argc--;
00087          break;
00088        case 'y':
00089          dysize = atoi(*++argv);
00090          argc--;
00091          break;
00092        case 'o':
00093          strcpy(outname, *++argv);
00094          outtack = findtack(outname);
00095          argc--;
00096          break;
00097        default:
00098          error(WARNING, "unknown option");
00099          break;
00100        }
00101     argv++;
00102     argc--;
00103     }
00104 
00105  if (conditioned) {
00106     if (argc)
00107        while (argc)  {
00108          fp = efopen(*argv, "r");
00109          plot(fp);
00110          fclose(fp);
00111          argv++;
00112          argc--;
00113          }
00114     else
00115        plot(stdin);
00116     if (lineno)
00117        nextpage();
00118  } else  {
00119     comargs[0] = '\0';
00120     while (argc)  {
00121        strcat(comargs, " ");
00122        strcat(comargs, *argv);
00123        argv++;
00124        argc--;
00125        }
00126     sprintf(command, XCOM, comargs);
00127     if (condonly)
00128        return(system(command));
00129     else  {
00130        if ((fp = popen(command, "r")) == NULL)
00131           error(SYSTEM, "cannot execute input filter");
00132        plot(fp);
00133        pclose(fp);
00134        if (lineno)
00135          nextpage();
00136        }
00137     }
00138 
00139  return(0);
00140  }
00141 
00142 
00143 
00144 void
00145 thispage(void)              /* rewind current file */
00146 {
00147     if (lineno)
00148        error(USER, "cannot restart page in thispage");
00149 }
00150 
00151 
00152 void
00153 initfile(void)              /* initialize this file */
00154 {
00155     /*
00156     static unsigned char  cmap[24] = {255,255,255, 255,152,0, 0,188,0, 0,0,255,
00157                      179,179,0, 255,0,255, 0,200,200, 0,0,0};
00158      */
00159     static const unsigned char  cmap[8][3] = {{0,0,0}, {0,0,255}, {0,188,0},
00160               {255,152,0}, {0,200,200}, {255,0,255}, {179,179,0}, {255,255,255}};
00161     static int  filenum = 0;
00162     BMPHeader  *hp;
00163     register int  i;
00164 
00165     hp = BMPmappedHeader(dxsize, dysize, 0, 256);
00166     hp->compr = BI_RLE8;
00167     if (hp == NULL)
00168        error(USER, "Illegal image parameter(s)");
00169     for (i = 0; i < 8; i++) {
00170        hp->palette[i].r = cmap[i][2];
00171        hp->palette[i].g = cmap[i][1];
00172        hp->palette[i].b = cmap[i][0];
00173     }
00174     hp->impColors = 8;
00175     if (outtack != NULL) {
00176        sprintf(outtack, "%d.bmp", ++filenum);
00177        bmpw = BMPopenOutputFile(outname, hp);
00178     } else {
00179        bmpw = BMPopenOutputStream(stdout, hp);
00180     }
00181     if (bmpw == NULL)
00182        error(USER, "Cannot create output");
00183 }
00184 
00185 
00186 
00187 void
00188 nextpage(void)              /* advance to next page */
00189 
00190 {
00191 
00192     if (lineno == 0)
00193        return;
00194     if (bmpw != NULL) {
00195        while (lineno < dysize) {
00196            nextblock();
00197            outputblock();
00198        }
00199        BMPcloseOutput(bmpw);
00200        bmpw = NULL;
00201     }
00202     lineno = 0;
00203 
00204 }
00205 
00206 
00207 
00208 #define MINRUN       4
00209 
00210 extern void
00211 printblock(void)            /* output scanline block to file */
00212 
00213 {
00214     int  i;
00215     register unsigned char  *scanline;
00216 
00217     if (lineno == 0)
00218        initfile();
00219     for (i = outblock.ybot; i <= outblock.ytop && i < dysize; i++) {
00220        scanline = outblock.cols[i-outblock.ybot] + outblock.xleft;
00221        memcpy((void *)bmpw->scanline, (void *)scanline,
00222                      sizeof(uint8)*(outblock.xright-outblock.xleft+1));
00223        if (BMPwriteScanline(bmpw) != BIR_OK)
00224               error(SYSTEM, "Error writing BMP file");
00225        lineno++;
00226     }
00227     
00228 }