Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
tobw.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  * tobw.c - Filter to change an RLE file to B&W.
20  *
21  * Author: Spencer W. Thomas
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Tue Feb 25 1986
25  * Copyright (c) 1986, University of Utah
26  *
27  */
28 #ifndef lint
29 static char rcs_ident[] = "$Header: /n/hgp/mendel/u/spencer/src/urt/tools/RCS/tobw.c,v 3.0 1990/08/03 15:24:09 spencer Exp spencer $";
30 #endif
31 
32 #include <stdio.h>
33 #include <math.h>
34 #include "rle.h"
35 
36 #ifdef USE_STDLIB_H
37 #include <stdlib.h>
38 #else
39 
40 #ifdef VOID_STAR
41 extern void *malloc();
42 #else
43 extern char *malloc();
44 #endif
45 extern void free();
46 
47 #endif /* USE_STDLIB_H */
48 
49 /*****************************************************************
50  * TAG( main )
51  *
52  * Usage: tobw [-t] [-o outfile] [infile]
53  *
54  * Inputs:
55  * -t: If specified, output a 3-channel image (all three
56  * the same).
57  * infile: Input (color) RLE file. Stdin used if not
58  * specified.
59  * Outputs:
60  * outfile: Output (black & white) RLE file. Stdout used
61  * if not specified.
62  * Assumptions:
63  * [None]
64  * Algorithm:
65  * [None]
66  */
67 void
69 int argc;
70 char **argv;
71 {
72  char * infname = NULL, * outfname = NULL;
73  FILE * outfile = stdout;
74  int oflag = 0, tflag = 0, nrow;
75  rle_hdr out_hdr;
76  unsigned char ** scan, *outscan[4];
77  unsigned char * buffer;
78  int rle_cnt, rle_err;
79 
80  if ( scanargs( argc, argv, "% t%- o%-outfile!s infile%s",
81  &tflag, &oflag, &outfname, &infname ) == 0 )
82  exit( 1 );
83 
85 
86  for ( rle_cnt = 0;
87  (rle_err = rle_get_setup( &rle_dflt_hdr )) == RLE_SUCCESS;
88  rle_cnt++ )
89  {
90  if ( rle_dflt_hdr.ncolors == 1 )
91  {
92  fprintf( stderr, "%s: %s is already black & white\n",
93  cmd_name( argv ), infname ? infname : "stdin" );
94  exit( 1 );
95  }
96  if ( rle_dflt_hdr.ncolors < 3 )
97  {
98  fprintf( stderr, "%s: %s is not RGB",
99  cmd_name( argv ), infname ? infname : "stdin" );
100  exit( 1 );
101  }
102  if ( rle_dflt_hdr.alpha )
104 
105  out_hdr = rle_dflt_hdr;
106  if ( rle_cnt == 0 )
107  outfile = rle_open_f(cmd_name( argv ), outfname, "w");
109  rle_dflt_hdr.xmin = 0;
110  nrow = rle_dflt_hdr.xmax + 1;
111  buffer = (unsigned char *)malloc( nrow );
113  if ( rle_dflt_hdr.alpha )
114  {
115  outscan[0] = scan[-1];
116  }
117  outscan[1] = buffer;
118  /* If 3 channel output, reference the line 3 times */
119  if ( tflag )
120  outscan[2] = outscan[3] = buffer;
121 
122  if ( ! tflag )
123  out_hdr.ncolors = 1;
124  else
125  out_hdr.ncolors = 3;
126 
127  if ( rle_dflt_hdr.background != 0 )
128  {
129  rle_pixel r, g, b, o;
130 
131  out_hdr.bg_color = (int *)malloc( sizeof( int ) );
132  r = rle_dflt_hdr.bg_color[0];
133  g = rle_dflt_hdr.bg_color[1];
134  b = rle_dflt_hdr.bg_color[2];
135  rgb_to_bw( &r, &g, &b, &o, 1 );
136  out_hdr.bg_color[0] = o;
137  }
138 
139  rle_addhist( argv, &rle_dflt_hdr, &out_hdr );
140 
141  out_hdr.rle_file = outfile;
142 
143  rle_put_setup( &out_hdr );
144 
145  while ( rle_getrow( &rle_dflt_hdr, scan ) <= rle_dflt_hdr.ymax )
146  {
147  rgb_to_bw( scan[0], scan[1], scan[2], buffer, nrow );
148  rle_putrow( &outscan[1], nrow, &out_hdr );
149  }
150  rle_puteof( &out_hdr );
151 
153  }
154 
155  /* Check for an error. EOF or EMPTY is ok if at least one image
156  * has been read. Otherwise, print an error message.
157  */
158  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
159  rle_get_error( rle_err, cmd_name( argv ), infname );
160 
161  exit( 0 );
162 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int xmin
Definition: rle.h:100
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
#define RLE_EMPTY
Definition: rle.h:73
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int * bg_color
Definition: rle.h:100
#define RLE_SUCCESS
Definition: rle.h:70
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
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
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
#define RLE_EOF
Definition: rle.h:74
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
int background
Definition: rle.h:100
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
#define USE_STDLIB_H
Definition: rle_config.h:25
#define RLE_ALPHA
Definition: rle.h:65
rle_hdr rle_dflt_hdr
Definition: rle_global.c:66
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
static char rcs_ident[]
Definition: tobw.c:29