| 1 | 
– | 
/* Copyright (c) 1991 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 | 
  | 
 *  ra_t16.c - program to convert between RADIANCE and | 
| 6 | 
  | 
 *              Targa 16, 24 and 32-bit images. | 
| 8 | 
  | 
 *      11/2/88         Adapted from ra_t8.c | 
| 9 | 
  | 
 */ | 
| 10 | 
  | 
 | 
| 14 | 
– | 
#include  <stdio.h> | 
| 15 | 
– | 
 | 
| 16 | 
– | 
#ifdef MSDOS | 
| 17 | 
– | 
#include  <fcntl.h> | 
| 18 | 
– | 
#endif | 
| 19 | 
– | 
 | 
| 11 | 
  | 
#include  <math.h> | 
| 12 | 
< | 
 | 
| 12 | 
> | 
#include  "platform.h" | 
| 13 | 
> | 
#include  "rtio.h" | 
| 14 | 
> | 
#include  "rtmisc.h" | 
| 15 | 
  | 
#include  "color.h" | 
| 23 | 
– | 
 | 
| 16 | 
  | 
#include  "resolu.h" | 
| 25 | 
– | 
 | 
| 17 | 
  | 
#include  "random.h" | 
| 27 | 
– | 
 | 
| 18 | 
  | 
#include  "targa.h" | 
| 19 | 
  | 
 | 
| 20 | 
  | 
#define  goodpic(h)     (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ | 
| 22 | 
  | 
 | 
| 23 | 
  | 
#define  taralloc(h)    (unsigned char *)emalloc((h)->x*(h)->y*(h)->dataBits/8) | 
| 24 | 
  | 
 | 
| 25 | 
< | 
#define  readtarga(h,d,f)       ((h)->dataBits==16 ? readt16(h,d,f) : \ | 
| 26 | 
< | 
                                        readt24(h,d,f)) | 
| 25 | 
> | 
#define  readtarga(h,d,f)       ((h)->dataBits==16 ? \ | 
| 26 | 
> | 
                        readt16(h,(unsigned short*)d,f) : readt24(h,d,f)) | 
| 27 | 
  | 
 | 
| 28 | 
< | 
#define  writetarga(h,d,f)      ((h)->dataBits==16 ? writet16(h,d,f) : \ | 
| 29 | 
< | 
                                        writet24(h,d,f)) | 
| 28 | 
> | 
#define  writetarga(h,d,f)      ((h)->dataBits==16 ? \ | 
| 29 | 
> | 
                        writet16(h,(unsigned short*)d,f) : writet24(h,d,f)) | 
| 30 | 
  | 
 | 
| 31 | 
< | 
extern char     *ecalloc(), *emalloc(); | 
| 42 | 
< | 
 | 
| 43 | 
< | 
double  gamma = 2.2;                    /* gamma correction */ | 
| 44 | 
< | 
 | 
| 31 | 
> | 
double  gamcor = 2.2;                   /* gamma correction */ | 
| 32 | 
  | 
int  bradj = 0;                         /* brightness adjustment */ | 
| 46 | 
– | 
 | 
| 47 | 
– | 
char  *progname; | 
| 48 | 
– | 
 | 
| 33 | 
  | 
char  msg[128]; | 
| 34 | 
  | 
 | 
| 35 | 
+ | 
static int getint2(FILE *fp); | 
| 36 | 
+ | 
static void putint2(int i, FILE *fp); | 
| 37 | 
+ | 
static void quiterr(char *err); | 
| 38 | 
+ | 
static int getthead(struct hdStruct *hp, char *ip, FILE *fp); | 
| 39 | 
+ | 
static int putthead(struct hdStruct *hp, char *ip, FILE *fp); | 
| 40 | 
+ | 
static void tg2ra(struct hdStruct *hp); | 
| 41 | 
+ | 
static void ra2tg(struct hdStruct *hp); | 
| 42 | 
+ | 
static void writet24(struct hdStruct *h, unsigned char *d, FILE *fp); | 
| 43 | 
+ | 
static void writet16(struct hdStruct *h, unsigned short *d, FILE *fp); | 
| 44 | 
+ | 
static void readt24(struct hdStruct *h, unsigned char *data, FILE *fp); | 
| 45 | 
+ | 
static void readt16(struct hdStruct *h, unsigned short *data, FILE *fp); | 
| 46 | 
  | 
 | 
