Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rlescale.c
Go to the documentation of this file.
1 /*
2  * rlescale.c - Generate a gray-scale RLE file.
3  *
4  * Author: Spencer W. Thomas
5  * Electrical Engineering & Computer Science Dept.
6  * University of Michigan
7  * Date: Mon Jun 13 1988
8  * Copyright (c) 1988, University of Michigan
9  *
10  * Usage:
11  * rlescale [-c] [-l] [-n nsteps] [xsize] [ysize]
12  *
13  * Generates a xsize x ysize gray-scale RLE file (by default 480x512).
14  * Has color squares along the bottom, with nsteps (default 16) gray
15  * log stepped intensity gray rectangles above that. The -l flag
16  * makes a linear scale instead of a log scale. If -c is
17  * specified, does separate white, red, green, and blue scales.
18  */
19 
20 #include <stdio.h>
21 #include "rle.h"
22 #include "rle_raw.h"
23 #include <math.h>
24 
25 void
27 int argc;
28 char ** argv;
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 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
#define RRunDataOp
Definition: rle_code.h:41
int length
Definition: rle_raw.h:51
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
int rle_raw_alloc(rle_hdr *the_hdr, rle_op ***scanp, int **nrawp)
Definition: rle_raw_alc.c:60
int opcode
Definition: rle_raw.h:49
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
void rle_putraw(rle_op **scanraw, int *nraw, rle_hdr *the_hdr)
Definition: rle_putraw.c:60
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int xloc
Definition: rle_raw.h:50
const char * cmd
Definition: rle.h:133
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
int xmax
Definition: rle.h:100
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
int ymax
Definition: rle.h:100
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
Definition: rle_putcom.c:82
int alpha
Definition: rle.h:100
void rle_raw_free(rle_hdr *the_hdr, rle_op **scanp, nrawp)
Definition: rle_raw_alc.c:131
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86