w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pnmhistmap.c
Go to the documentation of this file.
1 /* pnmhistmap.c -
2  * Draw a histogram for a PGM or PPM file
3  *
4  * Options: -verbose: the usual
5  * -max N: force scaling value to N
6  * -black: ignore all-black count
7  * -white: ignore all-white count
8  *
9  * - PGM histogram is a PBM file, PPM histogram is a PPM file
10  * - No conditional code - assumes all three: PBM, PGM, PPM
11  *
12  * Copyright (C) 1993 by Wilson H. Bent, Jr (whb@usc.edu)
13  *
14  */
15 
16 #include <string.h>
17 #include "pnm.h"
18 
19 /* prototypes */
20 static void ppm_hist ARGS((FILE *fp));
21 static void pgm_hist ARGS((FILE *fp));
22 
23 #define HIST_WIDTH 256
24 #define HIST_HEIGHT 200
25 
28 int format;
29 
30 int verbose = 0;
31 int no_black = 0;
32 int no_white = 0;
33 
37 int hmax = -1;
38 double scale;
39 
40 int
42 int argc;
43 char *argv[];
44 {
45  FILE *ifp;
46  int argn = 1;
47  char *usage = "[-white] [-black] [-max maxvalue] [-verbose] [pnmfile]";
48 
49  pnm_init (&argc, argv);
50 
51  /* Check for flags. */
52  while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
53  if ( pm_keymatch( argv[argn], "-white", 2 ) )
54  no_white = 1;
55  else if ( pm_keymatch( argv[argn], "-black", 2 ) )
56  no_black = 1;
57  else if ( pm_keymatch( argv[argn], "-max", 2 ) ) {
58  if ((hmax = atoi (argv[++argn])) <= 0)
59  pm_error ("Max. value (%d) must be positive", hmax, 0, 0, 0, 0);
60  }
61  else if ( pm_keymatch( argv[argn], "-verbose", 2 ) )
62  verbose = 1;
63  else
64  pm_usage( usage );
65  ++argn;
66  }
67 
68  if (--argc > argn)
69  pm_usage (usage);
70  else if (argc == argn)
71  ifp = pm_openr (argv[argn]);
72  else
73  ifp = stdin;
74 
76  switch (PNM_FORMAT_TYPE (format)) {
77  case PPM_TYPE:
78  ppm_hist (ifp);
79  break;
80  case PGM_TYPE:
81  pgm_hist (ifp);
82  break;
83  case PBM_TYPE:
84  pm_error ("no histograms for PBM files", 0, 0, 0, 0, 0);
85  break;
86  }
87  exit (0);
88 }
89 
90 static void
92 FILE *fp;
93 {
94  pixel *pixrow;
95  pixel **pixels;
96  int i, j;
97  int start, finish;
98 
100  pm_error ("no space for output array (%d pixels)",
101  HIST_WIDTH * HIST_HEIGHT, 0,0,0,0);
102  for (i = 0; i < HIST_HEIGHT; ++i)
103  bzero (pixels[i], HIST_WIDTH * sizeof (pixel));
104  bzero (rhist, sizeof (rhist));
105  bzero (ghist, sizeof (ghist));
106  bzero (bhist, sizeof (bhist));
107 
108  /* read the pixel values into the histogram arrays */
110  /*XX error-check! */
111  if (verbose) pm_message ("making histogram...", 0,0,0,0,0);
112  for (i = rows; i > 0; --i) {
114  for (j = hist_cols-1; j >= 0; --j) {
115  rhist[PPM_GETR(pixrow[j])]++;
116  ghist[PPM_GETG(pixrow[j])]++;
117  bhist[PPM_GETB(pixrow[j])]++;
118  }
119  }
121  fclose (fp);
122 
123  /* find the highest-valued slot and set the scale value */
124  if (verbose)
125  pm_message ("finding max. slot height...", 0, 0, 0, 0, 0);
126  if (hmax == -1) {
127  start = (no_black ? 1 : 0);
128  finish = (no_white ? HIST_WIDTH - 1 : HIST_WIDTH);
129  for (hmax = 0, i = start; i < finish; ++i) {
130  if (hmax < rhist[i])
131  hmax = rhist[i];
132  if (hmax < ghist[i])
133  hmax = ghist[i];
134  if (hmax < bhist[i])
135  hmax = bhist[i];
136  }
137  }
138  for (i = 0; i < HIST_WIDTH; ++i) {
139  if (rhist[i] > hmax)
140  rhist[i] = hmax;
141  if (ghist[i] > hmax)
142  ghist[i] = hmax;
143  if (bhist[i] > hmax)
144  bhist[i] = hmax;
145  }
146  if (verbose)
147  pm_message ("Done: height = %d", hmax, 0, 0, 0, 0);
148  scale = (double) HIST_HEIGHT / hmax;
149 
150  for (i = 0; i < HIST_WIDTH; ++i) {
151  for (j = HIST_HEIGHT - (int)(scale * rhist[i]); j < HIST_HEIGHT; ++j)
152  PPM_PUTR(pixels[j][i], maxval);
153  for (j = HIST_HEIGHT - (int)(scale * ghist[i]); j < HIST_HEIGHT; ++j)
154  PPM_PUTG(pixels[j][i], maxval);
155  for (j = HIST_HEIGHT - (int)(scale * bhist[i]); j < HIST_HEIGHT; ++j)
156  PPM_PUTB(pixels[j][i], maxval);
157  }
158 
160 }
161 
162 static void
164 FILE *fp;
165 {
166  gray *grayrow;
167  bit **bits;
168  int i, j;
169  int start, finish;
170 
172  pm_error ("no space for output array (%d bits)",
173  HIST_WIDTH * HIST_HEIGHT, 0,0,0,0);
174  bzero (ghist, sizeof (ghist));
175 
176  /* read the pixel values into the histogram arrays */
178  /*XX error-check! */
179  if (verbose) pm_message ("making histogram...", 0,0,0,0,0);
180  for (i = rows; i > 0; --i) {
182  for (j = hist_cols-1; j >= 0; --j)
183  ghist[grayrow[j]]++;
184  }
186  fclose (fp);
187 
188  /* find the highest-valued slot and set the scale value */
189  if (verbose)
190  pm_message ("finding max. slot height...", 0, 0, 0, 0, 0);
191  if (hmax == -1) {
192  start = (no_black ? 1 : 0);
193  finish = (no_white ? HIST_WIDTH - 1 : HIST_WIDTH);
194  for (hmax = 0, i = start; i < finish; ++i)
195  if (hmax < ghist[i])
196  hmax = ghist[i];
197  }
198  for (i = 0; i < HIST_WIDTH; ++i)
199  if (ghist[0] > hmax)
200  ghist[0] = hmax;
201  if (verbose)
202  pm_message ("Done: height = %d", hmax, 0, 0, 0, 0);
203  scale = (double) HIST_HEIGHT / hmax;
204 
205  for (i = 0; i < HIST_WIDTH; ++i) {
206  int mark = HIST_HEIGHT - (int)(scale * ghist[i]);
207  for (j = 0; j < mark; ++j)
208  bits[j][i] = PBM_BLACK;
209  for ( ; j < HIST_HEIGHT; ++j)
210  bits[j][i] = PBM_WHITE;
211  }
212 
214 }
#define mark
Definition: aptex-macros.h:374
int pixels
Definition: dvipng.h:106
static char usage[]
Definition: giftopnm.c:59
long grayrow[1023]
Definition: hbf2gf.c:383
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
#define bits
Definition: infblock.c:15
void exit()
int atoi(const char *)
#define fclose
Definition: debug.h:100
#define bzero(p, n)
Definition: config.h:6
void pm_usage(char *usage)
Definition: libpbm1.c:343
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
void pbm_writepbm(FILE *file, bit **bits, int cols, int rows, int forceplain)
Definition: libpbm3.c:136
#define PBM_TYPE
Definition: pbm.h:21
unsigned char bit
Definition: pbm.h:9
#define pbm_allocarray(cols, rows)
Definition: pbm.h:34
#define PBM_BLACK
Definition: pbm.h:11
#define PBM_WHITE
Definition: pbm.h:10
#define ARGS(alist)
Definition: pbmplus.h:235
void pgm_readpgmrow(FILE *file, gray *grayrow, int cols, gray maxval, int format)
Definition: libpgm1.c:104
#define pgm_allocrow(cols)
Definition: pgm.h:58
#define PGM_TYPE
Definition: pgm.h:45
unsigned int gray
Definition: pgm.h:10
#define pgm_freerow(grayrow)
Definition: pgm.h:60
#define fp
#define pm_error
Definition: png22pnm.c:118
#define pm_keymatch(stra, strb, _x)
Definition: png22pnm.c:121
#define pnm_init(_a, _b)
Definition: png22pnm.c:114
#define pm_message
Definition: png22pnm.c:116
void pnm_readpnminit(FILE *file, int *colsP, int *rowsP, xelval *maxvalP, int *formatP)
Definition: libpnm1.c:35
#define PNM_FORMAT_TYPE(f)
Definition: pnm.h:15
pixval xelval
Definition: pnm.h:9
static void ppm_hist()
int hmax
Definition: pnmhistmap.c:37
int rows
Definition: pnmhistmap.c:26
int verbose
Definition: pnmhistmap.c:30
int format
Definition: pnmhistmap.c:28
int bhist[256]
Definition: pnmhistmap.c:36
int hist_cols
Definition: pnmhistmap.c:26
xelval maxval
Definition: pnmhistmap.c:27
#define HIST_HEIGHT
Definition: pnmhistmap.c:24
double scale
Definition: pnmhistmap.c:38
int rhist[256]
Definition: pnmhistmap.c:34
#define HIST_WIDTH
Definition: pnmhistmap.c:23
int no_black
Definition: pnmhistmap.c:31
int no_white
Definition: pnmhistmap.c:32
static void pgm_hist()
int main(int argc, argv)
Definition: pnmhistmap.c:41
int ghist[256]
Definition: pnmhistmap.c:35
void ppm_readppmrow(FILE *file, pixel *pixelrow, int cols, pixval maxval, int format)
Definition: libppm1.c:87
void ppm_writeppm(FILE *file, pixel **pixels, int cols, int rows, pixval maxval, int forceplain)
Definition: libppm2.c:148
#define PPM_GETR(p)
Definition: ppm.h:36
#define PPM_PUTG(p, grn)
Definition: ppm.h:42
#define PPM_GETG(p)
Definition: ppm.h:37
#define PPM_PUTR(p, red)
Definition: ppm.h:41
#define PPM_PUTB(p, blu)
Definition: ppm.h:43
#define ppm_allocarray(cols, rows)
Definition: ppm.h:71
#define ppm_freerow(pixelrow)
Definition: ppm.h:74
#define PPM_GETB(p)
Definition: ppm.h:38
#define ppm_allocrow(cols)
Definition: ppm.h:72
#define PPM_TYPE
Definition: ppm.h:59
static pixel * pixrow
Definition: ppmtoilbm.c:128
Definition: namelist.c:170
Definition: ppm.h:33
#define FILE
Definition: t1stdio.h:34
int j
Definition: t4ht.c:1589
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
FILE * ifp
Definition: t1asm.c:88
@ start
Definition: preamble.c:52
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
#define argn