Back to index

radiance  4R0+20100331
Defines | Typedefs | Functions | Variables
ra_gif.c File Reference
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "platform.h"
#include "color.h"
#include "resolu.h"
#include "clrtab.h"
#include <ctype.h>

Go to the source code of this file.

Defines

#define MAXCOLORS   256
#define TRUE   1
#define FALSE   0
#define CBITS   12
#define HSIZE   5003 /* 80% occupancy */
#define ARGVAL()   (*++(*argv) || (--argc && *++argv))
#define MAXCODE(n_bits)   (((code_int) 1 << (n_bits)) - 1)
#define HashTabOf(i)   htab[i]
#define CodeTabOf(i)   codetab[i]
#define tab_prefixof(i)   CodeTabOf(i)
#define tab_suffixof(i)   ((char_type *)(htab))[i]
#define de_stack   ((char_type *)&tab_suffixof((code_int)1<<CBITS))

Typedefs

typedef int ifun_t (int x, int y)
typedef int code_int
typedef long int count_int
typedef unsigned char char_type

Functions

static void getrow (int y)
static void mkclrmap (int nc)
static void mkgrymap (int nc)
static void GIFEncode (FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int BitsPerPixel, int Red[], int Green[], int Blue[], ifun_t *GetPixel)
int main (int argc, char *argv[])
static int getgifpix (int x, int y)
static void BumpPixel (void)
static int GIFNextPixel (ifun_t *getpixel)
static void Putword (int w, FILE *fp)
static void compress (int init_bits, FILE *outfile, ifun_t *ReadValue)
static void output (code_int code)
static void cl_block (void)
static void cl_hash (count_int hsize)
static void writeerr (void)
static void char_init (void)
static void char_out (int c)
static void flush_char (void)
static void gammawarp (short *sbuf, float gam, int n)
static void cl_hash (register count_int hsize)

Variables

static const char RCSid [] = "$Id: ra_gif.c,v 2.11 2004/03/28 20:33:14 schorsch Exp $"
int rmap [MAXCOLORS]
int gmap [MAXCOLORS]
int bmap [MAXCOLORS]
int currow
long picstart
BYTE clrtab [256][3]
int samplefac
COLRscanln
BYTEpixscan
int xmax
int ymax
double gamv = 2.2
int greyscale = 0
int dither = 1
int bradj = 0
int ncolors = 0
char * progname
static ifun_t getgifpix
int Width
int Height
int curx
int cury
long CountDown
int Pass
int Interlace
unsigned long cur_accum = 0
int cur_bits = 0
int n_bits
int maxbits = CBITS
code_int maxcode
code_int maxmaxcode = (code_int)1 << CBITS
count_int htab [HSIZE]
unsigned short codetab [HSIZE]
code_int hsize = HSIZE
code_int free_ent = 0
int clear_flg = 0
int offset
long int in_count = 1
long int out_count = 0
int g_init_bits
FILE * g_outfile
int ClearCode
int EOFCode
unsigned long masks []
int a_count
char accum [256]
static float curgamma
static short gamtab [256]

Define Documentation

#define ARGVAL ( )    (*++(*argv) || (--argc && *++argv))

Definition at line 461 of file ra_gif.c.

#define CBITS   12

Definition at line 442 of file ra_gif.c.

#define CodeTabOf (   i)    codetab[i]

Definition at line 472 of file ra_gif.c.

#define de_stack   ((char_type *)&tab_suffixof((code_int)1<<CBITS))

Definition at line 486 of file ra_gif.c.

#define FALSE   0

Definition at line 255 of file ra_gif.c.

#define HashTabOf (   i)    htab[i]

Definition at line 471 of file ra_gif.c.

#define HSIZE   5003 /* 80% occupancy */

Definition at line 443 of file ra_gif.c.

#define MAXCODE (   n_bits)    (((code_int) 1 << (n_bits)) - 1)

Definition at line 467 of file ra_gif.c.

#define MAXCOLORS   256

Definition at line 18 of file ra_gif.c.

#define tab_prefixof (   i)    CodeTabOf(i)

Definition at line 484 of file ra_gif.c.