| 47 | 
< | 
main(argc, argv) | 
| 48 | 
< | 
int  argc; | 
| 49 | 
< | 
char  *argv[]; | 
| 47 | 
> | 
 | 
| 48 | 
> | 
int | 
| 49 | 
> | 
main(int  argc, char  *argv[]) | 
| 50 | 
  | 
{ | 
| 51 | 
  | 
        struct hdStruct  head; | 
| 52 | 
  | 
        int  reverse = 0; | 
| 53 | 
  | 
        int  i; | 
| 54 | 
< | 
#ifdef MSDOS | 
| 55 | 
< | 
        extern int  _fmode; | 
| 56 | 
< | 
        _fmode = O_BINARY; | 
| 57 | 
< | 
        setmode(fileno(stdin), O_BINARY); | 
| 58 | 
< | 
        setmode(fileno(stdout), O_BINARY); | 
| 64 | 
< | 
#endif | 
| 65 | 
< | 
        progname = argv[0]; | 
| 54 | 
> | 
         | 
| 55 | 
> | 
        SET_DEFAULT_BINARY(); | 
| 56 | 
> | 
        SET_FILE_BINARY(stdin); | 
| 57 | 
> | 
        SET_FILE_BINARY(stdout); | 
| 58 | 
> | 
        fixargv0(argv[0]); | 
| 59 | 
  | 
 | 
| 60 | 
  | 
        head.dataBits = 16; | 
| 61 | 
  | 
        for (i = 1; i < argc; i++) | 
| 62 | 
  | 
                if (argv[i][0] == '-') | 
| 63 | 
  | 
                        switch (argv[i][1]) { | 
| 64 | 
  | 
                        case 'g': | 
| 65 | 
< | 
                                gamma = atof(argv[++i]); | 
| 65 | 
> | 
                                gamcor = atof(argv[++i]); | 
| 66 | 
  | 
                                break; | 
| 67 | 
  | 
                        case 'r': | 
| 68 | 
  | 
                                reverse = !reverse; | 
| 97 | 
  | 
                quiterr(msg); | 
| 98 | 
  | 
        } | 
| 99 | 
  | 
                                        /* set gamma */ | 
| 100 | 
< | 
        setcolrgam(gamma); | 
| 100 | 
> | 
        setcolrgam(gamcor); | 
| 101 | 
  | 
                                        /* convert */ | 
| 102 | 
  | 
        if (reverse) { | 
| 103 | 
  | 
                                        /* get header */ | 
| 106 | 
  | 
                if (!goodpic(&head)) | 
| 107 | 
  | 
                        quiterr("incompatible format"); | 
| 108 | 
  | 
                                        /* put header */ | 
| 109 | 
+ | 
                newheader("RADIANCE", stdout); | 
| 110 | 
  | 
                printargs(i, argv, stdout); | 
| 111 | 
  | 
                fputformat(COLRFMT, stdout); | 
| 112 | 
  | 
                putchar('\n'); | 
| 137 | 
  | 
} | 
| 138 | 
  | 
 | 
| 139 | 
  | 
 | 
| 140 | 
< | 
int | 
| 141 | 
< | 
getint2(fp)                     /* get a 2-byte positive integer */ | 
| 142 | 
< | 
register FILE   *fp; | 
| 140 | 
> | 
static int | 
| 141 | 
> | 
getint2(                        /* get a 2-byte positive integer */ | 
| 142 | 
> | 
        register FILE   *fp | 
| 143 | 
> | 
) | 
| 144 | 
  | 
{ | 
| 145 | 
  | 
        register int  b1, b2; | 
| 146 | 
  | 
 | 
| 151 | 
  | 
} | 
| 152 | 
  | 
 | 
| 153 | 
  | 
 | 
| 154 | 
< | 
putint2(i, fp)                  /* put a 2-byte positive integer */ | 
| 155 | 
< | 
register int  i; | 
| 156 | 
< | 
register FILE   *fp; | 
| 154 | 
> | 
static void | 
| 155 | 
> | 
putint2(                        /* put a 2-byte positive integer */ | 
| 156 | 
> | 
        register int  i, | 
| 157 | 
> | 
        register FILE   *fp | 
| 158 | 
> | 
) | 
| 159 | 
  | 
{ | 
| 160 | 
  | 
        putc(i&0xff, fp); | 
| 161 | 
  | 
        putc(i>>8&0xff, fp); | 
| 162 | 
  | 
} | 
| 163 | 
  | 
 | 
| 164 | 
  | 
 | 
| 165 | 
< | 
quiterr(err)            /* print message and exit */ | 
| 166 | 
< | 
char  *err; | 
| 165 | 
> | 
static void | 
| 166 | 
> | 
quiterr(                /* print message and exit */ | 
| 167 | 
> | 
        char  *err | 
| 168 | 
> | 
) | 
| 169 | 
  | 
{ | 
| 170 | 
  | 
        fprintf(stderr, "%s: %s\n", progname, err); | 
| 171 | 
  | 
        exit(1); | 
| 172 | 
  | 
} | 
| 173 | 
  | 
 | 
| 174 | 
  | 
 | 
| 175 | 
< | 
eputs(s) | 
| 176 | 
< | 
char *s; | 
| 175 | 
> | 
void | 
| 176 | 
> | 
eputs( | 
| 177 | 
> | 
        char *s | 
| 178 | 
> | 
) | 
| 179 | 
  | 
{ | 
| 180 | 
  | 
        fputs(s, stderr); | 
| 181 | 
  | 
} | 
| 182 | 
  | 
 | 
| 183 | 
  | 
 | 
| 184 | 
< | 
quit(code) | 
| 185 | 
< | 
int code; | 
| 184 | 
> | 
void | 
| 185 | 
> | 
quit( | 
| 186 | 
> | 
        int code | 
| 187 | 
> | 
) | 
| 188 | 
  | 
{ | 
| 189 | 
  | 
        exit(code); | 
| 190 | 
  | 
} | 
| 191 | 
  | 
 | 
| 192 | 
  | 
 | 
| 193 | 
< | 
getthead(hp, ip, fp)            /* read header from input */ | 
| 194 | 
< | 
struct hdStruct  *hp; | 
| 195 | 
< | 
char  *ip; | 
| 196 | 
< | 
register FILE  *fp; | 
| 193 | 
> | 
static int | 
| 194 | 
> | 
getthead(               /* read header from input */ | 
| 195 | 
> | 
        struct hdStruct  *hp, | 
| 196 | 
> | 
        char  *ip, | 
| 197 | 
> | 
        register FILE  *fp | 
| 198 | 
> | 
) | 
| 199 | 
  | 
{ | 
| 200 | 
  | 
        int     nidbytes; | 
| 201 | 
  | 
 | 
| 225 | 
  | 
} | 
| 226 | 
  | 
 | 
| 227 | 
  | 
 | 
| 228 | 
< | 
putthead(hp, ip, fp)            /* write header to output */ | 
| 229 | 
< | 
struct hdStruct  *hp; | 
| 230 | 
< | 
char  *ip; | 
| 231 | 
< | 
register FILE  *fp; | 
| 228 | 
> | 
static int | 
| 229 | 
> | 
putthead(               /* write header to output */ | 
| 230 | 
> | 
        struct hdStruct  *hp, | 
| 231 | 
> | 
        char  *ip, | 
| 232 | 
> | 
        register FILE  *fp | 
| 233 | 
> | 
) | 
| 234 | 
  | 
{ | 
| 235 | 
  | 
        if (ip != NULL) | 
| 236 | 
  | 
                putc(strlen(ip), fp); | 
| 255 | 
  | 
} | 
| 256 | 
  | 
 | 
| 257 | 
  | 
 | 
| 258 | 
< | 
tg2ra(hp)                       /* targa file to RADIANCE file */ | 
| 259 | 
< | 
struct hdStruct  *hp; | 
| 258 | 
> | 
static void | 
| 259 | 
> | 
tg2ra(                  /* targa file to RADIANCE file */ | 
| 260 | 
> | 
        struct hdStruct  *hp | 
| 261 | 
> | 
) | 
| 262 | 
  | 
{ | 
| 263 | 
  | 
        COLR  *scanline; | 
| 264 | 
  | 
        unsigned char  *tarData; | 
| 299 | 
  | 
                if (fwritecolrs(scanline, hp->x, stdout) < 0) | 
| 300 | 
  | 
                        quiterr("error writing RADIANCE file"); | 
| 301 | 
  | 
        } | 
| 302 | 
< | 
        free((char *)scanline); | 
| 303 | 
< | 
        free((char *)tarData); | 
| 302 | 
> | 
        free((void *)scanline); | 
| 303 | 
> | 
        free((void *)tarData); | 
| 304 | 
  | 
} | 
| 305 | 
  | 
 | 
