| 1 | 
– | 
/* Copyright (c) 1992 Regents of the University of California */ | 
| 2 | 
– | 
 | 
| 1 | 
  | 
#ifndef lint | 
| 2 | 
< | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 2 | 
> | 
static const char       RCSid[] = "$Id$"; | 
| 3 | 
  | 
#endif | 
| 6 | 
– | 
 | 
| 4 | 
  | 
/* | 
| 5 | 
  | 
 *  program to convert from RADIANCE RLE to flat format | 
| 6 | 
  | 
 */ | 
| 7 | 
  | 
 | 
| 8 | 
  | 
#include  <stdio.h> | 
| 9 | 
< | 
 | 
| 9 | 
> | 
#include  <math.h> | 
| 10 | 
> | 
#include  <time.h> | 
| 11 | 
  | 
#include  "color.h" | 
| 12 | 
  | 
#include  "resolu.h" | 
| 13 | 
  | 
 | 
| 14 | 
+ | 
#ifdef MSDOS | 
| 15 | 
+ | 
#include  <fcntl.h> | 
| 16 | 
+ | 
#endif | 
| 17 | 
+ | 
 | 
| 18 | 
+ | 
extern int      addhline(); | 
| 19 | 
+ | 
 | 
| 20 | 
+ | 
#define dumpheader(fp)  fwrite(headlines, 1, headlen, fp) | 
| 21 | 
+ | 
 | 
| 22 | 
  | 
int  bradj = 0;                         /* brightness adjustment */ | 
| 23 | 
  | 
 | 
| 24 | 
  | 
int  doflat = 1;                        /* produce flat file */ | 
| 25 | 
  | 
 | 
| 26 | 
+ | 
int  force = 0;                         /* force file overwrite? */ | 
| 27 | 
+ | 
 | 
| 28 | 
+ | 
int  findframe = 0;                     /* find a specific frame? */ | 
| 29 | 
+ | 
 | 
| 30 | 
+ | 
int  frameno = 0;                       /* current frame number */ | 
| 31 | 
+ | 
int  fmterr = 0;                        /* got input format error */ | 
| 32 | 
+ | 
char  *headlines;                       /* current header info. */ | 
| 33 | 
+ | 
int  headlen;                           /* current header length */ | 
| 34 | 
+ | 
 | 
| 35 | 
  | 
char  *progname; | 
| 36 | 
  | 
 | 
| 37 | 
  | 
 | 
| 39 | 
  | 
int  argc; | 
| 40 | 
  | 
char  *argv[]; | 
| 41 | 
  | 
{ | 
| 42 | 
+ | 
        char    *ospec; | 
| 43 | 
  | 
        int  i; | 
| 44 | 
< | 
         | 
| 44 | 
> | 
 | 
| 45 | 
  | 
        progname = argv[0]; | 
| 46 | 
  | 
 | 
| 47 | 
  | 
        for (i = 1; i < argc; i++) | 
| 55 | 
  | 
                                        goto userr; | 
| 56 | 
  | 
                                bradj = atoi(argv[++i]); | 
| 57 | 
  | 
                                break; | 
| 58 | 
+ | 
                        case 'n': | 
| 59 | 
+ | 
                                findframe = atoi(argv[++i]); | 
| 60 | 
+ | 
                                break; | 
| 61 | 
+ | 
                        case 'f': | 
| 62 | 
+ | 
                                force++; | 
| 63 | 
+ | 
                                break; | 
| 64 | 
+ | 
                        case '\0': | 
| 65 | 
+ | 
                                goto gotfile; | 
| 66 | 
  | 
                        default: | 
| 67 | 
  | 
                                goto userr; | 
| 68 | 
  | 
                        } | 
| 69 | 
  | 
                else | 
| 70 | 
  | 
                        break; | 
| 71 | 
< | 
 | 
| 71 | 
> | 
gotfile: | 
| 72 | 
  | 
        if (i < argc-2) | 
| 73 | 
  | 
                goto userr; | 