#define tab_suffixof (   i)    ((char_type *)(htab))[i]

Definition at line 485 of file ra_gif.c.

#define TRUE   1

Definition at line 254 of file ra_gif.c.


Typedef Documentation

typedef unsigned char char_type

Definition at line 274 of file ra_gif.c.

typedef int code_int

Definition at line 272 of file ra_gif.c.

typedef long int count_int

Definition at line 273 of file ra_gif.c.

typedef int ifun_t(int x, int y)

Definition at line 43 of file ra_gif.c.


Function Documentation

static void BumpPixel ( void  ) [static]

Definition at line 291 of file ra_gif.c.

{
    curx++;
    if( curx == Width ) {
       curx = 0;
       if( !Interlace ) {
           cury++;
       } else {
           switch( Pass ) {
              case 0:
                  cury += 8;
                  if( cury >= Height ) {
                     Pass++;
                     cury = 4;
                  } 
                  break;
              case 1:
                  cury += 8;
                      if( cury >= Height ) {
                         Pass++;
                         cury = 2;
                     }
                     break;
                     case 2:
                  cury += 4;
                  if( cury >= Height ) {
                     Pass++;
                     cury = 1;
                  }
                  break;
                     case 3:
                  cury += 2;
                  break;
           }
       }
    }
}

Here is the caller graph for this function:

static void char_init ( void  ) [static]

Definition at line 740 of file ra_gif.c.

{
        a_count = 0;
}

Here is the caller graph for this function:

static void char_out ( int  c) [static]

Definition at line 755 of file ra_gif.c.

{
        accum[ a_count++ ] = c;
        if( a_count >= 254 )
                flush_char();
}

Here is the call graph for this function:

static void cl_block ( void  ) [static]

Definition at line 677 of file ra_gif.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static void cl_hash ( count_int  hsize) [static]

Here is the caller graph for this function:

static void cl_hash ( register count_int  hsize) [static]

Definition at line 686 of file ra_gif.c.

{
        register count_int *htab_p = htab+hsize;
        register long i;
        register long m1 = -1;

        i = hsize - 16;
        do {                            /* might use Sys V memset(3) here */
                *(htab_p-16) = m1;
                *(htab_p-15) = m1;
                *(htab_p-14) = m1;
                *(htab_p-13) = m1;
                *(htab_p-12) = m1;
                *(htab_p-11) = m1;
                *(htab_p-10) = m1;
                *(htab_p-9) = m1;
                *(htab_p-8) = m1;
                *(htab_p-7) = m1;
                *(htab_p-6) = m1;
                *(htab_p-5) = m1;
                *(htab_p-4) = m1;
                *(htab_p-3) = m1;
                *(htab_p-2) = m1;
                *(htab_p-1) = m1;
                htab_p -= 16;
        } while ((i -= 16) >= 0);
        for ( i += 16; i > 0; i-- )
                *--htab_p = m1;
}
static void compress ( int  init_bits,
FILE *  outfile,
ifun_t ReadValue 
) [static]

Definition at line 521 of file ra_gif.c.

{
    register long fcode;
    register code_int i = 0;
    register int c;
    register code_int ent;
    register code_int disp;
    register code_int hsize_reg;
    register int hshift;

    /*
     * Set up the globals:  g_init_bits - initial number of bits
     *                      g_outfile   - pointer to output file
     */
    g_init_bits = init_bits;
    g_outfile = outfile;
    /*
     * Set up the necessary values
     */
    offset = 0;
    out_count = 0;
    clear_flg = 0;
    in_count = 1;
    maxcode = MAXCODE(n_bits = g_init_bits);
    ClearCode = (1 << (init_bits - 1));
    EOFCode = ClearCode + 1;
    free_ent = ClearCode + 2;
    char_init();
    ent = GIFNextPixel( ReadValue );
    hshift = 0;
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
        hshift++;
    hshift = 8 - hshift;                /* set hash code range bound */
    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg);            /* clear hash table */
    output( (code_int)ClearCode );
    while ( (c = GIFNextPixel( ReadValue )) != EOF ) {
        in_count++;
        fcode = (long) (((long) c << maxbits) + ent);
        /* i = (((code_int)c << hshift) ~ ent); */   /* xor hashing */
        i = (((code_int)c << hshift) ^ ent);    /* xor hashing */
        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        } else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
            goto nomatch;
        disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
        if ( i == 0 )
            disp = 1;
