Debian Codegraph @

converter/other/jbig/pnmtojbig.c

raw symbols SCIP
1 /*
2 pnmtojbig - PNM to JBIG converter
3
4 This program was derived from pbmtojbg.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 /*
12 The JBIG standard doesn't say which end of the scale is white and
13 which end is black in a BIE. It has a recommendation in terms of
14 foreground and background (a concept which does not exist in the
15 Netpbm formats) for single-plane images, and is silent for
16 multi-plane images.
17
18 Kuhn's implementation of the JBIG standard says if the BIE has a
19 single plane, then in that plane a zero bit means white and a one
20 bit means black. But if it has multiple planes, a composite zero
21 value means black and a composite maximal value means white.
22
23 Actually, Kuhn's pbmtojbg doesn't even implement this, but rather
24 bases the distinction on whether the input file was PBM or PGM.
25 This means that if you convert a PGM file with maxval 1 to a JBIG
26 file and then back, the result (which is a PBM file) is the
27 inverse of what you started with. Same if the PGM file has
28 maxval > 1 but you use a -t option to write only one plane. We
29 assume this is just a bug in pbmtojpg and that hardly anybody does
30 this. So we adopt the implementation described above.
31
32 This means that after jbg_split_planes() hands us a set of bitmap
33 planes, if there is only one of them, we have to invert all the
34 bits in it.
35
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <jbig.h>
42
43 #include "pm_c_util.h"
44 #include "mallocvar.h"
45 #include "pnm.h"
46
47 static unsigned long total_length = 0;
48 /* used for determining output file length */
49
50 /*
51 * malloc() with exception handler
52 */
53 static void
54 *checkedmalloc(size_t n)
55 {
56 void *p;
57
58 if ((p = malloc(n)) == NULL) {
59 fprintf(stderr, "Sorry, not enough memory available!\n");
60 exit(1);
61 }
62
63 return p;
64 }
65
66
67
68 /*
69 * Callback procedure which is used by JBIG encoder to deliver the
70 * encoded data. It simply sends the bytes to the output file.
71 */
72 static void data_out(unsigned char *start, size_t len, void *file)
73 {
74 fwrite(start, len, 1, (FILE *) file);
75 total_length += len;
76 return;
77 }
78
79
80
81 static void
82 readPbm(FILE * const fin,
83 unsigned int const cols,
84 unsigned int const rows,
85 unsigned char *** const bitmapP) {
86
87 unsigned int const bytes_per_line = pbm_packed_bytes(cols);
88
89 unsigned char ** bitmap;
90
91 /* Read the input image into bitmap[] */
92 /* Shortcut for PBM */
93 int row;
94 bitmap = (unsigned char **) checkedmalloc(sizeof(unsigned char *));
95 bitmap[0] = (unsigned char *) checkedmalloc(bytes_per_line * rows);
96
97 for (row = 0; row < rows; row++)
98 pbm_readpbmrow_packed(fin, &bitmap[0][row*bytes_per_line],
99 cols, RPBM_FORMAT);
100
101 *bitmapP = bitmap;
102 }
103
104
105
106 static void
107 readImage(FILE * const fin,
108 unsigned int const cols,
109 unsigned int const rows,
110 xelval const maxval,
111 int const format,
112 unsigned int const bpp,
113 unsigned char ** const imageP) {
114 /*----------------------------------------------------------------------------
115 Read the input image and put it into *imageP;
116
117 Although the PBM case is separated, this logic works also for
118 PBM, bpp=1.
119 -----------------------------------------------------------------------------*/
120 unsigned char *image;
121 /* This is a representation of the entire image with 'bpp' bytes per
122 pixel. The 'bpp' bytes for each pixel are arranged MSB first
123 and its numerical value is the value from the PNM input.
124 The pixels are laid out in row-major format in this rectangle.
125
126 The point of this data structure is it is what jbg_split_planes()
127 wants for input.
128 */
129 xel* pnm_row;
130 unsigned int row;
131
132 pnm_row = pnm_allocrow(cols); /* row buffer */
133 MALLOCARRAY_NOFAIL(image, cols * rows * bpp);
134
135 for (row = 0; row < rows; ++row) {
136 unsigned int col;
137 pnm_readpnmrow(fin, pnm_row, cols, maxval, format);
138 for (col = 0; col < cols; col++) {
139 unsigned int j;
140 /* Move each byte of the sample into image[], MSB first */
141 for (j = 0; j < bpp; ++j)
142 image[(((row*cols)+col) * bpp) + j] = (unsigned char)
143 PNM_GET1(pnm_row[col]) >> ((bpp-1-j) * 8);
144 }
145 }
146 pnm_freerow(pnm_row);
147 *imageP = image;
148 }
149
150
151
152 static void
153 convertImageToBitmap(unsigned char * const image,
154 unsigned char *** const bitmapP,
155 unsigned int const encode_planes,
156 unsigned int const bytes_per_line,
157 unsigned int const lines) {
158
159 /* Convert image[] into bitmap[] */
160
161 unsigned char ** bitmap;
162 unsigned int i;
163
164 MALLOCARRAY_NOFAIL(bitmap, encode_planes);
165 for (i = 0; i < encode_planes; ++i)
166 MALLOCARRAY_NOFAIL(bitmap[i], bytes_per_line * lines);
167
168 *bitmapP = bitmap;
169 }
170
171
172
173 static void
174 readPnm(FILE * const fin,
175 unsigned int const cols,
176 unsigned int const rows,
177 xelval const maxval,
178 int const format,
179 unsigned int const bpp,
180 unsigned int const planes,
181 unsigned int const encode_planes,
182 bool const use_graycode,
183 unsigned char *** const bitmapP) {
184
185 unsigned int const bytes_per_line = pbm_packed_bytes(cols);
186
187 unsigned char * image;
188 unsigned char ** bitmap;
189
190 readImage(fin, cols, rows, maxval, format, bpp, &image);
191
192 convertImageToBitmap(image, &bitmap, encode_planes, bytes_per_line, rows);
193
194 jbg_split_planes(cols, rows, planes, encode_planes, image, bitmap,
195 use_graycode);
196 free(image);
197
198 /* Invert the image if it is just one plane. See top of this file
199 for an explanation why. Because of the separate handling of PBM,
200 this is for exceptional PGM files.
201 */
202
203 if (encode_planes == 1) {
204 unsigned int row;
205 for (row = 0; row < rows; ++row) {
206 unsigned int i;
207 for (i = 0; i < bytes_per_line; i++)
208 bitmap[0][(row*bytes_per_line) + i] ^= 0xff;
209
210 if (cols % 8 > 0) {
211 bitmap[0][ (row+1)*bytes_per_line -1] >>= 8-cols%8;
212 bitmap[0][ (row+1)*bytes_per_line -1] <<= 8-cols%8;
213 }
214 }
215 }
216 *bitmapP = bitmap;
217 }
218
219
220
221 int
222 main(int argc, char **argv) {
223 FILE *fin = stdin, *fout = stdout;
224 const char *fnin = "<stdin>", *fnout = "<stdout>";
225 int i;
226 int all_args = 0, files = 0;
227 int bpp, planes, encode_planes = -1;
228 int cols, rows;
229 xelval maxval;
230 int format;
231 unsigned char **bitmap;
232 /* This is an array of the planes of the image. Each plane is a
233 two-dimensional array of pixels laid out in row-major format.
234 format with each pixel being one bit. A byte in the array
235 contains 8 pixels left to right, msb to lsb.
236 */
237
238 struct jbg_enc_state s;
239 int verbose = 0, delay_at = 0, use_graycode = 1;
240 long mwidth = 640, mheight = 480;
241 int dl = -1, dh = -1, d = -1, l0 = -1, mx = -1;
242 int options = JBG_TPDON | JBG_TPBON | JBG_DPON;
243 int order = JBG_ILEAVE | JBG_SMID;
244
245 pbm_init(&argc, argv);
246
247 /* parse command line arguments */
248 for (i = 1; i < argc; ++i) {
249 int j;
250 if (!all_args && argv[i][0] == '-') {
251 if (argv[i][1] == '\0' && files == 0)
252 ++files;
253 else {
254 for (j = 1; j > 0 && argv[i][j]; j++) {
255 switch(tolower(argv[i][j])) {
256 case '-' :
257 all_args = 1;
258 break;
259 case 'v':
260 verbose = 1;
261 break;
262 case 'b':
263 use_graycode = 0;
264 break;
265 case 'c':
266 delay_at = 1;
267 break;
268 case 'x':
269 if (++i >= argc)
270 pm_error("-x needs a value");
271 j = -1;
272 mwidth = atol(argv[i]);
273 break;
274 case 'y':
275 if (++i >= argc)
276 pm_error("-y needsa value");
277 j = -1;
278 mheight = atol(argv[i]);
279 break;
280 case 'o':
281 if (++i >= argc)
282 pm_error("-o needs a value");
283 j = -1;
284 order = atoi(argv[i]);
285 break;
286 case 'p':
287 if (++i >= argc)
288 pm_error("-p needs a value");
289 j = -1;
290 options = atoi(argv[i]);
291 break;
292 case 'l':
293 if (++i >= argc)
294 pm_error("-l needs a value");
295 j = -1;
296 dl = atoi(argv[i]);
297 break;
298 case 'h':
299 if (++i >= argc)
300 pm_error("-h needs a value");
301 j = -1;
302 dh = atoi(argv[i]);
303 break;
304 case 'q':
305 d = 0;
306 break;
307 case 'd':
308 if (++i >= argc)
309 pm_error("-d needs a value");
310 j = -1;
311 d = atoi(argv[i]);
312 break;
313 case 's':
314 if (++i >= argc)
315 pm_error("-s needs a value");
316 j = -1;
317 l0 = atoi(argv[i]);
318 break;
319 case 't':
320 if (++i >= argc)
321 pm_error("-t needs a value");
322 j = -1;
323 encode_planes = atoi(argv[i]);
324 break;
325 case 'm':
326 if (++i >= argc)
327 pm_error("-m needs a value");
328 j = -1;
329 mx = atoi(argv[i]);
330 break;
331 default:
332 pm_error("Unrecognized option: %c", argv[i][j]);
333 }
334 }
335 }
336 } else {
337 switch (files++) {
338 case 0:
339 if (argv[i][0] != '-' || argv[i][1] != '\0') {
340 fnin = argv[i];
341 fin = fopen(fnin, "rb");
342 if (!fin) {
343 fprintf(stderr, "Can't open input file '%s", fnin);
344 perror("'");
345 exit(1);
346 }
347 }
348 break;
349 case 1:
350 fnout = argv[i];
351 fout = fopen(fnout, "wb");
352 if (!fout) {
353 fprintf(stderr, "Can't open input file '%s", fnout);
354 perror("'");
355 exit(1);
356 }
357 break;
358 default:
359 pm_error("too many non-option arguments");
360 }
361 }
362 }
363
364 pnm_readpnminit(fin, &cols, &rows, &maxval, &format);
365
366 if (PNM_FORMAT_TYPE(format) != PGM_TYPE &&
367 PNM_FORMAT_TYPE(format) != PBM_TYPE)
368 pm_error("This program accepts PBM and PGM input only. "
369 "Try Ppmtopgm.");
370
371 planes = pm_maxvaltobits(maxval);
372
373 /* In a JBIG file, maxvals are determined only by the number of planes,
374 so must be a power of 2 minus 1
375 */
376
377 if ((1UL << planes)-1 != maxval)
378 pm_error("Input image has unacceptable maxval: %d. JBIG files must "
379 "have a maxval which is a power of 2 minus 1. Use "
380 "Ppmdepth to adjust the image's maxval", maxval);
381
382 bpp = (planes + 7) / 8;
383
384 if (encode_planes < 0 || encode_planes > planes)
385 encode_planes = planes;
386
387 if (bpp == 1 && PNM_FORMAT_TYPE(format) == PBM_TYPE)
388 readPbm(fin, cols, rows, &bitmap);
389 else
390 readPnm(fin, cols, rows, maxval, format, bpp,
391 planes, encode_planes, use_graycode,
392 &bitmap);
393
394 /* Apply JBIG algorithm and write BIE to output file */
395
396 /* initialize parameter struct for JBIG encoder*/
397 jbg_enc_init(&s, cols, rows, encode_planes, bitmap, data_out, fout);
398
399 /* Select number of resolution layers either directly or based
400 * on a given maximum size for the lowest resolution layer */
401 if (d >= 0)
402 jbg_enc_layers(&s, d);
403 else
404 jbg_enc_lrlmax(&s, mwidth, mheight);
405
406 /* Specify a few other options (each is ignored if negative) */
407 if (delay_at)
408 options |= JBG_DELAY_AT;
409 jbg_enc_lrange(&s, dl, dh);
410 jbg_enc_options(&s, order, options, l0, mx, -1);
411
412 /* now encode everything and send it to data_out() */
413 jbg_enc_out(&s);
414
415 /* give encoder a chance to free its temporary data structures */
416 jbg_enc_free(&s);
417
418 /* check for file errors and close fout */
419 if (ferror(fout) || fclose(fout)) {
420 fprintf(stderr, "Problem while writing output file '%s", fnout);
421 perror("'");
422 exit(1);
423 }
424
425 /* In case the user wants to know all the gory details ... */
426 if (verbose) {
427 fprintf(stderr, "Information about the created JBIG bi-level image entity "
428 "(BIE):\n\n");
429 fprintf(stderr, " input image size: %ld x %ld pixel\n",
430 s.xd, s.yd);
431 fprintf(stderr, " bit planes: %d\n", s.planes);
432 if (s.planes > 1)
433 fprintf(stderr, " encoding: %s code, MSB first\n",
434 use_graycode ? "Gray" : "binary");
435 fprintf(stderr, " stripes: %ld\n", s.stripes);
436 fprintf(stderr, " lines per stripe in layer 0: %ld\n", s.l0);
437 fprintf(stderr, " total number of diff. layers: %d\n", s.d);
438 fprintf(stderr, " lowest layer in BIE: %d\n", s.dl);
439 fprintf(stderr, " highest layer in BIE: %d\n", s.dh);
440 fprintf(stderr, " lowest layer size: %lu x %lu pixel\n",
441 jbg_ceil_half(s.xd, s.d - s.dl), jbg_ceil_half(s.yd, s.d - s.dl));
442 fprintf(stderr, " highest layer size: %lu x %lu pixel\n",
443 jbg_ceil_half(s.xd, s.d - s.dh), jbg_ceil_half(s.yd, s.d - s.dh));
444 fprintf(stderr, " option bits:%s%s%s%s%s%s%s\n",
445 s.options & JBG_LRLTWO ? " LRLTWO" : "",
446 s.options & JBG_VLENGTH ? " VLENGTH" : "",
447 s.options & JBG_TPDON ? " TPDON" : "",
448 s.options & JBG_TPBON ? " TPBON" : "",
449 s.options & JBG_DPON ? " DPON" : "",
450 s.options & JBG_DPPRIV ? " DPPRIV" : "",
451 s.options & JBG_DPLAST ? " DPLAST" : "");
452 fprintf(stderr, " order bits:%s%s%s%s\n",
453 s.order & JBG_HITOLO ? " HITOLO" : "",
454 s.order & JBG_SEQ ? " SEQ" : "",
455 s.order & JBG_ILEAVE ? " ILEAVE" : "",
456 s.order & JBG_SMID ? " SMID" : "");
457 fprintf(stderr, " AT maximum x-offset: %d\n"
458 " AT maximum y-offset: %d\n", s.mx, s.my);
459 fprintf(stderr, " length of output file: %lu byte\n\n",
460 total_length);
461 }
462
463 return 0;
464 }
465
466
467