| 74 | 
< | 
        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { | 
| 74 | 
> | 
        if (i <= argc-1 && strcmp(argv[i], "-") && | 
| 75 | 
> | 
                        freopen(argv[i], "r", stdin) == NULL) { | 
| 76 | 
  | 
                fprintf(stderr, "%s: can't open input \"%s\"\n", | 
| 77 | 
  | 
                                progname, argv[i]); | 
| 78 | 
  | 
                exit(1); | 
| 79 | 
  | 
        } | 
| 80 | 
< | 
        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { | 
| 81 | 
< | 
                fprintf(stderr, "can't open output \"%s\"\n", | 
| 82 | 
< | 
                                progname, argv[i+1]); | 
| 83 | 
< | 
                exit(1); | 
| 84 | 
< | 
        } | 
| 85 | 
< | 
        transfer(); | 
| 80 | 
> | 
#ifdef MSDOS | 
| 81 | 
> | 
        setmode(fileno(stdin), O_BINARY); | 
| 82 | 
> | 
#endif | 
| 83 | 
> | 
        ospec = i==argc-2 ? argv[i+1] : (char *)NULL; | 
| 84 | 
> | 
        while (transfer(ospec)) | 
| 85 | 
> | 
                ; | 
| 86 | 
  | 
        exit(0); | 
| 87 | 
  | 
userr: | 
| 88 | 
< | 
        fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n", | 
| 88 | 
> | 
        fprintf(stderr, | 
| 89 | 
> | 
        "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", | 
| 90 | 
  | 
                        progname); | 
| 91 | 
  | 
        exit(1); | 
| 92 | 
  | 
} | 
| 93 | 
  | 
 | 
| 94 | 
  | 
 | 
| 95 | 
< | 
quiterr(err)            /* print message and exit */ | 
| 96 | 
< | 
char  *err; | 
| 95 | 
> | 
transfer(ospec)                 /* transfer a Radiance picture */ | 
| 96 | 
> | 
char    *ospec; | 
| 97 | 
  | 
{ | 
| 98 | 
< | 
        if (err != NULL) { | 
| 99 | 
< | 
                fprintf(stderr, "%s: %s\n", progname, err); | 
| 74 | 
< | 
                exit(1); | 
| 75 | 
< | 
        } | 
| 76 | 
< | 
        exit(0); | 
| 77 | 
< | 
} | 
| 78 | 
< | 
 | 
| 79 | 
< | 
 | 
