Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2001-2009 Artifex Software, Inc.
  5.  
  6.     This software is distributed under license and may not
  7.     be copied, modified or distributed except as expressly
  8.     authorized under the terms of the license contained in
  9.     the file LICENSE in this distribution.
  10.  
  11.     For further licensing information refer to http://artifex.com/ or
  12.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  13.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  14. */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19.  
  20. #ifndef PACKAGE
  21. #define PACKAGE "jbig2dec"
  22. #endif
  23. #ifndef VERSION
  24. #define VERSION "unknown-version"
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <string.h>
  31.  
  32. #ifdef HAVE_GETOPT_H
  33. # include <getopt.h>
  34. #else
  35. # include "getopt.h"
  36. #endif
  37.  
  38. #include "os_types.h"
  39. #include "sha1.h"
  40.  
  41. #include "jbig2.h"
  42. #include "jbig2_image.h"
  43.  
  44. typedef enum {
  45.     usage,dump,render
  46. } jbig2dec_mode;
  47.  
  48. typedef enum {
  49.     jbig2dec_format_jbig2,
  50.     jbig2dec_format_pbm,
  51.     jbig2dec_format_png,
  52.     jbig2dec_format_none
  53. } jbig2dec_format;
  54.  
  55. typedef struct {
  56.         jbig2dec_mode mode;
  57.         int verbose, hash;
  58.         SHA1_CTX *hash_ctx;
  59.         char *output_file;
  60.         jbig2dec_format output_format;
  61. } jbig2dec_params_t;
  62.  
  63. static int print_version(void);
  64. static int print_usage(void);
  65.  
  66. /* page hashing functions */
  67. static void
  68. hash_init(jbig2dec_params_t *params)
  69. {
  70.     params->hash_ctx = malloc(sizeof(SHA1_CTX));
  71.     if (params->hash_ctx == NULL) {
  72.         fprintf(stderr, "unable to allocate hash state\n");
  73.         params->hash = 0;
  74.         return;
  75.     } else {
  76.         SHA1_Init(params->hash_ctx);
  77.     }
  78. }
  79.  
  80. static void
  81. hash_image(jbig2dec_params_t *params, Jbig2Image *image)
  82. {
  83.     unsigned int N = image->stride * image->height;
  84.     SHA1_Update(params->hash_ctx, image->data, N);
  85. }
  86.  
  87. static void
  88. hash_print(jbig2dec_params_t *params, FILE *out)
  89. {
  90.     unsigned char md[SHA1_DIGEST_SIZE];
  91.     char digest[2*SHA1_DIGEST_SIZE + 1];
  92.     int i;
  93.  
  94.     SHA1_Final(params->hash_ctx, md);
  95.     for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
  96.         snprintf(&(digest[2*i]), 3, "%02x", md[i]);
  97.     }
  98.     fprintf(out, "%s", digest);
  99. }
  100.  
  101. static void
  102. hash_free(jbig2dec_params_t *params)
  103. {
  104.     free(params->hash_ctx);
  105.     params->hash_ctx = NULL;
  106. }
  107.  
  108. static int
  109. set_output_format(jbig2dec_params_t *params, const char *format)
  110. {
  111. #ifdef HAVE_LIBPNG
  112.     /* this should really by strncasecmp()
  113.        TODO: we need to provide our own for portability */
  114.     if (!strncmp(format, "png", 3) || !strncmp(format, "PNG", 3)) {
  115.         params->output_format=jbig2dec_format_png;
  116.         return 0;
  117.     }
  118. #endif
  119.     /* default to pbm */
  120.     params->output_format=jbig2dec_format_pbm;
  121.  
  122.     return 0;
  123. }
  124.  
  125. static int
  126. parse_options(int argc, char *argv[], jbig2dec_params_t *params)
  127. {
  128.         static struct option long_options[] = {
  129.                 {"version", 0, NULL, 'V'},
  130.                 {"help", 0, NULL, 'h'},
  131.                 {"quiet", 0, NULL, 'q'},
  132.                 {"verbose", 2, NULL, 'v'},
  133.                 {"dump", 0, NULL, 'd'},
  134.                 {"hash", 0, NULL, 'm'},
  135.                 {"output", 1, NULL, 'o'},
  136.                 {"format", 1, NULL, 't'},
  137.                 {NULL, 0, NULL, 0}
  138.         };
  139.         int option_idx = 1;
  140.         int option;
  141.  
  142.         while (1) {
  143.                 option = getopt_long(argc, argv,
  144.                         "Vh?qvdo:t:", long_options, &option_idx);
  145.                 if (option == -1) break;
  146.  
  147.                 switch (option) {
  148.                         case 0: /* unknown long option */
  149.                                 if (!params->verbose) fprintf(stdout,
  150.                                         "unrecognized option: --%s\n",
  151.                                         long_options[option_idx].name);
  152.                                         break;
  153.                         case 'q':
  154.                                 params->verbose = 0;
  155.                                 break;
  156.                         case 'v':
  157.                                 if (optarg) params->verbose = atoi(optarg);
  158.                                 else params->verbose = 2;
  159.                                 break;
  160.                         case 'h':
  161.                         case '?':
  162.                                 params->mode = usage;
  163.                                 break;
  164.                         case 'V':
  165.                                 /* the GNU Coding Standards suggest --version
  166.                                    should override all other options */
  167.                                 print_version();
  168.                                 exit(0);
  169.                                 break;
  170.                         case 'd':
  171.                                 params->mode=dump;
  172.                                 break;
  173.                         case 'm':
  174.                                 params->hash = 1;
  175.                                 break;
  176.                         case 'o':
  177.                                 params->output_file = strdup(optarg);
  178.                                 break;
  179.                         case 't':
  180.                                 set_output_format(params, optarg);
  181.                                 break;
  182.                         default:
  183.                                 if (!params->verbose) fprintf(stdout,
  184.                                         "unrecognized option: -%c\n", option);
  185.                                 break;
  186.                 }
  187.         }
  188.         return (optind);
  189. }
  190.  
  191. static int
  192. print_version (void)
  193. {
  194.     fprintf(stdout, "%s %s\n", PACKAGE, VERSION);
  195.     return 0;
  196. }
  197.  
  198. static int
  199. print_usage (void)
  200. {
  201.   fprintf(stderr,
  202.     "Usage: jbig2dec [options] <file.jbig2>\n"
  203.     "   or  jbig2dec [options] <global_stream> <page_stream>\n"
  204.     "\n"
  205.     "  When invoked with a single file, it attempts to parse it as\n"
  206.     "  a normal jbig2 file. Invoked with two files, it treats the\n"
  207.     "  first as the global segments, and the second as the segment\n"
  208.     "  stream for a particular page. This is useful for examining\n"
  209.     "  embedded streams.\n"
  210.     "\n"
  211.     "  available options:\n"
  212.     "    -h --help      this usage summary\n"
  213.     "    -q --quiet     suppress diagnostic output\n"
  214.     "    -v --verbose   set the verbosity level\n"
  215.     "    -d --dump      print the structure of the jbig2 file\n"
  216.     "                   rather than explicitly decoding\n"
  217.     "       --version   program name and version information\n"
  218.     "       --hash      print a hash of the decoded document\n"
  219.     "    -o <file>      send decoded output to <file>\n"
  220.     "                   Defaults to the the input with a different\n"
  221.     "                   extension. Pass '-' for stdout.\n"
  222.     "    -t <type>      force a particular output file format\n"
  223.  #ifdef HAVE_LIBPNG
  224.     "                   supported options are 'png' and 'pbm'\n"
  225.  #else
  226.     "                   the only supported option is 'pbm'\n"
  227.  #endif
  228.     "\n"
  229.   );
  230.  
  231.   return 1;
  232. }
  233.  
  234. static int
  235. error_callback(void *error_callback_data, const char *buf, Jbig2Severity severity,
  236.                int32_t seg_idx)
  237. {
  238.     const jbig2dec_params_t *params = error_callback_data;
  239.     char *type;
  240.     char segment[22];
  241.  
  242.     switch (severity) {
  243.         case JBIG2_SEVERITY_DEBUG:
  244.             if (params->verbose < 3) return 0;
  245.             type = "DEBUG"; break;;
  246.         case JBIG2_SEVERITY_INFO:
  247.             if (params->verbose < 2) return 0;
  248.             type = "info"; break;;
  249.         case JBIG2_SEVERITY_WARNING:
  250.             if (params->verbose < 1) return 0;
  251.             type = "WARNING"; break;;
  252.         case JBIG2_SEVERITY_FATAL: type = "FATAL ERROR"; break;;
  253.         default: type = "unknown message"; break;;
  254.     }
  255.     if (seg_idx == -1) segment[0] = '\0';
  256.     else snprintf(segment, sizeof(segment), "(segment 0x%02x)", seg_idx);
  257.  
  258.     fprintf(stderr, "jbig2dec %s %s %s\n", type, buf, segment);
  259.  
  260.     return 0;
  261. }
  262.  
  263. static char *
  264. make_output_filename(const char *input_filename, const char *extension)
  265. {
  266.     char *output_filename;
  267.     const char *c, *e;
  268.     int len;
  269.  
  270.     if (extension == NULL) {
  271.         fprintf(stderr, "make_output_filename called with no extension!\n");
  272.         exit (1);
  273.     }
  274.  
  275.     if (input_filename == NULL)
  276.       c = "out";
  277.     else {
  278.       /* strip any leading path */
  279.       c = strrchr(input_filename, '/'); /* *nix */
  280.       if (c == NULL)
  281.         c = strrchr(input_filename, '\\'); /* win32/dos */
  282.       if (c != NULL)
  283.         c++; /* skip the path separator */
  284.       else
  285.         c = input_filename; /* no leading path */
  286.     }
  287.  
  288.     /* make sure we haven't just stripped the last character */
  289.     if (*c == '\0')
  290.       c = "out";
  291.  
  292.     /* strip the extension */
  293.     len = strlen(c);
  294.     e = strrchr(c, '.');
  295.     if (e != NULL)
  296.       len -= strlen(e);
  297.  
  298.     /* allocate enough space for the base + ext */
  299.     output_filename = malloc(len + strlen(extension) + 1);
  300.     if (output_filename == NULL) {
  301.         fprintf(stderr, "couldn't allocate memory for output_filename\n");
  302.         exit (1);
  303.     }
  304.  
  305.     strncpy(output_filename, c, len);
  306.     strncpy(output_filename + len, extension, strlen(extension));
  307.     *(output_filename + len + strlen(extension)) = '\0';
  308.  
  309.     /* return the new string */
  310.     return (output_filename);
  311. }
  312.  
  313. static int
  314. write_page_image(jbig2dec_params_t *params, Jbig2Image *image)
  315. {
  316.       if (!strncmp(params->output_file, "-", 2))
  317.         {
  318.           switch (params->output_format) {
  319. #ifdef HAVE_LIBPNG
  320.             case jbig2dec_format_png:
  321.               jbig2_image_write_png(image, stdout);
  322.               break;
  323. #endif
  324.             case jbig2dec_format_pbm:
  325.               jbig2_image_write_pbm(image, stdout);
  326.               break;
  327.             default:
  328.               fprintf(stderr, "unsupported output format.\n");
  329.               return 1;
  330.           }
  331.         }
  332.       else
  333.         {
  334.           if (params->verbose > 1)
  335.             fprintf(stderr, "saving decoded page as '%s'\n", params->output_file);
  336.           switch (params->output_format) {
  337. #ifdef HAVE_LIBPNG
  338.             case jbig2dec_format_png:
  339.               jbig2_image_write_png_file(image, params->output_file);
  340.               break;
  341. #endif
  342.             case jbig2dec_format_pbm:
  343.               jbig2_image_write_pbm_file(image, params->output_file);
  344.               break;
  345.             default:
  346.               fprintf(stderr, "unsupported output format.\n");
  347.               return 1;
  348.           }
  349.         }
  350.  
  351.   return 0;
  352. }
  353.  
  354. static int
  355. write_document_hash(jbig2dec_params_t *params)
  356. {
  357.     FILE *out;
  358.  
  359.     if (!strncmp(params->output_file, "-", 2)) {
  360.         out = stderr;
  361.     } else {
  362.         out = stdout;
  363.     }
  364.  
  365.     fprintf(out, "Hash of decoded document: ");
  366.     hash_print(params, out);
  367.     fprintf(out, "\n");
  368.  
  369.     return 0;
  370. }
  371.  
  372. int
  373. main (int argc, char **argv)
  374. {
  375.   FILE *f = NULL, *f_page = NULL;
  376.   Jbig2Ctx *ctx;
  377.   uint8_t buf[4096];
  378.   jbig2dec_params_t params;
  379.   int filearg;
  380.  
  381.   /* set defaults */
  382.   params.mode = render;
  383.   params.verbose = 1;
  384.   params.hash = 0;
  385.   params.output_file = NULL;
  386.   params.output_format = jbig2dec_format_none;
  387.  
  388.   filearg = parse_options(argc, argv, &params);
  389.  
  390.   if (params.hash) hash_init(&params);
  391.  
  392.   switch (params.mode) {
  393.     case usage:
  394.         print_usage();
  395.         exit (0);
  396.         break;
  397.     case dump:
  398.         fprintf(stderr, "Sorry, segment dump not yet implemented\n");
  399.         break;
  400.     case render:
  401.  
  402.   if ((argc - filearg) == 1)
  403.   /* only one argument--open as a jbig2 file */
  404.     {
  405.       char *fn = argv[filearg];
  406.  
  407.       f = fopen(fn, "rb");
  408.       if (f == NULL)
  409.         {
  410.           fprintf(stderr, "error opening %s\n", fn);
  411.           return 1;
  412.         }
  413.     }
  414.   else if ((argc - filearg) == 2)
  415.   /* two arguments open as separate global and page streams */
  416.     {
  417.       char *fn = argv[filearg];
  418.       char *fn_page = argv[filearg+1];
  419.  
  420.       f = fopen(fn, "rb");
  421.       if (f == NULL)
  422.         {
  423.           fprintf(stderr, "error opening %s\n", fn);
  424.           return 1;
  425.         }
  426.  
  427.       f_page = fopen(fn_page, "rb");
  428.       if (f_page == NULL)
  429.         {
  430.           fprintf(stderr, "error opening %s\n", fn_page);
  431.           return 1;
  432.         }
  433.     }
  434.   else
  435.   /* any other number of arguments */
  436.     return print_usage();
  437.  
  438.   ctx = jbig2_ctx_new(NULL, f_page != NULL ? JBIG2_OPTIONS_EMBEDDED : 0,
  439.                       NULL,
  440.                       error_callback, &params);
  441.  
  442.   /* pull the whole file/global stream into memory */
  443.   for (;;)
  444.     {
  445.       int n_bytes = fread(buf, 1, sizeof(buf), f);
  446.       if (n_bytes <= 0)
  447.         break;
  448.       if (jbig2_data_in(ctx, buf, n_bytes))
  449.         break;
  450.     }
  451.   fclose(f);
  452.  
  453.   /* if there's a local page stream read that in its entirety */
  454.   if (f_page != NULL)
  455.     {
  456.       Jbig2GlobalCtx *global_ctx = jbig2_make_global_ctx(ctx);
  457.       ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, global_ctx,
  458.                          error_callback, &params);
  459.       for (;;)
  460.         {
  461.           int n_bytes = fread(buf, 1, sizeof(buf), f_page);
  462.           if (n_bytes <= 0)
  463.             break;
  464.           if (jbig2_data_in(ctx, buf, n_bytes))
  465.             break;
  466.         }
  467.       fclose(f_page);
  468.       jbig2_global_ctx_free(global_ctx);
  469.     }
  470.  
  471.   /* retrieve and output the returned pages */
  472.   {
  473.     Jbig2Image *image;
  474.  
  475.     /* work around broken CVision embedded streams */
  476.     if (f_page != NULL)
  477.       jbig2_complete_page(ctx);
  478.  
  479.     if (params.output_file == NULL)
  480.       {
  481. #ifdef HAVE_LIBPNG
  482.         params.output_file = make_output_filename(argv[filearg], ".png");
  483.         params.output_format = jbig2dec_format_png;
  484. #else
  485.         params.output_file = make_output_filename(argv[filearg], ".pbm");
  486.         params.output_format = jbig2dec_format_pbm;
  487. #endif
  488.       } else {
  489.         int len = strlen(params.output_file);
  490.         if ((len >= 3) && (params.output_format == jbig2dec_format_none))
  491.           /* try to set the output type by the given extension */
  492.           set_output_format(&params, params.output_file + len - 3);
  493.       }
  494.  
  495.     /* retrieve and write out all the completed pages */
  496.     while ((image = jbig2_page_out(ctx)) != NULL) {
  497.       write_page_image(&params, image);
  498.       if (params.hash) hash_image(&params, image);
  499.       jbig2_release_page(ctx, image);
  500.     }
  501.     if (params.hash) write_document_hash(&params);
  502.   }
  503.  
  504.   jbig2_ctx_free(ctx);
  505.  
  506.   } /* end params.mode switch */
  507.  
  508.   if (params.output_file) free(params.output_file);
  509.   if (params.hash) hash_free(&params);
  510.  
  511.   /* fin */
  512.   return 0;
  513. }
  514.