--- ray/src/px/ra_t8.c 1992/10/13 11:36:13 2.4 +++ ray/src/px/ra_t8.c 2018/03/20 18:45:04 2.15 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ra_t8.c,v 2.15 2018/03/20 18:45:04 greg Exp $"; #endif - /* * ra_t8.c - program to convert between RADIANCE and * Targa 8-bit color-mapped images. @@ -12,56 +9,55 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include +#include +#include +#include "platform.h" +#include "rtio.h" +#include "rtmisc.h" #include "color.h" - #include "resolu.h" - +#include "clrtab.h" #include "targa.h" -#ifdef MSDOS -#include -#endif -#include - -#ifndef BSD -#define bcopy(s,d,n) (void)memcpy(d,s,n) -extern char *memcpy(); -#endif - #define goodpic(h) (my_imType(h) && my_mapType(h)) #define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ && (h)->dataBits==8 && (h)->imType==0) #define my_mapType(h) ((h)->mapType==CM_HASMAP && \ ((h)->CMapBits==24 || (h)->CMapBits==32)) -#define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y) +#define taralloc(h) (uby8 *)emalloc((h)->x*(h)->y) -extern BYTE clrtab[][3]; - -extern char *ecalloc(), *emalloc(); - -extern long ftell(); - +uby8 clrtab[256][3]; +extern int samplefac; double gamv = 2.2; /* gamv correction */ - int bradj = 0; /* brightness adjustment */ - char *progname; - char errmsg[128]; - COLR *inl; - -BYTE *tarData; - +uby8 *tarData; int xmax, ymax; +static int getint2(FILE *fp); +static void putint2(int i, FILE *fp); +static void quiterr(char *err); +static int getthead(struct hdStruct *hp, char *ip, FILE *fp); +static int putthead(struct hdStruct *hp, char *ip, FILE *fp); +static int getrhead(struct hdStruct *h, FILE *fp); +static void tg2ra(struct hdStruct *hp); +static void getmapped(int nc, int dith); +static void getgrey(int nc); +static void writetarga(struct hdStruct *h, uby8 *d, FILE *fp); +static void readtarga(struct hdStruct *h, uby8 *data, FILE *fp); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { struct hdStruct head; int dither = 1; @@ -69,13 +65,11 @@ char *argv[]; int ncolors = 256; int greyscale = 0; int i; -#ifdef MSDOS - extern int _fmode; - _fmode = O_BINARY; - setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); -#endif + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); progname = argv[0]; + samplefac = 0; for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -100,6 +94,9 @@ char *argv[]; case 'c': ncolors = atoi(argv[++i]); break; + case 'n': + samplefac = atoi(argv[++i]); + break; default: goto userr; } @@ -125,6 +122,7 @@ char *argv[]; xmax = head.x; ymax = head.y; /* put header */ + newheader("RADIANCE", stdout); printargs(i, argv, stdout); fputformat(COLRFMT, stdout); putchar('\n'); @@ -147,7 +145,7 @@ char *argv[]; quiterr(NULL); userr: fprintf(stderr, - "Usage: %s [-d][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", + "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", progname); fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n", progname); @@ -155,9 +153,10 @@ userr: } -int -getint2(fp) /* get a 2-byte positive integer */ -register FILE *fp; +static int +getint2( /* get a 2-byte positive integer */ + register FILE *fp +) { register int b1, b2; @@ -168,17 +167,21 @@ register FILE *fp; } -putint2(i, fp) /* put a 2-byte positive integer */ -register int i; -register FILE *fp; +static void +putint2( /* put a 2-byte positive integer */ + register int i, + register FILE *fp +) { putc(i&0xff, fp); putc(i>>8&0xff, fp); } -quiterr(err) /* print message and exit */ -char *err; +static void +quiterr( /* print message and exit */ + char *err +) { if (err != NULL) { fprintf(stderr, "%s: %s\n", progname, err); @@ -188,6 +191,7 @@ char *err; } +void eputs(s) char *s; { @@ -195,6 +199,7 @@ char *s; } +void quit(code) int code; { @@ -202,10 +207,12 @@ int code; } -getthead(hp, ip, fp) /* read header from input */ -struct hdStruct *hp; -char *ip; -register FILE *fp; +static int +getthead( /* read header from input */ + struct hdStruct *hp, + char *ip, + register FILE *fp +) { int nidbytes; @@ -235,10 +242,12 @@ register FILE *fp; } -putthead(hp, ip, fp) /* write header to output */ -struct hdStruct *hp; -char *ip; -register FILE *fp; +static int +putthead( /* write header to output */ + struct hdStruct *hp, + char *ip, + register FILE *fp +) { if (ip != NULL) putc(strlen(ip), fp); @@ -263,9 +272,11 @@ register FILE *fp; } -getrhead(h, fp) /* load RADIANCE input file header */ -register struct hdStruct *h; -FILE *fp; +static int +getrhead( /* load RADIANCE input file header */ + register struct hdStruct *h, + FILE *fp +) { /* get header info. */ if (checkheader(fp, COLRFMT, NULL) < 0 || @@ -293,12 +304,14 @@ FILE *fp; } -tg2ra(hp) /* targa file to RADIANCE file */ -struct hdStruct *hp; +static void +tg2ra( /* targa file to RADIANCE file */ + struct hdStruct *hp +) { union { - BYTE c3[256][3]; - BYTE c4[256][4]; + uby8 c3[256][3]; + uby8 c4[256][4]; } map; COLR ctab[256]; COLR *scanline; @@ -337,51 +350,68 @@ struct hdStruct *hp; if (fwritecolrs(scanline, xmax, stdout) < 0) quiterr("error writing RADIANCE file"); } - free((char *)scanline); - free((char *)tarData); + free((void *)scanline); + free((void *)tarData); } -getmapped(nc, dith) /* read in and quantize image */ -int nc; /* number of colors to use */ -int dith; /* use dithering? */ +static void +getmapped( /* read in and quantize image */ + int nc, /* number of colors to use */ + int dith /* use dithering? */ +) { long fpos; register int y; setcolrgam(gamv); fpos = ftell(stdin); - new_histo(); /* build histogram */ + if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1) + quiterr("cannot initialized histogram"); for (y = ymax-1; y >= 0; y--) { if (freadcolrs(inl, xmax, stdin) < 0) quiterr("error reading Radiance input"); if (bradj) shiftcolrs(inl, xmax, bradj); colrs_gambs(inl, xmax); - cnt_colrs(inl, xmax); + if (samplefac) + neu_colrs(inl, xmax); + else + cnt_colrs(inl, xmax); } if (fseek(stdin, fpos, 0) == EOF) quiterr("Radiance input must be from a file"); - new_clrtab(nc); /* map colors */ + if (samplefac) /* map colors */ + neu_clrtab(nc); + else + new_clrtab(nc); for (y = ymax-1; y >= 0; y--) { if (freadcolrs(inl, xmax, stdin) < 0) quiterr("error reading Radiance input"); if (bradj) shiftcolrs(inl, xmax, bradj); colrs_gambs(inl, xmax); - if (dith) - dith_colrs(tarData+y*xmax, inl, xmax); + if (samplefac) + if (dith) + neu_dith_colrs(tarData+y*xmax, inl, xmax); + else + neu_map_colrs(tarData+y*xmax, inl, xmax); else - map_colrs(tarData+y*xmax, inl, xmax); + if (dith) + dith_colrs(tarData+y*xmax, inl, xmax); + else + map_colrs(tarData+y*xmax, inl, xmax); } } -getgrey(nc) /* read in and convert to greyscale image */ -int nc; /* number of colors to use */ +static void +getgrey( /* read in and convert to greyscale image */ + int nc /* number of colors to use */ +) { int y; - register BYTE *dp; + register uby8 *dp; register int x; setcolrgam(gamv); @@ -391,25 +421,30 @@ int nc; /* number of colors to use */ quiterr("error reading Radiance input"); if (bradj) shiftcolrs(inl, xmax, bradj); + x = xmax; + while (x--) + inl[x][GRN] = normbright(inl[x]); colrs_gambs(inl, xmax); x = xmax; if (nc < 256) while (x--) - *--dp = ((long)normbright(inl[x])*nc+128)>>8; + *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8; else while (x--) - *--dp = normbright(inl[x]); + *--dp = inl[x][GRN]; } for (x = 0; x < nc; x++) clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = ((long)x*256+nc/2)/nc; + clrtab[x][BLU] = ((long)x*256+128)/nc; } -writetarga(h, d, fp) /* write out targa data */ -struct hdStruct *h; -BYTE *d; -FILE *fp; +static void +writetarga( /* write out targa data */ + struct hdStruct *h, + uby8 *d, + FILE *fp +) { register int i, j; @@ -417,7 +452,7 @@ FILE *fp; for (j = 2; j >= 0; j--) putc(clrtab[i][j], fp); if (h->dataType == IM_CMAP) { /* uncompressed */ - if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y) + if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y) quiterr("error writing targa file"); return; } @@ -425,16 +460,18 @@ FILE *fp; } -readtarga(h, data, fp) /* read in targa data */ -struct hdStruct *h; -BYTE *data; -FILE *fp; +static void +readtarga( /* read in targa data */ + struct hdStruct *h, + uby8 *data, + FILE *fp +) { register int cnt, c; - register BYTE *dp; + register uby8 *dp; if (h->dataType == IM_CMAP) { /* uncompressed */ - if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y) + if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y) goto readerr; return; }