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

Go to the source code of this file.

Macros

#define MAXCHAN   10
 

Functions

void main (int argc, argv)
 

Variables

static char rcs_ident [] = "$Id: rlehisto.c,v 3.0.1.2 1992/02/11 21:36:13 spencer Exp $"
 

Macro Definition Documentation

#define MAXCHAN   10

Definition at line 31 of file rlehisto.c.

Function Documentation

void main ( int  argc,
argv   
)

Definition at line 34 of file rlehisto.c.

37 {
38  FILE *outfile = stdout;
39  int i, j, bflag=0, cflag=0, tflag=0, oflag=0;
40  int hist_height = 256;
42  rle_pixel ** rows, ** rowsout;
43  rle_pixel *pixptr;
44  long *pixelcount[256];
45  long maxcount;
46  long n;
47  int chan, nchan;
48  int rle_cnt, rle_err;
49  char *infname = NULL, *outfname = NULL;
50 
51  in_hdr = *rle_hdr_init( NULL );
52  out_hdr = *rle_hdr_init( NULL );
53 
54  if ( scanargs( argc, argv,
55  "% b%- c%- t%- h%-height!d o%-outfile!s infile%s",
56  &bflag, &cflag, &tflag,
57  &i, &hist_height,
58  &oflag, &outfname, &infname ) == 0 )
59  exit( 1 );
60 
61  in_hdr.rle_file = rle_open_f(cmd_name( argv ), infname, "r");
62  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
63  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
64 
65  for ( rle_cnt = 0;
66  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
67  rle_cnt++ )
68  {
69  if ( rle_cnt == 0 )
70  outfile = rle_open_f(cmd_name( argv ), outfname, "w");
71 
72  /* Only pay attention to bflag if background color is defined. */
73  bflag = (bflag && in_hdr.bg_color != NULL);
74 
75  in_hdr.xmax -= in_hdr.xmin;
76  in_hdr.xmin = 0;
77 
78  (void)rle_hdr_cp( &in_hdr, &out_hdr );
79  out_hdr.cmap = (rle_map *)NULL;
80  out_hdr.ncmap = 0;
81  out_hdr.cmaplen = 0;
82  out_hdr.background = 2;
83  out_hdr.bg_color = (int *)calloc( in_hdr.ncolors, sizeof(int) );
84  RLE_CHECK_ALLOC( cmd_name( argv ), out_hdr.bg_color,
85  "background" );
86  out_hdr.alpha = 0;
87  out_hdr.xmin = 0;
88  out_hdr.xmax = 255;
89  out_hdr.ymin = 0;
90  out_hdr.ymax = hist_height - 1;
91  out_hdr.rle_file = outfile;
92 
93  nchan = in_hdr.ncolors;
94 
95  if (!tflag)
96  {
97  rle_addhist( argv, &in_hdr, &out_hdr );
98  rle_put_setup( &out_hdr );
99  }
100 
101  if ( rle_row_alloc( &out_hdr, &rowsout ) < 0 ||
102  rle_row_alloc( &in_hdr, &rows ) < 0 )
103  RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 );
104 
105  for ( j = 0; j < 256; j++)
106  {
107  if ( rle_cnt == 0 )
108  {
109  pixelcount[j] = (long *) malloc(sizeof(long) * nchan);
110  RLE_CHECK_ALLOC( cmd_name( argv ), pixelcount[j], 0 );
111  }
112  for (chan=0; chan < nchan; chan++)
113  {
114  pixelcount[j][chan] = 0;
115  }
116  }
117  maxcount = 0;
118 
119  for (j=in_hdr.ymin; j <= in_hdr.ymax; j++)
120  {
121  rle_getrow(&in_hdr, rows);
122  for (chan=0; chan < nchan; chan++)
123  {
124  pixptr = rows[chan];
125  for (i=0; i < in_hdr.xmax + 1; i++)
126  pixelcount[ *pixptr++ ][ chan ] += 1;
127  }
128  }
129 
130  /* create cumulative figures if those are wanted. */
131  if (cflag)
132  {
133  for (chan = 0; chan < nchan; chan++)
134  {
135  for (j = 1; j < 256; j++)
136  pixelcount[j][chan] += pixelcount[j-1][chan];
137  if (pixelcount[255][chan] > maxcount)
138  maxcount = pixelcount[255][chan];
139  }
140  }
141  else
142  for ( chan = 0; chan < nchan; chan++ )
143  for ( j = 0; j < 256; j++ )
144  if ( bflag && j == in_hdr.bg_color[chan] )
145  continue;
146  else
147  if ( pixelcount[j][chan] > maxcount )
148  maxcount = pixelcount[j][chan];
149 
150  /* after entire image has been read in, output the histogram */
151 
152  if (tflag)
153  {
154  if ( rle_cnt > 0 )
155  fprintf( outfile, "\n\n" );
156  for (j = 0; j < 256; j++)
157  {
158  for (chan = 0; chan < nchan; chan++)
159  if (j > 0 && cflag) {
160  if (pixelcount[j][chan] != pixelcount[j-1][chan])
161  break;
162  } else {
163  if (pixelcount[j][chan] != 0)
164  break;
165  }
166  if (chan == nchan) /* if all entries zero, suppress line */
167  continue;
168  fprintf(outfile, "%3d.", j);
169  for (chan = 0; chan < nchan; chan++)
170  fprintf(outfile, "\t%ld", pixelcount[j][chan]);
171  fprintf(outfile, "\n");
172  }
173  }
174  else
175  {
176  for (i = 0; i < hist_height; i++)
177  {
178  n = (maxcount * i) / (hist_height - 2);
179  for (chan = 0; chan < nchan; chan++)
180  {
181  for (j = 0; j < 256; j++)
182  {
183  if (pixelcount[j][chan] > n)
184  rowsout[chan][j] = 255;
185  else
186  rowsout[chan][j] = 0;
187  }
188  }
189  rle_putrow( rowsout, 256, &out_hdr);
190  }
191  rle_puteof( &out_hdr );
192  }
193 
194  /* Free memory. */
195  rle_row_free( &out_hdr, rowsout );
196  rle_row_free( &in_hdr, rows );
197  }
198  /* Check for an error. EOF or EMPTY is ok if at least one image
199  * has been read. Otherwise, print an error message.
200  */
201  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
202  rle_get_error( rle_err, cmd_name( argv ), infname );
203 
204  exit( 0 );
205 }
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
rle_map * cmap
Definition: rle.h:112
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
int * bg_color
Definition: rle.h:100
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
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 xmax
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
rle_pixel ** rows
Definition: rletopaint.c:57
int background
Definition: rle.h:100
int ncmap
Definition: rle.h:100
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
void * malloc()
int cmaplen
Definition: rle.h:100
int i
Definition: rletorla.c:82
int alpha
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
int nchan
Definition: fant.c:104
int cflag
Definition: getgmr.c:19
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
unsigned short rle_map
Definition: rle.h:57
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
int ncolors
Definition: rle.h:100
#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

Variable Documentation

char rcs_ident[] = "$Id: rlehisto.c,v 3.0.1.2 1992/02/11 21:36:13 spencer Exp $"
static

Definition at line 25 of file rlehisto.c.