Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
getsun.c File Reference
#include <stdio.h>
#include <math.h>
#include <sys/file.h>
#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include "rle.h"
Include dependency graph for getsun.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 get_pic ()
 
void create_window ()
 
void init_color ()
 
void map_rgb_to_bw ()
 
void map_scanline ()
 
void put_scanline ()
 
void main (int argc, char **argv)
 
void get_pic (FILE *infile, char *infname, char *cmdname)
 
void create_window (int width, int height, char *name)
 
void map_scanline (rgb, int n, int s, int y,*line)
 
void put_scanline (unsigned char *scan, int width, int x, int y)
 
void map_rgb_to_bw (rle_pixel *red_row, rle_pixel *green_row, rle_pixel *blue_row, rle_pixel *bw_row, rle_pixel **map, int rowlen)
 

Variables

static char rcsid [] = "$Header: /l/spencer/src/urt/get/RCS/getsun.c,v 3.0.1.2 1992/04/30 14:05:10 spencer Exp $"
 
int dm16 [16][16]
 
rle_pixel ** in_cmap
 
int modN [256]
 
int divN [256]
 
int bwflag = 0
 
int levels = 0
 
int levelsq
 
int levelsc
 
double disp_gam = 2.5
 
double img_gam = 1.0
 
int iflag = 0
 
int nscan
 
int nrow
 
int nbyte
 
int dbg = 0
 
unsigned char * buffer
 
Frame frame
 
Canvas canvas
 
Pixwin * pw
 

Macro Definition Documentation

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

Definition at line 357 of file getsun.c.

Function Documentation

void create_window ( )
void create_window ( int  width,
int  height,
char *  name 
)

Definition at line 315 of file getsun.c.

318 {
319  char buf[BUFSIZ];
320 
321  sprintf(buf,"\"%s\": %d X %d",
322  name, height, width);
323  frame = window_create(0, FRAME,
324  FRAME_SHOW_LABEL, TRUE,
325  FRAME_LABEL, buf,
326  FRAME_NO_CONFIRM, TRUE,
327  0);
328  canvas = window_create(frame, CANVAS,
329  WIN_HEIGHT, height,
330  WIN_WIDTH, width,
331  0);
332  window_fit(frame);
333  if ((pw = canvas_pixwin(canvas)) == NULL) {
334  perror("Cannot create pixwin");
335  exit( 1 );
336  }
337  if (pw->pw_pixrect->pr_depth == 1) bwflag=2;
338 }
int width
Definition: pgmtorle.c:51
int height
Definition: pgmtorle.c:51
static char buf[4096 +1]
Definition: into.c:46
#define TRUE
Definition: giftorle.c:38
Pixwin * pw
Definition: getsun.c:91
Canvas canvas
Definition: getsun.c:90
int bwflag
Definition: getsun.c:65
Frame frame
Definition: getsun.c:89
void get_pic ( )
void get_pic ( FILE *  infile,
char *  infname,
char *  cmdname 
)

Definition at line 185 of file getsun.c.

