about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2007-03-25 02:25:48 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2007-03-25 02:25:48 +0000
commitfcdd46b1c437b5c4c9103f96eced98dc494bea62 (patch)
treeb7dc4f70a1ce55ee40b4daf10ca9a7b1f55ae434
parent4c731873f76411195fd17185474e16690985e6a5 (diff)
downloadnetpbm-mirror-fcdd46b1c437b5c4c9103f96eced98dc494bea62.tar.gz
netpbm-mirror-fcdd46b1c437b5c4c9103f96eced98dc494bea62.tar.xz
netpbm-mirror-fcdd46b1c437b5c4c9103f96eced98dc494bea62.zip
Run Ppmtogif input through Pnmremap before passing to Pamtogif if user says -mapfile
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@257 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/ppm/ppmtogif.c70
-rw-r--r--doc/HISTORY4
2 files changed, 59 insertions, 15 deletions
diff --git a/converter/ppm/ppmtogif.c b/converter/ppm/ppmtogif.c
index 2b8a2596..2718af88 100644
--- a/converter/ppm/ppmtogif.c
+++ b/converter/ppm/ppmtogif.c
@@ -2,12 +2,18 @@
 
    Pamtogif replaced Ppmtogif in Netpbm 10.37 (December 2006).
 
-   The only significant way Pamtogif is not backward compatible with
-   old Ppmtogif is that Pamtogif does not have a -alpha option.
+   The only significant ways Pamtogif are not backward compatible with
+   old Ppmtogif are:
+
+     - Pamtogif does not have a -alpha option.
+
+     - Pamtogif requires a user-specififed map file (-mapfile) to
+       match the input in depth.
 */
 #define _BSD_SOURCE   /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
+#include <assert.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/stat.h>
@@ -42,7 +48,7 @@ struct cmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char *input_filespec; /* Filespec of input file */
+    const char *inputFileName;  /* Name of input file */
     const char *alpha_filespec; /* Filespec of alpha file; NULL if none */
     const char *alphacolor;     /* -alphacolor option value or default */
     unsigned int interlace;     /* -interlace option value */
@@ -146,12 +152,12 @@ parseCommandLine(int argc, char ** argv,
         handleLatex2htmlHack();
 
     if (argc-1 == 0) 
-        cmdlineP->input_filespec = "-";
+        cmdlineP->inputFileName = "-";
     else if (argc-1 != 1)
         pm_error("Program takes zero or one argument (filename).  You "
                  "specified %d", argc-1);
     else
-        cmdlineP->input_filespec = argv[1];
+        cmdlineP->inputFileName = argv[1];
 
     if (cmdlineP->alpha_filespec && cmdlineP->transparent)
         pm_error("You cannot specify both -alpha and -transparent.");
@@ -159,6 +165,43 @@ parseCommandLine(int argc, char ** argv,
 
 
 
+static void
+openPnmremapStream(const char * const inputFileName,
+                   const char * const mapFileName,
+                   bool         const verbose,
+                   FILE **      const pnmremapPipeP) {
+/*----------------------------------------------------------------------------
+   Create a process to run the image in file inputFileName[] through
+   Pnmremap, remapping it to the colors in mapFileName[].  Have it
+   write its output to a pipe and return as *pnmremapPipeP the other
+   end of that pipe.
+-----------------------------------------------------------------------------*/
+    FILE * pnmremapPipe;
+    const char * pnmremapCommand;
+
+    assert(inputFileName != NULL);
+    assert(mapFileName != NULL);
+
+    asprintfN(&pnmremapCommand, "pnmremap -mapfile='%s' %s",
+              mapFileName, inputFileName);
+
+    if (verbose)
+        pm_message("Preprocessing Pamtogif input with shell command '%s'",
+                   pnmremapCommand);
+
+    pnmremapPipe = popen(pnmremapCommand, "r");
+
+    if (pnmremapPipe == NULL)
+        pm_error("Shell command '%s', via popen(), to prepare the input "
+                 "for Pamtogif, failed.", pnmremapCommand);
+    else
+        *pnmremapPipeP = pnmremapPipe;
+
+    strfree(pnmremapCommand);
+}
+
+
+
 static const char *
 pamtogifCommand(const char *       const arg0,
                 struct cmdlineInfo const cmdline) {
@@ -168,7 +211,6 @@ pamtogifCommand(const char *       const arg0,
     const char * retval;
 
     const char * commandVerb;
-    const char * mapfileOpt;
     const char * transparentOpt;
     const char * commentOpt;
 
@@ -189,11 +231,6 @@ pamtogifCommand(const char *       const arg0,
     } else
         commandVerb = strdup(pamtogifName);
 
-    if (cmdline.mapfile)
-        asprintfN(&mapfileOpt, "-mapfile=%s", cmdline.mapfile);
-    else
-        mapfileOpt = strdup("");
-
     if (cmdline.transparent)
         asprintfN(&transparentOpt, "-transparent=%s", cmdline.transparent);
     else
@@ -204,18 +241,16 @@ pamtogifCommand(const char *       const arg0,
     else
         commentOpt = strdup("");
 
-    asprintfN(&retval, "%s - -alphacolor=%s %s %s %s %s %s %s %s",
+    asprintfN(&retval, "%s - -alphacolor=%s %s %s %s %s %s %s",
               commandVerb,
               cmdline.alphacolor,
               cmdline.interlace ? "-interlace" : "",
               cmdline.sort ? "-sort" : "",
-              mapfileOpt,
               transparentOpt,
               commentOpt,
               cmdline.nolzw ? "-nolzw" : "",
               cmdline.verbose ? "-verbose" : "");
     
-    strfree(mapfileOpt);
     strfree(transparentOpt);
     strfree(commentOpt);
 
@@ -352,6 +387,12 @@ main(int    argc,
 
     parseCommandLine(argc, argv, &cmdline);
 
+    if (cmdline.mapfile)
+        openPnmremapStream(cmdline.inputFileName, cmdline.mapfile,
+                           cmdline.verbose, &ifP);
+    else
+        ifP = pm_openr(cmdline.inputFileName);
+        
     command = pamtogifCommand(argv[0], cmdline);
 
     if (cmdline.verbose)
@@ -362,7 +403,6 @@ main(int    argc,
     if (pipeToPamtogif == NULL)
         pm_error("Shell command '%s', via popen(), failed.", command);
 
-    ifP = pm_openr(cmdline.input_filespec);
     pnm_readpaminit(ifP, &inPam, PAM_STRUCT_SIZE(allocation_depth));
 
     feedPamtogif(&inPam, cmdline.alpha_filespec, pipeToPamtogif);
diff --git a/doc/HISTORY b/doc/HISTORY
index 5d8eb361..317a6bbb 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -56,6 +56,10 @@ not yet  BJH  Release 10.38.0
 
               pamtogif: fix crash with -mapfile.
 
+              ppmtogif: handle case that map file and input file are not
+              the same depth, as was the case with the original ppmtogif.
+              Run the input through pnmremap.
+
               pamtoxvmini: fix bug: produces garbage when maxval is
               not 255.