Potrace library: update version from 1.8 to 1.12

This commit is contained in:
jean-pierre charras 2015-12-03 16:46:17 +01:00
parent 9b4d12653e
commit 23a0dddea9
21 changed files with 1530 additions and 1004 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
@ -8,21 +8,23 @@
#ifndef AUXILIARY_H
#define AUXILIARY_H
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#include "config.h"
#endif
/* ---------------------------------------------------------------------- */
/* point arithmetic */
#include <potracelib.h>
#include "potracelib.h"
struct point_s
{
long x;
long y;
long x;
long y;
};
typedef struct point_s point_t;
typedef struct point_s point_t;
typedef potrace_dpoint_t dpoint_t;
@ -31,8 +33,8 @@ static inline dpoint_t dpoint( point_t p )
{
dpoint_t res;
res.x = p.x;
res.y = p.y;
res.x = p.x;
res.y = p.y;
return res;
}
@ -42,14 +44,13 @@ static inline dpoint_t interval( double lambda, dpoint_t a, dpoint_t b )
{
dpoint_t res;
res.x = a.x + lambda * (b.x - a.x);
res.y = a.y + lambda * (b.y - a.y);
res.x = a.x + lambda * (b.x - a.x);
res.y = a.y + lambda * (b.y - a.y);
return res;
}
/* ---------------------------------------------------------------------- */
/* some useful macros. Note: the "mod" macro works correctly for
* negative a. Also note that the test for a>=n, while redundant,
* speeds up the mod function by 70% in the average case (significant

View File

@ -1,19 +1,16 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
#ifndef BITMAP_H
#define BITMAP_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/* The bitmap type is defined in potracelib.h */
#include <potracelib.h>
#include "potracelib.h"
/* The present file defines some convenient macros and static inline
* functions for accessing bitmaps. Since they only produce inline
@ -31,21 +28,21 @@
/* macros for accessing pixel at index (x,y). U* macros omit the
* bounds check. */
#define bm_scanline( bm, y ) ( (bm)->map + (y) * (bm)->dy )
#define bm_index( bm, x, y ) (&bm_scanline( bm, y )[(x) / BM_WORDBITS])
#define bm_mask( x ) ( BM_HIBIT >> ( (x) & (BM_WORDBITS - 1) ) )
#define bm_range( x, a ) ( (int) (x) >= 0 && (int) (x) < (a) )
#define bm_safe( bm, x, y ) ( bm_range( x, (bm)->w ) && bm_range( y, (bm)->h ) )
#define BM_UGET( bm, x, y ) ( ( *bm_index( bm, x, y ) & bm_mask( x ) ) != 0 )
#define BM_USET( bm, x, y ) ( *bm_index( bm, x, y ) |= bm_mask( x ) )
#define BM_UCLR( bm, x, y ) ( *bm_index( bm, x, y ) &= ~bm_mask( x ) )
#define BM_UINV( bm, x, y ) ( *bm_index( bm, x, y ) ^= bm_mask( x ) )
#define BM_UPUT( bm, x, y, b ) ( (b) ? BM_USET( bm, x, y ) : BM_UCLR( bm, x, y ) )
#define BM_GET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UGET( bm, x, y ) : 0)
#define BM_SET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_USET( bm, x, y ) : 0)
#define BM_CLR( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UCLR( bm, x, y ) : 0)
#define BM_INV( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UINV( bm, x, y ) : 0)
#define BM_PUT( bm, x, y, b ) (bm_safe( bm, x, y ) ? BM_UPUT( bm, x, y, b ) : 0)
#define bm_scanline( bm, y ) ( (bm)->map + (ssize_t) (y) * (ssize_t) (bm)->dy )
#define bm_index( bm, x, y ) (&bm_scanline( bm, y )[(x) / BM_WORDBITS])
#define bm_mask( x ) ( BM_HIBIT >> ( (x) & (BM_WORDBITS - 1) ) )
#define bm_range( x, a ) ( (int) (x) >= 0 && (int) (x) < (a) )
#define bm_safe( bm, x, y ) ( bm_range( x, (bm)->w ) && bm_range( y, (bm)->h ) )
#define BM_UGET( bm, x, y ) ( ( *bm_index( bm, x, y ) & bm_mask( x ) ) != 0 )
#define BM_USET( bm, x, y ) ( *bm_index( bm, x, y ) |= bm_mask( x ) )
#define BM_UCLR( bm, x, y ) ( *bm_index( bm, x, y ) &= ~bm_mask( x ) )
#define BM_UINV( bm, x, y ) ( *bm_index( bm, x, y ) ^= bm_mask( x ) )
#define BM_UPUT( bm, x, y, b ) ( (b) ? BM_USET( bm, x, y ) : BM_UCLR( bm, x, y ) )
#define BM_GET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UGET( bm, x, y ) : 0)
#define BM_SET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_USET( bm, x, y ) : 0)
#define BM_CLR( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UCLR( bm, x, y ) : 0)
#define BM_INV( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UINV( bm, x, y ) : 0)
#define BM_PUT( bm, x, y, b ) (bm_safe( bm, x, y ) ? BM_UPUT( bm, x, y, b ) : 0)
/* free the given bitmap. Leaves errno untouched. */
static inline void bm_free( potrace_bitmap_t* bm )
@ -54,30 +51,44 @@ static inline void bm_free( potrace_bitmap_t* bm )
{
free( bm->map );
}
free( bm );
}
/* return new un-initialized bitmap. NULL with errno on error */
/* return new un-initialized bitmap. NULL with errno on error.
* Assumes w, h >= 0. */
static inline potrace_bitmap_t* bm_new( int w, int h )
{
potrace_bitmap_t* bm;
int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
int dy = w == 0 ? 0 : (w - 1) / BM_WORDBITS + 1;
ssize_t size = (ssize_t) dy * (ssize_t) h * (ssize_t) BM_WORDSIZE;
/* check for overflow error */
if( size < 0 || size / h / dy != BM_WORDSIZE )
{
errno = ENOMEM;
return NULL;
}
bm = (potrace_bitmap_t*) malloc( sizeof(potrace_bitmap_t) );
if( !bm )
{
return NULL;
}
bm->w = w;
bm->h = h;
bm->dy = dy;
bm->map = (potrace_word*) malloc( dy * h * BM_WORDSIZE );
bm->map = (potrace_word*) malloc( size );
if( !bm->map )
{
free( bm );
return NULL;
}
return bm;
}
@ -85,7 +96,11 @@ static inline potrace_bitmap_t* bm_new( int w, int h )
/* clear the given bitmap. Set all bits to c. */
static inline void bm_clear( potrace_bitmap_t* bm, int c )
{
memset( bm->map, c ? -1 : 0, bm->dy * bm->h * BM_WORDSIZE );
/* Note: if the bitmap was created with bm_new, then it is
* guaranteed that size will fit into the ssize_t type. */
ssize_t size = (ssize_t) bm->dy * (ssize_t) bm->h * (ssize_t) BM_WORDSIZE;
memset( bm->map, c ? -1 : 0, size );
}
@ -93,12 +108,14 @@ static inline void bm_clear( potrace_bitmap_t* bm, int c )
static inline potrace_bitmap_t* bm_dup( const potrace_bitmap_t* bm )
{
potrace_bitmap_t* bm1 = bm_new( bm->w, bm->h );
ssize_t size = (ssize_t) bm->dy * (ssize_t) bm->h * (ssize_t) BM_WORDSIZE;
if( !bm1 )
{
return NULL;
}
memcpy( bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE );
memcpy( bm1->map, bm->map, size );
return bm1;
}
@ -106,9 +123,10 @@ static inline potrace_bitmap_t* bm_dup( const potrace_bitmap_t* bm )
/* invert the given bitmap. */
static inline void bm_invert( potrace_bitmap_t* bm )
{
int i;
ssize_t i;
ssize_t size = (ssize_t) bm->dy * (ssize_t) bm->h;
for( i = 0; i < bm->dy * bm->h; i++ )
for( i = 0; i < size; i++ )
{
bm->map[i] ^= BM_ALLBITS;
}

View File

@ -1,19 +1,19 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: bitmap_io.c 147 2007-04-09 00:44:09Z selinger $ */
/* Routines for manipulating bitmaps, including reading pbm files. */
#include <stdio.h>
#include <bitmap.h>
#include "bitmap.h"
#include "bitops.h"
#define INTBITS ( 8 * sizeof(int) )
static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp );
static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, int magic );
static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp );
static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, int magic );
/* ---------------------------------------------------------------------- */
/* routines for reading pnm streams */
@ -27,17 +27,20 @@ static int fgetc_ws( FILE* f )
while( 1 )
{
c = fgetc( f );
if( c=='#' )
{
while( 1 )
{
c = fgetc( f );
if( c=='\n' || c==EOF )
{
break;
}
}
}
/* space, tab, line feed, carriage return, form-feed */
if( c!=' ' && c!='\t' && c!='\r' && c!='\n' && c!=12 )
{
@ -61,10 +64,12 @@ static int readnum( FILE* f )
while( 1 )
{
c = fgetc_ws( f );
if( c==EOF )
{
return -1;
}
if( c>='0' && c<='9' )
{
break;
@ -73,18 +78,22 @@ static int readnum( FILE* f )
/* first digit is already in c */
acc = c - '0';
while( 1 )
{
c = fgetc( f );
if( c==EOF )
{
break;
}
if( c<'0' || c>'9' )
{
ungetc( c, f );
break;
}
acc *= 10;
acc += c - '0';
}
@ -104,10 +113,12 @@ static int readbit( FILE* f )
while( 1 )
{
c = fgetc_ws( f );
if( c==EOF )
{
return -1;
}
if( c>='0' && c<='1' )
{
break;
@ -126,7 +137,7 @@ static int readbit( FILE* f )
* error message in bm_read_error), and 1 on premature end of file, -3
* on empty file (including files which contain only whitespace and
* comments), -4 if wrong magic number. If the return value is >=0,
*bmp is valid. */
* bmp is valid. */
const char* bm_read_error = NULL;
@ -140,19 +151,24 @@ int bm_read( FILE* f, double threshold, potrace_bitmap_t** bmp )
* by the PNM standard, but there is no harm in being lenient. */
magic[0] = fgetc_ws( f );
if( magic[0] == EOF )
{
return -3;
}
magic[1] = fgetc( f );
if( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' )
{
return bm_readbody_pnm( f, threshold, bmp, magic[1] );
}
if( magic[0] == 'B' && magic[1] == 'M' )
{
return bm_readbody_bmp( f, threshold, bmp );
}
return -4;
}
@ -171,12 +187,14 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
bm = NULL;
w = readnum( f );
if( w<0 )
{
goto format_error;
}
h = readnum( f );
if( h<0 )
{
goto format_error;
@ -184,6 +202,7 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* allocate bitmap */
bm = bm_new( w, h );
if( !bm )
{
return -1;
@ -206,10 +225,12 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( x = 0; x<w; x++ )
{
b = readbit( f );
if( b<0 )
{
goto eof;
}
BM_UPUT( bm, x, y, b );
}
}
@ -220,6 +241,7 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* read P2 format: PGM ascii */
max = readnum( f );
if( max<1 )
{
goto format_error;
@ -230,10 +252,12 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( x = 0; x<w; x++ )
{
b = readnum( f );
if( b<0 )
{
goto eof;
}
BM_UPUT( bm, x, y, b > threshold * max ? 0 : 1 );
}
}
@ -244,6 +268,7 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* read P3 format: PPM ascii */
max = readnum( f );
if( max<1 )
{
goto format_error;
@ -254,13 +279,16 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( x = 0; x<w; x++ )
{
sum = 0;
for( i = 0; i<3; i++ )
{
b = readnum( f );
if( b<0 )
{
goto eof;
}
sum += b;
}
@ -274,6 +302,7 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* read P4 format: PBM raw */
b = fgetc( f ); /* read single white-space character after height */
if( b==EOF )
{
goto format_error;
@ -286,13 +315,15 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( i = 0; i<bpr; i++ )
{
b = fgetc( f );
if( b==EOF )
{
goto eof;
}
*bm_index( bm, i * 8,
y ) |= ( (potrace_word) b ) <<
( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) );
y ) |= ( (potrace_word) b ) <<
( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) );
}
}
@ -302,12 +333,14 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* read P5 format: PGM raw */
max = readnum( f );
if( max<1 )
{
goto format_error;
}
b = fgetc( f ); /* read single white-space character after max */
if( b==EOF )
{
goto format_error;
@ -318,16 +351,21 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( x = 0; x<w; x++ )
{
b = fgetc( f );
if( b==EOF )
goto eof;
if( max>=256 )
{
b <<= 8;
b <<= 8;
b1 = fgetc( f );
if( b1==EOF )
goto eof;
b |= b1;
}
BM_UPUT( bm, x, y, b > threshold * max ? 0 : 1 );
}
}
@ -338,12 +376,14 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
/* read P6 format: PPM raw */
max = readnum( f );
if( max<1 )
{
goto format_error;
}
b = fgetc( f ); /* read single white-space character after max */
if( b==EOF )
{
goto format_error;
@ -354,21 +394,27 @@ static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, i
for( x = 0; x<w; x++ )
{
sum = 0;
for( i = 0; i<3; i++ )
{
b = fgetc( f );
if( b==EOF )
{
goto eof;
}
if( max>=256 )
{
b <<= 8;
b <<= 8;
b1 = fgetc( f );
if( b1==EOF )
goto eof;
b |= b1;
}
sum += b;
}
@ -388,6 +434,7 @@ eof:
format_error:
bm_free( bm );
if( magic == '1' || magic == '4' )
{
bm_read_error = "invalid pbm file";
@ -400,6 +447,7 @@ format_error:
{
bm_read_error = "invalid ppm file";
}
return -2;
}
@ -409,49 +457,56 @@ format_error:
struct bmp_info_s
{
unsigned int FileSize;
unsigned int reserved;
unsigned int DataOffset;
unsigned int InfoSize;
unsigned int w; /* width */
unsigned int h; /* height */
unsigned int Planes;
unsigned int bits; /* bits per sample */
unsigned int comp; /* compression mode */
unsigned int ImageSize;
unsigned int XpixelsPerM;
unsigned int YpixelsPerM;
unsigned int ncolors; /* number of colors in palette */
unsigned int ColorsImportant;
unsigned int ctbits; /* sample size for color table */
unsigned int FileSize;
unsigned int reserved;
unsigned int DataOffset;
unsigned int InfoSize;
unsigned int w; /* width */
unsigned int h; /* height */
unsigned int Planes;
unsigned int bits; /* bits per sample */
unsigned int comp; /* compression mode */
unsigned int ImageSize;
unsigned int XpixelsPerM;
unsigned int YpixelsPerM;
unsigned int ncolors; /* number of colors in palette */
unsigned int ColorsImportant;
unsigned int RedMask;
unsigned int GreenMask;
unsigned int BlueMask;
unsigned int AlphaMask;
unsigned int ctbits; /* sample size for color table */
int topdown; /* top-down mode? */
};
typedef struct bmp_info_s bmp_info_t;
/* auxiliary */
static int bmp_count = 0; /* counter for byte padding */
static int bmp_pos = 0; /* counter from start of BMP data */
static int bmp_count = 0; /* counter for byte padding */
static int bmp_pos = 0; /* counter from start of BMP data */
/* read n-byte little-endian integer. Return 1 on EOF or error, else
* 0. Assume n<=4. */
static int bmp_readint( FILE* f, int n, unsigned int* p )
{
int i;
int i;
unsigned int sum = 0;
int b;
int b;
for( i = 0; i<n; i++ )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
sum += b << (8 * i);
}
bmp_count += n;
bmp_pos += n;
bmp_pos += n;
*p = sum;
return 0;
}
@ -471,16 +526,18 @@ static int bmp_pad( FILE* f )
int c, i, b;
c = (-bmp_count) & 3;
for( i = 0; i<c; i++ )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
}
bmp_pos += c;
bmp_pos += c;
bmp_count = 0;
return 0;
}
@ -494,10 +551,12 @@ static int bmp_forward( FILE* f, int pos )
while( bmp_pos < pos )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
bmp_pos++;
bmp_count++;
}
@ -506,11 +565,14 @@ static int bmp_forward( FILE* f, int pos )
}
#define TRY( x ) if( x ) \
#define TRY( x ) if( x ) \
goto try_error
#define TRY_EOF( x ) if( x ) \
#define TRY_EOF( x ) if( x ) \
goto eof
/* correct y-coordinate for top-down format */
#define ycorr( y ) (bmpinfo.topdown ? bmpinfo.h - 1 - y : y)
/* read BMP stream after magic number. Return values as for bm_read.
* We choose to be as permissive as possible, since there are many
* programs out there which produce BMP. For instance, ppmtobmp can
@ -520,16 +582,17 @@ static int bmp_forward( FILE* f, int pos )
* Windows BMP format. */
static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
{
bmp_info_t bmpinfo;
int* coltable;
unsigned int b, c;
unsigned int i;
bmp_info_t bmpinfo;
int* coltable;
unsigned int b, c;
unsigned int i;
potrace_bitmap_t* bm;
int mask;
unsigned int x, y;
unsigned int x, y;
int col[2];
unsigned int bitbuf;
unsigned int n;
unsigned int bitbuf;
unsigned int n;
unsigned int redshift, greenshift, blueshift;
int col1[2];
bm_read_error = NULL;
@ -545,7 +608,9 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
/* info header */
TRY( bmp_readint( f, 4, &bmpinfo.InfoSize ) );
if( bmpinfo.InfoSize == 40 || bmpinfo.InfoSize == 64 )
if( bmpinfo.InfoSize == 40 || bmpinfo.InfoSize == 64
|| bmpinfo.InfoSize == 108 || bmpinfo.InfoSize == 124 )
{
/* Windows or new OS/2 format */
bmpinfo.ctbits = 32; /* sample size in color table */
@ -559,6 +624,34 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
TRY( bmp_readint( f, 4, &bmpinfo.YpixelsPerM ) );
TRY( bmp_readint( f, 4, &bmpinfo.ncolors ) );
TRY( bmp_readint( f, 4, &bmpinfo.ColorsImportant ) );
if( bmpinfo.InfoSize >= 108 ) /* V4 and V5 bitmaps */
{
TRY( bmp_readint( f, 4, &bmpinfo.RedMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.GreenMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.BlueMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.AlphaMask ) );
}
if( bmpinfo.w > 0x7fffffff )
{
goto format_error;
}
if( bmpinfo.h > 0x7fffffff )
{
bmpinfo.h = (-bmpinfo.h) & 0xffffffff;
bmpinfo.topdown = 1;
}
else
{
bmpinfo.topdown = 0;
}
if( bmpinfo.h > 0x7fffffff )
{
goto format_error;
}
}
else if( bmpinfo.InfoSize == 12 )
{
@ -568,15 +661,22 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
TRY( bmp_readint( f, 2, &bmpinfo.h ) );
TRY( bmp_readint( f, 2, &bmpinfo.Planes ) );
TRY( bmp_readint( f, 2, &bmpinfo.bits ) );
bmpinfo.comp = 0;
bmpinfo.comp = 0;
bmpinfo.ncolors = 0;
bmpinfo.topdown = 0;
}
else
{
goto format_error;
}
/* forward to color table (i.e., if bmpinfo.InfoSize == 64) */
if( bmpinfo.comp == 3 && bmpinfo.InfoSize < 108 )
{
/* bitfield feature is only understood with V4 and V5 format */
goto format_error;
}
/* forward to color table (e.g., if bmpinfo.InfoSize == 64) */
TRY( bmp_forward( f, 14 + bmpinfo.InfoSize ) );
if( bmpinfo.Planes != 1 )
@ -593,7 +693,8 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
/* color table, present only if bmpinfo.bits <= 8. */
if( bmpinfo.bits <= 8 )
{
coltable = (int*) malloc( bmpinfo.ncolors * sizeof(int) );
coltable = (int*) calloc( bmpinfo.ncolors, sizeof(int) );
if( !coltable )
{
goto std_error;
@ -606,6 +707,7 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
TRY( bmp_readint( f, bmpinfo.ctbits / 8, &c ) );
c = ( (c >> 16) & 0xff ) + ( (c >> 8) & 0xff ) + (c & 0xff);
coltable[i] = (c > 3 * threshold * 255 ? 0 : 1);
if( i<2 )
{
col1[i] = c;
@ -621,6 +723,7 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
/* allocate bitmap */
bm = bm_new( bmpinfo.w, bmpinfo.h );
if( !bm )
{
goto std_error;
@ -636,6 +739,7 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
break;
case 0x001: /* monochrome palette */
if( col1[0] < col1[1] ) /* make the darker color black */
{
mask = 0xff;
@ -649,13 +753,14 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( i = 0; 8 * i<bmpinfo.w; i++ )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
b ^= mask;
*bm_index( bm, i * 8,
y ) |= ( (potrace_word) b ) <<
( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) );
ycorr( y ) ) |= ( (potrace_word) b ) <<
( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) );
}
TRY( bmp_pad( f ) );
@ -670,11 +775,13 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
case 0x006:
case 0x007:
case 0x008:
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
bitbuf = 0; /* bit buffer: bits in buffer are high-aligned */
n = 0; /* number of bits currently in bitbuffer */
for( x = 0; x<bmpinfo.w; x++ )
{
if( n < bmpinfo.bits )
@ -683,10 +790,11 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
bitbuf |= b << (INTBITS - 8 - n);
n += 8;
}
b = bitbuf >> (INTBITS - bmpinfo.bits);
bitbuf <<= bmpinfo.bits;
n -= bmpinfo.bits;
BM_UPUT( bm, x, y, coltable[b] );
BM_UPUT( bm, x, ycorr( y ), coltable[b] );
}
TRY( bmp_pad( f ) );
@ -695,7 +803,6 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
break;
case 0x010: /* 16-bit encoding */
/* can't do this format because it is not well-documented and I
* don't have any samples */
bm_read_error = "cannot handle bmp 16-bit coding";
@ -704,14 +811,39 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
case 0x018: /* 24-bit encoding */
case 0x020: /* 32-bit encoding */
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( x = 0; x<bmpinfo.w; x++ )
{
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
c = ( (c >> 16) & 0xff ) + ( (c >> 8) & 0xff ) + (c & 0xff);
BM_UPUT( bm, x, y, c > 3 * threshold * 255 ? 0 : 1 );
BM_UPUT( bm, x, ycorr( y ), c > 3 * threshold * 255 ? 0 : 1 );
}
TRY( bmp_pad( f ) );
}
break;
case 0x320: /* 32-bit encoding with bitfields */
redshift = lobit( bmpinfo.RedMask );
greenshift = lobit( bmpinfo.GreenMask );
blueshift = lobit( bmpinfo.BlueMask );
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( x = 0; x<bmpinfo.w; x++ )
{
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
c = ( (c & bmpinfo.RedMask) >> redshift ) +
( (c & bmpinfo.GreenMask) >> greenshift ) +
( (c & bmpinfo.BlueMask) >> blueshift );
BM_UPUT( bm, x, ycorr( y ), c > 3 * threshold * 255 ? 0 : 1 );
}
TRY( bmp_pad( f ) );
@ -720,17 +852,20 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
break;
case 0x204: /* 4-bit runlength compressed encoding (RLE4) */
x = 0;
y = 0;
x = 0;
y = 0;
while( 1 )
{
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
if( b>0 )
{
/* repeat count */
col[0] = coltable[(c >> 4) & 0xf];
col[1] = coltable[c & 0xf];
col[0] = coltable[(c >> 4) & 0xf];
col[1] = coltable[c & 0xf];
for( i = 0; i<b && x<bmpinfo.w; i++ )
{
if( x>=bmpinfo.w )
@ -738,11 +873,13 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
BM_UPUT( bm, x, y, col[i & 1] );
BM_UPUT( bm, x, ycorr( y ), col[i & 1] );
x++;
}
}
@ -762,8 +899,8 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
/* "delta": skip pixels in x and y directions */
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
x += b;
y += c;
x += b;
y += c;
}
else
{
@ -774,16 +911,19 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
}
if( x>=bmpinfo.w )
{
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
BM_PUT( bm, x, y, coltable[( b >> ( 4 - 4 * (i & 1) ) ) & 0xf] );
BM_PUT( bm, x, ycorr( y ), coltable[( b >> ( 4 - 4 * (i & 1) ) ) & 0xf] );
x++;
}
@ -798,12 +938,14 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
break;
case 0x108: /* 8-bit runlength compressed encoding (RLE8) */
x = 0;
y = 0;
x = 0;
y = 0;
while( 1 )
{
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
if( b>0 )
{
/* repeat count */
@ -814,11 +956,13 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
BM_UPUT( bm, x, y, coltable[c] );
BM_UPUT( bm, x, ycorr( y ), coltable[c] );
x++;
}
}
@ -838,8 +982,8 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
/* "delta": skip pixels in x and y directions */
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
x += b;
y += c;
x += b;
y += c;
}
else
{
@ -847,16 +991,19 @@ static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
for( i = 0; i<c; i++ )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
if( x>=bmpinfo.w )
{
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
BM_PUT( bm, x, y, coltable[b] );
BM_PUT( bm, x, ycorr( y ), coltable[b] );
x++;
}
@ -888,10 +1035,12 @@ format_error:
try_error:
free( coltable );
free( bm );
if( !bm_read_error )
{
bm_read_error = "invalid bmp file";
}
return -2;
std_error:
@ -908,24 +1057,23 @@ void bm_writepbm( FILE* f, potrace_bitmap_t* bm )
{
int w, h, bpr, y, i, c;
w = bm->w;
h = bm->h;
w = bm->w;
h = bm->h;
bpr = (w + 7) / 8;
fprintf( f, "P4\n%d %d\n", w, h );
for( y = h - 1; y>=0; y-- )
{
for( i = 0; i<bpr; i++ )
{
c =
( *bm_index( bm, i * 8,
y ) >> ( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) ) ) & 0xff;
y ) >> ( 8 * ( BM_WORDSIZE - 1 - (i % BM_WORDSIZE) ) ) ) & 0xff;
fputc( c, f );
}
}
return;
}
@ -940,14 +1088,15 @@ int bm_print( FILE* f, potrace_bitmap_t* bm )
int d;
int sw, sh;
sw = bm->w < 79 ? bm->w : 79;
sh = bm->w < 79 ? bm->h : bm->h * sw * 44 / (79 * bm->w);
sw = bm->w < 79 ? bm->w : 79;
sh = bm->w < 79 ? bm->h : bm->h * sw * 44 / (79 * bm->w);
for( yy = sh - 1; yy>=0; yy-- )
{
for( xx = 0; xx<sw; xx++ )
{
d = 0;
for( x = xx * bm->w / sw; x<(xx + 1) * bm->w / sw; x++ )
{
for( y = yy * bm->h / sh; y<(yy + 1) * bm->h / sh; y++ )

View File

@ -1,23 +1,22 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: bitmap_io.h 147 2007-04-09 00:44:09Z selinger $ */
/* bitmap input/output functions */
#ifndef BITMAP_IO_H
#define BITMAP_IO_H
#include <stdio.h>
#include <bitmap.h>
#include "bitmap.h"
/* Note that bitmaps are stored bottom to top, i.e., the first
* scanline is the bottom-most one */
extern char* bm_read_error;
extern const char* bm_read_error;
int bm_read( FILE* f, double blacklevel, potrace_bitmap_t** bmp );
void bm_writepbm( FILE* f, potrace_bitmap_t* bm );
int bm_print( FILE* f, potrace_bitmap_t* bm );
int bm_read( FILE* f, double blacklevel, potrace_bitmap_t** bmp );
void bm_writepbm( FILE* f, potrace_bitmap_t* bm );
int bm_print( FILE* f, potrace_bitmap_t* bm );
#endif /* BITMAP_IO_H */

View File

@ -1,50 +1,102 @@
/* Copyright (C) 2001-2007 Peter Selinger.
This file is part of Potrace. It is free software and it is covered
by the GNU General Public License. See the file COPYING for details. */
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: bitops.h 147 2007-04-09 00:44:09Z selinger $ */
/* bits.h: this file defines some macros for bit manipulations. We
provide a generic implementation */
* provide a generic implementation, as well as machine- and
* compiler-specific fast implementations */
/* lobit: return the position of the rightmost "1" bit of an int, or
32 if none. hibit: return 1 + the position of the leftmost "1" bit
of an int, or 0 if none. Note: these functions work on 32-bit
integers. */
* 32 if none. hibit: return 1 + the position of the leftmost "1" bit
* of an int, or 0 if none. Note: these functions work on 32-bit
* integers. */
#ifndef BITOPS_H
#define BITOPS_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#include "config.h"
#endif
/* generic macros */
/* ---------------------------------------------------------------------- */
/* machine specific macros */
static inline unsigned int lobit(unsigned int x) {
unsigned int res = 32;
while (x & 0xffffff) {
x <<= 8;
res -= 8;
}
while (x) {
x <<= 1;
res -= 1;
}
return res;
#if defined(HAVE_I386)
static inline unsigned int lobit( unsigned int x )
{
unsigned int res;
asm ("bsf %1,%0\n\t"
"jnz 0f\n\t"
"movl $32,%0\n"
"0:"
: "=r" (res)
: "r" (x)
: "cc");
return res;
}
static inline unsigned int hibit(unsigned int x) {
unsigned int res = 0;
while (x > 0xff) {
x >>= 8;
res += 8;
}
while (x) {
x >>= 1;
res += 1;
}
return res;
static inline unsigned int hibit( unsigned int x )
{
unsigned int res;
asm ("bsr %1,%0\n\t"
"jnz 0f\n\t"
"movl $-1,%0\n"
"0:"
: "=r" (res)
: "r" (x)
: "cc");
return res + 1;
}
/* ---------------------------------------------------------------------- */
#else /* generic macros */
static inline unsigned int lobit( unsigned int x )
{
unsigned int res = 32;
while( x & 0xffffff )
{
x <<= 8;
res -= 8;
}
while( x )
{
x <<= 1;
res -= 1;
}
return res;
}
static inline unsigned int hibit( unsigned int x )
{
unsigned int res = 0;
while( x > 0xff )
{
x >>= 8;
res += 8;
}
while( x )
{
x >>= 1;
res += 1;
}
return res;
}
#endif
#endif /* BITOPS_H */

View File

@ -1,38 +1,37 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: curve.c 147 2007-04-09 00:44:09Z selinger $ */
/* private part of the path and curve data structures */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <potracelib.h>
#include <lists.h>
#include <curve.h>
#include "potracelib.h"
#include "lists.h"
#include "curve.h"
#define SAFE_MALLOC( var, n, typ ) \
if( ( var = (typ*) malloc( (n)* sizeof(typ) ) ) == NULL ) \
goto malloc_error
#define SAFE_CALLOC( var, n, typ ) \
if( ( var = (typ*) calloc( n, sizeof(typ) ) ) == NULL ) \
goto calloc_error
/* ---------------------------------------------------------------------- */
/* allocate and free path objects */
path_t* path_new( void )
{
path_t* p = NULL;
path_t* p = NULL;
privpath_t* priv = NULL;
SAFE_MALLOC( p, 1, path_t );
SAFE_CALLOC( p, 1, path_t );
memset( p, 0, sizeof(path_t) );
SAFE_MALLOC( priv, 1, privpath_t );
SAFE_CALLOC( priv, 1, privpath_t );
memset( priv, 0, sizeof(privpath_t) );
p->priv = priv;
return p;
malloc_error:
calloc_error:
free( p );
free( priv );
return NULL;
@ -65,9 +64,11 @@ void path_free( path_t* p )
privcurve_free_members( &p->priv->curve );
privcurve_free_members( &p->priv->ocurve );
}
free( p->priv );
/* do not free p->fcurve ! */
}
free( p );
}
@ -94,15 +95,15 @@ int privcurve_init( privcurve_t* curve, int n )
{
memset( curve, 0, sizeof(privcurve_t) );
curve->n = n;
SAFE_MALLOC( curve->tag, n, int );
SAFE_MALLOC( curve->c, n, dpoint3_t );
SAFE_MALLOC( curve->vertex, n, dpoint_t );
SAFE_MALLOC( curve->alpha, n, double );
SAFE_MALLOC( curve->alpha0, n, double );
SAFE_MALLOC( curve->beta, n, double );
SAFE_CALLOC( curve->tag, n, int );
SAFE_CALLOC( curve->c, n, dpoint3_t );
SAFE_CALLOC( curve->vertex, n, dpoint_t );
SAFE_CALLOC( curve->alpha, n, double );
SAFE_CALLOC( curve->alpha0, n, double );
SAFE_CALLOC( curve->beta, n, double );
return 0;
malloc_error:
calloc_error:
free( curve->tag );
free( curve->c );
free( curve->vertex );
@ -116,7 +117,7 @@ malloc_error:
/* copy private to public curve structure */
void privcurve_to_curve( privcurve_t* pc, potrace_curve_t* c )
{
c->n = pc->n;
c->n = pc->n;
c->tag = pc->tag;
c->c = pc->c;
c->c = pc->c;
}

View File

@ -1,11 +1,11 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
#ifndef CURVE_H
#define CURVE_H
#include <auxiliary.h>
#include "auxiliary.h"
/* vertex is c[1] for tag=POTRACE_CORNER, and the intersection of
* .c[-1][2]..c[0] and c[1]..c[2] for tag=POTRACE_CURVETO. alpha is only
@ -17,29 +17,28 @@
struct privcurve_s
{
int n; /* number of segments */
int* tag; /* tag[n]: POTRACE_CORNER or POTRACE_CURVETO */
dpoint_t( * c )[3]; /* c[n][i]: control points.
* c[n][0] is unused for tag[n]=POTRACE_CORNER */
int n; /* number of segments */
int* tag; /* tag[n]: POTRACE_CORNER or POTRACE_CURVETO */
dpoint_t( *c )[3]; /* c[n][i]: control points.
* c[n][0] is unused for tag[n]=POTRACE_CORNER */
/* the remainder of this structure is special to privcurve, and is
* used in EPS debug output and special EPS "short coding". These
* fields are valid only if "alphacurve" is set. */
int alphacurve; /* have the following fields been initialized? */
dpoint_t* vertex; /* for POTRACE_CORNER, this equals c[1] */
double* alpha; /* only for POTRACE_CURVETO */
double* alpha0; /* "uncropped" alpha parameter - for debug output only */
double* beta;
int alphacurve; /* have the following fields been initialized? */
dpoint_t* vertex; /* for POTRACE_CORNER, this equals c[1] */
double* alpha; /* only for POTRACE_CURVETO */
double* alpha0; /* "uncropped" alpha parameter - for debug output only */
double* beta;
};
typedef struct privcurve_s privcurve_t;
struct sums_s
{
double x;
double y;
double x2;
double xy;
double y2;
double x;
double y;
double x2;
double xy;
double y2;
};
typedef struct sums_s sums_t;
@ -50,26 +49,26 @@ typedef struct sums_s sums_t;
* other fields. */
struct potrace_privpath_s
{
int len;
int len;
point_t* pt; /* pt[len]: path as extracted from bitmap */
int* lon; /* lon[len]: (i,lon[i]) = longest straight line from i */
int* lon; /* lon[len]: (i,lon[i]) = longest straight line from i */
int x0, y0; /* origin for sums */
int x0, y0; /* origin for sums */
sums_t* sums; /* sums[len+1]: cache for fast summing */
int m; /* length of optimal polygon */
int m; /* length of optimal polygon */
int* po; /* po[m]: optimal polygon */
privcurve_t curve; /* curve[m]: array of curve elements */
privcurve_t ocurve; /* ocurve[om]: array of curve elements */
privcurve_t* fcurve; /* final curve: this points to either curve or
* ocurve. Do not free this separately. */
privcurve_t curve; /* curve[m]: array of curve elements */
privcurve_t ocurve; /* ocurve[om]: array of curve elements */
privcurve_t* fcurve; /* final curve: this points to either curve or
* ocurve. Do not free this separately. */
};
typedef struct potrace_privpath_s potrace_privpath_t;
/* shorter names */
typedef potrace_privpath_t privpath_t;
typedef potrace_path_t path_t;
typedef potrace_privpath_t privpath_t;
typedef potrace_path_t path_t;
path_t* path_new( void );
void path_free( path_t* p );

View File

@ -1,21 +1,50 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: decompose.c 146 2007-04-09 00:43:46Z selinger $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <potracelib.h>
#include <curve.h>
#include <lists.h>
#include <auxiliary.h>
#include <bitmap.h>
#include <decompose.h>
#include <progress.h>
#include "potracelib.h"
#include "curve.h"
#include "lists.h"
#include "bitmap.h"
#include "decompose.h"
#include "progress.h"
/* ---------------------------------------------------------------------- */
/* deterministically and efficiently hash (x,y) into a pseudo-random bit */
static inline int detrand( int x, int y )
{
unsigned int z;
static const unsigned char t[256] =
{
/* non-linear sequence: constant term of inverse in GF(8),
* mod x^8+x^4+x^3+x+1 */
0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1,
0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1,
0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
};
/* 0x04b3e375 and 0x05a8ef93 are chosen to contain every possible
* 5-bit sequence */
z = ( (0x04b3e375 * x) ^ y ) * 0x05a8ef93;
z = t[z & 0xff] ^ t[(z >> 8) & 0xff] ^ t[(z >> 16) & 0xff] ^ t[(z >> 24) & 0xff];
return z;
}
/* ---------------------------------------------------------------------- */
/* auxiliary bitmap manipulations */
@ -24,11 +53,12 @@
static void bm_clearexcess( potrace_bitmap_t* bm )
{
potrace_word mask;
int y;
int y;
if( bm->w % BM_WORDBITS != 0 )
{
mask = BM_ALLBITS << ( BM_WORDBITS - (bm->w % BM_WORDBITS) );
for( y = 0; y<bm->h; y++ )
{
*bm_index( bm, bm->w, y ) &= mask;
@ -64,35 +94,6 @@ static void clear_bm_with_bbox( potrace_bitmap_t* bm, bbox_t* bbox )
/* ---------------------------------------------------------------------- */
/* auxiliary functions */
/* deterministically and efficiently hash (x,y) into a pseudo-random bit */
static inline int detrand( int x, int y )
{
unsigned int z;
static const unsigned char t[256] =
{
/* non-linear sequence: constant term of inverse in GF(8),
* mod x^8+x^4+x^3+x+1 */
0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1,
0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1,
0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
};
/* 0x04b3e375 and 0x05a8ef93 are chosen to contain every possible
* 5-bit sequence */
z = ( (0x04b3e375 * x) ^ y ) * 0x05a8ef93;
z = t[z & 0xff] ^ t[(z >> 8) & 0xff] ^ t[(z >> 16) & 0xff] ^ t[(z >> 24) & 0xff];
return z & 1;
}
/* return the "majority" value of bitmap bm at intersection (x,y). We
* assume that the bitmap is balanced at "radius" 1. */
static int majority( potrace_bitmap_t* bm, int x, int y )
@ -102,12 +103,13 @@ static int majority( potrace_bitmap_t* bm, int x, int y )
for( i = 2; i<5; i++ ) /* check at "radius" i */
{
ct = 0;
for( a = -i + 1; a<=i - 1; a++ )
{
ct += BM_GET( bm, x + a, y + i - 1 ) ? 1 : -1;
ct += BM_GET( bm, x + i - 1, y + a - 1 ) ? 1 : -1;
ct += BM_GET( bm, x + a - 1, y - i ) ? 1 : -1;
ct += BM_GET( bm, x - i, y + a ) ? 1 : -1;
ct += BM_GET( bm, x + a, y + i - 1 ) ? 1 : -1;
ct += BM_GET( bm, x + i - 1, y + a - 1 ) ? 1 : -1;
ct += BM_GET( bm, x + a - 1, y - i ) ? 1 : -1;
ct += BM_GET( bm, x - i, y + a ) ? 1 : -1;
}
if( ct>0 )
@ -179,6 +181,7 @@ static void xor_path( potrace_bitmap_t* bm, path_t* p )
y1 = p->priv->pt[p->priv->len - 1].y;
xa = p->priv->pt[0].x & - BM_WORDBITS;
for( k = 0; k<p->priv->len; k++ )
{
x = p->priv->pt[k].x;
@ -215,14 +218,17 @@ static void setbbox_path( bbox_t* bbox, path_t* p )
{
bbox->x0 = x;
}
if( x > bbox->x1 )
{
bbox->x1 = x;
}
if( y < bbox->y0 )
{
bbox->y0 = y;
}
if( y > bbox->y1 )
{
bbox->y1 = y;
@ -239,18 +245,18 @@ static void setbbox_path( bbox_t* bbox, path_t* p )
* of turnpolicies. */
static path_t* findpath( potrace_bitmap_t* bm, int x0, int y0, int sign, int turnpolicy )
{
int x, y, dirx, diry, len, size, area;
int c, d, tmp;
int x, y, dirx, diry, len, size, area;
int c, d, tmp;
point_t* pt, * pt1;
path_t* p = NULL;
path_t* p = NULL;
x = x0;
y = y0;
x = x0;
y = y0;
dirx = 0;
diry = -1;
len = size = 0;
pt = NULL;
len = size = 0;
pt = NULL;
area = 0;
while( 1 )
@ -259,21 +265,24 @@ static path_t* findpath( potrace_bitmap_t* bm, int x0, int y0, int sign, int tur
if( len>=size )
{
size += 100;
size = (int) ( 1.3 * size );
pt1 = (point_t*) realloc( pt, size * sizeof(point_t) );
size = (int) (1.3 * size);
pt1 = (point_t*) realloc( pt, size * sizeof(point_t) );
if( !pt1 )
{
goto error;
}
pt = pt1;
}
pt[len].x = x;
pt[len].y = y;
len++;
/* move to next point */
x += dirx;
y += diry;
x += dirx;
y += diry;
area += x * diry;
/* path complete? */
@ -289,32 +298,32 @@ static path_t* findpath( potrace_bitmap_t* bm, int x0, int y0, int sign, int tur
if( c && !d ) /* ambiguous turn */
{
if( turnpolicy == POTRACE_TURNPOLICY_RIGHT
|| (turnpolicy == POTRACE_TURNPOLICY_BLACK && sign == '+')
|| (turnpolicy == POTRACE_TURNPOLICY_WHITE && sign == '-')
|| ( turnpolicy == POTRACE_TURNPOLICY_RANDOM && detrand( x, y ) )
|| ( turnpolicy == POTRACE_TURNPOLICY_MAJORITY && majority( bm, x, y ) )
|| ( turnpolicy == POTRACE_TURNPOLICY_MINORITY && !majority( bm, x, y ) ) )
|| (turnpolicy == POTRACE_TURNPOLICY_BLACK && sign == '+')
|| (turnpolicy == POTRACE_TURNPOLICY_WHITE && sign == '-')
|| ( turnpolicy == POTRACE_TURNPOLICY_RANDOM && detrand( x, y ) )
|| ( turnpolicy == POTRACE_TURNPOLICY_MAJORITY && majority( bm, x, y ) )
|| ( turnpolicy == POTRACE_TURNPOLICY_MINORITY && !majority( bm, x, y ) ) )
{
tmp = dirx; /* right turn */
tmp = dirx; /* right turn */
dirx = diry;
diry = -tmp;
}
else
{
tmp = dirx; /* left turn */
tmp = dirx; /* left turn */
dirx = -diry;
diry = tmp;
}
}
else if( c ) /* right turn */
{
tmp = dirx;
tmp = dirx;
dirx = diry;
diry = -tmp;
}
else if( !d ) /* left turn */
{
tmp = dirx;
tmp = dirx;
dirx = -diry;
diry = tmp;
}
@ -322,12 +331,13 @@ static path_t* findpath( potrace_bitmap_t* bm, int x0, int y0, int sign, int tur
/* allocate new path object */
p = path_new();
if( !p )
{
goto error;
}
p->priv->pt = pt;
p->priv->pt = pt;
p->priv->len = len;
p->area = area;
p->sign = sign;
@ -358,18 +368,19 @@ error:
static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
{
path_t* p, * p1;
path_t* heap, * heap1;
path_t* cur;
path_t* head;
path_t** hook, ** hook_in, ** hook_out; /* for fast appending to linked list */
bbox_t bbox;
path_t* p, * p1;
path_t* heap, * heap1;
path_t* cur;
path_t* head;
path_t** plist_hook; /* for fast appending to linked list */
path_t** hook_in, ** hook_out; /* for fast appending to linked list */
bbox_t bbox;
bm_clear( bm, 0 );
/* save original "next" pointers */
list_forall( p, plist ) {
p->sibling = p->next;
p->sibling = p->next;
p->childlist = NULL;
}
@ -385,13 +396,13 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
while( heap )
{
/* unlink first sublist */
cur = heap;
cur = heap;
heap = heap->childlist;
cur->childlist = NULL;
/* unlink first path */
head = cur;
cur = cur->next;
cur = cur->next;
head->next = NULL;
/* render path */
@ -401,7 +412,7 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
/* now do insideness test for each element of cur; append it to
* head->childlist if it's inside head, else append it to
* head->next. */
hook_in = &head->childlist;
hook_in = &head->childlist;
hook_out = &head->next;
list_forall_unlink( p, cur ) {
if( p->priv->pt[0].y <= bbox.y0 )
@ -411,6 +422,7 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
*hook_out = cur;
break;
}
if( BM_GET( bm, p->priv->pt[0].x, p->priv->pt[0].y - 1 ) )
{
list_insert_beforehook( p, hook_in );
@ -431,6 +443,7 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
head->next->childlist = heap;
heap = head->next;
}
if( head->childlist )
{
head->childlist->childlist = heap;
@ -440,6 +453,7 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
/* copy sibling structure from "next" to "sibling" component */
p = plist;
while( p )
{
p1 = p->sibling;
@ -453,26 +467,31 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
* contains a list of childlists which still need to be
* processed. */
heap = plist;
if( heap )
{
heap->next = NULL; /* heap is a linked list of childlists */
}
plist = NULL;
hook = &plist;
plist_hook = &plist;
while( heap )
{
heap1 = heap->next;
for( p = heap; p; p = p->sibling )
{
/* p is a positive path */
/* append to linked list */
list_insert_beforehook( p, hook );
list_insert_beforehook( p, plist_hook );
/* go through its children */
for( p1 = p->childlist; p1; p1 = p1->sibling )
{
/* append to linked list */
list_insert_beforehook( p1, hook );
list_insert_beforehook( p1, plist_hook );
/* append its childlist to heap, if non-empty */
if( p1->childlist )
{
@ -483,8 +502,6 @@ static void pathlist_to_tree( path_t* plist, potrace_bitmap_t* bm )
heap = heap1;
}
return;
}
@ -497,10 +514,13 @@ static int findnext( potrace_bitmap_t* bm, int* xp, int* yp )
{
int x;
int y;
int x0;
x0 = (*xp) & ~(BM_WORDBITS - 1);
for( y = *yp; y>=0; y-- )
{
for( x = 0; x<bm->w; x += BM_WORDBITS )
for( x = x0; x<bm->w; x += BM_WORDBITS )
{
if( *bm_index( bm, x, y ) )
{
@ -515,6 +535,8 @@ static int findnext( potrace_bitmap_t* bm, int* xp, int* yp )
return 0;
}
}
x0 = 0;
}
/* not found */
@ -528,19 +550,20 @@ static int findnext( potrace_bitmap_t* bm, int* xp, int* yp )
* set. */
int bm_to_pathlist( const potrace_bitmap_t* bm,
path_t** plistp,
const potrace_param_t* param,
progress_t* progress )
path_t** plistp,
const potrace_param_t* param,
progress_t* progress )
{
int x;
int y;
path_t* p;
path_t* plist = NULL; /* linked list of path objects */
path_t** hook = &plist; /* used to speed up appending to linked list */
potrace_bitmap_t* bm1 = NULL;
int sign;
path_t* p;
path_t* plist = NULL; /* linked list of path objects */
path_t** plist_hook = &plist; /* used to speed up appending to linked list */
potrace_bitmap_t* bm1 = NULL;
int sign;
bm1 = bm_dup( bm );
if( !bm1 )
{
goto error;
@ -551,7 +574,9 @@ int bm_to_pathlist( const potrace_bitmap_t* bm,
bm_clearexcess( bm1 );
/* iterate through components */
x = 0;
y = bm1->h - 1;
while( findnext( bm1, &x, &y ) == 0 )
{
/* calculate the sign by looking at the original */
@ -559,6 +584,7 @@ int bm_to_pathlist( const potrace_bitmap_t* bm,
/* calculate the path */
p = findpath( bm1, x, y + 1, sign, param->turnpolicy );
if( p==NULL )
{
goto error;
@ -574,7 +600,7 @@ int bm_to_pathlist( const potrace_bitmap_t* bm,
}
else
{
list_insert_beforehook( p, hook );
list_insert_beforehook( p, plist_hook );
}
if( bm1->h > 0 ) /* to be sure */

View File

@ -1,18 +1,18 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: decompose.h 147 2007-04-09 00:44:09Z selinger $ */
#ifndef DECOMPOSE_H
#define DECOMPOSE_H
#include <potracelib.h>
#include <progress.h>
#include "potracelib.h"
#include "progress.h"
#include "curve.h"
int bm_to_pathlist( const potrace_bitmap_t* bm,
path_t** plistp,
const potrace_param_t* param,
progress_t* progress );
path_t** plistp,
const potrace_param_t* param,
progress_t* progress );
#endif /* DECOMPOSE_H */

View File

@ -1,51 +1,60 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: greymap.c 147 2007-04-09 00:44:09Z selinger $ */
/* Routines for manipulating greymaps, including reading pgm files. We
* only deal with greymaps of depth 8 bits. */
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <cmath>
#include <math.h>
#include <errno.h>
#include <greymap.h>
#include "greymap.h"
#include "bitops.h"
#define INTBITS ( 8 * sizeof(int) )
#define mod( a, n ) ( (a)>=(n) ? (a) % (n) : (a)>=0 ? (a) : (n) - 1 - ( -1 - (a) ) % (n) )
static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic );
static int gm_readbody_bmp( FILE* f, greymap_t** gmp );
static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic );
static int gm_readbody_bmp( FILE* f, greymap_t** gmp );
/* ---------------------------------------------------------------------- */
/* basic greymap routines */
/* return new un-initialized greymap. NULL with errno on error */
/* return new un-initialized greymap. NULL with errno on error.
* Assumes w, h >= 0. */
greymap_t* gm_new( int w, int h )
{
greymap_t* gm;
int errno_save;
ssize_t size = (ssize_t) w * (ssize_t) h * (ssize_t) sizeof(signed short int);
/* check for overflow error */
if( size < 0 || size / w / h != sizeof(signed short int) )
{
errno = ENOMEM;
return NULL;
}
gm = (greymap_t*) malloc( sizeof(greymap_t) );
if( !gm )
{
return NULL;
}
gm->w = w;
gm->h = h;
gm->map = (signed short int*) malloc( w * h * sizeof(signed short int) );
gm->map = (signed short int*) malloc( size );
if( !gm->map )
{
errno_save = errno;
free( gm );
errno = errno_save;
return NULL;
}
return gm;
}
@ -57,6 +66,7 @@ void gm_free( greymap_t* gm )
{
free( gm->map );
}
free( gm );
}
@ -70,7 +80,8 @@ greymap_t* gm_dup( greymap_t* gm )
{
return NULL;
}
memcpy( gm1->map, gm->map, gm->w * gm->h * 2 );
memcpy( gm1->map, gm->map, gm->w * gm->h * sizeof(signed short int) );
return gm1;
}
@ -82,7 +93,7 @@ void gm_clear( greymap_t* gm, int b )
if( b==0 )
{
memset( gm->map, 0, gm->w * gm->h * 2 );
memset( gm->map, 0, gm->w * gm->h * sizeof(signed short int) );
}
else
{
@ -106,17 +117,20 @@ static int fgetc_ws( FILE* f )
while( 1 )
{
c = fgetc( f );
if( c=='#' )
{
while( 1 )
{
c = fgetc( f );
if( c=='\n' || c==EOF )
{
break;
}
}
}
/* space, tab, line feed, carriage return, form-feed */
if( c!=' ' && c!='\t' && c!='\r' && c!='\n' && c!=12 )
{
@ -140,10 +154,12 @@ static int readnum( FILE* f )
while( 1 )
{
c = fgetc_ws( f );
if( c==EOF )
{
return -1;
}
if( c>='0' && c<='9' )
{
break;
@ -152,18 +168,22 @@ static int readnum( FILE* f )
/* first digit is already in c */
acc = c - '0';
while( 1 )
{
c = fgetc( f );
if( c==EOF )
{
break;
}
if( c<'0' || c>'9' )
{
ungetc( c, f );
break;
}
acc *= 10;
acc += c - '0';
}
@ -183,10 +203,12 @@ static int readbit( FILE* f )
while( 1 )
{
c = fgetc_ws( f );
if( c==EOF )
{
return -1;
}
if( c>='0' && c<='1' )
{
break;
@ -219,20 +241,25 @@ int gm_read( FILE* f, greymap_t** gmp )
* by the PNM standard, but there is no harm in being lenient. */
magic[0] = fgetc_ws( f );
if( magic[0] == EOF )
{
/* files which contain only comments and whitespace count as "empty" */
return -3;
}
magic[1] = fgetc( f );
if( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' )
{
return gm_readbody_pnm( f, gmp, magic[1] );
}
if( magic[0] == 'B' && magic[1] == 'M' )
{
return gm_readbody_bmp( f, gmp );
}
return -4;
}
@ -244,19 +271,21 @@ int gm_read( FILE* f, greymap_t** gmp )
static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
{
greymap_t* gm;
int x, y, i, j, b, b1, sum;
int bpr; /* bytes per row (as opposed to 4*gm->c) */
int w, h, max;
int x, y, i, j, b, b1, sum;
int bpr; /* bytes per row (as opposed to 4*gm->c) */
int w, h, max;
gm = NULL;
w = readnum( f );
if( w<0 )
{
goto format_error;
}
h = readnum( f );
if( h<0 )
{
goto format_error;
@ -264,6 +293,7 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* allocate greymap */
gm = gm_new( w, h );
if( !gm )
{
return -1;
@ -286,10 +316,12 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( x = 0; x<w; x++ )
{
b = readbit( f );
if( b<0 )
{
goto eof;
}
GM_UPUT( gm, x, y, b ? 0 : 255 );
}
}
@ -300,6 +332,7 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* read P2 format: PGM ascii */
max = readnum( f );
if( max<1 )
{
goto format_error;
@ -310,10 +343,12 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( x = 0; x<w; x++ )
{
b = readnum( f );
if( b<0 )
{
goto eof;
}
GM_UPUT( gm, x, y, b * 255 / max );
}
}
@ -324,6 +359,7 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* read P3 format: PPM ascii */
max = readnum( f );
if( max<1 )
{
goto format_error;
@ -334,13 +370,16 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( x = 0; x<w; x++ )
{
sum = 0;
for( i = 0; i<3; i++ )
{
b = readnum( f );
if( b<0 )
{
goto eof;
}
sum += b;
}
@ -354,6 +393,7 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* read P4 format: PBM raw */
b = fgetc( f ); /* read single white-space character after height */
if( b==EOF )
{
goto format_error;
@ -366,13 +406,15 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( i = 0; i<bpr; i++ )
{
b = fgetc( f );
if( b==EOF )
{
goto eof;
}
for( j = 0; j<8; j++ )
{
GM_PUT( gm, i * 8 + j, y, b & (0x80 >> j) ? 0 : 255 );
GM_PUT( gm, i * 8 + j, y, (b & (0x80 >> j)) ? 0 : 255 );
}
}
}
@ -383,12 +425,14 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* read P5 format: PGM raw */
max = readnum( f );
if( max<1 )
{
goto format_error;
}
b = fgetc( f ); /* read single white-space character after max */
if( b==EOF )
{
goto format_error;
@ -399,16 +443,21 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( x = 0; x<w; x++ )
{
b = fgetc( f );
if( b==EOF )
goto eof;
if( max>=256 )
{
b <<= 8;
b <<= 8;
b1 = fgetc( f );
if( b1==EOF )
goto eof;
b |= b1;
}
GM_UPUT( gm, x, y, b * 255 / max );
}
}
@ -419,12 +468,14 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
/* read P6 format: PPM raw */
max = readnum( f );
if( max<1 )
{
goto format_error;
}
b = fgetc( f ); /* read single white-space character after max */
if( b==EOF )
{
goto format_error;
@ -435,21 +486,27 @@ static int gm_readbody_pnm( FILE* f, greymap_t** gmp, int magic )
for( x = 0; x<w; x++ )
{
sum = 0;
for( i = 0; i<3; i++ )
{
b = fgetc( f );
if( b==EOF )
{
goto eof;
}
if( max>=256 )
{
b <<= 8;
b <<= 8;
b1 = fgetc( f );
if( b1==EOF )
goto eof;
b |= b1;
}
sum += b;
}
@ -469,6 +526,7 @@ eof:
format_error:
gm_free( gm );
if( magic == '1' || magic == '4' )
{
gm_read_error = "invalid pbm file";
@ -481,6 +539,7 @@ format_error:
{
gm_read_error = "invalid ppm file";
}
return -2;
}
@ -490,49 +549,56 @@ format_error:
struct bmp_info_s
{
unsigned int FileSize;
unsigned int reserved;
unsigned int DataOffset;
unsigned int InfoSize;
unsigned int w; /* width */
unsigned int h; /* height */
unsigned int Planes;
unsigned int bits; /* bits per sample */
unsigned int comp; /* compression mode */
unsigned int ImageSize;
unsigned int XpixelsPerM;
unsigned int YpixelsPerM;
unsigned int ncolors; /* number of colors in palette */
unsigned int ColorsImportant;
unsigned int ctbits; /* sample size for color table */
unsigned int FileSize;
unsigned int reserved;
unsigned int DataOffset;
unsigned int InfoSize;
unsigned int w; /* width */
unsigned int h; /* height */
unsigned int Planes;
unsigned int bits; /* bits per sample */
unsigned int comp; /* compression mode */
unsigned int ImageSize;
unsigned int XpixelsPerM;
unsigned int YpixelsPerM;
unsigned int ncolors; /* number of colors in palette */
unsigned int ColorsImportant;
unsigned int RedMask;
unsigned int GreenMask;
unsigned int BlueMask;
unsigned int AlphaMask;
unsigned int ctbits; /* sample size for color table */
int topdown; /* top-down mode? */
};
typedef struct bmp_info_s bmp_info_t;
/* auxiliary */
static int bmp_count = 0; /* counter for byte padding */
static int bmp_pos = 0; /* counter from start of BMP data */
static int bmp_count = 0; /* counter for byte padding */
static int bmp_pos = 0; /* counter from start of BMP data */
/* read n-byte little-endian integer. Return 1 on EOF or error, else
* 0. Assume n<=4. */
static int bmp_readint( FILE* f, int n, unsigned int* p )
{
int i;
int i;
unsigned int sum = 0;
int b;
int b;
for( i = 0; i<n; i++ )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
sum += b << (8 * i);
}
bmp_count += n;
bmp_pos += n;
bmp_pos += n;
*p = sum;
return 0;
}
@ -552,16 +618,18 @@ static int bmp_pad( FILE* f )
int c, i, b;
c = (-bmp_count) & 3;
for( i = 0; i<c; i++ )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
}
bmp_pos += c;
bmp_pos += c;
bmp_count = 0;
return 0;
}
@ -575,10 +643,12 @@ static int bmp_forward( FILE* f, int pos )
while( bmp_pos < pos )
{
b = fgetc( f );
if( b==EOF )
{
return 1;
}
bmp_pos++;
bmp_count++;
}
@ -587,11 +657,14 @@ static int bmp_forward( FILE* f, int pos )
}
#define TRY( x ) if( x ) \
#define TRY( x ) if( x ) \
goto try_error
#define TRY_EOF( x ) if( x ) \
#define TRY_EOF( x ) if( x ) \
goto eof
/* correct y-coordinate for top-down format */
#define ycorr( y ) (bmpinfo.topdown ? bmpinfo.h - 1 - y : y)
/* read BMP stream after magic number. Return values as for gm_read.
* We choose to be as permissive as possible, since there are many
* programs out there which produce BMP. For instance, ppmtobmp can
@ -601,15 +674,16 @@ static int bmp_forward( FILE* f, int pos )
* Windows BMP format. */
static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
{
bmp_info_t bmpinfo;
int* coltable;
unsigned int b, c;
unsigned int i, j;
greymap_t* gm;
unsigned int x, y;
int col[2];
unsigned int bitbuf;
unsigned int n;
bmp_info_t bmpinfo;
int* coltable;
unsigned int b, c;
unsigned int i, j;
greymap_t* gm;
unsigned int x, y;
int col[2];
unsigned int bitbuf;
unsigned int n;
unsigned int redshift, greenshift, blueshift;
gm_read_error = NULL;
gm = NULL;
@ -624,7 +698,9 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
/* info header */
TRY( bmp_readint( f, 4, &bmpinfo.InfoSize ) );
if( bmpinfo.InfoSize == 40 || bmpinfo.InfoSize == 64 )
if( bmpinfo.InfoSize == 40 || bmpinfo.InfoSize == 64
|| bmpinfo.InfoSize == 108 || bmpinfo.InfoSize == 124 )
{
/* Windows or new OS/2 format */
bmpinfo.ctbits = 32; /* sample size in color table */
@ -638,6 +714,34 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
TRY( bmp_readint( f, 4, &bmpinfo.YpixelsPerM ) );
TRY( bmp_readint( f, 4, &bmpinfo.ncolors ) );
TRY( bmp_readint( f, 4, &bmpinfo.ColorsImportant ) );
if( bmpinfo.InfoSize >= 108 ) /* V4 and V5 bitmaps */
{
TRY( bmp_readint( f, 4, &bmpinfo.RedMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.GreenMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.BlueMask ) );
TRY( bmp_readint( f, 4, &bmpinfo.AlphaMask ) );
}
if( bmpinfo.w > 0x7fffffff )
{
goto format_error;
}
if( bmpinfo.h > 0x7fffffff )
{
bmpinfo.h = (-bmpinfo.h) & 0xffffffff;
bmpinfo.topdown = 1;
}
else
{
bmpinfo.topdown = 0;
}
if( bmpinfo.h > 0x7fffffff )
{
goto format_error;
}
}
else if( bmpinfo.InfoSize == 12 )
{
@ -647,15 +751,22 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
TRY( bmp_readint( f, 2, &bmpinfo.h ) );
TRY( bmp_readint( f, 2, &bmpinfo.Planes ) );
TRY( bmp_readint( f, 2, &bmpinfo.bits ) );
bmpinfo.comp = 0;
bmpinfo.comp = 0;
bmpinfo.ncolors = 0;
bmpinfo.topdown = 0;
}
else
{
goto format_error;
}
/* forward to color table (i.e., if bmpinfo.InfoSize == 64) */
if( bmpinfo.comp == 3 && bmpinfo.InfoSize < 108 )
{
/* bitfield feature is only understood with V4 and V5 format */
goto format_error;
}
/* forward to color table (e.g., if bmpinfo.InfoSize == 64) */
TRY( bmp_forward( f, 14 + bmpinfo.InfoSize ) );
if( bmpinfo.Planes != 1 )
@ -672,7 +783,8 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
/* color table, present only if bmpinfo.bits <= 8. */
if( bmpinfo.bits <= 8 )
{
coltable = (int*) malloc( bmpinfo.ncolors * sizeof(int) );
coltable = (int*) calloc( bmpinfo.ncolors, sizeof(int) );
if( !coltable )
{
goto std_error;
@ -696,6 +808,7 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
/* allocate greymap */
gm = gm_new( bmpinfo.w, bmpinfo.h );
if( !gm )
{
goto std_error;
@ -716,12 +829,15 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( i = 0; 8 * i<bmpinfo.w; i++ )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
for( j = 0; j<8; j++ )
{
GM_PUT( gm, i * 8 + j, y, b & (0x80 >> j) ? coltable[1] : coltable[0] );
GM_PUT( gm, i * 8 + j, ycorr( y ),
(b & (0x80 >> j)) ? coltable[1] : coltable[0] );
}
}
@ -737,11 +853,13 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
case 0x006:
case 0x007:
case 0x008:
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
bitbuf = 0; /* bit buffer: bits in buffer are high-aligned */
n = 0; /* number of bits currently in bitbuffer */
for( x = 0; x<bmpinfo.w; x++ )
{
if( n < bmpinfo.bits )
@ -750,10 +868,11 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
bitbuf |= b << (INTBITS - 8 - n);
n += 8;
}
b = bitbuf >> (INTBITS - bmpinfo.bits);
bitbuf <<= bmpinfo.bits;
n -= bmpinfo.bits;
GM_UPUT( gm, x, y, coltable[b] );
GM_UPUT( gm, x, ycorr( y ), coltable[b] );
}
TRY( bmp_pad( f ) );
@ -762,7 +881,6 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
break;
case 0x010: /* 16-bit encoding */
/* can't do this format because it is not well-documented and I
* don't have any samples */
gm_read_error = "cannot handle bmp 16-bit coding";
@ -771,14 +889,39 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
case 0x018: /* 24-bit encoding */
case 0x020: /* 32-bit encoding */
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( x = 0; x<bmpinfo.w; x++ )
{
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
c = ( (c >> 16) & 0xff ) + ( (c >> 8) & 0xff ) + (c & 0xff);
GM_UPUT( gm, x, y, c / 3 );
GM_UPUT( gm, x, ycorr( y ), c / 3 );
}
TRY( bmp_pad( f ) );
}
break;
case 0x320: /* 32-bit encoding with bitfields */
redshift = lobit( bmpinfo.RedMask );
greenshift = lobit( bmpinfo.GreenMask );
blueshift = lobit( bmpinfo.BlueMask );
for( y = 0; y<bmpinfo.h; y++ )
{
bmp_pad_reset();
for( x = 0; x<bmpinfo.w; x++ )
{
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
c = ( (c & bmpinfo.RedMask) >> redshift ) +
( (c & bmpinfo.GreenMask) >> greenshift ) +
( (c & bmpinfo.BlueMask) >> blueshift );
GM_UPUT( gm, x, ycorr( y ), c / 3 );
}
TRY( bmp_pad( f ) );
@ -787,17 +930,20 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
break;
case 0x204: /* 4-bit runlength compressed encoding (RLE4) */
x = 0;
y = 0;
x = 0;
y = 0;
while( 1 )
{
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
if( b>0 )
{
/* repeat count */
col[0] = coltable[(c >> 4) & 0xf];
col[1] = coltable[c & 0xf];
col[0] = coltable[(c >> 4) & 0xf];
col[1] = coltable[c & 0xf];
for( i = 0; i<b && x<bmpinfo.w; i++ )
{
if( x>=bmpinfo.w )
@ -805,11 +951,13 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
GM_UPUT( gm, x, y, col[i & 1] );
GM_UPUT( gm, x, ycorr( y ), col[i & 1] );
x++;
}
}
@ -829,8 +977,8 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
/* "delta": skip pixels in x and y directions */
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
x += b;
y += c;
x += b;
y += c;
}
else
{
@ -841,16 +989,19 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
}
if( x>=bmpinfo.w )
{
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
GM_PUT( gm, x, y, coltable[( b >> ( 4 - 4 * (i & 1) ) ) & 0xf] );
GM_PUT( gm, x, ycorr( y ), coltable[( b >> ( 4 - 4 * (i & 1) ) ) & 0xf] );
x++;
}
@ -865,12 +1016,14 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
break;
case 0x108: /* 8-bit runlength compressed encoding (RLE8) */
x = 0;
y = 0;
x = 0;
y = 0;
while( 1 )
{
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
if( b>0 )
{
/* repeat count */
@ -881,11 +1034,13 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
GM_UPUT( gm, x, y, coltable[c] );
GM_UPUT( gm, x, ycorr( y ), coltable[c] );
x++;
}
}
@ -905,8 +1060,8 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
/* "delta": skip pixels in x and y directions */
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
x += b;
y += c;
x += b;
y += c;
}
else
{
@ -914,16 +1069,19 @@ static int gm_readbody_bmp( FILE* f, greymap_t** gmp )
for( i = 0; i<c; i++ )
{
TRY_EOF( bmp_readint( f, 1, &b ) );
if( x>=bmpinfo.w )
{
x = 0;
y++;
}
if( y>=bmpinfo.h )
{
break;
}
GM_PUT( gm, x, y, coltable[b] );
GM_PUT( gm, x, ycorr( y ), coltable[b] );
x++;
}
@ -955,10 +1113,12 @@ format_error:
try_error:
free( coltable );
free( gm );
if( !gm_read_error )
{
gm_read_error = "invalid bmp file";
}
return -2;
std_error:
@ -985,9 +1145,10 @@ int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, doubl
if( gamma != 1.0 )
{
gammatable[0] = 0;
for( v = 1; v<256; v++ )
{
gammatable[v] = (int) ( 255 * exp( log( v / 255.0 ) / gamma ) + 0.5 );
gammatable[v] = (int) (255 * exp( log( v / 255.0 ) / gamma ) + 0.5);
}
}
else
@ -999,22 +1160,27 @@ int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, doubl
}
fprintf( f, raw ? "P5\n" : "P2\n" );
if( comment && *comment )
{
fprintf( f, "# %s\n", comment );
}
fprintf( f, "%d %d 255\n", gm->w, gm->h );
for( y = gm->h - 1; y>=0; y-- )
{
for( x = 0; x<gm->w; x++ )
{
v = GM_UGET( gm, x, y );
if( mode == GM_MODE_NONZERO )
{
if( v > 255 )
{
v = 510 - v;
}
if( v < 0 )
{
v = 0;
@ -1023,6 +1189,7 @@ int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, doubl
else if( mode == GM_MODE_ODD )
{
v = mod( v, 510 );
if( v > 255 )
{
v = 510 - v;
@ -1042,6 +1209,7 @@ int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, doubl
else if( mode == GM_MODE_NEGATIVE )
{
v = 510 - v;
if( v < 0 )
{
v = 0;
@ -1051,6 +1219,7 @@ int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, doubl
v = 255;
}
}
v = gammatable[v];
if( raw )
@ -1079,26 +1248,26 @@ int gm_print( FILE* f, greymap_t* gm )
int d, t;
int sw, sh;
sw = gm->w < 79 ? gm->w : 79;
sh = gm->w < 79 ? gm->h : gm->h * sw * 44 / (79 * gm->w);
sw = gm->w < 79 ? gm->w : 79;
sh = gm->w < 79 ? gm->h : gm->h * sw * 44 / (79 * gm->w);
for( yy = sh - 1; yy>=0; yy-- )
{
for( xx = 0; xx<sw; xx++ )
{
d = 0;
t = 0;
d = 0;
t = 0;
for( x = xx * gm->w / sw; x<(xx + 1) * gm->w / sw; x++ )
{
for( y = yy * gm->h / sh; y<(yy + 1) * gm->h / sh; y++ )
{
d += GM_GET( gm, x, y );
t += 256;
d += GM_GET( gm, x, y );
t += 256;
}
}
if( t )
fputc( "*#=- "[5 * d / t], f ); /* what a cute trick :) */
fputc( "*#=- "[5 * d / t], f ); /* what a cute trick :) */
}
fputc( '\n', f );

View File

@ -1,58 +1,61 @@
/* Copyright (C) 2001-2007 Peter Selinger.
This file is part of Potrace. It is free software and it is covered
by the GNU General Public License. See the file COPYING for details. */
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: greymap.h 147 2007-04-09 00:44:09Z selinger $ */
#ifndef PGM_H
#define PGM_H
#ifndef GREYMAP_H
#define GREYMAP_H
#include <stdio.h>
#include <stdlib.h>
/* internal format for greymaps. Note: in this format, rows are
ordered from bottom to top. The pixels in each row are given from
left to right. */
* ordered from bottom to top. The pixels in each row are given from
* left to right. */
struct greymap_s {
int w; /* width, in pixels */
int h; /* height, in pixels */
signed short int *map; /* raw data, w*h values */
struct greymap_s
{
int w; /* width, in pixels */
int h; /* height, in pixels */
signed short int* map; /* raw data, w*h values */
};
typedef struct greymap_s greymap_t;
/* macros for accessing pixel at index (x,y). Note that the origin is
in the *lower* left corner. U* macros omit the bounds check. */
* in the *lower* left corner. U* macros omit the bounds check. */
#define gm_index(gm, x, y) (&(gm)->map[(x)+(y)*(gm)->w])
#define gm_safe(gm, x, y) ((int)(x)>=0 && (int)(x)<(gm)->w && (int)(y)>=0 && (int)(y)<(gm)->h)
#define gm_bound(x, m) ((x)<0 ? 0 : (x)>=(m) ? (m)-1 : (x))
#define GM_UGET(gm, x, y) (*gm_index(gm, x, y))
#define GM_UINC(gm, x, y, b) (*gm_index(gm, x, y) += (short int)(b))
#define GM_UINV(gm, x, y) (*gm_index(gm, x, y) = 255 - *gm_index(gm, x, y))
#define GM_UPUT(gm, x, y, b) (*gm_index(gm, x, y) = (short int)(b))
#define GM_GET(gm, x, y) (gm_safe(gm, x, y) ? GM_UGET(gm, x, y) : 0)
#define GM_INC(gm, x, y, b) (gm_safe(gm, x, y) ? GM_UINC(gm, x, y, b) : 0)
#define GM_INV(gm, x, y) (gm_safe(gm, x, y) ? GM_UINV(gm, x, y) : 0)
#define GM_PUT(gm, x, y, b) (gm_safe(gm, x, y) ? GM_UPUT(gm, x, y, b) : 0)
#define GM_BGET(gm, x, y) GM_UGET(gm, gm_bound(x, gm->w), gm_bound(y, gm->h))
#define gm_index( gm, x, y ) (&(gm)->map[(x) + (y) * (ssize_t) (gm)->w])
#define gm_safe( gm, x, \
y ) ( (int) (x)>=0 && (int) (x)<(gm)->w && (int) (y)>=0 \
&& (int) (y)<(gm)->h )
#define gm_bound( x, m ) ( (x)<0 ? 0 : (x)>=(m) ? (m) - 1 : (x) )
#define GM_UGET( gm, x, y ) ( *gm_index( gm, x, y ) )
#define GM_UINC( gm, x, y, b ) ( *gm_index( gm, x, y ) += (short int) (b) )
#define GM_UINV( gm, x, y ) ( *gm_index( gm, x, y ) = 255 - *gm_index( gm, x, y ) )
#define GM_UPUT( gm, x, y, b ) ( *gm_index( gm, x, y ) = (short int) (b) )
#define GM_GET( gm, x, y ) (gm_safe( gm, x, y ) ? GM_UGET( gm, x, y ) : 0)
#define GM_INC( gm, x, y, b ) (gm_safe( gm, x, y ) ? GM_UINC( gm, x, y, b ) : 0)
#define GM_INV( gm, x, y ) (gm_safe( gm, x, y ) ? GM_UINV( gm, x, y ) : 0)
#define GM_PUT( gm, x, y, b ) (gm_safe( gm, x, y ) ? GM_UPUT( gm, x, y, b ) : 0)
#define GM_BGET( gm, x, y ) GM_UGET( gm, gm_bound( x, gm->w ), gm_bound( y, gm->h ) )
/* modes for cutting off out-of-range values. The following names
refer to winding numbers. I.e., make a pixel black if winding
number is nonzero, odd, or positive, respectively. We assume that 0
winding number corresponds to white (255). */
#define GM_MODE_NONZERO 1
#define GM_MODE_ODD 2
#define GM_MODE_POSITIVE 3
#define GM_MODE_NEGATIVE 4
* refer to winding numbers. I.e., make a pixel black if winding
* number is nonzero, odd, or positive, respectively. We assume that 0
* winding number corresponds to white (255). */
#define GM_MODE_NONZERO 1
#define GM_MODE_ODD 2
#define GM_MODE_POSITIVE 3
#define GM_MODE_NEGATIVE 4
extern const char *gm_read_error;
extern const char* gm_read_error;
greymap_t *gm_new(int w, int h);
greymap_t *gm_dup(greymap_t *gm);
void gm_free(greymap_t *gm);
void gm_clear(greymap_t *gm, int b);
int gm_read(FILE *f, greymap_t **gmp);
int gm_writepgm(FILE *f, greymap_t *gm, char *comment, int raw, int mode, double gamma);
int gm_print(FILE *f, greymap_t *gm);
greymap_t* gm_new( int w, int h );
greymap_t* gm_dup( greymap_t* gm );
void gm_free( greymap_t* gm );
void gm_clear( greymap_t* gm, int b );
int gm_read( FILE* f, greymap_t** gmp );
int gm_writepgm( FILE* f, greymap_t* gm, char* comment, int raw, int mode, double gamma );
int gm_print( FILE* f, greymap_t* gm );
#endif /* PGM_H */
#endif /* GREYMAP_H */

View File

@ -1,8 +1,7 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: lists.h 147 2007-04-09 00:44:09Z selinger $ */
#ifndef _PS_LISTS_H
#define _PS_LISTS_H
@ -33,7 +32,8 @@
* trick, we'd have to omit the ";" in such cases. */
#define MACRO_BEGIN do {
#define MACRO_END } while( 0 )
#define MACRO_END } \
while( 0 )
/* ---------------------------------------------------------------------- */
/* macros for singly-linked lists */
@ -45,7 +45,8 @@
* c, or NULL if not found */
#define list_find( elt, list, c ) \
MACRO_BEGIN list_forall( elt, list ) if( c ) \
break;MACRO_END
break; \
MACRO_END
/* like forall, except also set hook for elt. */
#define list_forall2( elt, list, hook ) \
@ -54,7 +55,8 @@
/* same as list_find, except also set hook for elt. */
#define list_find2( elt, list, c, hook ) \
MACRO_BEGIN list_forall2( elt, list, hook ) if( c ) \
break;MACRO_END
break; \
MACRO_END
/* same, except only use hook. */
#define _list_forall_hook( list, hook ) \
@ -63,7 +65,8 @@
/* same, except only use hook. Note: c may only refer to *hook, not elt. */
#define _list_find_hook( list, c, hook ) \
MACRO_BEGIN _list_forall_hook( list, hook ) if( c ) \
break;MACRO_END
break; \
MACRO_END
/* insert element after hook */
#define list_insert_athook( elt, hook ) \
@ -113,7 +116,7 @@
* Return NULL if out of bounds. */
#define list_nth( elt, list, n ) \
MACRO_BEGIN \
int _x; /* only evaluate n once */ \
int _x; /* only evaluate n once */ \
for( _x = (n), elt = list; _x && elt; _x--, elt = elt->next ) {} \
MACRO_END
@ -121,9 +124,9 @@
* Return NULL if out of bounds. */
#define list_nth_hook( elt, list, n, hook ) \
MACRO_BEGIN \
int _x; /* only evaluate n once */ \
for( _x = (n), elt = list, hook = &list; _x && elt; _x--, hook = &elt->next, elt =\
elt->next ) {} \
int _x; /* only evaluate n once */ \
for( _x = (n), elt = list, hook = &list; _x && elt; \
_x--, hook = &elt->next, elt = elt->next ) {} \
MACRO_END
/* set n to the length of the list */
@ -142,11 +145,11 @@
n = 0; \
list_forall( elt, list ) { \
if( c ) \
break;\
break; \
n++; \
} \
if( !elt ) \
n = -1;\
if( !elt ) \
n = -1; \
MACRO_END
/* set n to the number of elements in the list that satisfy condition c */
@ -155,7 +158,7 @@
n = 0; \
list_forall( elt, list ) { \
if( c ) \
n++;\
n++; \
} \
MACRO_END
@ -221,13 +224,14 @@
do { \
_hook1 = &(list); \
while( (a = *_hook1) != NULL && (b = a->next1) != NULL ) { \
_elt = b->next1; \
_list_merge_cond( listtype, a, b, cond, *_hook1 ); \
_hook1 = &( (*_hook1)->next1 ); \
*_hook1 = _elt; \
} \
} while( _hook1 != &(list) ); \
MACRO_END
_elt = b->next1; \
_list_merge_cond( listtype, a, b, cond, *_hook1 ); \
_hook1 = &( (*_hook1)->next1 ); \
*_hook1 = _elt; \
} \
} \
while( _hook1 != &(list) ); \
MACRO_END
/* merge two sorted lists. Store result at &result */
#define _list_merge_cond( listtype, a, b, cond, result ) \
@ -238,16 +242,19 @@
if( a==NULL ) { \
*_hook = b; \
break; \
} else if( b==NULL ) { \
} \
else if( b==NULL ) { \
*_hook = a; \
break; \
} else if( cond ) { \
*_hook = a; \
_hook = &(a->next); \
} \
else if( cond ) { \
*_hook = a; \
_hook = &(a->next); \
a = a->next; \
} else { \
*_hook = b; \
_hook = &(b->next); \
} \
else { \
*_hook = b; \
_hook = &(b->next); \
b = b->next; \
} \
} \
@ -262,7 +269,8 @@
elt->next = NULL; \
if( end ) { \
end->next = elt; \
} else { \
} \
else { \
head = elt; \
} \
end = elt; \
@ -270,7 +278,7 @@
/* let elt be each element of the list, unlinked. At the end, set list=NULL. */
#define dlist_forall_unlink( elt, head, end ) \
for( elt = head;\
for( elt = head; \
elt ? (head = elt->next, elt->next = NULL, elt->prev = NULL), 1 : (end = NULL, 0); \
elt = head )
@ -282,7 +290,8 @@
head = head->next; \
if( head ) { \
head->prev = NULL; \
} else { \
} \
else { \
end = NULL; \
} \
elt->prev = NULL; \

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
@ -8,14 +8,21 @@
#define PLATFORM_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#include "config.h"
#endif
/* in Windows, set all file i/o to binary */
#ifdef __MINGW32__
#include <fcntl.h>
unsigned int _CRT_fmode = _O_BINARY;
#endif
static inline void platform_init( void )
{
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
}
#else
#ifdef __CYGWIN__
#include <fcntl.h>
@ -34,6 +41,7 @@ static inline void platform_init( void )
}
#endif
#endif
#endif /* PLATFORM_H */

View File

@ -1 +1 @@
#define POTRACELIB_VERSION "potracelib 1.8"
#define POTRACELIB_VERSION "potracelib 1.12"

View File

@ -1,20 +1,19 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details.
*/
* by the GNU General Public License. See the file COPYING for details. */
#include <stdlib.h>
#include <string.h>
#include <potracelib.h>
#include <curve.h>
#include <decompose.h>
#include <trace.h>
#include <progress.h>
#include "potracelib.h"
#include "curve.h"
#include "decompose.h"
#include "trace.h"
#include "progress.h"
#include <potrace_version.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#include "config.h"
#endif
/* default parameters */
@ -40,10 +39,12 @@ potrace_param_t* potrace_param_default( void )
potrace_param_t* p;
p = (potrace_param_t*) malloc( sizeof(potrace_param_t) );
if( !p )
{
return NULL;
}
memcpy( p, &param_default, sizeof(potrace_param_t) );
return p;
}
@ -52,26 +53,28 @@ potrace_param_t* potrace_param_default( void )
/* On success, returns a Potrace state st with st->status ==
* POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
* could be created (with errno set), or returns an incomplete Potrace
* state (with st->status == POTRACE_STATUS_INCOMPLETE). Complete or
* incomplete Potrace state can be freed with potrace_state_free(). */
* state (with st->status == POTRACE_STATUS_INCOMPLETE, and with errno
* set). Complete or incomplete Potrace state can be freed with
* potrace_state_free(). */
potrace_state_t* potrace_trace( const potrace_param_t* param, const potrace_bitmap_t* bm )
{
int r;
path_t* plist = NULL;
int r;
path_t* plist = NULL;
potrace_state_t* st;
progress_t prog;
progress_t subprog;
progress_t prog;
progress_t subprog;
/* prepare private progress bar state */
prog.callback = param->progress.callback;
prog.data = param->progress.data;
prog.min = param->progress.min;
prog.max = param->progress.max;
prog.epsilon = param->progress.epsilon;
prog.d_prev = param->progress.min;
prog.data = param->progress.data;
prog.min = param->progress.min;
prog.max = param->progress.max;
prog.epsilon = param->progress.epsilon;
prog.d_prev = param->progress.min;
/* allocate state object */
st = (potrace_state_t*) malloc( sizeof(potrace_state_t) );
if( !st )
{
return NULL;
@ -81,15 +84,16 @@ potrace_state_t* potrace_trace( const potrace_param_t* param, const potrace_bitm
/* process the image */
r = bm_to_pathlist( bm, &plist, param, &subprog );
if( r )
{
free( st );
return NULL;
}
st->status = POTRACE_STATUS_OK;
st->plist = plist;
st->priv = NULL; /* private state currently unused */
st->status = POTRACE_STATUS_OK;
st->plist = plist;
st->priv = NULL; /* private state currently unused */
progress_subrange_end( &prog, &subprog );
@ -97,6 +101,7 @@ potrace_state_t* potrace_trace( const potrace_param_t* param, const potrace_bitm
/* partial success. */
r = process_path( plist, param, &subprog );
if( r )
{
st->status = POTRACE_STATUS_INCOMPLETE;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
@ -6,7 +6,11 @@
#define POTRACELIB_H
/* this file defines the API for the core Potrace library. For a more
* detailed description of the API, see doc/potracelib.txt */
* detailed description of the API, see potracelib.pdf */
#ifdef __cplusplus
extern "C" {
#endif
/* ---------------------------------------------------------------------- */
/* tracing parameters */
@ -23,22 +27,22 @@
/* structure to hold progress bar callback data */
struct potrace_progress_s
{
void (* callback)( double progress, void* privdata ); /* callback fn */
void* data; /* callback function's private data */
double min, max; /* desired range of progress, e.g. 0.0 to 1.0 */
double epsilon; /* granularity: can skip smaller increments */
void (* callback)( double progress, void* privdata ); /* callback fn */
void* data; /* callback function's private data */
double min, max; /* desired range of progress, e.g. 0.0 to 1.0 */
double epsilon; /* granularity: can skip smaller increments */
};
typedef struct potrace_progress_s potrace_progress_t;
/* structure to hold tracing parameters */
struct potrace_param_s
{
int turdsize; /* area of largest path to be ignored */
int turnpolicy; /* resolves ambiguous turns in path decomposition */
double alphamax; /* corner threshold */
int opticurve; /* use curve optimization? */
double opttolerance; /* curve optimization tolerance */
potrace_progress_t progress; /* progress callback function */
int turdsize; /* area of largest path to be ignored */
int turnpolicy; /* resolves ambiguous turns in path decomposition */
double alphamax; /* corner threshold */
int opticurve; /* use curve optimization? */
double opttolerance; /* curve optimization tolerance */
potrace_progress_t progress; /* progress callback function */
};
typedef struct potrace_param_s potrace_param_t;
@ -54,8 +58,8 @@ typedef unsigned long potrace_word;
* bit of scanline(n)[0]. */
struct potrace_bitmap_s
{
int w, h; /* width and height, in pixels */
int dy; /* words per scanline (not bytes) */
int w, h; /* width and height, in pixels */
int dy; /* words per scanline (not bytes) */
potrace_word* map; /* raw data, dy*h words */
};
typedef struct potrace_bitmap_s potrace_bitmap_t;
@ -77,25 +81,24 @@ typedef struct potrace_dpoint_s potrace_dpoint_t;
/* closed curve segment */
struct potrace_curve_s
{
int n; // number of segments
int* tag; // tag[n]: POTRACE_CURVETO or POTRACE_CORNER
potrace_dpoint_t( * c )[3]; /* c[n][3]: control points.
* c[n][0] is unused for tag[n]=POTRACE_CORNER
*/
int n; /* number of segments */
int* tag; /* tag[n]: POTRACE_CURVETO or POTRACE_CORNER */
potrace_dpoint_t( *c )[3]; /* c[n][3]: control points.
* c[n][0] is unused for tag[n]=POTRACE_CORNER */
};
typedef struct potrace_curve_s potrace_curve_t;
/* Linked list of signed curve segments. Also carries a tree structure. */
struct potrace_path_s
{
int area; /* area of the bitmap path */
int sign; /* '+' or '-', depending on orientation */
int area; /* area of the bitmap path */
int sign; /* '+' or '-', depending on orientation */
potrace_curve_t curve; /* this path's vector data */
struct potrace_path_s* next; /* linked list structure */
struct potrace_path_s* childlist; /* tree structure */
struct potrace_path_s* sibling; /* tree structure */
struct potrace_path_s* childlist; /* tree structure */
struct potrace_path_s* sibling; /* tree structure */
struct potrace_privpath_s* priv; /* private state */
};
@ -104,12 +107,12 @@ typedef struct potrace_path_s potrace_path_t;
/* ---------------------------------------------------------------------- */
/* Potrace state */
#define POTRACE_STATUS_OK 0
#define POTRACE_STATUS_INCOMPLETE 1
#define POTRACE_STATUS_OK 0
#define POTRACE_STATUS_INCOMPLETE 1
struct potrace_state_s
{
int status;
int status;
potrace_path_t* plist; /* vector data */
struct potrace_privstate_s* priv; /* private state */
@ -123,17 +126,21 @@ typedef struct potrace_state_s potrace_state_t;
potrace_param_t* potrace_param_default( void );
/* free parameter set */
void potrace_param_free( potrace_param_t* p );
void potrace_param_free( potrace_param_t* p );
/* trace a bitmap*/
potrace_state_t* potrace_trace( const potrace_param_t* param,
const potrace_bitmap_t* bm );
potrace_state_t* potrace_trace( const potrace_param_t* param,
const potrace_bitmap_t* bm );
/* free a Potrace state */
void potrace_state_free( potrace_state_t* st );
void potrace_state_free( potrace_state_t* st );
/* return a static plain text version string identifying this version
* of potracelib */
const char* potrace_version( void );
const char* potrace_version( void );
#ifdef __cplusplus
} /* end of extern "C" */
#endif
#endif /* POTRACELIB_H */

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
@ -14,8 +14,8 @@
/* structure to hold progress bar callback data */
struct progress_s
{
void (* callback)( double progress, void* privdata ); /* callback fn */
void* data; /* callback function's private data */
void (* callback)( double progress, void* privdata ); /* callback fn */
void* data; /* callback function's private data */
double min, max; /* desired range of progress, e.g. 0.0 to 1.0 */
double epsilon; /* granularity: can skip smaller increments */
double b; /* upper limit of subrange in superrange units */
@ -30,9 +30,10 @@ static inline void progress_update( double d, progress_t* prog )
{
double d_scaled;
if( prog->callback != NULL )
if( prog != NULL && prog->callback != NULL )
{
d_scaled = prog->min * (1 - d) + prog->max * d;
if( d == 1.0 || d_scaled >= prog->d_prev + prog->epsilon )
{
prog->callback( prog->min * (1 - d) + prog->max * d, prog->data );
@ -45,14 +46,14 @@ static inline void progress_update( double d, progress_t* prog )
/* start a subrange of the given progress object. The range is
* narrowed to [a..b], relative to 0.0-1.0 coordinates. If new range
* is below granularity threshold, disable further subdivisions. */
static inline void progress_subrange_start( double a,
double b,
const progress_t* prog,
progress_t* sub )
static inline void progress_subrange_start( double a,
double b,
const progress_t* prog,
progress_t* sub )
{
double min, max;
if( prog->callback == NULL )
if( prog == NULL || prog->callback == NULL )
{
sub->callback = NULL;
return;
@ -67,19 +68,19 @@ static inline void progress_subrange_start( double a,
sub->b = b;
return;
}
sub->callback = prog->callback;
sub->data = prog->data;
sub->epsilon = prog->epsilon;
sub->data = prog->data;
sub->epsilon = prog->epsilon;
sub->min = min;
sub->max = max;
sub->d_prev = prog->d_prev;
return;
}
static inline void progress_subrange_end( progress_t* prog, progress_t* sub )
{
if( prog->callback != NULL )
if( prog != NULL && prog->callback != NULL )
{
if( sub->callback == NULL )
{

View File

@ -1,17 +1,16 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: render.c 147 2007-04-09 00:44:09Z selinger $ */
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <string.h>
#include <render.h>
#include <greymap.h>
#include <auxiliary.h>
#include "render.h"
#include "greymap.h"
#include "auxiliary.h"
/* ---------------------------------------------------------------------- */
/* routines for anti-aliased rendering of curves */
@ -49,18 +48,22 @@ render_t* render_new( greymap_t* gm )
render_t* rm;
rm = (render_t*) malloc( sizeof(render_t) );
if( !rm )
{
return NULL;
}
memset( rm, 0, sizeof(render_t) );
rm->gm = gm;
rm->incrow_buf = (int*) malloc( gm->h * sizeof(int) );
rm->incrow_buf = (int*) calloc( gm->h, sizeof(int) );
if( !rm->incrow_buf )
{
free( rm );
return NULL;
}
memset( rm->incrow_buf, 0, gm->h * sizeof(int) );
return rm;
}
@ -82,6 +85,7 @@ void render_close( render_t* rm )
{
render_lineto( rm, rm->x0, rm->y0 );
}
GM_INC( rm->gm, rm->x0i, rm->y0i, (rm->a0 + rm->a1) * 255 );
/* assert (rm->x0i != rm->x1i || rm->y0i != rm->y1i); */
@ -125,13 +129,16 @@ static void incrow( render_t* rm, int x, int y, int b )
{
x = rm->gm->w;
}
if( rm->incrow_buf[y] == 0 )
{
rm->incrow_buf[y] = x + 1; /* store x+1 so that we can use 0 for "vacant" */
return;
}
x0 = rm->incrow_buf[y] - 1;
rm->incrow_buf[y] = 0;
if( x0 < x )
{
for( i = x0; i<x; i++ )
@ -152,30 +159,31 @@ static void incrow( render_t* rm, int x, int y, int b )
/* render a straight line */
void render_lineto( render_t* rm, double x2, double y2 )
{
int x2i, y2i;
int x2i, y2i;
double t0 = 2, s0 = 2;
int sn, tn;
int sn, tn;
double ss = 2, ts = 2;
double r0, r1;
int i, j;
int rxi, ryi;
int s;
int i, j;
int rxi, ryi;
int s;
x2i = (int) floor( x2 );
y2i = (int) floor( y2 );
sn = abs( x2i - rm->x1i );
tn = abs( y2i - rm->y1i );
sn = abs( x2i - rm->x1i );
tn = abs( y2i - rm->y1i );
if( sn )
{
s0 = ( (x2>rm->x1 ? rm->x1i + 1 : rm->x1i) - rm->x1 ) / (x2 - rm->x1);
ss = fabs( 1.0 / (x2 - rm->x1) );
s0 = ( (x2>rm->x1 ? rm->x1i + 1 : rm->x1i) - rm->x1 ) / (x2 - rm->x1);
ss = fabs( 1.0 / (x2 - rm->x1) );
}
if( tn )
{
t0 = ( (y2>rm->y1 ? rm->y1i + 1 : rm->y1i) - rm->y1 ) / (y2 - rm->y1);
ts = fabs( 1.0 / (y2 - rm->y1) );
t0 = ( (y2>rm->y1 ? rm->y1i + 1 : rm->y1i) - rm->y1 ) / (y2 - rm->y1);
ts = fabs( 1.0 / (y2 - rm->y1) );
}
r0 = 0;
@ -200,11 +208,12 @@ void render_lineto( render_t* rm, double x2, double y2 )
j++;
s = 0;
}
/* render line from r0 to r1 segment of (rm->x1,rm->y1)..(x2,y2) */
/* move point to r1 */
rm->a1 +=
(r1 - r0) * (y2 - rm->y1) * ( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
rm->a1 += (r1 - r0) * (y2 - rm->y1) *
( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
/* move point across pixel boundary */
if( s && x2>rm->x1 )
@ -241,8 +250,9 @@ void render_lineto( render_t* rm, double x2, double y2 )
/* move point to (x2,y2) */
r1 = 1;
rm->a1 += (r1 - r0) * (y2 - rm->y1) * ( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
r1 = 1;
rm->a1 += (r1 - r0) * (y2 - rm->y1) *
( rxi + 1 - ( (r0 + r1) / 2.0 * (x2 - rm->x1) + rm->x1 ) );
rm->x1i = x2i;
rm->y1i = y2i;
@ -254,18 +264,13 @@ void render_lineto( render_t* rm, double x2, double y2 )
/* render a Bezier curve. */
void render_curveto( render_t* rm,
double x2,
double y2,
double x3,
double y3,
double x4,
double y4 )
void render_curveto( render_t* rm, double x2, double y2, double x3, double y3, double x4,
double y4 )
{
double x1, y1, dd0, dd1, dd, delta, e2, epsilon, t;
x1 = rm->x1; /* starting point */
y1 = rm->y1;
x1 = rm->x1; /* starting point */
y1 = rm->y1;
/* we approximate the curve by small line segments. The interval
* size, epsilon, is determined on the fly so that the distance
@ -276,18 +281,18 @@ void render_curveto( render_t* rm,
/* let dd = maximal value of 2nd derivative over curve - this must
* occur at an endpoint. */
dd0 = sq( x1 - 2 * x2 + x3 ) + sq( y1 - 2 * y2 + y3 );
dd1 = sq( x2 - 2 * x3 + x4 ) + sq( y2 - 2 * y3 + y4 );
dd = 6 * sqrt( max( dd0, dd1 ) );
e2 = 8 * delta <= dd ? 8 * delta / dd : 1;
dd0 = sq( x1 - 2 * x2 + x3 ) + sq( y1 - 2 * y2 + y3 );
dd1 = sq( x2 - 2 * x3 + x4 ) + sq( y2 - 2 * y3 + y4 );
dd = 6 * sqrt( max( dd0, dd1 ) );
e2 = 8 * delta <= dd ? 8 * delta / dd : 1;
epsilon = sqrt( e2 ); /* necessary interval size */
for( t = epsilon; t<1; t += epsilon )
{
render_lineto( rm, x1 * cu( 1 - t ) + 3 * x2 * sq( 1 - t ) * t + 3 * x3 * (1 - t) * sq(
t ) + x4 * cu( t ),
y1 * cu( 1 - t ) + 3 * y2 * sq( 1 - t ) * t + 3 * y3 * (1 - t) * sq(
t ) + y4 * cu( t ) );
t ) + x4 * cu( t ),
y1 * cu( 1 - t ) + 3 * y2 * sq( 1 - t ) * t + 3 * y3 * (1 - t) * sq( t ) + y4 *
cu( t ) );
}
render_lineto( rm, x4, y4 );

View File

@ -1,35 +1,34 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: render.h 147 2007-04-09 00:44:09Z selinger $ */
#ifndef RENDER_H
#define RENDER_H
#include <greymap.h>
#include "greymap.h"
struct render_s
{
greymap_t* gm;
double x0, y0, x1, y1;
int x0i, y0i, x1i, y1i;
double a0, a1;
int* incrow_buf;
double x0, y0, x1, y1;
int x0i, y0i, x1i, y1i;
double a0, a1;
int* incrow_buf;
};
typedef struct render_s render_t;
render_t* render_new( greymap_t* gm );
void render_free( render_t* rm );
void render_close( render_t* rm );
void render_moveto( render_t* rm, double x, double y );
void render_lineto( render_t* rm, double x, double y );
void render_curveto( render_t* rm,
double x2,
double y2,
double x3,
double y3,
double x4,
double y4 );
render_t* render_new( greymap_t* gm );
void render_free( render_t* rm );
void render_close( render_t* rm );
void render_moveto( render_t* rm, double x, double y );
void render_lineto( render_t* rm, double x, double y );
void render_curveto( render_t* rm,
double x2,
double y2,
double x3,
double y3,
double x4,
double y4 );
#endif /* RENDER_H */

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,14 @@
/* Copyright (C) 2001-2007 Peter Selinger.
/* Copyright (C) 2001-2015 Peter Selinger.
* This file is part of Potrace. It is free software and it is covered
* by the GNU General Public License. See the file COPYING for details. */
/* $Id: trace.h 147 2007-04-09 00:44:09Z selinger $ */
#ifndef TRACE_H
#define TRACE_H
#include <potracelib.h>
#include <progress.h>
#include "potracelib.h"
#include "progress.h"
#include "curve.h"
int process_path( path_t* plist, const potrace_param_t* param, progress_t* progress );