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

Go to the source code of this file.

Functions

static void insert ()
 
void main (int argc, char **argv)
 
static void insert (int **sorted_list_p, int i, int n)
 

Variables

char rcsid [] = "$Header: /l/spencer/src/urt/tools/RCS/rleselect.c,v 3.0.1.2 1992/04/30 14:13:31 spencer Exp $"
 
static const char * my_name = "rleselect"
 

Function Documentation

static void insert ( )
static
static void insert ( int **  sorted_list_p,
int  i,
int  n 
)
static

Definition at line 244 of file rleselect.c.

247 {
248  register int *sorted_list = *sorted_list_p;
249  register int j;
250 
251  if ( n == 0 )
252  {
253  sorted_list = (int *)malloc( sizeof(int) );
254  RLE_CHECK_ALLOC( my_name, sorted_list, 0 );
255  *sorted_list = i;
256  }
257  else
258  {
259  sorted_list = (int *)realloc( sorted_list, (n + 1) * sizeof(int) );
260  RLE_CHECK_ALLOC( my_name, sorted_list, 0 );
261 
262  for ( j = n - 1; j >= 0 && sorted_list[j] > i; j-- )
263  sorted_list[j+1] = sorted_list[j];
264  sorted_list[j+1] = i;
265  }
266 
267  *sorted_list_p = sorted_list;
268 }
static const char * my_name
Definition: rleselect.c:39
int
Definition: getami.c:848
void * malloc()
int i
Definition: rletorla.c:82
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
void main ( int  argc,
char **  argv 
)

Definition at line 82 of file rleselect.c.