188 {
189  register int i,
190  y;
191  int ncolors;
192  unsigned char *scan[3];
193  rle_hdr hdr;
194 
195  /*
196  * Read setup info from file.
197  */
198  hdr = *rle_hdr_init( (rle_hdr *)NULL );
199  rle_names( &hdr, cmdname, infname, 0 );
200 
201  hdr.rle_file = infile;
202  rle_get_setup_ok(&hdr, NULL, NULL);
203 
204  if ( dbg )
205  rle_debug( 1 );
206 
207  /* We're only interested in R, G, & B */
208  RLE_CLR_BIT(hdr, RLE_ALPHA);
209  for (i = 3; i < hdr.ncolors; i++)
210  RLE_CLR_BIT(hdr, i);
211  ncolors = hdr.ncolors > 3 ? 3 : hdr.ncolors;
212 
213  /*
214  * Compute image size and allocate storage.
215  */
216  nrow = (hdr.xmax - hdr.xmin + 1);
217  nscan = (hdr.ymax - hdr.ymin + 1);
218 
219  /*
220  * Get a window of the right size.
221  */
223 
224  if ( bwflag == 2 )
225  nbyte = ((nrow + 15) / 16) * 2; /* 1 bit display */
226  else
227  nbyte = ((nrow + 1) / 2) * 2;
228 
229  buffer = (unsigned char *) malloc(nbyte * nscan);
230  RLE_CHECK_ALLOC( hdr.cmd, buffer, "image memory" );
231 
232  /*
233  * Set up for rle_getrow. Pretend image x origin is 0.
234  */
235  for (i = 0; i < 3; i++)
236  scan[i] = (unsigned char *) malloc(nrow);
237  RLE_CHECK_ALLOC( hdr.cmd, scan[0]&&scan[1]&&scan[2], "input scan memory" );
238  hdr.xmax -= hdr.xmin;
239  hdr.xmin = 0;
240 
241  /* If no image gamma on command line, check comments in file */
242  if ( ! iflag )
243  {
244  char * v;
245  if ( (v = rle_getcom( "image_gamma", &hdr )) != NULL )
246  {
247  img_gam = atof( v );
248  /* Protect against bogus information */
249  if ( img_gam == 0.0 )
250  img_gam = 1.0;
251  else
252  img_gam = 1.0 / img_gam;
253  }
254  else if ( (v = rle_getcom( "display_gamma", &hdr )) != NULL )
255  {
256  img_gam = atof( v );
257  /* Protect */
258  if ( img_gam == 0.0 )
259  img_gam = 1.0;
260  }
261  }
262 
263  /*
264  * Set up the color map.
265  */
266  /* Input map, at least 3 channels */
267  in_cmap = buildmap( &hdr, 3, img_gam, 1.0 );
268 
269  init_color();
270 
271  /*
272  * For each scan line, dither it and display.
273  */
274  while ((y = rle_getrow(&hdr, scan)) <= hdr.ymax)
275  {
276  if ( bwflag && ncolors > 1 )
277  {
278  map_rgb_to_bw( scan[0], scan[1], scan[ncolors - 1], scan[0],
279  in_cmap, nrow );
280  /* Note: map_scanline only uses channel 0 for B&W */
281  }
282  else if ( bwflag )
283  for ( i = 0; i < nrow; i++ )
284  scan[0][i] = in_cmap[0][scan[0][i]];
285  else
286  for (i = 2; i >= ncolors; i--)
287  bcopy(scan[0], scan[i], nrow);
288  map_scanline(scan, nrow, 1, y,
289  &buffer[(hdr.ymax - y) * nbyte]);
290  put_scanline(&buffer[(hdr.ymax - y) * nbyte], nrow, 0,
291  hdr.ymax - y );
292  }
293  /*
294  * Free temp storage
295  */
296  for (i = 0; i < 3; i++)
297  free(scan[i]);
298 }
int xmin
Definition: rle.h:100
char * rle_getcom(const char *name, rle_hdr *the_hdr)
void init_color()
Definition: getsun.c:477
rle_pixel ** in_cmap
Definition: getsun.c:63
rle_hdr hdr
Definition: iristorle.c:35
int ymin
Definition: rle.h:100
void map_rgb_to_bw()
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
Definition: rle.h:96
int xmax
Definition: rle.h:100
int iflag
Definition: getsun.c:80
void put_scanline()
int nscan
Definition: getsun.c:82
double img_gam
Definition: getsun.c:79
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
FILE * infile
Definition: targatorle.c:102
int ymax
Definition: rle.h:100
void rle_debug(int on_off)
Definition: rle_getrow.c:293
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
Definition: buildmap.c:56
void * malloc()
void create_window()
int i
Definition: rletorla.c:82
int dbg
Definition: getsun.c:85
#define RLE_ALPHA
Definition: rle.h:65
int bwflag
Definition: getsun.c:65
int nbyte
Definition: getsun.c:83
unsigned char * buffer
Definition: getsun.c:87
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
void map_scanline()
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 init_color ( )

Definition at line 477 of file getsun.c.

References bwdithermap(), bwflag, disp_gam, dithermap(), divN, dm16, levels, levelsc, levelsq, make_square(), and modN.

