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

Go to the source code of this file.

Functions

void main (int argc, char **argv)
 

Function Documentation

void main ( int  argc,
char **  argv 
)

Definition at line 26 of file rlescale.c.

29 {
30  int xsize = 512, ysize = 480, nsteps = 16, cflag = 0, oflag = 0;
31  int lflag = 0;
32  int y, i, * nscan;
33  rle_hdr hdr;
34  rle_op ** scans;
35  char buf[80], *out_fname = NULL;
36 
37  if ( scanargs(argc, argv,
38  "% c%- l%- n%-nsteps!d o%-outfile!s xsize%d ysize%d\n(\
39 \tDraw a step scale in gray or color.\n\
40 \t-c\tDraw color scales: gray, red, green, blue\n\
41 \t-l\tChange linearly between steps (default is exponential).\n\
42 \t-n\tNumber of steps in scale.)",
43  &cflag, &lflag, &nsteps, &nsteps,
44  &oflag, &out_fname, &xsize, &ysize ) == 0 )
45  exit( 1 );
46 
47  /* Sanity check parameters -- Runs must be at least 3 pixels long */
48  if ( xsize < 3 * nsteps || xsize < 24 )
49  {
50  fprintf( stderr,
51  "Image isn't wide enough for %d steps, should be at least %d pixels.\n",
52  nsteps, 24 < 3 * nsteps ? 3 * nsteps : 24 );
53  exit( 1 );
54  }
55 
56  hdr = *rle_hdr_init( (rle_hdr *)NULL );
57  rle_names( &hdr, cmd_name( argv ), out_fname, 0 );
58 
59  hdr.ncolors = 3;
60  hdr.alpha = 0; /* No coverage mask */
61  hdr.xmax = xsize - 1;
62  hdr.ymax = ysize - 1;
63  hdr.rle_file = rle_open_f(hdr.cmd, out_fname, "w");
64 
65  rle_addhist( argv, (rle_hdr *)0, &hdr );
66 
67  sprintf( buf, "IMAGE_TYPE=%s scale image with %d log steps",
68  cflag ? "Color" : "Gray", nsteps );
69  rle_putcom( buf, &hdr );
70 
71  /* Allocate storage for the output rows */
72  if ( rle_raw_alloc( &hdr, &scans, &nscan ) < 0 )
73  RLE_CHECK_ALLOC( hdr.cmd, 0, "output image data" );
74 
75  /* Create the header in the output file */
76  rle_put_setup( &hdr );
77 
78  /* Create the scanline for the color squares */
79  for ( i = 0; i < 8; i++ )
80  {
81  scans[0][i].u.run_val = (i & 1) ? 255 : 0;
82  scans[0][i].opcode = RRunDataOp;
83  scans[0][i].xloc = i * xsize / 8;
84  scans[0][i].length = (i + 1) * xsize / 8 - scans[0][i].xloc;
85  scans[1][i] = scans[0][i];
86  scans[1][i].u.run_val = (i & 2) ? 255 : 0;
87  scans[2][i] = scans[0][i];
88  scans[2][i].u.run_val = (i & 4) ? 255 : 0;
89  }
90  nscan[0] = 8;
91  nscan[1] = 8;
92  nscan[2] = 8;
93 
94  /* Write the color squares */
95  for ( y = 0; y < ysize / 8; y++ )
96  rle_putraw( scans, nscan, &hdr );
97 
98  /* Create the data for the scale */
99  for ( i = 0; i < nsteps; i++ )
100  {
101  if ( lflag )
102  /* Linear steps. */
103  scans[0][i].u.run_val = (int)(i * 255.0 / (nsteps - 1) + 0.5);
104  else
105  /* Exponential steps. */
106  scans[0][i].u.run_val =
107  (int)(255.0 / pow(2.0, i*(8.0/nsteps)) + 0.5);
108  scans[0][i].opcode = RRunDataOp;
109  scans[0][i].xloc = i * xsize / nsteps;
110  scans[0][i].length = (i + 1) * xsize / nsteps - scans[0][i].xloc;
111  scans[1][i] = scans[0][i];
112  scans[2][i] = scans[0][i];
113  }
114  nscan[0] = nsteps;
115  nscan[1] = nsteps;
116  nscan[2] = nsteps;
117 
118  /* Draw the scale */
119  if ( !cflag )
120  for ( ; y < ysize; y++ )
121  rle_putraw( scans, nscan, &hdr );
122  else
123  {
124  /* blue scale */
125  nscan[0] = nscan[1] = 0;
126  for ( ; y < ysize * 11./32. ; y++ )
127  rle_putraw( scans, nscan, &hdr );
128  /* green scale */
129  nscan[1] = nsteps;
130  nscan[2] = 0;
131  for ( ; y < ysize * 18./32.; y++ )
132  rle_putraw( scans, nscan, &hdr );
133  /* red scale */
134  nscan[0] = nsteps;
135  nscan[1] = 0;
136  for ( ; y < ysize * 25./32.; y++ )
137  rle_putraw( scans, nscan, &hdr );
138  /* white scale */
139  nscan[1] = nscan[2] = nsteps;
140  for ( ; y < ysize; y++ )
141  rle_putraw( scans, nscan, &hdr );
142  }
143 
144  rle_raw_free( &hdr, scans, nscan );
145 
146  rle_puteof( &hdr );
147 
148  exit( 0 );
149 }
#define RRunDataOp
Definition: rle_code.h:41
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int length
Definition: rle_raw.h:51
int opcode
Definition: rle_raw.h:49
const char * rle_putcom(const char *value, rle_hdr *the_hdr)
int rle_raw_alloc()
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
void rle_raw_free()
int xsize
Definition: read98721.c:58
rle_hdr hdr
Definition: iristorle.c:35
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
static int y
Definition: getami.c:691
int run_val
Definition: rle_raw.h:54
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
int xloc
Definition: rle_raw.h:50
const char * cmd
Definition: rle.h:133
Definition: rle.h:96
void rle_putraw()
int xmax
Definition: rle.h:100
static char buf[4096 +1]
Definition: into.c:46
int nscan
Definition: getsun.c:82
int
Definition: getami.c:848
int ymax
Definition: rle.h:100
int ysize
Definition: read98721.c:59
int i
Definition: rletorla.c:82
int alpha
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
union rle_op::@7 u
int cflag
Definition: getgmr.c:19
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
Definition: rle_raw.h:47
FILE * rle_file
Definition: rle.h:114
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