Debian Codegraph @

tools/minidjvu.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * minidjvu.c - an example of using the library
3 */
4
5 #include <minidjvu/minidjvu.h>
6 #include "../src/base/mdjvucfg.h" /* for i18n, HAVE_TIFF */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 #include <assert.h>
12 #include <locale.h>
13
14 /* TODO: remove duplicated code */
15
16
17 /* options */
18 int32 dpi = 300;
19 int32 pages_per_dict = 10; /* 0 means infinity */
20 int dpi_specified = 0;
21 int verbose = 0;
22 int smooth = 0;
23 int averaging = 0;
24 int match = 0;
25 int Match = 0;
26 int aggression = 100;
27 int erosion = 0;
28 int clean = 0;
29 int report = 0;
30 int no_prototypes = 0;
31 int warnings = 0;
32 int indirect = 0;
33 const char* dict_suffix = NULL;
34
35 /* ========================================================================= */
36
37 /* file name template routines (for multipage encoding) {{{ */
38
39 static int get_ext_delim_pos(const char *fname)
40 {
41 int pos = strcspn(fname,".");
42 int last = 0;
43
44 while (last + pos != strlen(fname))
45 {
46 last += (pos + 1);
47 pos = strcspn(fname + last,".");
48 }
49 return last;
50 }
51
52 static char *get_page_or_dict_name(char **elements, int cnt, const char *fname)
53 {
54 int i, extpos, same=-1;
55 char *page_name, *pattern;
56
57 extpos = get_ext_delim_pos(fname);
58 page_name = MDJVU_MALLOCV(char, extpos + 10);
59 memset(page_name,'\0',extpos + 6);
60 if (extpos > 0)
61 strncpy(page_name, fname, extpos-1);
62 strcat(page_name, ".djvu");
63
64 for (i=0; i<cnt; i++ )
65 {
66 if (strcmp(page_name, elements[i]) == 0)
67 {
68 same = i;
69 break;
70 }
71 }
72
73 if (same != -1)
74 {
75 int previdx=0, idx=0, res;
76 /* discard the extension */
77 page_name[extpos-1] = '\0';
78
79 pattern = MDJVU_MALLOCV(char, extpos + 10);
80 strcpy(pattern, page_name);
81 strcat(pattern, "#%d.");
82
83 for (i=same; i<cnt; i++)
84 {
85 if (mdjvu_ends_with_ignore_case(elements[i],".djvu"))
86 {
87 res = sscanf(elements[i],pattern,&idx);
88 if (res && idx > previdx) previdx = idx;
89 }
90 }
91 if (idx == 999)
92 {
93 fprintf(stderr, _("Cannot generate a unique name for %s\n"), fname);
94 exit(1);
95 }
96 sprintf(page_name + (extpos - 1),"#%03d.djvu",idx+1);
97 MDJVU_FREEV(pattern);
98 }
99 return(page_name);
100 }
101
102 static void replace_suffix(char *name, const char *suffix)
103 {
104 int len = strlen(name);
105
106 name[len-4] = '\0';
107 strcat(name, suffix);
108 }
109
110 /* file name template routines (for multipage encoding) }}} */
111
112 /* ========================================================================= */
113
114 static void show_usage_and_exit(void) /* {{{ */
115 {
116 const char *what_it_does = _("encode/decode bitonal DjVu files");
117 if (strcmp(MDJVU_VERSION, mdjvu_get_version()))
118 {
119 printf(_("minidjvu - %s\n"), what_it_does);
120 printf(_("Warning: program and library version mismatch:\n"));
121 printf(_(" program version %s, library version %s.\n\n"), MDJVU_VERSION, mdjvu_get_version());
122
123 }
124 else
125 {
126 printf("minidjvu %s - %s\n", MDJVU_VERSION, what_it_does);
127
128 }
129 printf(_("Usage:\n"));
130 printf(_("single page encoding/decoding:\n"));
131 printf(_(" minidjvu [options] <input file> <output file>\n"));
132 printf(_("multiple pages encoding:\n"));
133 printf(_(" minidjvu [options] <input file> ... <output file>\n"));
134 printf(_("Formats supported:\n"));
135
136 printf(_(" DjVu (single-page bitonal), PBM, Windows BMP"));
137 if (mdjvu_have_tiff_support())
138 printf(_(", TIFF.\n"));
139 else
140 printf(_("; TIFF support is OFF.\n"));
141
142 printf(_("Options:\n"));
143 printf(_(" -A, --Averaging: compute \"average\" representatives\n"));
144 printf(_(" -a <n>, --aggression <n>: set aggression level (default 100)\n"));
145 printf(_(" -c, --clean remove small black pieces\n"));
146 printf(_(" -d <n> --dpi <n>: set resolution in dots per inch\n"));
147 printf(_(" -e, --erosion sacrifice quality to gain in size\n"));
148 printf(_(" -i, --indirect: generate an indirect multipage document\n"));
149 printf(_(" -l, --lossy: use all lossy options (-s -c -m -e -A)\n"));
150 printf(_(" -m, --match: match and substitute patterns\n"));
151 printf(_(" -n, --no-prototypes: do not search for prototypes\n"));
152 printf(_(" -p <n>, --pages-per-dict <n>: pages per dictionary (default 10)\n"));
153 printf(_(" -r, --report: report multipage coding progress\n"));
154 printf(_(" -s, --smooth: remove some badly looking pixels\n"));
155 printf(_(" -v, --verbose: print messages about everything\n"));
156 printf(_(" -X, --Xtension: file extension for shared dictionary files\n"));
157 printf(_(" -w, --warnings: do not suppress TIFF warnings\n"));
158 printf(_("See the man page for detailed description of each option.\n"));
159 exit(2);
160 } /* }}} */
161
162 static int decide_if_bmp(const char *path)
163 {
164 return mdjvu_ends_with_ignore_case(path, ".bmp");
165 }
166
167 static int decide_if_djvu(const char *path)
168 {
169 return mdjvu_ends_with_ignore_case(path, ".djvu")
170 || mdjvu_ends_with_ignore_case(path, ".djv");
171 }
172
173 static int decide_if_tiff(const char *path)
174 {
175 return mdjvu_ends_with_ignore_case(path, ".tiff")
176 || mdjvu_ends_with_ignore_case(path, ".tif");
177 }
178
179 /* ========================================================================= */
180
181 static mdjvu_image_t load_image(const char *path)
182 {
183 mdjvu_error_t error;
184 mdjvu_image_t image;
185
186 if (verbose) printf(_("loading a DjVu page from `%s'\n"), path);
187 image = mdjvu_load_djvu_page(path, &error);
188 if (!image)
189 {
190 fprintf(stderr, "%s: %s\n", path, mdjvu_get_error_message(error));
191 exit(1);
192 }
193 if (verbose)
194 {
195 printf(_("loaded; the page has %d bitmaps and %d blits\n"),
196 mdjvu_image_get_bitmap_count(image),
197 mdjvu_image_get_blit_count(image));
198 }
199 return image;
200 }
201
202 static mdjvu_matcher_options_t get_matcher_options(void)
203 {
204 mdjvu_matcher_options_t m_options = NULL;
205 if (match || Match)
206 {
207 m_options = mdjvu_matcher_options_create();
208 mdjvu_use_matcher_method(m_options, MDJVU_MATCHER_PITH_2);
209 if (Match)
210 mdjvu_use_matcher_method(m_options, MDJVU_MATCHER_RAMPAGE);
211 mdjvu_set_aggression(m_options, aggression);
212 }
213 return m_options;
214 }
215
216 static void sort_and_save_image(mdjvu_image_t image, const char *path)
217 {
218 mdjvu_error_t error;
219
220 mdjvu_compression_options_t options = mdjvu_compression_options_create();
221 mdjvu_set_matcher_options(options, get_matcher_options());
222
223 mdjvu_set_clean(options, clean);
224 mdjvu_set_verbose(options, verbose);
225 mdjvu_set_no_prototypes(options, no_prototypes);
226 mdjvu_set_averaging(options, averaging);
227 mdjvu_compress_image(image, options);
228 mdjvu_compression_options_destroy(options);
229
230 if (verbose) printf(_("encoding to `%s'\n"), path);
231
232 if (!mdjvu_save_djvu_page(image, path, NULL, &error, erosion))
233 {
234 fprintf(stderr, "%s: %s\n", path, mdjvu_get_error_message(error));
235 exit(1);
236 }
237 }
238
239 static mdjvu_bitmap_t load_bitmap(const char *path, int tiff_idx)
240 {
241 mdjvu_error_t error;
242 mdjvu_bitmap_t bitmap;
243
244 if (decide_if_bmp(path))
245 {
246 if (verbose) printf(_("loading from Windows BMP file `%s'\n"), path);
247 bitmap = mdjvu_load_bmp(path, &error);
248 }
249 else if (decide_if_tiff(path))
250 {
251 if (verbose) printf(_("loading from TIFF file `%s'\n"), path);
252 if (!warnings)
253 mdjvu_disable_tiff_warnings();
254 if (dpi_specified)
255 bitmap = mdjvu_load_tiff(path, NULL, &error, tiff_idx);
256 else
257 bitmap = mdjvu_load_tiff(path, &dpi, &error, tiff_idx);
258 if (verbose) printf(_("resolution is %d dpi\n"), dpi);
259 }
260 else if (decide_if_djvu(path))
261 {
262 mdjvu_image_t image = load_image(path);
263 bitmap = mdjvu_render(image);
264 mdjvu_image_destroy(image);
265 if (verbose)
266 {
267 printf(_("bitmap %d x %d rendered\n"),
268 mdjvu_bitmap_get_width(bitmap),
269 mdjvu_bitmap_get_height(bitmap));
270 }
271 }
272 else
273 {
274 if (verbose) printf(_("loading from PBM file `%s'\n"), path);
275 bitmap = mdjvu_load_pbm(path, &error);
276 }
277
278 if (!bitmap)
279 {
280 fprintf(stderr, "%s: %s\n", path, mdjvu_get_error_message(error));
281 exit(1);
282 }
283
284 if (smooth)
285 {
286 if (verbose) printf(_("smoothing the bitmap\n"));
287 mdjvu_smooth(bitmap);
288 }
289
290 return bitmap;
291 }
292
293 static void save_bitmap(mdjvu_bitmap_t bitmap, const char *path)
294 {
295 mdjvu_error_t error;
296 int result;
297
298 if (decide_if_bmp(path))
299 {
300 if (verbose) printf(_("saving to Windows BMP file `%s'\n"), path);
301 result = mdjvu_save_bmp(bitmap, path, dpi, &error);
302 }
303 else if (decide_if_tiff(path))
304 {
305 if (verbose) printf(_("saving to TIFF file `%s'\n"), path);
306 if (!warnings)
307 mdjvu_disable_tiff_warnings();
308 result = mdjvu_save_tiff(bitmap, path, &error);
309 }
310 else
311 {
312 if (verbose) printf(_("saving to PBM file `%s'\n"), path);
313 result = mdjvu_save_pbm(bitmap, path, &error);
314 }
315
316 if (!result)
317 {
318 fprintf(stderr, "%s: %s\n", path, mdjvu_get_error_message(error));
319 exit(1);
320 }
321 }
322
323 /* ========================================================================= */
324
325 static void decode(int argc, char **argv)
326 {
327 mdjvu_image_t image; /* a sequence of blits (what is stored in DjVu) */
328 mdjvu_bitmap_t bitmap; /* the result */
329
330 if (verbose) printf(_("\nDECODING\n"));
331 if (verbose) printf(_("________\n\n"));
332
333 image = load_image(argv[1]);
334 bitmap = mdjvu_render(image);
335 mdjvu_image_destroy(image);
336
337 if (verbose)
338 {
339 printf(_("bitmap %d x %d rendered\n"),
340 mdjvu_bitmap_get_width(bitmap),
341 mdjvu_bitmap_get_height(bitmap));
342 }
343
344 if (smooth)
345 {
346 if (verbose) printf(_("smoothing the bitmap\n"));
347 mdjvu_smooth(bitmap);
348 }
349
350 save_bitmap(bitmap, argv[2]);
351 mdjvu_bitmap_destroy(bitmap);
352 }
353
354
355 static mdjvu_image_t split_and_destroy(mdjvu_bitmap_t bitmap)
356 {
357 mdjvu_image_t image;
358 if (verbose) printf(_("splitting the bitmap into pieces\n"));
359 image = mdjvu_split(bitmap, dpi, /* options:*/ NULL);
360 mdjvu_bitmap_destroy(bitmap);
361 if (verbose)
362 {
363 printf(_("the splitted image has %d pieces\n"),
364 mdjvu_image_get_blit_count(image));
365 }
366 if (clean)
367 {
368 if (verbose) printf(_("cleaning\n"));
369 mdjvu_clean(image);
370 if (verbose)
371 {
372 printf(_("the cleaned image has %d pieces\n"),
373 mdjvu_image_get_blit_count(image));
374 }
375 }
376 return image;
377 }
378
379
380 static void encode(int argc, char **argv)
381 {
382 mdjvu_bitmap_t bitmap;
383 mdjvu_image_t image;
384
385 if (verbose) printf(_("\nENCODING\n"));
386 if (verbose) printf(_("________\n\n"));
387
388 bitmap = load_bitmap(argv[1], 0);
389
390 image = split_and_destroy(bitmap);
391 sort_and_save_image(image, argv[2]);
392 mdjvu_image_destroy(image);
393 }
394
395
396 /* Filtering is nondjvu->nondjvu job. */
397 static void filter(int argc, char **argv)
398 {
399 mdjvu_bitmap_t bitmap;
400
401 if (verbose) printf(_("\nFILTERING\n"));
402 if (verbose) printf(_("_________\n\n"));
403
404 bitmap = load_bitmap(argv[1], 0);
405 save_bitmap(bitmap, argv[2]);
406 mdjvu_bitmap_destroy(bitmap);
407 }
408
409
410 static const char *strip(const char *str, char sep)
411 {
412 const char *t = strrchr(str, sep);
413 if (t)
414 return t + 1;
415 else
416 return str;
417 }
418
419 /* return path without a directory name */
420 static const char *strip_dir(const char *path)
421 {
422 return strip(strip(path, '\\'), '/');
423 }
424
425
426 static void multipage_encode(int n, char **pages, char *outname, uint32 multipage_tiff)
427 {
428 mdjvu_image_t *images;
429 mdjvu_image_t dict;
430 int i, el = 0;
431 int ndicts = (pages_per_dict <= 0)? 1 :
432 (n % pages_per_dict > 0) ? (int) fabs(n/pages_per_dict) + 1:
433 (int) fabs(n/pages_per_dict);
434 char *dict_name, *path;
435 char **elements = MDJVU_MALLOCV(char *, n + ndicts);
436 int *sizes = MDJVU_MALLOCV(int, n + ndicts);
437 mdjvu_compression_options_t options;
438 mdjvu_bitmap_t bitmap;
439 mdjvu_error_t error;
440 int32 pages_compressed;
441 FILE *f, *tf=NULL;
442
443 match = 1;
444
445 if (!decide_if_djvu(outname))
446 {
447 fprintf(stderr, _("when encoding many pages, output file must be DjVu\n"));
448 exit(1);
449 }
450 if (!indirect)
451 {
452 tf = tmpfile();
453 if (!tf)
454 {
455 fprintf(stderr, _("Could not create a temporary file\n"));
456 exit(1);
457 }
458 }
459
460 if (verbose) printf(_("\nMULTIPAGE ENCODING\n"));
461 if (verbose) printf(_("__________________\n\n"));
462 if (verbose) printf(_("%d pages total\n"), n);
463
464 options = mdjvu_compression_options_create();
465 mdjvu_set_matcher_options(options, get_matcher_options());
466
467 mdjvu_set_clean(options, clean);
468 mdjvu_set_verbose(options, verbose);
469 mdjvu_set_no_prototypes(options, no_prototypes);
470 mdjvu_set_report(options, report);
471 mdjvu_set_averaging(options, averaging);
472 mdjvu_set_report_total_pages(options, n);
473
474 /* compressing */
475 if (pages_per_dict <= 0) pages_per_dict = n;
476 if (pages_per_dict > n) pages_per_dict = n;
477 images = MDJVU_MALLOCV(mdjvu_image_t, pages_per_dict);
478 pages_compressed = 0;
479
480 while (n - pages_compressed)
481 {
482 int32 pages_to_compress = n - pages_compressed;
483 if (pages_to_compress > pages_per_dict)
484 pages_to_compress = pages_per_dict;
485
486 mdjvu_set_report_start_page(options, pages_compressed + 1);
487
488 for (i = 0; i < pages_to_compress; i++)
489 {
490 if (multipage_tiff)
491 bitmap = load_bitmap(pages[0], pages_compressed + i);
492 else
493 bitmap = load_bitmap(pages[pages_compressed + i], 0);
494 images[i] = split_and_destroy(bitmap);
495 if (report)
496 printf(_("Loading: %d of %d completed\n"), pages_compressed + i + 1, n);
497 }
498
499 dict = mdjvu_compress_multipage(pages_to_compress, images, options);
500
501 path = get_page_or_dict_name(elements, el, strip_dir(pages[multipage_tiff ? 0 : pages_compressed]));
502 dict_name = MDJVU_MALLOCV(char, strlen(path) + strlen(dict_suffix) - 2);
503 strcpy(dict_name, path);
504 replace_suffix(dict_name, dict_suffix);
505
506 if (!indirect)
507 sizes[el] = mdjvu_file_save_djvu_dictionary(dict, (mdjvu_file_t) tf, 0, &error, erosion);
508 else
509 sizes[el] = mdjvu_save_djvu_dictionary(dict, dict_name, &error, erosion);
510
511 if (!sizes[el])
512 {
513 fprintf(stderr, "%s: %s\n", dict_name, mdjvu_get_error_message(error));
514 exit(1);
515 }
516 elements[el++] = dict_name;
517
518 for (i = 0; i < pages_to_compress; i++)
519 {
520 if (i > 0)
521 path = get_page_or_dict_name(elements, el, strip_dir(pages[multipage_tiff ? 0 : pages_compressed + i]));
522
523 if (verbose)
524 printf(_("saving page #%d into %s using dictionary %s\n"), pages_compressed + i + 1, path, dict_name);
525
526 if (!indirect)
527 sizes[el] = mdjvu_file_save_djvu_page(images[i], (mdjvu_file_t) tf, strip_dir(dict_name), 0, &error, erosion);
528 else
529 sizes[el] = mdjvu_save_djvu_page(images[i], path, strip_dir(dict_name), &error, erosion);
530 if (!sizes[el])
531 {
532 fprintf(stderr, "%s: %s\n", path, mdjvu_get_error_message(error));
533 exit(1);
534 }
535 elements[el++] = path;
536 mdjvu_image_destroy(images[i]);
537 if (report)
538 printf(_("Saving: %d of %d completed\n"), pages_compressed + i + 1, n);
539 }
540 mdjvu_image_destroy(dict);
541 pages_compressed += pages_to_compress;
542 }
543 if (!indirect)
544 {
545 f = fopen(outname, "wb");
546 if (!f)
547 {
548 fprintf(stderr, "%s: %s\n", outname, (const char *) mdjvu_get_error(mdjvu_error_fopen_write));
549 exit(1);
550 }
551 mdjvu_file_save_djvu_dir(elements, sizes, el, (mdjvu_file_t) f, (mdjvu_file_t) tf, &error);
552 fclose(tf);
553 fclose(f);
554 }
555 else
556 mdjvu_save_djvu_dir(elements,sizes,el,outname,&error);
557
558 for (i=0; i<el; i++) MDJVU_FREEV(elements[i]);
559 MDJVU_FREEV(elements);
560 MDJVU_FREEV(sizes);
561
562 /* destroying */
563 mdjvu_compression_options_destroy(options);
564
565 MDJVU_FREEV(images);
566 }
567
568 /* same_option(foo, "opt") returns 1 in three cases:
569 *
570 * foo is "o" (first letter of opt)
571 * foo is "opt"
572 * foo is "-opt"
573 */
574 static int same_option(const char *option, const char *s)
575 {
576 if (option[0] == s[0] && !option[1]) return 1;
577 if (!strcmp(option, s)) return 1;
578 if (option[0] == '-' && !strcmp(option + 1, s)) return 1;
579 return 0;
580 }
581
582 static int process_options(int argc, char **argv)
583 {
584 int i;
585 for (i = 1; i < argc && argv[i][0] == '-'; i++)
586 {
587 char *option = argv[i] + 1;
588 if (same_option(option, "verbose"))
589 verbose = 1;
590 else if (same_option(option, "smooth"))
591 smooth = 1;
592 else if (same_option(option, "match"))
593 match = 1;
594 else if (same_option(option, "Match"))
595 Match = 1;
596 else if (same_option(option, "no-prototypes"))
597 no_prototypes = 1;
598 else if (same_option(option, "erosion"))
599 erosion = 1;
600 else if (same_option(option, "clean"))
601 clean = 1;
602 else if (same_option(option, "warnings"))
603 warnings = 1;
604 else if (same_option(option, "report"))
605 report = 1;
606 else if (same_option(option, "Averaging"))
607 averaging = 1;
608 else if (same_option(option, "lossy"))
609 {
610 smooth = 1;
611 match = 1;
612 erosion = 1;
613 clean = 1;
614 averaging = 1;
615 }
616 else if (same_option(option, "Lossy"))
617 {
618 smooth = 1;
619 Match = match = 1;
620 erosion = 1;
621 clean = 1;
622 averaging = 1;
623 }
624 else if (same_option(option, "pages-per-dict"))
625 {
626 i++;
627 if (i == argc) show_usage_and_exit();
628 pages_per_dict = atoi(argv[i]);
629 if (pages_per_dict < 0)
630 {
631 fprintf(stderr, _("bad --pages-per-dict value\n"));
632 exit(2);
633 }
634 }
635 else if (same_option(option, "dpi"))
636 {
637 i++;
638 if (i == argc) show_usage_and_exit();
639 dpi = atoi(argv[i]);
640 dpi_specified = 1;
641 if (dpi < 20 || dpi > 2000)
642 {
643 fprintf(stderr, _("bad resolution\n"));
644 exit(2);
645 }
646 }
647 else if (same_option(option, "aggression"))
648 {
649 i++;
650 if (i == argc) show_usage_and_exit();
651 aggression = atoi(argv[i]);
652 match = 1;
653 }
654 else if (same_option(option, "Xtension"))
655 {
656 i++;
657 if (i == argc) show_usage_and_exit();
658 dict_suffix = argv[i];
659 }
660 else if (same_option(option, "indirect"))
661 indirect = 1;
662 else
663 {
664 fprintf(stderr, _("unknown option: %s\n"), argv[i]);
665 exit(2);
666 }
667 }
668 return i;
669 }
670
671 int main(int argc, char **argv)
672 {
673 int arg_start;
674 #ifdef HAVE_TIFF
675 int tiff_cnt;
676 #endif
677
678 setlocale(LC_ALL, "");
679 #ifdef HAVE_GETTEXT
680 bindtextdomain("minidjvu", LOCALEDIR);
681 textdomain("minidjvu");
682 #endif
683
684
685 arg_start = process_options(argc, argv);
686 if ( dict_suffix == NULL ) dict_suffix = "iff";
687
688 argc -= arg_start - 1;
689 argv += arg_start - 1;
690
691 if (argc < 3)
692 show_usage_and_exit();
693
694 if (argc > 3)
695 {
696 multipage_encode(argc - 2, argv + 1, argv[argc - 1], 0);
697 }
698 #ifdef HAVE_TIFF
699 else if (decide_if_tiff(argv[1]) && (tiff_cnt = mdjvu_get_tiff_page_count(argv[1])) > 1 )
700 {
701 multipage_encode(tiff_cnt, argv + 1, argv[argc - 1], 1);
702 }
703 #endif
704 else if (decide_if_djvu(argv[2]))
705 {
706 encode(argc, argv);
707 }
708 else
709 {
710 if (decide_if_djvu(argv[1]))
711 decode(argc, argv);
712 else
713 filter(argc, argv);
714 }
715
716 if (verbose) printf("\n");
717 #ifndef NDEBUG
718 if (alive_bitmap_counter)
719 printf(_("alive_bitmap_counter = %d\n"), alive_bitmap_counter);
720 #endif
721 return 0;
722 }