Debian Codegraph @

converter/pbm/pbmtoln03.c

raw
1 /*
2 From [email protected] Fri May 7 00:18:57 1993
3 From: Tim Cook <[email protected]>
4 Date: Fri, 7 May 1993 15:18:34 -0500
5 X-Mailer: Mail User's Shell (7.2.4 2/2/92)
6 To: [email protected]
7 Subject: Re: DEC LN03+ printer (not postscript) under SunOS (on SS10) ?
8 Content-Length: 5893
9
10 In a message dated 6 May, 9:32, [email protected]
11 (Richard L. Dyson) wrote:
12 > > Just in case anyone is interested, I have a pbmtoln03 utility I wrote
13 > > when I was mucking about with an LN03+. If you are interested in
14 > > printing your bitmaps at 300x300dpi, ask me for the source.
15 >
16 > I would be interested. We still only have LN03+ printers on our VMS
17 > machines here...
18
19 Ok, here goes. Note that you will need the source from the pbmplus
20 utilities, because my pbmtoln03 utility uses the library routines that
21 are a part of pbmplus to read a PBM file (I linked it with libpbm.a).
22 I have not tested this utility on VMS, but it looks like it should
23 work.
24 */
25
26 /* pbmtoln03.c - Converts a PBM bitmap to DEC LN03 SIXEL bitmap
27 *
28 * SYNOPSIS
29 * pbmtoln03 [pbm-file]
30 *
31 * OPTIONS
32 * -l nn Use "nn" as value for left margin (default 0).
33 * -r nn Use "nn" as value for right margin (default 2400).
34 * -t nn Use "nn" as value for top margin (default 0).
35 * -b nn Use "nn" as value for bottom margin (default 3400).
36 * -f nn Use "nn" as value for form length (default 3400).
37 *
38 * Tim Cook, 26 Feb 1992
39 * changed option parsing to PBM standards - Ingo Wilken, 13 Oct 1993
40 */
41
42 #include <stdio.h>
43
44 #include "mallocvar.h"
45 #include "pbm.h"
46
47
48
49 static void
50 outputSixelRecord(unsigned char * const record,
51 unsigned int const width) {
52
53 unsigned int i, j;
54 unsigned char lastChar;
55 int startRepeat;
56 char repeatedStr[16];
57
58 /* Do RLE */
59 lastChar = record[0];
60 j = 0;
61
62 /* This will make the following loop complete */
63 record[width] = '\0' ;
64
65 for (i = 1, startRepeat = 0; i <= width; ++i) {
66 unsigned int const repeated = i - startRepeat;
67
68 if (record[i] != lastChar || repeated >= 32766) {
69
70 /* Repeat has ended */
71
72 if (repeated > 3) {
73 /* Do an encoding */
74 char * p;
75 record[j++] = '!' ;
76 sprintf(repeatedStr, "%u", i - startRepeat);
77 for (p = repeatedStr; *p; ++p)
78 record[j++] = *p;
79 record[j++] = lastChar;
80 } else {
81 unsigned int k;
82
83 for (k = 0; k < repeated; ++k)
84 record[j++] = lastChar;
85 }
86
87 startRepeat = i ;
88 lastChar = record[i];
89 }
90 }
91
92 fwrite((char *) record, j, 1, stdout) ;
93 putchar('-') ; /* DECGNL (graphics new-line) */
94 putchar('\n') ;
95 }
96
97
98
99 static void
100 convert(FILE * const ifP,
101 unsigned int const width,
102 unsigned int const height,
103 unsigned int const format) {
104
105 unsigned char * sixel; /* A row of sixels */
106 unsigned int sixelRow;
107 bit * bitrow;
108 unsigned int remainingHeight;
109
110 bitrow = pbm_allocrow(width);
111
112 MALLOCARRAY(sixel, width + 2);
113 if (!sixel)
114 pm_error("Unable to allocation %u bytes for a row buffer", width + 2);
115
116 for (remainingHeight = height, sixelRow = 0;
117 remainingHeight > 0;
118 --remainingHeight) {
119
120 unsigned int i;
121
122 pbm_readpbmrow(ifP, bitrow, width, format);
123
124 switch (sixelRow) {
125 case 0 :
126 for (i = 0; i < width; ++i)
127 sixel[i] = bitrow[i] ;
128 break ;
129 case 1 :
130 for (i = 0; i < width; ++i)
131 sixel[i] += bitrow[i] << 1;
132 break ;
133 case 2 :
134 for (i = 0; i < width; ++i)
135 sixel[i] += bitrow[i] << 2;
136 break ;
137 case 3 :
138 for (i = 0; i < width; ++i)
139 sixel[i] += bitrow[i] << 3;
140 break ;
141 case 4 :
142 for (i = 0; i < width; ++i)
143 sixel[i] += bitrow[i] << 4;
144 break ;
145 case 5 :
146 for (i = 0; i < width; ++i)
147 sixel[i] += (bitrow[i] << 5) + 077;
148 outputSixelRecord(sixel, width);
149 break ; }
150 if (sixelRow == 5)
151 sixelRow = 0;
152 else
153 ++sixelRow;
154 }
155
156 if (sixelRow > 0) {
157 /* Incomplete sixel record needs to be output */
158 unsigned int i;
159 for (i = 0; i < width; ++i)
160 sixel[i] += 077;
161 outputSixelRecord(sixel, width);
162 }
163
164 pbm_freerow(bitrow);
165 free(sixel);
166 }
167
168
169
170 int
171 main (int argc, char **argv) {
172 int argc_copy = argc ;
173 char **argv_copy = argv ;
174 int argn;
175 const char * const usage = "[-left <nn>] [-right <nn>] [-top <nn>] "
176 "[-bottom <nn>] [-formlength <nn>] [pbmfile]";
177
178 /* Options */
179 /* These defaults are for a DEC LN03 with A4 paper (2400x3400 pixels) */
180 const char *opt_left_margin = "0";
181 const char *opt_top_margin = opt_left_margin;
182 const char *opt_right_margin = "2400";
183 const char *opt_bottom_margin = "3400";
184 const char *opt_form_length = opt_bottom_margin;
185
186 FILE * ifP;
187 int width, height, format ;
188
189 pbm_init (&argc_copy, argv_copy) ;
190
191 argn = 1;
192 while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) {
193 if( pm_keymatch(argv[argn], "-left", 2) ) {
194 if( ++argn >= argc )
195 pm_usage(usage);
196 opt_left_margin = argv[argn];
197 }
198 else
199 if( pm_keymatch(argv[argn], "-right", 2) ) {
200 if( ++argn >= argc )
201 pm_usage(usage);
202 opt_right_margin = argv[argn];
203 }
204 else
205 if( pm_keymatch(argv[argn], "-top", 2) ) {
206 if( ++argn >= argc )
207 pm_usage(usage);
208 opt_top_margin = argv[argn];
209 }
210 else
211 if( pm_keymatch(argv[argn], "-bottom", 2) ) {
212 if( ++argn >= argc )
213 pm_usage(usage);
214 opt_bottom_margin = argv[argn];
215 }
216 else
217 if( pm_keymatch(argv[argn], "-formlength", 2) ) {
218 if( ++argn >= argc )
219 pm_usage(usage);
220 opt_form_length = argv[argn];
221 }
222 else
223 pm_usage(usage);
224 ++argn;
225 }
226
227 if( argn < argc ) {
228 ifP = pm_openr( argv[argn] );
229 argn++;
230 }
231 else
232 ifP = stdin;
233
234 if( argn != argc )
235 pm_usage(usage);
236
237 pbm_readpbminit (ifP, &width, &height, &format) ;
238
239 /*
240 * In explanation of the sequence below:
241 * <ESC>[!p DECSTR soft terminal reset
242 * <ESC>[11h PUM select unit of measurement
243 * <ESC>[7 I SSU select pixel as size unit
244 * <ESC>[?52l DECOPM origin is corner of printable area
245 * <ESC>[%s;%ss DECSLRM left and right margins
246 * <ESC>[%s;%sr DECSTBM top and bottom margins
247 * <ESC>[%st DECSLPP form length
248 * <ESC>P0;0;1q select sixel graphics mode
249 * "1;1 DECGRA aspect ratio (1:1)
250 */
251
252 /* Initialize sixel file */
253 printf ("\033[!p\033[11h\033[7 I\033[?52l\033[%s;%ss\033"
254 "[%s;%sr\033[%st\033P0;0;1q\"1;1",
255 opt_left_margin, opt_right_margin, opt_top_margin, opt_bottom_margin,
256 opt_form_length);
257
258 /* Convert data */
259 convert (ifP, width, height, format) ;
260
261 /* Terminate sixel data */
262 puts ("\033\\") ;
263
264 /* If the program failed, it previously aborted with nonzero completion
265 code, via various function calls.
266 */
267 return 0;
268 }
269
270
271