probe:
        if ( (i -= disp) < 0 )
            i += hsize_reg;
        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        }
        if ( (long)HashTabOf (i) > 0 )
            goto probe;
nomatch:
        output ( (code_int) ent );
        out_count++;
        ent = c;
        if ( free_ent < maxmaxcode ) {
            CodeTabOf (i) = free_ent++; /* code -> hashtable */
            HashTabOf (i) = fcode;
        } else
                cl_block();
    }
    /*
     * Put out the final code.
     */
    output( (code_int)ent );
    out_count++;
    output( (code_int) EOFCode );
    return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void flush_char ( void  ) [static]

Definition at line 768 of file ra_gif.c.

{
        if( a_count > 0 ) {
                fputc( a_count, g_outfile );
                fwrite( accum, 1, a_count, g_outfile );
                a_count = 0;
        }
}

Here is the caller graph for this function:

static void gammawarp ( short *  sbuf,
float  gam,
int  n 
) [static]

Definition at line 781 of file ra_gif.c.

{
    int i;

    if(gam!=curgamma) {
       for(i=0; i<256; i++) 
           gamtab[i] = 255*pow(i/255.0,gam)+0.5;
       curgamma = gam;
    }
    while(n--) {
       *sbuf = gamtab[*sbuf];
       sbuf++;
    }
}
static int getgifpix ( int  x,
int  y 
) [static]

Definition at line 223 of file ra_gif.c.

{
       getrow(y);
       if (greyscale)
              return((normbright(scanln[x])*ncolors)>>8);
       if (pixscan != NULL)
              return(pixscan[x]);
       return(samplefac ? neu_map_pixel(scanln[x]) : map_pixel(scanln[x]));
}

Here is the call graph for this function:

static void getrow ( int  y) [static]

Definition at line 144 of file ra_gif.c.

