Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
to8.c File Reference
#include <stdio.h>
#include <math.h>
#include "rle.h"
Include dependency graph for to8.c:

Go to the source code of this file.

Macros

#define DMAP(v, x, y)   (modN[v]>dm16[x][y] ? divN[v] + 1 : divN[v])
 

Functions

void init_color ()
 
void map_scanline ()
 
void main (int argc, char **argv)
 
void map_scanline (rgb, int n, int y,*line)
 

Variables

static char rcs_ident [] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/to8.c,v 3.0.1.4 1992/04/30 14:14:43 spencer Exp spencer $"
 
short map [3][256]
 
int colmap [216][3]
 
rle_pixel ** in_cmap
 
int dm16 [16][16]
 
int modN [256]
 
int divN [256]
 
double disp_gam = 2.5
 
double img_gam = 1.0
 

Macro Definition Documentation

#define DMAP (   v,
  x,
  y 
)    (modN[v]>dm16[x][y] ? divN[v] + 1 : divN[v])

Definition at line 236 of file to8.c.

Function Documentation

void init_color ( )

Definition at line 218 of file to8.c.

References colmap, disp_gam, dithermap(), divN, dm16, map, and modN.

219 {
220  int i;
221 
223 
224  for (i = 0; i < 216; i++)
225  {
226  map[0][i] = colmap[i][0] << 8;
227  map[1][i] = colmap[i][1] << 8;
228  map[2][i] = colmap[i][2] << 8;
229  }
230 
231 }
int modN[256]
Definition: to8.c:45
int divN[256]
Definition: to8.c:45
void dithermap(int levels, double gamma, int rgbmap[][3], int divN[256], int modN[256], int magic[16][16])
int dm16[16][16]
Definition: to8.c:42
double disp_gam
Definition: to8.c:47
short map[3][256]
Definition: to8.c:38
int colmap[216][3]
Definition: to8.c:39
int i
Definition: rletorla.c:82

Here is the call graph for this function:

void main ( int  argc,
char **  argv 
)

Definition at line 77 of file to8.c.