478 {
479  register int i;
480  int rgbmap[256][3], bwmap[256];
481  unsigned char colormap[3][256];
482  char buf[12];
483 
484  if ( !bwflag )
485  {
486  /*
487  * Figure out how many color map entries we can get
488  */
489  if ( levels == 0 )
490  levels = 5; /* default starting point */
491 
492  levelsq = levels * levels;
493  levelsc = levelsq * levels;
494 
495  dithermap( levels, disp_gam, rgbmap, divN, modN, dm16 );
496 
497  /*
498  * Copy the color map entries into something that sunwindows
499  * can work with.
500  */
501  for(i = 0; i < levelsc; i++) {
502  colormap[0][i] = (u_char)rgbmap[i][0]&0xff;
503  colormap[1][i] = (u_char)rgbmap[i][1]&0xff;
504  colormap[2][i] = (u_char)rgbmap[i][2]&0xff;
505  }
506  sprintf(buf,"getsun.c%1d",levels);
507  pw_setcmsname(pw, buf);
508  pw_putcolormap(pw, 0, 4<<levels,colormap[0],colormap[1],colormap[2]);
509  return;
510  }
511 
512  /* Get a B&W color map (gray scale) */
513  if ( bwflag == 1 )
514  {
515  if ( levels == 0 )
516  levels = 5;
517  levelsq = levels * levels;
518  levelsc = levelsq * levels;
519  bwdithermap( levels, disp_gam, bwmap, divN, modN, dm16 );
520 
521  /*
522  * Copy the color map entries into something that sunwindows
523  * can work with.
524  */
525  for ( i = 0; i < levelsc; i++ )
526  {
527  colormap[0][i] = (u_char)bwmap[i]&0xff;
528  colormap[1][i] = (u_char)bwmap[i]&0xff;
529  colormap[2][i] = (u_char)bwmap[i]&0xff;
530  }
531  sprintf(buf,"getsun.g%1d",levels);
532  pw_setcmsname(pw, buf);
533  pw_putcolormap(pw, 0,4<<levels, colormap[0],colormap[1],colormap[2]);
534  return;
535  }
536 
537  /* If b&w 1-bit display, just use two colors */
538  if ( bwflag == 2 )
539  {
540  /* All we care about, really, is the magic square */
541  make_square( 255.0, divN, modN, dm16 );
542  levels = levelsc = 2;
543  return;
544  }
545 }
void bwdithermap(int levels, double gamma, int bwmap[], int divN[256], int modN[256], int magic[16][16])
int levels
Definition: getsun.c:73
int modN[256]
Definition: getsun.c:64
int divN[256]
Definition: getsun.c:64
void make_square(double N, int divN[256], int modN[256], int magic[16][16])
double disp_gam
Definition: getsun.c:78
rle_map * colormap
Definition: rletoppm.c:56
void dithermap(int levels, double gamma, int rgbmap[][3], int divN[256], int modN[256], int magic[16][16])
static char buf[4096 +1]
Definition: into.c:46
int dm16[16][16]
Definition: getsun.c:58
Pixwin * pw
Definition: getsun.c:91
int i
Definition: rletorla.c:82
int bwflag
Definition: getsun.c:65
int levelsq
Definition: getsun.c:73
int levelsc
Definition: getsun.c:73

Here is the call graph for this function:

void main ( int  argc,
char **  argv 
)

Definition at line 125 of file getsun.c.

128 {
129  CONST_DECL char *infname = NULL;
130  FILE * infile;
131  int gflag = 0;
132  int forkflg = 0;
133 
134  if ( scanargs( argc, argv,
135  "% Ww%- D%- f%- l%-levels!d Ii%-gamma!F g%-gamma!F file%s",
136  &bwflag, &dbg, &forkflg,
137  &levels, &levels,
138  &iflag, &img_gam,
139  &gflag, &disp_gam,
140  &infname ) == 0 )
141  exit( 1 );
142 
143 
144  if ( levels != 0 && (levels > 6 || levels < 2)) {
145  fprintf(stderr,"Level must be in the range [2,6]; %d is invalid!\n", levels);
146  exit( 1 );
147  }
148  if ( iflag == 1 ) /* -i */
149  img_gam = 1.0 / img_gam;
150 
151  infile = rle_open_f("getsun", infname, "r");
152  if ( infile == stdin )
153  infname = "Standard Input";
154 
155  if ( !forkflg && fork() != 0 )
156  exit(0);
157  get_pic( infile, infname);
158  if ( infname != NULL) fclose( infile);
159 
160  if ( !forkflg )
161  {
162  fclose(stdin); /* Close stdio to release rshd. */
163  fclose(stdout);
164  fclose(stderr);
165  }
166 
167  window_main_loop(frame);
168  exit(0);
169 }
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int levels
Definition: getsun.c:73
int forkflg
Definition: get4d.c:31
double disp_gam
Definition: getsun.c:78
string infname
Definition: getbob.c:68
int iflag
Definition: getsun.c:80
#define CONST_DECL
Definition: rle_config.h:42
double img_gam
Definition: getsun.c:79
FILE * infile
Definition: targatorle.c:102
void get_pic()
int dbg
Definition: getsun.c:85
int bwflag
Definition: getsun.c:65
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
Frame frame
Definition: getsun.c:89
void map_rgb_to_bw ( )
void map_rgb_to_bw ( rle_pixel red_row,
rle_pixel green_row,
rle_pixel blue_row,
rle_pixel bw_row,
rle_pixel **  map,
int  rowlen 
)

Definition at line 447 of file getsun.c.

