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)  

rawtopgm.c
Go to the documentation of this file.
1 /* rawtopgm.c - convert raw grayscale bytes into a portable graymap
2 **
3 ** Copyright (C) 1989 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12 
13 #include <math.h>
14 #include "pgm.h"
15 
16 
17 int
19  int argc;
20  char* argv[];
21  {
22  FILE* ifp;
23  gray* grayrow;
24  register gray* gP;
25  int argn, headerskip, row, i;
26  float rowskip, toskip;
27  register int col, val;
28  int rows=0, cols=0, topbottom=0;
29  char* buf = NULL;
30  char* pos;
31  long nread = 0;
32  char* usage = "[-headerskip N] [-rowskip N] [-tb|-topbottom] [<width> <height>] [rawfile]";
33  /* double atof(); should be declared by math.h */
34 
35 
36  pgm_init( &argc, argv );
37 
38  argn = 1;
39  headerskip = 0;
40  rowskip = 0.0;
41 
42  while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
43  {
44  if ( pm_keymatch( argv[argn], "-headerskip", 2 ) )
45  {
46  ++argn;
47  if ( argn >= argc )
48  pm_usage( usage );
49  headerskip = atoi( argv[argn] );
50  }
51  else if ( pm_keymatch( argv[argn], "-rowskip", 2 ) )
52  {
53  ++argn;
54  if ( argn >= argc )
55  pm_usage( usage );
56  rowskip = atof( argv[argn] );
57  }
58  else if ( pm_keymatch( argv[argn], "-tb", 2 ) ||
59  pm_keymatch( argv[argn], "-topbottom", 2 ))
60  {
61  topbottom=1;
62  }
63  else
64  pm_usage( usage );
65  ++argn;
66  }
67 
68  if ( argn + 2 <= argc ) { /* Read cols and rows */
69 
70  cols = atoi( argv[argn++] );
71  rows = atoi( argv[argn++] );
72  if ( cols <= 0 || rows <= 0 )
73  pm_usage( usage );
74  }
75 
76  if ( argn < argc )
77  {
78  ifp = pm_openr( argv[argn] );
79  ++argn;
80  }
81  else
82  ifp = stdin;
83 
84  if ( argn != argc )
85  pm_usage( usage );
86 
87  if (cols==0 || topbottom) {
88  buf = pm_read_unknown_size( ifp, &nread );
89  if (cols==0) {
90  rows = cols = (int) sqrt((double) nread);
91  if (rows*cols+headerskip != nread)
92  pm_error( "Not a quadratic input picture" );
93  pos = buf;
94  pm_message( "Image size: %d cols, %d rows", cols, rows);
95  }
96  }
97 
98  for ( i = 0; i < headerskip; ++i )
99  if (nread)
100  pos++;
101  else
102  {
103  val = getc( ifp );
104  if ( val == EOF )
105  pm_error( "EOF / read error" );
106  }
107  toskip = 0.00001;
108 
109  pgm_writepgminit( stdout, cols, rows, (gray) 255, 0 );
111 
112  for ( row = 0; row < rows; ++row)
113  {
114  if (topbottom)
115  pos = buf + (rows-row-1) * cols + headerskip;
116  for ( col = 0, gP = grayrow; col < cols; ++col )
117  if (nread)
118  {
119  *gP++ = *pos++;
120  }
121  else
122  {
123  val = getc( ifp );
124  if ( val == EOF )
125  pm_error( "EOF / read error" );
126  *gP++ = val;
127  }
128  for ( toskip += rowskip; toskip >= 1.0; toskip -= 1.0 )
129  if (nread)
130  {
131  pos++;
132  }
133  else
134  {
135  val = getc( ifp );
136  if ( val == EOF )
137  pm_error( "EOF / read error" );
138  }
139  pgm_writepgmrow( stdout, grayrow, cols, (gray) 255, 0 );
140  }
141 
142  if (nread)
143  free(buf);
144  pm_close( ifp );
145  pm_close( stdout );
146 
147  exit( 0 );
148 }
#define free(a)
Definition: decNumber.cpp:310
double sqrt()
static char usage[]
Definition: giftopnm.c:59
int col
Definition: gsftopk.c:443
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
void exit()
#define EOF
Definition: afmparse.c:59
int atoi(const char *)
double atof(const char *)
#define buf
#define getc
Definition: line.c:39
const int * pos
Definition: combiners.h:905
void pm_usage(char *usage)
Definition: libpbm1.c:343
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
char * pm_read_unknown_size(FILE *file, long *nread)
Definition: libpbm1.c:799
static int rows
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
void pgm_init(int *argcP, argv)
Definition: libpgm1.c:19
void pgm_writepgminit(FILE *file, int cols, int rows, gray maxval, int forceplain)
Definition: libpgm2.c:21
void pgm_writepgmrow(FILE *file, gray *grayrow, int cols, gray maxval, int forceplain)
Definition: libpgm2.c:125
#define pgm_allocrow(cols)
Definition: pgm.h:58
unsigned int gray
Definition: pgm.h:10
#define pm_error
Definition: png22pnm.c:118
#define pm_keymatch(stra, strb, _x)
Definition: png22pnm.c:121
#define pm_close(file)
Definition: png22pnm.c:120
#define pm_message
Definition: png22pnm.c:116
static void topbottom()
static int row
Definition: ps2pk.c:587
int main(int argc, argv)
Definition: rawtopgm.c:18
Definition: strexpr.c:21
#define FILE
Definition: t1stdio.h:34
val
Definition: tex4ht.c:3227
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
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
#define argn