about summary refs log tree commit diff
path: root/converter/pbm/pbmtoln03.c
blob: 07c8629f09ebbd19bf94586dcd177aa4d23ab4e1 (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
/*
From tim@deakin.edu.au Fri May  7 00:18:57 1993
From: Tim Cook <tim@deakin.edu.au>
Date: Fri, 7 May 1993 15:18:34 -0500
X-Mailer: Mail User's Shell (7.2.4 2/2/92)
To: dyson@sunfish.Physics.UIowa.Edu
Subject: Re: DEC LN03+ printer (not postscript) under SunOS (on SS10) ?
Content-Length: 5893

In a message dated 6 May,  9:32, dyson@sunfish.Physics.UIowa.Edu
(Richard L. Dyson) wrote:
> > Just in case anyone is interested, I have a pbmtoln03 utility I wrote
> > when I was mucking about with an LN03+.  If you are interested in
> > printing your bitmaps at 300x300dpi, ask me for the source.
>
> I would be interested.  We still only have LN03+ printers on our VMS
> machines here...

Ok, here goes.  Note that you will need the source from the pbmplus
utilities, because my pbmtoln03 utility uses the library routines that
are a part of pbmplus to read a PBM file (I linked it with libpbm.a).
I have not tested this utility on VMS, but it looks like it should
work.
*/

/* pbmtoln03.c -        Converts a PBM bitmap to DEC LN03 SIXEL bitmap
 *
 * SYNOPSIS
 *      pbmtoln03 [pbm-file]
 *
 * OPTIONS
 *      -l nn   Use "nn" as value for left margin (default 0).
 *      -r nn   Use "nn" as value for right margin (default 2400).
 *      -t nn   Use "nn" as value for top margin (default 0).
 *      -b nn   Use "nn" as value for bottom margin (default 3400).
 *      -f nn   Use "nn" as value for form length (default 3400).
 *
 * Tim Cook, 26 Feb 1992
 * changed option parsing to PBM standards  - Ingo Wilken, 13 Oct 1993
 */

#include <stdio.h>
#include "pbm.h"

FILE *input ;

#ifndef print
#define print(s)        fputs (s, stdout)
#define fprint(f, s)    fputs (s, f)
#endif


static void 
output_sixel_record (unsigned char * record, int width) {

   int i, j, k ;
   unsigned char last_char ;
   int start_repeat = 0 ;
   int repeated ;
   char repeated_str[16] ;
   char *p ;

   /* Do RLE */
   last_char = record[0] ;
   j = 0 ;

   /* This will make the following loop complete */
   record[width] = '\0' ;

   for (i = 1 ; i <= width ; i++) {

      repeated = i - start_repeat ;

      if (record[i] != last_char || repeated >= 32766) {

         /* Repeat has ended */

         if (repeated > 3) {

            /* Do an encoding */
            record[j++] = '!' ;
            sprintf (repeated_str, "%d", i - start_repeat) ;
            for (p = repeated_str ; *p ; p++)
               record[j++] = *p ;
               record[j++] = last_char ; }

         else {
            for (k = 0 ; k < repeated ; k++)
               record[j++] = last_char ; }

         start_repeat = i ;
         last_char = record[i] ; }
      }

   fwrite ((char *) record, j, 1, stdout) ;
   putchar ('-') ;      /* DECGNL (graphics new-line) */
   putchar ('\n') ;
   }


static void 
convert (int width, int height, int format) {

   int i ;
   unsigned char *sixel ;               /* A row of sixels */
   int sixel_row ;

   bit *row = pbm_allocrow (width) ;

   sixel = (unsigned char *) malloc (width + 2) ;

   sixel_row = 0 ;
   while (height--) {
      pbm_readpbmrow (input, row, width, format) ;
      switch (sixel_row) {
         case 0 :
           for (i = 0 ; i < width ; i++)
              sixel[i] = row[i] ;
           break ;
         case 1 :
           for (i = 0 ; i < width ; i++)
              sixel[i] += row[i] << 1 ;
           break ;
         case 2 :
           for (i = 0 ; i < width ; i++)
              sixel[i] += row[i] << 2 ;
           break ;
         case 3 :
           for (i = 0 ; i < width ; i++)
              sixel[i] += row[i] << 3 ;
           break ;
         case 4 :
           for (i = 0 ; i < width ; i++)
              sixel[i] += row[i] << 4 ;
           break ;
         case 5 :
           for (i = 0 ; i < width ; i++)
              sixel[i] += (row[i] << 5) + 077 ;
           output_sixel_record (sixel, width) ;
           break ; }
      if (sixel_row == 5)
         sixel_row = 0 ;
      else
         sixel_row++ ;
      }

   if (sixel_row > 0) {
      /* Incomplete sixel record needs to be output */
      for (i = 0 ; i < width ; i++)
         sixel[i] += 077 ;
      output_sixel_record (sixel, width) ; }
   }



int
main (int argc, char **argv) {
   int argc_copy = argc ;
   char **argv_copy = argv ;
   int argn;
   const char * const usage = "[-left <nn>] [-right <nn>] [-top <nn>] "
       "[-bottom <nn>] [-formlength <nn>] [pbmfile]";

   /* Options */
   /* These defaults are for a DEC LN03 with A4 paper (2400x3400 pixels) */
   const char *opt_left_margin = "0";
   const char *opt_top_margin = opt_left_margin;
   const char *opt_right_margin = "2400";
   const char *opt_bottom_margin = "3400";
   const char *opt_form_length = opt_bottom_margin;

   int width, height, format ;

   pbm_init (&argc_copy, argv_copy) ;

   argn = 1;
   while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
      if( pm_keymatch(argv[argn], "-left", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_left_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-right", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_right_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-top", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_top_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-bottom", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_bottom_margin = argv[argn];
      }
      else
      if( pm_keymatch(argv[argn], "-formlength", 2) ) {
         if( ++argn >= argc )
            pm_usage(usage);
         opt_form_length = argv[argn];
      }
      else
         pm_usage(usage);
      ++argn;
   }

   if( argn < argc ) {
      input = pm_openr( argv[argn] );
      argn++;
   }
   else
      input = stdin;

   if( argn != argc )
      pm_usage(usage);


   /* Initialize pbm file */
   pbm_readpbminit (input, &width, &height, &format) ;

   if (format != PBM_FORMAT && format != RPBM_FORMAT)
      pm_error ("input not in PBM format") ;

/*
 * In explanation of the sequence below:
 *      <ESC>[!p        DECSTR  soft terminal reset
 *      <ESC>[11h       PUM     select unit of measurement
 *      <ESC>[7 I       SSU     select pixel as size unit
 *      <ESC>[?52l      DECOPM  origin is corner of printable area
 *      <ESC>[%s;%ss    DECSLRM left and right margins
 *      <ESC>[%s;%sr    DECSTBM top and bottom margins
 *      <ESC>[%st       DECSLPP form length
 *      <ESC>P0;0;1q            select sixel graphics mode
 *      "1;1            DECGRA  aspect ratio (1:1)
 */

   /* Initialize sixel file */
   printf ("\033[!p\033[11h\033[7 I\033[?52l\033[%s;%ss\033"
           "[%s;%sr\033[%st\033P0;0;1q\"1;1",
      opt_left_margin, opt_right_margin, opt_top_margin, opt_bottom_margin,
      opt_form_length);

   /* Convert data */
   convert (width, height, format) ;

   /* Terminate sixel data */
   print ("\033\\\n") ;

   /* If the program failed, it previously aborted with nonzero completion
      code, via various function calls.
   */
   return 0;
}