about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-10-10 00:31:14 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-10-10 00:31:14 +0000
commitde71c6d3a277244a660cd602a418818aa9659861 (patch)
tree6d9b29d891d4f8c886226b4419e296d0b447bbe6 /converter
parentd113bea623ccd7b95f2672fbd1fcb88b550196a8 (diff)
downloadnetpbm-mirror-de71c6d3a277244a660cd602a418818aa9659861.tar.gz
netpbm-mirror-de71c6d3a277244a660cd602a418818aa9659861.tar.xz
netpbm-mirror-de71c6d3a277244a660cd602a418818aa9659861.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4742 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter')
-rw-r--r--converter/other/fiasco/Makefile2
-rw-r--r--converter/other/fiasco/binerror.c136
-rw-r--r--converter/other/fiasco/binerror.h48
-rw-r--r--converter/other/fiasco/fiascotopnm.c323
-rw-r--r--converter/other/fiasco/params.c28
-rw-r--r--converter/other/fiasco/params.h2
-rw-r--r--converter/other/fiasco/pnmtofiasco.c356
7 files changed, 332 insertions, 563 deletions
diff --git a/converter/other/fiasco/Makefile b/converter/other/fiasco/Makefile
index 392e843c..5ce529fd 100644
--- a/converter/other/fiasco/Makefile
+++ b/converter/other/fiasco/Makefile
@@ -25,7 +25,7 @@ FIASCOLIBS = codec/libfiasco_codec.a \
 	     output/libfiasco_output.a \
 	     lib/libfiasco_lib.a 
 
-ADDL_OBJECTS = binerror.o getopt.o getopt1.o params.o
+ADDL_OBJECTS = getopt.o getopt1.o params.o
 
 OBJECTS = $(BINARIES:%=%.o) $(ADDL_OBJECTS)
 
diff --git a/converter/other/fiasco/binerror.c b/converter/other/fiasco/binerror.c
deleted file mode 100644
index 7a1ac656..00000000
--- a/converter/other/fiasco/binerror.c
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- *  Error handling
- *
- *  Written by:         Stefan Frank
- *                      Ullrich Hafner
- *
- *  Credits:    Modelled after variable argument routines from Jef
- *              Poskanzer's pbmplus package.
- *
- *  This file is part of FIASCO (Fractal Image And Sequence COdec)
- *  Copyright (C) 1994-2000 Ullrich Hafner
- */
-
-/*
- *  $Date: 2000/03/20 21:29:59 $
- *  $Author: hafner $
- *  $Revision: 4.3 $
- *  $State: Exp $
- */
-
-#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
-#define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
-#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
-#define _ERROR_C
-
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-
-#if STDC_HEADERS
-#       include <stdarg.h>
-#       define VA_START(args, lastarg) va_start(args, lastarg)
-#else  /* not STDC_HEADERS */
-#       include <varargs.h>
-#       define VA_START(args, lastarg) va_start(args)
-#endif /* not STDC_HEADERS */
-#include <string.h>
-
-#if HAVE_SETJMP_H
-#       include <setjmp.h>
-#endif /* HAVE_SETJMP_H */
-
-#include "fiasco.h"
-#include "binerror.h"
-
-/*****************************************************************************
-
-                             global variables
-
-*****************************************************************************/
-
-int   error_line = 0;
-const char *error_file = NULL;
-
-/*****************************************************************************
-
-                             local variables
-
-*****************************************************************************/
-
-static const char *executable = "(name not initialized)";
-
-/*****************************************************************************
-
-                               public code
-
-*****************************************************************************/
-
-void
-init_error_handling (const char *name)
-/*
- *  Initialize filename of executable.
- *
- *  No return value.
- */
-{
-   if (name)
-      executable = strdup (name);
-}
-
-void
-_error (const char *format, ...)
-/*
- *  Print error message and exit.
- *
- *  No return value.
- */
-{
-   va_list      args;
-
-   VA_START (args, format);
-
-   fprintf (stderr, "%s: %s: line %d:\nError: ",
-            executable, error_file, error_line);
-   vfprintf (stderr, format, args);
-   fputc ('\n', stderr);
-   va_end(args);
-
-   exit (1);
-}
-
-void
-_file_error (const char *filename)
-/*
- *  Print file error message and exit.
- *
- *  No return value.
- */
-{
-   fprintf (stderr, "%s: %s: line %d:\nError: ",
-            executable, error_file, error_line);
-   perror (filename);
-
-   exit (2);
-}
-
-void
-_warning (const char *format, ...)
-/*
- *  Issue a warning and continue execution.
- *
- *  No return value.
- */
-{
-   va_list args;
-
-   VA_START (args, format);
-
-   fprintf (stderr, "%s: %s: line %d:\nWarning: ",
-            executable, error_file, error_line);
-   vfprintf (stderr, format, args);
-   fputc ('\n', stderr);
-
-   va_end (args);
-}
diff --git a/converter/other/fiasco/binerror.h b/converter/other/fiasco/binerror.h
deleted file mode 100644
index 50532be9..00000000
--- a/converter/other/fiasco/binerror.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- *
- *  Written by:         Stefan Frank
- *                      Ullrich Hafner
- *
- *  This file is part of FIASCO (Fractal Image And Sequence COdec)
- *  Copyright (C) 1994-2000 Ullrich Hafner
- */
-
-/*
- *  $Date: 2000/03/20 21:29:59 $
- *  $Author: hafner $
- *  $Revision: 4.3 $
- *  $State: Exp $
- */
-
-#ifndef BINERROR_H_INCLUDED
-#define BINERROR_H_INCLUDED
-
-#define error          error_line=__LINE__,error_file=__FILE__,_error
-#define warning        error_line=__LINE__,error_file=__FILE__,_warning
-#define file_error(fn) error_line=__LINE__,error_file=__FILE__,_file_error(fn)
-
-#ifdef _ERROR_C
-#define _EXTERN_TYPE
-#else
-#define _EXTERN_TYPE    extern
-#endif
-
-_EXTERN_TYPE int   error_line;
-_EXTERN_TYPE const char *error_file;
-
-void
-init_error_handling (const char *name);
-void
-_error (const char *format, ...);
-void
-_warning (const char *format, ...);
-void
-_file_error (const char *filename);
-
-#if HAVE_ASSERT_H
-#       include <assert.h>
-#else /* not HAVE_ASSERT_H */
-#       define assert(exp)      {if (!(exp)) error ("Assertion `" #exp " != NULL' failed.");}
-#endif /* not HAVE_ASSERT_H */
-
-#endif
diff --git a/converter/other/fiasco/fiascotopnm.c b/converter/other/fiasco/fiascotopnm.c
index eb3c1994..340f9d4e 100644
--- a/converter/other/fiasco/fiascotopnm.c
+++ b/converter/other/fiasco/fiascotopnm.c
@@ -1,5 +1,5 @@
 /*
- *  dwfa.c:     Decoding of WFA-files
+ *  Decode WFA-files
  *
  *  Written by:     Ullrich Hafner
  *          Michael Unger
@@ -25,75 +25,63 @@
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
+#include <getopt.h>
 
+#include "mallocvar.h"
 #include "nstring.h"
 
 #include "types.h"
 #include "macros.h"
 
-#include <getopt.h>
-
-#include "binerror.h"
 #include "misc.h"
 #include "params.h"
 #include "fiasco.h"
 
 
-/*****************************************************************************
 
-                prototypes
-
-*****************************************************************************/
-
-static int
-checkargs (int argc, char **argv, bool_t *double_resolution, bool_t *panel,
-           int *fps, char **image_name, fiasco_d_options_t **options);
 static void
