| 5 | 
  | 
 *  program to convert from RADIANCE RLE to flat format | 
| 6 | 
  | 
 */ | 
| 7 | 
  | 
 | 
| 8 | 
– | 
#include  <stdio.h> | 
| 8 | 
  | 
#include  <math.h> | 
| 9 | 
< | 
#include  <time.h> | 
| 9 | 
> | 
 | 
| 10 | 
> | 
#include  "platform.h" | 
| 11 | 
> | 
#include  "rtio.h" | 
| 12 | 
  | 
#include  "color.h" | 
| 13 | 
  | 
#include  "resolu.h" | 
| 14 | 
  | 
 | 
| 15 | 
< | 
#ifdef MSDOS | 
| 15 | 
< | 
#include  <fcntl.h> | 
| 16 | 
< | 
#endif | 
| 15 | 
> | 
#define dumpheader(fp)  putbinary(headlines, 1, headlen, fp) | 
| 16 | 
  | 
 | 
| 18 | 
– | 
extern int      addhline(); | 
| 19 | 
– | 
 | 
| 20 | 
– | 
#define dumpheader(fp)  fwrite(headlines, 1, headlen, fp) | 
| 21 | 
– | 
 | 
| 17 | 
  | 
int  bradj = 0;                         /* brightness adjustment */ | 
| 23 | 
– | 
 | 
| 18 | 
  | 
int  doflat = 1;                        /* produce flat file */ | 
| 25 | 
– | 
 | 
| 19 | 
  | 
int  force = 0;                         /* force file overwrite? */ | 
| 27 | 
– | 
 | 
| 20 | 
  | 
int  findframe = 0;                     /* find a specific frame? */ | 
| 29 | 
– | 
 | 
| 21 | 
  | 
int  frameno = 0;                       /* current frame number */ | 
| 22 | 
  | 
int  fmterr = 0;                        /* got input format error */ | 
| 23 | 
< | 
char  *headlines;                       /* current header info. */ | 
| 24 | 
< | 
int  headlen;                           /* current header length */ | 
| 23 | 
> | 
char  *headlines = NULL;                /* current header info. */ | 
| 24 | 
> | 
int  headlen1 = 0;                      /* length of initial frame header */ | 
| 25 | 
> | 
int  headlen = 0;                       /* current header length */ | 
| 26 | 
> | 
char  fmt[MAXFMTLEN];                   /* input format */ | 
| 27 | 
  | 
 | 
| 28 | 
  | 
char  *progname; | 
| 29 | 
  | 
 | 
| 30 | 
+ | 
static gethfunc addhline; | 
| 31 | 
+ | 
static int transfer(char *ospec); | 
| 32 | 
+ | 
static int loadheader(FILE *fp); | 
| 33 | 
  | 
 | 
| 34 | 
< | 
main(argc, argv) | 
| 35 | 
< | 
int  argc; | 
| 36 | 
< | 
char  *argv[]; | 
| 34 | 
> | 
 | 
| 35 | 
> | 
int | 
| 36 | 
> | 
main(int  argc, char  *argv[]) | 
| 37 | 
  | 
{ | 
| 38 | 
  | 
        char    *ospec; | 
| 39 | 
  | 
        int  i; | 
| 73 | 
  | 
                                progname, argv[i]); | 
| 74 | 
  | 
                exit(1); | 
| 75 | 
  | 
        } | 
| 76 | 
< | 
#ifdef MSDOS | 
| 81 | 
< | 
        setmode(fileno(stdin), O_BINARY); | 
| 82 | 
< | 
#endif | 
| 76 | 
> | 
        SET_FILE_BINARY(stdin); | 
| 77 | 
  | 
        ospec = i==argc-2 ? argv[i+1] : (char *)NULL; | 
| 78 | 
  | 
        while (transfer(ospec)) | 
| 79 | 
  | 
                ; | 
| 86 | 
  | 
} | 
| 87 | 
  | 
 | 
| 88 | 
  | 
 | 
| 89 | 
< | 
transfer(ospec)                 /* transfer a Radiance picture */ | 
| 90 | 
< | 
char    *ospec; | 
| 89 | 
> | 
static int | 
| 90 | 
> | 
transfer(                       /* transfer a Radiance picture */ | 
| 91 | 
> | 
        char    *ospec | 
| 92 | 
> | 
) | 
| 93 | 
  | 
{ | 
| 94 | 
< | 
        char    oname[128]; | 
| 94 | 
> | 
        char    oname[PATH_MAX]; | 
| 95 | 
  | 
        FILE    *fp; | 
| 96 | 
  | 
        int     order; | 
| 97 | 
  | 
        int     xmax, ymax; | 
| 108 | 
  | 
        if (findframe && findframe < frameno) | 
| 109 | 
  | 
                return(0); | 
| 110 | 
  | 
                                        /* allocate scanline */ | 
| 111 | 
< | 
        scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); | 
