1 /**
2 ** to_pbm.c: PortableBitMap (PBM) converter part of project "hp2xx"
3 **
4 ** 92/04/14 V 1.00 CHL Originating: Copied from to_pcx.c and to_gnu.c
5 ** 92/04/16 V 1.01 CHL Better error handling
6 ** 92/05/17 V 1.01b HWW Output to stdout if outfile == '-'
7 ** 92/05/19 V 1.01c HWW Abort if color mode
8 ** 94/02/10 V 2.00 IJMP Add colour/use binary mode
9 ** (IJMP = Ian_MacPhedran@engr.usask.ca)
10 ** 94/02/14 V 2.10 HWW Adapted to changes in hp2xx.h
11 **/
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "bresnham.h"
16 #include "hp2xx.h"
17 #include "pendef.h"
18 #define GGE >>=
19 #define MAXOUTPUTROWS 70
20
21
22
PicBuf_to_PBM(const GEN_PAR * pg,const OUT_PAR * po)23 int PicBuf_to_PBM(const GEN_PAR * pg, const OUT_PAR * po)
24 {
25 FILE *fd;
26 int row_c, byte_c, x;
27 const RowBuf *row;
28 const PicBuf *pb;
29 #if 0
30 #ifdef PBMascii
31 int bit, row_count = 0;
32 char *ppm[] = { "1 1 1", "0 0 0", "1 0 0", "0 1 0",
33 "0 0 1", "0 1 1", "1 0 1", "1 1 0"
34 };
35 #else
36 int ppm[][3] =
37 { {255, 255, 255}, {0, 0, 0}, {255, 0, 0}, {0, 255, 0},
38 {0, 0, 255}, {0, 255, 255}, {255, 0, 255}, {255, 255, 0}
39 };
40 #endif /*PBMascii */
41 #endif /* used ? */
42 int colour;
43
44 if (pg == NULL || po == NULL)
45 return ERROR;
46 pb = po->picbuf;
47 if (pb == NULL)
48 return ERROR;
49
50 if (!pg->quiet)
51 Eprintf("\nWriting PBM output: %s\n", po->outfile);
52 if (*po->outfile != '-') {
53
54 #ifdef VAX
55 if ((fd =
56 fopen(po->outfile, WRITE_BIN, "rfm=var",
57 "mrs=512")) == NULL)
58 #else
59 if ((fd = fopen(po->outfile, WRITE_BIN)) == NULL)
60 #endif
61 goto ERROR_EXIT;
62 } else
63 fd = stdout;
64
65 if (pb->depth > 1) {
66 #ifdef PBMascii
67 if (fprintf(fd, "P3\n") == EOF)
68 goto ERROR_EXIT;
69 if (fprintf(fd, "%d %d\n1\n", pb->nc, pb->nr) == EOF)
70 goto ERROR_EXIT;
71 #else
72 if (fprintf(fd, "P6\n") == EOF)
73 goto ERROR_EXIT;
74 if (fprintf(fd, "%d %d\n255\n", pb->nc, pb->nr) == EOF)
75 goto ERROR_EXIT;
76 #endif /* PBMascii */
77
78 for (row_c = 0; row_c < pb->nr; row_c++) {
79 row = get_RowBuf(pb, pb->nr - row_c - 1);
80 if (row == NULL)
81 continue;
82
83 for (x = 0; x < pb->nc; x++) {
84 colour = index_from_RowBuf(row, x, pb);
85 #ifdef PBMascii
86 if (fprintf(fd, "%s", ppm[colour]) == EOF)
87 goto ERROR_EXIT;
88 #else
89 /* if (fprintf(fd,"%c%c%c",ppm[colour][0],ppm[colour][1],
90 ppm[colour][2]) == EOF) goto ERROR_EXIT;
91 */
92 if (fprintf
93 (fd, "%c%c%c", pt.clut[colour][0],
94 pt.clut[colour][1],
95 pt.clut[colour][2]) == EOF)
96 goto ERROR_EXIT;
97 #endif /* PBMascii */
98 #ifdef PBMascii
99 row_count++;
100 if (row_count >= MAXOUTPUTROWS) {
101 row_count = 0;
102 if (putc('\n', fd) == EOF)
103 goto ERROR_EXIT;
104 } else {
105 if (putc(' ', fd) == EOF)
106 goto ERROR_EXIT;
107 }
108 #endif /* PBMascii */
109 }
110 if ((!pg->quiet) && (row_c % 10 == 0))
111 /* For the impatients among us ... */
112 Eprintf(".");
113 #ifdef PBMascii
114 row_count = 0;
115 putc('\n', fd);
116 #endif /* PBMascii */
117 }
118 } else {
119 #ifdef PBMascii
120 if (fprintf(fd, "P1\n") == EOF)
121 #else
122 if (fprintf(fd, "P4\n") == EOF)
123 #endif /* PBMascii */
124 goto ERROR_EXIT;
125 if (fprintf(fd, "%d %d\n", (pb->nb) * 8, pb->nr) == EOF)
126 goto ERROR_EXIT;
127
128 for (row_c = 0; row_c < pb->nr; row_c++) {
129 row = get_RowBuf(pb, pb->nr - row_c - 1);
130 if (row == NULL)
131 continue;
132
133 for (byte_c = x = 0; byte_c < pb->nb; byte_c++)
134 #ifdef PBMascii
135 {
136 for (bit = 128; bit; bit GGE 1, x++)
137 if (bit & row->buf[byte_c]) {
138 if (putc('1', fd) == EOF)
139 goto ERROR_EXIT;
140 row_count++;
141 if (row_count >=
142 MAXOUTPUTROWS) {
143 row_count = 0;
144 if (putc('\n', fd)
145 == EOF)
146 goto ERROR_EXIT;
147 }
148 } else {
149 putc('0', fd);
150 row_count++;
151 if (row_count >=
152 MAXOUTPUTROWS) {
153 row_count = 0;
154 if (putc('\n', fd)
155 == EOF)
156 goto ERROR_EXIT;
157 }
158 }
159 }
160 #else
161 {
162 if (putc(row->buf[byte_c], fd) == EOF)
163 goto ERROR_EXIT;
164 }
165 #endif /* PBMascii */
166 if ((!pg->quiet) && (row_c % 10 == 0))
167 /* For the impatients among us ... */
168 Eprintf(".");
169 #ifdef PBMascii
170 row_count = 0;
171 putc('\n', fd);
172 #endif /* PBMascii */
173 }
174 }
175 fflush(fd);
176
177 if (!pg->quiet)
178 Eprintf("\n");
179 if (fd != stdout)
180 fclose(fd);
181 return 0;
182
183 ERROR_EXIT:
184 PError("write_PBM");
185 /*ERROR_EXIT_2:*/
186 return ERROR;
187 }
188