454 {
455  register int x, bw;
456 
457  for (x=0; x<rowlen; x++)
458  {
459  /* 68000 won't store float > 127 into byte? */
460  /* HP compiler blows it */
461  bw = .35*map[0][red_row[x]] + .55*map[1][green_row[x]] +
462  .10*map[2][blue_row[x]];
463  bw_row[x] = bw;
464  }
465 }
static int bw
Definition: getami.c:106
short map[3][256]
Definition: getap.c:115
static int x
Definition: getami.c:691
void map_scanline ( )
void map_scanline ( rgb  ,
int  n,
int  s,
int  y,
line 
)

Definition at line 359 of file getsun.c.

362 {
363  register int i, col, row;
364 
365  if ( !bwflag )
366  {
367  register unsigned char *r, *g, *b;
368  for ( row = y % 16, col = 0, i = 0, r = rgb[0], g = rgb[1], b = rgb[2];
369  i < n; i++, r+=s, g+=s, b+=s, col = ((col + 1) & 15) )
370  line[i] = DMAP(in_cmap[0][*r], col, row) +
371  DMAP(in_cmap[1][*g], col, row) * levels +
372  DMAP(in_cmap[2][*b], col, row) * levelsq;
373  }
374  else if ( bwflag == 1 ) /* gray scale display */
375  {
376  register unsigned char *r;
377 
378  for ( row = y % 16, col = 0, i = 0, r = rgb[0];
379  i < n; i++, r+=s, col = ((col + 1) & 15) )
380  line[i] = DMAP(*r, col, row);
381  }
382  else /* bitmap display */
383  {
384  register unsigned short *l = (unsigned short *)line;
385  register unsigned char * r;
386 
387  for ( row = y % 16, col = 0, i = 0, r = rgb[0], *l = 0;
388  i < n; r+=s, col = ((col + 1) & 15) )
389  {
390  *l |= (*r > dm16[col][row] ? 0 : 1) << (15-(i % 16));
391  if ( (++i % 16) == 0 )
392  *++l = 0;
393  }
394  }
395 }
static unsigned char g
Definition: getami.c:692
rle_pixel rgb[3]
Definition: wasatchrle.c:47
int levels
Definition: getsun.c:73
static unsigned char r
Definition: getami.c:692
rle_pixel ** in_cmap
Definition: getsun.c:63
_urt_stack * s
Definition: rleClock.c:919
static int y
Definition: getami.c:691
static unsigned char b
Definition: getami.c:692
int dm16[16][16]
Definition: getsun.c:58
int i
Definition: rletorla.c:82
int bwflag
Definition: getsun.c:65
int levelsq
Definition: getsun.c:73
int row
Definition: rle.c:27
#define DMAP(v, x, y)
Definition: getsun.c:357
void put_scanline ( )
void put_scanline ( unsigned char *  scan,
int  width,
int  x,
int  y 
)

Definition at line 412 of file getsun.c.

415 {
416  Pixrect *pix;
417 
418  if ((pix = mem_point(width,1,pw->pw_pixrect->pr_depth,scan)) == NULL) {
419  perror("Unable to allocate pixrect");
420  exit( 1 );
421  }
422 
423  pw_write(pw, x, y, width, 1, PIX_SRC, pix, 0, 0);
424  free(pix);
425 }
int width
Definition: pgmtorle.c:51
static int y
Definition: getami.c:691
unsigned char ** scan
Definition: rle.c:26
static int x
Definition: getami.c:691
Pixwin * pw
Definition: getsun.c:91

Variable Documentation

unsigned char* buffer

Definition at line 87 of file getsun.c.

int bwflag = 0

Definition at line 65 of file getsun.c.

Canvas canvas

Definition at line 90 of file getsun.c.

int dbg = 0

Definition at line 85 of file getsun.c.

double disp_gam = 2.5

Definition at line 78 of file getsun.c.

int divN[256]

Definition at line 64 of file getsun.c.

int dm16[16][16]

Definition at line 58 of file getsun.c.

Frame frame

Definition at line 89 of file getsun.c.

int iflag = 0

Definition at line 80 of file getsun.c.

double img_gam = 1.0

Definition at line 79 of file getsun.c.

rle_pixel** in_cmap

Definition at line 63 of file getsun.c.

int levels = 0

Definition at line 73 of file getsun.c.

int levelsc

Definition at line 73 of file getsun.c.

int levelsq

Definition at line 73 of file getsun.c.

int modN[256]

Definition at line 64 of file getsun.c.

int nbyte

Definition at line 83 of file getsun.c.

int nrow

Definition at line 82 of file getsun.c.

int nscan

Definition at line 82 of file getsun.c.

Pixwin* pw

Definition at line 91 of file getsun.c.

char rcsid[] = "$Header: /l/spencer/src/urt/get/RCS/getsun.c,v 3.0.1.2 1992/04/30 14:05:10 spencer Exp $"
static

Definition at line 40 of file getsun.c.