Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Functions
rleskel.c File Reference
#include <stdio.h>
#include "rle.h"
Include dependency graph for rleskel.c:

Go to the source code of this file.

Functions

 main (int argc, char **argv)
 

Function Documentation

main ( int  argc,
char **  argv 
)

Definition at line 54 of file rleskel.c.

57 {
58  char *infname = NULL,
59  *outfname = NULL;
60  int oflag = 0;
61  int rle_cnt, rle_err, width, y;
62  FILE *outfile = 0;
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 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
#define RLE_EMPTY
Definition: rle.h:73
int width
Definition: pgmtorle.c:51
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
static int y
Definition: getami.c:691
string infname
Definition: getbob.c:68
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
Definition: rle.h:96
void rle_close_f(FILE *fd)
Definition: rle_open_f.c:244
int xmax
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
#define RLE_EOF
Definition: rle.h:74
rle_pixel ** rows
Definition: rletopaint.c:57
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
rle_hdr out_hdr
Definition: dvirle2.c:89
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267