| 1 | 
greg | 
1.1 | 
#ifndef lint | 
| 2 | 
schorsch | 
1.2 | 
static const char RCSid[] = "$Id: meta2bmp.c,v 1.1 2005/05/04 23:43:19 greg Exp $"; | 
| 3 | 
greg | 
1.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 *  Program to convert meta-files to BMP 8-bit color-mapped format | 
| 6 | 
  | 
  | 
 */ | 
| 7 | 
  | 
  | 
 | 
| 8 | 
  | 
  | 
#include  "copyright.h" | 
| 9 | 
  | 
  | 
 | 
| 10 | 
  | 
  | 
#include  "rtprocess.h" | 
| 11 | 
  | 
  | 
#include  "meta.h" | 
| 12 | 
  | 
  | 
#include  "plot.h" | 
| 13 | 
  | 
  | 
#include  "rast.h" | 
| 14 | 
  | 
  | 
#include  "bmpfile.h" | 
| 15 | 
schorsch | 
1.2 | 
#include  "targa.h" | 
| 16 | 
greg | 
1.1 | 
 | 
| 17 | 
  | 
  | 
#define  MAXALLOC  30000 | 
| 18 | 
  | 
  | 
#define  DXSIZE  400            /* default x resolution */ | 
| 19 | 
  | 
  | 
#define  DYSIZE  400            /* default y resolution */ | 
| 20 | 
  | 
  | 
#define  XCOM  "pexpand +vOCImsp -DP %s | psort +y" | 
| 21 | 
  | 
  | 
 | 
| 22 | 
  | 
  | 
 | 
| 23 | 
  | 
  | 
char  *progname; | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
SCANBLOCK       outblock; | 
| 26 | 
  | 
  | 
 | 
| 27 | 
  | 
  | 
int  dxsize = DXSIZE, dysize = DYSIZE; | 
| 28 | 
  | 
  | 
 | 
| 29 | 
  | 
  | 
int  maxalloc = MAXALLOC; | 
| 30 | 
  | 
  | 
 | 
| 31 | 
  | 
  | 
int  ydown = 0; | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
static char  outname[64]; | 
| 34 | 
  | 
  | 
static char  *outtack = NULL; | 
| 35 | 
  | 
  | 
 | 
| 36 | 
  | 
  | 
static BMPWriter  *bmpw = NULL; | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
static int  lineno = 0; | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
static short  condonly = FALSE, | 
| 41 | 
  | 
  | 
              conditioned = FALSE; | 
| 42 | 
  | 
  | 
 | 
| 43 | 
  | 
  | 
 | 
| 44 | 
  | 
  | 
 | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
char * | 
| 47 | 
  | 
  | 
findtack(s)                     /* find place to tack on suffix */ | 
| 48 | 
  | 
  | 
register char *s; | 
| 49 | 
  | 
  | 
{ | 
| 50 | 
  | 
  | 
        while (*s && *s != '.') | 
| 51 | 
  | 
  | 
                s++; | 
| 52 | 
  | 
  | 
        return(s); | 
| 53 | 
  | 
  | 
} | 
| 54 | 
  | 
  | 
 | 
| 55 | 
  | 
  | 
 | 
| 56 | 
  | 
  | 
int | 
| 57 | 
  | 
  | 
main( | 
| 58 | 
  | 
  | 
        int  argc, | 
| 59 | 
  | 
  | 
        char  **argv | 
| 60 | 
  | 
  | 
) | 
| 61 | 
  | 
  | 
 | 
