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)  

asciitopgm.c
Go to the documentation of this file.
1 /* asciitopgm.c - read an ASCII graphics file and produce a portable graymap
2 **
3 ** Copyright (C) 1989 by Wilson H. Bent, Jr
4 **
5 ** - Based on fstopgm.c and other works which bear the following notice:
6 ** Copyright (C) 1989 by Jef Poskanzer.
7 **
8 ** Permission to use, copy, modify, and distribute this software and its
9 ** documentation for any purpose and without fee is hereby granted, provided
10 ** that the above copyright notice appear in all copies and that both that
11 ** copyright notice and this permission notice appear in supporting
12 ** documentation. This software is provided "as is" without express or
13 ** implied warranty.
14 */
15 
16 #include <string.h>
17 #include "pgm.h"
18 
19 static char gmap [128] = {
20 /*00 nul-bel*/ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21 /*08 bs -si */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22 /*10 dle-etb*/ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23 /*18 can-us */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 /*20 sp - ' */ 0x00, 0x21, 0x1b, 0x7f, 0x70, 0x25, 0x20, 0x0a,
25 /*28 ( - / */ 0x11, 0x11, 0x2a, 0x2b, 0x0b, 0x13, 0x04, 0x10,
26 /*30 0 - 7 */ 0x30, 0x28, 0x32, 0x68, 0x39, 0x35, 0x39, 0x16,
27 /*38 8 - ? */ 0x38, 0x39, 0x14, 0x15, 0x11, 0x1c, 0x11, 0x3f,
28 /*40 @ - G */ 0x40, 0x49, 0x52, 0x18, 0x44, 0x3c, 0x38, 0x38,
29 /*48 H - O */ 0x55, 0x28, 0x2a, 0x70, 0x16, 0x7f, 0x70, 0x14,
30 /*50 P - W */ 0x60, 0x20, 0x62, 0x53, 0x1a, 0x55, 0x36, 0x57,
31 /*58 X - _ */ 0x50, 0x4c, 0x5a, 0x24, 0x10, 0x24, 0x5e, 0x13,
32 /*60 ` - g */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33 /*68 h - o */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x2a,
34 /*70 p - w */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
35 /*78 x -del*/ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
36 };
37 
38 static gray maxval = 127;
39 
40 int
42 int argc;
43 char *argv[];
44  {
45  FILE *ifd;
46  register gray **grays;
47  int argn, row;
48  register int c, i;
49  int rows = 0, cols = 0;
50  int divisor = 1;
51  int warned = 0;
52  int *obuf;
53  char *usage = "[-d <val>] height width [asciifile]";
54 
55  pgm_init( &argc, argv );
56 
57  argn = 1;
58 
59  if ( argc < 3 || argc > 6 )
60  pm_usage( usage );
61 
62  if ( argv[argn][0] == '-' )
63  {
64  if ( strcmp( argv[argn], "-d" ) == 0 )
65  {
66  if ( argc == argn + 1 )
67  pm_usage( usage );
68  if ( sscanf( argv[argn+1], "%d", &divisor ) != 1 )
69  pm_usage( usage );
70  argn += 2;
71  }
72  else
73  pm_usage( usage );
74  }
75 
76  if ( sscanf( argv[argn++], "%d", &rows ) != 1 )
77  pm_usage( usage );
78  if ( sscanf( argv[argn++], "%d", &cols ) != 1 )
79  pm_usage( usage );
80  if ( rows < 1 )
81  pm_error( "height is less than 1", 0,0,0,0,0 );
82  if ( cols < 1 )
83  pm_error( "width is less than 1", 0,0,0,0,0 );
84 
85  if ( argc > argn + 1 )
86  pm_usage( usage );
87 
88  if ( argc == argn + 1 )
89  ifd = pm_openr( argv[argn] );
90  else
91  ifd = stdin;
92 
93  /* Here's where the work is done:
94  * - Usually, the graymap value of the input char is summed into grays
95  * - For a 'normal' newline, the current row is adjusted by the divisor
96  * and the current row is incremented
97  * - If the first char in the input line is a '+', then the current row
98  * stays the same to allow 'overstriking'
99  * NOTE that we assume the user specified a sufficiently large width!
100  */
101  if ((obuf = (int *) calloc (cols, sizeof (int))) == NULL)
102  pm_error( "malloc error", 0,0,0,0,0 ); /* wimpy message! */
103  grays = pgm_allocarray( cols, rows );
104  row = i = 0;
105  while ( row < rows )
106  {
107  switch (c = getc (ifd))
108  {
109  case EOF:
110  goto line_done;
111  case '\n':
112  if ((c = getc (ifd)) == EOF)
113  goto line_done;
114  if (c == '+')
115  i = 0;
116  else
117  {
118  line_done:
119  for (i = 0; i < cols; ++i)
120  grays[row][i] = maxval - (obuf[i] / divisor);
121  bzero (obuf, cols * sizeof (int));
122  i = 0;
123  ++row;
124  if ( row >= rows )
125  break;
126  if (c != EOF)
127  obuf[i++] += gmap[c];
128  }
129  break;
130  default:
131  if (c > 0x7f) /* !isascii(c) */
132  {
133  if (!warned)
134  {
135  fprintf (stderr, "(Warning: non-ASCII char(s) in input)\n");
136  warned = 1;
137  }
138  c &= 0x7f; /* toascii(c) */
139  }
140  obuf[i++] += gmap[c];
141  break;
142  }
143  }
144  pm_close( ifd );
145 
146  pgm_writepgm( stdout, grays, cols, rows, maxval, 0 );
147 
148  exit( 0 );
149  }
static char gmap[128]
Definition: asciitopgm.c:19
static gray maxval
Definition: asciitopgm.c:38
int main(int argc, argv)
Definition: asciitopgm.c:41
int strcmp()
Definition: coll.cpp:143
int sscanf()
static char usage[]
Definition: giftopnm.c:59
#define c(n)
Definition: gpos-common.c:150
#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
voidp calloc()
#define getc
Definition: line.c:39
#define fprintf
Definition: mendex.h:64
#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
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_writepgm(FILE *file, gray **grays, int cols, int rows, gray maxval, int forceplain)
Definition: libpgm2.c:144
#define pgm_allocarray(cols, rows)
Definition: pgm.h:57
unsigned int gray
Definition: pgm.h:10
#define pm_error
Definition: png22pnm.c:118
#define pm_close(file)
Definition: png22pnm.c:120
static int row
Definition: ps2pk.c:587
static uint8_t obuf[8192]
Definition: buildvm_lib.c:13
#define FILE
Definition: t1stdio.h:34
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
static int warned
Definition: zdump.c:229
#define argn