about summary refs log tree commit diff
path: root/converter/pbm/pbmto10x.c
blob: 99f35847ed83879fcc16649acc4ec23807ecbc10 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* pbmto10x.c - read a portable bitmap and produce a Gemini 10X printer file
**
** Copyright (C) 1990, 1994 by Ken Yap
**
** 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.
**
** Modified to shorten stripes and eliminate blank stripes. Dec 1994.
*/

#include <stdbool.h>

#include "pbm.h"
#include "mallocvar.h"

#define LOW_RES_ROWS    8       /* printed per pass */
#define HIGH_RES_ROWS   16      /* printed per pass */



static void
outstripe(char * const stripe,
          char * const sP,
          int    const reschar) {

    char * p;

    p = sP;  /* initial value */

    /* scan backwards, removing empty columns */
    while (p != stripe)
        if (*--p != 0) {
            ++p;
            break;
        }

    {
        unsigned int const ncols = p - stripe;

        if (ncols > 0) {
            printf("\033%c%c%c", reschar, ncols % 256, ncols / 256);
            fwrite(stripe, sizeof(char), ncols, stdout);
        }
    }
    putchar('\n');          /* flush buffer */
}



static void
res_60x72(FILE * const ifP,
          int    const rows,
          int    const cols,
          int    const format) {

    int row;
    unsigned int i;
    bit * bitrows[LOW_RES_ROWS];
    char *stripe;
    char *sP;

    MALLOCARRAY(stripe, cols);
    if (stripe == NULL)
        pm_error("Unable to allocate %u bytes for a stripe buffer.",
                 (unsigned)(cols * sizeof(stripe[0])));

    for (i = 0; i < LOW_RES_ROWS; ++i)
        bitrows[i] = pbm_allocrow(cols);

    printf("\033A\010");        /* '\n' = 8/72 */

    for (row = 0, sP = stripe; row < rows; row += LOW_RES_ROWS, sP = stripe) {
        unsigned int col;
        unsigned int i;
        unsigned int npins;
        bit * bP[LOW_RES_ROWS];

        if (row + LOW_RES_ROWS <= rows)
            npins = LOW_RES_ROWS;
        else
            npins = rows - row;

        for (i = 0; i < npins; ++i)
            pbm_readpbmrow(ifP, bP[i] = bitrows[i], cols, format);

        for (col = 0; col < cols; ++col) {
            unsigned int item;

            item = 0;
            for (i = 0; i < npins; ++i)
                if (*(bP[i]++) == PBM_BLACK)
                    item |= 1 << (7 - i);
            *sP++ = item;
        }
        outstripe(stripe, sP, 'K');
    }
    printf("\033@");

    for (i = 0; i < LOW_RES_ROWS; ++i)
        pbm_freerow(bitrows[i]);

    free(stripe);
}



static void
res_120x144(FILE * const ifP,
            int    const rows,
            int    const cols,
            int    const format) {

    unsigned int i;
    int row;
    char *stripe;
    char * sP;
    bit * bitrows[HIGH_RES_ROWS];

    MALLOCARRAY(stripe, cols);
    if (stripe == NULL)
        pm_error("Unable to allocate %u bytes for a stripe buffer.",
                 (unsigned)(cols * sizeof(stripe[0])));

    for (i = 0; i < HIGH_RES_ROWS; ++i)
        bitrows[i] = pbm_allocrow(cols);

    printf("\0333\001");            /* \n = 1/144" */

    for (row = 0, sP = stripe; row < rows; row += HIGH_RES_ROWS, sP = stripe) {
        unsigned int i;
        unsigned int col;
        bit * bP[HIGH_RES_ROWS];
        unsigned int npins;

        if (row + HIGH_RES_ROWS <= rows)
            npins = HIGH_RES_ROWS;
        else
            npins = rows - row;
        for (i = 0; i < npins; ++i)
            pbm_readpbmrow(ifP, bP[i] = bitrows[i], cols, format);
        for (col = 0; col < cols; ++col) {
            unsigned int pin;
            unsigned int item;
            item = 0;
            /* even rows */
            for (pin = i = 0; i < npins; i += 2, ++pin)
                if (*(bP[i]++) == PBM_BLACK)
                    item |= 1 << (7 - pin);
            *sP++ = item;
        }
        outstripe(stripe, sP, 'L');
        for (col = 0, sP = stripe; col < cols; ++col) {
            unsigned int pin;
            unsigned int item;
            item = 0;
            /* odd rows */
            for (i = 1, pin = 0; i < npins; i += 2, ++pin)
                if (*(bP[i]++) == PBM_BLACK)
                    item |= 1 << (7 - pin);
            *sP++ = item;
        }
        outstripe(stripe, sP, 'L');
        printf("\033J\016");        /* 14/144 down, \n did 1/144 */
    }
    printf("\033@");

    for (i = 0; i < LOW_RES_ROWS; ++i)
        pbm_freerow(bitrows[i]);

    free(stripe);
}



int
main(int argc, const char ** argv) {

    const char * fname;
    static FILE * ifP;
    int rows, cols, format;

    bool isHighRes;

    pm_proginit(&argc, argv);

    isHighRes = false;  /* initial assumption */
    if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h') {
        isHighRes = true;
        --argc;
        ++argv;
    }
    if (argc-1 > 1)
        pm_error("Too many arguments.  Only argument is file name");
    else if (argc-1 == 1)
        fname = argv[1];
    else
        fname = "-";

    ifP = pm_openr(fname);

    pbm_readpbminit(ifP, &cols, &rows, &format);

    if (isHighRes)
        res_120x144(ifP, rows, cols, format);
    else
        res_60x72(ifP, rows, cols, format);

    pm_close(ifP);

    return 0;
}