about summary refs log tree commit diff
path: root/converter/other/pamtosvg
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
commit1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch)
tree64c8c96cf54d8718847339a403e5e67b922e8c3f /converter/other/pamtosvg
downloadnetpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other/pamtosvg')
-rw-r--r--converter/other/pamtosvg/Makefile55
-rw-r--r--converter/other/pamtosvg/README109
-rw-r--r--converter/other/pamtosvg/autotrace.c220
-rw-r--r--converter/other/pamtosvg/autotrace.h258
-rw-r--r--converter/other/pamtosvg/bitmap.c116
-rw-r--r--converter/other/pamtosvg/bitmap.h53
-rw-r--r--converter/other/pamtosvg/curve.c314
-rw-r--r--converter/other/pamtosvg/curve.h160
-rw-r--r--converter/other/pamtosvg/epsilon-equal.c19
-rw-r--r--converter/other/pamtosvg/epsilon-equal.h20
-rw-r--r--converter/other/pamtosvg/exception.c55
-rw-r--r--converter/other/pamtosvg/exception.h43
-rw-r--r--converter/other/pamtosvg/fit.c1923
-rw-r--r--converter/other/pamtosvg/fit.h34
-rw-r--r--converter/other/pamtosvg/image-header.h19
-rw-r--r--converter/other/pamtosvg/image-proc.c516
-rw-r--r--converter/other/pamtosvg/image-proc.h42
-rw-r--r--converter/other/pamtosvg/logreport.c17
-rw-r--r--converter/other/pamtosvg/logreport.h28
-rw-r--r--converter/other/pamtosvg/message.h47
-rw-r--r--converter/other/pamtosvg/output-svg.c130
-rw-r--r--converter/other/pamtosvg/output-svg.h37
-rw-r--r--converter/other/pamtosvg/pamtosvg.c395
-rw-r--r--converter/other/pamtosvg/pamtosvg.test6
-rw-r--r--converter/other/pamtosvg/point.h8
-rw-r--r--converter/other/pamtosvg/pxl-outline.c1370
-rw-r--r--converter/other/pamtosvg/pxl-outline.h79
-rw-r--r--converter/other/pamtosvg/spline.c193
-rw-r--r--converter/other/pamtosvg/spline.h90
-rw-r--r--converter/other/pamtosvg/testgrid.svg5
-rw-r--r--converter/other/pamtosvg/testline.svg5
-rw-r--r--converter/other/pamtosvg/thin-image.c373
-rw-r--r--converter/other/pamtosvg/thin-image.h37
-rw-r--r--converter/other/pamtosvg/vector.c254
-rw-r--r--converter/other/pamtosvg/vector.h71
35 files changed, 7101 insertions, 0 deletions
diff --git a/converter/other/pamtosvg/Makefile b/converter/other/pamtosvg/Makefile
new file mode 100644
index 00000000..7f9c3e30
--- /dev/null
+++ b/converter/other/pamtosvg/Makefile
@@ -0,0 +1,55 @@
+ifeq ($(SRCDIR)x,x)
+  SRCDIR = $(CURDIR)/../../..
+  BUILDDIR = $(SRCDIR)
+endif
+SUBDIR = converter/other/pamtosvg
+VPATH=.:$(SRCDIR)/$(SUBDIR)
+
+include $(BUILDDIR)/Makefile.config
+
+BINARIES = pamtosvg
+
+PAMTOSVG_OBJECTS = \
+	pamtosvg.o \
+	output-svg.o \
+	fit.o \
+	spline.o \
+	curve.o \
+	vector.o \
+	epsilon-equal.o \
+	autotrace.o \
+	pxl-outline.o \
+	bitmap.o \
+	thin-image.o \
+	logreport.o \
+	exception.o \
+	image-proc.o \
+
+MERGE_OBJECTS = \
+	pamtosvg.o2 \
+	output-svg.o \
+	fit.o \
+	spline.o \
+	curve.o \
+	vector.o \
+	epsilon-equal.o \
+	autotrace.o \
+	pxl-outline.o \
+	bitmap.o \
+	thin-image.o \
+	logreport.o \
+	exception.o \
+	image-proc.o \
+
+OBJECTS = $(PAMTOSVG_OBJECTS)
+
+MERGEBINARIES = $(BINARIES)
+
+all: $(BINARIES)
+
+include $(SRCDIR)/Makefile.common
+
+pamtosvg: $(PAMTOSVG_OBJECTS) $(NETPBMLIB) $(LIBOPT)
+	$(LD) $(LDFLAGS) -o $@ $(PAMTOSVG_OBJECTS) \
+	  $(shell $(LIBOPT) $(NETPBMLIB)) \
+	  $(MATHLIB) $(LDLIBS) $(RPATH) $(LADD)
diff --git a/converter/other/pamtosvg/README b/converter/other/pamtosvg/README
new file mode 100644
index 00000000..a06b71a5
--- /dev/null
+++ b/converter/other/pamtosvg/README
@@ -0,0 +1,109 @@
+The core of this program is derived from Martin Weber's
+(martweb@gmx.net) Autotrace.  Bryan Henderson adapted it to Netpbm in 
+February 2006.
+
+Much of the Autotrace code has been rewritten to be easier to read and match
+Netpbm coding style.
+
+Pieces of Autotrace that duplicate other Netpbm programs or just don't fit
+the Netpbm philosophy have been removed.  In particular, Autotrace has the
+ability to take formats other than Netpbm formats for input, to despeckle
+the image before tracing, and to quantize colors before tracing.  Pamtosvg
+has none of that.
+
+Pamtosvg uses libnetpbm to read the input image, process the command line,
+manage memory, and do several other minor things.  Autotrace has its own
+code for those things.
+
+Autotrace uses a shmaltzy trick to deal with open outlines.  There is a
+piece of the program that divides an outline at its corners and then creates
+a curve for the pieces between every two corners.  For an open outline,
+there are segments of the outline at each end that are not between two
+corners.  One piece of code finds the corners, and another computes the
+curves between them.  
+
+In order to use the closed outline code for the open outline case,
+Autotrace includes a dummy corner at the start of the outline in the
+list of corners.  The curve-finding code then has a special case for
+the segment after the last corner.  Pamtosvg uses a cleaner approach.
+The corner list is an actual list of corners -- there is no dummy
+corner.  The curve-finding code has two special cases for open
+outlines -- one for the segment before the first corner, and one for
+the segment after the last corner.
+
+
+STRATEGY
+--------
+
+Autotrace is capable of many more vector graphics output formats
+besides SVG.  The basic curve tracing is the same for all; it just has
+output formatting modules.  Netpbm ought to be able to generate those
+formats as well.  SVG is just the beginning.
+
+Of course, having a program that generates multiple output formats based
+on command line options is not the Netpbm way to approach it.  A pure
+traditional Netpbm approach would be to have a converter between PAM
+and each of the vector graphics formats.
+
+That isn't practical because of the information that gets lost when you
+convert from vector to raster form, and also because 99% of the logic
+in converting between PAM and a vector graphics format is the same for
+all the vector graphics formats.
+
+Therefore, the strategy is to adopt a single vector graphics format
+and use it as the common intermediate format.  Right now, it looks
+like SVG would be a good choice for that.  However, it may turn out to
+be better to create a vector graphics format specially for Netpbm.
+The reason for that is that an intermediate Netpbm format has a rather
+different requirement from all these other formats -- it isn't meant
+to be transmitted or stored, but it is meant to be easy for a
+programmer to work with.
+
+We need a program to go the other way -- to convert from SVG to PAM.
+I.e. a curve drawing program.  The Ppmdraw program does this kind of
+curve drawing (in fact, a Ppmdraw script can be considered a vector
+graphics format).  Ppmdraw itself might not be adaptable, but the
+library routines it uses are probably all we need to convert SVG to
+PAM.
+
+The Pamtosvg code should be reworked to use a libnetpbm tuple array
+instead of its at_bitmap_type for the raster.
+
+Nobody has any plans to do any of this work.  I document the strategy
+only so that if someone decides to do some work, he can go in the
+right direction.
+
+
+COPYRIGHT CONSIDERATIONS
+------------------------
+
+Bryan took the code and has distributed it under a copyright license
+granted to him, as a member of the public, by the authors.  That
+license is the GNU Lesser Public License Version 2.1.  All the authors
+offer that same license for Pamtosvg to the public.  A copy of it is
+in the Netpbm doc/ directory.
+
+
+CREDITS
+-------
+
+Autotrace's source code listed the following as contributors to it:
+
+Martin Weber <martweb@gmx.net>
+Bernhard Herzog (Postscript, svg and sk export filter) 
+Ian MacPhedran (xfig export filter) 
+Martin Kroeker (bugfixes) 
+Tobias Polzin (bugfixes) 
+Kevin O'Gorman (Shockwave support) 
+MenTaLguY (png import filter)
+Peter Cucka (bugfixes)
+Enrico Persiani (emf export) 
+Johannes Schindelin (Magick import filter)
+Masatake YAMATO (library, help with cvs)
+Steffen Politzky (dxf export)
+David A. Bartold (part of despeckle)
+Han-Wen Nienhuys (rpm-spec file)
+R. P. C. Rodgers (man page)
+Allen Barnett (improved emf export)
+Andrew Elia (dr2d export filter)
+Ralf Stubner (bugfixes about pstoedit usage)
diff --git a/converter/other/pamtosvg/autotrace.c b/converter/other/pamtosvg/autotrace.c
new file mode 100644
index 00000000..e9902669
--- /dev/null
+++ b/converter/other/pamtosvg/autotrace.c
@@ -0,0 +1,220 @@
+/* autotrace.c --- Autotrace API
+
+  Copyright (C) 2000, 2001, 2002 Martin Weber
+
+  The author can be contacted at <martweb@gmx.net>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "mallocvar.h"
+
+#include "autotrace.h"
+#include "exception.h"
+
+#include "fit.h"
+#include "bitmap.h"
+#include "spline.h"
+
+#include "image-header.h"
+#include "image-proc.h"
+#include "thin-image.h"
+
+
+#define AT_DEFAULT_DPI 72
+
+at_fitting_opts_type *
+at_fitting_opts_new(void)
+{
+  at_fitting_opts_type * opts;
+  MALLOCVAR_NOFAIL(opts);
+  return opts;
+}
+
+at_fitting_opts_type *
+at_fitting_opts_copy (at_fitting_opts_type * original)
+{
+  at_fitting_opts_type * new_opts;
+  if (original == NULL)
+    return NULL;
+
+  new_opts = at_fitting_opts_new ();
+  *new_opts = *original;
+  new_opts->backgroundSpec = original->backgroundSpec;
+  new_opts->background_color = original->background_color;
+  return new_opts;
+}
+
+void 
+at_fitting_opts_free(at_fitting_opts_type * opts)
+{
+  free(opts);
+}
+
+at_output_opts_type *
+at_output_opts_new(void)
+{
+  at_output_opts_type * opts;
+  MALLOCVAR_NOFAIL(opts);
+  opts->dpi          = AT_DEFAULT_DPI;
+  return opts;
+}
+
+at_output_opts_type *
+at_output_opts_copy(at_output_opts_type * original)
+{
+  at_output_opts_type * opts =  at_output_opts_new();
+  *opts = *original;
+  return opts;
+}
+
+void
+at_output_opts_free(at_output_opts_type * opts)
+{
+  free(opts);
+}
+
+/* at_splines_new_full modifies its 'bitmap' argument
+   when it does the thin_image thing.
+*/
+at_spline_list_array_type * 
+at_splines_new_full(at_bitmap_type *       const bitmap,
+                    at_fitting_opts_type * const opts,
+                    at_msg_func                  msg_func, 
+                    void *                 const msg_data,
+                    at_progress_func             notify_progress,
+                    void *                 const progress_data,
+                    at_testcancel_func           test_cancel,
+                    void *                 const testcancel_data) {
+
+    at_spline_list_array_type * retval;
+    image_header_type image_header;
+    pixel_outline_list_type pixelOutlineList;
+    at_exception_type exp;
+    distance_map_type distanceMap;
+    bool haveDistMap;
+
+    exp = at_exception_new(msg_func, msg_data);
+
+    image_header.width  = at_bitmap_get_width(bitmap);
+    image_header.height = at_bitmap_get_height(bitmap);
+
+    if (opts->centerline) {
+        if (opts->preserve_width) {
+            /* Preserve line width prior to thinning. */
+            bool const paddedTrue = true;
+            distanceMap = new_distance_map(*bitmap, 255, paddedTrue, &exp);
+            haveDistMap = true;
+        } else
+            haveDistMap = false;
+        thin_image(bitmap, opts->backgroundSpec, opts->background_color, &exp);
+    } else
+        haveDistMap = false;
+
+    if (at_exception_got_fatal(&exp))
+        retval = NULL;
+    else {
+        if (opts->centerline) {
+            pixel background_color;
+
+            if (opts->backgroundSpec) 
+                background_color = opts->background_color;
+            else
+                PPM_ASSIGN(background_color, 255, 255, 255);
+            
+            pixelOutlineList =
+                find_centerline_pixels(*bitmap, background_color, 
+                                       notify_progress, progress_data,
+                                       test_cancel, testcancel_data, &exp);
+        } else
+            pixelOutlineList =
+                find_outline_pixels(*bitmap,
+                                    opts->backgroundSpec,
+                                    opts->background_color, 
+                                    notify_progress, progress_data,
+                                    test_cancel, testcancel_data, &exp);
+
+        if (at_exception_got_fatal(&exp) ||
+            (test_cancel && test_cancel(testcancel_data)))
+            retval = NULL;
+        else {
+            at_spline_list_array_type * splinesP;
+        
+            MALLOCVAR_NOFAIL(splinesP); 
+            fit_outlines_to_splines(pixelOutlineList, opts,
+                                    haveDistMap ? &distanceMap : NULL,
+                                    image_header.width,
+                                    image_header.height,
+                                    &exp,
+                                    notify_progress, progress_data,
+                                    test_cancel, testcancel_data,
+                                    splinesP);
+
+            if (at_exception_got_fatal(&exp) ||
+                (test_cancel && test_cancel(testcancel_data)))
+                retval = NULL;
+            else {
+                if (notify_progress)
+                    notify_progress(1.0, progress_data);
+
+                retval = splinesP;
+            }
+            free_pixel_outline_list(&pixelOutlineList);
+        }
+        if (haveDistMap)
+            free_distance_map(&distanceMap);
+    }
+    return retval;
+}
+
+
+
+void 
+at_splines_write(at_output_write_func                  outputWriter,
+                 FILE *                          const writeto,
+                 at_output_opts_type *           const optsArg,
+                 at_spline_list_array_type *     const splinesP,
+                 at_msg_func                           msgFunc,
+                 void *                          const msgData) {
+
+    at_output_opts_type * optsP;
+    bool newOpts;
+    int llx, lly, urx, ury;
+    llx = 0;
+    lly = 0;
+    urx = splinesP->width;
+    ury = splinesP->height;
+    
+    if (optsArg == NULL) {
+        newOpts = true;
+        optsP   = at_output_opts_new();
+    } else {
+        newOpts = false;
+        optsP   = optsArg;
+    }
+    (*outputWriter)(writeto, "DUMMYFILENAME",
+                    llx, lly, urx, ury, optsP, *splinesP,
+                    msgFunc, msgData);
+    if (newOpts)
+        at_output_opts_free(optsP);
+}
+
+
+
+void 
+at_splines_free(at_spline_list_array_type * const splines) {
+
+    free_spline_list_array(splines);
+    free(splines);
+}
diff --git a/converter/other/pamtosvg/autotrace.h b/converter/other/pamtosvg/autotrace.h
new file mode 100644
index 00000000..2ac81a08
--- /dev/null
+++ b/converter/other/pamtosvg/autotrace.h
@@ -0,0 +1,258 @@
+/* autotrace.h --- Autotrace API
+
+  Copyright (C) 2000, 2001, 2002 Martin Weber
+
+  The author can be contacted at <martweb@gmx.net>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#ifndef AUTOTRACE_H
+#define AUTOTRACE_H
+
+#include <stdio.h>
+
+#include "point.h"
+#include "pm_c_util.h"
+#include "ppm.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* ===================================================================== *
+ * Typedefs
+ * ===================================================================== */
+
+typedef struct _at_fitting_opts_type at_fitting_opts_type;
+typedef struct _at_input_opts_type   at_input_opts_type;
+typedef struct _at_output_opts_type  at_output_opts_type;
+typedef struct _at_bitmap_type at_bitmap_type;
+typedef struct _at_spline_type at_spline_type;
+typedef struct _at_spline_list_type at_spline_list_type;
+typedef struct _at_spline_list_array_type at_spline_list_array_type;
+
+/* Third degree is the highest we deal with.  */
+typedef enum _at_polynomial_degree
+{
+  AT_LINEARTYPE = 1, 
+  AT_QUADRATICTYPE = 2, 
+  AT_CUBICTYPE = 3, 
+  AT_PARALLELELLIPSETYPE = 4,
+  AT_ELLIPSETYPE = 5, 
+  AT_CIRCLETYPE = 6 
+  /* not the real number of points to define a
+     circle but to distinguish between a cubic spline */
+} at_polynomial_degree;
+
+/* A Bezier spline can be represented as four points in the real plane:
+   a starting point, ending point, and two control points.  The
+   curve always lies in the convex hull defined by the four points.  It
+   is also convenient to save the divergence of the spline from the
+   straight line defined by the endpoints.  */
+struct _at_spline_type
+{
+  float_coord v[4];	/* The control points.  */
+  at_polynomial_degree degree;
+  float linearity;
+};
+
+/* Each outline in a character is typically represented by many
+   splines.  So, here is a list structure for that:  */
+struct _at_spline_list_type
+{
+  at_spline_type *data;
+  unsigned length;
+  bool clockwise;
+  pixel color;
+  bool open;
+};
+
+/* Each character is in general made up of many outlines. So here is one
+   more list structure.  */
+struct _at_spline_list_array_type
+{
+  at_spline_list_type *data;
+  unsigned length;
+
+  /* splines bbox */
+  unsigned short height, width;
+  
+  /* the values for following members are inherited from 
+     at_fitting_opts_type */
+  bool backgroundSpec;
+  pixel background_color;
+  bool centerline;
+  bool preserve_width;
+  float width_weight_factor;
+
+};
+
+
+/* Fitting option.
+   With using at_fitting_opts_doc macro, the description of 
+   each option could be get. e.g. at_fitting_opts_doc(background_color) */
+struct _at_fitting_opts_type {
+    bool backgroundSpec;
+    pixel background_color;
+    float corner_always_threshold;
+    unsigned corner_surround;
+    float corner_threshold;
+    float error_threshold;
+    unsigned filter_iterations;
+    float line_reversion_threshold;
+    float line_threshold;
+    bool remove_adjacent_corners;
+    unsigned tangent_surround;
+    bool centerline;
+    bool preserve_width;
+    float width_weight_factor;
+};
+
+struct _at_output_opts_type
+{
+  int dpi;			/* DPI is used only in MIF output.*/
+};
+
+struct _at_bitmap_type
+{
+  unsigned short height;
+  unsigned short width;
+  unsigned char *bitmap;
+  unsigned int np;
+};
+
+typedef enum _at_msg_type
+{
+  AT_MSG_FATAL = 1,
+  AT_MSG_WARNING
+} at_msg_type;
+
+typedef
+void (* at_msg_func) (const char * const msg,
+                      at_msg_type  const msg_type,
+                      void *       const client_data);
+
+typedef 
+int (*at_output_write_func) (FILE *                          const file,
+                             const char *                    const name,
+                             int                             const llx,
+                             int                             const lly, 
+                             int                             const urx,
+                             int                             const ury,
+                             at_output_opts_type *           const opts,
+                             at_spline_list_array_type       const shape,
+                             at_msg_func                           msg_func, 
+                             void *                          const msg_data);
+
+/*
+ * Progress handler typedefs
+ * 0.0 <= percentage <= 1.0
+ */
+typedef
+void (*at_progress_func) (float const percentage,
+                           void *  const client_data);
+
+/*
+ * Test cancel
+ * Return true if auto-tracing should be stopped.
+ */
+typedef
+bool (*at_testcancel_func) (void * const client_data);
+
+/* ===================================================================== *
+ * Functions
+ * ===================================================================== */
+
+/* --------------------------------------------------------------------- *
+ * Fitting option related
+ *
+ * TODO: internal data access, copy
+ * --------------------------------------------------------------------- */
+at_fitting_opts_type * at_fitting_opts_new(void);
+at_fitting_opts_type * at_fitting_opts_copy (at_fitting_opts_type * original); 
+void at_fitting_opts_free(at_fitting_opts_type * opts);
+
+/* TODO: Gettextize */
+#define at_fitting_opts_doc(opt) _(at_doc__##opt)
+
+/* --------------------------------------------------------------------- *
+ * Output option related
+ *
+ * TODO: internal data access
+ * --------------------------------------------------------------------- */
+at_output_opts_type * at_output_opts_new(void);
+at_output_opts_type * at_output_opts_copy(at_output_opts_type * original);
+void at_output_opts_free(at_output_opts_type * opts);
+
+/* --------------------------------------------------------------------- *
+ * Spline related
+ *
+ * TODO: internal data access
+ * --------------------------------------------------------------------- */
+/* at_splines_new_full
+
+   args:
+
+   NOTIFY_PROGRESS is called repeatedly inside at_splines_new_full
+   to notify the progress of the execution. This might be useful for 
+   interactive applications. NOTIFY_PROGRESS is called following 
+   format:
+
+   NOTIFY_PROGRESS (percentage, progress_data);
+
+   test_cancel is called repeatedly inside at_splines_new_full
+   to test whether the execution is canceled or not.
+   If test_cancel returns TRUE, execution of at_splines_new_full
+   is stopped as soon as possible and returns NULL. If test_cancel 
+   returns FALSE, nothing happens. test_cancel  is called following
+   format:
+
+   TEST_CANCEL (testcancel_data);
+   
+   NULL is valid value for notify_progress and/or test_cancel if 
+   you don't need to know the progress of the execution and/or 
+   cancel the execution */ 
+
+at_spline_list_array_type * 
+at_splines_new_full(at_bitmap_type *       const bitmap,
+                    at_fitting_opts_type * const opts,
+                    at_msg_func                  msg_func, 
+                    void *                 const msg_data,
+                    at_progress_func             notify_progress,
+                    void *                 const progress_data,
+                    at_testcancel_func           test_cancel,
+                    void *                 const testcancel_data);
+
+void 
+at_splines_write(at_output_write_func                  output_writer,
+                 FILE *                          const writeto,
+                 at_output_opts_type *           const opts,
+                 at_spline_list_array_type *     const splines,
+                 at_msg_func                           msg_func,
+                 void *                          const msg_data);
+
+void
+at_splines_free(at_spline_list_array_type * const splines);
+
+
+/* --------------------------------------------------------------------- *
+ * Misc
+ * --------------------------------------------------------------------- */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif
diff --git a/converter/other/pamtosvg/bitmap.c b/converter/other/pamtosvg/bitmap.c
new file mode 100644
index 00000000..1a00e748
--- /dev/null
+++ b/converter/other/pamtosvg/bitmap.c
@@ -0,0 +1,116 @@
+/* bitmap.c: operations on bitmaps. */
+
+#include <string.h>
+
+#include "mallocvar.h"
+
+#include "bitmap.h"
+
+at_bitmap_type *
+at_bitmap_new(unsigned short width,
+              unsigned short height,
+              unsigned int planes) {
+
+    at_bitmap_type * bitmap;
+
+    MALLOCVAR_NOFAIL(bitmap); 
+
+    *bitmap = at_bitmap_init(NULL, width, height, planes);
+
+    return bitmap;
+}
+
+
+
+at_bitmap_type *
+at_bitmap_copy(at_bitmap_type * src)
+{
+    at_bitmap_type * dist;
+    unsigned short width, height, planes;
+
+    width  = at_bitmap_get_width(src);
+    height = at_bitmap_get_height(src);
+    planes = at_bitmap_get_planes(src);
+    
+    dist = at_bitmap_new(width, height, planes);
+    memcpy(dist->bitmap, 
+           src->bitmap, 
+           width * height * planes * sizeof(unsigned char));
+    return dist;
+}
+
+
+
+at_bitmap_type
+at_bitmap_init(unsigned char * area,
+               unsigned short width,
+               unsigned short height,
+               unsigned int planes) {
+
+    at_bitmap_type bitmap;
+    
+    if (area)
+        bitmap.bitmap = area;
+    else {
+        if (width * height == 0)
+            bitmap.bitmap = NULL;
+        else {
+            MALLOCARRAY(bitmap.bitmap, width * height * planes);
+            if (bitmap.bitmap == NULL)
+                pm_error("Unable to allocate %u x %u x %u bitmap array",
+                         width, height, planes);
+            bzero(bitmap.bitmap,
+                  width * height * planes * sizeof(unsigned char));
+        }
+    }
+    
+    bitmap.width  = width;
+    bitmap.height = height;
+    bitmap.np     =  planes;
+
+    return bitmap;  
+}
+
+void 
+at_bitmap_free (at_bitmap_type * bitmap)
+{
+    free_bitmap (bitmap);
+    free(bitmap);
+}
+
+unsigned short
+at_bitmap_get_width (at_bitmap_type * bitmap)
+{
+    return bitmap->width;
+}
+
+unsigned short
+at_bitmap_get_height (at_bitmap_type * bitmap)
+{
+    return bitmap->height;
+}
+
+unsigned short
+at_bitmap_get_planes (at_bitmap_type * bitmap)
+{
+    return bitmap->np;
+}
+
+
+
+bitmap_type
+new_bitmap (unsigned short width, unsigned short height)
+{
+    return at_bitmap_init(NULL,width,height,1);
+}
+
+/* Free the storage that is allocated for a bitmap.  On the other hand,
+   the bitmap might not have any storage allocated for it if it is zero
+   in either dimension; in that case, don't free it.  */
+
+void
+free_bitmap (bitmap_type *b)
+{
+    if (b->bitmap != NULL)
+        free (b->bitmap);
+}
diff --git a/converter/other/pamtosvg/bitmap.h b/converter/other/pamtosvg/bitmap.h
new file mode 100644
index 00000000..7334f138
--- /dev/null
+++ b/converter/other/pamtosvg/bitmap.h
@@ -0,0 +1,53 @@
+/* bitmap.h: definition for a bitmap type.  No packing is done by
+   default; each pixel is represented by an entire byte.  Among other
+   things, this means the type can be used for both grayscale and binary
+   images. */
+
+#ifndef BITMAP_H
+#define BITMAP_H
+
+#include "autotrace.h"
+#include <stdio.h>
+
+/* at_ prefix removed version */
+typedef at_bitmap_type bitmap_type;
+#define BITMAP_PLANES(b)          AT_BITMAP_PLANES(b)
+#define BITMAP_BITS(b)            AT_BITMAP_BITS(b)  
+#define BITMAP_WIDTH(b)           AT_BITMAP_WIDTH(b)  
+#define BITMAP_HEIGHT(b)          AT_BITMAP_HEIGHT(b) 
+
+/* This is the pixel at [ROW,COL].  */
+#define BITMAP_PIXEL(b, row, col)					\
+  ((b).bitmap + ((row) * (b).width + (col)) * (b).np)
+
+#define BITMAP_VALID_PIXEL(b, row, col)					\
+   	((row) < (b).height && (col) < (b).width)
+
+/* Allocate storage for the bits, set them all to white, and return an
+   initialized structure.  */
+extern bitmap_type new_bitmap (unsigned short width, unsigned short height);
+
+/* Free that storage.  */
+extern void free_bitmap (bitmap_type *);
+
+
+at_bitmap_type * at_bitmap_new(unsigned short width,
+			       unsigned short height,
+			       unsigned int planes);
+at_bitmap_type * at_bitmap_copy(at_bitmap_type * src);
+
+/* We have to export functions that supports internal datum 
+   access. Such functions might be useful for 
+   at_bitmap_new user. */
+unsigned short at_bitmap_get_width (at_bitmap_type * bitmap);
+unsigned short at_bitmap_get_height (at_bitmap_type * bitmap);
+unsigned short at_bitmap_get_planes (at_bitmap_type * bitmap);
+void at_bitmap_free (at_bitmap_type * bitmap);
+
+at_bitmap_type
+at_bitmap_init(unsigned char * area,
+	       unsigned short width,
+	       unsigned short height,
+	       unsigned int planes);
+
+#endif /* not BITMAP_H */
diff --git a/converter/other/pamtosvg/curve.c b/converter/other/pamtosvg/curve.c
new file mode 100644
index 00000000..cc8aeb59
--- /dev/null
+++ b/converter/other/pamtosvg/curve.c
@@ -0,0 +1,314 @@
+/* curve.c: operations on the lists of pixels and lists of curves.
+
+   The code was partially derived from limn.
+
+   Copyright (C) 1992 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include "mallocvar.h"
+
+#include "logreport.h"
+#include "curve.h"
+
+
+static float_coord
+int_to_real_coord(pm_pixelcoord const int_coord) {
+/*----------------------------------------------------------------------------
+  Turn an integer point into a real one.
+-----------------------------------------------------------------------------*/
+    float_coord real_coord;
+
+    real_coord.x = int_coord.col;
+    real_coord.y = int_coord.row;
+    real_coord.z = 0.0;
+    
+    return real_coord;
+}
+
+
+
+/* Return an entirely empty curve.  */
+
+curve_type
+new_curve (void)
+{
+  curve_type curve;
+  MALLOCVAR_NOFAIL(curve);
+  curve->point_list = NULL;
+  CURVE_LENGTH (curve) = 0;
+  CURVE_CYCLIC (curve) = false;
+  CURVE_START_TANGENT (curve) = CURVE_END_TANGENT (curve) = NULL;
+  PREVIOUS_CURVE (curve) = NEXT_CURVE (curve) = NULL;
+
+  return curve;
+}
+
+
+/* Don't copy the points or tangents, but copy everything else.  */
+
+curve_type
+copy_most_of_curve (curve_type old_curve)
+{
+  curve_type curve = new_curve ();
+
+  CURVE_CYCLIC (curve) = CURVE_CYCLIC (old_curve);
+  PREVIOUS_CURVE (curve) = PREVIOUS_CURVE (old_curve);
+  NEXT_CURVE (curve) = NEXT_CURVE (old_curve);
+
+  return curve;
+}
+
+
+/* The length of CURVE will be zero if we ended up not being able to fit
+   it (which in turn implies a problem elsewhere in the program, but at
+   any rate, we shouldn't try here to free the nonexistent curve).  */
+
+void
+free_curve (curve_type curve)
+{
+  if (CURVE_LENGTH (curve) > 0)
+    free (curve->point_list);
+  if (CURVE_START_TANGENT (curve))
+    free (CURVE_START_TANGENT (curve));
+  if (CURVE_END_TANGENT (curve))
+    free (CURVE_END_TANGENT (curve));
+}
+
+
+void
+append_point(curve_type  const curve,
+             float_coord const coord) {
+
+    CURVE_LENGTH(curve)++;
+    REALLOCARRAY_NOFAIL(curve->point_list, CURVE_LENGTH(curve));
+    LAST_CURVE_POINT(curve) = coord;
+    /* The t value does not need to be set.  */
+}
+
+
+void
+append_pixel(curve_type    const curve,
+             pm_pixelcoord const coord) {
+
+    append_point(curve, int_to_real_coord(coord));
+}
+
+
+/* Print a curve in human-readable form.  It turns out we never care
+   about most of the points on the curve, and so it is pointless to
+   print them all out umpteen times.  What matters is that we have some
+   from the end and some from the beginning.  */
+
+#define NUM_TO_PRINT 3
+
+#define LOG_CURVE_POINT(c, p, print_t)					\
+  do									\
+    {									\
+      LOG2 ("(%.3f,%.3f)", CURVE_POINT (c, p).x, CURVE_POINT (c, p).y);	\
+      if (print_t)							\
+        LOG1 ("/%.2f", CURVE_T (c, p));					\
+    }									\
+  while (0)
+
+void
+log_curve (curve_type curve, bool print_t)
+{
+  unsigned this_point;
+
+  if (!log_file) return;
+
+  LOG1 ("curve id = %lx:\n", (unsigned long) curve);
+  LOG1 ("  length = %u.\n", CURVE_LENGTH (curve));
+  if (CURVE_CYCLIC (curve))
+    LOG ("  cyclic.\n");
+
+  /* It should suffice to check just one of the tangents for being null
+     -- either they both should be, or neither should be.  */
+  if (CURVE_START_TANGENT (curve) != NULL)
+    LOG4 ("  tangents = (%.3f,%.3f) & (%.3f,%.3f).\n",
+          CURVE_START_TANGENT (curve)->dx, CURVE_START_TANGENT (curve)->dy,
+          CURVE_END_TANGENT (curve)->dx, CURVE_END_TANGENT (curve)->dy);
+
+  LOG ("  ");
+
+  /* If the curve is short enough, don't use ellipses.  */
+  if (CURVE_LENGTH (curve) <= NUM_TO_PRINT * 2)
+    {
+      for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
+        {
+          LOG_CURVE_POINT (curve, this_point, print_t);
+          LOG (" ");
+
+          if (this_point != CURVE_LENGTH (curve) - 1
+              && (this_point + 1) % NUM_TO_PRINT == 0)
+            LOG ("\n  ");
+        }
+    }
+  else
+    {
+      for (this_point = 0;
+           this_point < NUM_TO_PRINT && this_point < CURVE_LENGTH (curve);
+           this_point++)
+        {
+          LOG_CURVE_POINT (curve, this_point, print_t);
+          LOG (" ");
+        }
+
+      LOG ("...\n   ...");
+
+      for (this_point = CURVE_LENGTH (curve) - NUM_TO_PRINT;
+           this_point < CURVE_LENGTH (curve);
+           this_point++)
+        {
+          LOG (" ");
+          LOG_CURVE_POINT (curve, this_point, print_t);
+        }
+    }
+
+  LOG (".\n");
+}
+
+
+/* Like `log_curve', but write the whole thing.  */
+
+void
+log_entire_curve (curve_type curve)
+{
+  unsigned this_point;
+
+  if (!log_file) return;
+
+  LOG1 ("curve id = %lx:\n", (unsigned long) curve);
+  LOG1 ("  length = %u.\n", CURVE_LENGTH (curve));
+  if (CURVE_CYCLIC (curve))
+    LOG ("  cyclic.\n");
+
+  /* It should suffice to check just one of the tangents for being null
+     -- either they both should be, or neither should be.  */
+  if (CURVE_START_TANGENT (curve) != NULL)
+    LOG4 ("  tangents = (%.3f,%.3f) & (%.3f,%.3f).\n",
+          CURVE_START_TANGENT (curve)->dx, CURVE_START_TANGENT (curve)->dy,
+          CURVE_END_TANGENT (curve)->dx, CURVE_END_TANGENT (curve)->dy);
+
+  LOG (" ");
+
+  for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
+    {
+      LOG (" ");
+      LOG_CURVE_POINT (curve, this_point, true);
+      /* Compiler warning `Condition is always true' can be ignored */
+    }
+
+  LOG (".\n");
+}
+
+
+/* Return an initialized but empty curve list.  */
+
+curve_list_type
+new_curve_list (void)
+{
+  curve_list_type curve_list;
+
+  curve_list.length = 0;
+  curve_list.data = NULL;
+
+  return curve_list;
+}
+
+
+/* Free a curve list and all the curves it contains.  */
+
+void
+free_curve_list(curve_list_type * const curve_list) {
+
+  unsigned this_curve;
+
+  for (this_curve = 0; this_curve < curve_list->length; this_curve++)
+    {
+      free_curve (curve_list->data[this_curve]);
+      free (curve_list->data[this_curve]);
+    }
+
+  /* If the character was empty, it won't have any curves.  */
+  if (curve_list->data != NULL)
+    free (curve_list->data);
+}
+
+
+/* Add an element to a curve list.  */
+
+void
+append_curve (curve_list_type *curve_list, curve_type curve)
+{
+  curve_list->length++;
+  REALLOCARRAY_NOFAIL(curve_list->data, curve_list->length);
+  curve_list->data[curve_list->length - 1] = curve; }
+
+
+/* Return an initialized but empty curve list array.  */
+
+curve_list_array_type
+new_curve_list_array (void)
+{
+  curve_list_array_type curve_list_array;
+
+  CURVE_LIST_ARRAY_LENGTH (curve_list_array) = 0;
+  curve_list_array.data = NULL;
+
+  return curve_list_array;
+}
+
+
+/* Free a curve list array and all the curve lists it contains.  */
+
+void
+free_curve_list_array(const curve_list_array_type * const curve_list_array,
+                      at_progress_func                    notify_progress, 
+                      void *                        const client_data) {
+
+  unsigned this_list;
+
+  for (this_list = 0; this_list < CURVE_LIST_ARRAY_LENGTH(*curve_list_array);
+       this_list++) {
+      if (notify_progress)
+          notify_progress(((float)this_list)/
+                          (CURVE_LIST_ARRAY_LENGTH(*curve_list_array) *
+                           (float)3.0)+(float)0.666 ,
+                          client_data);
+      free_curve_list(&CURVE_LIST_ARRAY_ELT (*curve_list_array, this_list));
+  }
+  
+  /* If the character was empty, it won't have any curves.  */
+  if (curve_list_array->data != NULL)
+      free(curve_list_array->data);
+}
+
+
+/* Add an element to a curve list array.  */
+
+void
+append_curve_list(curve_list_array_type * const curve_list_array,
+                  curve_list_type         const curve_list) {
+
+  CURVE_LIST_ARRAY_LENGTH (*curve_list_array)++;
+  REALLOCARRAY_NOFAIL(curve_list_array->data,
+                      CURVE_LIST_ARRAY_LENGTH(*curve_list_array));
+  LAST_CURVE_LIST_ARRAY_ELT (*curve_list_array) = curve_list;
+}
+
+
+
diff --git a/converter/other/pamtosvg/curve.h b/converter/other/pamtosvg/curve.h
new file mode 100644
index 00000000..db3cc682
--- /dev/null
+++ b/converter/other/pamtosvg/curve.h
@@ -0,0 +1,160 @@
+/* curve.h: data structures for the conversion from pixels to splines. */
+
+#ifndef CURVE_H
+#define CURVE_H
+
+#include "autotrace.h"
+#include "point.h"
+#include "vector.h"
+
+/* We are simultaneously manipulating two different representations of
+   the same outline: one based on (x,y) positions in the plane, and one
+   based on parametric splines.  (We are trying to match the latter to
+   the former.)  Although the original (x,y)'s are pixel positions,
+   i.e., integers, after filtering they are reals.  */
+
+typedef struct {
+    float_coord coord;
+    float       t;
+} point_type;
+
+
+
+struct curve {
+/*----------------------------------------------------------------------------
+  An ordered list of contiguous points in the raster, with no corners
+  in it.  I.e. something that could reasonably be fit to a spline.
+-----------------------------------------------------------------------------*/
+    point_type *   point_list;
+    unsigned       length;
+    bool           cyclic;
+    vector_type *  start_tangent;
+    vector_type *  end_tangent;
+    struct curve * previous;
+    struct curve * next;
+};
+
+typedef struct curve * curve_type;
+
+/* Get at the coordinates and the t values.  */
+#define CURVE_POINT(c, n) ((c)->point_list[n].coord)
+#define LAST_CURVE_POINT(c) ((c)->point_list[(c)->length-1].coord)
+#define CURVE_T(c, n) ((c)->point_list[n].t)
+#define LAST_CURVE_T(c) ((c)->point_list[(c)->length-1].t)
+
+/* This is the length of `point_list'.  */
+#define CURVE_LENGTH(c)  ((c)->length)
+
+/* A curve is ``cyclic'' if it didn't have any corners, after all, so
+   the last point is adjacent to the first.  */
+#define CURVE_CYCLIC(c)  ((c)->cyclic)
+
+/* If the curve is cyclic, the next and previous points should wrap
+   around; otherwise, if we get to the end, we return CURVE_LENGTH and
+   -1, respectively.  */
+#define CURVE_NEXT(c, n)						\
+  ((n) + 1 >= CURVE_LENGTH (c)						\
+  ? CURVE_CYCLIC (c) ? ((n) + 1) % CURVE_LENGTH (c) : CURVE_LENGTH (c)	\
+  : (n) + 1)
+#define CURVE_PREV(c, n)						\
+  ((signed int) (n) - 1 < 0							\
+  ? CURVE_CYCLIC (c) ? (signed int) CURVE_LENGTH (c) + (signed int) (n) - 1 : -1\
+  : (signed int) (n) - 1)
+
+/* The tangents at the endpoints are computed using the neighboring curves.  */
+#define CURVE_START_TANGENT(c) ((c)->start_tangent)
+#define CURVE_END_TANGENT(c) ((c)->end_tangent)
+#define PREVIOUS_CURVE(c) ((c)->previous)
+#define NEXT_CURVE(c) ((c)->next)
+
+
+/* Return an entirely empty curve.  */
+extern curve_type new_curve (void);
+
+/* Return a curve the same as C, except without any points.  */
+extern curve_type copy_most_of_curve (curve_type c);
+
+/* Free the memory C uses.  */
+extern void free_curve (curve_type c);
+
+/* Append the point P to the end of C's list.  */
+void
+append_pixel(curve_type    const c,
+             pm_pixelcoord const p);
+
+/* Like `append_pixel', for a point in real coordinates.  */
+extern void append_point (curve_type c, float_coord p);
+
+/* Write some or all, respectively, of the curve C in human-readable
+   form to the log file, if logging is enabled.  */
+extern void log_curve (curve_type c, bool print_t);
+extern void log_entire_curve (curve_type c);
+
+/* Display the curve C online, if displaying is enabled.  */
+extern void display_curve (curve_type);
+
+
+
+typedef struct {
+/*----------------------------------------------------------------------------
+   An ordered list of contiguous curves of a particular color.
+-----------------------------------------------------------------------------*/
+    curve_type * data;
+    unsigned     length;
+    bool         clockwise;
+    pixel        color;
+    bool         open;
+        /* The curve list does not form a closed shape;  i.e. the last
+           curve doesn't end where the first one starts.
+        */
+} curve_list_type;
+
+/* Number of curves in the list.  */
+#define CURVE_LIST_LENGTH(c_l)  ((c_l).length)
+#define CURVE_LIST_EMPTY(c_l) ((c_l).length == 0)
+
+/* Access the individual curves.  */
+#define CURVE_LIST_ELT(c_l, n) ((c_l).data[n])
+#define LAST_CURVE_LIST_ELT(c_l) ((c_l).data[CURVE_LIST_LENGTH (c_l) - 1])
+
+/* Says whether the outline that this curve list represents moves
+   clockwise or counterclockwise.  */
+#define CURVE_LIST_CLOCKWISE(c_l) ((c_l).clockwise)
+
+
+extern curve_list_type new_curve_list (void);
+
+void
+free_curve_list(curve_list_type * const curve_list);
+
+extern void append_curve (curve_list_type *, curve_type);
+
+/* And a character is a list of outlines.  I named this
+   `curve_list_array_type' because `curve_list_list_type' seemed pretty
+   monstrous.  */
+typedef struct
+{
+  curve_list_type *data;
+  unsigned length;
+} curve_list_array_type;
+
+/* Turns out we can use the same definitions for lists of lists as for
+   just lists.  But we define the usual names, just in case.  */
+#define CURVE_LIST_ARRAY_LENGTH CURVE_LIST_LENGTH
+#define CURVE_LIST_ARRAY_ELT CURVE_LIST_ELT
+#define LAST_CURVE_LIST_ARRAY_ELT LAST_CURVE_LIST_ELT
+
+curve_list_array_type
+new_curve_list_array(void);
+
+void
+free_curve_list_array(const curve_list_array_type * const curve_list_array,
+                      at_progress_func                    notify_progress, 
+                      void *                        const client_data);
+
+void
+append_curve_list(curve_list_array_type * const curve_list_array,
+                  curve_list_type         const curve_list);
+
+#endif
+
diff --git a/converter/other/pamtosvg/epsilon-equal.c b/converter/other/pamtosvg/epsilon-equal.c
new file mode 100644
index 00000000..46d630c5
--- /dev/null
+++ b/converter/other/pamtosvg/epsilon-equal.c
@@ -0,0 +1,19 @@
+/* epsilon-equal.c: define a error resist compare. */
+
+#include <math.h>
+
+#include "epsilon-equal.h"
+
+/* Numerical errors sometimes make a floating point number just slightly
+   larger or smaller than its true value.  When it matters, we need to
+   compare with some tolerance, REAL_EPSILON, defined in kbase.h.  */
+
+bool
+epsilon_equal(float const v1,
+              float const v2) {
+
+    return
+        v1 == v2		       /* Usually they'll be exactly equal, anyway.  */
+        || fabs(v1 - v2) <= REAL_EPSILON;
+}
+
diff --git a/converter/other/pamtosvg/epsilon-equal.h b/converter/other/pamtosvg/epsilon-equal.h
new file mode 100644
index 00000000..fa0437cd
--- /dev/null
+++ b/converter/other/pamtosvg/epsilon-equal.h
@@ -0,0 +1,20 @@
+/* epsilon-equal.h: define an error resist compare. */
+
+#ifndef EPSILON_EQUAL_H
+#define EPSILON_EQUAL_H
+
+#include "pm_c_util.h"
+
+/* Says whether V1 and V2 are within REAL_EPSILON of each other.
+   Fixed-point arithmetic would be better, to guarantee machine
+   independence, but it's so much more painful to work with.  The value
+   here is smaller than can be represented in either a `fix_word' or a
+   `scaled_num', so more precision than this will be lost when we
+   output, anyway.  */
+bool epsilon_equal(float const v1,
+                   float const v2);
+
+#define REAL_EPSILON 0.00001
+
+#endif /* not EPSILON_EQUAL_H */
+
diff --git a/converter/other/pamtosvg/exception.c b/converter/other/pamtosvg/exception.c
new file mode 100644
index 00000000..43761936
--- /dev/null
+++ b/converter/other/pamtosvg/exception.c
@@ -0,0 +1,55 @@
+
+#include "exception.h"
+
+at_exception_type
+at_exception_new(at_msg_func       client_func,
+                 void *      const client_data) {
+
+    at_exception_type e;
+
+    e.msg_type = 0;
+    e.client_func = client_func;
+    e.client_data = client_data;
+    
+    return e;
+}
+
+
+
+bool
+at_exception_got_fatal(at_exception_type * const exception) {
+
+    return (exception->msg_type == AT_MSG_FATAL);
+}
+
+
+
+void
+at_exception_fatal(at_exception_type * const exception,
+                   const char *        const message) {
+
+    if (exception) {
+        exception->msg_type = AT_MSG_FATAL;
+        if (exception->client_func) {
+            exception->client_func(message, 
+                                   AT_MSG_FATAL,
+                                   exception->client_data);
+        }
+    }
+}
+
+
+
+void
+at_exception_warning(at_exception_type * const exception,
+                     const char *        const message) {
+
+    if (exception) {
+        exception->msg_type = AT_MSG_WARNING;
+        if (exception->client_func) {
+            exception->client_func(message, 
+                                   AT_MSG_WARNING,
+                                   exception->client_data);
+        }
+    }
+}
diff --git a/converter/other/pamtosvg/exception.h b/converter/other/pamtosvg/exception.h
new file mode 100644
index 00000000..113f65e6
--- /dev/null
+++ b/converter/other/pamtosvg/exception.h
@@ -0,0 +1,43 @@
+/* exception.h: facility to handle error in autotrace */
+
+#ifndef AT_EXCEPTION_H
+#define AT_EXCEPTION_H 
+
+#include "autotrace.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* Protocol:
+   If a function raises a FATAL(including propagation), 
+   the function must release resources allocated by the 
+   function itself.
+*/
+typedef struct _at_exception_type at_exception_type;
+struct _at_exception_type {
+    at_msg_type msg_type;
+    at_msg_func client_func;
+    void **     client_data;
+};
+
+at_exception_type
+at_exception_new(at_msg_func       client_func,
+                 void *      const client_data);
+
+bool
+at_exception_got_fatal(at_exception_type * const exception);
+
+void
+at_exception_fatal(at_exception_type * const exception,
+                   const char *        const message);
+
+void
+at_exception_warning(at_exception_type * const exception,
+                     const char *        const message);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/converter/other/pamtosvg/fit.c b/converter/other/pamtosvg/fit.c
new file mode 100644
index 00000000..bcda033f
--- /dev/null
+++ b/converter/other/pamtosvg/fit.c
@@ -0,0 +1,1923 @@
+/* fit.c: turn a bitmap representation of a curve into a list of splines.
+    Some of the ideas, but not the code, comes from the Phoenix thesis.
+   See README for the reference.
+
+   The code was partially derived from limn.
+
+   Copyright (C) 1992 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include <math.h>
+#include <limits.h>
+#include <float.h>
+#include <string.h>
+#include <assert.h>
+
+#include "pm_c_util.h"
+#include "mallocvar.h"
+
+#include "autotrace.h"
+#include "fit.h"
+#include "message.h"
+#include "logreport.h"
+#include "spline.h"
+#include "vector.h"
+#include "curve.h"
+#include "pxl-outline.h"
+#include "epsilon-equal.h"
+
+#define CUBE(x) ((x) * (x) * (x))
+
+/* We need to manipulate lists of array indices.  */
+
+typedef struct index_list
+{
+  unsigned *data;
+  unsigned length;
+} index_list_type;
+
+/* The usual accessor macros.  */
+#define GET_INDEX(i_l, n)  ((i_l).data[n])
+#define INDEX_LIST_LENGTH(i_l)  ((i_l).length)
+#define GET_LAST_INDEX(i_l)  ((i_l).data[INDEX_LIST_LENGTH (i_l) - 1])
+
+static void append_index (index_list_type *, unsigned);
+static void free_index_list (index_list_type *);
+static index_list_type new_index_list (void);
+static void remove_adjacent_corners (index_list_type *, unsigned, bool,
+                     at_exception_type * exception);
+static void filter (curve_type, fitting_opts_type *);
+static void find_vectors
+  (unsigned, pixel_outline_type, vector_type *, vector_type *, unsigned);
+static float find_error (curve_type, spline_type, unsigned *,
+               at_exception_type * exception);
+static vector_type find_half_tangent (curve_type, bool start, unsigned *, unsigned);
+static void find_tangent (curve_type, bool, bool, unsigned);
+static void remove_knee_points (curve_type, bool);
+static void set_initial_parameter_values (curve_type);
+static float distance (float_coord, float_coord);
+
+
+static pm_pixelcoord
+real_to_int_coord(float_coord const real_coord) {
+/*----------------------------------------------------------------------------
+  Turn an real point into a integer one.
+-----------------------------------------------------------------------------*/
+
+    pm_pixelcoord int_coord;
+
+    int_coord.col = ROUND(real_coord.x);
+    int_coord.row = ROUND(real_coord.y);
+    
+    return int_coord;
+}
+
+
+static void
+appendCorner(index_list_type *  const cornerListP,
+             unsigned int       const pixelSeq,
+             pixel_outline_type const outline,
+             float              const angle,
+             char               const logType) {
+
+    pm_pixelcoord const coord = O_COORDINATE(outline, pixelSeq);
+
+    append_index(cornerListP, pixelSeq);
+    LOG4(" (%d,%d)%c%.3f", coord.col, coord.row, logType, angle);
+}
+
+
+
+static void
+lookAheadForBetterCorner(pixel_outline_type  const outline,
+                         unsigned int        const basePixelSeq,
+                         float               const baseCornerAngle,
+                         unsigned int        const cornerSurround,
+                         unsigned int        const cornerAlwaysThreshold,
+                         unsigned int *      const highestExaminedP,
+                         float *             const bestCornerAngleP,
+                         unsigned int *      const bestCornerIndexP,
+                         index_list_type *   const equallyGoodListP,
+                         index_list_type *   const cornerListP,
+                         at_exception_type * const exceptionP) {
+/*----------------------------------------------------------------------------
+   'basePixelSeq' is the sequence position within 'outline' of a pixel
+   that has a sufficiently small angle (to wit 'baseCornerAngle') to
+   be a corner.  We look ahead in 'outline' for an even better one.
+   We'll look up to 'cornerSurround' pixels ahead.
+
+   We return the pixel sequence of the best corner we find (which could
+   be the base) as *bestCornerIndexP.  Its angle is *bestCornerAngleP.
+
+   We return as *highestExaminedP the pixel sequence of the last pixel
+   we examined in our search (Caller can use this information to avoid
+   examining them again).
+
+   And we have this really dirty side effect: If we encounter any
+   corner whose angle is less than 'cornerAlwaysThreshold', we add
+   that to the list *cornerListP along the way.
+-----------------------------------------------------------------------------*/
+    float bestCornerAngle;
+    unsigned bestCornerIndex;
+    index_list_type equallyGoodList;
+    unsigned int q;
+    unsigned int i;
+
+    bestCornerIndex = basePixelSeq;     /* initial assumption */
+    bestCornerAngle = baseCornerAngle;    /* initial assumption */
+    
+    equallyGoodList = new_index_list();
+    
+    q = basePixelSeq;
+    i = basePixelSeq + 1;  /* Start with the next pixel */
+    
+    while (i < bestCornerIndex + cornerSurround &&
+           i < O_LENGTH(outline) &&
+           !at_exception_got_fatal(exceptionP)) {
+
+        vector_type inVector, outVector;
+        float cornerAngle;
+        
+        /* Check the angle.  */
+
+        q = i % O_LENGTH(outline);
+        find_vectors(q, outline, &inVector, &outVector, cornerSurround);
+        cornerAngle = Vangle(inVector, outVector, exceptionP);
+        if (!at_exception_got_fatal(exceptionP)) {
+            /* Perhaps the angle is sufficiently small that we want to
+               consider this a corner, even if it's not the best
+               (unless we've already wrapped around in the search, in
+               which case we have already added the corner, and we
+               don't want to add it again).
+            */
+            if (cornerAngle <= cornerAlwaysThreshold && q >= basePixelSeq)
+                appendCorner(cornerListP, q, outline, cornerAngle, '\\');
+
+            if (epsilon_equal(cornerAngle, bestCornerAngle))
+                append_index(&equallyGoodList, q);
+            else if (cornerAngle < bestCornerAngle) {
+                bestCornerAngle = cornerAngle;
+                /* We want to check `cornerSurround' pixels beyond the
+                   new best corner.
+                */
+                i = bestCornerIndex = q;
+                free_index_list(&equallyGoodList);
+                equallyGoodList = new_index_list();
+            }
+            ++i;
+        }
+    }
+    *bestCornerAngleP = bestCornerAngle;
+    *bestCornerIndexP = bestCornerIndex;
+    *equallyGoodListP = equallyGoodList;
+    *highestExaminedP = q;
+}
+
+
+
+static void
+establishCornerSearchLimits(pixel_outline_type  const outline,
+                            fitting_opts_type * const fittingOptsP,
+                            unsigned int *      const firstP,
+                            unsigned int *      const lastP) {
+/*----------------------------------------------------------------------------
+   Determine where in the outline 'outline' we should look for corners.
+-----------------------------------------------------------------------------*/
+    assert(O_LENGTH(outline) >= 1);
+    assert(O_LENGTH(outline) - 1 >= fittingOptsP->corner_surround);
+
+    *firstP = 0;
+    *lastP  = O_LENGTH(outline) - 1;
+    if (outline.open) {
+        *firstP += fittingOptsP->corner_surround;
+        *lastP  -= fittingOptsP->corner_surround;
+    }
+}
+
+
+
+static void
+removeAdjacent(index_list_type *   const cornerListP,
+               pixel_outline_type  const outline,
+               fitting_opts_type * const fittingOptsP,
+               at_exception_type * const exception) {
+               
+    /* We never want two corners next to each other, since the
+       only way to fit such a ``curve'' would be with a straight
+       line, which usually interrupts the continuity dreadfully.
+    */
+
+    if (INDEX_LIST_LENGTH(*cornerListP) > 0)
+        remove_adjacent_corners(
+            cornerListP,
+            O_LENGTH(outline) - (outline.open ? 2 : 1),
+            fittingOptsP->remove_adjacent_corners,
+            exception);
+}
+
+
+
+static index_list_type
+find_corners(pixel_outline_type  const outline,
+             fitting_opts_type * const fittingOptsP,
+             at_exception_type * const exceptionP) {
+
+    /* We consider a point to be a corner if (1) the angle defined by
+       the `corner_surround' points coming into it and going out from
+       it is less than `corner_threshold' degrees, and no point within
+       `corner_surround' points has a smaller angle; or (2) the angle
+       is less than `corner_always_threshold' degrees.
+    */
+    unsigned int p;
+    unsigned int firstPixelSeq, lastPixelSeq;
+    index_list_type cornerList;
+
+    cornerList = new_index_list();
+
+    if (O_LENGTH(outline) <= fittingOptsP->corner_surround * 2 + 1)
+        return cornerList;
+
+    establishCornerSearchLimits(outline, fittingOptsP,
+                                &firstPixelSeq, &lastPixelSeq);
+    
+    /* Consider each pixel on the outline in turn.  */
+    for (p = firstPixelSeq; p <= lastPixelSeq;) {
+        vector_type inVector, outVector;
+        float cornerAngle;
+
+        /* Check if the angle is small enough.  */
+        find_vectors(p, outline, &inVector, &outVector,
+                     fittingOptsP->corner_surround);
+        cornerAngle = Vangle(inVector, outVector, exceptionP);
+        if (at_exception_got_fatal(exceptionP))
+            goto cleanup;
+
+        if (fabs(cornerAngle) <= fittingOptsP->corner_threshold) {
+            /* We want to keep looking, instead of just appending the
+               first pixel we find with a small enough angle, since there
+               might be another corner within `corner_surround' pixels, with
+               a smaller angle.  If that is the case, we want that one.
+
+               If we come across a corner that is just as good as the
+               best one, we should make it a corner, too.  This
+               happens, for example, at the points on the `W' in some
+               typefaces, where the "points" are flat.
+            */
+            float bestCornerAngle;
+            unsigned bestCornerIndex;
+            index_list_type equallyGoodList;
+            unsigned int q;
+
+            if (cornerAngle <= fittingOptsP->corner_always_threshold)
+                /* The angle is sufficiently small that we want to
+                   consider this a corner, even if it's not the best.
+                */
+                appendCorner(&cornerList, p, outline, cornerAngle, '\\');
+
+            lookAheadForBetterCorner(outline, p, cornerAngle,
+                                     fittingOptsP->corner_surround,
+                                     fittingOptsP->corner_always_threshold,
+                                     &q,
+                                     &bestCornerAngle, &bestCornerIndex,
+                                     &equallyGoodList,
+                                     &cornerList,
+                                     exceptionP);
+
+            if (at_exception_got_fatal(exceptionP))
+                goto cleanup;
+
+            /* `q' is the index of the last point lookAhead checked.
+               He added the corner if `bestCornerAngle' is less than
+               `corner_always_threshold'.  If we've wrapped around, we
+               added the corner on the first pass.  Otherwise, we add
+               the corner now.
+            */
+            if (bestCornerAngle > fittingOptsP->corner_always_threshold
+                && bestCornerIndex >= p) {
+
+                unsigned int j;
+                
+                appendCorner(&cornerList, bestCornerIndex,
+                             outline, bestCornerAngle, '/');
+                
+                for (j = 0; j < INDEX_LIST_LENGTH (equallyGoodList); ++j)
+                    appendCorner(&cornerList, GET_INDEX(equallyGoodList, j),
+                                 outline, bestCornerAngle, '@');
+            }
+            free_index_list(&equallyGoodList);
+
+            /* If we wrapped around in our search, we're done;
+               otherwise, we move on to the pixel after the highest
+               one we just checked.
+            */
+            p = (q < p) ? O_LENGTH(outline) : q + 1;
+        } else
+            ++p;
+    }
+    removeAdjacent(&cornerList, outline, fittingOptsP, exceptionP);
+
+cleanup:  
+    return cornerList;
+}
+
+
+
+static void
+makeOutlineOneCurve(pixel_outline_type const outline,
+                    curve_list_type *  const curveListP) {
+/*----------------------------------------------------------------------------
+   Add to *curveListP a single curve that represents the outline 'outline'.
+-----------------------------------------------------------------------------*/
+    curve_type curve;
+    unsigned int pixelSeq;
+
+    curve = new_curve();
+    
+    for (pixelSeq = 0; pixelSeq < O_LENGTH(outline); ++pixelSeq)
+        append_pixel(curve, O_COORDINATE(outline, pixelSeq));
+    
+    if (curveListP->open)
+        CURVE_CYCLIC(curve) = false;
+    else
+        CURVE_CYCLIC(curve) = true;
+    
+    /* Make it a one-curve cycle */
+    NEXT_CURVE(curve)     = curve;
+    PREVIOUS_CURVE(curve) = curve;
+
+    append_curve(curveListP, curve);
+}
+
+
+
+static void
+addCurveStartingAtCorner(pixel_outline_type const outline,
+                         index_list_type    const cornerList,
+                         unsigned int       const cornerSeq,
+                         curve_list_type *  const curveListP) {
+
+    unsigned int const cornerPixelSeq = GET_INDEX(cornerList, cornerSeq);
+    
+    unsigned int lastPixelSeq;
+    curve_type curve;
+    unsigned int pixelSeq;
+    
+    if (cornerSeq + 1 >= cornerList.length)
+        /* No more corners, so we go through the end of the outline. */
+        lastPixelSeq = O_LENGTH(outline) - 1;
+    else
+        /* Go through the next corner */
+        lastPixelSeq = GET_INDEX(cornerList, cornerSeq + 1);
+    
+    curve = new_curve();
+
+    for (pixelSeq = cornerPixelSeq; pixelSeq <= lastPixelSeq; ++pixelSeq)
+        append_pixel(curve, O_COORDINATE(outline, pixelSeq));
+    
+    {
+        /* Add curve to end of chain */
+        if (!CURVE_LIST_EMPTY(*curveListP)) {
+            curve_type const previousCurve = LAST_CURVE_LIST_ELT(*curveListP);
+            NEXT_CURVE(previousCurve) = curve;
+            PREVIOUS_CURVE(curve)     = previousCurve;
+        }
+    }
+    append_curve(curveListP, curve);
+}
+
+
+
+static void
+divideOutlineWithCorners(pixel_outline_type const outline,
+                         index_list_type    const cornerList,
+                         curve_list_type *  const curveListP) {
+/*----------------------------------------------------------------------------
+   Divide the outline 'outline' into curves at the corner points
+   'cornerList' and add each curve to *curveListP.
+
+   Each curve contains the corners at each end.
+
+   The last curve is special.  It consists of the pixels (inclusive)
+   between the last corner and the end of the outline, and the
+   beginning of the outline and the first corner.
+
+   We link the curves in a chain.  If the outline (and therefore the
+   curve list) is closed, the chain is a cycle of all the curves.  If
+   it is open, the chain is a linear chain of all the curves except
+   the last one (the one that goes from the last corner to the first
+   corner).
+
+   Assume there is at least one corner.
+-----------------------------------------------------------------------------*/
+    unsigned int const firstCurveSeq = CURVE_LIST_LENGTH(*curveListP);
+        /* Index in curve list of the first curve we add */
+    unsigned int cornerSeq;
+
+    assert(cornerList.length > 0);
+
+    if (outline.open) {
+        /* Start with a curve that contains the point up to the first
+           corner
+        */
+        curve_type curve;
+        unsigned int pixelSeq;
+        
+        curve = new_curve();
+
+        for (pixelSeq = 0; pixelSeq <= GET_INDEX(cornerList, 0); ++pixelSeq)
+            append_pixel(curve, O_COORDINATE(outline, pixelSeq));
+
+        append_curve(curveListP, curve);
+    } else
+        /* We'll pick up the pixels before the first corner at the end */
+
+    /* Add to the list a curve that starts at each corner and goes
+       through the following corner, or the end of the outline if
+       there is no following corner.  Do it in order of the corners.
+    */
+    for (cornerSeq = 0; cornerSeq < cornerList.length; ++cornerSeq)
+        addCurveStartingAtCorner(outline, cornerList, cornerSeq, curveListP);
+
+    if (!outline.open) {
+        /* Come around to the start of the curve list -- add the pixels
+           before the first corner to the last curve, and chain the last
+           curve to the first one.
+        */
+        curve_type const firstCurve =
+            CURVE_LIST_ELT(*curveListP, firstCurveSeq);
+        curve_type const lastCurve  =
+            LAST_CURVE_LIST_ELT(*curveListP);
+
+        unsigned int pixelSeq;
+
+        for (pixelSeq = 0; pixelSeq <= GET_INDEX(cornerList, 0); ++pixelSeq)
+            append_pixel(lastCurve, O_COORDINATE(outline, pixelSeq));
+
+        NEXT_CURVE(lastCurve)      = firstCurve;
+        PREVIOUS_CURVE(firstCurve) = lastCurve;
+    }
+}
+
+
+
+static curve_list_array_type
+split_at_corners(pixel_outline_list_type const pixel_list,
+                 fitting_opts_type *     const fitting_opts,
+                 at_exception_type *     const exception) {
+/*----------------------------------------------------------------------------
+   Find the corners in PIXEL_LIST, the list of points.  (Presumably we
+   can't fit a single spline around a corner.)  The general strategy
+   is to look through all the points, remembering which we want to
+   consider corners.  Then go through that list, producing the
+   curve_list.  This is dictated by the fact that PIXEL_LIST does not
+   necessarily start on a corner---it just starts at the character's
+   first outline pixel, going left-to-right, top-to-bottom.  But we
+   want all our splines to start and end on real corners.
+
+   For example, consider the top of a capital `C' (this is in cmss20):
+                     x
+                     ***********
+                  ******************
+
+   PIXEL_LIST will start at the pixel below the `x'.  If we considered
+   this pixel a corner, we would wind up matching a very small segment
+   from there to the end of the line, probably as a straight line, which
+   is certainly not what we want.
+
+   PIXEL_LIST has one element for each closed outline on the character.
+   To preserve this information, we return an array of curve_lists, one
+   element (which in turn consists of several curves, one between each
+   pair of corners) for each element in PIXEL_LIST.
+-----------------------------------------------------------------------------*/
+    unsigned outlineSeq;
+    curve_list_array_type curve_array;
+
+    curve_array = new_curve_list_array();
+
+    LOG("\nFinding corners:\n");
+
+    for (outlineSeq = 0;
+         outlineSeq < O_LIST_LENGTH(pixel_list);
+         ++outlineSeq) {
+
+        pixel_outline_type const outline =
+            O_LIST_OUTLINE(pixel_list, outlineSeq);
+
+        index_list_type corner_list;
+        curve_list_type curve_list;
+
+        curve_list = new_curve_list();
+
+        CURVE_LIST_CLOCKWISE(curve_list) = O_CLOCKWISE(outline);
+        curve_list.color = outline.color;
+        curve_list.open  = outline.open;
+
+        LOG1("#%u:", outlineSeq);
+        
+        /* If the outline does not have enough points, we can't do
+           anything.  The endpoints of the outlines are automatically
+           corners.  We need at least `corner_surround' more pixels on
+           either side of a point before it is conceivable that we might
+           want another corner.
+        */
+        if (O_LENGTH(outline) > fitting_opts->corner_surround * 2 + 2)
+            corner_list = find_corners(outline, fitting_opts, exception);
+
+        else {
+            int const surround = (O_LENGTH(outline) - 3) / 2;
+            if (surround >= 2) {
+                unsigned int const save_corner_surround =
+                    fitting_opts->corner_surround;
+                fitting_opts->corner_surround = surround;
+                corner_list = find_corners(outline, fitting_opts, exception);
+                fitting_opts->corner_surround = save_corner_surround;
+            } else {
+                corner_list.length = 0;
+                corner_list.data = NULL;
+            }
+        }
+
+        if (corner_list.length == 0)
+            /* No corners.  Use all of the pixel outline as the one curve. */
+            makeOutlineOneCurve(outline, &curve_list);
+        else
+            divideOutlineWithCorners(outline, corner_list, &curve_list);
+        
+        LOG1(" [%u].\n", corner_list.length);
+        free_index_list(&corner_list);
+
+        /* And now add the just-completed curve list to the array.  */
+        append_curve_list(&curve_array, curve_list);
+    }
+
+    return curve_array;
+}
+
+
+
+static void
+removeKnees(curve_list_type const curveList) {
+/*----------------------------------------------------------------------------
+  Remove the extraneous ``knee'' points before filtering.  Since the
+  corners have already been found, we don't need to worry about
+  removing a point that should be a corner.
+-----------------------------------------------------------------------------*/
+    unsigned int curveSeq;
+    
+    LOG("\nRemoving knees:\n");
+    for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) {
+        LOG1("#%u:", curveSeq);
+        remove_knee_points(CURVE_LIST_ELT(curveList, curveSeq),
+                           CURVE_LIST_CLOCKWISE(curveList));
+    }
+}
+    
+
+
+static void
+computePointWeights(curve_list_type     const curveList,
+                    fitting_opts_type * const fittingOptsP,
+                    distance_map_type * const distP) {
+
+    unsigned int const height = distP->height;
+
+    unsigned int curveSeq;
+    
+    for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) {
+        unsigned pointSeq;
+        curve_type const curve = CURVE_LIST_ELT(curveList, curveSeq);
+        for (pointSeq = 0; pointSeq < CURVE_LENGTH(curve); ++pointSeq) {
+            float_coord * const coordP = &CURVE_POINT(curve, pointSeq);
+            unsigned int x = coordP->x;
+            unsigned int y = height - (unsigned int)coordP->y - 1;
+            
+            float width, w;
+
+            /* Each (x, y) is a point on the skeleton of the curve, which
+               might be offset from the true centerline, where the width
+               is maximal.  Therefore, use as the local line width the
+               maximum distance over the neighborhood of (x, y). 
+            */
+            width = distP->d[y][x];  /* initial value */
+            if (y - 1 >= 0) {
+                if ((w = distP->d[y-1][x]) > width)
+                    width = w;
+                if (x - 1 >= 0) {
+                    if ((w = distP->d[y][x-1]) > width)
+                        width = w;
+                    if ((w = distP->d[y-1][x-1]) > width)
+                        width = w;
+                }
+                if (x + 1 < distP->width) {
+                    if ((w = distP->d[y][x+1]) > width)
+                        width = w;
+                    if ((w = distP->d[y-1][x+1]) > width)
+                        width = w;
+                }
+            }
+            if (y + 1 < height) {
+                if ((w = distP->d[y+1][x]) > width)
+                    width = w;
+                if (x - 1 >= 0 && (w = distP->d[y+1][x-1]) > width)
+                    width = w;
+                if (x + 1 < distP->width && (w = distP->d[y+1][x+1]) > width)
+                    width = w;
+            }
+            coordP->z = width * (fittingOptsP->width_weight_factor);
+        }
+    }
+}
+
+
+
+static void
+filterCurves(curve_list_type     const curveList,
+             fitting_opts_type * const fittingOptsP) {
+             
+    unsigned int curveSeq;
+
+    LOG("\nFiltering curves:\n");
+
+    for (curveSeq = 0; curveSeq < curveList.length; ++curveSeq) {
+        LOG1("#%u: ", curveSeq);
+        filter(CURVE_LIST_ELT(curveList, curveSeq), fittingOptsP);
+    }
+}
+
+
+
+static void
+logSplinesForCurve(unsigned int     const curveSeq,
+                   spline_list_type const curveSplines) {
+
+    unsigned int splineSeq;
+
+    LOG1("Fitted splines for curve #%u:\n", curveSeq);
+    for (splineSeq = 0;
+         splineSeq < SPLINE_LIST_LENGTH(curveSplines);
+         ++splineSeq) {
+        LOG1("  %u: ", splineSeq);
+        if (log_file)
+            print_spline(log_file, SPLINE_LIST_ELT(curveSplines, splineSeq));
+    }
+}     
+       
+
+
+static void
+change_bad_lines(spline_list_type *        const spline_list,
+                 const fitting_opts_type * const fitting_opts) {
+
+/* Unfortunately, we cannot tell in isolation whether a given spline
+   should be changed to a line or not.  That can only be known after the
+   entire curve has been fit to a list of splines.  (The curve is the
+   pixel outline between two corners.)  After subdividing the curve, a
+   line may very well fit a portion of the curve just as well as the
+   spline---but unless a spline is truly close to being a line, it
+   should not be combined with other lines.  */
+
+  unsigned this_spline;
+  bool found_cubic = false;
+  unsigned length = SPLINE_LIST_LENGTH (*spline_list);
+
+  LOG1 ("\nChecking for bad lines (length %u):\n", length);
+
+  /* First see if there are any splines in the fitted shape.  */
+  for (this_spline = 0; this_spline < length; this_spline++)
+    {
+      if (SPLINE_DEGREE (SPLINE_LIST_ELT (*spline_list, this_spline)) ==
+       CUBICTYPE)
+        {
+          found_cubic = true;
+          break;
+        }
+    }
+
+  /* If so, change lines back to splines (we haven't done anything to
+     their control points, so we only have to change the degree) unless
+     the spline is close enough to being a line.  */
+  if (found_cubic)
+    for (this_spline = 0; this_spline < length; this_spline++)
+      {
+        spline_type s = SPLINE_LIST_ELT (*spline_list, this_spline);
+
+        if (SPLINE_DEGREE (s) == LINEARTYPE)
+          {
+            LOG1 ("  #%u: ", this_spline);
+            if (SPLINE_LINEARITY (s) > fitting_opts->line_reversion_threshold)
+              {
+                LOG ("reverted, ");
+                SPLINE_DEGREE (SPLINE_LIST_ELT (*spline_list, this_spline))
+                  = CUBICTYPE;
+              }
+            LOG1 ("linearity %.3f.\n", SPLINE_LINEARITY (s));
+          }
+      }
+    else
+      LOG ("  No lines.\n");
+}
+
+
+
+static bool
+spline_linear_enough(spline_type *             const spline,
+                     curve_type                const curve,
+                     const fitting_opts_type * const fitting_opts) {
+
+/* Supposing that we have accepted the error, another question arises:
+   would we be better off just using a straight line?  */
+
+  float A, B, C;
+  unsigned this_point;
+  float dist = 0.0, start_end_dist, threshold;
+
+  LOG ("Checking linearity:\n");
+
+  A = END_POINT(*spline).x - START_POINT(*spline).x;
+  B = END_POINT(*spline).y - START_POINT(*spline).y;
+  C = END_POINT(*spline).z - START_POINT(*spline).z;
+
+  start_end_dist = (float) (SQR(A) + SQR(B) + SQR(C));
+  LOG1 ("start_end_distance is %.3f.\n", sqrt(start_end_dist));
+
+  LOG3 ("  Line endpoints are (%.3f, %.3f, %.3f) and ", START_POINT(*spline).x, START_POINT(*spline).y, START_POINT(*spline).z);
+  LOG3 ("(%.3f, %.3f, %.3f)\n", END_POINT(*spline).x, END_POINT(*spline).y, END_POINT(*spline).z);
+
+  /* LOG3 ("  Line is %.3fx + %.3fy + %.3f = 0.\n", A, B, C); */
+
+  for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
+    {
+      float a, b, c, w;
+      float t = CURVE_T (curve, this_point);
+      float_coord spline_point = evaluate_spline (*spline, t);
+
+      a = spline_point.x - START_POINT(*spline).x;
+      b = spline_point.y - START_POINT(*spline).y;
+      c = spline_point.z - START_POINT(*spline).z;
+      w = (A*a + B*b + C*c) / start_end_dist;
+
+      dist += (float)sqrt(SQR(a-A*w) + SQR(b-B*w) + SQR(c-C*w));
+    }
+  LOG1 ("  Total distance is %.3f, ", dist);
+
+  dist /= (CURVE_LENGTH (curve) - 1);
+  LOG1 ("which is %.3f normalized.\n", dist);
+
+  /* We want reversion of short curves to splines to be more likely than
+     reversion of long curves, hence the second division by the curve
+     length, for use in `change_bad_lines'.  */
+  SPLINE_LINEARITY (*spline) = dist;
+  LOG1 ("  Final linearity: %.3f.\n", SPLINE_LINEARITY (*spline));
+  if (start_end_dist * (float) 0.5 > fitting_opts->line_threshold)
+    threshold = fitting_opts->line_threshold;
+  else
+    threshold = start_end_dist * (float) 0.5;
+  LOG1 ("threshold is %.3f .\n", threshold);
+  if (dist < threshold)
+    return true;
+  else
+    return false;
+}
+
+
+
+/* Forward declaration for recursion */
+
+static spline_list_type *
+fitCurve(curve_type                const curve,
+         const fitting_opts_type * const fitting_opts,
+         at_exception_type *       const exception);
+
+
+
+static spline_list_type *
+fit_with_line(curve_type const curve) {
+/*----------------------------------------------------------------------------
+  This routine returns the curve fitted to a straight line in a very
+  simple way: make the first and last points on the curve be the
+  endpoints of the line.  This simplicity is justified because we are
+  called only on very short curves.
+-----------------------------------------------------------------------------*/
+    spline_type line;
+
+    LOG("Fitting with straight line:\n");
+
+    SPLINE_DEGREE(line) = LINEARTYPE;
+    START_POINT(line) = CONTROL1(line) = CURVE_POINT(curve, 0);
+    END_POINT(line) = CONTROL2(line) = LAST_CURVE_POINT(curve);
+
+    /* Make sure that this line is never changed to a cubic.  */
+    SPLINE_LINEARITY(line) = 0;
+
+    if (log_file) {
+        LOG("  ");
+        print_spline(log_file, line);
+    }
+
+    return new_spline_list_with_spline(line);
+}
+
+
+
+#define B0(t) CUBE ((float) 1.0 - (t))
+#define B1(t) ((float) 3.0 * (t) * SQR ((float) 1.0 - (t)))
+#define B2(t) ((float) 3.0 * SQR (t) * ((float) 1.0 - (t)))
+#define B3(t) CUBE (t)
+
+static spline_type
+fit_one_spline(curve_type          const curve, 
+               at_exception_type * const exception) {
+/*----------------------------------------------------------------------------
+   Our job here is to find alpha1 (and alpha2), where t1_hat (t2_hat) is
+   the tangent to CURVE at the starting (ending) point, such that:
+
+   control1 = alpha1 * t1_hat + starting point
+   control2 = alpha2 * t2_hat + ending_point
+
+   and the resulting spline (starting_point .. control1 and control2 ..
+   ending_point) minimizes the least-square error from CURVE.
+
+   See pp.57--59 of the Phoenix thesis.
+
+   The B?(t) here corresponds to B_i^3(U_i) there.
+   The Bernshte\u in polynomials of degree n are defined by
+   B_i^n(t) = { n \choose i } t^i (1-t)^{n-i}, i = 0..n
+-----------------------------------------------------------------------------*/
+    /* Since our arrays are zero-based, the `C0' and `C1' here correspond
+       to `C1' and `C2' in the paper. 
+    */
+    float X_C1_det, C0_X_det, C0_C1_det;
+    float alpha1, alpha2;
+    spline_type spline;
+    vector_type start_vector, end_vector;
+    unsigned i;
+    vector_type * A;
+    vector_type t1_hat;
+    vector_type t2_hat;
+    float C[2][2] = { { 0.0, 0.0 }, { 0.0, 0.0 } };
+    float X[2] = { 0.0, 0.0 };
+
+    t1_hat = *CURVE_START_TANGENT(curve);  /* initial value */
+    t2_hat = *CURVE_END_TANGENT(curve);    /* initial value */
+
+    MALLOCARRAY_NOFAIL(A, CURVE_LENGTH(curve) * 2);
+
+    START_POINT(spline) = CURVE_POINT(curve, 0);
+    END_POINT(spline)   = LAST_CURVE_POINT(curve);
+    start_vector = make_vector(START_POINT(spline));
+    end_vector   = make_vector(END_POINT(spline));
+
+    for (i = 0; i < CURVE_LENGTH(curve); ++i) {
+        A[(i << 1) + 0] = Vmult_scalar(t1_hat, B1(CURVE_T(curve, i)));
+        A[(i << 1) + 1] = Vmult_scalar(t2_hat, B2(CURVE_T(curve, i)));
+    }
+
+    for (i = 0; i < CURVE_LENGTH(curve); ++i) {
+        vector_type temp, temp0, temp1, temp2, temp3;
+        vector_type * Ai = A + (i << 1);
+
+        C[0][0] += Vdot(Ai[0], Ai[0]);
+        C[0][1] += Vdot(Ai[0], Ai[1]);
+        /* C[1][0] = C[0][1] (this is assigned outside the loop)  */
+        C[1][1] += Vdot(Ai[1], Ai[1]);
+
+        /* Now the right-hand side of the equation in the paper.  */
+        temp0 = Vmult_scalar(start_vector, B0(CURVE_T(curve, i)));
+        temp1 = Vmult_scalar(start_vector, B1(CURVE_T(curve, i)));
+        temp2 = Vmult_scalar(end_vector, B2(CURVE_T(curve, i)));
+        temp3 = Vmult_scalar(end_vector, B3(CURVE_T(curve, i)));
+
+        temp = make_vector(
+            Vsubtract_point(CURVE_POINT(curve, i),
+                            Vadd(temp0, Vadd(temp1, Vadd(temp2, temp3)))));
+
+        X[0] += Vdot(temp, Ai[0]);
+        X[1] += Vdot(temp, Ai[1]);
+    }
+    free(A);
+
+    C[1][0] = C[0][1];
+    
+    X_C1_det = X[0] * C[1][1] - X[1] * C[0][1];
+    C0_X_det = C[0][0] * X[1] - C[0][1] * X[0];
+    C0_C1_det = C[0][0] * C[1][1] - C[1][0] * C[0][1];
+    if (C0_C1_det == 0.0) {
+        LOG ("zero determinant of C0*C1");
+        at_exception_fatal(exception, "zero determinant of C0*C1");
+        goto cleanup;
+    }
+
+    alpha1 = X_C1_det / C0_C1_det;
+    alpha2 = C0_X_det / C0_C1_det;
+
+    CONTROL1(spline) = Vadd_point(START_POINT(spline),
+                                  Vmult_scalar(t1_hat, alpha1));
+    CONTROL2(spline) = Vadd_point(END_POINT(spline),
+                                  Vmult_scalar(t2_hat, alpha2));
+    SPLINE_DEGREE(spline) = CUBICTYPE;
+
+cleanup:
+    return spline;
+}
+
+
+
+static void
+logSplineFit(spline_type const spline) {
+
+    if (SPLINE_DEGREE(spline) == LINEARTYPE)
+        LOG("  fitted to line:\n");
+    else
+        LOG("  fitted to spline:\n");
+    
+    if (log_file) {
+        LOG ("    ");
+        print_spline (log_file, spline);
+    }
+}
+
+
+
+static spline_list_type *
+fit_with_least_squares(curve_type                const curve,
+                       const fitting_opts_type * const fitting_opts,
+                       at_exception_type *       const exception) {
+/*----------------------------------------------------------------------------
+  The least squares method is well described in Schneider's thesis.
+  Briefly, we try to fit the entire curve with one spline.  If that
+  fails, we subdivide the curve. 
+-----------------------------------------------------------------------------*/
+    float error;
+    float best_error;
+    spline_type spline;
+    spline_type best_spline;
+    spline_list_type * spline_list;
+    unsigned int worst_point;
+    float previous_error;
+    
+    best_error = FLT_MAX;  /* initial value */
+    previous_error = FLT_MAX;  /* initial value */
+    spline_list = NULL;  /* initial value */
+    worst_point = 0;  /* initial value */
+
+    LOG ("\nFitting with least squares:\n");
+    
+    /* Phoenix reduces the number of points with a ``linear spline
+       technique''.  But for fitting letterforms, that is
+       inappropriate.  We want all the points we can get.
+    */
+    
+    /* It makes no difference whether we first set the `t' values or
+       find the tangents.  This order makes the documentation a little
+       more coherent.
+    */
+
+    LOG("Finding tangents:\n");
+    find_tangent(curve, /* to_start */ true,  /* cross_curve */ false,
+                 fitting_opts->tangent_surround);
+    find_tangent(curve, /* to_start */ false, /* cross_curve */ false,
+                 fitting_opts->tangent_surround);
+
+    set_initial_parameter_values(curve);
+
+    /* Now we loop, subdividing, until CURVE has been fit.  */
+    while (true) {
+        float error;
+
+        spline = fit_one_spline(curve, exception);
+        best_spline = spline;
+        if (at_exception_got_fatal(exception))
+            goto cleanup;
+
+        logSplineFit(spline);
+        
+        if (SPLINE_DEGREE(spline) == LINEARTYPE)
+            break;
+
+        error = find_error(curve, spline, &worst_point, exception);
+        if (error <= previous_error) {
+            best_error  = error;
+            best_spline = spline;
+        }
+        break;
+    }
+
+    if (SPLINE_DEGREE(spline) == LINEARTYPE) {
+        spline_list = new_spline_list_with_spline(spline);
+        LOG1("Accepted error of %.3f.\n", error);
+        return spline_list;
+    }
+
+    /* Go back to the best fit.  */
+    spline = best_spline;
+    error = best_error;
+
+    if (error < fitting_opts->error_threshold && !CURVE_CYCLIC(curve)) {
+        /* The points were fitted with a spline.  We end up here
+           whenever a fit is accepted.  We have one more job: see if
+           the ``curve'' that was fit should really be a straight
+           line.
+        */
+        if (spline_linear_enough(&spline, curve, fitting_opts)) {
+            SPLINE_DEGREE(spline) = LINEARTYPE;
+            LOG("Changed to line.\n");
+        }
+        spline_list = new_spline_list_with_spline(spline);
+        LOG1("Accepted error of %.3f.\n", error);
+    } else {
+        /* We couldn't fit the curve acceptably, so subdivide.  */
+        unsigned subdivision_index;
+        spline_list_type * left_spline_list;
+        spline_list_type * right_spline_list;
+        curve_type left_curve, right_curve;
+
+        left_curve  = new_curve();
+        right_curve = new_curve();
+
+        /* Insert 'left_curve', then 'right_curve' after 'curve' in the list */
+        NEXT_CURVE(right_curve) = NEXT_CURVE(curve);
+        PREVIOUS_CURVE(right_curve) = left_curve;
+        NEXT_CURVE(left_curve) = right_curve;
+        PREVIOUS_CURVE(left_curve) = curve;
+        NEXT_CURVE(curve) = left_curve;
+
+        LOG1("\nSubdividing (error %.3f):\n", error);
+        LOG3("  Original point: (%.3f,%.3f), #%u.\n",
+             CURVE_POINT (curve, worst_point).x,
+             CURVE_POINT (curve, worst_point).y, worst_point);
+        subdivision_index = worst_point;
+        LOG3 ("  Final point: (%.3f,%.3f), #%u.\n",
+              CURVE_POINT (curve, subdivision_index).x,
+              CURVE_POINT (curve, subdivision_index).y, subdivision_index);
+
+        /* The last point of the left-hand curve will also be the first
+           point of the right-hand curve.  */
+        CURVE_LENGTH(left_curve)  = subdivision_index + 1;
+        CURVE_LENGTH(right_curve) = CURVE_LENGTH(curve) - subdivision_index;
+        left_curve->point_list = curve->point_list;
+        right_curve->point_list = curve->point_list + subdivision_index;
+
+        /* We want to use the tangents of the curve which we are
+           subdividing for the start tangent for left_curve and the
+           end tangent for right_curve.
+        */
+        CURVE_START_TANGENT(left_curve) = CURVE_START_TANGENT(curve);
+        CURVE_END_TANGENT(right_curve)  = CURVE_END_TANGENT(curve);
+
+        /* We have to set up the two curves before finding the tangent at
+           the subdivision point.  The tangent at that point must be the
+           same for both curves, or noticeable bumps will occur in the
+           character.  But we want to use information on both sides of the
+           point to compute the tangent, hence cross_curve = true.
+        */
+        find_tangent(left_curve, /* to_start_point: */ false,
+                     /* cross_curve: */ true, fitting_opts->tangent_surround);
+        CURVE_START_TANGENT(right_curve) = CURVE_END_TANGENT(left_curve);
+
+        /* Now that we've set up the curves, we can fit them.  */
+        left_spline_list = fitCurve(left_curve, fitting_opts, exception);
+        if (at_exception_got_fatal(exception))
+            /* TODO: Memory allocated for left_curve and right_curve
+               will leak.*/
+            goto cleanup;
+
+        right_spline_list = fitCurve(right_curve, fitting_opts, exception);
+        /* TODO: Memory allocated for left_curve and right_curve
+           will leak.*/
+        if (at_exception_got_fatal(exception))
+            goto cleanup;
+        
+        /* Neither of the subdivided curves could be fit, so fail.  */
+        if (left_spline_list == NULL && right_spline_list == NULL)
+            return NULL;
+
+        /* Put the two together (or whichever of them exist).  */
+        spline_list = new_spline_list();
+
+        if (left_spline_list == NULL) {
+            LOG1("Could not fit spline to left curve (%lx).\n",
+                 (unsigned long) left_curve);
+            at_exception_warning(exception, "Could not fit left spline list");
+        } else {
+            concat_spline_lists(spline_list, *left_spline_list);
+            free_spline_list(*left_spline_list);
+            free(left_spline_list);
+        }
+        
+        if (right_spline_list == NULL) {
+            LOG1("Could not fit spline to right curve (%lx).\n",
+                 (unsigned long) right_curve);
+            at_exception_warning(exception, "Could not fit right spline list");
+        } else {
+            concat_spline_lists(spline_list, *right_spline_list);
+            free_spline_list(*right_spline_list);
+            free(right_spline_list);
+        }
+        if (CURVE_END_TANGENT(left_curve))
+            free(CURVE_END_TANGENT(left_curve));
+        free(left_curve);
+        free(right_curve);
+    }
+cleanup:
+
+    return spline_list;
+}
+
+
+
+static spline_list_type *
+fitCurve(curve_type                const curve,
+         const fitting_opts_type * const fittingOptsP,
+         at_exception_type *       const exception) {
+/*----------------------------------------------------------------------------
+  Transform a set of locations to a list of splines (the fewer the
+  better).  We are guaranteed that CURVE does not contain any corners.
+  We return NULL if we cannot fit the points at all.
+-----------------------------------------------------------------------------*/
+    spline_list_type * fittedSplinesP;
+
+    if (CURVE_LENGTH(curve) < 2) {
+        LOG("Tried to fit curve with less than two points");
+        at_exception_warning(exception, 
+                             "Tried to fit curve with less than two points");
+        fittedSplinesP = NULL;
+    } else if (CURVE_LENGTH(curve) < 4)
+        fittedSplinesP = fit_with_line(curve);
+    else
+        fittedSplinesP =
+            fit_with_least_squares(curve, fittingOptsP, exception);
+
+    return fittedSplinesP;
+}
+
+
+
+static void
+fitCurves(curve_list_type           const curveList,
+          pixel                     const color,
+          const fitting_opts_type * const fittingOptsP,
+          spline_list_type *        const splinesP,
+          at_exception_type *       const exceptionP) {
+          
+    spline_list_type curveListSplines;
+    unsigned int curveSeq;
+
+    curveListSplines = empty_spline_list();
+    
+    curveListSplines.open      = curveList.open;
+    curveListSplines.clockwise = curveList.clockwise;
+    curveListSplines.color     = color;
+
+    for (curveSeq = 0;
+         curveSeq < curveList.length && !at_exception_got_fatal(exceptionP);
+         ++curveSeq) {
+
+        curve_type const currentCurve = CURVE_LIST_ELT(curveList, curveSeq);
+
+        spline_list_type * curveSplinesP;
+
+        LOG1("\nFitting curve #%u:\n", curveSeq);
+
+        curveSplinesP = fitCurve(currentCurve, fittingOptsP, exceptionP);
+        if (!at_exception_got_fatal(exceptionP)) {
+            if (curveSplinesP == NULL) {
+                LOG1("Could not fit curve #%u", curveSeq);
+                at_exception_warning(exceptionP, "Could not fit curve");
+            } else {
+                logSplinesForCurve(curveSeq, *curveSplinesP);
+                
+                /* After fitting, we may need to change some would-be lines
+                   back to curves, because they are in a list with other
+                   curves.
+                */
+                change_bad_lines(curveSplinesP, fittingOptsP);
+                
+                concat_spline_lists(&curveListSplines, *curveSplinesP);
+                free_spline_list(*curveSplinesP);
+                free(curveSplinesP);
+            }
+        }
+    }
+    if (at_exception_got_fatal(exceptionP))
+        free_spline_list(curveListSplines);
+    else
+        *splinesP = curveListSplines;
+}
+    
+
+
+static void
+logFittedSplines(spline_list_type const curve_list_splines) {
+
+    unsigned int splineSeq;
+
+    LOG("\nFitted splines are:\n");
+    for (splineSeq = 0;
+         splineSeq < SPLINE_LIST_LENGTH(curve_list_splines);
+         ++splineSeq) {
+        LOG1("  %u: ", splineSeq);
+        print_spline(log_file,
+                     SPLINE_LIST_ELT(curve_list_splines, splineSeq));
+    }
+}
+
+
+
+static void
+fitCurveList(curve_list_type     const curveList,
+             fitting_opts_type * const fittingOptsP,
+             distance_map_type * const dist,
+             pixel               const color,
+             spline_list_type *  const splineListP,
+             at_exception_type * const exception) {
+/*----------------------------------------------------------------------------
+  Fit the list of curves CURVE_LIST to a list of splines, and return
+  it.  CURVE_LIST represents a single closed paths, e.g., either the
+  inside or outside outline of an `o'.
+-----------------------------------------------------------------------------*/
+    curve_type curve;
+    spline_list_type curveListSplines;
+
+    removeKnees(curveList);
+
+    if (dist != NULL)
+        computePointWeights(curveList, fittingOptsP, dist);
+    
+    /* We filter all the curves in 'curveList' at once; otherwise, we
+       would look at an unfiltered curve when computing tangents.
+    */
+    filterCurves(curveList, fittingOptsP);
+
+    /* Make the first point in the first curve also be the last point in
+       the last curve, so the fit to the whole curve list will begin and
+       end at the same point.  This may cause slight errors in computing
+       the tangents and t values, but it's worth it for the continuity.
+       Of course we don't want to do this if the two points are already
+       the same, as they are if the curve is cyclic.  (We don't append it
+       earlier, in `split_at_corners', because that confuses the
+       filtering.)  Finally, we can't append the point if the curve is
+       exactly three points long, because we aren't adding any more data,
+       and three points isn't enough to determine a spline.  Therefore,
+       the fitting will fail.
+    */
+    curve = CURVE_LIST_ELT(curveList, 0);
+    if (CURVE_CYCLIC(curve))
+        append_point(curve, CURVE_POINT(curve, 0));
+
+    /* Finally, fit each curve in the list to a list of splines.  */
+
+    fitCurves(curveList, color, fittingOptsP, &curveListSplines, exception);
+    if (!at_exception_got_fatal(exception)) {
+        if (log_file)
+            logFittedSplines(curveListSplines);
+        *splineListP = curveListSplines;
+    }
+}
+
+
+
+static void
+fitCurvesToSplines(curve_list_array_type    const curveArray,
+                   fitting_opts_type *      const fittingOptsP,
+                   distance_map_type *      const dist,
+                   unsigned short           const width,
+                   unsigned short           const height,
+                   at_exception_type *      const exception,
+                   at_progress_func               notifyProgress, 
+                   void *                   const progressData,
+                   at_testcancel_func             testCancel,
+                   void *                   const testcancelData,
+                   spline_list_array_type * const splineListArrayP) {
+    
+    unsigned splineListSeq;
+    bool cancelled;
+    spline_list_array_type splineListArray;
+
+    splineListArray = new_spline_list_array();
+    splineListArray.centerline          = fittingOptsP->centerline;
+    splineListArray.preserve_width      = fittingOptsP->preserve_width;
+    splineListArray.width_weight_factor = fittingOptsP->width_weight_factor;
+    splineListArray.backgroundSpec      = fittingOptsP->backgroundSpec;
+    splineListArray.background_color    = fittingOptsP->background_color;
+    /* Set dummy values. Real value is set in upper context. */
+    splineListArray.width  = width;
+    splineListArray.height = height;
+    
+    for (splineListSeq = 0, cancelled = false;
+         splineListSeq < CURVE_LIST_ARRAY_LENGTH(curveArray) &&
+             !at_exception_got_fatal(exception) && !cancelled;
+         ++splineListSeq) {
+
+        curve_list_type const curveList =
+            CURVE_LIST_ARRAY_ELT(curveArray, splineListSeq);
+      
+        spline_list_type curveSplineList;
+
+        if (notifyProgress)
+            notifyProgress((((float)splineListSeq)/
+                            ((float)CURVE_LIST_ARRAY_LENGTH(curveArray) *
+                             (float)3.0) + (float)0.333),
+                           progressData);
+        if (testCancel && testCancel(testcancelData))
+            cancelled = true;
+
+        LOG1("\nFitting curve list #%u:\n", splineListSeq);
+
+        fitCurveList(curveList, fittingOptsP, dist, curveList.color,
+                     &curveSplineList, exception);
+        if (!at_exception_got_fatal(exception))
+            append_spline_list(&splineListArray, curveSplineList);
+    }
+    *splineListArrayP = splineListArray;
+}
+
+
+
+void
+fit_outlines_to_splines(pixel_outline_list_type  const pixelOutlineList,
+                        fitting_opts_type *      const fittingOptsP,
+                        distance_map_type *      const dist,
+                        unsigned short           const width,
+                        unsigned short           const height,
+                        at_exception_type *      const exception,
+                        at_progress_func               notifyProgress, 
+                        void *                   const progressData,
+                        at_testcancel_func             testCancel,
+                        void *                   const testcancelData,
+                        spline_list_array_type * const splineListArrayP) {
+/*----------------------------------------------------------------------------
+   Transform a list of pixels in the outlines of the original character to
+   a list of spline lists fitted to those pixels.
+-----------------------------------------------------------------------------*/
+    curve_list_array_type const curveListArray =
+        split_at_corners(pixelOutlineList, fittingOptsP, exception);
+
+    fitCurvesToSplines(curveListArray, fittingOptsP, dist, width, height,
+                       exception, notifyProgress, progressData,
+                       testCancel, testcancelData, splineListArrayP);
+
+
+    free_curve_list_array(&curveListArray, notifyProgress, progressData);
+    
+    flush_log_output();
+}
+
+
+
+
+static void
+find_vectors(unsigned int       const test_index,
+             pixel_outline_type const outline,
+             vector_type *      const in,
+             vector_type *      const out,
+             unsigned int       const corner_surround) {
+/*----------------------------------------------------------------------------
+  Return the difference vectors coming in and going out of the outline
+  OUTLINE at the point whose index is TEST_INDEX.  In Phoenix,
+  Schneider looks at a single point on either side of the point we're
+  considering.  That works for him because his points are not touching.
+  But our points *are* touching, and so we have to look at
+  `corner_surround' points on either side, to get a better picture of
+  the outline's shape.
+-----------------------------------------------------------------------------*/
+    int i;
+    unsigned n_done;
+    pm_pixelcoord const candidate = O_COORDINATE(outline, test_index);
+
+    in->dx  = in->dy  = in->dz  = 0.0;
+    out->dx = out->dy = out->dz = 0.0;
+    
+    /* Add up the differences from p of the `corner_surround' points
+       before p.
+    */
+    for (i = O_PREV(outline, test_index), n_done = 0;
+         n_done < corner_surround;
+         i = O_PREV(outline, i), ++n_done)
+        *in = Vadd(*in, IPsubtract(O_COORDINATE(outline, i), candidate));
+    
+    /* And the points after p. */
+    for (i = O_NEXT (outline, test_index), n_done = 0;
+         n_done < corner_surround;
+         i = O_NEXT(outline, i), ++n_done)
+        *out = Vadd(*out, IPsubtract(O_COORDINATE(outline, i), candidate));
+}
+
+
+
+/* Remove adjacent points from the index list LIST.  We do this by first
+   sorting the list and then running through it.  Since these lists are
+   quite short, a straight selection sort (e.g., p.139 of the Art of
+   Computer Programming, vol.3) is good enough.  LAST_INDEX is the index
+   of the last pixel on the outline, i.e., the next one is the first
+   pixel. We need this for checking the adjacency of the last corner.
+
+   We need to do this because the adjacent corners turn into
+   two-pixel-long curves, which can only be fit by straight lines.  */
+
+static void
+remove_adjacent_corners (index_list_type *list, unsigned last_index,
+             bool remove_adj_corners,
+             at_exception_type * exception)
+             
+{
+  unsigned j;
+  unsigned last;
+  index_list_type new_list = new_index_list ();
+
+  for (j = INDEX_LIST_LENGTH (*list) - 1; j > 0; j--)
+    {
+      unsigned search;
+      unsigned temp;
+      /* Find maximal element below `j'.  */
+      unsigned max_index = j;
+
+      for (search = 0; search < j; search++)
+        if (GET_INDEX (*list, search) > GET_INDEX (*list, max_index))
+          max_index = search;
+
+      if (max_index != j)
+        {
+          temp = GET_INDEX (*list, j);
+          GET_INDEX (*list, j) = GET_INDEX (*list, max_index);
+          GET_INDEX (*list, max_index) = temp;
+      
+      /* xx -- really have to sort?  */
+      LOG ("needed exchange");
+      at_exception_warning(exception, "needed exchange");
+        }
+    }
+
+  /* The list is sorted.  Now look for adjacent entries.  Each time
+     through the loop we insert the current entry and, if appropriate,
+     the next entry.  */
+  for (j = 0; j < INDEX_LIST_LENGTH (*list) - 1; j++)
+    {
+      unsigned current = GET_INDEX (*list, j);
+      unsigned next = GET_INDEX (*list, j + 1);
+
+      /* We should never have inserted the same element twice.  */
+      /* assert (current != next); */
+
+      if ((remove_adj_corners) && ((next == current + 1) || (next == current)))
+        j++;
+
+      append_index (&new_list, current);
+    }
+
+  /* Don't append the last element if it is 1) adjacent to the previous
+     one; or 2) adjacent to the very first one.  */
+  last = GET_LAST_INDEX (*list);
+  if (INDEX_LIST_LENGTH (new_list) == 0
+      || !(last == GET_LAST_INDEX (new_list) + 1
+           || (last == last_index && GET_INDEX (*list, 0) == 0)))
+    append_index (&new_list, last);
+
+  free_index_list (list);
+  *list = new_list;
+}
+
+/* A ``knee'' is a point which forms a ``right angle'' with its
+   predecessor and successor.  See the documentation (the `Removing
+   knees' section) for an example and more details.
+
+   The argument CLOCKWISE tells us which direction we're moving.  (We
+   can't figure that information out from just the single segment with
+   which we are given to work.)
+
+   We should never find two consecutive knees.
+
+   Since the first and last points are corners (unless the curve is
+   cyclic), it doesn't make sense to remove those.  */
+
+/* This evaluates to true if the vector V is zero in one direction and
+   nonzero in the other.  */
+#define ONLY_ONE_ZERO(v)                                                \
+  (((v).dx == 0.0 && (v).dy != 0.0) || ((v).dy == 0.0 && (v).dx != 0.0))
+
+/* There are four possible cases for knees, one for each of the four
+   corners of a rectangle; and then the cases differ depending on which
+   direction we are going around the curve.  The tests are listed here
+   in the order of upper left, upper right, lower right, lower left.
+   Perhaps there is some simple pattern to the
+   clockwise/counterclockwise differences, but I don't see one.  */
+#define CLOCKWISE_KNEE(prev_delta, next_delta)                                                  \
+  ((prev_delta.dx == -1.0 && next_delta.dy == 1.0)                                              \
+   || (prev_delta.dy == 1.0 && next_delta.dx == 1.0)                                    \
+   || (prev_delta.dx == 1.0 && next_delta.dy == -1.0)                                   \
+   || (prev_delta.dy == -1.0 && next_delta.dx == -1.0))
+
+#define COUNTERCLOCKWISE_KNEE(prev_delta, next_delta)                                   \
+  ((prev_delta.dy == 1.0 && next_delta.dx == -1.0)                                              \
+   || (prev_delta.dx == 1.0 && next_delta.dy == 1.0)                                    \
+   || (prev_delta.dy == -1.0 && next_delta.dx == 1.0)                                   \
+   || (prev_delta.dx == -1.0 && next_delta.dy == -1.0))
+
+
+
+static void
+remove_knee_points(curve_type const curve,
+                   bool       const clockwise) {
+
+      unsigned const offset = (CURVE_CYCLIC(curve) == true) ? 0 : 1;
+      curve_type const trimmed_curve = copy_most_of_curve(curve);
+
+      pm_pixelcoord previous;
+      unsigned i;
+
+      if (!CURVE_CYCLIC(curve))
+          append_pixel(trimmed_curve,
+                       real_to_int_coord(CURVE_POINT(curve, 0)));
+
+      previous = real_to_int_coord(CURVE_POINT(curve,
+                                               CURVE_PREV(curve, offset)));
+
+      for (i = offset; i < CURVE_LENGTH (curve) - offset; ++i) {
+          pm_pixelcoord const current =
+              real_to_int_coord(CURVE_POINT(curve, i));
+          pm_pixelcoord const next =
+              real_to_int_coord(CURVE_POINT(curve, CURVE_NEXT(curve, i)));
+          vector_type const prev_delta = IPsubtract(previous, current);
+          vector_type const next_delta = IPsubtract(next, current);
+
+          if (ONLY_ONE_ZERO(prev_delta) && ONLY_ONE_ZERO(next_delta)
+              && ((clockwise && CLOCKWISE_KNEE(prev_delta, next_delta))
+                  || (!clockwise
+                      && COUNTERCLOCKWISE_KNEE(prev_delta, next_delta))))
+              LOG2(" (%d,%d)", current.col, current.row);
+          else {
+              previous = current;
+              append_pixel(trimmed_curve, current);
+          }
+      }
+
+      if (!CURVE_CYCLIC(curve))
+          append_pixel(trimmed_curve,
+                       real_to_int_coord(LAST_CURVE_POINT(curve)));
+
+      if (CURVE_LENGTH(trimmed_curve) == CURVE_LENGTH(curve))
+          LOG(" (none)");
+
+      LOG(".\n");
+
+      free_curve(curve);
+      *curve = *trimmed_curve;
+      free(trimmed_curve);      /* free_curve? --- Masatake */
+}
+
+
+
+/* Smooth the curve by adding in neighboring points.  Do this
+   `filter_iterations' times.  But don't change the corners.  */
+
+static void
+filter (curve_type curve, fitting_opts_type *fitting_opts)
+{
+  unsigned iteration, this_point;
+  unsigned offset = (CURVE_CYCLIC (curve) == true) ? 0 : 1;
+  float_coord prev_new_point;
+
+  /* We must have at least three points---the previous one, the current
+     one, and the next one.  But if we don't have at least five, we will
+     probably collapse the curve down onto a single point, which means
+     we won't be able to fit it with a spline.  */
+  if (CURVE_LENGTH (curve) < 5)
+    {
+      LOG1 ("Length is %u, not enough to filter.\n", CURVE_LENGTH (curve));
+      return;
+    }
+
+  prev_new_point.x = FLT_MAX;
+  prev_new_point.y = FLT_MAX;
+  prev_new_point.z = FLT_MAX;
+
+  for (iteration = 0; iteration < fitting_opts->filter_iterations;
+   iteration++)
+    {
+      curve_type newcurve = copy_most_of_curve (curve);
+      bool collapsed = false;
+
+      /* Keep the first point on the curve.  */
+      if (offset)
+        append_point (newcurve, CURVE_POINT (curve, 0));
+
+      for (this_point = offset; this_point < CURVE_LENGTH (curve) - offset;
+           this_point++)
+        {
+          vector_type in, out, sum;
+          float_coord new_point;
+
+          /* Calculate the vectors in and out, computed by looking at n points
+             on either side of this_point. Experimental it was found that 2 is
+             optimal. */
+
+          signed int prev, prevprev; /* have to be signed */
+          unsigned int next, nextnext;
+          float_coord candidate = CURVE_POINT (curve, this_point);
+
+          prev = CURVE_PREV (curve, this_point);
+          prevprev = CURVE_PREV (curve, prev);
+          next = CURVE_NEXT (curve, this_point);
+          nextnext = CURVE_NEXT (curve, next);
+
+          /* Add up the differences from p of the `surround' points
+             before p.  */
+          in.dx = in.dy = in.dz = 0.0;
+
+          in = Vadd (in, Psubtract (CURVE_POINT (curve, prev), candidate));
+          if (prevprev >= 0)
+              in = Vadd (in, Psubtract (CURVE_POINT (curve, prevprev), candidate));
+
+          /* And the points after p.  Don't use more points after p than we
+             ended up with before it.  */
+          out.dx = out.dy = out.dz = 0.0;
+
+          out = Vadd (out, Psubtract (CURVE_POINT (curve, next), candidate));
+          if (nextnext < CURVE_LENGTH (curve))
+              out = Vadd (out, Psubtract (CURVE_POINT (curve, nextnext), candidate));
+
+          /* Start with the old point.  */
+          new_point = candidate;
+          sum = Vadd (in, out);
+          /* We added 2*n+2 points, so we have to divide the sum by 2*n+2 */
+          new_point.x += sum.dx / 6;
+          new_point.y += sum.dy / 6;
+          new_point.z += sum.dz / 6;
+          if (fabs (prev_new_point.x - new_point.x) < 0.3
+              && fabs (prev_new_point.y - new_point.y) < 0.3
+              && fabs (prev_new_point.z - new_point.z) < 0.3)
+            {
+              collapsed = true;
+              break;
+            }
+
+
+          /* Put the newly computed point into a separate curve, so it
+             doesn't affect future computation (on this iteration).  */
+          append_point (newcurve, prev_new_point = new_point);
+        }
+
+      if (collapsed)
+    free_curve (newcurve);
+      else
+    {
+          /* Just as with the first point, we have to keep the last point.  */
+          if (offset)
+        append_point (newcurve, LAST_CURVE_POINT (curve));
+      
+          /* Set the original curve to the newly filtered one, and go again.  */
+          free_curve (curve);
+          *curve = *newcurve;
+    }
+      free (newcurve);
+    }
+
+  log_curve (curve, false);
+}
+
+
+
+/* Find reasonable values for t for each point on CURVE.  The method is
+   called chord-length parameterization, which is described in Plass &
+   Stone.  The basic idea is just to use the distance from one point to
+   the next as the t value, normalized to produce values that increase
+   from zero for the first point to one for the last point.  */
+
+static void
+set_initial_parameter_values (curve_type curve)
+{
+  unsigned p;
+
+  LOG ("\nAssigning initial t values:\n  ");
+
+  CURVE_T (curve, 0) = 0.0;
+
+  for (p = 1; p < CURVE_LENGTH (curve); p++)
+    {
+      float_coord point = CURVE_POINT (curve, p),
+                           previous_p = CURVE_POINT (curve, p - 1);
+      float d = distance (point, previous_p);
+      CURVE_T (curve, p) = CURVE_T (curve, p - 1) + d;
+    }
+
+  assert (LAST_CURVE_T (curve) != 0.0);
+
+  for (p = 1; p < CURVE_LENGTH (curve); p++)
+    CURVE_T (curve, p) = CURVE_T (curve, p) / LAST_CURVE_T (curve);
+
+  log_entire_curve (curve);
+}
+
+/* Find an approximation to the tangent to an endpoint of CURVE (to the
+   first point if TO_START_POINT is true, else the last).  If
+   CROSS_CURVE is true, consider points on the adjacent curve to CURVE.
+
+   It is important to compute an accurate approximation, because the
+   control points that we eventually decide upon to fit the curve will
+   be placed on the half-lines defined by the tangents and
+   endpoints...and we never recompute the tangent after this.  */
+
+static void
+find_tangent (curve_type curve, bool to_start_point, bool cross_curve,
+  unsigned tangent_surround)
+{
+  vector_type tangent;
+  vector_type **curve_tangent = (to_start_point == true) ? &(CURVE_START_TANGENT (curve))
+                                               : &(CURVE_END_TANGENT (curve));
+  unsigned n_points = 0;
+
+  LOG1 ("  tangent to %s: ", (to_start_point == true) ? "start" : "end");
+
+  if (*curve_tangent == NULL)
+    {
+        MALLOCVAR_NOFAIL(*curve_tangent);
+      do
+        {
+          tangent = find_half_tangent (curve, to_start_point, &n_points,
+            tangent_surround);
+
+          if ((cross_curve == true) || (CURVE_CYCLIC (curve) == true))
+            {
+              curve_type adjacent_curve
+                = (to_start_point == true) ? PREVIOUS_CURVE (curve) : NEXT_CURVE (curve);
+              vector_type tangent2
+                = (to_start_point == false) ? find_half_tangent (adjacent_curve, true, &n_points,
+                tangent_surround) : find_half_tangent (adjacent_curve, true, &n_points,
+                tangent_surround);
+
+              LOG3 ("(adjacent curve half tangent (%.3f,%.3f,%.3f)) ",
+                tangent2.dx, tangent2.dy, tangent2.dz);
+              tangent = Vadd (tangent, tangent2);
+            }
+          tangent_surround--;
+
+        }
+      while (tangent.dx == 0.0 && tangent.dy == 0.0);
+
+      assert (n_points > 0);
+      **curve_tangent = Vmult_scalar (tangent, (float)(1.0 / n_points));
+      if ((CURVE_CYCLIC (curve) == true) && CURVE_START_TANGENT (curve))
+          *CURVE_START_TANGENT (curve) = **curve_tangent;
+      if  ((CURVE_CYCLIC (curve) == true) && CURVE_END_TANGENT (curve))
+          *CURVE_END_TANGENT (curve) = **curve_tangent;
+    }
+  else
+    LOG ("(already computed) ");
+
+  LOG3 ("(%.3f,%.3f,%.3f).\n", (*curve_tangent)->dx, (*curve_tangent)->dy, (*curve_tangent)->dz);
+}
+
+/* Find the change in y and change in x for `tangent_surround' (a global)
+   points along CURVE.  Increment N_POINTS by the number of points we
+   actually look at.  */
+
+static vector_type
+find_half_tangent (curve_type c, bool to_start_point, unsigned *n_points,
+  unsigned tangent_surround)
+{
+  unsigned p;
+  int factor = to_start_point ? 1 : -1;
+  unsigned tangent_index = to_start_point ? 0 : c->length - 1;
+  float_coord tangent_point = CURVE_POINT (c, tangent_index);
+  vector_type tangent = { 0.0, 0.0 };
+  unsigned int surround;
+
+  if ((surround = CURVE_LENGTH (c) / 2) > tangent_surround)
+    surround = tangent_surround;
+
+  for (p = 1; p <= surround; p++)
+    {
+      int this_index = p * factor + tangent_index;
+      float_coord this_point;
+
+      if (this_index < 0 || this_index >= (int) c->length)
+        break;
+
+      this_point = CURVE_POINT (c, p * factor + tangent_index);
+
+      /* Perhaps we should weight the tangent from `this_point' by some
+         factor dependent on the distance from the tangent point.  */
+      tangent = Vadd (tangent,
+                      Vmult_scalar (Psubtract (this_point, tangent_point),
+                                    (float) factor));
+      (*n_points)++;
+    }
+
+  return tangent;
+}
+
+/* When this routine is called, we have computed a spline representation
+   for the digitized curve.  The question is, how good is it?  If the
+   fit is very good indeed, we might have an error of zero on each
+   point, and then WORST_POINT becomes irrelevant.  But normally, we
+   return the error at the worst point, and the index of that point in
+   WORST_POINT.  The error computation itself is the Euclidean distance
+   from the original curve CURVE to the fitted spline SPLINE.  */
+
+static float
+find_error (curve_type curve, spline_type spline, unsigned *worst_point,
+        at_exception_type * exception)
+{
+  unsigned this_point;
+  float total_error = 0.0;
+  float worst_error = FLT_MIN;
+
+  *worst_point = CURVE_LENGTH (curve) + 1;   /* A sentinel value.  */
+
+  for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
+    {
+      float_coord curve_point = CURVE_POINT (curve, this_point);
+      float t = CURVE_T (curve, this_point);
+      float_coord spline_point = evaluate_spline (spline, t);
+      float this_error = distance (curve_point, spline_point);
+      if (this_error >= worst_error)
+        {
+         *worst_point = this_point;
+          worst_error = this_error;
+        }
+      total_error += this_error;
+    }
+
+  if (*worst_point == CURVE_LENGTH (curve) + 1)
+    { /* Didn't have any ``worst point''; the error should be zero.  */
+      if (epsilon_equal (total_error, 0.0))
+        LOG ("  Every point fit perfectly.\n");
+      else
+    {
+      LOG("No worst point found; something is wrong");
+      at_exception_warning(exception, "No worst point found; something is wrong");
+    }
+    }
+  else
+    {
+      if (epsilon_equal (total_error, 0.0))
+        LOG ("  Every point fit perfectly.\n");
+      else
+        {
+          LOG5 ("  Worst error (at (%.3f,%.3f,%.3f), point #%u) was %.3f.\n",
+              CURVE_POINT (curve, *worst_point).x,
+              CURVE_POINT (curve, *worst_point).y,
+              CURVE_POINT (curve, *worst_point).z, *worst_point, worst_error);
+          LOG1 ("  Total error was %.3f.\n", total_error);
+          LOG2 ("  Average error (over %u points) was %.3f.\n",
+              CURVE_LENGTH (curve), total_error / CURVE_LENGTH (curve));
+        }
+    }
+
+  return worst_error;
+}
+
+
+/* Lists of array indices (well, that is what we use it for).  */
+
+static index_list_type
+new_index_list (void)
+{
+  index_list_type index_list;
+
+  index_list.data = NULL;
+  INDEX_LIST_LENGTH (index_list) = 0;
+
+  return index_list;
+}
+
+static void
+free_index_list (index_list_type *index_list)
+{
+  if (INDEX_LIST_LENGTH (*index_list) > 0)
+    {
+      free (index_list->data);
+      index_list->data = NULL;
+      INDEX_LIST_LENGTH (*index_list) = 0;
+    }
+}
+
+static void
+append_index (index_list_type *list, unsigned new_index)
+{
+  INDEX_LIST_LENGTH (*list)++;
+  REALLOCARRAY_NOFAIL(list->data, INDEX_LIST_LENGTH(*list));
+  list->data[INDEX_LIST_LENGTH (*list) - 1] = new_index;
+}
+
+
+/* Return the Euclidean distance between P1 and P2.  */
+
+static float
+distance (float_coord p1, float_coord p2)
+{
+  float x = p1.x - p2.x, y = p1.y - p2.y, z = p1.z - p2.z;
+  return (float) sqrt (SQR(x) + SQR(y) + SQR(z));
+}
diff --git a/converter/other/pamtosvg/fit.h b/converter/other/pamtosvg/fit.h
new file mode 100644
index 00000000..529da5c7
--- /dev/null
+++ b/converter/other/pamtosvg/fit.h
@@ -0,0 +1,34 @@
+/* fit.h: convert the pixel representation to splines. */
+
+#ifndef FIT_H_INCLUDED
+#define FIT_H_INCLUDED
+
+#include "autotrace.h"
+#include "image-proc.h"
+#include "pxl-outline.h"
+#include "spline.h"
+#include "exception.h"
+
+/* See fit.c for descriptions of these variables, all of which can be
+   set using options. 
+*/
+typedef at_fitting_opts_type fitting_opts_type;
+
+void
+fit_outlines_to_splines(pixel_outline_list_type  const pixelOutlineList,
+                        fitting_opts_type *      const fittingOptsP,
+                        distance_map_type *      const dist,
+                        unsigned short           const width,
+                        unsigned short           const height,
+                        at_exception_type *      const exception,
+                        at_progress_func               notifyProgress, 
+                        void *                   const progressData,
+                        at_testcancel_func             testCancel,
+                        void *                   const testcancelData,
+                        spline_list_array_type * const splineListArrayP);
+
+/* Get a new set of fitting options */
+fitting_opts_type
+new_fitting_opts(void);
+
+#endif
diff --git a/converter/other/pamtosvg/image-header.h b/converter/other/pamtosvg/image-header.h
new file mode 100644
index 00000000..adcf4771
--- /dev/null
+++ b/converter/other/pamtosvg/image-header.h
@@ -0,0 +1,19 @@
+/* image-header.h: declarations for a generic image header. */
+
+#ifndef IMAGE_HEADER_H
+#define IMAGE_HEADER_H
+
+
+/* The important general information about the image data.
+   See `get_{img,pbm}_header' for the full details of the headers for
+   the particular formats.  */
+typedef struct
+{
+  unsigned short hres, vres;	/* In pixels per inch.  */
+  unsigned short width, height;	/* In bits.  */
+  unsigned short depth;		/* Perhaps the depth?  */
+  unsigned format;		/* (for pbm) Whether packed or not.  */
+} image_header_type;
+
+#endif /* not IMAGE_HEADER_H */
+
diff --git a/converter/other/pamtosvg/image-proc.c b/converter/other/pamtosvg/image-proc.c
new file mode 100644
index 00000000..287e6384
--- /dev/null
+++ b/converter/other/pamtosvg/image-proc.c
@@ -0,0 +1,516 @@
+/* image-proc.c: image processing routines */
+
+#include <assert.h>
+#include <math.h>
+#include <string.h>
+
+#include "mallocvar.h"
+
+#include "message.h"
+
+#include "image-proc.h"
+
+#define BLACK 0
+#define WHITE 0xff
+#ifndef M_SQRT2
+#define M_SQRT2 1.41421356237
+#endif
+
+#if 0
+struct etyp { int t00, t11, t01, t01s; };
+
+
+static bool get_edge(bitmap_type, int y, int x, struct etyp *t);
+static void check(int v1, int v2, int v3, struct etyp *t);
+#endif
+
+
+/* Allocate storage for a new distance map with the same dimensions
+   as BITMAP and initialize it so that pixels in BITMAP with value
+   TARGET_VALUE are at distance zero and all other pixels are at
+   distance infinity.  Then compute the gray-weighted distance from
+   every non-target point to the nearest target point. */
+
+distance_map_type
+new_distance_map(bitmap_type bitmap, unsigned char target_value, bool padded, at_exception_type * exp)
+{
+    signed x, y;
+    float d, min;
+    distance_map_type dist;
+    unsigned char *b = bitmap.bitmap;
+    unsigned w = bitmap.width;
+    unsigned h = bitmap.height;
+    unsigned spp = bitmap.np;
+
+    dist.height = h; dist.width = w;
+    MALLOCARRAY(dist.d, h);
+    MALLOCARRAY(dist.weight, h);
+    if (dist.d == NULL || dist.weight == NULL)
+        pm_error("Unable to get memory for distance map");
+    for (y = 0; y < (signed) h; y++)
+    {
+        MALLOCARRAY(dist.d[y], w);
+        if (dist.d[y] == NULL)
+            pm_error("Unable to get memory for distance map");
+        bzero(dist.d[y], w * sizeof(float));
+        
+        MALLOCARRAY(dist.weight[y], w);
+        if (dist.weight[y] == NULL)
+            pm_error("Unable to get memory for distance map");
+    }
+
+    if (spp == 3)
+    {
+      for (y = 0; y < (signed) h; y++)
+      {
+        for (x = 0; x < (signed) w; x++, b += spp)
+        {
+          int gray; float fgray;
+          gray = (int)LUMINANCE(b[0], b[1], b[2]);
+          dist.d[y][x] = (gray == target_value ? 0.0F : 1.0e10F);
+          fgray = gray * 0.0039215686F;  /* = gray / 255.0F */
+          dist.weight[y][x] = 1.0F - fgray;
+/*        dist.weight[y][x] = 1.0F - (fgray * fgray);*/
+/*        dist.weight[y][x] = (fgray < 0.5F ? 1.0F - fgray : -2.0F * fgray * (fgray - 1.0F));*/
+	    }
+      }
+    }
+    else
+    {
+      for (y = 0; y < (signed) h; y++)
+      {
+        for (x = 0; x < (signed) w; x++, b += spp)
+        {
+          int gray; float fgray;
+          gray = b[0];
+          dist.d[y][x] = (gray == target_value ? 0.0F : 1.0e10F);
+          fgray = gray * 0.0039215686F;  /* = gray / 255.0F */
+          dist.weight[y][x] = 1.0F - fgray;
+/*        dist.weight[y][x] = 1.0F - (fgray * fgray);*/
+/*        dist.weight[y][x] = (fgray < 0.5F ? 1.0F - fgray : -2.0F * fgray * (fgray - 1.0F)); */
+        }
+      }
+    }
+
+    /* If the image is padded then border points are all at most
+       one unit away from the nearest target point. */
+    if (padded)
+    {
+        for (y = 0; y < (signed) h; y++)
+        {
+            if (dist.d[y][0] > dist.weight[y][0])
+                dist.d[y][0] = dist.weight[y][0];
+            if (dist.d[y][w - 1] > dist.weight[y][w - 1])
+                dist.d[y][w - 1] = dist.weight[y][w - 1];
+        }
+        for (x = 0; x < (signed) w; x++)
+        {
+            if (dist.d[0][x] > dist.weight[0][x])
+                dist.d[0][x] = dist.weight[0][x];
+            if (dist.d[h - 1][x] > dist.weight[h - 1][x])
+                dist.d[h - 1][x] = dist.weight[h - 1][x];
+        }
+    }
+
+    /* Scan the image from left to right, top to bottom.
+       Examine the already-visited neighbors of each point (those
+       situated above or to the left of it).  Each neighbor knows
+       the distance to its nearest target point; add to this distance
+       the distance from the central point to the neighbor (either
+       sqrt(2) or one) multiplied by the central point's weight
+       (derived from its gray level).  Replace the distance already
+       stored at the central point if the new distance is smaller. */
+    for (y = 1; y < (signed) h; y++)
+    {
+        for (x = 1; x < (signed) w; x++)
+        {
+            if (dist.d[y][x] == 0.0F) continue;
+
+            min = dist.d[y][x];
+
+            /* upper-left neighbor */
+            d = dist.d[y - 1][x - 1] + (float) M_SQRT2 * dist.weight[y][x];
+            if (d < min) min = dist.d[y][x] = d;
+
+            /* upper neighbor */
+            d = dist.d[y - 1][x] + dist.weight[y][x];
+            if (d < min) min = dist.d[y][x] = d;
+
+            /* left neighbor */
+            d = dist.d[y][x - 1] + dist.weight[y][x];
+            if (d < min) min = dist.d[y][x] = d;
+
+            /* upper-right neighbor (except at the last column) */
+            if (x + 1 < (signed) w)
+            {
+                d = dist.d[y - 1][x + 1] + (float) M_SQRT2 * dist.weight[y][x];
+                if (d < min) min = dist.d[y][x] = d;
+            }
+        }
+    }
+
+    /* Same as above, but now scanning right to left, bottom to top. */
+    for (y = h - 2; y >= 0; y--)
+    {
+        for (x = w - 2; x >= 0; x--)
+        {
+            min = dist.d[y][x];
+
+            /* lower-right neighbor */
+            d = dist.d[y + 1][x + 1] + (float) M_SQRT2 * dist.weight[y][x];
+	        if (d < min) min = dist.d[y][x] = d;
+
+            /* lower neighbor */
+            d = dist.d[y + 1][x] + dist.weight[y][x];
+	        if (d < min) min = dist.d[y][x] = d;
+
+            /* right neighbor */
+            d = dist.d[y][x + 1] + dist.weight[y][x];
+	        if (d < min) min = dist.d[y][x] = d;
+
+            /* lower-left neighbor (except at the first column) */
+            if (x - 1 >= 0)
+            {
+                d = dist.d[y + 1][x - 1] + (float) M_SQRT2 * dist.weight[y][x];
+                if (d < min) min = dist.d[y][x] = d;
+            }
+        }
+    }
+    return dist;
+}
+
+
+/* Free the dynamically-allocated storage associated with a distance map. */
+
+void
+free_distance_map(distance_map_type *dist)
+{
+    unsigned y, h;
+
+    if (!dist) return;
+
+    h = dist->height;
+
+    if (dist->d != NULL)
+    {
+	for (y = 0; y < h; y++)
+	    free(dist->d[y]);
+        free(dist->d);
+    }
+    if (dist->weight != NULL)
+    {
+	for (y = 0; y < h; y++)
+	    free(dist->weight[y]);
+        free(dist->weight);
+    }
+}
+
+
+#if 0
+void
+medial_axis(bitmap_type *bitmap, distance_map_type *dist,
+            bool bgSpec, pixel bg_color)
+{
+    unsigned x, y, test;
+    unsigned w, h;
+    unsigned char *b;
+    float **d, f;
+    pixel bg;
+
+    assert(bitmap != NULL);
+
+    assert(BITMAP_PLANES(*bitmap) == 1);
+
+    b = BITMAP_BITS(*bitmap);
+    assert(b != NULL);
+    assert(dist != NULL);
+    d = dist->d;
+    assert(d != NULL);
+
+    h = BITMAP_HEIGHT(*dist);
+    w = BITMAP_WIDTH(*dist);
+    assert(BITMAP_WIDTH(*bitmap) == w && BITMAP_HEIGHT(*bitmap) == h);
+
+    if (bgSpec)
+        bg = bg_color;
+    else 
+        PPM_ASSIGN(bg, 255, 255, 255);
+
+    f = d[0][0] + 0.5;
+    test = (f < d[1][0]) + (f < d[1][1]) + (f < d[0][1]);
+    if (test > 1) b[0] = PPM_GETR(bg);
+
+    f = d[0][w-1] + 0.5;
+    test = (f < d[1][w-1]) + (f < d[1][w-2]) + (f < d[0][w-2]);
+    if (test > 1) b[w-1] = PPM_GETR(bg);
+
+    for (x = 1; x < w - 1; x++)
+    {
+	    f = d[0][x] + 0.5;
+	    test = (f < d[0][x-1]) + (f < d[0][x+1]) + (f < d[1][x-1])
+	        + (f < d[1][x]) + (f < d[1][x+1]);
+	    if (test > 1) b[x] = PPM_GETR(bg);
+    }
+    b += w;
+
+    for (y = 1; y < h - 1; y++)
+    {
+	    f = d[y][0] + 0.5;
+	    test = (f < d[y-1][0]) + (f < d[y-1][1]) + (f < d[y][1])
+	        + (f < d[y+1][0]) + (f < d[y+1][1]);
+	    if (test > 1) b[0] = PPM_GETR(bg);
+
+	    for (x = 1; x < w - 1; x++)
+		{
+	        f = d[y][x] + 0.5;
+	        test = (f < d[y-1][x-1]) + (f < d[y-1][x]) + (f < d[y-1][x+1])
+		    + (f < d[y][x-1]) + (f < d[y][x+1])
+		    + (f < d[y+1][x-1]) + (f < d[y+1][x]) + (f < d[y+1][x+1]);
+	        if (test > 1) b[x] = PPM_GETR(bg)
+		}
+
+	    f = d[y][w-1] + 0.5;
+	    test = (f < d[y-1][w-1]) + (f < d[y-1][w-2]) + (f < d[y][w-2])
+	        + (f < d[y+1][w-1]) + (f < d[y+1][w-2]);
+	    if (test > 1)
+	        b[w-1] = PPM_GETR(bg);
+
+        b += w;
+    }
+
+    for (x = 1; x < w - 1; x++)
+    {
+	    f = d[h-1][x] + 0.5;
+	    test = (f < d[h-1][x-1]) + (f < d[h-1][x+1])
+	        + (f < d[h-2][x-1]) + (f < d[h-2][x]) + (f < d[h-2][x+1]);
+	    if (test > 1) b[x] = PPM_GETR(bg);
+    }
+
+    f = d[h-1][0] + 0.5;
+    test = (f < d[h-2][0]) + (f < d[h-2][1]) + (f < d[h-1][1]);
+    if (test > 1) b[0] = PPM_GETR(bg);
+
+    f = d[h-1][w-1] + 0.5;
+    test = (f < d[h-2][w-1]) + (f < d[h-2][w-2]) + (f < d[h-1][w-2]);
+    if (test > 1) b[w-1] = PPM_GETR(bg);
+}
+#endif
+
+
+/* Binarize a grayscale or color image. */
+
+void
+binarize(bitmap_type *bitmap)
+{
+    unsigned i, npixels, spp;
+    unsigned char *b;
+
+    assert(bitmap != NULL);
+    assert(bitmap->bitmap != NULL);
+
+    b = bitmap->bitmap;
+    spp = bitmap->np;
+    npixels = bitmap->width * bitmap->height;
+
+    if (spp == 1)
+    {
+	    for (i = 0; i < npixels; i++)
+	        b[i] = (b[i] > GRAY_THRESHOLD ? WHITE : BLACK);
+    }
+    else if (spp == 3)
+    {
+	    unsigned char *rgb = b;
+	    for (i = 0; i < npixels; i++, rgb += 3)
+		{
+	        b[i] = (LUMINANCE(rgb[0], rgb[1], rgb[2]) > GRAY_THRESHOLD
+		        ? WHITE : BLACK);
+		}
+	    REALLOCARRAY_NOFAIL(bitmap->bitmap, npixels);
+	    bitmap->np = 1;
+    }
+    else
+    {
+	    WARNING1("binarize: %u-plane images are not supported", spp);
+    }
+}
+
+
+#if 0
+/* Thin a binary image, replacing the original image with the thinned one. */
+
+bitmap_type
+ip_thin(bitmap_type input_b)
+{
+    unsigned y, x, i;
+    bool k, again;
+    struct etyp t;
+    unsigned w = BITMAP_WIDTH(input_b);
+    unsigned h = BITMAP_HEIGHT(input_b);
+    size_t num_bytes = w * h;
+    bitmap_type b = input_b;
+
+    if (BITMAP_PLANES(input_b) != 1)
+    {
+	    FATAL1("thin: single-plane image required; "
+	        "%u-plane images cannot be thinned", BITMAP_PLANES(input_b));
+	    return b;
+    }
+
+    /* Process and return a copy of the input image. */
+    MALLOCARRAY(b.bitmap, num_bytes);
+    if (b.bitmap == NULL)
+        pm_error("Unable to get memory for bitmap");
+    memcpy(b.bitmap, input_b.bitmap, num_bytes);
+
+    /* Set background pixels to zero, foreground pixels to one. */
+    for (i = 0; i < num_bytes; i++)
+	b.bitmap[i] = (b.bitmap[i] == BLACK ? 1 : 0);
+
+    again = true;
+    while (again)
+    {
+	again = false;
+
+	for (y = 1; y < h - 1; y++)
+	{
+	    for (x = 1; x < w - 1; x++)
+	    {
+		    /* During processing, pixels are used to store edge
+		       type codes, so we can't just test for WHITE or BLACK. */
+		    if (*BITMAP_PIXEL(b, y, x) == 0) continue;
+
+		    k = (!get_edge(b, y, x, &t)
+		        || (get_edge(b, y, x+1, &t) && *BITMAP_PIXEL(b, y-1, x)
+			    && *BITMAP_PIXEL(b, y+1, x))
+		        || (get_edge(b, y+1, x, &t) && *BITMAP_PIXEL(b, y, x-1)
+			    && *BITMAP_PIXEL(b, y, x+1))
+		        || (get_edge(b, y, x+1, &t) && get_edge(b, y+1, x+1, &t)
+			    && get_edge(b, y+1, x, &t)));
+		    if (k) continue;
+
+		    get_edge(b, y, x, &t);
+		    if (t.t01) *BITMAP_PIXEL(b, y, x) |= 4;
+		    *BITMAP_PIXEL(b, y, x) |= 2;
+		    again = true;
+	    }
+	}
+
+	for (y = 0; y < h; y++)
+	    for (x = 0; x < w; x++)
+		    if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
+
+	for (y = 1; y < h - 1; y++)
+	{
+	    for (x = 1; x < w - 1; x++)
+	    {
+		    if (*BITMAP_PIXEL(b, y, x) == 0) continue;
+
+		    k = (!get_edge(b, y, x, &t)
+		        || ((*BITMAP_PIXEL(b, y, x) & 04) == 0)
+		        || (get_edge(b, y+1, x, &t) && (*BITMAP_PIXEL(b, y, x-1))
+			    && *BITMAP_PIXEL(b, y, x+1))
+		        || (get_edge(b, y, x+1, &t) && *BITMAP_PIXEL(b, y-1, x)
+			    && *BITMAP_PIXEL(b, y+1, x))
+		        || (get_edge(b, y+1, x, &t) && get_edge(b, y, x+1, &t)
+			    && get_edge(b, y+1, x+1, &t)));
+		    if (k) continue;
+
+		    *BITMAP_PIXEL(b, y, x) |= 02;
+		    again = true;
+	    }
+	}
+
+	for (y = 0; y < h; y++)
+	{
+	    for (x = 0; x < w; x++)
+	    {
+		    if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
+		    else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
+	    }
+	}
+    }
+
+    /* Staircase removal; northward bias. */
+    for (y = 1; y < h - 1; y++)
+    {
+	    for (x = 1; x < w - 1; x++)
+		{
+	        if (*BITMAP_PIXEL(b, y, x) == 0) continue;
+
+	        k = !(*BITMAP_PIXEL(b, y-1, x)
+		        && ((*BITMAP_PIXEL(b, y, x+1) && !*BITMAP_PIXEL(b, y-1, x+1)
+		        && !*BITMAP_PIXEL(b, y+1, x-1)
+		        && (!*BITMAP_PIXEL(b, y, x-1) || !*BITMAP_PIXEL(b, y+1, x)))
+		        || (*BITMAP_PIXEL(b, y, x-1) && !*BITMAP_PIXEL(b, y-1, x-1)
+		        && !*BITMAP_PIXEL(b, y+1, x+1) &&
+		        (!*BITMAP_PIXEL(b, y, x+1) || !*BITMAP_PIXEL(b, y+1, x)))));
+	        if (k) continue;
+
+	        *BITMAP_PIXEL(b, y, x) |= 02;
+		}
+    }
+    for (y = 0; y < h; y++)
+    {
+	    for (x = 0; x < w; x++)
+		{
+	        if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
+	        else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
+		}
+    }
+
+    /* Southward bias */
+    for (y = 1; y < h - 1; y++)
+    {
+	    for (x = 1; x < w - 1; x++)
+		{
+	        if (*BITMAP_PIXEL(b, y, x) == 0) continue;
+
+	        k = !(*BITMAP_PIXEL(b, y+1, x)
+		    && ((*BITMAP_PIXEL(b, y, x+1) && !*BITMAP_PIXEL(b, y+1, x+1)
+		    && !*BITMAP_PIXEL(b, y-1, x-1) && (!*BITMAP_PIXEL(b, y, x-1)
+		    || !*BITMAP_PIXEL(b, y-1, x))) || (*BITMAP_PIXEL(b, y, x-1)
+		    && !*BITMAP_PIXEL(b, y+1, x-1) && !*BITMAP_PIXEL(b, y-1, x+1)
+		    && (!*BITMAP_PIXEL(b, y, x+1) || !*BITMAP_PIXEL(b, y-1, x)) )));
+	        if (k) continue;
+
+	        *BITMAP_PIXEL(b, y, x) |= 02;
+		}
+    }
+    for (y = 0; y < h; y++)
+    {
+	    for (x = 0; x < w; x++)
+		{
+	        if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
+	        else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
+		}
+    }
+
+    /* Set background pixels to WHITE, foreground pixels to BLACK. */
+    for (i = 0; i < num_bytes; i++)
+	b.bitmap[i] = (b.bitmap[i] == 0 ? WHITE : BLACK);
+    return b;
+}
+
+
+bool get_edge(bitmap_type b, int y, int x, struct etyp *t)
+{
+    t->t00 = 0; t->t01 = 0; t->t01s = 0; t->t11 = 0;
+    check(*BITMAP_PIXEL(b, y - 1, x - 1), *BITMAP_PIXEL(b, y - 1, x),
+	*BITMAP_PIXEL(b, y - 1, x + 1), t);
+    check(*BITMAP_PIXEL(b, y - 1, x + 1), *BITMAP_PIXEL(b, y, x + 1),
+	*BITMAP_PIXEL(b, y + 1, x + 1), t);
+    check(*BITMAP_PIXEL(b, y + 1, x + 1), *BITMAP_PIXEL(b, y + 1, x),
+	*BITMAP_PIXEL(b, y + 1, x - 1), t);
+    check(*BITMAP_PIXEL(b, y + 1, x - 1), *BITMAP_PIXEL(b, y, x - 1),
+	*BITMAP_PIXEL(b, y - 1, x - 1), t);
+    return *BITMAP_PIXEL(b, y, x) && t->t00 && t->t11 && !t->t01s;
+}
+
+
+void check(int v1, int v2, int v3, struct etyp *t)
+{
+    if (!v2 && (!v1 || !v3)) t->t00 = 1;
+    if (v2 && (v1 || v3)) t->t11 = 1;
+    if ((!v1 && v2) || (!v2 && v3)) { t->t01s = t->t01; t->t01 = 1; }
+}
+#endif
diff --git a/converter/other/pamtosvg/image-proc.h b/converter/other/pamtosvg/image-proc.h
new file mode 100644
index 00000000..a5b86ec1
--- /dev/null
+++ b/converter/other/pamtosvg/image-proc.h
@@ -0,0 +1,42 @@
+/* image-proc.h: image processing routines */
+
+#ifndef IMAGE_PROC_H
+#define IMAGE_PROC_H
+
+#include "bitmap.h"
+#include "exception.h"
+
+
+/* Threshold for binarizing a monochrome image */
+#define GRAY_THRESHOLD 225
+
+/* RGB to grayscale */
+#define LUMINANCE(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11 + 0.5)
+
+
+typedef struct
+{
+  unsigned height, width;
+  float **weight;
+  float **d;
+} distance_map_type;
+
+
+/* Allocate and compute a new distance map. */
+extern distance_map_type new_distance_map(bitmap_type,
+    unsigned char target_value, bool padded,
+					  at_exception_type * exp);
+
+/* Free the dynamically-allocated storage associated with a distance map. */
+extern void free_distance_map(distance_map_type*);
+
+
+/* Binarize a grayscale or color image. */
+extern void binarize(bitmap_type*);
+
+
+/* Thin a binary image, replacing the original image with the thinned one. */
+extern bitmap_type ip_thin(bitmap_type);
+
+#endif /* not IMAGE_PROC_H */
+
diff --git a/converter/other/pamtosvg/logreport.c b/converter/other/pamtosvg/logreport.c
new file mode 100644
index 00000000..7d726584
--- /dev/null
+++ b/converter/other/pamtosvg/logreport.c
@@ -0,0 +1,17 @@
+/* logreport.c: showing information to the user. */
+
+#include "logreport.h"
+#include "message.h"
+
+/* Says whether to output detailed progress reports, i.e., all the data
+   on the fitting, as we run.  (-log)  */
+FILE *log_file = NULL;
+
+
+void
+flush_log_output (void)
+{
+  if (log_file)
+    fflush (log_file);
+}
+
diff --git a/converter/other/pamtosvg/logreport.h b/converter/other/pamtosvg/logreport.h
new file mode 100644
index 00000000..577da8df
--- /dev/null
+++ b/converter/other/pamtosvg/logreport.h
@@ -0,0 +1,28 @@
+/* logreport.h: status reporting routines. */
+
+#ifndef LOGREPORT_H
+#define LOGREPORT_H
+
+#include <stdio.h>
+
+/* The file we write information to.  */
+extern FILE *at_log_file;
+#define log_file at_log_file
+
+extern void flush_log_output (void);
+
+#define LOG(s)								\
+  do { if (log_file) fputs (s, log_file); } while (0)
+#define LOG1(s, e)							\
+  do { if (log_file) fprintf (log_file, s, e); } while (0)
+#define LOG2(s, e1, e2)							\
+  do { if (log_file) fprintf (log_file, s, e1, e2); } while (0)
+#define LOG3(s, e1, e2, e3)						\
+  do { if (log_file) fprintf (log_file, s, e1, e2, e3); } while (0)
+#define LOG4(s, e1, e2, e3, e4)						\
+  do { if (log_file) fprintf (log_file, s, e1, e2, e3, e4); } while (0)
+#define LOG5(s, e1, e2, e3, e4, e5)					\
+  do { if (log_file) fprintf (log_file, s, e1, e2, e3, e4, e5); } while (0)
+
+#endif /* not LOGREPORT_H */
+
diff --git a/converter/other/pamtosvg/message.h b/converter/other/pamtosvg/message.h
new file mode 100644
index 00000000..0a226206
--- /dev/null
+++ b/converter/other/pamtosvg/message.h
@@ -0,0 +1,47 @@
+/* message.h: extend the standard programming environment a little. */
+
+#ifndef MESSAGE_H
+#define MESSAGE_H
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "logreport.h"
+
+/* Define common sorts of messages.  */
+
+/* This should be called only after a system call fails.  */
+#define FATAL_PERROR(s) do { perror (s); exit (errno); } while (0)
+
+
+#define START_FATAL() do { fputs ("fatal: ", stderr); LOG("fatal: ")
+#define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
+
+#define FATAL(s)							\
+  START_FATAL (); fprintf (stderr, "%s", s); LOG (s); END_FATAL ()
+#define FATAL1(s, e1)							\
+  START_FATAL (); fprintf (stderr, s, e1); LOG1 (s, e1); END_FATAL ()
+#define FATAL2(s, e1, e2)						\
+  START_FATAL (); fprintf (stderr, s, e1, e2); LOG2 (s, e1, e2); END_FATAL ()
+#define FATAL3(s, e1, e2, e3)						\
+  START_FATAL (); fprintf (stderr, s, e1, e2, e3); LOG3 (s, e1, e2, e3); END_FATAL ()
+#define FATAL4(s, e1, e2, e3, e4)					\
+  START_FATAL (); fprintf (stderr, s, e1, e2, e3, e4); LOG4 (s, e1, e2, e3, e4); END_FATAL ()
+
+
+#define START_WARNING() do { fputs ("warning: ", stderr); LOG ("warning: ")
+#define END_WARNING() fputs (".\n", stderr); } while (0)
+
+#define WARNING(s)							\
+  START_WARNING (); fprintf (stderr, "%s", s); LOG (s); END_WARNING ()
+#define WARNING1(s, e1)							\
+  START_WARNING (); fprintf (stderr, s, e1); LOG1 (s, e1); END_WARNING ()
+#define WARNING2(s, e1, e2)						\
+  START_WARNING (); fprintf (stderr, s, e1, e2); LOG2 (s, e1, e2); END_WARNING ()
+#define WARNING3(s, e1, e2, e3)						\
+  START_WARNING (); fprintf (stderr, s, e1, e2, e3); LOG3 (s, e1, e2, e3); END_WARNING ()
+#define WARNING4(s, e1, e2, e3, e4)					\
+  START_WARNING (); fprintf (stderr, s, e1, e2, e3, e4); LOG4 (s, e1, e2, e3, e4); END_WARNING ()
+
+#endif /* not MESSAGE_H */
+
diff --git a/converter/other/pamtosvg/output-svg.c b/converter/other/pamtosvg/output-svg.c
new file mode 100644
index 00000000..53a8d4fd
--- /dev/null
+++ b/converter/other/pamtosvg/output-svg.c
@@ -0,0 +1,130 @@
+/* output-svg.h - output in SVG format
+
+   Copyright (C) 1999, 2000, 2001 Bernhard Herzog
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License
+   as published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+   USA. */
+
+#include "spline.h"
+#include "output-svg.h"
+
+
+
+static void
+outSplineList(FILE *           const fileP,
+              spline_list_type const splineList,
+              unsigned int     const height) {
+              
+    unsigned splineSeq;
+        
+    for (splineSeq = 0;
+         splineSeq < SPLINE_LIST_LENGTH(splineList);
+         ++splineSeq) {
+        
+        spline_type const spline = SPLINE_LIST_ELT(splineList, splineSeq);
+        
+        if (SPLINE_DEGREE(spline) == LINEARTYPE) {
+            fprintf(fileP, "L%g %g",
+                    END_POINT(spline).x, height - END_POINT(spline).y);
+        } else
+            fprintf(fileP, "C%g %g %g %g %g %g",
+                    CONTROL1(spline).x, height  - CONTROL1(spline).y,
+                    CONTROL2(spline).x, height  - CONTROL2(spline).y,
+                    END_POINT(spline).x, height - END_POINT(spline).y);
+    }
+}
+
+
+
+static void
+out_splines(FILE *                 const fileP,
+            spline_list_array_type const shape,
+            unsigned int           const height) {
+
+    unsigned listSeq;
+    pixel lastColor;
+    
+    PPM_ASSIGN(lastColor, 0, 0, 0);
+    
+    for (listSeq = 0;
+         listSeq < SPLINE_LIST_ARRAY_LENGTH(shape);
+         ++listSeq) {
+        
+        spline_list_type const splineList =
+            SPLINE_LIST_ARRAY_ELT(shape, listSeq);
+        spline_type const first = SPLINE_LIST_ELT(splineList, 0);
+
+        if (listSeq == 0 || !PPM_EQUAL(splineList.color, lastColor)) {
+            if (listSeq > 0) {
+                /* Close previous <path> element */
+                if (!(shape.centerline || splineList.open))
+                    fputs("z", fileP);
+                fputs("\"/>\n", fileP);
+            }
+            /* Open new <path> element */
+            fprintf(fileP, "<path style=\"%s:#%02x%02x%02x; %s:none;\" d=\"",
+                    (shape.centerline || splineList.open) ? "stroke" : "fill",
+                    PPM_GETR(splineList.color),
+                    PPM_GETG(splineList.color),
+                    PPM_GETB(splineList.color),
+                    (shape.centerline || splineList.open) ? "fill" : "stroke");
+        }
+        fprintf(fileP, "M%g %g",
+                START_POINT(first).x, height - START_POINT(first).y);
+
+        outSplineList(fileP, splineList, height);
+
+        lastColor = splineList.color;
+    }
+
+    if (SPLINE_LIST_ARRAY_LENGTH(shape) > 0) {
+        spline_list_type const lastSplineList =
+            SPLINE_LIST_ARRAY_ELT(shape, SPLINE_LIST_ARRAY_LENGTH(shape)-1);
+
+        if (!(shape.centerline || lastSplineList.open))
+            fputs("z", fileP);
+
+        /* Close last <path> element */
+        fputs("\"/>\n", fileP);
+    }
+}
+
+
+
+int
+output_svg_writer(FILE *                    const fileP,
+                  const char *              const name,
+                  int                       const llx,
+                  int                       const lly,
+                  int                       const urx,
+                  int                       const ury, 
+                  at_output_opts_type *     const opts,
+                  at_spline_list_array_type const shape,
+                  at_msg_func                     msg_func, 
+                  void *                    const msg_data) {
+
+    int const width  = urx - llx;
+    int const height = ury - lly;
+
+    fputs("<?xml version=\"1.0\" standalone=\"yes\"?>\n", fileP);
+
+    fprintf(fileP, "<svg width=\"%d\" height=\"%d\">\n", width, height);
+
+    out_splines(fileP, shape, height);
+
+    fputs("</svg>\n", fileP);
+    
+    return 0;
+}
diff --git a/converter/other/pamtosvg/output-svg.h b/converter/other/pamtosvg/output-svg.h
new file mode 100644
index 00000000..46fc8f8f
--- /dev/null
+++ b/converter/other/pamtosvg/output-svg.h
@@ -0,0 +1,37 @@
+/* output-svg.h - output in SVG format
+
+   Copyright (C) 1999, 2000, 2001 Bernhard Herzog
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License
+   as published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+   USA. */
+
+#ifndef OUTPUT_SVG_H
+#define OUTPUT_SVG_H
+
+int
+output_svg_writer(FILE *                    const file,
+                  const char *              const name,
+                  int                       const llx,
+                  int                       const lly,
+                  int                       const urx,
+                  int                       const ury, 
+                  at_output_opts_type *     const opts,
+                  at_spline_list_array_type const shape,
+                  at_msg_func                     msg_func, 
+                  void *                    const msg_data);
+
+
+#endif /* not OUTPUT_SVG_H */
+
diff --git a/converter/other/pamtosvg/pamtosvg.c b/converter/other/pamtosvg/pamtosvg.c
new file mode 100644
index 00000000..31597a4a
--- /dev/null
+++ b/converter/other/pamtosvg/pamtosvg.c
@@ -0,0 +1,395 @@
+/* main.c: main driver for autotrace -- convert bitmaps to splines. */
+
+#include <string.h>
+#include <assert.h>
+#include <math.h>
+
+#include "mallocvar.h"
+#include "nstring.h"
+#include "shhopt.h"
+#include "pam.h"
+
+#include "autotrace.h"
+#include "message.h"
+#include "logreport.h"
+#include "output-svg.h"
+#include "bitmap.h"
+
+#define dot_printer_max_column 50
+#define dot_printer_char '|'
+
+
+
+static void
+readImageToBitmap(FILE *            const ifP,
+                  at_bitmap_type ** const bitmapPP) {
+
+    at_bitmap_type * bitmapP;
+    struct pam pam;
+    tuple ** tuples;
+    uint row;
+    tuple * row255;
+
+    MALLOCVAR_NOFAIL(bitmapP);
+
+    tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
+
+    bitmapP->width  = pam.width;
+    bitmapP->height = pam.height;
+    bitmapP->np     = pam.depth;
+
+    MALLOCARRAY(bitmapP->bitmap, pam.width * pam.height * pam.depth);
+
+    row255 = pnm_allocpamrow(&pam);
+
+    for (row = 0; row < pam.height; ++row) {
+        unsigned int col;
+
+        pnm_scaletuplerow(&pam, row255, tuples[row], 255);
+        
+        for (col = 0; col < pam.width; ++col) {
+            unsigned int plane;
+
+            for (plane = 0; plane < pam.depth; ++plane) {
+                unsigned int const bitmapIndex = 
+                    (row * pam.width + col) * pam.depth + plane;
+                bitmapP->bitmap[bitmapIndex] = row255[col][plane];
+            }
+        }
+    }
+    pnm_freepamrow(row255);
+    pnm_freepamarray(tuples, &pam);
+    
+    *bitmapPP = bitmapP;
+}
+
+
+
+static void
+dotPrinter(float  const percentage,
+           void * const clientData) {
+
+    int * const currentP = (int *)clientData;
+    float const unit     = (float)1.0 / (float)(dot_printer_max_column) ;
+    int   const maximum  = (int)(percentage / unit);
+    
+    while (*currentP < maximum) {
+        fputc(dot_printer_char, stderr);
+        (*currentP)++;
+    }
+}
+
+
+
+static void
+exceptionHandler(const char * const msg,
+                 at_msg_type  const type,
+                 void *       const data) {
+
+    if (type == AT_MSG_FATAL)
+        pm_error("%s", msg);
+    else if (type == AT_MSG_WARNING)
+        pm_message("%s", msg);
+    else
+        exceptionHandler("Wrong type of msg", AT_MSG_FATAL, NULL);
+}
+
+
+
+struct cmdlineInfo {
+    const char * inputFileName;
+    float        align_threshold;
+    unsigned int backgroundSpec;
+    pixel        background_color;
+    unsigned int centerline;
+    float        corner_always_threshold;
+    unsigned int corner_surround;
+    float        corner_threshold;
+    unsigned int dpi;
+    float        error_threshold;
+    unsigned int filter_iterations;
+    float        line_reversion_threshold;
+    float        line_threshold;
+    unsigned int log;
+    unsigned int preserve_width;
+    unsigned int remove_adjacent_corners;
+    unsigned int tangent_surround;
+    unsigned int report_progress;
+    float        width_weight_factor;
+};
+
+
+static void 
+parseCommandLine(int argc, 
+                 char ** argv, 
+                 struct cmdlineInfo  * const cmdlineP) {
+/* --------------------------------------------------------------------------
+   Parse program command line described in Unix standard form by argc
+   and argv.  Return the information in the options as *cmdlineP.  
+
+   If command line is internally inconsistent (invalid options, etc.),
+   issue error message to stderr and abort program.
+
+   Note that the strings we return are stored in the storage that
+   was passed to us as the argv array.  We also trash *argv.
+--------------------------------------------------------------------------*/
+    optEntry *option_def;
+    /* Instructions to optParseOptions3 on how to parse our options. */
+    optStruct3 opt;
+
+    const char * background_colorOpt;
+  
+    unsigned int option_def_index;
+
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
+    option_def_index = 0;   /* incremented by OPTENT3 */
+    OPTENT3(0, "align-threshold",     OPT_FLOAT,
+            &cmdlineP->align_threshold,  NULL,                              0);
+    OPTENT3(0, "background-color",    OPT_STRING,
+            &background_colorOpt,        &cmdlineP->backgroundSpec,         0);
+    OPTENT3(0, "centerline",          OPT_FLAG,
+            NULL,                        &cmdlineP->centerline,             0);
+    OPTENT3(0, "corner-always-threshold", OPT_FLOAT, 
+            &cmdlineP->corner_always_threshold, NULL,                       0);
+    OPTENT3(0, "corner-surround",     OPT_UINT,
+            &cmdlineP->corner_surround,  NULL,                              0);
+    OPTENT3(0, "corner-threshold",    OPT_FLOAT,
+            &cmdlineP->corner_threshold, NULL,                              0);
+    OPTENT3(0, "dpi",                 OPT_UINT,
+            &cmdlineP->dpi,              NULL,                              0);
+    OPTENT3(0, "error-threshold",     OPT_FLOAT,
+            &cmdlineP->error_threshold,  NULL,                              0);
+    OPTENT3(0, "filter-iterations",   OPT_UINT,
+            &cmdlineP->filter_iterations, NULL,                             0);
+    OPTENT3(0, "line-reversion-threshold", OPT_FLOAT,
+            &cmdlineP->line_reversion_threshold, NULL,                    0);
+    OPTENT3(0, "line-threshold",      OPT_FLOAT,
+            &cmdlineP->line_threshold, NULL,                                0);
+    OPTENT3(0, "log",                 OPT_FLAG,
+            NULL,                         &cmdlineP->log,                   0);
+    OPTENT3(0, "preserve-width",      OPT_FLAG,
+            NULL,                         &cmdlineP->preserve_width,        0);
+    OPTENT3(0, "remove-adjacent-corners", OPT_UINT,
+            NULL,                       &cmdlineP->remove_adjacent_corners, 0);
+    OPTENT3(0, "tangent-surround",    OPT_UINT,    
+            &cmdlineP->tangent_surround, NULL,                              0);
+    OPTENT3(0, "report-progress",     OPT_FLAG,
+            NULL,                       &cmdlineP->report_progress,         0);
+    OPTENT3(0, "width-weight-factor", OPT_FLOAT,    
+            &cmdlineP->width_weight_factor, NULL,                           0);
+
+
+    opt.opt_table = option_def;
+    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
+    opt.allowNegNum = FALSE;   /* We have no parms that are negative numbers */
+
+    /* Set some defaults the lazy way (using multiple setting of variables) */
+
+    cmdlineP->corner_always_threshold  = 60.0;
+    cmdlineP->corner_surround          = 4;
+    cmdlineP->corner_threshold         = 100.0;
+    cmdlineP->error_threshold          = 2.0;
+    cmdlineP->filter_iterations        = 4;
+    cmdlineP->line_reversion_threshold = 0.01;
+    cmdlineP->line_threshold           = 1.0;
+    cmdlineP->tangent_surround         = 3;
+    cmdlineP->width_weight_factor      = 6.0;
+
+    optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
+    /* Uses and sets argc, argv, and some of *cmdlineP and others. */
+
+    if (cmdlineP->backgroundSpec)
+        cmdlineP->background_color = ppm_parsecolor(background_colorOpt, 255);
+
+    if (argc-1 < 1)
+        cmdlineP->inputFileName = "-";
+    else {
+        cmdlineP->inputFileName = argv[1];
+        
+        if (argc-1 > 1)
+            pm_error("Too many arguments (%u).  The only non-option argument "
+                     "is the input file name.", argc-1);
+    }
+}
+
+
+
+static void
+fitSplines(at_bitmap_type *             const bitmapP,
+           struct cmdlineInfo           const cmdline,
+           at_msg_func                        exceptionHandler,
+           at_progress_func                   progressFunc,
+           at_spline_list_array_type ** const splinesPP) {
+
+    unsigned int progressStat;
+    at_fitting_opts_type * fittingOptsP;
+
+    progressStat = 0;
+           
+    fittingOptsP = at_fitting_opts_new();
+
+    fittingOptsP->backgroundSpec           = cmdline.backgroundSpec;
+    fittingOptsP->background_color         = cmdline.background_color;
+    fittingOptsP->corner_always_threshold  = cmdline.corner_always_threshold;
+    fittingOptsP->corner_surround          = cmdline.corner_surround;
+    fittingOptsP->corner_threshold         = cmdline.corner_threshold;
+    fittingOptsP->error_threshold          = cmdline.error_threshold;
+    fittingOptsP->filter_iterations        = cmdline.filter_iterations;
+    fittingOptsP->line_reversion_threshold = cmdline.line_reversion_threshold;
+    fittingOptsP->line_threshold           = cmdline.line_threshold;
+    fittingOptsP->remove_adjacent_corners  = cmdline.remove_adjacent_corners;
+    fittingOptsP->tangent_surround         = cmdline.tangent_surround;
+    fittingOptsP->centerline               = cmdline.centerline;
+    fittingOptsP->preserve_width           = cmdline.preserve_width;
+    fittingOptsP->width_weight_factor      = cmdline.width_weight_factor;
+
+    *splinesPP = at_splines_new_full(bitmapP, fittingOptsP,
+                                     exceptionHandler, NULL,
+                                     progressFunc, &progressStat,
+                                     NULL, NULL);
+
+    at_fitting_opts_free(fittingOptsP);
+}
+  
+
+
+static void
+writeSplines(at_spline_list_array_type * const splinesP,
+             struct cmdlineInfo          const cmdline,
+             at_output_write_func              outputWriter,
+             FILE *                      const ofP,
+             at_msg_func                       exceptionHandler) {
+
+    at_output_opts_type * outputOptsP;
+
+    outputOptsP = at_output_opts_new();
+    outputOptsP->dpi = cmdline.dpi;
+    
+    at_splines_write(outputWriter, ofP, outputOptsP,
+                     splinesP, exceptionHandler, NULL);
+
+    at_output_opts_free(outputOptsP);
+}  
+
+
+
+static const char *
+filenameRoot(const char * const filename) {
+/*----------------------------------------------------------------------------
+   Return the root of the filename.  E.g. for /home/bryanh/foo.ppm,
+   return 'foo'.
+-----------------------------------------------------------------------------*/
+    char * buffer;
+    bool foundSlash;
+    unsigned int slashPos;
+    bool foundDot;
+    unsigned int dotPos;
+    unsigned int rootStart, rootEnd;
+    unsigned int i, j;
+
+    for (i = 0, foundSlash = FALSE; i < strlen(filename); ++i) {
+        if (filename[i] == '/') {
+            foundSlash = TRUE;
+            slashPos = i;
+        }
+    }
+
+    if (foundSlash)
+        rootStart = slashPos + 1;
+    else
+        rootStart = 0;
+
+    for (i = rootStart, foundDot = FALSE; i < strlen(filename); ++i) {
+        if (filename[i] == '.') {
+            foundDot = TRUE;
+            dotPos = i;
+        }
+    }
+
+    if (foundDot)
+        rootEnd = dotPos;
+    else
+        rootEnd = strlen(filename);
+
+    MALLOCARRAY(buffer, rootEnd - rootStart + 1);
+    
+    j = 0;
+    for (i = rootStart; i < rootEnd; ++i)
+        buffer[j++] = filename[i];
+
+    buffer[j] = '\0';
+    
+    return buffer;
+}
+
+
+
+static void
+openLogFile(FILE **      const logFileP,
+            const char * const inputRootName) {
+
+    const char * logfileName;
+
+    asprintfN(&logfileName, "%s.log", inputRootName);
+
+    *logFileP = pm_openw(logfileName);
+
+    strfree(logfileName);
+}
+    
+
+
+int
+main(int argc, char * argv[]) {
+
+    struct cmdlineInfo cmdline;
+    FILE * ifP;
+    at_bitmap_type * bitmapP;
+    at_spline_list_array_type * splinesP;
+    at_progress_func progressReporter;
+    const char * inputRootName;
+
+    pnm_init(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFileName);
+
+    inputRootName = filenameRoot(cmdline.inputFileName);
+    if (inputRootName == NULL)
+        pm_error("Can't find the root portion of file name '%s'",
+                 cmdline.inputFileName);
+    
+    if (cmdline.log)
+        openLogFile(&log_file, inputRootName);
+
+    readImageToBitmap(ifP, &bitmapP);
+    
+    if (cmdline.report_progress) {
+        progressReporter = dotPrinter;
+        fprintf(stderr, "%-15s", cmdline.inputFileName);
+    } else
+        progressReporter = NULL;
+
+    fitSplines(bitmapP, cmdline, exceptionHandler,
+               progressReporter, &splinesP);
+
+    writeSplines(splinesP, cmdline, output_svg_writer, stdout,
+                 exceptionHandler);
+
+    strfree(inputRootName);
+
+    pm_close(stdout);
+    pm_close(ifP);
+    if (cmdline.log)
+        pm_close(log_file);
+    
+    at_splines_free(splinesP);
+    at_bitmap_free(bitmapP);
+
+    if (cmdline.report_progress)
+        fputs("\n", stderr);
+    
+    return 0;
+}
diff --git a/converter/other/pamtosvg/pamtosvg.test b/converter/other/pamtosvg/pamtosvg.test
new file mode 100644
index 00000000..df3a07d3
--- /dev/null
+++ b/converter/other/pamtosvg/pamtosvg.test
@@ -0,0 +1,6 @@
+# This will print nothing if successful (diff will find no difference)
+ppmmake black 20 20 | ppmdraw -script="line 5 2 15 17" | pamtosvg | \
+  diff testline.svg -
+
+# This will print nothing if successful (diff will find no difference)
+pamtosvg ../../../../testgrid.pbm | diff testgrid.svg -
diff --git a/converter/other/pamtosvg/point.h b/converter/other/pamtosvg/point.h
new file mode 100644
index 00000000..037ec8a0
--- /dev/null
+++ b/converter/other/pamtosvg/point.h
@@ -0,0 +1,8 @@
+#ifndef POINT_H_INCLUDED
+#define POINT_H_INCLUDED
+
+typedef struct {
+  float x, y, z;
+} float_coord;
+
+#endif
diff --git a/converter/other/pamtosvg/pxl-outline.c b/converter/other/pamtosvg/pxl-outline.c
new file mode 100644
index 00000000..68cd0565
--- /dev/null
+++ b/converter/other/pamtosvg/pxl-outline.c
@@ -0,0 +1,1370 @@
+/* pxl-outline.c: find the outlines of a bitmap image; each outline is
+   made up of one or more pixels; and each pixel participates via one
+   or more edges.
+*/
+
+#include <assert.h>
+
+#include "mallocvar.h"
+
+#include "message.h"
+#include "bitmap.h"
+#include "bitmap.h"
+#include "logreport.h"
+#include "pxl-outline.h"
+
+/* We consider each pixel to consist of four edges, and we travel along
+   edges, instead of through pixel centers.  This is necessary for those
+   unfortunate times when a single pixel is on both an inside and an
+   outside outline.
+
+   The numbers chosen here are not arbitrary; the code that figures out
+   which edge to move to depends on particular values.  See the
+   `TRY_PIXEL' macro in `edge.c'.  To emphasize this, I've written in the
+   numbers we need for each edge value.  */
+
+typedef enum
+  {
+    TOP = 1, LEFT = 2, BOTTOM = 3, RIGHT = 0, NO_EDGE = 4
+  } edge_type;
+
+/* This choice is also not arbitrary: starting at the top edge makes the
+   code find outside outlines before inside ones, which is certainly
+   what we want.  */
+#define START_EDGE  top
+
+typedef enum
+  {
+    NORTH = 0, NORTHWEST = 1, WEST = 2, SOUTHWEST = 3, SOUTH = 4,
+    SOUTHEAST = 5, EAST = 6, NORTHEAST = 7
+  } direction_type;
+
+#define NUM_EDGES NO_EDGE
+
+#define COMPUTE_DELTA(axis, dir)                        \
+  ((dir) % 2 != 0                                       \
+    ? COMPUTE_##axis##_DELTA ((dir) - 1)                \
+      + COMPUTE_##axis##_DELTA (((dir) + 1) % 8)        \
+    : COMPUTE_##axis##_DELTA (dir)                      \
+  )
+
+#define COMPUTE_ROW_DELTA(dir)                          \
+  ((dir) == NORTH ? -1 : (dir) == SOUTH ? +1 : 0)
+
+#define COMPUTE_COL_DELTA(dir)                  \
+  ((dir) == WEST ? -1 : (dir) == EAST ? +1 : 0)
+
+static void append_pixel_outline (pixel_outline_list_type *,
+                                  pixel_outline_type);
+static pixel_outline_type new_pixel_outline (void);
+static void free_pixel_outline (pixel_outline_type *);
+static void concat_pixel_outline (pixel_outline_type *,
+                                  const pixel_outline_type*);
+static bool is_marked_edge (edge_type, unsigned short, unsigned short, bitmap_type);
+
+static void
+mark_edge (edge_type e, unsigned short, unsigned short, bitmap_type *);
+
+static bool is_marked_dir(unsigned short, unsigned short, direction_type, bitmap_type);
+static bool is_other_dir_marked(unsigned short, unsigned short, direction_type, bitmap_type);
+static void mark_dir(unsigned short, unsigned short, direction_type, bitmap_type *);
+
+static unsigned
+num_neighbors(unsigned short, unsigned short, bitmap_type);
+
+#define CHECK_FATAL() if (at_exception_got_fatal(exp)) goto cleanup;
+#define RETURN_IF_FATAL() if (at_exception_got_fatal(exp)) return;
+
+
+
+
+static pixel
+getBitmapColor(bitmap_type  const bitmap,
+               unsigned int const row,
+               unsigned int const col) {
+
+    pixel pix;
+    unsigned char *p = BITMAP_PIXEL (bitmap, row, col);
+  
+    if (bitmap.np >= 3)
+        PPM_ASSIGN(pix, p[0], p[1], p[2]);
+    else
+        PPM_ASSIGN(pix, p[0], p[0], p[0]);
+
+    return pix;
+}
+
+
+
+
+static void
+append_outline_pixel(pixel_outline_type * const pixelOutlineP,
+                     pm_pixelcoord        const coord) {
+/*----------------------------------------------------------------------------
+  Add a point to the pixel list.
+-----------------------------------------------------------------------------*/
+
+    O_LENGTH(*pixelOutlineP)++;
+    REALLOCARRAY_NOFAIL(pixelOutlineP->data, O_LENGTH(*pixelOutlineP));
+
+    O_COORDINATE(*pixelOutlineP, O_LENGTH(*pixelOutlineP) - 1) = coord;
+}
+
+
+
+/* We check to see if the edge of the pixel at position ROW and COL
+   is an outline edge */
+
+static bool
+is_outline_edge (edge_type edge, bitmap_type bitmap,
+                 unsigned short row, unsigned short col, pixel color,
+                 at_exception_type * exp)
+{
+  /* If this pixel isn't of the same color, it's not part of the outline. */
+  if (!PPM_EQUAL (getBitmapColor (bitmap, row, col), color))
+    return false;
+
+  switch (edge)
+    {
+    case LEFT:
+      return (bool)
+          (col == 0 ||
+           !PPM_EQUAL (getBitmapColor (bitmap, row, col - 1), color));
+
+    case TOP:
+      return (bool)
+          (row == 0 ||
+           !PPM_EQUAL (getBitmapColor (bitmap, row - 1, col), color));
+
+    case RIGHT:
+        return (bool)
+            (col == bitmap.width - 1
+             || !PPM_EQUAL(getBitmapColor(bitmap, row, col + 1), color));
+
+    case BOTTOM:
+        return (bool)
+            (row == bitmap.height - 1
+             || !PPM_EQUAL(getBitmapColor (bitmap, row + 1, col), color));
+
+    case NO_EDGE:
+    default:
+      LOG1 ("is_outline_edge: Bad edge value(%d)", edge);
+      at_exception_fatal(exp, "is_outline_edge: Bad edge value");
+    }
+
+  return false; /* NOT REACHED */
+}
+
+
+/* Is this really an edge and is it still unmarked? */
+
+static bool
+is_unmarked_outline_edge(unsigned short row,
+                         unsigned short col,
+                         edge_type edge,
+                         bitmap_type bitmap,
+                         bitmap_type marked,
+                         pixel color,
+                         at_exception_type * exp)
+{
+  return
+    (bool)(!is_marked_edge (edge, row, col, marked)
+              && is_outline_edge (edge, bitmap, row, col, color, exp));
+}
+
+
+static bool
+is_valid_dir(unsigned int   const row,
+             unsigned int   const col,
+             direction_type const dir,
+             bitmap_type    const bitmap,
+             bitmap_type    const marked) {
+  
+    return(!is_marked_dir(row, col, dir, marked)
+           && COMPUTE_DELTA(ROW, dir)+row > 0
+           && COMPUTE_DELTA(COL, dir)+col > 0
+           && BITMAP_VALID_PIXEL(bitmap,
+                                 COMPUTE_DELTA(ROW, dir) + row,
+                                 COMPUTE_DELTA(COL, dir) + col)
+           && PPM_EQUAL(getBitmapColor(bitmap,
+                                       COMPUTE_DELTA(ROW, dir) + row,
+                                       COMPUTE_DELTA(COL, dir) + col),
+                        getBitmapColor(bitmap, row, col)));
+}
+
+
+
+static bool
+next_unmarked_pixel(unsigned int *   const row,
+                    unsigned int *   const col,
+                    direction_type * const dir,
+                    bitmap_type      const bitmap,
+                    bitmap_type *    const marked) {
+
+    unsigned int   const orig_row = *row;
+    unsigned int   const orig_col = *col;
+    direction_type const orig_dir = *dir;
+
+    direction_type test_dir;
+    pixel color;
+
+    test_dir = *dir;  /* initial value */
+    color = getBitmapColor(bitmap, *row, *col);
+
+    do {
+        if (is_valid_dir(orig_row, orig_col, test_dir, bitmap, *marked)) {
+            *row = orig_row + COMPUTE_DELTA(ROW, test_dir);
+            *col = orig_col + COMPUTE_DELTA(COL, test_dir);
+            *dir = test_dir;
+            break;
+        }
+
+        if (orig_dir == test_dir)
+            test_dir = (orig_dir + 2) % 8;
+        else if ((orig_dir + 2) % 8 == test_dir)
+            test_dir = (orig_dir + 6) % 8;
+        else if ((orig_dir + 6) % 8 == test_dir)
+            test_dir = (orig_dir + 1) % 8;
+        else if ((orig_dir + 1) % 8 == test_dir)
+            test_dir = (orig_dir + 7) % 8;
+        else if ((orig_dir + 7) % 8 == test_dir)
+            test_dir = (orig_dir + 3) % 8;
+        else if ((orig_dir + 3) % 8 == test_dir)
+            test_dir = (orig_dir + 5) % 8;
+        else if ((orig_dir + 5) % 8 == test_dir)
+            break;
+    } while (1);
+
+    if ((*row != orig_row || *col != orig_col) && 
+        (!(is_other_dir_marked(orig_row, orig_col, test_dir, *marked) &&
+           is_other_dir_marked(orig_row + COMPUTE_DELTA(ROW, test_dir),
+                               orig_col + COMPUTE_DELTA(COL, test_dir),
+                               test_dir,*marked))))
+        return true;
+    else
+        return false;
+}
+
+
+
+static pixel_outline_type
+find_one_centerline(bitmap_type    const bitmap,
+                    direction_type const original_dir,
+                    unsigned int   const original_row,
+                    unsigned int   const original_col,
+                    bitmap_type *  const marked) {
+
+    direction_type search_dir;
+    unsigned int row, col;
+    pixel_outline_type outline;
+
+    outline = new_pixel_outline();
+    outline.open  = false;
+    outline.color = getBitmapColor(bitmap, row, col);
+
+    /* Add the starting pixel to the output list, changing from bitmap
+       to Cartesian coordinates and specifying the left edge so that
+       the coordinates won't be adjusted.
+    */
+    {
+        pm_pixelcoord pos;
+        pos.col = col; pos.row = bitmap.height - row - 1;
+        LOG2(" (%d,%d)", pos.col, pos.row);
+        append_outline_pixel(&outline, pos);
+    }
+    search_dir = original_dir;  /* initial value */
+    row = original_row;         /* initial value */
+    col = original_col;         /* initial values */
+
+    for ( ; ; ) {
+        unsigned int const prev_row = row;
+        unsigned int const prev_col = col;
+
+        /* If there is no adjacent, unmarked pixel, we can't proceed
+           any further, so return an open outline.
+        */
+        if (!next_unmarked_pixel(&row, &col, &search_dir, bitmap, marked)) {
+            outline.open = true;
+            break;
+        }
+
+        /* If we've moved to a new pixel, mark all edges of the previous
+           pixel so that it won't be revisited.
+        */
+        if (!(prev_row == original_row && prev_col == original_col))
+            mark_dir(prev_row, prev_col, search_dir, marked);
+        mark_dir(row, col, (search_dir + 4) % 8, marked);
+
+        /* If we've returned to the starting pixel, we're done. */
+        if (row == original_row && col == original_col)
+            break;
+
+        
+        {
+            /* Add the new pixel to the output list. */
+            pm_pixelcoord pos;
+            pos.col = col; pos.row = bitmap.height - row - 1;
+            LOG2(" (%d,%d)", pos.col, pos.row);
+            append_outline_pixel(&outline, pos);
+        }
+    }
+    mark_dir(original_row, original_col, original_dir, marked);
+
+    return outline;
+}
+
+
+
+static bool
+wrongDirection(unsigned int   const row,
+               unsigned int   const col,
+               direction_type const dir,
+               bitmap_type    const bitmap,
+               bitmap_type    const marked) {
+
+    return (!is_valid_dir(row, col, dir, bitmap, marked)
+            || (!is_valid_dir(COMPUTE_DELTA(ROW, dir) + row,
+                              COMPUTE_DELTA(COL, dir) + col,
+                              dir, bitmap, marked)
+                && num_neighbors(row, col, bitmap) > 2)
+            || num_neighbors(row, col, bitmap) > 4
+            || num_neighbors(COMPUTE_DELTA(ROW, dir) + row,
+                             COMPUTE_DELTA(COL, dir) + col, bitmap) > 4
+            || (is_other_dir_marked(row, col, dir, marked)
+                && is_other_dir_marked(row + COMPUTE_DELTA(ROW, dir),
+                                       col + COMPUTE_DELTA(COL, dir),
+                                       dir, marked)));
+}
+
+
+
+pixel_outline_list_type
+find_centerline_pixels(bitmap_type         const bitmap,
+                       pixel               const bg_color, 
+                       at_progress_func          notify_progress,
+                       void *              const progress_data,
+                       at_testcancel_func        test_cancel,
+                       void *              const testcancel_data,
+                       at_exception_type * const exp) {
+
+  pixel_outline_list_type outline_list;
+  signed short row;
+  bitmap_type marked = new_bitmap(bitmap.width, bitmap.height);
+  unsigned int const max_progress = bitmap.height * bitmap.width;
+    
+  O_LIST_LENGTH(outline_list) = 0;
+  outline_list.data = NULL;
+
+  for (row = 0; row < bitmap.height; ++row) {
+      signed short col;
+      for (col = 0; col < bitmap.width; ) {
+          bool           const clockwise = false;
+
+          direction_type dir;
+          pixel_outline_type outline;
+
+          if (notify_progress)
+              notify_progress((float)(row * bitmap.width + col) /
+                              ((float) max_progress * (float)3.0),
+                              progress_data);
+
+		  if (PPM_EQUAL(getBitmapColor(bitmap, row, col), bg_color)) {
+	          ++col;
+			  continue;
+          }
+
+          dir = EAST;
+
+          if (wrongDirection(row, col, dir, bitmap, marked)) {
+              dir = SOUTHEAST;
+              if (wrongDirection(row, col, dir, bitmap, marked)) {
+                  dir = SOUTH;
+                  if (wrongDirection(row, col, dir, bitmap, marked)) {
+                      dir = SOUTHWEST;
+                      if (wrongDirection(row, col, dir, bitmap, marked)) {
+						  ++col;
+						  continue;
+                      }
+                  }
+              }
+          }
+
+          LOG2("#%u: (%sclockwise) ", O_LIST_LENGTH(outline_list),
+               clockwise ? "" : "counter");
+
+          outline = find_one_centerline(bitmap, dir, row, col, &marked);
+
+          /* If the outline is open (i.e., we didn't return to the
+             starting pixel), search from the starting pixel in the
+             opposite direction and concatenate the two outlines.
+          */
+
+          if (outline.open) {
+              pixel_outline_type partial_outline;
+              bool okay = false;
+
+              if (dir == EAST) {
+                  dir = SOUTH;
+                  okay = is_valid_dir(row, col, dir, bitmap, marked);
+                  if (!okay) {
+                      dir = SOUTHWEST;
+                      okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      if (!okay) {
+                          dir = SOUTHEAST;
+                          okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      }
+                  }
+              } else if (dir == SOUTHEAST) {
+                  dir = SOUTHWEST;
+                  okay = is_valid_dir(row, col, dir, bitmap, marked);
+                  if (!okay) {
+                      dir = EAST;
+                      okay=is_valid_dir(row, col, dir, bitmap, marked);
+                      if (!okay) {
+                          dir = SOUTH;
+                          okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      }
+                  }
+              } else if (dir == SOUTH) {
+                  dir = EAST;
+                  okay = is_valid_dir(row, col, dir, bitmap, marked);
+                  if (!okay) {
+                      dir = SOUTHEAST;
+                      okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      if (!okay) {
+                          dir = SOUTHWEST;
+                          okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      }
+                  }
+              } else if (dir == SOUTHWEST) {
+                  dir = SOUTHEAST;
+                  okay = is_valid_dir(row, col, dir, bitmap, marked);
+                  if (!okay) {
+                      dir = EAST;
+                      okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      if (!okay) {
+                          dir = SOUTH;
+                          okay = is_valid_dir(row, col, dir, bitmap, marked);
+                      }
+                  }
+              }
+              if (okay) {
+                  partial_outline =
+                      find_one_centerline(bitmap, dir, row, col, &marked);
+                  concat_pixel_outline(&outline, &partial_outline);
+                  if (partial_outline.data)
+                      free(partial_outline.data);
+              } else
+                  ++col;
+          }        
+            
+          /* Outside outlines will start at a top edge, and move
+             counterclockwise, and inside outlines will start at a
+             bottom edge, and move clockwise.  This happens because of
+             the order in which we look at the edges.
+          */
+          O_CLOCKWISE(outline) = clockwise;
+          if (O_LENGTH(outline) > 1)
+              append_pixel_outline(&outline_list, outline);
+          LOG1("(%s)", (outline.open ? " open" : " closed"));
+          LOG1(" [%u].\n", O_LENGTH(outline));
+          if (O_LENGTH(outline) == 1)
+              free_pixel_outline(&outline);
+        }
+  }
+  if (test_cancel && test_cancel(testcancel_data)) {
+      if (O_LIST_LENGTH (outline_list) != 0)
+          free_pixel_outline_list (&outline_list);
+  }
+  free_bitmap(&marked);
+  flush_log_output();
+  return outline_list;
+}
+
+
+
+/* Add an outline to an outline list. */
+
+static void
+append_pixel_outline (pixel_outline_list_type *outline_list,
+                      pixel_outline_type outline)
+{
+  O_LIST_LENGTH (*outline_list)++;
+  REALLOCARRAY_NOFAIL(outline_list->data, outline_list->length);
+  O_LIST_OUTLINE (*outline_list, O_LIST_LENGTH (*outline_list) - 1) = outline;
+}
+
+
+/* Free the list of outline lists. */
+
+void
+free_pixel_outline_list (pixel_outline_list_type *outline_list)
+{
+  unsigned this_outline;
+
+  for (this_outline = 0; this_outline < outline_list->length; this_outline++)
+    {
+      pixel_outline_type o = outline_list->data[this_outline];
+      free_pixel_outline (&o);
+    }
+  outline_list->length = 0;
+
+  if (outline_list->data != NULL)
+    {
+      free (outline_list->data);
+      outline_list->data = NULL;
+    }
+
+  flush_log_output ();
+}
+
+
+/* Return an empty list of pixels.  */
+
+
+static pixel_outline_type
+new_pixel_outline (void)
+{
+  pixel_outline_type pixel_outline;
+
+  O_LENGTH (pixel_outline) = 0;
+  pixel_outline.data = NULL;
+  pixel_outline.open = false;
+
+  return pixel_outline;
+}
+
+static void
+free_pixel_outline (pixel_outline_type * outline)
+{
+  if (outline->data)
+    {
+      free (outline->data) ;
+      outline->data   = NULL;
+      outline->length = 0;
+    }
+}
+
+/* Concatenate two pixel lists. The two lists are assumed to have the
+   same starting pixel and to proceed in opposite directions therefrom. */
+
+static void
+concat_pixel_outline(pixel_outline_type *o1, const pixel_outline_type *o2)
+{
+  int src, dst;
+  unsigned o1_length, o2_length;
+  if (!o1 || !o2 || O_LENGTH(*o2) <= 1) return;
+
+  o1_length = O_LENGTH(*o1);
+  o2_length = O_LENGTH(*o2);
+  O_LENGTH(*o1) += o2_length - 1;
+  /* Resize o1 to the sum of the lengths of o1 and o2 minus one (because
+     the two lists are assumed to share the same starting pixel). */
+  REALLOCARRAY_NOFAIL(o1->data, O_LENGTH(*o1));
+  /* Shift the contents of o1 to the end of the new array to make room
+     to prepend o2. */
+  for (src = o1_length - 1, dst = O_LENGTH(*o1) - 1; src >= 0; src--, dst--)
+    O_COORDINATE(*o1, dst) = O_COORDINATE(*o1, src);
+  /* Prepend the contents of o2 (in reverse order) to o1. */
+  for (src = o2_length - 1, dst = 0; src > 0; src--, dst++)
+    O_COORDINATE(*o1, dst) = O_COORDINATE(*o2, src);
+}
+
+
+/* If EDGE is not already marked, we mark it; otherwise, it's a fatal error.
+   The position ROW and COL should be inside the bitmap MARKED. EDGE can be
+   NO_EDGE. */
+
+static void
+mark_edge (edge_type edge, unsigned short row,
+           unsigned short col, bitmap_type *marked)
+{
+  *BITMAP_PIXEL (*marked, row, col) |= 1 << edge;
+}
+
+
+/* Mark the direction of the pixel ROW/COL in MARKED. */
+
+static void
+mark_dir(unsigned short row, unsigned short col, direction_type dir, bitmap_type *marked)
+{
+  *BITMAP_PIXEL(*marked, row, col) |= 1 << dir;
+}
+
+
+/* Test if the direction of pixel at ROW/COL in MARKED is marked. */
+
+static bool
+is_marked_dir(unsigned short row, unsigned short col, direction_type dir, bitmap_type marked)
+{
+  return (bool)((*BITMAP_PIXEL(marked, row, col) & 1 << dir) != 0);
+}
+
+
+static bool
+is_other_dir_marked(unsigned short row, unsigned short col, direction_type dir, bitmap_type marked)
+{
+  return (bool)((*BITMAP_PIXEL(marked, row, col) & (255 - (1 << dir) - (1 << ((dir + 4) % 8))) ) != 0);
+}
+
+
+/* Return the number of pixels adjacent to pixel ROW/COL that are black. */
+
+static unsigned
+num_neighbors(unsigned short row, unsigned short col, bitmap_type bitmap)
+{
+    unsigned dir, count = 0;
+    pixel color = getBitmapColor(bitmap, row, col);
+    for (dir = NORTH; dir <= NORTHEAST; dir++)
+    {
+	int delta_r = COMPUTE_DELTA(ROW, dir);
+	int delta_c = COMPUTE_DELTA(COL, dir);
+	unsigned int test_row = row + delta_r;
+	unsigned int test_col = col + delta_c;
+	if (BITMAP_VALID_PIXEL(bitmap, test_row, test_col)
+	    && PPM_EQUAL(getBitmapColor(bitmap, test_row, test_col), color))
+	    ++count;
+    }
+    return count;
+}
+
+
+/* Test if the edge EDGE at ROW/COL in MARKED is marked.  */
+
+static bool
+is_marked_edge (edge_type edge, unsigned short row, unsigned short col, bitmap_type marked)
+{
+  return
+    (bool)(edge == NO_EDGE ? false : (*BITMAP_PIXEL (marked, row, col) & (1 << edge)) != 0);
+}
+
+
+
+static void
+nextClockwisePointTop(bitmap_type         const bitmap,
+                      edge_type *         const edge,
+                      unsigned int *      const row,
+                      unsigned int *      const col,
+                      pixel               const color,
+                      bitmap_type         const marked,
+                      at_exception_type * const exp,
+                      pm_pixelcoord *     const posP) {
+
+    if ((*col >= 1 && !is_marked_edge(TOP, *row, *col-1, marked) &&
+             is_outline_edge(TOP, bitmap, *row, *col-1, color, exp))) {
+
+        /* WEST */
+
+        *edge = TOP;
+        --*col;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col >= 1 && *row >= 1 &&
+         !is_marked_edge(RIGHT, *row-1, *col-1, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row-1, *col-1,
+                         color, exp)) &&
+        !(is_marked_edge(LEFT, *row-1, *col, marked) &&
+          is_marked_edge(TOP, *row,*col-1, marked)) &&
+        !(is_marked_edge(BOTTOM, *row-1, *col, marked) &&
+          is_marked_edge(RIGHT, *row, *col-1, marked))) {
+
+        /* NORTHWEST */
+
+        *edge = RIGHT;
+        --*col;
+        --*row;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row;
+        return;
+    } 
+
+    RETURN_IF_FATAL();
+
+    if ((!is_marked_edge(LEFT, *row, *col, marked)
+         && is_outline_edge(LEFT, bitmap, *row, *col, color, exp))) {
+
+        *edge = LEFT;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextClockwisePointRight(bitmap_type         const bitmap,
+                        edge_type *         const edge,
+                        unsigned int *      const row,
+                        unsigned int *      const col,
+                        pixel               const color,
+                        bitmap_type         const marked,
+                        at_exception_type * const exp,
+                        pm_pixelcoord *     const posP) {
+
+    if ((*row >= 1 &&
+         !is_marked_edge(RIGHT, *row-1, *col, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row-1, *col, color, exp))) {
+
+         /* NORTH */
+        
+        *edge = RIGHT;
+        --*row;
+        posP->col = *col+1;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+    
+    if ((*col+1 < marked.width && *row >= 1 &&
+         !is_marked_edge(BOTTOM, *row-1, *col+1, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row-1, *col+1,
+                         color, exp)) &&
+        !(is_marked_edge(LEFT, *row, *col+1, marked) &&
+          is_marked_edge(BOTTOM, *row-1, *col, marked)) &&
+        !(is_marked_edge(TOP, *row, *col+1, marked) &&
+          is_marked_edge(RIGHT, *row-1, *col, marked))) {
+
+        /* NORTHEAST */
+        *edge = BOTTOM;
+        ++*col;
+        --*row;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    } 
+
+    RETURN_IF_FATAL();
+
+    if ((!is_marked_edge(TOP, *row, *col, marked) &&
+         is_outline_edge(TOP, bitmap, *row, *col, color, exp))) {
+
+        *edge = TOP;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextClockwisePointBottom(bitmap_type         const bitmap,
+                         edge_type *         const edge,
+                         unsigned int *      const row,
+                         unsigned int *      const col,
+                         pixel               const color,
+                         bitmap_type         const marked,
+                         at_exception_type * const exp,
+                         pm_pixelcoord *     const posP) {
+    
+    if ((*col+1 < marked.width &&
+         !is_marked_edge(BOTTOM, *row, *col+1, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row, *col+1, color, exp))) {
+
+        /* EAST */
+
+        *edge = BOTTOM;
+        ++*col;
+        posP->col = *col+1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col + 1 < marked.width && *row+1 < marked.height &&
+         !is_marked_edge(LEFT, *row+1,*col+1, marked) &&
+         is_outline_edge(LEFT, bitmap, *row+1, *col+1, color, exp)) &&
+        !(is_marked_edge(TOP, *row+1, *col, marked) &&
+          is_marked_edge(LEFT, *row, *col+1, marked)) &&
+        !(is_marked_edge(RIGHT, *row+1, *col, marked) &&
+          is_marked_edge(BOTTOM, *row, *col+1, marked))) {
+
+        /* SOUTHEAST */
+
+        *edge = LEFT;
+        ++*col;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((!is_marked_edge(RIGHT, *row, *col, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row, *col, color, exp))) {
+
+        *edge = RIGHT;
+        posP->col = *col+1;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextClockwisePointLeft(bitmap_type         const bitmap,
+                       edge_type *         const edge,
+                       unsigned int *      const row,
+                       unsigned int *      const col,
+                       pixel               const color,
+                       bitmap_type         const marked,
+                       at_exception_type * const exp,
+                       pm_pixelcoord *     const posP) {
+
+    if ((*row+1 < marked.height &&
+         !is_marked_edge(LEFT, *row+1, *col, marked) &&
+         is_outline_edge(LEFT, bitmap, *row+1, *col, color, exp))) {
+
+        /* SOUTH */
+
+        *edge = LEFT;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col >= 1 && *row+1 < marked.height &&
+         !is_marked_edge(TOP, *row+1, *col-1, marked) &&
+         is_outline_edge(TOP, bitmap, *row+1, *col-1, color, exp)) &&
+        !(is_marked_edge(RIGHT, *row, *col-1, marked) &&
+          is_marked_edge(TOP, *row+1, *col, marked)) &&
+        !(is_marked_edge(BOTTOM, *row, *col-1, marked) &&
+          is_marked_edge(LEFT, *row+1, *col, marked))) {
+        
+        /* SOUTHWEST */
+        
+        *edge = TOP;
+        --*col;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    } 
+
+    RETURN_IF_FATAL();
+
+    if ((!is_marked_edge(BOTTOM, *row, *col, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row, *col, color, exp))) {
+        *edge = BOTTOM;
+        posP->col = *col+1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextClockwisePoint(bitmap_type         const bitmap,
+                   edge_type *         const edge,
+                   unsigned int *      const row,
+                   unsigned int *      const col,
+                   pixel               const color,
+                   bitmap_type         const marked,
+                   at_exception_type * const exp,
+                   pm_pixelcoord *     const posP) {
+    
+    switch (*edge) {
+    case TOP:
+        nextClockwisePointTop(  bitmap, edge, row, col, color,
+                                marked, exp, posP);
+        break;
+    case RIGHT: 
+        nextClockwisePointRight(bitmap, edge, row, col, color,
+                                marked, exp, posP);
+        break;
+    case BOTTOM: 
+        nextClockwisePointBottom(bitmap, edge, row, col, color,
+                                 marked, exp, posP);
+        break;
+    case LEFT: 
+        nextClockwisePointLeft(  bitmap, edge, row, col, color,
+                                 marked, exp, posP);
+        break;
+    case NO_EDGE:
+        break;
+    default:
+        *edge = NO_EDGE;
+        break;
+    }
+}
+
+
+
+static void
+nextCcwPointTop(bitmap_type         const bitmap,
+                edge_type *         const edge,
+                unsigned int *      const row,
+                unsigned int *      const col,
+                pixel               const color,
+                bitmap_type         const marked,
+                at_exception_type * const exp,
+                pm_pixelcoord *     const posP) {
+
+    if ((!is_marked_edge(LEFT, *row, *col, marked) &&
+         is_outline_edge(LEFT,bitmap,*row,*col, color, exp))) {
+
+        *edge = LEFT;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col >= 1 &&
+         !is_marked_edge(TOP, *row, *col-1, marked) &&
+         is_outline_edge(TOP, bitmap, *row, *col-1, color, exp))) {
+
+        /* WEST */
+
+        *edge = TOP;
+        --*col;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+    
+    if ((*col >= 1 && *row >= 1 &&
+         !is_marked_edge(RIGHT, *row-1, *col-1, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row-1, *col-1, color, exp))) {
+
+        /* NORTHWEST */
+
+        *edge = RIGHT;
+        --*col;
+        --*row;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row;
+        return;
+    } 
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextCcwPointRight(bitmap_type         const bitmap,
+                  edge_type *         const edge,
+                  unsigned int *      const row,
+                  unsigned int *      const col,
+                  pixel               const color,
+                  bitmap_type         const marked,
+                  at_exception_type * const exp,
+                  pm_pixelcoord *     const posP) {
+
+    if ((!is_marked_edge(TOP, *row, *col, marked) &&
+         is_outline_edge(TOP, bitmap, *row, *col, color, exp))) {
+
+        *edge = TOP;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*row >= 1 &&
+         !is_marked_edge(RIGHT, *row-1, *col, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row-1, *col, color, exp))) {
+
+        /* NORTH */
+        
+        *edge = RIGHT;
+        --*row;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col + 1 < marked.width && *row >= 1 &&
+         !is_marked_edge(BOTTOM, *row-1, *col+1, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row-1, *col+1, color, exp))) {
+
+        /* NORTHEAST */
+
+        *edge = BOTTOM;
+        ++*col;
+        ++*row;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextCcwPointBottom(bitmap_type         const bitmap,
+                   edge_type *         const edge,
+                   unsigned int *      const row,
+                   unsigned int *      const col,
+                   pixel               const color,
+                   bitmap_type         const marked,
+                   at_exception_type * const exp,
+                   pm_pixelcoord *     const posP) {
+
+    if ((!is_marked_edge(RIGHT, *row, *col, marked) &&
+         is_outline_edge(RIGHT, bitmap, *row, *col, color, exp))) {
+
+        *edge = RIGHT;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col + 1 < marked.width &&
+         !is_marked_edge(BOTTOM, *row, *col+1, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row, *col+1, color, exp))) {
+
+        /* EAST */
+
+        *edge = BOTTOM;
+        ++*col;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*col + 1 < marked.width && *row + 1 < marked.height &&
+         !is_marked_edge(LEFT, *row+1, *col+1, marked) &&
+         is_outline_edge(LEFT, bitmap, *row+1, *col+1, color, exp))) {
+
+        /* SOUTHEAST */
+
+        *edge = LEFT;
+        ++*col;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+    
+    *edge = NO_EDGE;
+}
+
+
+
+static void
+nextCcwPointLeft(bitmap_type         const bitmap,
+                 edge_type *         const edge,
+                 unsigned int *      const row,
+                 unsigned int *      const col,
+                 pixel               const color,
+                 bitmap_type         const marked,
+                 at_exception_type * const exp,
+                 pm_pixelcoord *     const posP) {
+
+
+    if ((!is_marked_edge(BOTTOM, *row, *col, marked) &&
+         is_outline_edge(BOTTOM, bitmap, *row, *col, color, exp))) {
+
+        *edge = BOTTOM;
+        posP->col = *col + 1;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    if ((*row + 1 < marked.height &&
+         !is_marked_edge(LEFT, *row+1, *col, marked) &&
+         is_outline_edge(LEFT, bitmap, *row+1, *col, color, exp))) {
+
+        /* SOUTH */
+
+        *edge = LEFT;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row - 1;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+    
+    if ((*col >= 1 && *row + 1 < marked.height &&
+         !is_marked_edge(TOP, *row+1, *col-1, marked) &&
+         is_outline_edge(TOP, bitmap, *row+1, *col-1, color, exp))) {
+
+        /* SOUTHWEST */
+
+        *edge = TOP;
+        --*col;
+        ++*row;
+        posP->col = *col;
+        posP->row = bitmap.height - *row;
+        return;
+    }
+
+    RETURN_IF_FATAL();
+
+    *edge = NO_EDGE;
+}
+
+static void
+nextCounterClockwisePoint(bitmap_type         const bitmap,
+                          edge_type *         const edge,
+                          unsigned int *      const row,
+                          unsigned int *      const col,
+                          pixel               const color,
+                          bitmap_type         const marked,
+                          at_exception_type * const exp,
+                          pm_pixelcoord *     const posP) {
+
+    switch (*edge) {
+    case TOP:
+        nextCcwPointTop(   bitmap, edge, row, col, color, marked, exp, posP);
+        break;
+    case RIGHT: 
+        nextCcwPointRight( bitmap, edge, row, col, color, marked, exp, posP);
+        break;
+    case BOTTOM: 
+        nextCcwPointBottom(bitmap, edge, row, col, color, marked, exp, posP);
+        break;
+    case LEFT: 
+        nextCcwPointLeft(  bitmap, edge, row, col, color, marked, exp, posP);
+        break;
+    case NO_EDGE:
+        break;
+    default: 
+        *edge = NO_EDGE;
+        break;
+    }
+}
+
+
+
+static void
+nextPoint(bitmap_type         const bitmap,
+          edge_type *         const edge,
+          unsigned int *      const row,
+          unsigned int *      const col,
+          pm_pixelcoord *     const nextPointP,
+          pixel               const color,
+          bool                const clockwise,
+          bitmap_type         const marked,
+          at_exception_type * const exp) {
+
+    if (!clockwise)
+        nextClockwisePoint(       bitmap, edge, row, col, color,
+                                  marked, exp, nextPointP);
+    else
+        nextCounterClockwisePoint(bitmap, edge, row, col, color,
+                                  marked, exp, nextPointP);
+}
+
+
+
+static pixel_outline_type
+find_one_outline(bitmap_type         const bitmap,
+                 edge_type           const originalEdge,
+                 unsigned int        const originalRow,
+                 unsigned int        const originalCol,
+                 bitmap_type *       const marked,
+                 bool                const clockwise,
+                 bool                const ignore,
+                 at_exception_type * const exp) {
+/*----------------------------------------------------------------------------
+  Calculate one single outline.  We pass the position of the
+  starting pixel and the starting edge.  We mark all edges we track along
+  and append the outline pixels to the coordinate list.
+-----------------------------------------------------------------------------*/
+    unsigned int row;
+    unsigned int col;
+    edge_type    edge;
+
+    pixel_outline_type outline;
+    pm_pixelcoord pos;
+    
+    outline = new_pixel_outline();
+    outline.color = getBitmapColor(bitmap, originalRow, originalCol);
+
+    row  = originalRow;   /* initial value */
+    col  = originalCol;   /* initial value */
+    edge = originalEdge;  /* initial value */
+
+    /* Set initial position */
+    pos.col = col + ((edge == RIGHT) || (edge == BOTTOM) ? 1 : 0);
+    pos.row = bitmap.height - row - 1 + 
+        (edge == TOP || edge == RIGHT ? 1 : 0);
+
+    do {
+        /* Put this edge into the output list */
+        if (!ignore) {
+            LOG2(" (%d,%d)", pos.col, pos.row);
+            append_outline_pixel(&outline, pos);
+        }
+        
+        mark_edge(edge, row, col, marked);
+        nextPoint(bitmap, &edge, &row, &col, &pos, outline.color, clockwise,
+                  *marked, exp);
+            /* edge, row, and col are both input and output in the above */
+    } while (edge != NO_EDGE && !at_exception_got_fatal(exp));
+
+    if (ignore || at_exception_got_fatal(exp))
+        free_pixel_outline(&outline);
+
+    return outline;
+}
+
+
+
+pixel_outline_list_type
+find_outline_pixels(bitmap_type         const bitmap,
+                    bool                const bg_spec,
+                    pixel               const bg_color, 
+                    at_progress_func          notify_progress,
+                    void *              const progress_data,
+                    at_testcancel_func        test_cancel,
+                    void *              const testcancel_data,
+                    at_exception_type * const exp) {
+/*----------------------------------------------------------------------------
+   Return a list of outlines in the image whose raster is 'bitmap'.
+
+   The background color of the image is 'bg_color' if 'bg_spec' is true;
+   otherwise, there is no background color.
+-----------------------------------------------------------------------------*/
+    /* We go through a bitmap TOP to BOTTOM, LEFT to RIGHT, looking for
+       each pixel with an unmarked edge that we consider a starting point
+       of an outline.  When we find one, we trace the outline and add it
+       to the list, marking the edges in it as we go.
+    */
+    unsigned int const max_progress = bitmap.height * bitmap.width;
+    
+    pixel_outline_list_type outline_list;
+    unsigned int row;
+    bitmap_type marked;
+    
+    marked = new_bitmap (bitmap.width, bitmap.height);
+    
+    O_LIST_LENGTH(outline_list) = 0;
+    outline_list.data = NULL;
+    
+    for (row = 0; row < bitmap.height; ++row) {
+        unsigned int col;
+        for (col = 0; col < bitmap.width; ++col) {
+            pixel const color = getBitmapColor(bitmap, row, col);
+            bool const is_background =
+                bg_spec && PPM_EQUAL(color, bg_color);
+
+            if (notify_progress)
+                notify_progress((float)(row * bitmap.width + col) /
+                                ((float) max_progress * (float)3.0),
+                                progress_data);
+
+            /* A valid edge can be TOP for an outside outline.
+               Outside outlines are traced counterclockwise.
+            */
+
+            if (!is_background &&
+                is_unmarked_outline_edge(row, col, TOP,
+                                         bitmap, marked, color, exp)) {
+                pixel_outline_type outline;
+                
+                CHECK_FATAL();   /* FREE(DONE) outline_list */
+                
+                LOG1("#%u: (counterclockwise)", O_LIST_LENGTH(outline_list));
+                
+                outline = find_one_outline(bitmap, TOP, row, col, &marked,
+                                           false, false, exp);
+                CHECK_FATAL();    /* FREE(DONE) outline_list */
+                
+                O_CLOCKWISE(outline) = false;
+                append_pixel_outline(&outline_list, outline);
+                
+                LOG1(" [%u].\n", O_LENGTH (outline));
+            } else
+                CHECK_FATAL ();	/* FREE(DONE) outline_list */
+
+            /* A valid edge can be BOTTOM for an inside outline.
+               Inside outlines are traced clockwise.
+            */
+            if (row > 0) {
+                pixel const colorAbove = getBitmapColor(bitmap, row-1, col);
+                if (!(bg_spec && PPM_EQUAL(colorAbove, bg_color)) &&
+                    is_unmarked_outline_edge(row-1, col, BOTTOM,
+                                             bitmap, marked, colorAbove,exp)) {
+                    CHECK_FATAL(); /* FREE(DONE) outline_list */
+                    
+                    /* This lines are for debugging only:*/
+                    if (is_background) {
+                        pixel_outline_type outline;
+                    
+                        LOG1("#%u: (clockwise)", O_LIST_LENGTH(outline_list));
+                        
+                        outline = find_one_outline(bitmap, BOTTOM, row-1, col,
+                                                   &marked, true, false, exp);
+                        CHECK_FATAL(); /* FREE(DONE) outline_list */
+                        
+                        O_CLOCKWISE(outline) = true;
+                        append_pixel_outline(&outline_list, outline);
+                        
+                        LOG1(" [%u].\n", O_LENGTH(outline));
+                    } else {
+                        find_one_outline(bitmap, BOTTOM, row-1, col,
+                                         &marked, true, true, exp);
+                        CHECK_FATAL(); /* FREE(DONE) outline_list */
+                    }
+                } else
+                    CHECK_FATAL();	/* FREE(DONE) outline_list */
+            }
+            if (test_cancel && test_cancel(testcancel_data)) {
+                free_pixel_outline_list(&outline_list);
+                goto cleanup;
+            }
+        }
+    }
+ cleanup:
+    free_bitmap(&marked);
+    flush_log_output();
+    if (at_exception_got_fatal(exp))
+        free_pixel_outline_list(&outline_list);
+
+    return outline_list;
+}
+
diff --git a/converter/other/pamtosvg/pxl-outline.h b/converter/other/pamtosvg/pxl-outline.h
new file mode 100644
index 00000000..e37ccaf6
--- /dev/null
+++ b/converter/other/pamtosvg/pxl-outline.h
@@ -0,0 +1,79 @@
+/* pxl-outline.h: find a list of outlines which make up one character. */
+
+#ifndef PXL_OUTLINE_H
+#define PXL_OUTLINE_H
+
+#include "ppm.h"
+
+#include "autotrace.h"
+#include "exception.h"
+#include "bitmap.h"
+
+/* This is a list of contiguous points on the bitmap.  */
+typedef struct
+{
+  pm_pixelcoord * data;
+  unsigned length;
+  bool clockwise;
+  pixel color;
+  bool open;
+} pixel_outline_type;
+
+
+/* The Nth coordinate in the list.  */
+#define O_COORDINATE(p_o, n) ((p_o).data[n])
+
+
+/* The length of the list.  */
+#define O_LENGTH(p_o) ((p_o).length)
+
+/* Whether the outline moves clockwise or counterclockwise.  */
+#define O_CLOCKWISE(p_o) ((p_o).clockwise)
+
+/* Since a pixel outline is cyclic, the index of the next coordinate
+   after the last is the first, and the previous coordinate before the
+   first is the last.  */
+#define O_NEXT(p_o, n) (((n) + 1) % O_LENGTH (p_o))
+#define O_PREV(p_o, n) ((n) == 0				\
+                         ? O_LENGTH (p_o) - 1			\
+                         : (n) - 1)
+
+/* And the character turns into a list of such lists.  */
+typedef struct
+{
+  pixel_outline_type *data;
+  unsigned length;
+} pixel_outline_list_type;
+
+/* The Nth list in the list of lists.  */
+#define O_LIST_OUTLINE(p_o_l, n) ((p_o_l).data[n])
+
+/* The length of the list of lists.  */
+#define O_LIST_LENGTH(p_o_l) ((p_o_l).length)
+
+/* Find all pixels on the outline in the character C.  */
+pixel_outline_list_type
+find_outline_pixels (bitmap_type         const type,
+                     bool                const bg_spec,
+                     pixel               const bg_color, 
+                     at_progress_func          notify_progress,
+                     void *              const progress_data,
+                     at_testcancel_func        test_cancel,
+                     void *              const testcancel_data,
+                     at_exception_type * const exp);
+
+/* Find all pixels on the center line of the character C.  */
+pixel_outline_list_type
+find_centerline_pixels (bitmap_type         const type,
+                        pixel               const bg_color, 
+                        at_progress_func          notify_progress,
+                        void *              const progress_data,
+                        at_testcancel_func        test_cancel,
+                        void *              const testcancel_data,
+                        at_exception_type * const exp);
+
+/* Free the memory in the list.  */
+extern void
+free_pixel_outline_list (pixel_outline_list_type *);
+
+#endif /* not PXL_OUTLINE_H */
diff --git a/converter/other/pamtosvg/spline.c b/converter/other/pamtosvg/spline.c
new file mode 100644
index 00000000..5bdf0c0e
--- /dev/null
+++ b/converter/other/pamtosvg/spline.c
@@ -0,0 +1,193 @@
+/* spline.c: spline and spline list (represented as arrays) manipulation. */
+
+#include <assert.h>
+
+#include "mallocvar.h"
+
+#include "message.h"
+#include "point.h"
+#include "spline.h"
+#include "vector.h"
+
+/* Print a spline in human-readable form.  */
+
+void
+print_spline (FILE *f, spline_type s)
+{
+  assert(SPLINE_DEGREE (s) == LINEARTYPE || SPLINE_DEGREE (s) == CUBICTYPE);
+
+  if (SPLINE_DEGREE (s) == LINEARTYPE)
+    fprintf (f, "(%.3f,%.3f)--(%.3f,%.3f).\n",
+                START_POINT (s).x, START_POINT (s).y,
+                END_POINT (s).x, END_POINT (s).y);
+
+  else if (SPLINE_DEGREE (s) == CUBICTYPE)
+    fprintf (f, "(%.3f,%.3f)..ctrls(%.3f,%.3f)&(%.3f,%.3f)..(%.3f,%.3f).\n",
+                START_POINT (s).x, START_POINT (s).y,
+                CONTROL1 (s).x, CONTROL1 (s).y,
+                CONTROL2 (s).x, CONTROL2 (s).y,
+                END_POINT (s).x, END_POINT (s).y);
+}
+
+
+/* Evaluate the spline S at a given T value.  This is an implementation
+   of de Casteljau's algorithm.  See Schneider's thesis, p.37.
+   The variable names are taken from there.  */
+
+float_coord
+evaluate_spline (spline_type s, float t)
+{
+  spline_type V[4];    /* We need degree+1 splines, but assert degree <= 3.  */
+  signed i, j;
+  float one_minus_t = (float) 1.0 - t;
+  polynomial_degree degree = SPLINE_DEGREE (s);
+
+  for (i = 0; i <= degree; i++)
+    {
+      V[0].v[i].x = s.v[i].x;
+      V[0].v[i].y = s.v[i].y;
+      V[0].v[i].z = s.v[i].z;
+    }
+
+  for (j = 1; j <= degree; j++)
+    for (i = 0; i <= degree - j; i++)
+      {
+        float_coord t1 = Pmult_scalar (V[j - 1].v[i], one_minus_t);
+        float_coord t2 = Pmult_scalar (V[j - 1].v[i + 1], t);
+        float_coord temp = Padd (t1, t2);
+        V[j].v[i].x = temp.x;
+        V[j].v[i].y = temp.y;
+        V[j].v[i].z = temp.z;
+      }
+
+  return V[degree].v[0];
+}
+
+
+/* Return a new, empty, spline list.  */
+
+spline_list_type *
+new_spline_list (void)
+{
+  spline_list_type *answer;
+
+  MALLOCVAR(answer);
+  *answer = empty_spline_list();
+  return answer;
+}
+
+spline_list_type 
+empty_spline_list (void)
+{
+  spline_list_type answer;
+  SPLINE_LIST_DATA (answer) = NULL;
+  SPLINE_LIST_LENGTH (answer) = 0;
+  return answer;
+}
+
+/* Return a new spline list with SPLINE as the first element.  */
+
+spline_list_type *
+new_spline_list_with_spline (spline_type spline)
+{
+  spline_list_type *answer;
+
+  answer = new_spline_list();
+  MALLOCVAR(SPLINE_LIST_DATA(*answer));
+  SPLINE_LIST_ELT (*answer, 0) = spline;
+  SPLINE_LIST_LENGTH (*answer) = 1;
+
+  return answer;
+}
+
+
+/* Free the storage in a spline list.  We don't have to free the
+   elements, since they are arrays in automatic storage.  And we don't
+   want to free the list if it was empty.  */
+
+void
+free_spline_list (spline_list_type spline_list)
+{
+  if (SPLINE_LIST_DATA (spline_list) != NULL)
+    free (SPLINE_LIST_DATA (spline_list));
+}
+
+
+/* Append the spline S to the list SPLINE_LIST.  */
+
+void
+append_spline (spline_list_type *l, spline_type s)
+{
+  assert (l != NULL);
+
+  SPLINE_LIST_LENGTH (*l)++;
+  REALLOCARRAY(SPLINE_LIST_DATA(*l), SPLINE_LIST_LENGTH(*l));
+  LAST_SPLINE_LIST_ELT (*l) = s;
+}
+
+
+/* Tack the elements in the list S2 onto the end of S1.
+   S2 is not changed.  */
+
+void
+concat_spline_lists (spline_list_type *s1, spline_list_type s2)
+{
+  unsigned this_spline;
+  unsigned new_length;
+
+  assert (s1 != NULL);
+
+  new_length = SPLINE_LIST_LENGTH (*s1) + SPLINE_LIST_LENGTH (s2);
+
+  REALLOCARRAY_NOFAIL(SPLINE_LIST_DATA(*s1), new_length);
+
+  for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (s2); this_spline++)
+    SPLINE_LIST_ELT (*s1, SPLINE_LIST_LENGTH (*s1)++)
+      = SPLINE_LIST_ELT (s2, this_spline);
+}
+
+
+/* Return a new, empty, spline list array.  */
+
+spline_list_array_type
+new_spline_list_array (void)
+{
+  spline_list_array_type answer;
+
+  SPLINE_LIST_ARRAY_DATA (answer) = NULL;
+  SPLINE_LIST_ARRAY_LENGTH (answer) = 0;
+
+  return answer;
+}
+
+
+/* Free the storage in a spline list array.  We don't
+   want to free the list if it is empty.  */
+void
+free_spline_list_array (spline_list_array_type *spline_list_array)
+{
+  unsigned this_list;
+
+  for (this_list = 0;
+       this_list < SPLINE_LIST_ARRAY_LENGTH (*spline_list_array);
+       this_list++)
+    free_spline_list (SPLINE_LIST_ARRAY_ELT (*spline_list_array, this_list));
+
+  if (SPLINE_LIST_ARRAY_DATA (*spline_list_array) != NULL)
+    free (SPLINE_LIST_ARRAY_DATA (*spline_list_array));
+
+  flush_log_output ();
+}
+
+
+/* Append the spline S to the list SPLINE_LIST_ARRAY.  */
+
+void
+append_spline_list (spline_list_array_type *l, spline_list_type s)
+{
+  SPLINE_LIST_ARRAY_LENGTH (*l)++;
+  REALLOCARRAY_NOFAIL(SPLINE_LIST_ARRAY_DATA(*l),
+                      SPLINE_LIST_ARRAY_LENGTH(*l));
+  LAST_SPLINE_LIST_ARRAY_ELT (*l) = s;
+}
+
diff --git a/converter/other/pamtosvg/spline.h b/converter/other/pamtosvg/spline.h
new file mode 100644
index 00000000..05a56e23
--- /dev/null
+++ b/converter/other/pamtosvg/spline.h
@@ -0,0 +1,90 @@
+/* spline.h: manipulate the spline representation.  */
+
+#ifndef SPLINE_H
+#define SPLINE_H
+
+#include <stdio.h>
+
+#include "point.h"
+#include "autotrace.h"
+
+typedef at_polynomial_degree polynomial_degree;
+typedef at_spline_type spline_type;
+
+#define LINEARTYPE          AT_LINEARTYPE
+#define QUADRATICTYPE       AT_QUADRATICTYPE
+#define CUBICTYPE           AT_CUBICTYPE
+#define PARALLELELLIPSETYPE AT_PARALLELELLIPSETYPE
+#define ELLIPSETYPE         AT_ELLIPSETYPE
+#define CIRCLETYPE          AT_CIRCLETYPE
+
+#define START_POINT(spl)        ((spl).v[0])
+#define CONTROL1(spl)           ((spl).v[1])
+#define CONTROL2(spl)           ((spl).v[2])
+#define END_POINT(spl)          ((spl).v[3])
+#define SPLINE_DEGREE(spl)      ((spl).degree)
+#define SPLINE_LINEARITY(spl)   ((spl).linearity)
+
+#ifndef _IMPORTING
+/* Print a spline on the given file.  */
+extern void print_spline (FILE *, spline_type);
+
+/* Evaluate SPLINE at the given T value.  */
+extern float_coord evaluate_spline (spline_type spline, float t);
+#endif
+
+/* Each outline in a character is typically represented by many
+   splines.  So, here is a list structure for that:  */
+typedef at_spline_list_type spline_list_type;
+
+
+/* An empty list will have length zero (and null data).  */
+#define SPLINE_LIST_LENGTH(spll)  ((spll).length)
+
+/* The address of the beginning of the array of data.  */
+#define SPLINE_LIST_DATA(spll)    ((spll).data)
+
+/* The element with index 'index' in S_L.  */
+#define SPLINE_LIST_ELT(spll, index) ((spll).data[index])
+
+/* The last element in S_L.  */
+#define LAST_SPLINE_LIST_ELT(s_l) \
+  (SPLINE_LIST_DATA (s_l)[SPLINE_LIST_LENGTH (s_l) - 1])
+
+/* The previous and next elements to INDEX in S_L.  */
+#define NEXT_SPLINE_LIST_ELT(s_l, index)                \
+  SPLINE_LIST_ELT (s_l, ((index) + 1) % SPLINE_LIST_LENGTH (s_l))
+#define PREV_SPLINE_LIST_ELT(s_l, index)                \
+  SPLINE_LIST_ELT (s_l, index == 0                  \
+                        ? SPLINE_LIST_LENGTH (s_l) - 1          \
+                        : index - 1)
+
+#ifndef _IMPORTING
+/* Construct and destroy new `spline_list_type' objects.  */
+extern spline_list_type *new_spline_list (void); /* Allocate new memory */
+extern spline_list_type empty_spline_list (void); /* No allocation */
+extern spline_list_type *new_spline_list_with_spline (spline_type);
+extern void free_spline_list (spline_list_type);
+
+/* Append the spline S to the list S_LIST.  */
+extern void append_spline (spline_list_type *s_list, spline_type s);
+
+/* Append the elements in list S2 to S1, changing S1.  */
+extern void concat_spline_lists (spline_list_type *s1, spline_list_type s2);
+#endif
+
+typedef at_spline_list_array_type spline_list_array_type;
+
+/* Turns out we can use the same definitions for lists of lists as for
+   just lists.  But we define the usual names, just in case.  */
+#define SPLINE_LIST_ARRAY_LENGTH(spll) ((spll).length)
+#define SPLINE_LIST_ARRAY_DATA     SPLINE_LIST_DATA
+#define SPLINE_LIST_ARRAY_ELT(spll, index) ((spll).data[index])
+#define LAST_SPLINE_LIST_ARRAY_ELT LAST_SPLINE_LIST_ELT
+
+extern spline_list_array_type new_spline_list_array (void);
+extern void append_spline_list (spline_list_array_type *, spline_list_type);
+extern void free_spline_list_array (spline_list_array_type *);
+
+#endif /* not SPLINE_H */
+
diff --git a/converter/other/pamtosvg/testgrid.svg b/converter/other/pamtosvg/testgrid.svg
new file mode 100644
index 00000000..e82c5551
--- /dev/null
+++ b/converter/other/pamtosvg/testgrid.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" standalone="yes"?>
+<svg width="14" height="16">
+<path style="fill:#000000; stroke:none;" d="M0 0L0 16L14 16L14 1L13 0L0 0z"/>
+<path style="fill:#ffffff; stroke:none;" d="M1 0L2 1L1 0M3 0L4 1L3 0M5 0L6 1L5 0M7 0L8 1L7 0M9 0L10 1L9 0M11 0L12 1L11 0M13 0L14 1L13 0M1 2L2 3L1 2M3 2L4 3L3 2M5 2L6 3L5 2M7 2L8 3L7 2M9 2L10 3L9 2M11 2L12 3L11 2M13 2L14 3L13 2M1 4L2 5L1 4M3 4L4 5L3 4M5 4L6 5L5 4M7 4L8 5L7 4M9 4L10 5L9 4M11 4L12 5L11 4M13 4L14 5L13 4M1 6L2 7L1 6M3 6L4 7L3 6M5 6L6 7L5 6M7 6L8 7L7 6M9 6L10 7L9 6M11 6L12 7L11 6M13 6L14 7L13 6M1 8L2 9L1 8M3 8L4 9L3 8M5 8L6 9L5 8M7 8L8 9L7 8M9 8L10 9L9 8M11 8L12 9L11 8M13 8L14 9L13 8M1 10L2 11L1 10M3 10L4 11L3 10M5 10L6 11L5 10M7 10L8 11L7 10M9 10L10 11L9 10M11 10L12 11L11 10M13 10L14 11L13 10M1 12L2 13L1 12M3 12L4 13L3 12M5 12L6 13L5 12M7 12L8 13L7 12M9 12L10 13L9 12M11 12L12 13L11 12M13 12L14 13L13 12M1 14L2 15L1 14M3 14L4 15L3 14M5 14L6 15L5 14M7 14L8 15L7 14M9 14L10 15L9 14M11 14L12 15L11 14M13 14L14 15L13 14z"/>
+</svg>
diff --git a/converter/other/pamtosvg/testline.svg b/converter/other/pamtosvg/testline.svg
new file mode 100644
index 00000000..e704ce74
--- /dev/null
+++ b/converter/other/pamtosvg/testline.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" standalone="yes"?>
+<svg width="20" height="20">
+<path style="fill:#000000; stroke:none;" d="M0 0L0 20L20 20L20 0L0 0z"/>
+<path style="fill:#ffffff; stroke:none;" d="M5 2L15 18L16 18L5 2z"/>
+</svg>
diff --git a/converter/other/pamtosvg/thin-image.c b/converter/other/pamtosvg/thin-image.c
new file mode 100644
index 00000000..40ced794
--- /dev/null
+++ b/converter/other/pamtosvg/thin-image.c
@@ -0,0 +1,373 @@
+/* thin-image.c: thin binary image
+
+   Copyright (C) 2001, 2002 Martin Weber
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License
+   as published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+   USA. */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "mallocvar.h"
+
+#include "thin-image.h"
+#include "logreport.h"
+#include "message.h"
+#include "bitmap.h"
+ 
+#define PIXEL_SET(p, new)  ((void)memcpy((p), (new), sizeof(Pixel)))
+#define PIXEL_EQUAL(p1, p2) \
+    ((p1)[0] == (p2)[0] && (p1)[1] == (p2)[1] && (p1)[2] == (p2)[2])
+
+ 
+typedef unsigned char Pixel[3];  /* RGB pixel data type */ 
+
+ 
+void thin3(bitmap_type *image, Pixel colour); 
+void thin1(bitmap_type *image, unsigned char colour); 
+ 
+ 
+/* -------------------------------- ThinImage - Thin binary image. --------------------------- * 
+ *                                                            
+ *    Description:                                                    
+ *        Thins the supplied binary image using Rosenfeld's parallel   
+ *        thinning algorithm.                                         
+ *                                                                     
+ *    On Entry:                                                        
+ *        image = Image to thin.                                       
+ *                                                                     
+ * -------------------------------------------------------------------------------------------- */ 
+ 
+ 
+/* Direction masks:                  */ 
+/*   N     S     W        E            */ 
+static        unsigned int     masks[]         = { 0200, 0002, 0040, 0010 }; 
+ 
+/*    True if pixel neighbor map indicates the pixel is 8-simple and  */ 
+/*    not an end point and thus can be deleted.  The neighborhood     */ 
+/*    map is defined as an integer of bits abcdefghi with a non-zero  */ 
+/*    bit representing a non-zero pixel.  The bit assignment for the  */ 
+/*    neighborhood is:                                                */ 
+/*                                                                    */ 
+/*                            a b c                                   */ 
+/*                            d e f                                   */ 
+/*                            g h i                                   */ 
+ 
+static        unsigned char   todelete[512] = { 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 
+              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+              1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 
+
+static pixel background;
+
+
+void
+thin_image(bitmap_type *image, bool bgSpec, pixel bg,
+           at_exception_type * exp)
+{ 
+    /* This is nasty as we need to call thin once for each  
+     * colour in the image the way I do this is to keep a second  
+     * copy of the bitmap and to use this to keep 
+     * track of which colours have not yet been processed, 
+     * trades time for pathological case memory.....*/ 
+    long m, n, num_pixels;
+    bitmap_type bm; 
+    unsigned int const spp = image->np;
+	unsigned int const width = image->width;
+	unsigned int const height = image->height;
+
+    if (bgSpec)
+        background = bg;
+    else 
+        PPM_ASSIGN(background, 255, 255, 255);
+
+    /* Clone the image */
+    bm.height = image->height;
+    bm.width = image->width;
+    bm.np = image->np;
+    MALLOCARRAY(bm.bitmap, height * width * spp); 
+    if (bm.bitmap == NULL)
+        pm_error("Unable to get memory for thin image bitmap clone");
+    memcpy(bm.bitmap, image->bitmap, height * width * spp); 
+
+    num_pixels = height * width;
+    switch (spp)
+    {
+	case 3:
+	{
+	    Pixel *ptr = (Pixel*)bm.bitmap;
+	    Pixel bg_color;
+	    bg_color[0] = PPM_GETR(background);
+	    bg_color[1] = PPM_GETG(background);
+	    bg_color[2] = PPM_GETB(background);
+
+	    for (n = num_pixels - 1; n >= 0L; --n)
+	    {
+		Pixel p;
+
+		PIXEL_SET(p, ptr[n]);
+		if (!PIXEL_EQUAL(p, bg_color))
+		{ 
+		    /* we have a new colour in the image */ 
+		    LOG3("Thinning colour (%x, %x, %x)\n", p[0], p[1], p[2]);
+		    for (m = n - 1; m >= 0L; --m)
+		    {
+			if (PIXEL_EQUAL(ptr[m], p))
+			    PIXEL_SET(ptr[m], bg_color);
+		    }
+		    thin3(image, p); 
+		} 
+	    } 
+	    break;
+	} 
+
+	case 1:
+	{
+	    unsigned char * const ptr = bm.bitmap;
+	    unsigned char bg_color;
+
+	    if (PPM_ISGRAY(background))
+            bg_color = PPM_GETR(background);
+	    else
+            bg_color = PPM_LUMIN(background);
+
+	    for (n = num_pixels - 1; n >= 0L; --n)
+	    {
+		unsigned char c = ptr[n];
+		if (c != bg_color)
+		{ 
+		    LOG1 ("Thinning colour %x\n", c);
+		    for (m = n - 1; m >= 0L; --m)
+			if (ptr[m] == c) ptr[m] = bg_color;
+		    thin1(image, c); 
+		} 
+	    } 
+	    break;
+	} 
+
+	default:
+	{
+	  LOG1 ("thin_image: %u-plane images are not supported", spp);
+	  at_exception_fatal(exp, "thin_image: wrong plane images are passed");
+	  goto cleanup;
+	}
+    }
+ cleanup:
+    free (bm.bitmap); 
+} 
+
+ 
+void thin3(bitmap_type *image, Pixel colour) 
+{ 
+      Pixel *ptr, *y_ptr, *y1_ptr;
+      Pixel bg_color;
+      unsigned int    xsize, ysize;   /* Image resolution             */ 
+      unsigned int    x, y;           /* Pixel location               */ 
+      unsigned int    i;              /* Pass index           */ 
+      unsigned int    pc      = 0;    /* Pass count           */ 
+      unsigned int    count   = 1;    /* Deleted pixel count          */ 
+      unsigned int    p, q;           /* Neighborhood maps of adjacent*/ 
+                                      /* cells                        */ 
+      unsigned char   *qb;            /* Neighborhood maps of previous*/ 
+                                      /* scanline                     */ 
+      unsigned int    m;              /* Deletion direction mask      */ 
+ 
+      bg_color[0] = PPM_GETR(background);
+      bg_color[1] = PPM_GETG(background);
+      bg_color[2] = PPM_GETB(background);
+
+      LOG (" Thinning image.....\n "); 
+      xsize = image->width;
+      ysize = image->height;
+      MALLOCARRAY_NOFAIL(qb, xsize); 
+      qb[xsize-1] = 0;                /* Used for lower-right pixel   */ 
+      ptr = (Pixel*)image->bitmap;
+ 
+      while ( count ) {               /* Scan image while deletions   */ 
+          pc++; 
+          count = 0; 
+ 
+          for ( i = 0 ; i < 4 ; i++ ) { 
+ 
+              m = masks[i]; 
+ 
+              /* Build initial previous scan buffer.                  */ 
+              p = PIXEL_EQUAL(ptr[0], colour); 
+              for ( x = 0 ; x < xsize-1 ; x++ ) 
+                  qb[x] = (unsigned char) (p = ((p<<1)&0006) | (unsigned int) PIXEL_EQUAL(ptr[x+1],
+				   colour)); 
+ 
+              /* Scan image for pixel deletion candidates.            */ 
+	      y_ptr = ptr; y1_ptr = ptr + xsize; 
+              for (y = 0; y < ysize - 1; y++, y_ptr += xsize, y1_ptr += xsize)
+	      { 
+                  q = qb[0]; 
+                  p = ((q<<2)&0330) | (unsigned int) PIXEL_EQUAL(y1_ptr[0], colour); 
+ 
+                  for ( x = 0 ; x < xsize-1 ; x++ ) { 
+                      q = qb[x]; 
+                      p = ((p<<1)&0666) | ((q<<3)&0110) | 
+			  (unsigned int) PIXEL_EQUAL(y1_ptr[x+1], colour);
+                      qb[x] = (unsigned char) p; 
+                      if ((i != 2 || x != 0) && ((p&m) == 0) && todelete[p] ) { 
+                          count++;  /* delete the pixel */ 
+			  PIXEL_SET(y_ptr[x], bg_color);
+                      } 
+                  } 
+ 
+                  /* Process right edge pixel.                        */ 
+                  p = (p<<1)&0666; 
+                  if  (i != 3 && (p&m) == 0 && todelete[p] ) { 
+                      count++; 
+		      PIXEL_SET(y_ptr[xsize-1], bg_color);
+                  } 
+              } 
+ 
+	      if (i != 1)
+	      {
+            /* Process bottom scan line.                            */ 
+            q = qb[0]; 
+            p = ((q<<2)&0330); 
+
+            y_ptr = ptr + xsize * (ysize - 1);
+            for ( x = 0 ; x < xsize ; x++ ) { 
+              q = qb[x]; 
+              p = ((p<<1)&0666) | ((q<<3)&0110); 
+              if ((i != 2 || x != 0) && (p&m) == 0 && todelete[p]) { 
+                count++; 
+                PIXEL_SET(y_ptr[x], bg_color);
+		      } 
+            } 
+           }
+          } 
+          LOG2 ("ThinImage: pass %d, %d pixels deleted\n", pc, count); 
+      } 
+      free (qb); 
+} 
+
+ 
+void thin1(bitmap_type *image, unsigned char colour) 
+{ 
+      unsigned char *ptr, *y_ptr, *y1_ptr;
+      unsigned char bg_color;
+      unsigned int    xsize, ysize;   /* Image resolution             */ 
+      unsigned int    x, y;           /* Pixel location               */ 
+      unsigned int    i;              /* Pass index           */ 
+      unsigned int    pc      = 0;    /* Pass count           */ 
+      unsigned int    count   = 1;    /* Deleted pixel count          */ 
+      unsigned int    p, q;           /* Neighborhood maps of adjacent*/ 
+                                      /* cells                        */ 
+      unsigned char   *qb;            /* Neighborhood maps of previous*/ 
+                                      /* scanline                     */ 
+      unsigned int    m;              /* Deletion direction mask      */ 
+
+      if (PPM_ISGRAY(background))
+          bg_color = PPM_GETR(background);
+      else
+          bg_color = PPM_LUMIN(background);
+
+      LOG (" Thinning image.....\n "); 
+      xsize = image->width;
+      ysize = image->height;
+      MALLOCARRAY_NOFAIL(qb, xsize); 
+      qb[xsize-1] = 0;                /* Used for lower-right pixel   */ 
+      ptr = image->bitmap;
+ 
+      while ( count ) {               /* Scan image while deletions   */ 
+          pc++; 
+          count = 0; 
+ 
+          for ( i = 0 ; i < 4 ; i++ ) { 
+ 
+              m = masks[i]; 
+ 
+              /* Build initial previous scan buffer.                  */ 
+              p = (ptr[0] == colour); 
+              for ( x = 0 ; x < xsize-1 ; x++ ) 
+                  qb[x] = (unsigned char) (p = ((p<<1)&0006) | (unsigned int)(ptr[x+1] == colour)); 
+ 
+              /* Scan image for pixel deletion candidates.            */ 
+	      y_ptr = ptr; y1_ptr = ptr + xsize; 
+              for (y = 0; y < ysize - 1; y++, y_ptr += xsize, y1_ptr += xsize)
+	      { 
+                  q = qb[0]; 
+                  p = ((q<<2)&0330) | (y1_ptr[0] == colour); 
+ 
+                  for ( x = 0 ; x < xsize-1 ; x++ ) { 
+                      q = qb[x]; 
+                      p = ((p<<1)&0666) | ((q<<3)&0110) | (unsigned int) (y1_ptr[x+1]==colour); 
+                      qb[x] = (unsigned char) p; 
+                      if  ( ((p&m) == 0) && todelete[p] ) { 
+                          count++; 
+			  y_ptr[x] = bg_color;  /* delete the pixel */ 
+                      } 
+                  } 
+ 
+                  /* Process right edge pixel.                        */ 
+                  p = (p<<1)&0666; 
+                  if  ( (p&m) == 0 && todelete[p] ) { 
+                      count++; 
+                      y_ptr[xsize-1] = bg_color;
+                  } 
+              } 
+ 
+              /* Process bottom scan line.                            */ 
+	      q = qb[0]; 
+	      p = ((q<<2)&0330); 
+ 
+	      y_ptr = ptr + xsize * (ysize - 1);
+              for ( x = 0 ; x < xsize ; x++ ) { 
+                  q = qb[x]; 
+                  p = ((p<<1)&0666) | ((q<<3)&0110); 
+                  if  ( (p&m) == 0 && todelete[p] ) { 
+                      count++; 
+                      y_ptr[x] = bg_color;
+                  } 
+              } 
+          } 
+          LOG2("thin1: pass %d, %d pixels deleted\n", pc, count); 
+      } 
+      free (qb); 
+} 
diff --git a/converter/other/pamtosvg/thin-image.h b/converter/other/pamtosvg/thin-image.h
new file mode 100644
index 00000000..4e0a77ee
--- /dev/null
+++ b/converter/other/pamtosvg/thin-image.h
@@ -0,0 +1,37 @@
+/* thin-image.h: thin binary image
+
+   Copyright (C) 2001, 2002 Martin Weber
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License
+   as published by the Free Software Foundation; either version 2.1 of
+   the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+   USA. */
+
+#ifndef THIN_IMAGE_H
+#define THIN_IMAGE_H
+
+/*
+ * C code from the article
+ * "Efficient Binary Image Thinning using Neighborhood Maps"
+ * by Joseph M. Cychosz, 3ksnn64@ecn.purdue.edu
+ * in "Graphics Gems IV", Academic Press, 1994
+ */
+
+#include "bitmap.h"
+#include "exception.h"
+
+void
+thin_image(bitmap_type *image, bool bg_spec, pixel bg_color,
+           at_exception_type * exp);
+
+#endif /* not THIN_IMAGE_H */
diff --git a/converter/other/pamtosvg/vector.c b/converter/other/pamtosvg/vector.c
new file mode 100644
index 00000000..559163fc
--- /dev/null
+++ b/converter/other/pamtosvg/vector.c
@@ -0,0 +1,254 @@
+/* vector.c: vector/point operations. */
+
+#include <math.h>
+#include <errno.h>
+#include <assert.h>
+#include <string.h>
+
+#include "pm_c_util.h"
+
+#include "vector.h"
+#include "message.h"
+#include "epsilon-equal.h"
+
+static float acos_d (float, at_exception_type * excep);
+
+
+/* Given the point COORD, return the corresponding vector.  */
+
+vector_type
+make_vector (const float_coord c)
+{
+  vector_type v;
+
+  v.dx = c.x;
+  v.dy = c.y;
+  v.dz = c.z;
+
+  return v;
+}
+
+
+/* And the converse: given a vector, return the corresponding point.  */
+
+float_coord
+vector_to_point (const vector_type v)
+{
+  float_coord coord;
+
+  coord.x = v.dx;
+  coord.y = v.dy;
+
+  return coord;
+}
+
+
+float
+magnitude (const vector_type v)
+{
+  return (float) sqrt (v.dx * v.dx + v.dy * v.dy + v.dz * v.dz);
+}
+
+
+vector_type
+normalize (const vector_type v)
+{
+  vector_type new_v;
+  float m = magnitude (v);
+
+  /* assert (m > 0.0); */
+
+  if (m > 0.0)
+  {
+    new_v.dx = v.dx / m;
+    new_v.dy = v.dy / m;
+    new_v.dz = v.dz / m;
+  }
+  else
+  {
+	new_v.dx = v.dx;
+    new_v.dy = v.dy;
+    new_v.dz = v.dz;
+  }
+
+  return new_v;
+}
+
+
+vector_type
+Vadd (const vector_type v1, const vector_type v2)
+{
+  vector_type new_v;
+
+  new_v.dx = v1.dx + v2.dx;
+  new_v.dy = v1.dy + v2.dy;
+  new_v.dz = v1.dz + v2.dz;
+
+  return new_v;
+}
+
+
+float
+Vdot (const vector_type v1, const vector_type v2)
+{
+  return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz;
+}
+
+
+vector_type
+Vmult_scalar (const vector_type v, const float r)
+{
+  vector_type new_v;
+
+  new_v.dx = v.dx * r;
+  new_v.dy = v.dy * r;
+  new_v.dz = v.dz * r;
+
+  return new_v;
+}
+
+
+/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
+   degrees, in the range zero to 180.  */
+
+float
+Vangle (const vector_type in_vector, 
+	const vector_type out_vector,
+	at_exception_type * exp)
+{
+  vector_type v1 = normalize (in_vector);
+  vector_type v2 = normalize (out_vector);
+
+  return acos_d (Vdot (v2, v1), exp);
+}
+
+
+float_coord
+Vadd_point (const float_coord c, const vector_type v)
+{
+  float_coord new_c;
+
+  new_c.x = c.x + v.dx;
+  new_c.y = c.y + v.dy;
+  new_c.z = c.z + v.dz;
+  return new_c;
+}
+
+
+float_coord
+Vsubtract_point (const float_coord c, const vector_type v)
+{
+  float_coord new_c;
+
+  new_c.x = c.x - v.dx;
+  new_c.y = c.y - v.dy;
+  new_c.z = c.z - v.dz;
+  return new_c;
+}
+
+
+pm_pixelcoord
+Vadd_int_point(pm_pixelcoord const c,
+               vector_type   const v) {
+
+    pm_pixelcoord a;
+
+    a.col = ROUND ((float) c.col + v.dx);
+    a.row = ROUND ((float) c.row + v.dy);
+    
+    return a;
+}
+
+
+vector_type
+Vabs (const vector_type v)
+{
+  vector_type new_v;
+
+  new_v.dx = (float) fabs (v.dx);
+  new_v.dy = (float) fabs (v.dy);
+  new_v.dz = (float) fabs (v.dz);
+  return new_v;
+}
+
+
+/* Operations on points.  */
+
+float_coord
+Padd (const float_coord coord1, const float_coord coord2)
+{
+  float_coord sum;
+
+  sum.x = coord1.x + coord2.x;
+  sum.y = coord1.y + coord2.y;
+  sum.z = coord1.z + coord2.z;
+
+  return sum;
+}
+
+
+float_coord
+Pmult_scalar (const float_coord coord, const float r)
+{
+  float_coord answer;
+
+  answer.x = coord.x * r;
+  answer.y = coord.y * r;
+  answer.z = coord.z * r;
+
+  return answer;
+}
+
+
+vector_type
+Psubtract (const float_coord c1, const float_coord c2)
+{
+  vector_type v;
+
+  v.dx = c1.x - c2.x;
+  v.dy = c1.y - c2.y;
+  v.dz = c1.z - c2.z;
+
+  return v;
+}
+
+
+
+/* Operations on integer points.  */
+
+vector_type
+IPsubtract(pm_pixelcoord const coord1,
+           pm_pixelcoord const coord2) {
+
+    vector_type v;
+
+    v.dx = (int) (coord1.col - coord2.col);
+    v.dy = (int) (coord1.row - coord2.row);
+    v.dz = 0.0;
+    
+    return v;
+}
+
+
+
+static float
+acos_d (float v, at_exception_type * excep)
+{
+  float a;
+
+  if (epsilon_equal (v, 1.0))
+    v = 1.0;
+  else if (epsilon_equal (v, -1.0))
+    v = -1.0;
+
+  errno = 0;
+  a = (float) acos (v);
+  if (errno == ERANGE || errno == EDOM)
+    {
+      at_exception_fatal(excep, strerror(errno));
+      return 0.0;
+    }
+  
+  
+  return a * (float) 180.0 / (float) M_PI;
+}
diff --git a/converter/other/pamtosvg/vector.h b/converter/other/pamtosvg/vector.h
new file mode 100644
index 00000000..74fb2fd2
--- /dev/null
+++ b/converter/other/pamtosvg/vector.h
@@ -0,0 +1,71 @@
+/* vector.h: operations on vectors and points. */
+
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include "point.h"
+#include "exception.h"
+
+/* Our vectors are represented as displacements along the x and y axes.  */
+
+typedef struct
+{
+  float dx, dy, dz;
+} vector_type;
+
+
+/* Consider a point as a vector from the origin.  */
+extern vector_type make_vector (const float_coord);
+
+/* And a vector as a point, i.e., a displacement from the origin.  */
+extern float_coord vector_to_point (const vector_type);
+
+
+/* Definitions for these common operations can be found in any decent
+   linear algebra book, and most calculus books.  */
+
+extern float magnitude (const vector_type);
+extern vector_type normalize (const vector_type);
+
+extern vector_type Vadd (const vector_type, const vector_type);
+extern float Vdot (const vector_type, const vector_type);
+extern vector_type Vmult_scalar (const vector_type, const float);
+extern float Vangle (const vector_type in, const vector_type out, at_exception_type * exp);
+
+/* These operations could have been named `P..._vector' just as well as
+   V..._point, so we may as well allow both names.  */
+#define Padd_vector Vadd_point
+extern float_coord Vadd_point
+  (const float_coord, const vector_type);
+
+#define Psubtract_vector Vsubtract_point
+extern float_coord Vsubtract_point
+  (const float_coord, const vector_type);
+
+/* This returns the rounded sum.  */
+#define IPadd_vector Vadd_int_point
+
+pm_pixelcoord
+Vadd_int_point(pm_pixelcoord const c,
+               vector_type   const v);
+
+/* Take the absolute value of both components.  */
+extern vector_type Vabs (const vector_type);
+
+/* Operations on points with real coordinates.  It is not orthogonal,
+   but more convenient, to have the subtraction operator return a
+   vector, and the addition operator return a point.  */
+extern vector_type Psubtract
+  (const float_coord, const float_coord);
+
+vector_type
+IPsubtract(pm_pixelcoord const coord1,
+           pm_pixelcoord const coord2);
+
+/* These are heavily used in spline fitting.  */
+extern float_coord Padd (const float_coord,
+                                  const float_coord);
+extern float_coord Pmult_scalar (const float_coord, const float);
+
+#endif
+