| 62 | 
  | 
  | 
{ | 
| 63 | 
  | 
  | 
 FILE  *fp; | 
| 64 | 
  | 
  | 
 char  comargs[200], command[300]; | 
| 65 | 
  | 
  | 
 | 
| 66 | 
  | 
  | 
 progname = *argv++; | 
| 67 | 
  | 
  | 
 argc--; | 
| 68 | 
  | 
  | 
 | 
| 69 | 
  | 
  | 
 condonly = FALSE; | 
| 70 | 
  | 
  | 
 conditioned = FALSE; | 
| 71 | 
  | 
  | 
  | 
| 72 | 
  | 
  | 
 while (argc && **argv == '-')  { | 
| 73 | 
  | 
  | 
    switch (*(*argv+1))  { | 
| 74 | 
  | 
  | 
       case 'c': | 
| 75 | 
  | 
  | 
          condonly = TRUE; | 
| 76 | 
  | 
  | 
          break; | 
| 77 | 
  | 
  | 
       case 'r': | 
| 78 | 
  | 
  | 
          conditioned = TRUE; | 
| 79 | 
  | 
  | 
          break; | 
| 80 | 
  | 
  | 
       case 'm': | 
| 81 | 
  | 
  | 
          minwidth = atoi(*++argv); | 
| 82 | 
  | 
  | 
          argc--; | 
| 83 | 
  | 
  | 
          break; | 
| 84 | 
  | 
  | 
       case 'x': | 
| 85 | 
  | 
  | 
          dxsize = atoi(*++argv); | 
| 86 | 
  | 
  | 
          argc--; | 
| 87 | 
  | 
  | 
          break; | 
| 88 | 
  | 
  | 
       case 'y': | 
| 89 | 
  | 
  | 
          dysize = atoi(*++argv); | 
| 90 | 
  | 
  | 
          argc--; | 
| 91 | 
  | 
  | 
          break; | 
| 92 | 
  | 
  | 
       case 'o': | 
| 93 | 
  | 
  | 
          strcpy(outname, *++argv); | 
| 94 | 
  | 
  | 
          outtack = findtack(outname); | 
| 95 | 
  | 
  | 
          argc--; | 
| 96 | 
  | 
  | 
          break; | 
| 97 | 
  | 
  | 
       default: | 
| 98 | 
  | 
  | 
          error(WARNING, "unknown option"); | 
| 99 | 
  | 
  | 
          break; | 
| 100 | 
  | 
  | 
       } | 
| 101 | 
  | 
  | 
    argv++; | 
| 102 | 
  | 
  | 
    argc--; | 
| 103 | 
  | 
  | 
    } | 
| 104 | 
  | 
  | 
 | 
| 105 | 
  | 
  | 
 if (conditioned) { | 
| 106 | 
  | 
  | 
    if (argc) | 
| 107 | 
  | 
  | 
       while (argc)  { | 
| 108 | 
  | 
  | 
          fp = efopen(*argv, "r"); | 
| 109 | 
  | 
  | 
          plot(fp); | 
| 110 | 
  | 
  | 
          fclose(fp); | 
| 111 | 
  | 
  | 
          argv++; | 
| 112 | 
  | 
  | 
          argc--; | 
| 113 | 
  | 
  | 
          } | 
| 114 | 
  | 
  | 
    else | 
| 115 | 
  | 
  | 
       plot(stdin); | 
| 116 | 
  | 
  | 
    if (lineno) | 
| 117 | 
  | 
  | 
        nextpage(); | 
| 118 | 
  | 
  | 
 } else  { | 
| 119 | 
  | 
  | 
    comargs[0] = '\0'; | 
| 120 | 
  | 
  | 
    while (argc)  { | 
| 121 | 
  | 
  | 
       strcat(comargs, " "); | 
| 122 | 
  | 
  | 
       strcat(comargs, *argv); | 
| 123 | 
  | 
  | 
       argv++; | 
| 124 | 
  | 
  | 
       argc--; | 
| 125 | 
  | 
  | 
       } | 
| 126 | 
  | 
  | 
    sprintf(command, XCOM, comargs); | 
| 127 | 
  | 
  | 
    if (condonly) | 
| 128 | 
  | 
  | 
       return(system(command)); | 
| 129 | 
  | 
  | 
    else  { | 
| 130 | 
  | 
  | 
       if ((fp = popen(command, "r")) == NULL) | 
| 131 | 
  | 
  | 
          error(SYSTEM, "cannot execute input filter"); | 
| 132 | 
  | 
  | 
       plot(fp); | 
| 133 | 
  | 
  | 
       pclose(fp); | 
| 134 | 
  | 
  | 
       if (lineno) | 
| 135 | 
  | 
  | 
          nextpage(); | 
| 136 | 
  | 
  | 
       } | 
| 137 | 
  | 
  | 
    } | 
| 138 | 
  | 
  | 
 | 
| 139 | 
  | 
  | 
 return(0); | 
| 140 | 
  | 
  | 
 } | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
 | 
| 143 | 
  | 
  | 
 | 
| 144 | 
  | 
  | 
void | 
| 145 | 
  | 
  | 
thispage(void)          /* rewind current file */ | 
| 146 | 
  | 
  | 
{ | 
| 147 | 
  | 
  | 
    if (lineno) | 
| 148 | 
  | 
  | 
        error(USER, "cannot restart page in thispage"); | 
| 149 | 
  | 
  | 
} | 
| 150 | 
  | 
  | 
 | 
| 151 | 
  | 
  | 
 | 
| 152 | 
  | 
  | 
void | 
| 153 | 
  | 
  | 
