about summary refs log tree commit diff
path: root/converter/pbm/pbmtolps.c
blob: 5adef4c83f5da86afd481df8d634522b98d4cede (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
179
180
181
/*
 * pbmtolps -- convert a Portable BitMap into Postscript.  The
 * output Postscript uses lines instead of the image operator to
 * generate a (device dependent) picture which will be imaged
 * much faster.
 *
 * The Postscript path length is constrained to be less that 1000
 * points so that no limits are overrun on the Apple Laserwriter
 * and (presumably) no other printers.
 *
 * To do:
 *      make sure encapsulated format is correct
 *      repitition of black-white strips
 *      make it more device independent (is this possible?)
 *
 * Author:
 *      George Phillips <phillips@cs.ubc.ca>
 *      Department of Computer Science
 *      University of British Columbia
 */

#include <string.h>
#include <stdio.h>

#include "nstring.h"
#include "pbm.h"


static int prev_white = -1;
static int prev_black = -1;
static char cmd = '\0';
static int pointcount = 2;

#ifdef RUN
static int run = 1;
#endif

static char 
morepoints(char cmd, int howmany_pbmtolps) {
    pointcount += 2;
    if (pointcount > 1000) {
        pointcount = 2;
        cmd += 'm' - 'a';
    }
    return(cmd);
}



static void 
addstrip(int const white, 
         int const black) {

    if (cmd) {
#ifdef RUN
        if (white == prev_white && black == prev_black)
            run++;
        else {
            if (run == 1)
#endif
                printf("%d %d %c ", 
                       prev_black, prev_white, morepoints(cmd, 2));
#ifdef RUN
            else
                                /* of course, we need to give a new command */
                printf("%d %d %d %c ",
                       prev_white, prev_black, run,
                       morepoints(cmd + 'f' - 'a', 2 * run));
            run = 1;
        }
#endif
    }

    prev_white = white;
    prev_black = black;
    cmd = 'a';
}



static void 
nextline(void) {
    /* need to check run, should have an outcommand */
    if (cmd)
        printf("%d %d %c\n", prev_black, prev_white, morepoints('c', 3));
    else
        printf("%c\n", morepoints('b', 1));
    cmd = '\0';
}



int
main(int argc, char ** argv) {
    FILE*   fp;
    bit*    bits;
    int             row;
    int             col;
    int         rows;
    int             cols;
    int             format;
    int             white;
    int             black;
    const char*   name;
    float   dpi = 300.0;
    float   sc_rows;
    float   sc_cols;
    int             i;
    const char*   const usage = "[ -dpi n ] [ pbmfile ]";


	pbm_init(&argc, argv);

    i = 1;
    if (i < argc && streq(argv[i], "-dpi")) {
        if (i == argc - 1)
            pm_usage(usage);
        sscanf(argv[i + 1], "%f", &dpi);
        i += 2;
    }

    if (i < argc - 1)
        pm_usage(usage);

    if (i == argc) {
        name = "noname";
        fp = stdin;
    } else {
        name = argv[i];
        fp = pm_openr(name);
    }
    pbm_readpbminit(fp, &cols, &rows, &format);
    bits = pbm_allocrow(cols);

    sc_rows = (float)rows / dpi * 72.0;
    sc_cols = (float)cols / dpi * 72.0;

    puts("%!PS-Adobe-2.0 EPSF-2.0");
    puts("%%Creator: pbmtolps");
    printf("%%%%Title: %s\n", name);
    printf("%%%%BoundingBox: %d %d %d %d\n",
           (int)(305.5 - sc_cols / 2.0),
           (int)(395.5 - sc_rows / 2.0),
           (int)(306.5 + sc_cols / 2.0),
           (int)(396.5 + sc_rows / 2.0));
    puts("%%EndComments");
    puts("%%EndProlog");
    puts("gsave");

    printf("%f %f translate\n", 306.0 - sc_cols / 2.0, 396.0 + sc_rows / 2.0);
    printf("72 %f div dup neg scale\n", dpi);
    puts("/a { 0 rmoveto 0 rlineto } def");
    puts("/b { 0 row 1 add dup /row exch def moveto } def");
    puts("/c { a b } def");
    puts("/m { currentpoint stroke newpath moveto a } def");
    puts("/n { currentpoint stroke newpath moveto b } def");
    puts("/o { currentpoint stroke newpath moveto c } def");
    puts("/row 0 def");
    puts("newpath 0 0 moveto");

    for (row = 0; row < rows; row++) {
        pbm_readpbmrow(fp, bits, cols, format);
        /* output white-strip+black-strip sequences */
        for (col = 0; col < cols; ) {
            for (white = 0; col < cols && bits[col] == PBM_WHITE; col++)
                white++;
            for (black = 0; col < cols && bits[col] == PBM_BLACK; col++)
                black++;

            if (black != 0)
                addstrip(white, black);
        }
        nextline();
    }
    puts("stroke grestore showpage");
    puts("%%Trailer");

    pm_close(fp);

    exit(0);
}