| 111 | 
> | 
        scanin = (COLR *)malloc(xmax*sizeof(COLR)); | 
| 112 | 
  | 
        if (scanin == NULL) { | 
| 113 | 
  | 
                perror(progname); | 
| 114 | 
  | 
                exit(1); | 
| 115 | 
  | 
        } | 
| 116 | 
  | 
                                        /* skip frame? */ | 
| 117 | 
  | 
        if (findframe > frameno) { | 
| 118 | 
< | 
                for (y = ymax; y--; ) | 
| 118 | 
> | 
                if (NCSAMP > 3) { | 
| 119 | 
> | 
                        if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) { | 
| 120 | 
> | 
                                perror(progname); | 
| 121 | 
> | 
                                exit(1); | 
| 122 | 
> | 
                        } | 
| 123 | 
> | 
                } else | 
| 124 | 
> | 
                    for (y = ymax; y--; ) | 
| 125 | 
  | 
                        if (freadcolrs(scanin, xmax, stdin) < 0) { | 
| 126 | 
  | 
                                fprintf(stderr, | 
| 127 | 
  | 
                                        "%s: error reading input picture\n", | 
| 128 | 
  | 
                                                progname); | 
| 129 | 
  | 
                                exit(1); | 
| 130 | 
  | 
                        } | 
| 131 | 
+ | 
                free(scanin); | 
| 132 | 
  | 
                return(1); | 
| 133 | 
  | 
        } | 
| 134 | 
  | 
                                        /* open output file/command */ | 
| 157 | 
  | 
                        } | 
| 158 | 
  | 
                } | 
| 159 | 
  | 
        } | 
| 160 | 
< | 
#ifdef MSDOS | 
| 161 | 
< | 
        setmode(fileno(fp), O_BINARY); | 
| 162 | 
< | 
#endif | 
| 160 | 
< | 
        dumpheader(fp);                 /* put out header */ | 
| 160 | 
> | 
        SET_FILE_BINARY(fp); | 
| 161 | 
> | 
        newheader("RADIANCE", fp);              /* put out header */ | 
| 162 | 
> | 
        dumpheader(fp); | 
| 163 | 
  | 
        fputs(progname, fp); | 
| 164 | 
  | 
        if (bradj) | 
| 165 | 
  | 
                fprintf(fp, " -e %+d", bradj); | 
| 168 | 
  | 
        fputc('\n', fp); | 
| 169 | 
  | 
        if (bradj) | 
| 170 | 
  | 
                fputexpos(pow(2.0, (double)bradj), fp); | 
| 171 | 
+ | 
        if (frameno) | 
| 172 | 
+ | 
                fprintf(fp, "FRAME=%d\n", frameno); | 
| 173 | 
+ | 
        if (fmt[0]) | 
| 174 | 
+ | 
                fputformat(fmt, fp); | 
| 175 | 
  | 
        fputc('\n', fp); | 
| 176 | 
  | 
        fputresolu(order, xmax, ymax, fp); | 
| 177 | 
  | 
                                        /* transfer picture */ | 
| 178 | 
  | 
        for (y = ymax; y--; ) { | 
| 179 | 
< | 
                if (freadcolrs(scanin, xmax, stdin) < 0) { | 
| 179 | 
> | 
                if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) { | 
| 180 | 
  | 
                        fprintf(stderr, "%s: error reading input picture\n", | 
| 181 | 
  | 
                                        progname); | 
| 182 | 
  | 
                        exit(1); | 
| 183 | 
  | 
                } | 
| 184 | 
  | 
                if (bradj) | 
| 185 | 
  | 
                        shiftcolrs(scanin, xmax, bradj); | 
| 186 | 
< | 
                if (doflat) | 
| 187 | 
< | 
                        fwrite((char *)scanin, sizeof(COLR), xmax, fp); | 
| 188 | 
< | 
                else | 
| 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 | 
< | 
                } | 
| 186 | 
> | 
                if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : | 
| 187 | 
> | 
                                (fwritecolrs(scanin, xmax, fp) < 0)) | 
| 188 | 
> | 
                        goto writerr; | 
| 189 | 
  | 
        } | 
| 190 | 
< | 
                                        /* clean up */ | 
| 191 | 
< | 
        if (oname[0] == '!') | 
| 192 | 
< | 
                pclose(fp); | 
| 193 | 
< | 
        else if (ospec != NULL) | 
| 190 | 
> | 
        free(scanin);                   /* clean up */ | 
| 191 | 
> | 
        if (fflush(fp) == EOF) | 
| 192 | 
> | 
                goto writerr; | 
