Debian Codegraph @

converter/pbm/pbmtoppa/cutswath.c

raw
1 /* cutswath.c
2 * functions to cut a swath of a PBM file for PPA printers
3 * Copyright (c) 1998 Tim Norman. See LICENSE for details.
4 * 3-15-98
5 *
6 * Mar 15, 1998 Jim Peterson <[email protected]>
7 *
8 * Structured to accommodate both the HP820/1000, and HP720 series.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "mallocvar.h"
16 #include "pm.h"
17
18 #include "ppa.h"
19 #include "ppapbm.h"
20 #include "cutswath.h"
21
22 extern int Width;
23 extern int Height;
24 extern int Pwidth;
25
26
27
28 int
29 cut_pbm_swath(pbm_stat * const pbmP,
30 ppa_stat * const prnP,
31 int const maxlines,
32 ppa_sweep_data * const sweepDataP) {
33 /*----------------------------------------------------------------------------
34 sweepDataP->direction must be set already
35
36 Upon successful completion, sweepDataP->image_data and
37 sweepDataP->nozzle_data have been set to pointers which this routine
38 malloc()'d.
39
40 Upon successful completion, all members of *sweepDataP have been set
41 except direction, vertical_pos, and next.
42
43 Returns: 0 if unsuccessful
44 1 if successful, but with non-printing result (end of page)
45 2 if successful, with printing result
46 -----------------------------------------------------------------------------*/
47 int const shift = prnP->DPI == 300 ? 6 : /* DPI=600 */ 12;
48
49 unsigned char *data, *ppa, *place, *maxplace;
50 int pWidth, width8, pWidth8;
51 int i, left, right, gotNonblank, numlines;
52 int horzpos, hp2;
53 ppa_nozzle_data nozzles[2];
54
55 ppa = NULL;
56
57 /* Safeguard against the user freeing these */
58 sweepDataP->image_data = NULL;
59 sweepDataP->nozzle_data = NULL;
60
61 /* Read the data from the input file */
62 width8 = (pbmP->width + 7) / 8;
63
64 MALLOCARRAY(data, width8 * maxlines);
65 if (!data) {
66 pm_message("could not malloc data storage");
67 return 0;
68 }
69
70 /* Ignore lines that are above the upper margin */
71 while (pbmP->current_line < prnP->top_margin) {
72 if (!pbm_readline(pbmP, data)) {
73 pm_message("could not read top margin");
74 free(data); data=NULL;
75 return 0;
76 }
77 }
78
79 /* Eat all lines that are below the lower margin */
80 if (pbmP->current_line >= Height - prnP->bottom_margin) {
81 while (pbmP->current_line < pbmP->height) {
82 if (!pbm_readline(pbmP, data)) {
83 pm_message("could not clear bottom margin");
84 free(data); data=NULL;
85 return 0;
86 }
87 }
88 free(data); data=NULL;
89 return 1;
90 }
91
92 left = Pwidth - prnP->right_margin / 8;
93 right = prnP->left_margin / 8;
94
95 /* Eat all beginning blank lines and then up to maxlines or lower margin */
96 gotNonblank = numlines = 0;
97 while ((pbmP->current_line < Height-prnP->bottom_margin) &&
98 (numlines < maxlines)) {
99 if (!pbm_readline(pbmP, data + width8 * numlines)) {
100 pm_message("could not read next line");
101 free(data); data=NULL;
102 return 0;
103 }
104 if (!gotNonblank) {
105 unsigned int j;
106 for (j = prnP->left_margin / 8; j < left; ++j) {
107 if (data[j]) {
108 left = j;
109 gotNonblank=1;
110 break;
111 }
112 }
113 }
114 if (gotNonblank) {
115 int newleft, newright;
116 unsigned int i;
117
118 /* Find left-most nonblank */
119 for (i = prnP->left_margin / 8, newleft = left; i < left; ++i) {
120 if (data[width8 * numlines + i]) {
121 newleft = i;
122 break;
123 }
124 }
125 /* Find right-most nonblank */
126 for (i = Pwidth - prnP->right_margin / 8 - 1, newright = right;
127 i >= right;
128 --i) {
129 if (data[width8 * numlines + i]) {
130 newright = i;
131 break;
132 }
133 }
134 ++numlines;
135
136 if (newright < newleft) {
137 pm_message("Ack! newleft=%d, newright=%d, left=%d, right=%d",
138 newleft, newright, left, right);
139 free(data); data=NULL;
140 return 0;
141 }
142
143 /* If the next line might push us over the buffer size, stop here!
144 ignore this test for the 720 right now. Will add better
145 size-guessing for compressed data in the near future!
146 */
147 if (numlines % 2 == 1 && prnP->version != HP720) {
148 int l = newleft;
149 int r = newright;
150 int w;
151
152 --l;
153 r += 2;
154 l *= 8;
155 r *= 8;
156 w = r-l;
157 w = (w + 7) / 8;
158
159 if ((w + 2 * shift) * numlines > prnP->bufsize) {
160 --numlines;
161 pbm_unreadline(pbmP, data + width8 * numlines);
162 break;
163 } else {
164 left = newleft;
165 right = newright;
166 }
167 } else {
168 left = newleft;
169 right = newright;
170 }
171 }
172 }
173
174 if (!gotNonblank) {
175 /* Eat all lines that are below the lower margin */
176 if (pbmP->current_line >= Height - prnP->bottom_margin) {
177 while (pbmP->current_line < pbmP->height) {
178 if (!pbm_readline(pbmP, data)) {
179 pm_message("could not clear bottom margin");
180 free(data); data = NULL;
181 return 0;
182 }
183 }
184 free(data); data = NULL;
185 return 1;
186 }
187 free(data); data = NULL;
188 return 0; /* error, since didn't get to lower margin, yet blank */
189 }
190
191 /* Make sure numlines is even and >= 2 (b/c we have to pass the printer
192 HALF of the number of pins used
193 */
194 if (numlines == 1) {
195 /* There's no way that we only have 1 line and not enough memory, so
196 we're safe to increase numlines here. Also, the bottom margin
197 should be > 0 so we have some lines to read
198 */
199 if (!pbm_readline(pbmP, data + width8 * numlines)) {
200 pm_message("could not read next line");
201 free(data); data = NULL;
202 return 0;
203 }
204 ++numlines;
205 }
206 if (numlines % 2 == 1) {
207 /* Decrease instead of increasing so we don't max out the buffer */
208 --numlines;
209 pbm_unreadline(pbmP, data + width8 * numlines);
210 }
211
212 sweepDataP->vertical_pos = pbmP->current_line;
213
214 /* Change sweep params */
215 left -= 1;
216 right += 2;
217 left *= 8;
218 right *= 8;
219
220 /* Construct the sweep data */
221 pWidth = right - left;
222 pWidth8 = (pWidth + 7) / 8;
223
224 MALLOCARRAY(ppa, (pWidth8 + 2 * shift) * numlines);
225 if (!ppa) {
226 pm_message("could not malloc ppa storage");
227 free(data); data = NULL;
228 return 0;
229 }
230
231 place = ppa;
232
233 /* Place 0's in the first 12 columns */
234 memset(place, 0, numlines/2 * shift);
235 place += numlines/2 * shift;
236
237 if (sweepDataP->direction == right_to_left) { /* right-to-left */
238 int i;
239
240 for (i = pWidth8 + shift - 1; i >= 0; --i) {
241 if (i >= shift) {
242 unsigned int j;
243
244 for (j = 0; j < numlines/2; ++j)
245 *place++ = data[j * 2 * width8 + i + left / 8 - shift];
246 } else {
247 memset(place, 0, numlines/2);
248 place += numlines/2;
249 }
250
251 if (i < pWidth8) {
252 unsigned int j;
253
254 for (j = 0; j < numlines/2; ++j)
255 *place++ = data[(j * 2 + 1) * width8 + i + left / 8];
256 } else {
257 memset(place, 0, numlines/2);
258 place += numlines/2;
259 }
260 }
261 } else {
262 /* sweep_data->direction == left_to_right */
263 unsigned int i;
264 for (i = 0; i < pWidth8 + shift; ++i) {
265 if (i < pWidth8) {
266 unsigned int j;
267 for (j = 0; j < numlines/2; ++j)
268 *place++ = data[(j * 2 + 1) * width8 + i + left / 8];
269 } else {
270 memset(place, 0, numlines/2);
271 place += numlines/2;
272 }
273
274 if (i >= shift) {
275 unsigned int j;
276 for (j = 0; j < numlines/2; ++j)
277 *place++ = data[j * 2 * width8 + i + left / 8 - shift];
278 } else {
279 memset(place, 0, numlines/2);
280 place += numlines/2;
281 }
282 }
283 }
284
285 /* Done with data */
286 free(data); data=NULL;
287
288 /* Place 0's in the last 12 columns */
289 memset(place, 0, numlines/2 * shift);
290 place += numlines/2 * shift;
291 maxplace = place;
292
293 /* Create sweep data */
294 sweepDataP->image_data = ppa;
295 sweepDataP->data_size = maxplace-ppa;
296 sweepDataP->in_color = False;
297
298 /*
299 horzpos = left*600/prn->DPI + (sweep_data->direction==left_to_right ? 0*600/prn->DPI : 0);
300 */
301 horzpos = left * 600 / prnP->DPI;
302
303 hp2 = horzpos + (pWidth8 + 2 * shift) * 8 * 600 /prnP->DPI;
304
305 sweepDataP->left_margin = horzpos;
306 sweepDataP->right_margin = hp2 + prnP->marg_diff;
307
308 for (i = 0; i < 2; ++i) {
309 nozzles[i].DPI = prnP->DPI;
310
311 nozzles[i].pins_used_d2 = numlines/2;
312 nozzles[i].unused_pins_p1 = 301 - numlines;
313 nozzles[i].first_pin = 1;
314
315 if (i == 0) {
316 nozzles[i].left_margin = horzpos + prnP->marg_diff;
317 nozzles[i].right_margin = hp2 + prnP->marg_diff;
318 if (sweepDataP->direction == right_to_left) /* 0 */
319
320 nozzles[i].nozzle_delay = prnP->right_to_left_delay[0];
321 else /* 6 */
322 nozzles[i].nozzle_delay = prnP->left_to_right_delay[0];
323 } else {
324 nozzles[i].left_margin = horzpos;
325 nozzles[i].right_margin = hp2;
326
327 if (sweepDataP->direction == right_to_left) /* 2 */
328 nozzles[i].nozzle_delay = prnP->right_to_left_delay[1];
329 else /* 0 */
330 nozzles[i].nozzle_delay = prnP->left_to_right_delay[1];
331 }
332 }
333
334 sweepDataP->nozzle_data_size = 2;
335 MALLOCARRAY_NOFAIL(sweepDataP->nozzle_data, 2);
336 sweepDataP->nozzle_data[0] = nozzles[0];
337 sweepDataP->nozzle_data[1] = nozzles[1];
338
339 return 2;
340 }
341
342
343