{
       if (y == currow)
              return;
       if (y < currow) {
              fseek(stdin, picstart, 0);
              currow = -1;
       }
       do
              if (freadcolrs(scanln, xmax, stdin) < 0) {
                     fprintf(stderr, "%s: error reading picture (y==%d)\n",
                                   progname, ymax-1-y);
                     exit(1);
              }
       while (++currow < y);
       if (bradj)
              shiftcolrs(scanln, xmax, bradj);
       colrs_gambs(scanln, xmax);
       if (pixscan != NULL) {
              if (samplefac)
                     neu_dith_colrs(pixscan, scanln, xmax);
              else
                     dith_colrs(pixscan, scanln, xmax);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void GIFEncode ( FILE *  fp,
int  GWidth,
int  GHeight,
int  GInterlace,
int  Background,
int  BitsPerPixel,
int  Red[],
int  Green[],
int  Blue[],
ifun_t GetPixel 
) [static]

Definition at line 351 of file ra_gif.c.

{
    int B;
    int RWidth, RHeight;
    int LeftOfs, TopOfs;
    int Resolution;
    int ColorMapSize;
    int InitCodeSize;
    int i;

    cur_accum = 0; /* globals */
    cur_bits = 0;

    Interlace = GInterlace;
    ColorMapSize = 1 << BitsPerPixel;
    RWidth = Width = GWidth;
    RHeight = Height = GHeight;
    LeftOfs = TopOfs = 0;
    Resolution = BitsPerPixel;

    CountDown = (long)Width * (long)Height;
    Pass = 0;
    if( BitsPerPixel <= 1 )
       InitCodeSize = 2;
    else
       InitCodeSize = BitsPerPixel;
    curx = cury = 0;
    fwrite( "GIF87a", 1, 6, fp );
    Putword( RWidth, fp );
    Putword( RHeight, fp );
    B = 0x80;       /* Yes, there is a color map */
    B |= (Resolution - 1) << 5;
    B |= (BitsPerPixel - 1);
    fputc( B, fp );
    fputc( Background, fp );
    fputc( 0, fp );
    for( i=0; i<ColorMapSize; i++ ) {
       fputc( Red[i], fp );
       fputc( Green[i], fp );
       fputc( Blue[i], fp );
    }
    fputc( ',', fp );
    Putword( LeftOfs, fp );
    Putword( TopOfs, fp );
    Putword( Width, fp );
    Putword( Height, fp );
    if( Interlace )
       fputc( 0x40, fp );
    else
       fputc( 0x00, fp );
    fputc( InitCodeSize, fp );
    compress( InitCodeSize+1, fp, GetPixel );
    fputc( 0, fp );
    fputc( ';', fp );
    fclose( fp );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int GIFNextPixel ( ifun_t getpixel) [static]

Definition at line 333 of file ra_gif.c.

{
    int r;

    if( CountDown == 0 )
       return EOF;
    CountDown--;
    r = (*getpixel)( curx, cury );
    BumpPixel();
    return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 56 of file ra_gif.c.

{
       int  bitsperpix;
       int  i;
       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] == '-')
                     switch (argv[i][1]) {
                     case 'g':
                            gamv = atof(argv[++i]);
                            break;
                     case 'b':
                            greyscale = 1;
                            break;
                     case 'd':
                            dither = !dither;
                            break;
                     case 'c':
                            ncolors = atoi(argv[++i]);
                            break;
                     case 'e':
                            if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
                                   goto userr;
                            bradj = atoi(argv[++i]);
                            break;
                     case 'n':
                            samplefac = atoi(argv[++i]);
                            break;
                     default:
                            goto userr;
                     }
              else
                     break;

       if (i < argc-2)
              goto userr;
       if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
              fprintf(stderr, "%s: cannot open input \"%s\"\n",
                            progname, argv[i]);
              exit(1);
       }
       if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
              fprintf(stderr, "%s: cannot open output \"%s\"\n",
                            progname, argv[i+1]);
              exit(1);
       }
       if (checkheader(stdin, COLRFMT, NULL) < 0 ||
                     fgetresolu(&xmax, &ymax, stdin) < 0) {
              fprintf(stderr, "%s: bad picture format\n", progname);
              exit(1);
       }
       picstart = ftell(stdin);
       currow = -1;
       scanln = (COLR *)malloc(xmax*sizeof(COLR));
       if (scanln == NULL) {
              fprintf(stderr, "%s: out of memory\n", progname);
              exit(1);
       }
                            /* set up gamma correction */
       setcolrgam(gamv);
                            /* figure out the bits per pixel */
       if ((ncolors < 2) | (ncolors > MAXCOLORS))
              ncolors = MAXCOLORS;
       for (bitsperpix = 1; ncolors > 1<<bitsperpix; bitsperpix++)
              ;
                            /* compute color map */
       if (greyscale)
              mkgrymap(ncolors);
       else
              mkclrmap(ncolors);
                            /* convert image */
       GIFEncode(stdout, xmax, ymax, 0, 0, bitsperpix,
                     rmap, gmap, bmap, getgifpix);
       exit(0);
userr:
       fprintf(stderr,
       "Usage: %s [-b][-d][-n samp][-c ncolors][-g gamv][-e +/-stops] input [output]\n",
                     progname);
       exit(1);
}

Here is the call graph for this function:

static void mkclrmap ( int  nc) [static]

Definition at line 174 of file ra_gif.c.

