Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rlegrid.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * rlegrid.c - Generate grids and checkerboards for test images
20  *
21  * Author: James Painter
22  * Computer Science
23  * University of Utah
24  * Date: Tue November 20, 1990
25  * Copyright (c) 1990, University of Utah
26  */
27 #ifndef lint
28 char rcsid[] = "$Header: /l/spencer/src/urt/tools/RCS/rlegrid.c,v 3.0.1.1 1992/04/30 14:12:01 spencer Exp $";
29 #endif
30 /*
31 rlegrid() Tag the file.
32 */
33 
34 #include <stdio.h>
35 #include "rle.h"
36 #include "rle_raw.h"
37 
38 /*****************************************************************
39  * TAG( main )
40  *
41  * Generate simple grids
42  *
43  * Usage:
44  * rlegrid [-o outfile] [-s xsize ysize] [-w width] [-f fb_color]
45  * [-b bg_color][-c]
46  *
47  * Outputs:
48  * -o outfile: The output RLE file. Default stdout.
49  * "-" means stdout.
50  * Assumptions:
51  * [None]
52  * Algorithm:
53  */
54 void
56 int argc;
57 char **argv;
58 {
59  char *outfname = NULL;
60  int oflag = 0, sflag=0, wflag=0, fflag=0, bflag=0, cflag=0;
61  int xsize=512, ysize=512, width=16, fg_color=255, bg_color=0;
62  int x,y,i;
63  FILE *outfile;
64  rle_hdr out_hdr;
65  rle_op **scanraw[2]; /* space for two raw scanline buffers */
66  rle_op *p, *q;
67  int *nrawp[2];
68  unsigned char fg, bg;
69 
70  out_hdr = *rle_hdr_init( NULL );
71 
72  if ( scanargs( argc, argv,
73  "% o%-outfile!s s%-xsize!dysize!d w%-width!d f%-fg_color!d b%-bg_color!d c%-",
74  &oflag, &outfname,
75  &sflag, &xsize, &ysize,
76  &wflag, &width,
77  &fflag, &fg_color,
78  &bflag, &bg_color,
79  &cflag )
80  == 0 )
81  exit( 1 );
82 
83  fg = fg_color;
84  bg = bg_color;
85 
86  outfile = rle_open_f( cmd_name( argv ), outfname, "w" );
87 
88  /* Set up the output header.
89  */
90  (void)rle_hdr_init( &out_hdr );
91  rle_names( &out_hdr, cmd_name( argv ), outfname, 0 );
92  out_hdr.rle_file = outfile;
93  out_hdr.xmax = xsize -1;
94  out_hdr.ymax = ysize -1;
95  out_hdr.ncolors = 1;
96  out_hdr.alpha = 0;
97 
98 
99  /* Add to the history comment. */
100  rle_addhist( argv, (rle_hdr *)0, &out_hdr );
101 
102  /* Write the output image header. */
103  rle_put_setup( &out_hdr );
104 
105 
106  /* Allocate memory into which the image scanlines can be read.
107  */
108  if ( rle_raw_alloc( &out_hdr, scanraw+0, nrawp+0 ) < 0 ||
109  rle_raw_alloc( &out_hdr, scanraw+1, nrawp+1 ) < 0 )
110  RLE_CHECK_ALLOC( cmd_name( argv ), 0, "image memory" );
111 
112  /* Fill the scan lines */
113  if (cflag)
114  {
115  /* checkerboard option */
116  p = scanraw[0][0];
117  q = scanraw[1][0];
118  *nrawp[0] = *nrawp[1] = 0;
119  for(x=0; x<xsize;x+=2*width)
120  {
121  p->opcode = RRunDataOp;
122  p->xloc = x;
123  p->length = width;
124  *q = *p;
125  p->u.run_val = fg_color;
126  q->u.run_val = bg_color;
127  (*nrawp[0])++; (*nrawp[1])++;
128 
129  p++; q++;
130  p->opcode = RRunDataOp;
131  p->xloc = x+width;
132  p->length = (x+width < xsize) ? width : (xsize-x);
133  *q = *p;
134  p->u.run_val = bg_color;
135  q->u.run_val = fg_color;
136  (*nrawp[0])++; (*nrawp[1])++;
137  p++; q++;
138  }
139  }
140  else
141  { /* default: grid */
142 
143  p = scanraw[0][0];
144  p->opcode = RRunDataOp;
145  p->xloc = 0;
146  p->length = xsize;
147  p->u.run_val = fg_color;
148  *nrawp[0] = 1;
149 
150  /* grided scanline */
151  p = scanraw[1][0];
152  *nrawp[1] = 0;
153  for(x=0; x<xsize;x+=width)
154  {
156  p->xloc = x;
157  p->length = 1;
158  p->u.pixels = (rle_pixel *) &fg;
159  (*nrawp[1])++;
160 
161  p++;
162  p->opcode = RRunDataOp;
163  p->xloc = x+1;
164  p->length = (x+width < xsize) ? width-1 : (xsize-x);
165  p->u.run_val = bg_color;
166  (*nrawp[1])++;
167  p++;
168  }
169  }
170 
171  /* write the output file */
172  for ( y = 0; y < ysize; y++ )
173  {
174  i = (cflag) ? ((y/width) %2) : ((y %width) != 0);
175  rle_putraw( scanraw[i], nrawp[i], &out_hdr );
176  }
177 
178  /* Free memory. */
179  rle_raw_free( &out_hdr, scanraw[0], nrawp[0] );
180  rle_raw_free( &out_hdr, scanraw[1], nrawp[1] );
181 
182  /* Write an end-of-image code. */
183  rle_puteof( &out_hdr );
184 
185  exit( 0 );
186 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
char rcsid[]
Definition: rleswap.c:28
#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
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
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
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 RByteDataOp
Definition: rle_code.h:40
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86