Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rleskel.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  * rleskel.c - Skeleton RLE tool.
20  *
21  * Author: Spencer W. Thomas
22  * EECS Dept.
23  * University of Michigan
24  * Date: Tue Jun 12 1990
25  * Copyright (c) 1990, University of Michigan
26  */
27 #if 0
28 rleskel() /* For tags. */
29 #endif
30 
31 #include <stdio.h>
32 #include "rle.h"
33 
34 /*****************************************************************
35  * TAG( main )
36  *
37  * A skeleton RLE tool. Demonstrates argument parsing, opening,
38  * reading, and writing RLE files. Includes support for files
39  * consisting of concatenated images.
40  * Usage:
41  * rleskel [-o outfile] [infile]
42  * Inputs:
43  * infile: The input RLE file. Default stdin.
44  * "-" means stdin.
45  * Outputs:
46  * -o outfile: The output RLE file. Default stdout.
47  * "-" means stdout.
48  * Assumptions:
49  * [None]
50  * Algorithm:
51  * Repeatedly read from the input until the file EOF or an
52  * error is encountered.
53  */
55 int argc;
56 char **argv;
57 {
58  char *infname = NULL,
59  *outfname = NULL;
60  int oflag = 0;
61  int rle_cnt, rle_err, width, y;
62  FILE *outfile = 0;
63  rle_hdr in_hdr, out_hdr;
64  rle_pixel **rows; /* Will be used for scanline storage. */
65 
66  in_hdr = *rle_hdr_init( NULL );
67  out_hdr = *rle_hdr_init( NULL );
68 
69  if ( scanargs( argc, argv, "% o%-outfile!s infile%s",
70  &oflag, &outfname, &infname ) == 0 )
71  exit( 1 );
72 
73  /* Open the input file.
74  * The output file won't be opened until the first image header
75  * has been read. This avoids unnecessarily wiping out a
76  * pre-existing file if the input is garbage.
77  */
78  in_hdr.rle_file = rle_open_f( cmd_name( argv ), infname, "r" );
79  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
80  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
81 
82  /* Read images from the input file until the end of file is
83  * encountered or an error occurs.
84  */
85  for ( rle_cnt = 0;
86  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
87  rle_cnt++ )
88  {
89  /* Open the output file when the first header is successfully read. */
90  if ( rle_cnt == 0 )
91  outfile = rle_open_f( cmd_name( argv ), outfname, "w" );
92 
93  /* The output header is a copy of the input header. The only
94  * difference is the FILE pointer.
95  */
96  (void)rle_hdr_cp( &in_hdr, &out_hdr );
97  out_hdr.rle_file = outfile;
98 
99  /* Add to the history comment. */
100  rle_addhist( argv, &in_hdr, &out_hdr );
101 
102  /* Write the output image header. */
103  rle_put_setup( &out_hdr );
104 
105  /* Since rle_getrow and rle_putrow use different array origins,
106  * we will compensate by adjusting the xmin and xmax values in
107  * the input header. [rle_getrow assumes that the scanline
108  * array starts at pixel 0, while rle_putrow assumes that the
109  * scanline array starts at pixel xmin. This is a botch, but
110  * it's too late to change it now.]
111  */
112  in_hdr.xmax -= in_hdr.xmin;
113  in_hdr.xmin = 0;
114  width = in_hdr.xmax + 1; /* Width of a scanline. */
115 
116  /* Allocate memory into which the image scanlines can be read.
117  * This should happen after the above adjustment, to minimize
118  * the amount of memory allocated.
119  */
120  if ( rle_row_alloc( &in_hdr, &rows ) < 0 )
121  RLE_CHECK_ALLOC( in_hdr.cmd, 0, "image memory" );
122 
123  /* Read the input image and copy it to the output file. */
124  for ( y = in_hdr.ymin; y <= in_hdr.ymax; y++ )
125  {
126  /* Read a scanline. */
127  rle_getrow( &in_hdr, rows );
128 
129  /* Process the scanline as desired here. */
130 
131  /* Write the processed scanline. */
132  rle_putrow( rows, width, &out_hdr );
133  }
134 
135  /* Protect from bad input. */
136  while ( rle_getskip( &in_hdr ) != 32768 )
137  ;
138 
139  /* Free memory. */
140  rle_row_free( &in_hdr, rows );
141 
142  /* Write an end-of-image code. */
143  rle_puteof( &out_hdr );
144  }
145 
146  /* Close the files. */
147  rle_close_f( in_hdr.rle_file );
148  rle_close_f( outfile ); /* Still safe if outfile == 0. */
149 
150  /* Check for an error. EOF or EMPTY is ok if at least one image
151  * has been read. Otherwise, print an error message.
152  */
153  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
154  rle_get_error( rle_err, cmd_name( argv ), infname );
155 
156  exit( 0 );
157 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
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 rle_close_f(FILE *fd)
Definition: rle_open_f.c:244
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
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
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
#define RLE_EOF
Definition: rle.h:74
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
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
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86