Debian Codegraph @

converter/other/jbig/jbigtopnm.c

raw symbols SCIP
1 /*
2 jbigtopnm - JBIG to PNM converter
3
4 This program was derived from jbgtopbm.c in Markus Kuhn's
5 JBIG-KIT package by Bryan Henderson on 2000.05.11
6
7 The main difference is that this version uses the Netpbm libraries.
8
9 */
10
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <limits.h>
16
17 #include <jbig.h>
18
19 #include "pnm.h"
20 #include "shhopt.h"
21 #include "mallocvar.h"
22
23 #define BUFSIZE 8192
24
25
26
27 typedef struct {
28 /* All the information the user supplied in the command line,
29 in a form easy for the program to use.
30 */
31 const char * inputFileName;
32 const char * outputFileName;
33 unsigned long xmax;
34 unsigned long ymax;
35 unsigned int binary;
36 unsigned int diagnose;
37 unsigned int planeSpec;
38 unsigned int plane;
39 } CmdlineInfo;
40
41
42
43 static void
44 parseCommandLine(int argc,
45 const char ** const argv,
46 CmdlineInfo * const cmdlineP) {
47 /*----------------------------------------------------------------------------
48 Note that the file spec array we return is stored in the storage that
49 was passed to us as the argv array.
50 -----------------------------------------------------------------------------*/
51 optEntry * option_def;
52
53 optStruct3 opt;
54
55 unsigned int xmaxSpec, ymaxSpec;
56
57 unsigned int option_def_index;
58
59 MALLOCARRAY_NOFAIL(option_def, 100);
60
61 option_def_index = 0; /* incremented by OPTENT3 */
62 OPTENT3(0, "binary", OPT_FLAG, NULL, &cmdlineP->binary, 0);
63 OPTENT3(0, "diagnose", OPT_FLAG, NULL, &cmdlineP->diagnose, 0);
64 OPTENT3(0, "plane", OPT_UINT, &cmdlineP->plane, &cmdlineP->planeSpec,0);
65 OPTENT3(0, "xmax", OPT_UINT, &cmdlineP->xmax, &xmaxSpec, 0);
66 OPTENT3(0, "ymax", OPT_UINT, &cmdlineP->ymax, &ymaxSpec, 0);
67
68 opt.opt_table = option_def;
69 opt.short_allowed = false; /* We have no short (old-fashioned) options */
70 opt.allowNegNum = false; /* We have no parms that are negative numbers */
71
72 pm_optParseOptions3(&argc, (char**)argv, opt, sizeof(opt), 0);
73 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
74
75 if (!xmaxSpec)
76 cmdlineP->xmax = UINT_MAX;
77 if (!ymaxSpec)
78 cmdlineP->ymax = UINT_MAX;
79
80 cmdlineP->inputFileName = (argc-1 >= 1) ? argv[1] : "-";
81 cmdlineP->outputFileName = (argc-1 >= 2) ? argv[2] : "-";
82
83 if (argc-1 > 2)
84 pm_error("Too many arguments: %u. The only possible "
85 "non-option arguments are input file name and "
86 "output file name", argc-1);
87 }
88
89
90
91 static void
92 collect_image (unsigned char *data, size_t len, void *image) {
93 static int cursor = 0;
94 int i;
95
96 for (i = 0; i < len; i++) {
97 ((unsigned char *)image)[cursor++] = data[i];
98 }
99 }
100
101
102
103 static void
104 write_pnm (FILE *fout, const unsigned char * const image, const int bpp,
105 const int rows, const int cols, const int maxval,
106 const int format) {
107
108 int row;
109 xel *pnm_row;
110
111 pnm_writepnminit(fout, cols, rows, maxval, format, 0);
112
113 pnm_row = pnm_allocrow(cols);
114
115 for (row = 0; row < rows; row++) {
116 int col;
117 for (col = 0; col < cols; col++) {
118 int j;
119 for (j = 0; j < bpp; j++)
120 PNM_ASSIGN1(pnm_row[col],
121 image[(((row*cols)+col) * bpp) + j]);
122 }
123 pnm_writepnmrow(fout, pnm_row, cols, maxval, format, 0);
124 }
125
126 pnm_freerow(pnm_row);
127 }
128
129
130
131 static void
132 write_raw_pbm(FILE * const fout,
133 const unsigned char * const binary_image,
134 int const cols,
135 int const rows) {
136
137 unsigned int const bytes_per_row = pbm_packed_bytes(cols);
138
139 int row;
140
141 pbm_writepbminit(fout, cols, rows, 0);
142
143 for (row = 0; row < rows; ++row)
144 pbm_writepbmrow_packed(fout, &binary_image[row*bytes_per_row], cols,
145 0);
146 }
147
148
149
150 /*
151 *
152 */
153 static void
154 diagnose_bie(FILE *f)
155 {
156 unsigned char bih[20];
157 int len;
158 unsigned long xd, yd, l0;
159
160 len = fread(bih, 1, 20, f);
161 if (len < 20) {
162 printf("Input file is %d < 20 bytes long and does therefore not "
163 "contain an intact BIE header!\n", len);
164 return;
165 }
166
167 printf("Decomposition of BIH:\n\n DL = %d\n D = %d\n P = %d\n"
168 " - = %d\n XD = %lu\n YD = %lu\n L0 = %lu\n MX = %d\n"
169 " MY = %d\n",
170 bih[0], bih[1], bih[2], bih[3],
171 xd = ((unsigned long) bih[ 4] << 24) | ((unsigned long)bih[ 5] << 16)|
172 ((unsigned long) bih[ 6] << 8) | ((unsigned long) bih[ 7]),
173 yd = ((unsigned long) bih[ 8] << 24) | ((unsigned long)bih[ 9] << 16)|
174 ((unsigned long) bih[10] << 8) | ((unsigned long) bih[11]),
175 l0 = ((unsigned long) bih[12] << 24) | ((unsigned long)bih[13] << 16)|
176 ((unsigned long) bih[14] << 8) | ((unsigned long) bih[15]),
177 bih[16], bih[17]);
178 printf(" order = %d %s%s%s%s%s\n", bih[18],
179 bih[18] & JBG_HITOLO ? " HITOLO" : "",
180 bih[18] & JBG_SEQ ? " SEQ" : "",
181 bih[18] & JBG_ILEAVE ? " ILEAVE" : "",
182 bih[18] & JBG_SMID ? " SMID" : "",
183 bih[18] & 0xf0 ? " other" : "");
184 printf(" options = %d %s%s%s%s%s%s%s%s\n", bih[19],
185 bih[19] & JBG_LRLTWO ? " LRLTWO" : "",
186 bih[19] & JBG_VLENGTH ? " VLENGTH" : "",
187 bih[19] & JBG_TPDON ? " TPDON" : "",
188 bih[19] & JBG_TPBON ? " TPBON" : "",
189 bih[19] & JBG_DPON ? " DPON" : "",
190 bih[19] & JBG_DPPRIV ? " DPPRIV" : "",
191 bih[19] & JBG_DPLAST ? " DPLAST" : "",
192 bih[19] & 0x80 ? " other" : "");
193 printf("\n %lu stripes, %d layers, %d planes\n\n",
194 ((yd >> bih[1]) + ((((1UL << bih[1]) - 1) & xd) != 0) + l0 - 1) / l0,
195 bih[1] - bih[0], bih[2]);
196
197 return;
198 }
199
200
201
202 int main (int argc, const char **argv)
203 {
204 CmdlineInfo cmdline;
205 FILE * ifP;
206 FILE * ofP;
207
208 pm_proginit(&argc, argv);
209
210 parseCommandLine(argc, argv, &cmdline);
211
212 ifP = pm_openr(cmdline.inputFileName);
213 ofP = pm_openw(cmdline.outputFileName);
214
215 if (cmdline.diagnose)
216 diagnose_bie(ifP);
217 else {
218 struct jbg_dec_state s;
219 unsigned char * buffer;
220 int result;
221
222 MALLOCARRAY(buffer, BUFSIZE);
223 if (!buffer)
224 pm_error("Failed to get %u bytes of memory for buffer", BUFSIZE);
225
226 /* send input file to decoder */
227 jbg_dec_init(&s);
228 jbg_dec_maxsize(&s, cmdline.xmax, cmdline.ymax);
229 result = JBG_EAGAIN;
230 do {
231 size_t len;
232 size_t cnt;
233 unsigned char * p;
234
235 len = fread(buffer, 1, BUFSIZE, ifP);
236 if (len == 0)
237 break;
238 cnt = 0;
239 p = &buffer[0];
240 while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {
241 result = jbg_dec_in(&s, p, len, &cnt);
242 p += cnt;
243 len -= cnt;
244 }
245 } while (result == JBG_EAGAIN || result == JBG_EOK);
246 if (ferror(ifP))
247 pm_error("Error reading input file");
248 if (result != JBG_EOK && result != JBG_EOK_INTR)
249 pm_error("Invalid contents of input file. %s",
250 jbg_strerror(result));
251 if (cmdline.planeSpec && jbg_dec_getplanes(&s) <= cmdline.plane)
252 pm_error("Image has only %u planes", jbg_dec_getplanes(&s));
253
254 {
255 /* Write it out */
256
257 int rows, cols;
258 int maxval;
259 int bpp;
260 bool justOnePlane;
261 unsigned int plane_to_write;
262
263 cols = jbg_dec_getwidth(&s);
264 rows = jbg_dec_getheight(&s);
265 maxval = pm_bitstomaxval(jbg_dec_getplanes(&s));
266 bpp = (jbg_dec_getplanes(&s)+7)/8;
267
268 if (jbg_dec_getplanes(&s) == 1) {
269 justOnePlane = true;
270 plane_to_write = 0;
271 } else {
272 if (cmdline.planeSpec) {
273 justOnePlane = true;
274 plane_to_write = cmdline.plane;
275 } else
276 justOnePlane = false;
277 }
278
279 if (justOnePlane) {
280 unsigned char * binary_image;
281
282 pm_message("WRITING PBM FILE");
283
284 binary_image=jbg_dec_getimage(&s, plane_to_write);
285 write_raw_pbm(ofP, binary_image, cols, rows);
286 } else {
287 unsigned char *image;
288 pm_message("WRITING PGM FILE");
289
290 /* Write out all the planes */
291 /* What jbig.doc doesn't tell you is that jbg_dec_merge_planes
292 delivers the image in chunks, in consecutive calls to
293 the data-out callback function. And a row can span two
294 chunks.
295 */
296 image = malloc(cols*rows*bpp);
297 jbg_dec_merge_planes(&s, !cmdline.binary, collect_image,
298 image);
299 write_pnm(ofP, image, bpp, rows, cols, maxval, PGM_TYPE);
300 free(image);
301 }
302 jbg_dec_free(&s);
303 }
304
305 pm_close(ofP);
306 pm_close(ifP);
307 free(buffer);
308 }
309 return 0;
310 }
311
312
313