jbig2dec

Ralph Giles <[email protected]> Fri, 12 Jul 2002 17:03:54 -0700
Newsgroups gmane.comp.printing.ghostscript.jbig2dec.cvs
Message-ID <[email protected]>
Update of /cvsroot/jbig2dec/jbig2dec
In directory usw-pr-cvs1:/tmp/cvs-serv27818

Modified Files:
	jbig2dec.c 
Log Message:
ui cleanup. output filename generation is broken

Index: jbig2dec.c
===================================================================
RCS file: /cvsroot/jbig2dec/jbig2dec/jbig2dec.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- jbig2dec.c	9 Jul 2002 09:45:32 -0000	1.29
+++ jbig2dec.c	13 Jul 2002 00:03:52 -0000	1.30
@@ -98,12 +98,12 @@
 					long_options[option_idx].name);
 					break;
 			case 'q':
-				params->verbose=0;
+				params->verbose = 0;
 				break;
 			case 'h':
 			case '?':
-				print_usage();
-				exit (0);
+				params->mode = usage;
+                                break;
 			case 'd':
 				params->mode=dump;
 				break;
@@ -143,6 +143,54 @@
     return 0;
 }
 
+static char *
+make_output_filename(const char *input_filename, const char *extension)
+{
+    char *output_filename;
+    const char *c, *e;
+    int len;
+    
+    if (extension == NULL) {
+        fprintf(stderr, "make_output_filename called with no extension!\n");
+        exit (1);
+    }
+    
+    if (input_filename == NULL)
+      output_filename = "out";
+      
+    /* strip any leading path */
+    c = strrchr(input_filename, '/'); /* *nix */
+    if (c == NULL)
+      c = strrchr(input_filename, '\\'); /* win32/dos */
+    if (c != NULL)
+      c++; /* skip the path separator */
+    else
+      c = input_filename; /* no leading path */
+    /* make sure we haven't just stripped the last character */
+    if (*c = '\0')
+      c = "out";
+        
+    /* strip the extension */
+    len = strlen(c);
+    e = strrchr(c, '.');
+    if (e != NULL)
+      len -= strlen(e);
+    
+    /* allocate enough space for the base + ext */
+    output_filename = malloc(len + strlen(extension) + 1);
+    if (output_filename == NULL) {
+        fprintf(stderr, "couldn't allocate memory for output_filename\n");
+        exit (1);
+    }
+    
+    strncpy(output_filename, c, len);
+    strncpy(output_filename + len, extension, strlen(extension));
+    *(output_filename + len + strlen(extension)) = '\0';
+    
+    /* return the new string */
+    return (output_filename);
+}
+
 static int
 write_page_image(jbig2dec_params_t *params, Jbig2Image *image)
 {
@@ -174,19 +222,23 @@
   FILE *f = NULL, *f_page = NULL;
   Jbig2Ctx *ctx;
   uint8_t buf[4096];
-  jbig2dec_params_t params = {usage,1,NULL};
+  jbig2dec_params_t params = {render,1,NULL};
   int filearg;
 
+  fprintf(stderr, "heisenbug detector: %s\n", make_output_filename(NULL, ".out"));
+  
+  
   filearg = parse_options(argc, argv, &params);
 
-  if (params.output_file == NULL)
-    {
-#ifdef HAVE_LIBPNG
-      params.output_file = "out.png";
-#else
-      params.output_file = "out.pbm";
-#endif
-    }
+  switch (params.mode) {
+    case usage:
+        print_usage();
+        exit (0);
+        break;
+    case dump:
+        fprintf(stderr, "Sorry, segment dump not yet implemented\n");
+        break;
+    case render:
     
   if ((argc - filearg) == 1)
   // only one argument--open as a jbig2 file
@@ -263,6 +315,15 @@
     if (f_page != NULL)
       jbig2_complete_page(ctx);
     
+    if (params.output_file == NULL)
+      {
+#ifdef HAVE_LIBPNG
+        params.output_file = make_output_filename(argv[filearg], ".png");
+#else
+        params.output_file = make_output_filename(argv[filearg], ".pbm");
+#endif
+      }
+    
     /* retrieve and write out all the completed pages */
     while ((image = jbig2_page_out(ctx)) != NULL) {
       write_page_image(&params, image);
@@ -272,6 +333,8 @@
   
   jbig2_ctx_free(ctx);
 
+  } /* end params.mode switch */
+  
   // fin
   return 0;
 }