Warning, /graphics/glaxnimate/external/potrace/doc/potracelib.tex is written in an unsupported language. File is not indexed.

0001 % Copyright (C) 2001-2019 Peter Selinger.
0002 % This file is part of Potrace. It is licensed under the GNU General
0003 % Public License. See the file COPYING for details.
0004 
0005 \documentclass{article}
0006 \usepackage{times}
0007 \usepackage{graphics}
0008 
0009 \title{Potrace Library API}
0010 \author{}
0011 \date{}
0012 
0013 % Alphabetic labels
0014 {\makeatletter
0015 \gdef\alphalabels{\def\theenumi{\@alph\c@enumi}\def\labelenumi{(\theenumi)}}}
0016 
0017 \begin{document}
0018 \maketitle
0019 
0020 \noindent {\it Copyright \copyright\ 2001-2019 Peter Selinger.  
0021   This file is part of Potrace. It is licensed under the GNU General
0022   Public License. See the file COPYING for details. }
0023 
0024 \section{Scope}
0025 
0026 The Potrace library provides:
0027 
0028 \begin{itemize}
0029 \item tracing, i.e., conversion of bitmaps to a vector representation
0030   (Bezier curves and straight line segments).
0031 \end{itemize}
0032 It does not provide frontend functionality such as:
0033 
0034 \begin{itemize}
0035 \item preparation of bitmaps (e.g.\ reading a bitmap from a file, preparing
0036   a bitmap by thresholding/scaling/filtering a greyscale image etc)
0037 \end{itemize}
0038 And it does not provide backend functionality such as:
0039 
0040 \begin{itemize}
0041 \item post-processing of the vector representation (e.g.\ conversion to a
0042   file format such as PostScript or SVG, scaling + rotation,
0043   quantization etc).
0044 \end{itemize}
0045 
0046 \section{Data representation}
0047 
0048 \subsection{Bitmaps}\label{ssec-bitmap}
0049 
0050 \subsubsection{Coordinate system}
0051 
0052 \begin{figure}[t]
0053 \[ \includegraphics*[0in,0in][3in,1.7in]{potracelib-fig3}
0054 \]
0055 \caption{The Potrace coordinate system}\label{fig-coord}
0056 \end{figure}
0057 
0058 For Potrace, a bitmap of size $w\times h$ is embedded in a cartesian
0059 coordinate system where each pixel takes up the space of one unit
0060 square. The pixels are positioned so that the {\em corners} of pixels
0061 (and not their centers) lie at points with integer coordinates, as
0062 illustrated in Figure~\ref{fig-coord}. The origin of the coordinate
0063 system is at the {\em lower left} corner of the bitmap. The four
0064 corners of the bitmaps have coordinates $(0,0)$, $(0,h)$, $(w,h)$, and
0065 $(w,0)$.
0066 
0067 Sometimes we need to refer to a specific pixel (as opposed to a point
0068 in the plane). When we speak of ``pixel $[i,j]$'', we mean the pixel
0069 whose corners have coordinates $(i,j)$, $(i,j+1)$, $(i+1,j+1)$,
0070 $(i+1,j)$ in Potrace's coordinate system. Thus, pixel $[i,j]$ is the
0071 pixel whose center is at coordinates $(i+0.5, j+0.5)$. To avoid
0072 confusion, we use square brackets to refer to the pixel $[i,j]$, and
0073 round brackets to refer to the point $(i,j)$.
0074 
0075 \subsubsection{Bitmap representation}
0076 
0077 The Potrace library expects bitmaps in the following format, defined
0078 in potracelib.h:
0079 
0080 \begin{verbatim}
0081 struct potrace_bitmap_s {
0082   int w, h;          /* width and height, in pixels */
0083   int dy;            /* scanline offset in words */
0084   potrace_word *map; /* pixel data, dy*h words */
0085 };
0086 typedef struct potrace_bitmap_s potrace_bitmap_t;
0087 \end{verbatim}
0088 
0089 Here, \verb!potrace_word! is an unsigned integer type defined in
0090 potracelib.h. It is usually equal to a native machine word (i.e., 32
0091 bits on a 32-bit architecture). In the following explanation, we
0092 assume that the type \verb!potrace_word! holds $N$ bits.
0093 
0094 A bitmap of dimensions $w\times h$ is divided, bottom to top, into $h$
0095 horizontal scanlines. Each scanline is divided, left to right, into
0096 blocks of $N$ pixels. Each such block of $N$ pixels is stored as a
0097 single \verb!potrace_word!, with the leftmost pixels of the block
0098 corresponding to the most significant bit of the word, and the
0099 rightmost pixel of the block corresponding to the least significant
0100 bit of the word.
0101 
0102 Pixels that are ``on'' (or ``black'' or ``foreground'') are
0103 represented by bit value $1$.  Pixels that are ``off'' (of ``white''
0104 or ``background'') are represented by bit value $0$.
0105 
0106 If the number of bits in a scanline is not divisible by $N$, then the
0107 rightmost word of the scanline is padded on the right with zeros.
0108 
0109 The data for scanline $0$ (the bottom-most scanline) begins at
0110 \verb!map[0]!. The data for scanline $1$ begins at \verb!map[dy]!. The
0111 data for scanline $2$ begins at \verb!map[2*dy]!, and so forth.  Note
0112 that $dy$ can be either positive or negative, depending on how an
0113 application wishes to lay out the image data in memory.
0114 
0115 In summary, the pixel with coordinates $[i,j]$ can be accessed by the
0116 following C formula:
0117 
0118 {\small
0119 \begin{verbatim}
0120  pixel(i,j) = ((map + j*dy)[i/N] & (1 << (N-1-i%N)) ? 1 : 0.
0121 \end{verbatim}
0122 }
0123 
0124 \subsubsection{Example}
0125 
0126 \begin{figure}
0127   \[\includegraphics{potracelib-fig2}\]
0128   \caption{Sample bitmap}\label{fig-bitmap}
0129 \begin{verbatim}
0130  w = 36;
0131  h = 12;
0132  dy = 2;
0133  map[22] = 0xfff0fc02;    map[23] = 0x00000000;
0134  map[20] = 0x7ff1fe02;    map[21] = 0x00000000;
0135  map[18] = 0x3ff3ff07;    map[19] = 0x00000000;
0136  map[16] = 0x1ff7ff87;    map[17] = 0x00000000;
0137  map[14] = 0x0ff7cf8f;    map[15] = 0x80000000;
0138  map[12] = 0x07f7878f;    map[13] = 0x80000000;
0139  map[10] = 0x03f7879f;    map[11] = 0xc0000000;
0140  map[8]  = 0x01f7cf9f;    map[9]  = 0xc0000000;
0141  map[6]  = 0x00f7ffbf;    map[7]  = 0xe0000000;
0142  map[4]  = 0x0073ff3f;    map[5]  = 0xe0000000;
0143  map[2]  = 0x0031fe7f;    map[3]  = 0xf0000000;
0144  map[0]  = 0x0010fc7f;    map[1]  = 0xf0000000;
0145 \end{verbatim}
0146   \caption{Sample bitmap representation}\label{fig-bitmap-rep}
0147 \end{figure}
0148 
0149 Figure~\ref{fig-bitmap} shows an example bitmap of size $36\times 12$.
0150 Shaded pixels are ``on'' and white pixels are ``off''.
0151 Figure~\ref{fig-bitmap-rep} shows a possible representation of this
0152 bitmap in the \verb!potrace_bitmap_t! data structure. Note that the
0153 data is stored in the \verb!map! array in a bottom-to-top and
0154 left-to-right fashion.
0155 
0156 
0157 \subsubsection{A remark on byte order}
0158 
0159 It is important to keep in mind that bitmaps are stored as arrays of
0160 words, {\em not} as arrays of bytes. While this distinction makes no
0161 difference on big-endian architectures, it makes a significant
0162 difference on little-endian architectures such as the Intel-based
0163 architecture. For instance, when the integer word 0x1f80fc02 is
0164 accessed as a byte-array on a little-endian machine, then the bytes
0165 appear in reverse order 0x02, 0xfc, 0x80, 0x1f. Therefore, special
0166 care must be taken when converting a bitmap from a byte-based
0167 format to Potrace's word-based format.
0168 
0169 \subsubsection{Coordinate independence}
0170 
0171 The vector data that is the output of Potrace is taken with respect
0172 to the same coordinate system as the input bitmap, i.e., the
0173 coordinate system from Figure~\ref{fig-coord}. In principle, it is
0174 immaterial whether an application puts the coordinate origin in the
0175 bottom-left corner or the top-left corner of an image, as long as it
0176 interprets the output coordinates in the same way as the input
0177 coordinates.
0178 
0179 However, a reversal of the coordinate system will upset the meaning of
0180 the words ``clockwise'' and ``counterclockwise'' in the specification
0181 of vector images below (see Section~\ref{sssec-vector}), and will also
0182 affect the meaning of Potrace's turnpolicies (see
0183 Section~\ref{ssec-parameters}). We therefore assume, for definiteness,
0184 that the coordinate origin is in the {\em lower} left corner.
0185 Applications that wish to follow a different convention have to
0186 compensate accordingly.
0187 
0188 \subsection{Vector format}
0189 
0190 \subsubsection{Points}
0191 
0192 A point $(x,y)$ in the Euclidean plane is represented in Potrace by a
0193 value of type \verb!potrace_dpoint_t!.
0194 
0195 \begin{verbatim}
0196 struct potrace_dpoint_s {
0197   double x, y;
0198 };
0199 typedef struct potrace_dpoint_s potrace_dpoint_t;
0200 \end{verbatim}
0201 
0202 \subsubsection{Segments}
0203 
0204 Curves in Potrace are composed of the following two types of segments:
0205 
0206 \begin{figure}
0207   \[\begin{array}{l@{\hspace{1in}}l}
0208     (a) & (b) \\
0209     \includegraphics{potracelib-fig4a}&
0210     \includegraphics{potracelib-fig4b}
0211   \end{array}
0212   \]
0213   \caption{(a) A Bezier curve segment. (b) A corner segment}\label{fig-segment}
0214 \end{figure}
0215 
0216 \begin{itemize}
0217 \item Bezier curve segments. A Bezier curve segment is given in the
0218   usual way by a starting point $a$, two control points $u$ and $w$,
0219   and an endpoint $b$, as shown in Figure~\ref{fig-segment}(a).
0220 \item Corner segments. A corner segment is given by a starting point
0221   $a$, a vertex $v$, and an endpoint $b$.  A corner segment is drawn
0222   as two straight lines: one from $a$ to $v$, and one from $v$ to $b$,
0223   as shown in Figure~\ref{fig-segment}(b).
0224 \end{itemize}
0225 
0226 \subsubsection{Curves}\label{sssec-curves}
0227 
0228 \begin{figure}
0229   \[\includegraphics{potracelib-fig5}
0230   \]
0231   \caption{A closed curve consisting of 4 segments}\label{fig-curve}
0232 \end{figure}
0233 
0234 A curve in Potrace is a sequence of segments, such that the endpoint
0235 of each segment coincides with the starting point of the next one. All
0236 curves in Potrace are closed, and therefore the endpoint of the final
0237 segment also coincides with the starting point of the first one.
0238 Figure~\ref{fig-curve} shows an example of a curve consisting of 4
0239 segments: 3 Bezier curve segments and 1 corner segment. For clarity,
0240 the start- and endpoints of segments have been marked with dots
0241 ``$\bullet$''.
0242 
0243 Curves are represented as values of type \verb!potrace_curve_t!,
0244 which is defined as follows:
0245 
0246 \begin{verbatim}
0247 struct potrace_curve_s {
0248   int n;                    /* number of segments */
0249   int *tag;                 /* array of segment types */
0250   potrace_dpoint_t (*c)[3]; /* array of control points. */
0251 };
0252 typedef struct potrace_curve_s potrace_curve_t;
0253 \end{verbatim}
0254 
0255 Here $n\geq 1$ is the number of segments in the curve. For $i=0,\ldots,n-1$,
0256 $\verb!tag[i]!$ is the type of the $i$-th segment, which is
0257 \verb!POTRACE_CURVETO! for a Bezier curve segment and
0258 \verb!POTRACE_CORNER! for a corner segment. $c$ is an array of size
0259 $n\times 3$ that holds the control points of the curve segments in
0260 the following manner:
0261 \begin{itemize}
0262 \item If the $i$-th segment is a Bezier curve segment, then
0263   $\verb!c[i][0]! = u$ and $\verb!c[i][1]! = w$ are the two control
0264   points of that segment, and $\verb!c[i][2]! = b$ is its endpoint.
0265 \item If the $i$-th segment is a corner segment, then $\verb!c[i][0]!$
0266   is unused, $\verb!c[i][1]! = v$ is the vertex of the segment, and
0267   $\verb!c[i][2]! = b$ is its endpoint.
0268 \end{itemize}
0269 
0270 Note that, since the starting point $a$ of each segment coincides with
0271 the endpoint $b$ of the preceding segment (and the starting point $a$
0272 of the first segment coincides with the endpoint $b$ of the last
0273 segment), there is no need to store the starting points $a$
0274 explicitly. Also, note that regardless of the type of segment, the
0275 endpoint of the $i$-th segment is $\verb!c[i][2]!$, and the starting point
0276 of the $i$-th segment is $\verb!c[i ? i-1 : n-1][2]!$.
0277 
0278 The curve from Figure~\ref{fig-curve} is therefore represented
0279 by the following data:
0280 \[\begin{array}{llll}
0281 \verb!n = 4;!\\
0282 \verb!tag[0] = POTRACE_CURVETO;!\\
0283 \verb!c[0][0] = !u_0\verb!;!\quad\quad\quad
0284 \verb!c[0][1] = !w_0\verb!;!\quad
0285 \verb!c[0][2] = !b_0\verb! = !a_1\verb!;!\\
0286 \verb!tag[1] = POTRACE_CURVETO;!\\
0287 \verb!c[1][0] = !u_1\verb!;!\quad\quad\quad
0288 \verb!c[1][1] = !w_1\verb!;!\quad
0289 \verb!c[1][2] = !b_1\verb! = !a_2\verb!;!\\
0290 \verb!tag[2] = POTRACE_CURVETO;!\\
0291 \verb!c[2][0] = !u_2\verb!;!\quad\quad\quad
0292 \verb!c[2][1] = !w_2\verb!;!\quad
0293 \verb!c[2][2] = !b_2\verb! = !a_3\verb!;!\\
0294 \verb!tag[3] = POTRACE_CORNER;!\\
0295 \verb!c[3][0] = !{\it unused}\verb!;!\quad
0296 \verb!c[3][1] = !v_3\verb!;!\quad
0297 \verb!c[3][2] = !b_3\verb! = !a_0\verb!;!\\
0298 \end{array}
0299 \]
0300 
0301 \subsubsection{Boundary decomposition of bitonal vector
0302   images}\label{sssec-boundary}
0303 
0304 \begin{figure}
0305   \[\begin{array}{l@{\hspace{.25in}}l}
0306     (a) & (b) \\
0307     \includegraphics{potracelib-fig1a}&
0308     \includegraphics{potracelib-fig1b}
0309   \end{array}
0310   \]
0311   \caption{(a) A vector image. (b) Its boundary
0312     decomposition.}\label{fig-vector}
0313 \end{figure}
0314 
0315 In Potrace, a bitonal (i.e. black-and-white) vector image, as in
0316 Figure~\ref{fig-vector}(a), is decomposed into a collection of closed
0317 boundary curves, shown in blue and red and labeled $A$--$I$ in
0318 Figure~\ref{fig-vector}(b).
0319 
0320 We introduce some terminology. A closed curve is {\em simple} if it
0321 does not intersect itself. Each simple closed curve, taken by itself,
0322 divides the plane into two regions, called the {\em inside} and and
0323 the {\em outside} of the curve. If $C_1$ and $C_2$ are simple closed
0324 curves, we say that $C_1$ is {\em contained} in $C_2$, written $C_1 <
0325 C_2$, if $C_1$ lies entirely within the inside of $C_2$. For example,
0326 in Figure~\ref{fig-vector}(b), the curves $B$--$E$ are contained in
0327 $A$, whereas $F$--$I$ are not.
0328 
0329 In a decomposition of a vector image as in Figure~\ref{fig-vector}, we
0330 say that a curve $C_1$ is a {\em child} of $C_2$ if $C_1 < C_2$ and
0331 there exists no other curve $C_3$ between $C_1$ and $C_2$ (i.e., no
0332 curve $C_3$ such that $C_1 < C_3 < C_2$). In this case, we also say
0333 that $C_1$ is a {\em parent} of $C_2$. Since boundary curves do not
0334 intersect, each curve has at most one parent.  Two curves are said to
0335 be {\em siblings} if they are either both parentless, or else they
0336 have a parent in common. Note that the ``child'' relation naturally
0337 defines a tree structure on the set of curves (more precisely, it
0338 defines a ``forest'', since there can be more than one root).
0339 
0340 For example, in Figure~\ref{fig-vector}(b), the curve $A$ has no
0341 parent, and has children $B$ and $E$. The curve $E$ has no children,
0342 and the curve $B$ has children $C$ and $D$. $A$ and $F$ are siblings,
0343 $B$ and $E$ are siblings, and $C$ and $D$ are siblings. The curves
0344 from Figure~\ref{fig-vector}(b) form the following forest under the
0345 ``child'' relation:
0346 
0347 \begin{verbatim}
0348                   A         F
0349                  / \        |
0350                 B   E       G
0351                / \          |
0352               C   D         H
0353                             |
0354                             I
0355 \end{verbatim}
0356 
0357 We can assign each curve a {\em sign} by calling a curve {\em
0358   positive} if it encloses a ``foreground'' region, and {\em negative}
0359 if it encloses a ``background'' region (or ``hole''). For example, in
0360 Figure~\ref{fig-vector}(b), positive curves are shown in blue and
0361 negative curves in red.
0362 
0363 Since foreground and background regions alternate, it follows that the
0364 sign of curves also alternates, i.e., parentless curves are always
0365 positive, and all other curves have the opposite sign of their parent.
0366 It follows that, in the tree structure, curves that appear at even
0367 levels are positive and those that appear at odd levels are negative.
0368 In particular, siblings share a common sign.
0369 
0370 \subsubsection{Representation of vector images}\label{sssec-vector}
0371 
0372 In Potrace, a vector image is represented as a linked collection of
0373 zero or more structures of type \verb!potrace_path_t!, which is
0374 defined as follows:
0375 
0376 \begin{verbatim}
0377 struct potrace_path_s {
0378   int area;                         /* enclosed area */
0379   int sign;                         /* '+' or '-' */
0380   potrace_curve_t curve;            /* vector data */
0381 
0382   struct potrace_path_s *next;      /* list structure */
0383 
0384   struct potrace_path_s *childlist; /* tree structure */
0385   struct potrace_path_s *sibling;   /* tree structure */
0386 
0387   struct potrace_privpath_s *priv;  /* private state */
0388 };
0389 typedef struct potrace_path_s potrace_path_t;
0390 \end{verbatim}
0391 
0392 Each such structure holds a single curve, and the structures are
0393 linked to each other via the \verb!next!, \verb!childlist!, and
0394 \verb!sibling! pointers.
0395 
0396 \begin{itemize}
0397 
0398 \item The \verb!sign! field holds the sign of the curve (\verb!'+'! or
0399   \verb!'-'! in ASCII).
0400 
0401 \item The \verb!curve! field contains the curve's vector data as
0402   described in Section~\ref{sssec-curves}. Potrace additionally
0403   follows the convention that positive curves run counterclockwise and
0404   negative curves run clockwise; this facilitates rendering in
0405   environments (such as PostScript or PDF) that have a ``fill'' rule
0406   based on winding number.
0407 
0408 \item The \verb!area! field gives the approximate magnitude of the
0409   area enclosed by the curve. (In fact, it is the precise integer area
0410   of the original untraced ``jaggy'' curve). Some clients use this
0411   information to improve interactive rendering speeds by ignoring very
0412   small areas in a first rendering pass. See also the description of
0413   the \verb!turdsize!  parameter in Section~\ref{ssec-parameters} below.
0414   
0415 \item The \verb!priv!  field is used internally by Potrace, and is not
0416   accessible to applications.
0417 
0418 \item The \verb!next! field is used to link all the curves of a given
0419   vector image into a linked list. Each member points to the next one
0420   via its \verb!next! field, and the last member of the list has
0421   \verb!next==NULL!.  The order of the elements of this list is
0422   unspecified, but is guaranteed to satisfy the following constraints:
0423   \begin{enumerate}\alphalabels
0424   \item outer curves appear before inner ones, so if $C_1 < C_2$, then
0425     $C_2$ always appears sometime before $C_1$ in the linked list, and
0426   \item each positive curve is immediately followed by all of its
0427     children.
0428   \end{enumerate}
0429   These two constraints make it easy for clients to render the image
0430   by simply processing the linked list in sequential order.
0431   Constraint (a) makes it possible to fill each curve with solid black
0432   or white color, allowing later curves to paint over parts of earlier
0433   ones.  Constraint (b) further allows a client to fill a positive
0434   curve, minus its negative children, in a single paint operation,
0435   leaving a ``hole'' for each of the negative children.
0436 
0437 \item The \verb!childlist! and \verb!sibling! fields define a forest
0438   structure on the set of curves, which can be used independently of
0439   the linked list structure.  For each curve, \verb!childlist! is a
0440   pointer to its first child, or \verb!NULL! if there are no children.
0441   Also, \verb!sibling! is a pointer to the next sibling, or
0442   \verb!NULL!  if there are no further siblings.  The relative order
0443   of siblings is unspecified. The root node of the tree structure
0444   always coincides with the root node of the linked list structure.
0445 \end{itemize}
0446 
0447 An image consisting of zero curves is represented as a \verb!NULL! pointer.
0448 
0449 \subsubsection{Intersecting curves}
0450 
0451 While in the above discussion we have assumed a set of
0452 non-intersecting curves, in practice it can happen that the curves
0453 output by Potrace intersect slightly. Clients should therefore
0454 carefully choose their rendering parameters (e.g., the non-zero
0455 winding number rule is preferable to the odd winding number rule) to
0456 avoid undesirable artifacts.
0457 
0458 \subsubsection{Example}
0459 
0460 The image from Figure~\ref{fig-vector} can be represented by the
0461 pointer \verb!plist!, where {\tt A}--{\tt I} are structures
0462 of type \verb!potrace_path_t!, as follows.  We do not show the
0463 \verb!area! and \verb!curve! fields.
0464 {\small
0465 \begin{verbatim}
0466  potrace_path_t *plist = &A;
0467 
0468  A.sign = '+';       B.sign = '-';       E.sign = '-';
0469  A.next = &B;        B.next = &E;        E.next = &C;
0470  A.childlist = &B;   B.childlist = &C;   E.childlist = NULL;
0471  A.sibling = &F;     B.sibling = &E;     E.sibling = NULL;
0472 
0473  C.sign = '+';       D.sign = '+';       F.sign = '+';
0474  C.next = &D;        D.next = &F;        F.next = &G;
0475  C.childlist = NULL; D.childlist = NULL; F.childlist = &G;
0476  C.sibling = &D;     D.sibling = NULL;   F.sibling = NULL;
0477 
0478  G.sign = '-';       H.sign = '+';       I.sign = '-';
0479  G.next = &H;        H.next = &I;        I.next = NULL;
0480  G.childlist = &H;   H.childlist = &I;   I.childlist = NULL;
0481  G.sibling = NULL;   H.sibling = NULL;   I.sibling = NULL;
0482 \end{verbatim}
0483 }
0484 
0485 \subsection{Tracing parameters}\label{ssec-parameters}
0486 
0487 The tracing operation of Potrace is controlled by a small number of
0488 parameters. The parameter structure is defined in potracelib.h as:
0489 
0490 \begin{verbatim}
0491 struct potrace_param_s {
0492   int turdsize;        
0493   int turnpolicy;      
0494   double alphamax;     
0495   int opticurve;       
0496   double opttolerance; 
0497   potrace_progress_t progress; 
0498 };
0499 typedef struct potrace_param_s potrace_param_t;
0500 \end{verbatim}
0501 
0502 For most practical purposes, the default parameters give excellent
0503 results. The function \verb!potrace_param_default()! (see
0504 Section~\ref{5c}) returns the set of default parameters. Applications
0505 must always start from these default parameters before changing any
0506 parameters. This will increase backward compatibility in case
0507 additional parameters are added in the future.
0508 
0509 \subsubsection{Turdsize}
0510 
0511 \begin{figure}
0512 \[\begin{array}{l@{\hspace{.5in}}l}
0513   \textrm{\rm Before:} & \textrm{\rm After:}\\
0514   \includegraphics{potracelib-fig6a}&
0515   \includegraphics{potracelib-fig6b}
0516 \end{array}
0517 \]
0518 \caption{Despeckling with turdsize=3.}\label{fig-turdsize}
0519 \end{figure}
0520 
0521 The \verb!turdsize! parameter can be used to ``despeckle'' the bitmap
0522 to be traced, by removing all curves whose enclosed area is below the
0523 given threshold. Figure~\ref{fig-turdsize} shows the result of
0524 applying turdsize=3 to a bitmap. The current default for the turdsize
0525 parameter is 2; its useful range is from 0 to infinity.
0526 
0527 \subsubsection{Turnpolicy}
0528 
0529 \begin{figure}
0530 \[\includegraphics{potracelib-fig8}\]
0531 \caption{Path decomposition}\label{fig-turns}
0532 \end{figure}
0533 
0534 The turnpolicy parameter determines how to resolve ambiguities during
0535 decomposition of bitmaps into paths. The ambiguity arises in the last
0536 situation shown in Figure~\ref{fig-turns}. The possible choices for
0537 the \verb!turnpolicy! parameter are:
0538 \begin{itemize}
0539 \item \verb!POTRACE_TURNPOLICY_BLACK!: prefers to connect black
0540   (foreground) components.
0541 \item \verb!POTRACE_TURNPOLICY_WHITE!: prefers to connect white
0542   (background) components.
0543 \item \verb!POTRACE_TURNPOLICY_LEFT!: always take a left turn.
0544 \item \verb!POTRACE_TURNPOLICY_RIGHT!: always take a right turn.
0545 \item \verb!POTRACE_TURNPOLICY_MINORITY!: prefers to connect the color
0546   (black or white) that occurs least frequently in a local
0547   neighborhood of the current position.
0548 \item \verb!POTRACE_TURNPOLICY_MAJORITY!: prefers to connect the color
0549   (black or white) that occurs most frequently in a local neighborhood
0550   of the current position.
0551 \item \verb!POTRACE_TURNPOLICY_RANDOM!: choose pseudo-randomly.
0552 \end{itemize}
0553 
0554 The current default policy is \verb!POTRACE_TURNPOLICY_MINORITY!, which tends
0555 to keep visual lines connected.
0556 
0557 \subsubsection{Alphamax}
0558 
0559 \begin{figure}
0560 \[\begin{array}{ccccc}
0561 \includegraphics{potracelib-fig7-00}&
0562 \includegraphics{potracelib-fig7-06}&
0563 \includegraphics{potracelib-fig7-10}&
0564 \includegraphics{potracelib-fig7-12}&
0565 \includegraphics{potracelib-fig7-13}\\
0566 \alpha_{max}=0.0&
0567 \alpha_{max}=0.6&
0568 \alpha_{max}=1.0&
0569 \alpha_{max}=1.2&
0570 \alpha_{max}=1.3
0571 \end{array}
0572 \]
0573 \caption{The alphamax parameter}\label{fig-alphamax}
0574 \end{figure}
0575 
0576 The \verb!alphamax! parameter is a threshold for the detection of
0577 corners. It controls the smoothness of the traced curve, as shown in
0578 Figure~\ref{fig-alphamax}. The current default is 1.0.  The useful range of
0579 this parameter is from 0.0 (polygon) to 1.3334 (no corners).
0580 
0581 \subsubsection{Opticurve and opttolerance}
0582 
0583 The \verb!opticurve! parameter is a boolean flag that controls whether
0584 Potrace will attempt to ``simplify'' the final curve by reducing the
0585 number of Bezier curve segments. Opticurve=1 turns on optimization,
0586 and opticurve=0 turns it off. The current default is on. 
0587 
0588 The \verb!opttolerance! parameter defines the amount of error allowed
0589 in this simplification. The current default is 0.2. Larger values tend to
0590 decrease the number of segments, at the expense of less accuracy.  The
0591 useful range is from 0 to infinity, although in practice one would
0592 hardly choose values greater than 1 or so. For most purposes, the
0593 default value is a good tradeoff between space and accuracy.
0594 
0595 \subsubsection{Progress reporting}
0596 
0597 Since tracing a large bitmap can be time consuming, Potrace has the
0598 option of reporting progress to the calling application. This is
0599 typically used in interactive applications to implement a progress
0600 bar. Progress reporting is controlled by the \verb!progress!
0601 parameter, which is a structure of type \verb!potrace_progress_t!,
0602 defined as follows:
0603 
0604 \begin{verbatim}
0605 struct potrace_progress_s {
0606   void (*callback)(double progress, void *privdata);
0607   void *data;      
0608   double min, max; 
0609   double epsilon;      
0610 };
0611 typedef struct potrace_progress_s potrace_progress_t;
0612 \end{verbatim}
0613 
0614 If \verb!callback! is not NULL, then progress reporting is enabled.
0615 In this case, \verb!callback! is the address of a function to be
0616 called for progress reports, and \verb!data! is a pointer to that
0617 function's private data.  Progress reports take the form of a function
0618 call \verb!callback(d, data)!, where \verb!d! is a number representing
0619 the amount of relative progress in the range
0620 \verb!min!\ldots\verb!max!. 
0621 
0622 The parameter \verb!epsilon! is a hint that tells Potrace what amount of
0623 progress the application considers ``too small to report''. Whenever
0624 convenient, Potrace will feel free to suppress progress reports if the
0625 increment since the previous report has been less than \verb!epsilon!.  As
0626 a special case, if $\verb!epsilon!=0$, then the maximal number of progress
0627 reports are sent. In any case, the application should handle progress
0628 reports very efficiently, as there may be a large number of reports. 
0629 
0630 The defaults are $\verb!callback!=\textrm{NULL}$,
0631 $\verb!data!=\textrm{NULL}$, $\verb!min!=0.0$, $\verb!max!=1.0$, and
0632 $\verb!epsilon!=0$.
0633 
0634 \subsection{Potrace state}\label{4}
0635 
0636 A Potrace state holds the result of a tracing operation. It is
0637 defined as follows:
0638 
0639 \begin{verbatim}
0640 struct potrace_state_s {
0641   int status;
0642   potrace_path_t *plist;            /* vector data */
0643 
0644   struct potrace_privstate_s *priv; /* private state */
0645 };
0646 typedef struct potrace_state_s potrace_state_t;
0647 \end{verbatim}
0648 
0649 The fields are as follows:
0650 \begin{itemize}
0651 \item The \verb!status! field is either \verb!POTRACE_STATUS_OK!, to
0652   indicate that the tracing operation was successful, or
0653   \verb!POTRACE_STATUS_INCOMPLETE!, to indicate that it was
0654   unsuccessful.
0655 \item In the event of success, \verb!plist! points to the
0656   representation of the bitonal traced vector image as described in
0657   Section~\ref{sssec-vector}.  In the event of failure, \verb!plist!
0658   points to a data structure whose properties are undefined, except
0659   that the Potrace state can still be freed with \verb!potrace_state_free()!.
0660 \item The \verb!priv! field is used internally by Potrace, and is not
0661   accessible by applications.
0662 \end{itemize}
0663 
0664 \section{API functions}\label{5}
0665 
0666 There is no global or static state in potracelib; all API functions
0667 are reentrant and thread-safe.
0668 
0669 \subsection{potrace\_trace}\label{5a}
0670 
0671 {\small
0672 \begin{verbatim}
0673 potrace_state_t *potrace_trace(const potrace_param_t *param,
0674                                const potrace_bitmap_t *bm);
0675 \end{verbatim}
0676 }
0677 
0678 \noindent Inputs: 
0679 \begin{itemize}
0680 \item \verb!bm!: a bitmap (see Section~\ref{ssec-bitmap}).
0681 \item \verb!param!: a set of tracing parameters (see
0682   Section~\ref{ssec-parameters}).
0683 \end{itemize}
0684 
0685 \noindent Output:
0686 \begin{itemize}
0687 \item a Potrace state (see Section~\ref{4}). 
0688 \end{itemize}
0689 
0690 This function attempts to trace the given bitmap using the given
0691 tracing parameters. In the event of success, it returns a valid
0692 Potrace state with the \verb!status! field set to
0693 \verb!POTRACE_STATUS_OK!.  In the event of failure, it sets
0694 \verb!errno! to an error number, and either returns \verb!NULL!, or
0695 else it returns an incomplete Potrace state, which by definition has
0696 the status field set to \verb!POTRACE_STATUS_INCOMPLETE!. Any Potrace
0697 state returned by \verb!potrace_trace()! (whether it is valid or
0698 invalid) can be freed using the \verb!potrace_state_free()! function
0699 below.
0700 
0701 \subsection{potrace\_state\_free}\label{5b}
0702 
0703 \begin{verbatim}
0704 void potrace_state_free(potrace_state_t *st);
0705 \end{verbatim}
0706 
0707 \noindent Input:
0708 \begin{itemize}
0709 \item \verb!st!: a Potrace state previously returned by
0710   \verb!potrace_trace()!.
0711 \end{itemize}
0712 
0713 This function frees the memory and other resources (if any) associated
0714 with the Potrace state.
0715 
0716 \subsection{potrace\_param\_default}\label{5c}
0717 
0718 \begin{verbatim}
0719 potrace_param_t *potrace_param_default();
0720 \end{verbatim}
0721 
0722 \noindent Output:
0723 \begin{itemize}
0724 \item a set of tracing parameters (see Section~\ref{ssec-parameters}). 
0725 \end{itemize}
0726 
0727 This function returns a fresh set of tracing parameters, initialized
0728 to defaults.  Applications must always use this function to create an
0729 object of type \verb!potrace_param_t!, and they must always start from
0730 the default parameters before modifying any parameters.  This will
0731 help increase backward compatibility when additional parameters are
0732 added in the future. The parameter set returned by this function can
0733 later be freed by \verb!potrace_param_free()!.
0734 
0735 \subsection{potrace\_param\_free()}\label{5d}
0736 
0737 \begin{verbatim}
0738 void potrace_param_free(potrace_param_t *p);
0739 \end{verbatim}
0740 
0741 \noindent Input:
0742 \begin{itemize}
0743 \item tracing parameters previously returned by
0744   \verb!potrace_param_default()!.
0745 \end{itemize}
0746 
0747 This function frees the memory occupied by a set of tracing parameters
0748 as returned by \verb!potrace_param_default()!. Only the fields
0749 initialized by Potrace are freed, not any fields set by the
0750 application itself (such as \verb!progress.data!).
0751 
0752 \subsection{potrace\_version()}\label{5e}
0753 
0754 \begin{verbatim}
0755 const char *potrace_version();
0756 \end{verbatim}
0757 
0758 This function returns a static human-readable text string identifying
0759 this version of potracelib.
0760 
0761 \end{document}