| 193 | 
> | 
        if (oname[0] == '!') { | 
| 194 | 
> | 
                if (pclose(fp) != 0) | 
| 195 | 
> | 
                        fprintf(stderr, "%s: warning - bad status from \"%s\"\n", | 
| 196 | 
> | 
                                        progname, oname); | 
| 197 | 
> | 
        } else if (ospec != NULL) | 
| 198 | 
  | 
                fclose(fp); | 
| 199 | 
  | 
        return(1); | 
| 200 | 
+ | 
writerr: | 
| 201 | 
+ | 
        fprintf(stderr, "%s: error writing output to \"%s\"\n", | 
| 202 | 
+ | 
                        progname, oname); | 
| 203 | 
+ | 
        exit(1); | 
| 204 | 
  | 
} | 
| 205 | 
  | 
 | 
| 206 | 
  | 
 | 
| 207 | 
< | 
int | 
| 208 | 
< | 
addhline(s)                     /* add a line to our info. header */ | 
| 209 | 
< | 
char    *s; | 
| 207 | 
> | 
static int | 
| 208 | 
> | 
addhline(                       /* add a line to our info. header */ | 
| 209 | 
> | 
        char    *s, | 
| 210 | 
> | 
        void    *p | 
| 211 | 
> | 
) | 
| 212 | 
  | 
{ | 
| 203 | 
– | 
        char    fmt[32]; | 
| 213 | 
  | 
        int     n; | 
| 214 | 
  | 
 | 
| 215 | 
< | 
        if (formatval(fmt, s)) | 
| 216 | 
< | 
                fmterr += !globmatch(PICFMT, fmt); | 
| 217 | 
< | 
        else if (!strncmp(s, "FRAME=", 6)) | 
| 215 | 
> | 
        if (isheadid(s)) | 
| 216 | 
> | 
                return(0); | 
| 217 | 
> | 
        if (!strncmp(s, "FRAME=", 6)) { | 
| 218 | 
  | 
                frameno = atoi(s+6); | 
| 219 | 
+ | 
                return(0); | 
| 220 | 
+ | 
        } | 
| 221 | 
+ | 
        if (formatval(fmt, s)) { | 
| 222 | 
+ | 
                if (!strcmp(fmt, SPECFMT)) | 
| 223 | 
+ | 
                        strcpy(fmt, COLRFMT); | 
| 224 | 
+ | 
                else | 
| 225 | 
+ | 
                        fmterr += !globmatch(PICFMT, fmt); | 
| 226 | 
+ | 
                return(0); | 
| 227 | 
+ | 
        } | 
| 228 | 
+ | 
        if (isncomp(s)) { | 
| 229 | 
+ | 
                NCSAMP = ncompval(s); | 
| 230 | 
+ | 
                return(NCSAMP - 3); | 
| 231 | 
+ | 
        } | 
| 232 | 
+ | 
        if (iswlsplit(s)) { | 
| 233 | 
+ | 
                wlsplitval(WLPART, s); | 
| 234 | 
+ | 
                return(0); | 
| 235 | 
+ | 
        } | 
| 236 | 
  | 
        n = strlen(s); | 
| 237 | 
  | 
        if (headlen) | 
| 238 | 
< | 
                headlines = (char *)realloc(headlines, headlen+n+1); | 
| 238 | 
> | 
                headlines = (char *)realloc((void *)headlines, headlen+n+1); | 
| 239 | 
  | 
        else | 
| 240 | 
  | 
                headlines = (char *)malloc(n+1); | 
| 241 | 
  | 
        if (headlines == NULL) { | 
| 248 | 
  | 
} | 
| 249 | 
  | 
 | 
| 250 | 
  | 
 | 
| 251 | 
< | 
loadheader(fp)                  /* load an info. header into memory */ | 
| 252 | 
< | 
FILE    *fp; | 
| 251 | 
> | 
static int | 
| 252 | 
> | 
loadheader(                     /* load an info. header into memory */ | 
| 253 | 
> | 
        FILE    *fp | 
| 254 | 
> | 
) | 
| 255 | 
  | 
{ | 
| 256 | 
  | 
        fmterr = 0; frameno = 0; | 
| 257 | 
< | 
        if (headlen) {                  /* free old header */ | 
| 258 | 
< | 
                free(headlines); | 
| 259 | 
< | 
                headlen = 0; | 
| 260 | 
< | 
        } | 
| 257 | 
> | 
                                /* revert to initial header length */ | 
| 258 | 
> | 
        if (!headlen1) headlen1 = headlen; | 
| 259 | 
> | 
        else headlen = headlen1; | 
| 260 | 
> | 
 | 
| 261 | 
  | 
        if (getheader(fp, addhline, NULL) < 0) | 
| 262 | 
  | 
                return(0); | 
| 263 | 
  | 
        if (fmterr) |