Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rlebox.c
Go to the documentation of this file.
1 /*
2  * rlebox.c - Find bounding box for an RLE image
3  *
4  * Author: Spencer W. Thomas
5  * Computer Science Dept.
6  * University of Utah
7  * Date: Wed Feb 11 1987
8  * Copyright (c) 1987, University of Utah
9  */
10 static char rcsid[] = "$Header: /l/spencer/src/urt/tools/RCS/rlebox.c,v 3.0.1.3 1992/04/30 14:11:24 spencer Exp $";
11 /*
12 rlebox() Tag the file.
13 */
14 
15 #include <stdio.h>
16 #include "rle.h"
17 #include "rle_raw.h"
18 
19 /*****************************************************************
20  * TAG( main )
21  *
22  * Find a bounding box for an RLE image.
23  *
24  * Usage:
25  * rlebox [-v] [-c] [infile]
26  * Inputs:
27  * -v: Verbose mode - identifies numbers with text.
28  * Otherwise, just prints numbers.
29  * -c: Outputs corners of box in an order useful for use
30  * as arguments to the crop program:
31  * crop `rlebox -c foo.rle` foo.rle
32  * infile: The input file.
33  * Outputs:
34  * Prints the bounding box for the rle file. That is, it finds the
35  * minimum and maximum values of x and y for which there is some
36  * non-background data.
37  * Assumptions:
38  *
39  * Algorithm:
40  * Read the image file and find the smallest and largest X and Y
41  * coordinates of real image data. Use raw interface for speed.
42  */
43 void
45 int argc;
46 char **argv;
47 {
48  extern void rle_box();
49  rle_hdr the_hdr;
50  char * rlefname = NULL;
51  int vflag = 0, cflag = 0, margin = 0;
52  int xmin, xmax, ymin, ymax;
53  int rle_cnt, rle_err;
54 
55  if ( scanargs( argc, argv, "% v%- c%- m%-margin!d infile%s",
56  &vflag, &cflag, &margin, &margin,
57  &rlefname ) == 0 )
58  exit( 1 );
59 
60  the_hdr = *rle_hdr_init( (rle_hdr *)NULL );
61  rle_names( &the_hdr, cmd_name( argv ), rlefname, 0 );
62  the_hdr.rle_file = rle_open_f(the_hdr.cmd, rlefname, "r");
63 
64  for ( rle_cnt = 0;
65  (rle_err = rle_get_setup( &the_hdr )) == RLE_SUCCESS;
66  rle_cnt++ )
67  {
68 
69  rle_box( &the_hdr, &xmin, &xmax, &ymin, &ymax );
70 
71  /* If margin, enlarge bounds. Don't let them go negative */
72  if ( margin )
73  {
74  xmax += margin;
75  ymax += margin;
76  xmin -= margin;
77  if ( xmin < 0 )
78  xmin = 0;
79  ymin -= margin;
80  if ( ymin < 0 )
81  ymin = 0;
82  }
83 
84  if ( cflag )
85  printf( vflag ? "xmin=%d ymin=%d xmax=%d ymax=%d\n" :
86  "%d %d %d %d\n",
87  xmin, ymin, xmax, ymax );
88  else
89  printf( vflag ? "xmin=%d xmax=%d ymin=%d ymax=%d\n" :
90  "%d %d %d %d\n",
91  xmin, xmax, ymin, ymax );
92  }
93 
94  /* Check for an error. EOF or EMPTY is ok if at least one image
95  * has been read. Otherwise, print an error message.
96  */
97  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
98  rle_get_error( rle_err, the_hdr.cmd, rlefname );
99 
100  exit( 0 );
101 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
#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
#define RLE_SUCCESS
Definition: rle.h:70
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
#define RLE_EOF
Definition: rle.h:74
void rle_box(rle_hdr *the_hdr, int *xminp, int *xmaxp, int *yminp, int *ymaxp)
Definition: rle_box.c:16
static char rcsid[]
Definition: rlebox.c:10
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114