about summary refs log tree commit diff
path: root/converter/pbm/pbmtogo.c
blob: 23b2ee9a3d26975f3370e2050e3d107d58ba4a07 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* pbmtogo.c - read a PBM image and produce a GraphOn terminal raster file
**      
**      Rev 1.1 was based on pbmtolj.c
**
**      Bo Thide', Swedish Institute of Space Physics, bt@irfu.se
**                                 
**
** $Log:        pbmtogo.c,v $
 * Revision 1.5  89/11/25  00:24:12  00:24:12  root (Bo Thide)
 * Bug found: The byte after 64 repeated bytes sometimes lost. Fixed.
 * 
 * Revision 1.4  89/11/24  14:56:04  14:56:04  root (Bo Thide)
 * Fixed the command line parsing since pbmtogo now always uses 2D
 * compression.  Added a few comments to the source.
 * 
 * Revision 1.3  89/11/24  13:43:43  13:43:43  root (Bo Thide)
 * Added capability for > 63 repeated bytes and > 62 repeated lines in
 * the 2D compression scheme.
 * 
 * Revision 1.2  89/11/15  01:04:47  01:04:47  root (Bo Thide)
 * First version that works reasonably well with GraphOn 2D runlength
 * encoding/compression.
 * 
 * Revision 1.1  89/11/02  23:25:25  23:25:25  root (Bo Thide)
 * Initial revision
 * 
**
** Copyright (C) 1988, 1989 by Jef Poskanzer, Michael Haberler, and Bo Thide'.
**
** 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 <assert.h>
#include <stdio.h>
#include <string.h>

#include "pm_c_util.h"
#include "pbm.h"

#define GRAPHON_WIDTH 1056 /* GraphOn has 1056 bit wide raster lines */
#define GRAPHON_WIDTH_BYTES (GRAPHON_WIDTH / 8)
#define REPEAT_CURRENT_LINE_MASK        0x00 
#define SKIP_AND_PLOT_MASK              0x40 
#define REPEAT_PLOT_MASK                0x80 
#define PLOT_ARBITRARY_DATA_MASK        0xc0 
#define MAX_REPEAT 64

static unsigned char * scanlineptr;
    /* Pointer to current scan line byte */

static int item, bitsperitem, bitshift;

static void
putinit(void) {

    /* Enter graphics window */
    printf("\0331");

    /* Erase graphics window */
    printf("\033\014");

    /* Set graphics window in raster mode */
    printf("\033r");

    /* Select standard Tek coding **/
    printf("\033[=11l");

    bitsperitem = 1;
    item = 0;
    bitshift = 7;
}



static void
putitem(void) {

    *scanlineptr++ = item;
    bitsperitem = 0;
    item = 0;
}



static void
putbit(bit const b) {

    if (b == PBM_BLACK)
        item += 1 << bitshift;

    bitshift--;

    if (bitsperitem == 8)
    {
        putitem();
        bitshift = 7;
    }
    bitsperitem++;
}