| 306 | 
  | 
 | 
| 307 | 
< | 
ra2tg(hp)                       /* convert radiance to targa file */ | 
| 308 | 
< | 
struct hdStruct  *hp; | 
| 307 | 
> | 
static void | 
| 308 | 
> | 
ra2tg(                  /* convert radiance to targa file */ | 
| 309 | 
> | 
        struct hdStruct  *hp | 
| 310 | 
> | 
) | 
| 311 | 
  | 
{ | 
| 312 | 
  | 
        register int    i, j; | 
| 313 | 
  | 
        unsigned char  *tarData; | 
| 350 | 
  | 
                                                /* write out targa data */ | 
| 351 | 
  | 
        writetarga(hp, tarData, stdout); | 
| 352 | 
  | 
 | 
| 353 | 
< | 
        free((char *)inl); | 
| 354 | 
< | 
        free((char *)tarData); | 
| 353 | 
> | 
        free((void *)inl); | 
| 354 | 
> | 
        free((void *)tarData); | 
| 355 | 
  | 
} | 
| 356 | 
  | 
 | 
| 357 | 
  | 
 | 
| 358 | 
< | 
writet24(h, d, fp)              /* write out 24-bit targa data */ | 
| 359 | 
< | 
struct hdStruct  *h; | 
| 360 | 
< | 
unsigned char  *d; | 
| 361 | 
< | 
FILE  *fp; | 
| 358 | 
> | 
static void | 
| 359 | 
> | 
writet24(               /* write out 24-bit targa data */ | 
| 360 | 
> | 
        struct hdStruct  *h, | 
| 361 | 
> | 
        unsigned char  *d, | 
| 362 | 
> | 
        FILE  *fp | 
| 363 | 
> | 
) | 
| 364 | 
  | 
{ | 
| 365 | 
  | 
        if (h->dataType == IM_RGB) {            /* uncompressed */ | 
| 366 | 
  | 
                if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) | 
| 371 | 
  | 
} | 
| 372 | 
  | 
 | 
