Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletoascii.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 notices are
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  * rletoascii.c - Take a RLE, make it black and white, dump it as ascii chars.
20  *
21  */
22 
23 #include <stdio.h>
24 #include "rle.h"
25 
26 typedef FILE *FILPTR;
27 
28 /*
29  * usage : rletoascii [-S asciistring] [-r] [-o outfile] [infile]
30  *
31  * asciistring String of characters to use for output
32  * -r reverse video
33  * infile File to dump. If none, uses stdin.
34  * -o outfile Where to put the output. Default stdout.
35  */
36 /* Default string based on X font 6x13, assumes black on white.
37  * @ = 24
38  * B = 23
39  * R = 22
40  * * = 21
41  * # = 20
42  * $ = 19
43  * P = 18
44  * X = 17
45  * 0 = 16
46  * w = 15
47  * o = 14
48  * I = 13
49  * c = 12
50  * v = 11
51  * : = 10
52  * + = 9
53  * ! = 8
54  * ~ = 7
55  * " = 6
56  * . = 5
57  * , = 4
58  * = 0
59  */
60 static char default_asciistr[] = "@BR*#$PX0woIcv:+!~\"., ";
61 
62 void
64 int argc;
65 char *argv[];
66 {
67  char *infnam = NULL; /* Input file name. */
68  rle_hdr hdr;
69  int nrow, nscan, i, row, index, rflag=0;
70  unsigned char ** scan;
71  unsigned char * buffer;
72  char *asciistr = NULL;
73  int numchars;
74 
75  if (! scanargs( argc,argv,
76  "% S%-ascii_string!s r%- infile%s\n(\
77 \t-S\tSpecify set of characters for output (dark to light).\n\
78 \t-r\tReverse black & white -- white on black.)",
79  &i, &asciistr, &rflag, &infnam ))
80  exit( -1 );
81 
82  /* Set up header. */
83  hdr = *rle_hdr_init( (rle_hdr *)NULL );
84  rle_names( &hdr, cmd_name( argv ), infnam, 0 );
85  hdr.rle_file = rle_open_f( hdr.cmd, infnam, "r" );
86 
87  /* Read header information. */
88 
89  rle_get_setup_ok( &hdr, "rletoascii", infnam );
90  if ( hdr.ncolors == 0 )
91  {
92  fprintf( stderr, "%s: Image %s has no data.\n",
93  hdr.cmd, hdr.file_name );
94  exit( 1 );
95  }
96 
97  if ( hdr.ncolors > 3 ) {
98  fprintf( stderr,
99  "%s: Only first 3 channels (out of %d) in %s are shown.\n",
100  hdr.cmd, hdr.ncolors, hdr.file_name );
101  for ( i = 3; i < hdr.ncolors; i++ )
102  RLE_CLR_BIT( hdr, i );
103  }
104  RLE_CLR_BIT( hdr, RLE_ALPHA );
105  hdr.xmax -= hdr.xmin;
106  hdr.xmin = 0;
107  nrow = hdr.xmax + 1;
108  nscan = (hdr.ymax - hdr.ymin + 1);
109  buffer = (unsigned char *)malloc( nrow );
110  scan = (unsigned char **) malloc( hdr.ncolors *
111  sizeof( unsigned char * ) );
112  for ( i = 0; i < hdr.ncolors; i++ )
113  scan[i] = (unsigned char *)malloc( nrow );
114  for ( ; i < 3; i++ )
115  scan[i] = scan[i-1];
116 
117  if ( asciistr == NULL ) {
118  asciistr = (char *) malloc( 1 + strlen( default_asciistr ) );
119  strcpy(asciistr, default_asciistr);
120  }
121  numchars = strlen(asciistr);
122 
123  /* Read .rle file, dumping ascii as we go. */
124 
125  for (row=0; (row<nscan); row++)
126  {
127  if (hdr.ncolors > 1) {
128  rle_getrow( &hdr, scan );
129  rgb_to_bw( scan[0], scan[1], scan[2], buffer, nrow );
130  }
131  else {
132  rle_getrow( &hdr, &buffer );
133  }
134  for (i=0; i < nrow; i++) {
135  index = ((int)(buffer[i]) * numchars) >> 8;
136  if (rflag)
137  putchar(asciistr[numchars - (index + 1)]);
138  else
139  putchar(asciistr[index]);
140  }
141  printf("\n");
142  }
143 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int ymin
Definition: rle.h:100
void rgb_to_bw(rle_pixel *red_row, rle_pixel *green_row, rle_pixel *blue_row, rle_pixel *bw_row, int rowlen)
Definition: rle_putrow.c:680
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
#define index
Definition: rle_config.h:96
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
FILE * FILPTR
Definition: rletoascii.c:26
static char default_asciistr[]
Definition: rletoascii.c:60
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
int ymax
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
const char * file_name
Definition: rle.h:134
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