initfile(void)          /* initialize this file */ | 
| 154 | 
  | 
  | 
{ | 
| 155 | 
  | 
  | 
    /* | 
| 156 | 
  | 
  | 
    static unsigned char  cmap[24] = {255,255,255, 255,152,0, 0,188,0, 0,0,255, | 
| 157 | 
  | 
  | 
                        179,179,0, 255,0,255, 0,200,200, 0,0,0}; | 
| 158 | 
  | 
  | 
     */ | 
| 159 | 
schorsch | 
1.2 | 
    static const unsigned char  cmap[8][3] = {{0,0,0}, {0,0,255}, {0,188,0}, | 
| 160 | 
  | 
  | 
                {255,152,0}, {0,200,200}, {255,0,255}, {179,179,0}, {255,255,255}}; | 
| 161 | 
greg | 
1.1 | 
    static int  filenum = 0; | 
| 162 | 
  | 
  | 
    BMPHeader  *hp; | 
| 163 | 
  | 
  | 
    register int  i; | 
| 164 | 
  | 
  | 
 | 
| 165 | 
  | 
  | 
    hp = BMPmappedHeader(dxsize, dysize, 0, 256); | 
| 166 | 
  | 
  | 
    hp->compr = BI_RLE8; | 
| 167 | 
  | 
  | 
    if (hp == NULL) | 
| 168 | 
  | 
  | 
        error(USER, "Illegal image parameter(s)"); | 
| 169 | 
  | 
  | 
    for (i = 0; i < 8; i++) { | 
| 170 | 
  | 
  | 
        hp->palette[i].r = cmap[i][2]; | 
| 171 | 
  | 
  | 
        hp->palette[i].g = cmap[i][1]; | 
| 172 | 
  | 
  | 
        hp->palette[i].b = cmap[i][0]; | 
| 173 | 
  | 
  | 
    } | 
| 174 | 
  | 
  | 
    hp->impColors = 8; | 
| 175 | 
  | 
  | 
    if (outtack != NULL) { | 
| 176 | 
  | 
  | 
        sprintf(outtack, "%d.bmp", ++filenum); | 
| 177 | 
  | 
  | 
        bmpw = BMPopenOutputFile(outname, hp); | 
| 178 | 
  | 
  | 
    } else { | 
| 179 | 
  | 
  | 
        bmpw = BMPopenOutputStream(stdout, hp); | 
| 180 | 
  | 
  | 
    } | 
| 181 | 
  | 
  | 
    if (bmpw == NULL) | 
| 182 | 
  | 
  | 
        error(USER, "Cannot create output"); | 
| 183 | 
  | 
  | 
} | 
| 184 | 
  | 
  | 
 | 
| 185 | 
  | 
  | 
 | 
| 186 | 
  | 
  | 
 | 
| 187 | 
  | 
  | 
void | 
| 188 | 
  | 
  | 
nextpage(void)          /* advance to next page */ | 
| 189 | 
  | 
  | 
 | 
| 190 | 
  | 
  | 
{ | 
| 191 | 
  | 
  | 
 | 
| 192 | 
  | 
  | 
    if (lineno == 0) | 
| 193 | 
  | 
  | 
        return; | 
| 194 | 
  | 
  | 
    if (bmpw != NULL) { | 
| 195 | 
  | 
  | 
        while (lineno < dysize) { | 
| 196 | 
  | 
  | 
            nextblock(); | 
| 197 | 
  | 
  | 
            outputblock(); | 
| 198 | 
  | 
  | 
        } | 
| 199 | 
  | 
  | 
        BMPcloseOutput(bmpw); | 
| 200 | 
  | 
  | 
        bmpw = NULL; | 
| 201 | 
  | 
  | 
    } | 
| 202 | 
  | 
  | 
    lineno = 0; | 
| 203 | 
  | 
  | 
 | 
| 204 | 
  | 
  | 
} | 
| 205 | 
  | 
  | 
 | 
| 206 | 
  | 
  | 
 | 
| 207 | 
  | 
  | 
 | 
| 208 | 
  | 
  | 
#define MINRUN  4 | 
| 209 | 
  | 
  | 
 | 
| 210 | 
  | 
  | 
extern void | 
| 211 | 
  | 
  | 
printblock(void)                /* output scanline block to file */ | 
| 212 | 
  | 
  | 
 | 
| 213 | 
  | 
  | 
{ | 
| 214 | 
schorsch | 
1.2 | 
    int  i; | 
| 215 | 
greg | 
1.1 | 
    register unsigned char  *scanline; | 
| 216 | 
  | 
  | 
 | 
| 217 | 
  | 
  | 
    if (lineno == 0) | 
| 218 | 
  | 
  | 
        initfile(); | 
| 219 | 
  | 
  | 
    for (i = outblock.ybot; i <= outblock.ytop && i < dysize; i++) { | 
| 220 | 
  | 
  | 
        scanline = outblock.cols[i-outblock.ybot] + outblock.xleft; | 
| 221 | 
  | 
  | 
        memcpy((void *)bmpw->scanline, (void *)scanline, | 
| 222 | 
  | 
  | 
                        sizeof(uint8)*(outblock.xright-outblock.xleft+1)); | 
| 223 | 
  | 
  | 
        if (BMPwriteScanline(bmpw) != BIR_OK) | 
| 224 | 
  | 
  | 
                error(SYSTEM, "Error writing BMP file"); | 
| 225 | 
  | 
  | 
        lineno++; | 
| 226 | 
  | 
  | 
    } | 
| 227 | 
  | 
  | 
     | 
| 228 | 
  | 
  | 
} |