| 373 | 
  | 
 | 
| 374 | 
< | 
writet16(h, d, fp)              /* write out 16-bit targa data */ | 
| 375 | 
< | 
struct hdStruct  *h; | 
| 376 | 
< | 
register unsigned short  *d; | 
| 377 | 
< | 
FILE  *fp; | 
| 374 | 
> | 
static void | 
| 375 | 
> | 
writet16(               /* write out 16-bit targa data */ | 
| 376 | 
> | 
        struct hdStruct  *h, | 
| 377 | 
> | 
        register unsigned short  *d, | 
| 378 | 
> | 
        FILE  *fp | 
| 379 | 
> | 
) | 
| 380 | 
  | 
{ | 
| 381 | 
  | 
        register int  cnt; | 
| 382 | 
  | 
 | 
| 391 | 
  | 
} | 
| 392 | 
  | 
 | 
| 393 | 
  | 
 | 
| 394 | 
< | 
readt24(h, data, fp)            /* read in 24-bit targa data */ | 
| 395 | 
< | 
register struct hdStruct  *h; | 
| 396 | 
< | 
unsigned char  *data; | 
| 397 | 
< | 
FILE  *fp; | 
| 394 | 
> | 
static void | 
| 395 | 
> | 
readt24(                /* read in 24-bit targa data */ | 
| 396 | 
> | 
        register struct hdStruct  *h, | 
| 397 | 
> | 
        unsigned char  *data, | 
| 398 | 
> | 
        FILE  *fp | 
| 399 | 
> | 
) | 
| 400 | 
  | 
{ | 
| 401 | 
  | 
        register int  cnt, c; | 
| 402 | 
  | 
        register unsigned char  *dp; | 
| 434 | 
  | 
} | 
| 435 | 
  | 
 | 
| 436 | 
  | 
 | 
| 437 | 
< | 
readt16(h, data, fp)            /* read in 16-bit targa data */ | 
| 438 | 
< | 
register struct hdStruct  *h; | 
| 439 | 
< | 
unsigned short  *data; | 
| 440 | 
< | 
FILE  *fp; | 
| 437 | 
> | 
static void | 
| 438 | 
> | 
readt16(                /* read in 16-bit targa data */ | 
| 439 | 
> | 
        register struct hdStruct  *h, | 
| 440 | 
> | 
        unsigned short  *data, | 
| 441 | 
> | 
        FILE  *fp | 
| 442 | 
> | 
) | 
| 443 | 
  | 
{ | 
| 444 | 
  | 
        register int  cnt, c; | 
| 445 | 
  | 
        register unsigned short  *dp; |