about summary refs log tree commit diff
path: root/converter/pgm/asciitopgm.c
blob: a3a5bd48b7e7bd87d6595b0b3c67e3d991f918e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* asciitopgm.c - read an ASCII graphics file and produce a portable graymap
**
** Copyright (C) 1989 by Wilson H. Bent, Jr
**
** - Based on fstopgm.c and other works which bear the following notice:
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>

#include "pm_c_util.h"
#include "pgm.h"
#include "mallocvar.h"
#include "nstring.h"

static char gmap [128] = {
/*00 nul-bel*/  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/*08 bs -si */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/*10 dle-etb*/  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/*18 can-us */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/*20 sp - ' */  0x00, 0x21, 0x1b, 0x7f, 0x70, 0x25, 0x20, 0x0a,
/*28  ( - / */  0x11, 0x11, 0x2a, 0x2b, 0x0b, 0x13, 0x04, 0x10,
/*30  0 - 7 */  0x30, 0x28, 0x32, 0x68, 0x39, 0x35, 0x39, 0x16,
/*38  8 - ? */  0x38, 0x39, 0x14, 0x15, 0x11, 0x1c, 0x11, 0x3f,
/*40  @ - G */  0x40, 0x49, 0x52, 0x18, 0x44, 0x3c, 0x38, 0x38,
/*48  H - O */  0x55, 0x28, 0x2a, 0x70, 0x16, 0x7f, 0x70, 0x14,
/*50  P - W */  0x60, 0x20, 0x62, 0x53, 0x1a, 0x55, 0x36, 0x57,
/*58  X - _ */  0x50, 0x4c, 0x5a, 0x24, 0x10, 0x24, 0x5e, 0x13,
/*60  ` - g */  0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
/*68  h - o */  0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x2a,
/*70  p - w */  0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
/*78  x -del*/  0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
};

static gray maxval = 127;

int
main( argc, argv )
    int argc;
    char *argv[];
{
    FILE *ifd;
    register gray **grays;
    int argn, row;
    register int c, i;
    int rows = 0, cols = 0;
    int divisor = 1;
    bool warned;
    int *obuf;
    const char * const usage = "[-d <val>] height width [asciifile]";
    char trunc;

    pgm_init( &argc, argv );

    warned = FALSE;

    argn = 1;

    if ( argc < 3 || argc > 6 )
        pm_usage( usage );

    if ( argv[argn][0] == '-' )
    {
        if ( streq( argv[argn], "-d" ) )
        {
            if ( argc == argn + 1 )
                pm_usage( usage );
            if ( sscanf( argv[argn+1], "%d", &divisor ) != 1 )
                pm_usage( usage );
            argn += 2;
        }
        else
            pm_usage( usage );
    }

    if ( sscanf( argv[argn++], "%d", &rows ) != 1 )
        pm_usage( usage );
    if ( sscanf( argv[argn++], "%d", &cols ) != 1 )
        pm_usage( usage );
    if ( rows < 1 )
        pm_error( "height is less than 1" );
    if ( cols < 1 )
        pm_error( "width is less than 1" );

    if ( argc > argn + 1 )
        pm_usage( usage );

    if ( argc == argn + 1 )
        ifd = pm_openr( argv[argn] );
    else
        ifd = stdin;

    /* Here's where the work is done:
     * - Usually, the graymap value of the input char is summed into grays
     * - For a 'normal' newline, the current row is adjusted by the divisor
     *   and the current row is incremented
     * - If the first char in the input line is a '+', then the current row
     *   stays the same to allow 'overstriking'
     * NOTE that we assume the user specified a sufficiently large width!
     */
    MALLOCARRAY( obuf, cols );
    if ( obuf == NULL )
        pm_error( "Unable to allocate memory for %d columns.", cols);
    else {
        unsigned int col;
        for (col = 0; col < cols; ++col) obuf[col] = 0;
    }
    grays = pgm_allocarray( cols, rows );
    row = i = trunc = 0;
    while ( row < rows )
    {
        switch (c = getc (ifd))
        {
        case EOF:
            goto line_done;
        case '\n':
	newline:
	    trunc = 0;
            if ((c = getc (ifd)) == EOF)
                goto line_done;
            if (c == '+')
                i = 0;
            else
            {
            line_done:
                for (i = 0; i < cols; ++i)
                    grays[row][i] = maxval - (obuf[i] / divisor);
                {
                    unsigned int col;
                    for (col = 0; col < cols; ++col) obuf[col] = 0;
                }
                i = 0;
                ++row;
                if ( row >= rows )
                    break;
		if (c == '\n')
		    goto newline;
                else if (c != EOF)
                    obuf[i++] += gmap[c];
            }
            break;
        default:
	    if (i == cols)
	    {
		if (! trunc)
		{
		    pm_message("Warning: row %d being truncated at %d columns",
			       row+1, cols);
		    trunc = 1;
		}
		continue;
	    }
            if (c > 0x7f)       /* !isascii(c) */
            {
                if (!warned)
                {
                    pm_message("Warning: non-ASCII char(s) in input");
                    warned = TRUE;
                }
                c &= 0x7f;      /* toascii(c) */
            }
            obuf[i++] += gmap[c];
            break;
        }
    }
    pm_close( ifd );

    pgm_writepgm( stdout, grays, cols, rows, maxval, 0 );

    return 0;
}