-video_decoder (const char *wfa_name, const char *image_name, bool_t panel,
-               bool_t double_resolution, int fps, fiasco_d_options_t *options);
-static void
-get_output_template (const char *image_name, const char *wfa_name,
-                     bool_t color, char **basename, char **suffix);
-
-/*****************************************************************************
+getOutputTemplate(const char *  const imageName,
+                  const char *  const wfaName,
+                  bool_t        const color,
+                  const char ** const basename,
+                  const char ** const suffixP) {
+/*----------------------------------------------------------------------------
+
+  Generate image filename template for output of image sequences.
+  'wfa_name' is the filename of the WFA stream.
+  Images are either saved with filename 'basename'.'suffix' (still images)
+  or 'basename'.%03d.'suffix' (videos).
+-----------------------------------------------------------------------------*/
+    char * suffixLoc;
+
+    /* Generate filename template */
+    if (!imageName || streq(imageName, "") || streq(imageName, "-")) {
+        if (!wfaName || streq(wfaName, "-"))
+            *basename = strdup("stdin");
+        else
+            *basename = strdup(wfaName);
+        suffixLoc = NULL;
+    } else {
+        *basename = strdup(imageName);
+        suffixLoc = strrchr(*basename, '.');
+    }
 
-                public code
+    if (suffixLoc) {
+        /* found name 'basename.suffix' */
 
-*****************************************************************************/
+        *suffixLoc = '\0';         /* remove dot from *basename*/
 
-int
-main (int argc, char **argv)
-{
-    char               *image_name        = NULL; /* output filename */
-    bool_t              double_resolution = NO; /* double resolution of image */
-    bool_t              panel             = NO; /* control panel */
-    int             fps               = -1; /* frame display rate */
-    fiasco_d_options_t *options           = NULL; /* additional coder options */
-    int                 last_arg;    /* last processed cmdline parameter */
-
-    init_error_handling (argv[0]);
-
-    last_arg = checkargs (argc, argv, &double_resolution, &panel, &fps,
-                          &image_name, &options);
-
-    if (last_arg >= argc)
-        video_decoder ("-", image_name, panel, double_resolution, fps, options);
-    else
-        while (last_arg++ < argc)
-            video_decoder (argv [last_arg - 1], image_name, panel,
-                           double_resolution, fps, options);
-
-    return 0;
+        if (*(suffixLoc+1) == '\0')
+            *suffixP = strdup(color ? "ppm" : "pgm");
+        else
+            *suffixP = strdup(suffixLoc + 1);
+    } else             /* no suffix found, generate one */
+        *suffixP = strdup(color ? "ppm" : "pgm");
 }
 
