Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
graytorle.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  * graytorle.c - Create an RLE image from gray pixels.
20  *
21  * Author: Michael J. Banks
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Wed Jun 22 1988
25  * Copyright (c) 1988, University of Utah
26  */
27 
28 #include <stdio.h>
29 #include "rle.h"
30 
31 typedef FILE *FILPTR;
32 
33 /*
34  * usage : graytorle xsize ysize [-h hdrsize] [-o outfile] [-a] files ...
35  *
36  * xsize,ysize Size of input files.
37  * -h hdrsize Input file header to discard.
38  * -o outfile Output file name.
39  * -a Uses first input file as alpha channel.
40  */
41 
42 void
44 int argc;
45 char *argv[];
46 {
47  int hflag = 0; /* Header size flag */
48  int aflag = 0; /* Alpha channel flag. */
49  int stdin_used = 0;
50  char *oname = NULL; /* Output file name. */
51  FILE **inpfil; /* Input file pointers. */
52  int xsize, ysize; /* Image size. */
53  int hsize; /* Image header size. */
54  int oflag; /* Output file flag. */
55  int files; /* Number of files. */
56  char **fname; /* List of input file names. */
57  rle_hdr out_hdr;
58  rle_pixel **outrow; /* Output buffer. */
59  int i, row;
60  char *trash;
61 
62  if (! scanargs( argc,argv,
63  "% a%- h%-hdrsize!d o%-outfile!s xsize!d ysize!d files!*s\n(\
64 \tConvert one or more raw images to an RLE image.\n\
65 \t-a\tFirst input file is alpha channel. Normally first is red.\n\
66 \t-h\tSpecify number of bytes to discard from input header.\n\
67 \txsize ysize\tSize of input images.\n\
68 \tTry rawtorle for more complex situations.)",
69  &aflag,
70  &hflag, &hsize,
71  &oflag, &oname,
72  &xsize, &ysize,
73  &files, &fname ))
74  exit( -1 );
75 
76  out_hdr = *rle_hdr_init( (rle_hdr *)NULL );
77  rle_names( &out_hdr, cmd_name( argv ), oname, 0 );
78 
79  /*
80  * Get enough file pointers for all input files that are specified,
81  * and try to open them.
82  */
83 
84  inpfil = (FILE **)malloc( sizeof( FILE * ) * files );
85  for ( i=0; i<files; i++ )
86  {
87  inpfil[i] = rle_open_f( "graytorle", fname[i], "r" );
88  if ( inpfil[i] == stdin )
89  {
90  if ( stdin_used )
91  {
92  fprintf( stderr,
93  "%s: Can't use standard input for more than one file\n",
94  out_hdr.cmd );
95  exit( -1 );
96  }
97  stdin_used++;
98  }
99  }
100 
101  /* Throw away file headers. */
102 
103  if ( hflag && (hsize > 0) )
104  {
105  trash = (char *)malloc( hsize );
106 
107  for ( i=0; i<files; i++ )
108  fread( trash, 1, hsize, inpfil[i] );
109 
110  free( trash );
111  }
112 
113  /* Adjust alpha channnel flag to use as index. */
114 
115  if ( aflag )
116  aflag = 1;
117  else
118  aflag = 0;
119 
120  /* Initialize the_hdr and allocate image row storage. */
121 
122  out_hdr.alpha = aflag;
123  out_hdr.ncolors = files - aflag;
124  out_hdr.xmax = xsize - 1;
125  out_hdr.ymax = ysize - 1;
126  out_hdr.rle_file = rle_open_f(out_hdr.cmd, oname, "w");
127  for ( i = -aflag; i<out_hdr.ncolors; i++)
128  RLE_SET_BIT( out_hdr, i );
129  rle_addhist( argv, (rle_hdr *)NULL, &out_hdr );
130  rle_put_setup( &out_hdr );
131 
132  if (rle_row_alloc( &out_hdr, &outrow ))
133  {
134  fprintf(stderr, "%s: Ran out of heap space!!\n",
135  out_hdr.cmd );
136  exit(-2);
137  }
138 
139  /* Combine rows and write to output file. Adjust for alpha. */
140 
141  for ( row=0; row<ysize; row++)
142  {
143  for ( i = -aflag; i<files-aflag; i++ )
144  fread( outrow[i], 1, xsize, inpfil[i+aflag] );
145  rle_putrow( outrow, xsize, &out_hdr );
146  }
147  rle_puteof( &out_hdr );
148 }
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
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_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
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
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
FILE * FILPTR
Definition: graytorle.c:31
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