80 {
81  char * infname = NULL, * outfname = NULL;
82  char comment[80]; /* for gamma comment */
83  FILE * outfile = stdout;
84  int oflag = 0, y, nrow, iflag = 0, gflag = 0;
86  unsigned char ** scan, *outscan[2];
87  unsigned char * buffer;
88  int rle_cnt, rle_err;
89 
90  in_hdr = *rle_hdr_init( NULL );
91  out_hdr = *rle_hdr_init( NULL );
92 
93  if ( scanargs( argc, argv,
94  "% Ii%-gamma!F g%-gamma!F o%-outfile!s infile%s",
95  &iflag, &img_gam, &gflag, &disp_gam,
96  &oflag, &outfname, &infname ) == 0 )
97  exit( 1 );
98  in_hdr.rle_file = rle_open_f(cmd_name( argv ), infname, "r");
99  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
100  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
101 
102  for ( rle_cnt = 0;
103  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
104  rle_cnt++ )
105  {
106 
107  if ( in_hdr.ncolors == 1 )
108  {
109  fprintf( stderr, "%s is already black & white\n",
110  infname ? infname : "stdin" );
111  exit( 1 );
112  }
113  if ( in_hdr.ncolors < 3 )
114  {
115  fprintf( stderr, "%s is not RGB",
116  infname ? infname : "stdin" );
117  exit( 1 );
118  }
119 
120  /* If no image gamma on command line, check comments in file */
121  if ( ! iflag )
122  {
123  char * v;
124  if ( (v = rle_getcom( "image_gamma", &in_hdr )) != NULL )
125  {
126  img_gam = atof( v );
127  /* Protect against bogus information */
128  if ( img_gam == 0.0 )
129  img_gam = 1.0;
130  else
131  img_gam = 1.0 / img_gam;
132  }
133  else if ( (v = rle_getcom( "display_gamma", &in_hdr )) != NULL )
134  {
135  img_gam = atof( v );
136  /* Protect */
137  if ( img_gam == 0.0 )
138  img_gam = 1.0;
139  }
140  }
141 
142  (void)rle_hdr_cp( &in_hdr, &out_hdr );
143  if ( rle_cnt == 0 )
144  outfile = rle_open_f(cmd_name( argv ), outfname, "w");
145  in_hdr.xmax -= in_hdr.xmin;
146  in_hdr.xmin = 0;
147  nrow = in_hdr.xmax + 1;
148  buffer = (unsigned char *)malloc( nrow );
149  RLE_CHECK_ALLOC( cmd_name( argv ), buffer, 0 );
150  if ( rle_row_alloc( &in_hdr, &scan ) < 0 )
151  RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 );
152 
153  if ( in_hdr.alpha )
154  {
155  outscan[0] = scan[-1];
156  }
157  outscan[1] = buffer;
158 
159  /* Use input color map, too */
160  in_cmap = buildmap( &in_hdr, 3, img_gam, 1.0 );
161 
162  init_color();
163  out_hdr.ncolors = 1;
164  out_hdr.ncmap = 3;
165  out_hdr.cmaplen = 8; /* 256 entries */
166  out_hdr.cmap = (rle_map *)map;
167  /* Delete color map length comment, if present. */
168  rle_delcom( "color_map_length", &out_hdr );
169 
170  /* Record gamma color map was computed for */
171  sprintf( comment, "display_gamma=%g", disp_gam );
172  rle_putcom( comment, &out_hdr );
173 
174  /*
175  * Give it a background color of black, since the real background
176  * will be dithered anyway.
177  */
178  if ( in_hdr.background != 0 )
179  {
180  out_hdr.bg_color = (int *)malloc( sizeof( int ) );
181  RLE_CHECK_ALLOC( cmd_name( argv ), out_hdr.bg_color, 0 );
182  out_hdr.bg_color[0] = 0;
183  }
184 
185  out_hdr.rle_file = outfile;
186 
187  rle_addhist( argv, &in_hdr, &out_hdr );
188 
189  rle_put_setup( &out_hdr );
190 
191  while ( (y = rle_getrow( &in_hdr, scan )) <=
192  in_hdr.ymax )
193  {
194  map_scanline( scan, nrow, y, buffer );
195  rle_putrow( &outscan[1], nrow, &out_hdr );
196  }
197 
198  rle_puteof( &out_hdr );
199 
200  rle_row_free( &in_hdr, scan );
201  }
202 
203  /* Check for an error. EOF or EMPTY is ok if at least one image
204  * has been read. Otherwise, print an error message.
205  */
206  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
207  rle_get_error( rle_err, cmd_name( argv ), infname );
208 
209  exit( 0 );
210 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
void init_color()
Definition: to8.c:218
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
char * rle_getcom(const char *name, rle_hdr *the_hdr)
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
const char * rle_delcom(const char *name, rle_hdr *the_hdr)
#define RLE_EMPTY
Definition: rle.h:73
const char * rle_putcom(const char *value, rle_hdr *the_hdr)
rle_map * cmap
Definition: rle.h:112
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
int * bg_color
Definition: rle.h:100
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
rle_pixel ** in_cmap
Definition: to8.c:40
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
static int y
Definition: getami.c:691
unsigned char ** scan
Definition: rle.c:26
string infname
Definition: getbob.c:68
int nrow
Definition: getsun.c:82
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
double disp_gam
Definition: to8.c:47
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
Definition: rle.h:96
int xmax
Definition: rle.h:100
int iflag
Definition: getsun.c:80
void map_scanline()
int rle_get_error(int code, const char *pgmname, const char *fname)
short map[3][256]
Definition: to8.c:38
#define RLE_EOF
Definition: rle.h:74
int background
Definition: rle.h:100
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
Definition: buildmap.c:56
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
double img_gam
Definition: to8.c:48
void * malloc()
int cmaplen
Definition: rle.h:100
int alpha
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
unsigned short rle_map
Definition: rle.h:57
char * cmd_name(char **argv)
Definition: cmd_name.c:31
unsigned char * buffer
Definition: getsun.c:87
int oflag
Definition: painttorle.c:45
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
rle_hdr out_hdr
Definition: dvirle2.c:89
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
void map_scanline ( )
void map_scanline ( rgb  ,
int  n,
int  y,
line 
)

Definition at line 239 of file to8.c.

242 {
243  register unsigned char *r, *g, *b;
244  register int i, col, row;
245  for ( row = y % 16, col = 0, i = 0, r = rgb[0], g = rgb[1], b = rgb[2];
246  i < n; i++, r++, g++, b++, col = ((col + 1) & 15) )
247  line[i] = DMAP(in_cmap[0][*r], col, row) +
248  DMAP(in_cmap[1][*g], col, row) * 6 +
249  DMAP(in_cmap[2][*b], col, row) * 36;
250 }
static unsigned char g
Definition: getami.c:692
#define DMAP(v, x, y)
Definition: to8.c:236
rle_pixel rgb[3]
Definition: wasatchrle.c:47
static unsigned char r
Definition: getami.c:692
rle_pixel ** in_cmap
Definition: to8.c:40
static int y
Definition: getami.c:691
static unsigned char b
Definition: getami.c:692
int i
Definition: rletorla.c:82
int row
Definition: rle.c:27

Variable Documentation

int colmap[216][3]
double disp_gam = 2.5

Definition at line 47 of file to8.c.

Referenced by init_color(), and load_x_map().

int divN[256]
int dm16[16][16]
double img_gam = 1.0

Definition at line 48 of file to8.c.

rle_pixel** in_cmap

Definition at line 40 of file to8.c.

Referenced by load_x_map().

short map[3][256]

Definition at line 38 of file to8.c.

Referenced by init_color(), and window_color_map().

int modN[256]

Definition at line 45 of file to8.c.

Referenced by init_color(), init_dither(), and window_color_map().

char rcs_ident[] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/to8.c,v 3.0.1.4 1992/04/30 14:14:43 spencer Exp spencer $"
static

Definition at line 29 of file to8.c.