{
       register int  i;

       if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
              goto memerr;
       for (i = 0; i < ymax; i++) {
              getrow(i);
              if (samplefac)
                     neu_colrs(scanln, xmax);
              else
                     cnt_colrs(scanln, xmax);
       }
       if (samplefac)
              neu_clrtab(nc);
       else
              new_clrtab(nc);
       for (i = 0; i < nc; i++) {
              rmap[i] = clrtab[i][RED];
              gmap[i] = clrtab[i][GRN];
              bmap[i] = clrtab[i][BLU];
       }
       if (dither && (pixscan = (BYTE *)malloc(xmax)) == NULL)
              goto memerr;
       return;
memerr:
       fprintf(stderr, "%s: out of memory\n", progname);
       exit(1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void mkgrymap ( int  nc) [static]

Definition at line 208 of file ra_gif.c.

{
       register int  i;

       for (i = 0; i < nc; i++) {
              rmap[i] =
              gmap[i] =
              bmap[i] = ((unsigned)i<<8)/nc;
       }
}

Here is the caller graph for this function:

static void output ( code_int  code) [static]
static void Putword ( int  w,
FILE *  fp 
) [static]

Definition at line 423 of file ra_gif.c.

{
    fputc( w & 0xff, fp );
    fputc( (w/256) & 0xff, fp );
}

Here is the caller graph for this function:

static void writeerr ( void  ) [static]

Definition at line 719 of file ra_gif.c.

{
        printf( "error writing output file\n" );
        exit(1);
}

Variable Documentation

int a_count

Definition at line 734 of file ra_gif.c.

char accum[256]

Definition at line 748 of file ra_gif.c.

Definition at line 22 of file ra_gif.c.

int bradj = 0

Definition at line 38 of file ra_gif.c.

int clear_flg = 0

Definition at line 494 of file ra_gif.c.

int ClearCode

Definition at line 517 of file ra_gif.c.

BYTE clrtab[256][3]

Definition at line 26 of file ra_gif.c.

unsigned short codetab[HSIZE]

Definition at line 470 of file ra_gif.c.

long CountDown

Definition at line 259 of file ra_gif.c.

unsigned long cur_accum = 0

Definition at line 262 of file ra_gif.c.

int cur_bits = 0

Definition at line 263 of file ra_gif.c.

float curgamma [static]

Definition at line 777 of file ra_gif.c.

int currow

Definition at line 24 of file ra_gif.c.

int curx

Definition at line 258 of file ra_gif.c.

int cury

Definition at line 258 of file ra_gif.c.

int dither = 1

Definition at line 37 of file ra_gif.c.

int EOFCode

Definition at line 518 of file ra_gif.c.

Definition at line 488 of file ra_gif.c.

Definition at line 515 of file ra_gif.c.

FILE* g_outfile

Definition at line 516 of file ra_gif.c.

short gamtab[256] [static]

Definition at line 778 of file ra_gif.c.

double gamv = 2.2

Definition at line 35 of file ra_gif.c.

ifun_t getgifpix [static]

Definition at line 48 of file ra_gif.c.

Definition at line 21 of file ra_gif.c.

int greyscale = 0

Definition at line 36 of file ra_gif.c.

int Height

Definition at line 257 of file ra_gif.c.

Definition at line 474 of file ra_gif.c.

Definition at line 469 of file ra_gif.c.

long int in_count = 1

Definition at line 496 of file ra_gif.c.

int Interlace

Definition at line 261 of file ra_gif.c.

unsigned long masks[]
Initial value:
 { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
                                  0x001F, 0x003F, 0x007F, 0x00FF,
                                  0x01FF, 0x03FF, 0x07FF, 0x0FFF,
                                  0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }

Definition at line 619 of file ra_gif.c.

int maxbits = CBITS

Definition at line 464 of file ra_gif.c.

Definition at line 465 of file ra_gif.c.

Definition at line 466 of file ra_gif.c.

int n_bits

Definition at line 463 of file ra_gif.c.

int ncolors = 0

Definition at line 39 of file ra_gif.c.

int offset

Definition at line 495 of file ra_gif.c.

long int out_count = 0

Definition at line 497 of file ra_gif.c.

int Pass

Definition at line 260 of file ra_gif.c.

long picstart

Definition at line 25 of file ra_gif.c.

Definition at line 32 of file ra_gif.c.

char* progname

Definition at line 41 of file ra_gif.c.

const char RCSid[] = "$Id: ra_gif.c,v 2.11 2004/03/28 20:33:14 schorsch Exp $" [static]

Definition at line 2 of file ra_gif.c.

Definition at line 20 of file ra_gif.c.

int samplefac

Definition at line 34 of file neuclrtab.c.

Definition at line 31 of file ra_gif.c.

int Width

Definition at line 257 of file ra_gif.c.

int xmax

Definition at line 34 of file ra_gif.c.

int ymax

Definition at line 34 of file ra_gif.c.