static void
putrest(void) {

    if (bitsperitem > 1)
        putitem();

    /* end raster downloading */
    printf("\033\134");

    /* Exit raster mode */
    printf("\033t");

    /* Exit graphics window
       printf("\0332"); */
}



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

    FILE * ifP;
    bit * bitrow;
    bit * bP;
    int rows, cols, format, rucols, padright, row, col;
    int nbyte, bytesperrow, ecount, ucount, nout, i, linerepeat;
    int olditem;
    unsigned char oldscanline[GRAPHON_WIDTH_BYTES];
    unsigned char newscanline[GRAPHON_WIDTH_BYTES];
    unsigned char diff[GRAPHON_WIDTH_BYTES];
    unsigned char buffer[GRAPHON_WIDTH_BYTES];
    unsigned char outbuffer[2*(GRAPHON_WIDTH_BYTES+1)];     /* Worst case. */

    pm_proginit(&argc, argv);

    if (argc-1 == 0)
      ifP = stdin;
    else if (argc-1 == 1)
      ifP = pm_openr(argv[1]);
    else
        pm_error("There is at most one argument: input file name.  "
                 "You specified %u", argc-1);

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

    if (cols > GRAPHON_WIDTH)
        pm_error("Image is wider (%u pixels) than a Graphon terminal "
                 "(%u pixels)", cols, GRAPHON_WIDTH);

    bitrow = pbm_allocrow(cols);

    /* Round cols up to the nearest multiple of 8. */
    rucols = ( cols + 7 ) / 8;
    bytesperrow = rucols;       /* GraphOn uses bytes */
    rucols = rucols * 8;
    padright = rucols - cols;

    for (i = 0; i < GRAPHON_WIDTH_BYTES; ++i )
      buffer[i] = oldscanline[i] = 0;
    putinit();

    /* Start donwloading screen raster */
    printf("\033P0;1;0;4;1;%d;%d;1!R1/", rows, rucols);

    linerepeat = 63; /*  63 means "Start new picture" */
    nout = 0;  /* To prevent compiler warning */
    for (row = 0; row < rows; row++) {
        /* Store scan line data in the new scan line vector */
        scanlineptr = newscanline;
        pbm_readpbmrow(ifP, bitrow, cols, format);
        /* Transfer raster graphics */
        for (col = 0, bP = bitrow; col < cols; col++, bP++)
          putbit(*bP);
        for (col = 0; col < padright; col++)
          putbit(0);

        assert(bytesperrow <= GRAPHON_WIDTH_BYTES);
        
        /* XOR data from the new scan line with data from old scan line */
        for (i = 0; i < bytesperrow; i++)
          diff[i] = oldscanline[i]^newscanline[i];
        
        /*
         ** If the difference map is different from current internal buffer,
         ** encode the difference and put it in the output buffer. 
         ** Else, increase the counter for the current buffer by one.
         */
        
        if ((memcmp(buffer, diff, bytesperrow) != 0) || (row == 0)) {
            /*    
             **Since the data in the buffer has changed, send the
             **scan line repeat count to cause the old line(s) to
             **be plotted on the screen, copy the new data into
             **the internal buffer, and reset the counters.  
             */
              
            putchar(linerepeat);
            for (i = 0; i < bytesperrow; ++i)
              buffer[i] = diff[i];
            nbyte = 0;          /* Internal buffer byte counter */
            nout = 0;           /* Output buffer byte counter */
              
            /* Run length encode the new internal buffr (= difference map) */
            while (TRUE) {
                ucount = 0;     /* Unique items counter */
                do              /* Find unique patterns */
                  {
                      olditem = buffer[nbyte++];
                      ucount++;
                  } while (nbyte < bytesperrow && (olditem != buffer[nbyte])
                           && (ucount < MIN(bytesperrow, MAX_REPEAT)));
                  
                if ((ucount != MAX_REPEAT) && (nbyte != bytesperrow)) {
                    /* Back up to the last truly unique pattern */
                    ucount--;
                    nbyte--;
                }

                if (ucount > 0) { 
                    /* Output the unique patterns */
                    outbuffer[nout++] = 
                      (ucount-1) | PLOT_ARBITRARY_DATA_MASK;
                    for (i = nbyte-ucount; i < nbyte; i++)
                      outbuffer[nout++] = buffer[i];
                }

                /*
                 ** If we already are at the end of the current scan
                 ** line, skip the rest of the encoding and start
                 ** with a new scan line.  
                 */

                if (nbyte >= bytesperrow)
                  goto nextrow;
                  
                ecount = 0;     /* Equal items counter */
                do              /* Find equal patterns */
                  {
                      olditem = buffer[nbyte++];
                      ecount++;
                  } while (nbyte < bytesperrow && (olditem == buffer[nbyte])
                           && (ecount < MIN(bytesperrow, MAX_REPEAT)));
                  
                if (ecount > 1) {
                    /* More than 1 equal pattern */
                    if (olditem == '\0') {
                        /* White patterns */
                        if (nbyte >= bytesperrow-1) {
                            /* No more valid data ahead */
                            outbuffer[nout++] = 
                              (ecount-2) | SKIP_AND_PLOT_MASK;
                            outbuffer[nout++] = buffer[nbyte-1];
                        } 
                        else { 
                            /* More valid data ahead */
                            outbuffer[nout++] = 
                              (ecount-1) | SKIP_AND_PLOT_MASK;
                            outbuffer[nout++] = buffer[nbyte++];
                        } 
                    }
                    else { 
                        /* Non-white patterns */
                        outbuffer[nout++] = (ecount-1) | REPEAT_PLOT_MASK;
                        outbuffer[nout++] = olditem;
                    } /* if (olditem == '\0') */
                } /* if (ecount > 1) */
                else
                  nbyte--;      /* No equal items found */
                  
                if (nbyte >= bytesperrow)
                  goto nextrow;
            } /* while (TRUE) */
              
          nextrow: printf("%d/", nout+1); /* Total bytes to xfer = nout+1 */
            fflush(stdout);

            /* Output the plot data */
            fwrite(outbuffer, 1, nout, stdout);

            /* Reset the counters */
            linerepeat = 0;
        } else {
            linerepeat++;
            if (linerepeat == 62) /* 62 lines max, then restart */
              {
                  putchar(linerepeat);
                  printf("%d/", nout+1);
                  fflush(stdout);
                  fwrite(outbuffer, 1, nout, stdout);
                  linerepeat = 0;
              }
        }
        
        /* Now we are ready for a new scan line */
        for (i = 0; i < bytesperrow; ++i)
          oldscanline[i] = newscanline[i];
    }
    putchar(linerepeat);        /* For the last line(s) to be plotted */
    pm_close(ifP);
    putrest();

    return 0;
}