85 {
86  char *infname = NULL,
87  *outfname = NULL;
88  int *image_list = NULL;
89  int oflag = 0,
90  iflag = 0,
91  verbose = 0;
92  int nimage = 0;
93  int nsorted = 0;
94  int thrulast = -1; /* If > 0, take this through last. */
95  int rle_cnt, rle_err;
96  FILE *outfile = stdout;
97  /* Headers for input and output files. */
99  int *sorted_list;
100  register int i, j;
101 
102  my_name = cmd_name( argv );
103  in_hdr = *rle_hdr_init( NULL );
104  out_hdr = *rle_hdr_init( NULL );
105 
106 
107  if ( scanargs( argc, argv,
108  "% i%-infile%s o%-outfile!s v%- image-numbers!*d\n(\
109 \tSelect images from a file.\n\
110 \t-v\tEcho list of images selected.\n\
111 \tPositive numbers (1,...) select individual images. A negative\n\
112 \tnumber indicates a range from the previous number (or from 1 if \n\
113 \tat the beginning of the list), thus 3 -8 selects images 3, 4, 5,\n\
114 \t6, 7, 8. Zero means \"through the last\", so 5 0 selects images\n\
115 \t5 through the last one in the file. Image selections may be given in\n\
116 \tany order, they will be sorted and merged by the program.)",
117  &iflag, &infname, &oflag, &outfname, &verbose,
118  &nimage, &image_list ) == 0 )
119  exit( 1 );
120 
121  if ( nimage == 0 )
122  {
123  fprintf( stderr, "%s: No images selected.\n", my_name );
124  exit( 0 );
125  }
126 
127  /*
128  * Scan the image list, expanding "through" specifications. Then
129  * sort the result.
130  */
131  for ( i = 0; i < nimage; i++ )
132  {
133  if ( image_list[i] < 0 )
134  {
135  image_list[i] = abs( image_list[i] );
136  if ( i == 0 )
137  j = 1;
138  else
139  j = image_list[i-1] + 1;
140  for ( ; j <= image_list[i]; j++ )
141  insert( &sorted_list, j, nsorted++ );
142  }
143  else if ( image_list[i] == 0 )
144  {
145  if ( i == 0 )
146  thrulast = 1;
147  else
148  thrulast = image_list[i-1];
149  }
150  else
151  insert( &sorted_list, image_list[i], nsorted++ );
152  }
153  /* Optimize list. */
154  if ( thrulast >= 0 )
155  {
156  for ( i = nsorted - 1; i >= 0 && sorted_list[i] >= thrulast; i-- )
157  ;
158  nsorted = i + 1;
159  }
160 
161  if ( verbose )
162  {
163  fprintf( stderr, "%s: Selecting images", my_name );
164  for ( i = 0; i < nsorted; i++ )
165  {
166  if ( i % 14 == 0 )
167  fprintf( stderr, "\n\t" );
168  fprintf( stderr, "%d%s", sorted_list[i],
169  ((i < nsorted - 1) || thrulast >= 0) ? ", " : "" );
170  }
171  if ( thrulast > 0 )
172  fprintf( stderr, "%s%d - last", i % 14 == 0 ? "\n\t" : "",
173  thrulast );
174  putc( '\n', stderr );
175  fflush( stderr );
176  }
177 
178 
179  /* Open the input file.
180  * The output file won't be opened until the first image header
181  * has been read. This avoids unnecessarily wiping out a
182  * pre-existing file if the input is garbage.
183  */
184  in_hdr.rle_file = rle_open_f( my_name, infname, "r" );
185  rle_names( &in_hdr, my_name, infname, 0 );
186  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
187 
188  /* Read images from the input file until the end of file is
189  * encountered or an error occurs.
190  */
191  rle_cnt = 0;
192  i = 0;
193  while ( (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS )
194  {
195  /* Open the output file when the first header is successfully read. */
196  if ( rle_cnt == 0 )
197  outfile = rle_open_f( my_name, outfname, "w" );
198 
199  /* Count the input images. */
200  rle_cnt++;
201 
202  /* If it's not in the list, skip it. */
203  while ( i < nsorted && rle_cnt > sorted_list[i] )
204  i++;
205  /* Quick out when all selected images have been copied. */
206  if ( thrulast < 0 && i >= nsorted )
207  break;
208  /* Otherwise, if between selections, skip. */
209  if ( ! (thrulast >= 0 && rle_cnt >= thrulast) &&
210  (i >= nsorted || rle_cnt < sorted_list[i]) )
211  {
212  while ( rle_getskip( &in_hdr ) != 32768 )
213  ;
214  continue;
215  }
216 
217  /* The output header is a copy of the input header. The only
218  * difference is the FILE pointer.
219  */
220  (void)rle_hdr_cp( &in_hdr, &out_hdr );
221  out_hdr.rle_file = outfile;
222 
223  /* Add to the history comment. */
224  rle_addhist( argv, &in_hdr, &out_hdr );
225 
226  /* Write the output image header. */
227  rle_put_setup( &out_hdr );
228 
229  /* Copy the image. */
230  rle_cp( &in_hdr, &out_hdr );
231  }
232 
233  /* Check for an error. EOF or EMPTY is ok if at least one image
234  * has been read. Otherwise, print an error message.
235  */
236  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
237  rle_get_error( rle_err, my_name, infname );
238 
239  exit( 0 );
240 }
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
#define RLE_EMPTY
Definition: rle.h:73
void rle_cp(rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_cp.c:69
static const char * my_name
Definition: rleselect.c:39
static void insert()
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
int verbose
Definition: aliastorle.c:96
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
int iflag
Definition: getsun.c:80
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_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
int i
Definition: rletorla.c:82
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
FILE * rle_file
Definition: rle.h:114
rle_hdr out_hdr
Definition: dvirle2.c:89
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267

Variable Documentation

const char* my_name = "rleselect"
static

Definition at line 39 of file rleselect.c.

char rcsid[] = "$Header: /l/spencer/src/urt/tools/RCS/rleselect.c,v 3.0.1.2 1992/04/30 14:13:31 spencer Exp $"

Definition at line 28 of file rleselect.c.