-/*****************************************************************************
-
-                private code
 
-*****************************************************************************/
 
-static param_t params [] =
-{
+static param_t params [] = {
     {"output", "FILE", 'o', PSTR, {0}, "-",
      "Write raw PNM frame(s) to `%s'."},
     {"double", NULL, 'd', PFLAG, {0}, "FALSE",
@@ -112,178 +100,165 @@ static param_t params [] =
 };
 
 static int
-checkargs (int argc, char **argv, bool_t *double_resolution, bool_t *panel,
-           int *fps, char **image_name, fiasco_d_options_t **options)
-/*
- *  Check validness of command line parameters and of the parameter files.
- *
- *  Return value.
- *  index in argv of the first argv-element that is not an option.
- *
- *  Side effects:
- *  'double_resolution', 'panel', 'fps', 'image_name' and 'options'
- *      are modified.
- */
-{
+checkargs(int                         argc,
+          const char **         const argv,
+          bool_t *              const double_resolution,
+          bool_t *              const panel,
+          int *                 const fps,
+          const char **         const image_name,
+          fiasco_d_options_t ** const options) {
+/*----------------------------------------------------------------------------
+  Check validness of command line parameters and of the parameter files.
+
+  Return value: index in argv of the first argv-element that is not an option.
+
+  Side effects:
+-----------------------------------------------------------------------------*/
     int optind;              /* last processed commandline param */
 
-    optind = parseargs (params, argc, argv,
-                        "Decode FIASCO-FILEs and write frame(s) to disk.",
-                        "With no FIASCO-FILE, or if FIASCO-FILE is -, "
-                        "read standard input.\n"
-                        "Environment:\n"
-                        "FIASCO_DATA   Search path for automata files. "
-                        "Default: ./\n"
-                        "FIASCO_IMAGES Save path for image files. "
+    optind = parseargs(params, argc, argv,
+                       "Decode FIASCO-FILEs and write frame(s) to disk.",
+                       "With no FIASCO-FILE, or if FIASCO-FILE is -, "
+                       "read standard input.\n"
+                       "Environment:\n"
+                       "FIASCO_DATA   Search path for automata files. "
+                       "Default: ./\n"
+                       "FIASCO_IMAGES Save path for image files. "
                         "Default: ./", " [FIASCO-FILE]...",
-                        FIASCO_SHARE, "system.fiascorc", ".fiascorc");
+                       FIASCO_SHARE, "system.fiascorc", ".fiascorc");
 
     *image_name        =   (char *)   parameter_value (params, "output");
     *double_resolution = *((bool_t *) parameter_value (params, "double"));
     *panel             = *((bool_t *) parameter_value (params, "panel"));
-    *fps           = *((int *)    parameter_value (params, "framerate"));
+    *fps               = *((int *)    parameter_value (params, "framerate"));
 
-    /*
-     *  Additional options ... (have to be set with the fiasco_set_... methods)
+    /* Additional options ... (have to be set with the fiasco_set_... methods)
      */
-    *options = fiasco_d_options_new ();
+    *options = fiasco_d_options_new();
 
     {
-        int const n = *((int *) parameter_value (params, "smoothing"));
+        int const n = *((int *)parameter_value(params, "smoothing"));
 
-        if (!fiasco_d_options_set_smoothing (*options, MAX(-1, n)))
-            error (fiasco_get_error_message ());
+        if (!fiasco_d_options_set_smoothing(*options, MAX(-1, n)))
+            pm_error("%s", fiasco_get_error_message());
     }
 
     {
-        int const n = *((int *) parameter_value (params, "magnify"));
+        int const n = *((int *)parameter_value(params, "magnify"));
 
-        if (!fiasco_d_options_set_magnification (*options, n))
-            error (fiasco_get_error_message ());
+        if (!fiasco_d_options_set_magnification(*options, n))
+            pm_error("%s", fiasco_get_error_message());
     }
 
     {
-        bool_t const n = *((bool_t *) parameter_value (params, "fast"));
+        bool_t const n = *((bool_t *)parameter_value(params, "fast"));
 
-        if (!fiasco_d_options_set_4_2_0_format (*options, n > 0 ? YES : NO))
-            error (fiasco_get_error_message ());
+        if (!fiasco_d_options_set_4_2_0_format(*options, n > 0 ? YES : NO))
+            pm_error("%s", fiasco_get_error_message ());
     }
 
     return optind;
 }
 
+
+
 static void
-video_decoder (const char *wfa_name, const char *image_name, bool_t panel,
-               bool_t double_resolution, int fps, fiasco_d_options_t *options)
-{
-    do
-    {
-        unsigned      width, height, frames, n;
-        fiasco_decoder_t *decoder_state;
-        char             *filename;
-        char             *basename;   /* basename of decoded frame */
-        char             *suffix;     /* suffix of decoded frame */
-        unsigned      frame_time;
-
-        if (!(decoder_state = fiasco_decoder_new (wfa_name, options)))
-            error (fiasco_get_error_message ());
-
-        if (fps <= 0)         /* then use value of FIASCO file */
-            fps = fiasco_decoder_get_rate (decoder_state);
+video_decoder(const char *         const wfa_name,
+              const char *         const image_name,
+              bool_t               const panel,
+              bool_t               const double_resolution,
+              int                  const fpsArg,
+              fiasco_d_options_t * const options) {
+    do {
+        int                fps;
+        unsigned int       width, height;
+        unsigned int       frames;
+        unsigned int       n;
+        fiasco_decoder_t * decoder_state;
+        char *             filename;
+        const char *       basename;   /* basename of decoded frame */
+        const char *       suffix;     /* suffix of decoded frame */
+        unsigned int       frame_time;
+
+        if (!(decoder_state = fiasco_decoder_new(wfa_name, options)))
+            pm_error("%s", fiasco_get_error_message ());
+
+        if (fpsArg <= 0)         /* then use value of FIASCO file */
+            fps = fiasco_decoder_get_rate(decoder_state);
+        else
+            fps = fpsArg;
+
         frame_time = fps ? (1000 / fps) : (1000 / 25);
 
-        if (!(width = fiasco_decoder_get_width (decoder_state)))
-            error (fiasco_get_error_message ());
+        if (!(width = fiasco_decoder_get_width(decoder_state)))
+            pm_error("%s", fiasco_get_error_message ());
 
-        if (!(height = fiasco_decoder_get_height (decoder_state)))
-            error (fiasco_get_error_message ());
+        if (!(height = fiasco_decoder_get_height(decoder_state)))
+            pm_error("%s", fiasco_get_error_message ());
 
-        if (!(frames = fiasco_decoder_get_length (decoder_state)))
-            error (fiasco_get_error_message ());
+        if (!(frames = fiasco_decoder_get_length(decoder_state)))
+            pm_error("%s", fiasco_get_error_message ());
 
-        get_output_template (image_name, wfa_name,
-                             fiasco_decoder_is_color (decoder_state),
-                             &basename, &suffix);
+        getOutputTemplate(image_name, wfa_name,
+                          fiasco_decoder_is_color(decoder_state),
+                          &basename, &suffix);
 
-        filename = calloc (strlen (basename) + strlen (suffix) + 2
-                           + 10 + (int) (log10 (frames) + 1), sizeof (char));
-        if (!filename)
-            error ("Out of memory.");
+        MALLOCARRAY_NOFAIL(filename,
+                           strlen (basename) + strlen (suffix) + 2
+                           + 10 + (int) (log10 (frames) + 1));
 
-        for (n = 0; n < frames; n++)
-        {
+        for (n = 0; n < frames; ++n) {
             clock_t fps_timer;     /* frames per second timer struct */
 
-            prg_timer (&fps_timer, START);
+            prg_timer(&fps_timer, START);
 
-            if (image_name)        /* just write frame to disk */
-            {
-                if (frames == 1)        /* just one image */
-                {
-                    if (streq (image_name, "-"))
-                        strcpy (filename, "-");
+            if (image_name) {
+                /* just write frame to disk */
+                if (frames == 1) {
+                    if (streq(image_name, "-"))
+                        strcpy(filename, "-");
                     else
-                        sprintf (filename, "%s.%s", basename, suffix);
-                }
-                else
-                {
-                    fprintf (stderr, "Decoding frame %d to file `%s.%0*d.%s\n",
-                             n, basename, (int) (log10 (frames - 1) + 1),
-                             n, suffix);
-                    sprintf (filename, "%s.%0*d.%s", basename,
-                             (int) (log10 (frames - 1) + 1), n, suffix);
+                        sprintf(filename, "%s.%s", basename, suffix);
+                } else {
+                    pm_message("Decoding frame %d to file `%s.%0*d.%s",
+                               n, basename, (int) (log10 (frames - 1) + 1),
+                               n, suffix);
+                    sprintf(filename, "%s.%0*d.%s", basename,
+                            (int) (log10 (frames - 1) + 1), n, suffix);
                 }
 
                 if (!fiasco_decoder_write_frame (decoder_state, filename))
-                    error (fiasco_get_error_message ());
+                    pm_error("%s", fiasco_get_error_message ());
             }
             if (frame_time) {/* defeat compiler warning */}
         }
-        free (filename);
+        free(filename);
 
-        fiasco_decoder_delete (decoder_state);
+        fiasco_decoder_delete(decoder_state);
     } while (panel);
 }
 
-static void
-get_output_template (const char *image_name, const char *wfa_name,
-                     bool_t color, char **basename, char **suffix)
-/*
- *  Generate image filename template for output of image sequences.
- *  'wfa_name' is the filename of the WFA stream.
- *  Images are either saved with filename 'basename'.'suffix' (still images)
- *  or 'basename'.%03d.'suffix' (videos).
- *
- *  No return value.
- *
- *  Side effects:
- *  '*basename' and '*suffix' is set.
- */
-{
-    if (!wfa_name || streq (wfa_name, "-"))
-        wfa_name = "stdin";
-    /*
-     *  Generate filename template
-     */
-    if (!image_name || streq (image_name, "") || streq (image_name, "-"))
-    {
-        *basename = strdup (wfa_name);
-        *suffix   = NULL;
-    }
+int
+main(int argc, const char **argv) {
+
+    const char *         imageName; /* output filename */
+    bool_t               doubleResolution;/* double resolution of image */
+    bool_t               panel; /* control panel */
+    int                  fps; /* frame display rate */
+    fiasco_d_options_t * options;/* additional coder options */
+    unsigned int         lastArg;    /* last processed cmdline parameter */
+
+    lastArg = checkargs(argc, argv, &doubleResolution, &panel, &fps,
+                        &imageName, &options);
+
+    if (lastArg >= argc)
+        video_decoder("-", imageName, panel, doubleResolution, fps, options);
     else
-    {
-        *basename = strdup (image_name);
-        *suffix   = strrchr (*basename, '.');
-    }
+        while (lastArg++ < argc)
+            video_decoder(argv [lastArg - 1], imageName, panel,
+                          doubleResolution, fps, options);
 
-    if (*suffix)         /* found name 'basename.suffix' */
-    {
-        **suffix = 0;         /* remove dot */
-        (*suffix)++;
-        if (**suffix == 0)
-            *suffix = strdup (color ? "ppm" : "pgm");
-    }
-    else             /* no suffix found, generate one */
-        *suffix = strdup (color ? "ppm" : "pgm");
+    return 0;
 }
 
+
diff --git a/converter/other/fiasco/params.c b/converter/other/fiasco/params.c
index f55e7e8d..042a14e2 100644
--- a/converter/other/fiasco/params.c
+++ b/converter/other/fiasco/params.c
@@ -323,14 +323,15 @@ parameter_value(const param_t * const params,
 
 
 int
-parseargs(param_t *    const usr_params,
-          int argc, char **argv,
-          const char * const synopsis,
-          const char * const comment,
-          const char * const non_opt_string,
-          const char * const path,
-          const char * const sys_file_name,
-          const char * const usr_file_name) {
+parseargs(param_t *     const usr_params,
+          int argc,
+          const char ** const argv,
+          const char *  const synopsis,
+          const char *  const comment,
+          const char *  const non_opt_string,
+          const char *  const path,
+          const char *  const sys_file_name,
+          const char *  const usr_file_name) {
 /*----------------------------------------------------------------------------
   Perform the command line parsing.
   List of allowed parameters is given by 'usr_params'.
@@ -499,7 +500,8 @@ parseargs(param_t *    const usr_params,
 
         /* Parse command line */
 
-        while ((optchar = getopt_long(argc, argv, optstr, long_options,
+        while ((optchar = getopt_long(argc, (char **)argv, optstr,
+                                      long_options,
                                       &option_index)) != EOF) {
             int param_index = -1;
 
@@ -554,12 +556,12 @@ parseargs(param_t *    const usr_params,
                     fprintf(stderr, "%s " VERSION "\n", argv [0]);
                     {
                         /* Kludge for standard Netpbm version announcement */
-                        char * modified_argv[2];
+                        const char * modifiedArgv[2];
                         int argc;
-                        modified_argv[0] = argv[0];
-                        modified_argv[1] = (char *) "--version";
+                        modifiedArgv[0] = argv[0];
+                        modifiedArgv[1] = (char *) "--version";
                         argc = 2;
-                        pm_proginit(&argc, (const char **) modified_argv);
+                        pm_proginit(&argc, modifiedArgv);
                     }
                     exit (2);
                 } else if (streq(params[param_index].name, "verbose"))
diff --git a/converter/other/fiasco/params.h b/converter/other/fiasco/params.h
index 76427577..85b0b648 100644
--- a/converter/other/fiasco/params.h
+++ b/converter/other/fiasco/params.h
@@ -44,7 +44,7 @@ typedef struct param_t
 
 int
 parseargs (param_t *usr_params,
-           int argc, char **argv,
+           int argc, const char **argv,
            const char *synopsis,
            const char *comment,
            const char *non_opt_string,
diff --git a/converter/other/fiasco/pnmtofiasco.c b/converter/other/fiasco/pnmtofiasco.c
index 9f63e7a1..311a21c3 100644
--- a/converter/other/fiasco/pnmtofiasco.c
+++ b/converter/other/fiasco/pnmtofiasco.c
@@ -1,5 +1,5 @@
 /*
- *  cwfa.c:     FIASCO coder
+ *  FIASCO coder
  *
  *  Written by:     Ullrich Hafner
  *
@@ -15,32 +15,25 @@
  */
 
 #include "config.h"
-#include "pm_c_util.h"
-#include "pnm.h"
-
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
 
+#include "pm_c_util.h"
+#include "mallocvar.h"
+#include "pnm.h"
+
 #include "types.h"
 #include "macros.h"
 
-#include "binerror.h"
 #include "misc.h"
 #include "params.h"
 #include "fiasco.h"
 
-/*****************************************************************************
 
-                 local variables
 
-*****************************************************************************/
-
-static param_t params [] =
-{
-  /*
-   *  Options for standard user
-   */
+static param_t params[] = {
+  /* Options for standard user */
   {"image-name", "FILE", 'i', PSTR, {0}, NULL,
    "Compress raw PPM/PGM image(s) `%s'."},
   {"output-name", "FILE", 'o', PSTR, {0}, "-",
@@ -136,244 +129,202 @@ static param_t params [] =
   {NULL, NULL, 0, PSTR, {0}, NULL, NULL }
 };
 
-/*****************************************************************************
-
-                prototypes
-
-*****************************************************************************/
-
-static void
-checkargs (int argc, char **argv, char const ***image_template,
-       char **wfa_name, float *quality, fiasco_c_options_t **options);
-
-/*****************************************************************************
-
-                public code
-
-*****************************************************************************/
-
-int
-main (int argc, char **argv)
-{
-   char const         **image_template; /* template for input image files */
-   char                *wfa_name;   /* filename of output WFA */
-   float            quality;    /* approximation quality */
-   fiasco_c_options_t  *options;    /* additional coder options */
-
-   pnm_init(&argc, argv);
-
-   init_error_handling (argv [0]);
-
-   checkargs (argc, argv, &image_template, &wfa_name, &quality, &options);
-
-   if (fiasco_coder (image_template, wfa_name, quality, options))
-      return 0;
-   else
-   {
-       fprintf (stderr, "%s", fiasco_get_error_message ());
-      fprintf (stderr, "\n");
-      return 1;
-   }
-}
-
-/*****************************************************************************
-
-                private code
 
-*****************************************************************************/
 
 static void
-checkargs (int argc, char **argv, char const ***image_template,
-           char **wfa_name, float *quality, fiasco_c_options_t **options)
-/*
- *  Check validness of command line parameters and of the parameter files.
- *
- *  Return value:
- *  1 on success
- *  0 otherwise
- *
- *
- *  Side effects:
- *  'image_template', 'wfa_name', 'quality' and 'options' are set.
- */
-{
-    int   optind;            /* last processed commandline param */
-    char *image_name;            /* filename given by option '--input_name' */
-    int   i;             /* counter */
-
-    optind = parseargs (params, argc, argv,
-                        "Compress raw PPM/PGM image FILEs to a FIASCO file.",
-                        "With no image FILE, or if FILE is -, "
-                        "read standard input.\n"
-                        "FILE must be either a filename"
-                        " or an image template of the form:\n"
-                        "`prefix[start-end{+,-}step]suffix'\n"
-                        "e.g., img0[12-01-1].pgm is substituted by"
-                        " img012.pgm ... img001.pgm\n\n"
-                        "Environment:\n"
-                        "FIASCO_DATA   Search and save path for FIASCO files. "
-                        "Default: ./\n"
-                        "FIASCO_IMAGES Search path for image files. "
-                        "Default: ./", " [FILE]...",
-                        FIASCO_SHARE, "system.fiascorc", ".fiascorc");
-
-    /*
-     *  Default options ...
-     */
-    image_name = (char *) parameter_value (params, "image-name");
-    *wfa_name  = (char *) parameter_value (params, "output-name");
-    for (;;)
-    {
-        *quality = * (float *) parameter_value (params, "quality");
+checkargs(int                         argc,
+          const char **               argv,
+          const char ***        const imageTemplateListP,
+          char **               const wfa_name,
+          float *               const quality,
+          fiasco_c_options_t ** const options) {
+/*----------------------------------------------------------------------------
+  Check validness of command line parameters and of the parameter files.
+
+  Return value:
+    1 on success
+    0 otherwise
+-----------------------------------------------------------------------------*/
+    int    optind;            /* last processed commandline param */
+    char * image_name;            /* filename given by option '--input_name' */
+    const char ** imageTemplateList;
+
+    optind = parseargs(params, argc, argv,
+                       "Compress raw PPM/PGM image FILEs to a FIASCO file.",
+                       "With no image FILE, or if FILE is -, "
+                       "read standard input.\n"
+                       "FILE must be either a filename"
+                       " or an image template of the form:\n"
+                       "`prefix[start-end{+,-}step]suffix'\n"
+                       "e.g., img0[12-01-1].pgm is substituted by"
+                       " img012.pgm ... img001.pgm\n\n"
+                       "Environment:\n"
+                       "FIASCO_DATA   Search and save path for FIASCO files. "
+                       "Default: ./\n"
+                       "FIASCO_IMAGES Search path for image files. "
+                       "Default: ./", " [FILE]...",
+                       FIASCO_SHARE, "system.fiascorc", ".fiascorc");
+
+    /* Default options  */
+    image_name = (char *) parameter_value(params, "image-name");
+    *wfa_name  = (char *) parameter_value(params, "output-name");
+    for (;;) {
+        *quality = * (float *) parameter_value(params, "quality");
         if (*quality > 100)
-            fprintf (stderr, "Typical range of quality: (0,100].\n"
-                     "Expect some trouble on slow machines.\n");
+            pm_message("Typical range of quality: (0,100].  "
+                       "Expect some trouble on slow machines.");
         if (*quality > 0)
             break;
-        ask_and_set (params, "quality",
-                     "Please enter coding quality 'q' ('q' > 0): ");
+        ask_and_set(params, "quality",
+                    "Please enter coding quality 'q' ('q' > 0): ");
     }
 
-    if (optind < argc)           /* Additional command line param */
-    {
+    /* Non-option command line params */
+    if (optind < argc) {
+        unsigned int i;
         if (image_name)
-            error ("Multiple image_template arguments."
-                   "\nOption --input-name %s already specified!", image_name);
-
-        *image_template = calloc (argc - optind + 1, sizeof (char *));
-        if (!*image_template)
-            error ("Out of memory.");
-        for (i = 0; optind < argc; i++, optind++)
-            (*image_template) [i] = argv [optind];
-        (*image_template) [i] = NULL;
-    }
-    else                 /* option -i image_name */
-    {
-        *image_template = calloc (2, sizeof (char *));
-        if (!*image_template)
-            error ("Out of memory.");
-        (*image_template) [0] = image_name;
-        (*image_template) [1] = NULL;
+            pm_error("Multiple image name template arguments.  "
+                     "Option --image-name already specified with '%s'",
+                     image_name);
+
+        MALLOCARRAY_NOFAIL(imageTemplateList, argc - optind + 1);
+
+        for (i = 0; optind < argc; ++i, ++optind)
+            imageTemplateList[i] = argv[optind];
+        imageTemplateList[i] = NULL;
+    } else {
+        /* option -i image_name */
+
+        MALLOCARRAY_NOFAIL(imageTemplateList, 2);
+
+        imageTemplateList[0] = image_name;
+        imageTemplateList[1] = NULL;
     }
-    /*
-     *  Additional options ... (have to be set with the fiasco_set_... methods)
+    /* Additional options ... (have to be set with the fiasco_set_... methods)
      */
     {
-        *options = fiasco_c_options_new ();
+        *options = fiasco_c_options_new();
 
         {
-            char *pattern = (char *) parameter_value (params, "pattern");
+            const char * const pattern = parameter_value(params, "pattern");
 
             if (!fiasco_c_options_set_frame_pattern (*options, pattern))
-                error (fiasco_get_error_message ());
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            char *basis = (char *) parameter_value (params, "basis-name");
+            const char *const basis = parameter_value(params, "basis-name");
 
             if (!fiasco_c_options_set_basisfile (*options, basis))
-                error (fiasco_get_error_message ());
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            int   n = * (int *) parameter_value (params, "chroma-dictionary");
-            float q = * (float *) parameter_value (params, "chroma-qfactor");
+            int   const n =
+                *(int *)parameter_value(params, "chroma-dictionary");
+            float const q =
+                *(float *)parameter_value(params, "chroma-qfactor");
 
-            if (!fiasco_c_options_set_chroma_quality (*options, q, MAX(0, n)))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_chroma_quality(*options, q, MAX(0, n)))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            int n = *((int *) parameter_value (params, "smooth"));
+            int const n = *((int *)parameter_value(params, "smooth"));
 
-            if (!fiasco_c_options_set_smoothing (*options, MAX(0, n)))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_smoothing(*options, MAX(0, n)))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            int n = * (int *) parameter_value (params, "progress-meter");
+            int const n = *(int *)parameter_value(params, "progress-meter");
             fiasco_progress_e type = (n < 0) ?
                 FIASCO_PROGRESS_NONE : (fiasco_progress_e) n;
 
-            if (!fiasco_c_options_set_progress_meter (*options, type))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_progress_meter(*options, type))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            char *t = (char *) parameter_value (params, "title");
+            const char * const t = parameter_value(params, "title");
 
-            if (strlen (t) > 0 && !fiasco_c_options_set_title (*options, t))
-                error (fiasco_get_error_message ());
+            if (strlen(t) > 0 && !fiasco_c_options_set_title(*options, t))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            char *c = (char *) parameter_value (params, "comment");
+            const char * const c = parameter_value(params, "comment");
 
             if (strlen (c) > 0 && !fiasco_c_options_set_comment (*options, c))
-                error (fiasco_get_error_message ());
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
             fiasco_tiling_e method = FIASCO_TILING_VARIANCE_DSC;
-            int   e  = * (int *) parameter_value (params, "tiling-exponent");
-            char *m  = (char *) parameter_value (params, "tiling-method");
+            int    const e =
+                *(int *)parameter_value(params, "tiling-exponent");
+            const char * const m = parameter_value (params, "tiling-method");
 
-            if (strcaseeq (m, "desc-variance"))
+            if (strcaseeq(m, "desc-variance"))
                 method = FIASCO_TILING_VARIANCE_DSC;
-            else if (strcaseeq (m, "asc-variance"))
+            else if (strcaseeq(m, "asc-variance"))
                 method = FIASCO_TILING_VARIANCE_ASC;
-            else if (strcaseeq (m, "asc-spiral"))
+            else if (strcaseeq(m, "asc-spiral"))
                 method = FIASCO_TILING_SPIRAL_ASC;
-            else if (strcaseeq (m, "dsc-spiral"))
+            else if (strcaseeq(m, "dsc-spiral"))
                 method = FIASCO_TILING_SPIRAL_DSC;
             else
-                error (_("Invalid tiling method `%s' specified."), m);
+                pm_error("Invalid tiling method `%s' specified.", m);
 
-            if (!fiasco_c_options_set_tiling (*options, method, MAX(0, e)))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_tiling(*options, method, MAX(0, e)))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
         {
-            int M/*  = * (int *) parameter_value (params, "max-level") */;
-            int m/*  = * (int *) parameter_value (params, "min-level") */;
-            int N/*  = * (int *) parameter_value (params, "max-elements") */;
-            int D = * (int *) parameter_value (params, "dictionary-size");
-            int o = * (int *) parameter_value (params, "optimize");
-
-            if (o <= 0)
-            {
+            int M /*  = * (int *) parameter_value (params, "max-level") */;
+            int m /*  = * (int *) parameter_value (params, "min-level") */;
+            int N /*  = * (int *) parameter_value (params, "max-elements") */;
+            int o;
+            int D = * (int *) parameter_value(params, "dictionary-size");
+            int const optimizeOpt =
+                *(int *)parameter_value(params, "optimize");
+
+            if (optimizeOpt <= 0) {
                 o = 0;
                 M = 10;
                 m = 6;
                 N = 3;
-            }
-            else
-            {
+            } else {
                 o -= 1;
                 M = 12;
                 m = 4;
                 N = 5;
             }
 
-            if (!fiasco_c_options_set_optimizations (*options, m, M, N,
-                                                     MAX(0, D), o))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_optimizations(*options, m, M, N,
+                                                    MAX(0, D), o))
+                pm_error("%s", fiasco_get_error_message ());
         }
         {
-            int M = * (int *) parameter_value (params, "max-level");
-            int m = * (int *) parameter_value (params, "min-level");
-            int p = * (int *) parameter_value (params, "prediction");
+            int const M = *(int *)parameter_value(params, "max-level");
+            int const m = *(int *)parameter_value(params, "min-level");
+            int const p = *(int *)parameter_value(params, "prediction");
 
             if (!fiasco_c_options_set_prediction (*options,
                                                   p, MAX(0, m), MAX(0, M)))
-                error (fiasco_get_error_message ());
+                pm_error("%s", fiasco_get_error_message ());
         }
         {
-            float r    = * (float *)parameter_value(params, "rpf-range");
-            float dc_r = * (float *)parameter_value(params, "dc-rpf-range");
-            int   m    = * (int *)  parameter_value(params, "rpf-mantissa");
-            int   dc_m = * (int *)  parameter_value(params, "dc-rpf-mantissa");
-            fiasco_rpf_range_e range, dc_range;
+            float const r    =
+                *(float *)parameter_value(params, "rpf-range");
+            float const dcR =
+                *(float *)parameter_value(params, "dc-rpf-range");
+            int   const m    =
+                *(int *)parameter_value(params, "rpf-mantissa");
+            int   const dcM =
+                *(int *)parameter_value(params, "dc-rpf-mantissa");
+
+            fiasco_rpf_range_e range, dcRange;
 
             if (r < 1)
                 range = FIASCO_RPF_RANGE_0_75;
@@ -384,24 +335,49 @@ checkargs (int argc, char **argv, char const ***image_template,
             else
                 range = FIASCO_RPF_RANGE_2_00;
 
-            if (dc_r < 1)
-                dc_range = FIASCO_RPF_RANGE_0_75;
-            else if (dc_r < 1.5)
-                dc_range = FIASCO_RPF_RANGE_1_00;
-            else if (dc_r < 2.0)
-                dc_range = FIASCO_RPF_RANGE_1_50;
+            if (dcR < 1)
+                dcRange = FIASCO_RPF_RANGE_0_75;
+            else if (dcR < 1.5)
+                dcRange = FIASCO_RPF_RANGE_1_00;
+            else if (dcR < 2.0)
+                dcRange = FIASCO_RPF_RANGE_1_50;
             else
-                dc_range = FIASCO_RPF_RANGE_2_00;
+                dcRange = FIASCO_RPF_RANGE_2_00;
 
-            if (!fiasco_c_options_set_quantization (*options,
-                                                    MAX(0, m), range,
-                                                    MAX(0, dc_m), dc_range))
-                error (fiasco_get_error_message ());
+            if (!fiasco_c_options_set_quantization(*options,
+                                                   MAX(0, m), range,
+                                                   MAX(0, dcM), dcRange))
+                pm_error("%s", fiasco_get_error_message ());
         }
 
-        if (fiasco_get_verbosity () == FIASCO_ULTIMATE_VERBOSITY)
-            write_parameters (params, stderr);
+        if (fiasco_get_verbosity() == FIASCO_ULTIMATE_VERBOSITY)
+            write_parameters(params, stderr);
+    }
+    *imageTemplateListP = imageTemplateList;
+}
+
+
+
+int
+main(int argc, const char **argv) {
+
+    char const **        image_template; /* template for input image files */
+    char *               wfa_name;   /* filename of output WFA */
+    float                quality;    /* approximation quality */
+    fiasco_c_options_t * options;    /* additional coder options */
+    int                  retval;
+
+    pm_proginit(&argc, argv);
+
+    checkargs(argc, argv, &image_template, &wfa_name, &quality, &options);
+
+    if (fiasco_coder(image_template, wfa_name, quality, options))
+        retval = 0;
+    else {
+        pm_message("Encoding failed.  %s", fiasco_get_error_message());
+        retval = 1;
     }
+    return retval;
 }