| 80 | 
< | 
transfer()              /* transfer Radiance picture */ | 
| 81 | 
< | 
{ | 
| 82 | 
< | 
        extern double   pow(); | 
| 98 | 
> | 
        char    oname[128]; | 
| 99 | 
> | 
        FILE    *fp; | 
| 100 | 
  | 
        int     order; | 
| 101 | 
< | 
        int     xmax, ymax; | 
| 101 | 
> | 
        int     xmax, ymax; | 
| 102 | 
  | 
        COLR    *scanin; | 
| 86 | 
– | 
        register int    x; | 
| 103 | 
  | 
        int     y; | 
| 104 | 
< | 
                                /* get header info. */ | 
| 105 | 
< | 
        if (checkheader(stdin, COLRFMT, stdout) < 0 || | 
| 106 | 
< | 
                        (order = fgetresolu(&xmax, &ymax, stdin)) < 0) | 
| 107 | 
< | 
                quiterr("bad picture format"); | 
| 108 | 
< | 
        fputs(progname, stdout); | 
| 109 | 
< | 
        if (bradj) | 
| 94 | 
< | 
                printf(" -e %+d", bradj); | 
| 95 | 
< | 
        if (doflat) | 
| 96 | 
< | 
                fputs("\n", stdout); | 
| 97 | 
< | 
        else { | 
| 98 | 
< | 
                fputs(" -r\n", stdout); | 
| 99 | 
< | 
                fputformat(COLRFMT, stdout); | 
| 104 | 
> | 
                                        /* get header info. */ | 
| 105 | 
> | 
        if (!(y = loadheader(stdin))) | 
| 106 | 
> | 
                return(0); | 
| 107 | 
> | 
        if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { | 
| 108 | 
> | 
                fprintf(stderr, "%s: bad input format\n", progname); | 
| 109 | 
> | 
                exit(1); | 
| 110 | 
  | 
        } | 
| 111 | 
+ | 
                                        /* did we pass the target frame? */ | 
| 112 | 
+ | 
        if (findframe && findframe < frameno) | 
| 113 | 
+ | 
                return(0); | 
| 114 | 
+ | 
                                        /* allocate scanline */ | 
| 115 | 
+ | 
        scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); | 
| 116 | 
+ | 
        if (scanin == NULL) { | 
| 117 | 
+ | 
                perror(progname); | 
| 118 | 
+ | 
                exit(1); | 
| 119 | 
+ | 
        } | 
| 120 | 
+ | 
                                        /* skip frame? */ | 
| 121 | 
+ | 
        if (findframe > frameno) { | 
| 122 | 
+ | 
                for (y = ymax; y--; ) | 
| 123 | 
+ | 
                        if (freadcolrs(scanin, xmax, stdin) < 0) { | 
| 124 | 
+ | 
                                fprintf(stderr, | 
| 125 | 
+ | 
                                        "%s: error reading input picture\n", | 
| 126 | 
+ | 
                                                progname); | 
| 127 | 
+ | 
                                exit(1); | 
| 128 | 
+ | 
                        } | 
| 129 | 
+ | 
                return(1); | 
| 130 | 
+ | 
        } | 
| 131 | 
+ | 
                                        /* open output file/command */ | 
| 132 | 
+ | 
        if (ospec == NULL) { | 
| 133 | 
+ | 
                strcpy(oname, "<stdout>"); | 
| 134 | 
+ | 
                fp = stdout; | 
| 135 | 
+ | 
        } else { | 
| 136 | 
+ | 
                sprintf(oname, ospec, frameno); | 
| 137 | 
+ | 
                if (oname[0] == '!') { | 
| 138 | 
+ | 
                        if ((fp = popen(oname+1, "w")) == NULL) { | 
| 139 | 
+ | 
                                fprintf(stderr, "%s: cannot start \"%s\"\n", | 
| 140 | 
+ | 
                                                progname, oname); | 
| 141 | 
+ | 
                                exit(1); | 
| 142 | 
+ | 
                        } | 
| 143 | 
+ | 
                } else { | 
| 144 | 
+ | 
                        if (!force && access(oname, 0) >= 0) { | 
| 145 | 
+ | 
                                fprintf(stderr, | 
| 146 | 
+ | 
                                        "%s: output file \"%s\" exists\n", | 
| 147 | 
+ | 
                                                progname, oname); | 
| 148 | 
+ | 
                                exit(1); | 
| 149 | 
+ | 
                        } | 
| 150 | 
+ | 
                        if ((fp = fopen(oname, "w")) == NULL) { | 
| 151 | 
+ | 
                                fprintf(stderr, "%s: ", progname); | 
| 152 | 
+ | 
                                perror(oname); | 
| 153 | 
+ | 
                                exit(1); | 
| 154 | 
+ | 
                        } | 
| 155 | 
+ | 
                } | 
| 156 | 
+ | 
        } | 
| 157 | 
+ | 
#ifdef MSDOS | 
| 158 | 
+ | 
        setmode(fileno(fp), O_BINARY); | 
| 159 | 
+ | 
#endif | 
| 160 | 
+ | 
        dumpheader(fp);                 /* put out header */ | 
| 161 | 
+ | 
        fputs(progname, fp); | 
| 162 | 
  | 
        if (bradj) | 
| 163 | 
< | 
                fputexpos(pow(2.0, (double)bradj), stdout); | 
| 164 | 
< | 
        fputs("\n", stdout); | 
| 165 | 
< | 
        fputresolu(order, xmax, ymax, stdout); | 
| 166 | 
< | 
                                                /* allocate scanline */ | 
| 167 | 
< | 
        scanin = (COLR *)malloc(xmax*sizeof(COLR)); | 
| 168 | 
< | 
        if (scanin == NULL) | 
| 169 | 
< | 
                quiterr("out of memory in transfer"); | 
| 170 | 
< | 
                                                /* convert image */ | 
| 171 | 
< | 
        for (y = ymax-1; y >= 0; y--) { | 
| 172 | 
< | 
                if (freadcolrs(scanin, xmax, stdin) < 0) | 
| 173 | 
< | 
                        quiterr("error reading input picture"); | 
| 163 | 
> | 
                fprintf(fp, " -e %+d", bradj); | 
| 164 | 
> | 
        if (!doflat) | 
| 165 | 
> | 
                fputs(" -r", fp); | 
| 166 | 
> | 
        fputc('\n', fp); | 
| 167 | 
> | 
        if (bradj) | 
| 168 | 
> | 
                fputexpos(pow(2.0, (double)bradj), fp); | 
| 169 | 
> | 
        fputc('\n', fp); | 
| 170 | 
> | 
        fputresolu(order, xmax, ymax, fp); | 
| 171 | 
> | 
                                        /* transfer picture */ | 
| 172 | 
> | 
        for (y = ymax; y--; ) { | 
| 173 | 
> | 
                if (freadcolrs(scanin, xmax, stdin) < 0) { | 
| 174 | 
> | 
                        fprintf(stderr, "%s: error reading input picture\n", | 
| 175 | 
> | 
                                        progname); | 
| 176 | 
> | 
                        exit(1); | 
| 177 | 
> | 
                } | 
| 178 | 
  | 
                if (bradj) | 
| 179 | 
  | 
                        shiftcolrs(scanin, xmax, bradj); | 
| 180 | 
  | 
                if (doflat) | 
| 181 | 
< | 
                        fwrite((char *)scanin, sizeof(COLR), xmax, stdout); | 
| 181 | 
> | 
                        fwrite((char *)scanin, sizeof(COLR), xmax, fp); | 
| 182 | 
  | 
                else | 
| 183 | 
< | 
                        fwritecolrs(scanin, xmax, stdout); | 
| 184 | 
< | 
                if (ferror(stdout)) | 
| 185 | 
< | 
                        quiterr("error writing rasterfile"); | 
| 183 | 
> | 
                        fwritecolrs(scanin, xmax, fp); | 
| 184 | 
> | 
                if (ferror(fp)) { | 
| 185 | 
> | 
                        fprintf(stderr, "%s: error writing output to \"%s\"\n", | 
| 186 | 
> | 
                                        progname, oname); | 
| 187 | 
> | 
                        exit(1); | 
| 188 | 
> | 
                } | 
| 189 | 
  | 
        } | 
| 190 | 
< | 
                                                /* free scanline */ | 
| 191 | 
< | 
        free((char *)scanin); | 
| 190 | 
> | 
                                        /* clean up */ | 
| 191 | 
> | 
        if (oname[0] == '!') | 
| 192 | 
> | 
                pclose(fp); | 
| 193 | 
> | 
        else if (ospec != NULL) | 
| 194 | 
> | 
                fclose(fp); | 
| 195 | 
> | 
        return(1); | 
| 196 | 
> | 
} | 
| 197 | 
> | 
 | 
| 198 | 
> | 
 | 
| 199 | 
> | 
int | 
| 200 | 
> | 
addhline(s)                     /* add a line to our info. header */ | 
| 201 | 
> | 
char    *s; | 
| 202 | 
> | 
{ | 
| 203 | 
> | 
        char    fmt[32]; | 
| 204 | 
> | 
        int     n; | 
| 205 | 
> | 
 | 
| 206 | 
> | 
        if (formatval(fmt, s)) | 
| 207 | 
> | 
                fmterr += !globmatch(PICFMT, fmt); | 
| 208 | 
> | 
        else if (!strncmp(s, "FRAME=", 6)) | 
| 209 | 
> | 
                frameno = atoi(s+6); | 
| 210 | 
> | 
        n = strlen(s); | 
| 211 | 
> | 
        if (headlen) | 
| 212 | 
> | 
                headlines = (char *)realloc(headlines, headlen+n+1); | 
| 213 | 
> | 
        else | 
| 214 | 
> | 
                headlines = (char *)malloc(n+1); | 
| 215 | 
> | 
        if (headlines == NULL) { | 
| 216 | 
> | 
                perror(progname); | 
| 217 | 
> | 
                exit(1); | 
| 218 | 
> | 
        } | 
| 219 | 
> | 
        strcpy(headlines+headlen, s); | 
| 220 | 
> | 
        headlen += n; | 
| 221 | 
> | 
        return(0); | 
| 222 | 
> | 
} | 
| 223 | 
> | 
 | 
| 224 | 
> | 
 | 
| 225 | 
> | 
loadheader(fp)                  /* load an info. header into memory */ | 
| 226 | 
> | 
FILE    *fp; | 
| 227 | 
> | 
{ | 
| 228 | 
> | 
        fmterr = 0; frameno = 0; | 
| 229 | 
> | 
        if (headlen) {                  /* free old header */ | 
| 230 | 
> | 
                free(headlines); | 
| 231 | 
> | 
                headlen = 0; | 
| 232 | 
> | 
        } | 
| 233 | 
> | 
        if (getheader(fp, addhline, NULL) < 0) | 
| 234 | 
> | 
                return(0); | 
| 235 | 
> | 
        if (fmterr) | 
| 236 | 
> | 
                return(-1); | 
| 237 | 
> | 
        return(1); | 
| 238 | 
  | 
} |