2017-11-27 11:54:18 +00:00
|
|
|
/* Copyright (C) 2001-2017 Peter Selinger.
|
2010-06-10 18:43:12 +00:00
|
|
|
* 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. */
|
|
|
|
|
|
|
|
|
|
|
|
/* Routines for manipulating bitmaps, including reading pbm files. */
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "bitmap.h"
|
2017-11-27 11:54:18 +00:00
|
|
|
#include "bitmap_io.h"
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "bitops.h"
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
#define INTBITS ( 8 * sizeof( int ) )
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
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 );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
#define TRY( x ) \
|
|
|
|
if( x ) \
|
|
|
|
goto try_error
|
|
|
|
#define TRY_EOF( x ) \
|
|
|
|
if( x ) \
|
|
|
|
goto eof
|
|
|
|
#define TRY_STD( x ) \
|
|
|
|
if( x ) \
|
|
|
|
goto std_error
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* routines for reading pnm streams */
|
|
|
|
|
|
|
|
/* read next character after whitespace and comments. Return EOF on
|
|
|
|
* end of file or error. */
|
|
|
|
static int fgetc_ws( FILE* f )
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c == '#' )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c == '\n' || c == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
/* space, tab, line feed, carriage return, form-feed */
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c != ' ' && c != '\t' && c != '\r' && c != '\n' && c != 12 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* skip whitespace and comments, then read a non-negative decimal
|
|
|
|
* number from a stream. Return -1 on EOF. Tolerate other errors (skip
|
|
|
|
* bad characters). Do not the read any characters following the
|
|
|
|
* number (put next character back into the stream) */
|
|
|
|
|
|
|
|
static int readnum( FILE* f )
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int acc;
|
|
|
|
|
|
|
|
/* skip whitespace and comments */
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = fgetc_ws( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c >= '0' && c <= '9' )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first digit is already in c */
|
|
|
|
acc = c - '0';
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c < '0' || c > '9' )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
ungetc( c, f );
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
acc *= 10;
|
|
|
|
acc += c - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* similar to readnum, but read only a single 0 or 1, and do not read
|
|
|
|
* any characters after it. */
|
|
|
|
|
|
|
|
static int readbit( FILE* f )
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* skip whitespace and comments */
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = fgetc_ws( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( c >= '0' && c <= '1' )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* read a PNM stream: P1-P6 format (see pnm(5)), or a BMP stream, and
|
|
|
|
* convert the output to a bitmap. Return bitmap in *bmp. Return 0 on
|
|
|
|
* success, -1 on error with errno set, -2 on bad file format (with
|
|
|
|
* 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,
|
2015-12-03 15:46:17 +00:00
|
|
|
* bmp is valid. */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
const char* bm_read_error = NULL;
|
|
|
|
|
|
|
|
int bm_read( FILE* f, double threshold, potrace_bitmap_t** bmp )
|
|
|
|
{
|
|
|
|
int magic[2];
|
|
|
|
|
|
|
|
/* read magic number. We ignore whitespace and comments before the
|
|
|
|
* magic, for the benefit of concatenated files in P1-P3 format.
|
|
|
|
* Multiple P1-P3 images in a single file are not formally allowed
|
|
|
|
* by the PNM standard, but there is no harm in being lenient. */
|
|
|
|
|
|
|
|
magic[0] = fgetc_ws( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( magic[0] == EOF )
|
|
|
|
{
|
|
|
|
return -3;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
magic[1] = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' )
|
|
|
|
{
|
|
|
|
return bm_readbody_pnm( f, threshold, bmp, magic[1] );
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( magic[0] == 'B' && magic[1] == 'M' )
|
|
|
|
{
|
|
|
|
return bm_readbody_bmp( f, threshold, bmp );
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* read PNM format */
|
|
|
|
|
|
|
|
/* read PNM stream after magic number. Return values as for bm_read */
|
|
|
|
static int bm_readbody_pnm( FILE* f, double threshold, potrace_bitmap_t** bmp, int magic )
|
|
|
|
{
|
|
|
|
potrace_bitmap_t* bm;
|
|
|
|
int x, y, i, b, b1, sum;
|
2017-11-27 11:54:18 +00:00
|
|
|
int bpr; /* bytes per row (as opposed to 4*bm->c) */
|
2010-06-10 18:43:12 +00:00
|
|
|
int w, h, max;
|
2017-11-27 11:54:18 +00:00
|
|
|
int realheight; /* in case of incomplete file, keeps track of how
|
|
|
|
* many scan lines actually contain data */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
bm = NULL;
|
|
|
|
|
|
|
|
w = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( w < 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( h < 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate bitmap */
|
|
|
|
bm = bm_new( w, h );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !bm )
|
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
goto std_error;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = 0;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
switch( magic )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
/* not reached */
|
|
|
|
goto format_error;
|
|
|
|
|
|
|
|
case '1':
|
|
|
|
/* read P1 format: PBM ascii */
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( x = 0; x < w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = readbit( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b < 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto eof;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
BM_UPUT( bm, x, y, b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '2':
|
|
|
|
/* read P2 format: PGM ascii */
|
|
|
|
|
|
|
|
max = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max < 1 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( x = 0; x < w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b < 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto eof;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
BM_UPUT( bm, x, y, b > threshold * max ? 0 : 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '3':
|
|
|
|
/* read P3 format: PPM ascii */
|
|
|
|
|
|
|
|
max = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max < 1 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( x = 0; x < w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
sum = 0;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b < 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto eof;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
sum += b;
|
|
|
|
}
|
|
|
|
|
|
|
|
BM_UPUT( bm, x, y, sum > 3 * threshold * max ? 0 : 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '4':
|
|
|
|
/* read P4 format: PBM raw */
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
b = fgetc( f ); /* read single white-space character after height */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
bpr = ( w + 7 ) / 8;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( i = 0; i < bpr; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto eof;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
*bm_index( bm, i * 8, y ) |= ( (potrace_word) b )
|
|
|
|
<< ( 8 * ( BM_WORDSIZE - 1 - ( i % BM_WORDSIZE ) ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '5':
|
|
|
|
/* read P5 format: PGM raw */
|
|
|
|
|
|
|
|
max = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max < 1 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
b = fgetc( f ); /* read single white-space character after max */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( x = 0; x < w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
goto eof;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max >= 256 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
b <<= 8;
|
2010-06-10 18:43:12 +00:00
|
|
|
b1 = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b1 == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
goto eof;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
b |= b1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
BM_UPUT( bm, x, y, b > threshold * max ? 0 : 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '6':
|
|
|
|
/* read P6 format: PPM raw */
|
|
|
|
|
|
|
|
max = readnum( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max < 1 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
b = fgetc( f ); /* read single white-space character after max */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
|
|
|
|
for( x = 0; x < w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
sum = 0;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < 3; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
goto eof;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( max >= 256 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
b <<= 8;
|
2010-06-10 18:43:12 +00:00
|
|
|
b1 = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b1 == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
goto eof;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
b |= b1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
sum += b;
|
|
|
|
}
|
|
|
|
|
|
|
|
BM_UPUT( bm, x, y, sum > 3 * threshold * max ? 0 : 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
bm_flip( bm );
|
2010-06-10 18:43:12 +00:00
|
|
|
*bmp = bm;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
eof:
|
2017-11-27 11:54:18 +00:00
|
|
|
TRY_STD( bm_resize( bm, realheight ) );
|
|
|
|
bm_flip( bm );
|
2010-06-10 18:43:12 +00:00
|
|
|
*bmp = bm;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
format_error:
|
|
|
|
bm_free( bm );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( magic == '1' || magic == '4' )
|
|
|
|
{
|
|
|
|
bm_read_error = "invalid pbm file";
|
|
|
|
}
|
|
|
|
else if( magic == '2' || magic == '5' )
|
|
|
|
{
|
|
|
|
bm_read_error = "invalid pgm file";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bm_read_error = "invalid ppm file";
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
return -2;
|
2017-11-27 11:54:18 +00:00
|
|
|
|
|
|
|
std_error:
|
|
|
|
bm_free( bm );
|
|
|
|
return -1;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* read BMP format */
|
|
|
|
|
|
|
|
struct bmp_info_s
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int FileSize;
|
|
|
|
unsigned int reserved;
|
|
|
|
unsigned int DataOffset;
|
|
|
|
unsigned int InfoSize;
|
2017-11-27 11:54:18 +00:00
|
|
|
unsigned int w; /* width */
|
|
|
|
unsigned int h; /* height */
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int Planes;
|
2017-11-27 11:54:18 +00:00
|
|
|
unsigned int bits; /* bits per sample */
|
|
|
|
unsigned int comp; /* compression mode */
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int ImageSize;
|
|
|
|
unsigned int XpixelsPerM;
|
|
|
|
unsigned int YpixelsPerM;
|
2017-11-27 11:54:18 +00:00
|
|
|
unsigned int ncolors; /* number of colors in palette */
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int ColorsImportant;
|
|
|
|
unsigned int RedMask;
|
|
|
|
unsigned int GreenMask;
|
|
|
|
unsigned int BlueMask;
|
|
|
|
unsigned int AlphaMask;
|
2017-11-27 11:54:18 +00:00
|
|
|
unsigned int ctbits; /* sample size for color table */
|
|
|
|
int topdown; /* top-down mode? */
|
2010-06-10 18:43:12 +00:00
|
|
|
};
|
|
|
|
typedef struct bmp_info_s bmp_info_t;
|
|
|
|
|
|
|
|
/* auxiliary */
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
static int bmp_count = 0; /* counter for byte padding */
|
|
|
|
static int bmp_pos = 0; /* counter from start of BMP data */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* 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 )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
int i;
|
2010-06-10 18:43:12 +00:00
|
|
|
unsigned int sum = 0;
|
2015-12-03 15:46:17 +00:00
|
|
|
int b;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < n; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
sum += (unsigned) b << ( 8 * i );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bmp_count += n;
|
2015-12-03 15:46:17 +00:00
|
|
|
bmp_pos += n;
|
2010-06-10 18:43:12 +00:00
|
|
|
*p = sum;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* reset padding boundary */
|
|
|
|
static void bmp_pad_reset( void )
|
|
|
|
{
|
|
|
|
bmp_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read padding bytes to 4-byte boundary. Return 1 on EOF or error,
|
|
|
|
* else 0. */
|
|
|
|
static int bmp_pad( FILE* f )
|
|
|
|
{
|
|
|
|
int c, i, b;
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
c = ( -bmp_count ) & 3;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < c; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
bmp_pos += c;
|
2010-06-10 18:43:12 +00:00
|
|
|
bmp_count = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* forward to the new file position. Return 1 on EOF or error, else 0 */
|
|
|
|
static int bmp_forward( FILE* f, int pos )
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
|
|
|
|
while( bmp_pos < pos )
|
|
|
|
{
|
|
|
|
b = fgetc( f );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b == EOF )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
bmp_pos++;
|
|
|
|
bmp_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
/* safe colortable access */
|
2017-11-27 11:54:18 +00:00
|
|
|
#define COLTABLE( c ) ( ( c ) < bmpinfo.ncolors ? coltable[( c )] : 0 )
|
2016-02-28 17:04:51 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
/* 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
|
|
|
|
* produce codings with anywhere from 1-8 or 24 bits per sample,
|
|
|
|
* although most specifications only allow 1,4,8,24,32. We can also
|
|
|
|
* read both the old and new OS/2 BMP formats in addition to the
|
|
|
|
* Windows BMP format. */
|
|
|
|
static int bm_readbody_bmp( FILE* f, double threshold, potrace_bitmap_t** bmp )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
bmp_info_t bmpinfo;
|
|
|
|
int* coltable;
|
|
|
|
unsigned int b, c;
|
|
|
|
unsigned int i;
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_bitmap_t* bm;
|
|
|
|
int mask;
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int x, y;
|
2010-06-10 18:43:12 +00:00
|
|
|
int col[2];
|
2015-12-03 15:46:17 +00:00
|
|
|
unsigned int bitbuf;
|
|
|
|
unsigned int n;
|
|
|
|
unsigned int redshift, greenshift, blueshift;
|
2010-06-10 18:43:12 +00:00
|
|
|
int col1[2];
|
2017-11-27 11:54:18 +00:00
|
|
|
int realheight; /* in case of incomplete file, keeps track of how
|
|
|
|
* many scan lines actually contain data */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
bm_read_error = NULL;
|
|
|
|
bm = NULL;
|
|
|
|
coltable = NULL;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
bmp_pos = 2; /* set file position */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* file header (minus magic number) */
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.FileSize ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.reserved ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.DataOffset ) );
|
|
|
|
|
|
|
|
/* info header */
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.InfoSize ) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( bmpinfo.InfoSize == 40 || bmpinfo.InfoSize == 64 || bmpinfo.InfoSize == 108
|
|
|
|
|| bmpinfo.InfoSize == 124 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
/* Windows or new OS/2 format */
|
2016-02-28 17:04:51 +00:00
|
|
|
bmpinfo.ctbits = 32; /* sample size in color table */
|
2010-06-10 18:43:12 +00:00
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.w ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.h ) );
|
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.Planes ) );
|
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.bits ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.comp ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.ImageSize ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.XpixelsPerM ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.YpixelsPerM ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.ncolors ) );
|
|
|
|
TRY( bmp_readint( f, 4, &bmpinfo.ColorsImportant ) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( bmpinfo.InfoSize >= 108 )
|
2015-12-03 15:46:17 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
/* V4 and V5 bitmaps */
|
2015-12-03 15:46:17 +00:00
|
|
|
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 )
|
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
bmpinfo.h = ( -bmpinfo.h ) & 0xffffffff;
|
2015-12-03 15:46:17 +00:00
|
|
|
bmpinfo.topdown = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bmpinfo.topdown = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bmpinfo.h > 0x7fffffff )
|
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else if( bmpinfo.InfoSize == 12 )
|
|
|
|
{
|
|
|
|
/* old OS/2 format */
|
2016-02-28 17:04:51 +00:00
|
|
|
bmpinfo.ctbits = 24; /* sample size in color table */
|
2010-06-10 18:43:12 +00:00
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.w ) );
|
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.h ) );
|
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.Planes ) );
|
|
|
|
TRY( bmp_readint( f, 2, &bmpinfo.bits ) );
|
2015-12-03 15:46:17 +00:00
|
|
|
bmpinfo.comp = 0;
|
2010-06-10 18:43:12 +00:00
|
|
|
bmpinfo.ncolors = 0;
|
2015-12-03 15:46:17 +00:00
|
|
|
bmpinfo.topdown = 0;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
if( bmpinfo.comp == 3 && bmpinfo.InfoSize < 108 )
|
|
|
|
{
|
|
|
|
/* bitfield feature is only understood with V4 and V5 format */
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
if( bmpinfo.comp > 3 || bmpinfo.bits > 32 )
|
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
/* forward to color table (e.g., if bmpinfo.InfoSize == 64) */
|
2010-06-10 18:43:12 +00:00
|
|
|
TRY( bmp_forward( f, 14 + bmpinfo.InfoSize ) );
|
|
|
|
|
|
|
|
if( bmpinfo.Planes != 1 )
|
|
|
|
{
|
|
|
|
bm_read_error = "cannot handle bmp planes";
|
2016-02-28 17:04:51 +00:00
|
|
|
goto format_error; /* can't handle planes */
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( bmpinfo.ncolors == 0 && bmpinfo.bits <= 8 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
bmpinfo.ncolors = 1 << bmpinfo.bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* color table, present only if bmpinfo.bits <= 8. */
|
|
|
|
if( bmpinfo.bits <= 8 )
|
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
coltable = (int*) calloc( bmpinfo.ncolors, sizeof( int ) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !coltable )
|
|
|
|
{
|
|
|
|
goto std_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: since we are reading a bitmap, we can immediately convert
|
|
|
|
* the color table entries to bits. */
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < bmpinfo.ncolors; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
TRY( bmp_readint( f, bmpinfo.ctbits / 8, &c ) );
|
2017-11-27 11:54:18 +00:00
|
|
|
c = ( ( c >> 16 ) & 0xff ) + ( ( c >> 8 ) & 0xff ) + ( c & 0xff );
|
|
|
|
coltable[i] = ( c > 3 * threshold * 255 ? 0 : 1 );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( i < 2 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
col1[i] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* forward to data */
|
2017-11-27 11:54:18 +00:00
|
|
|
if( bmpinfo.InfoSize != 12 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
/* not old OS/2 format */
|
2010-06-10 18:43:12 +00:00
|
|
|
TRY( bmp_forward( f, bmpinfo.DataOffset ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate bitmap */
|
|
|
|
bm = bm_new( bmpinfo.w, bmpinfo.h );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !bm )
|
|
|
|
{
|
|
|
|
goto std_error;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = 0;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
switch( bmpinfo.bits + 0x100 * bmpinfo.comp )
|
|
|
|
{
|
|
|
|
default:
|
2017-11-27 11:54:18 +00:00
|
|
|
goto format_error; break;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
case 0x001: /* monochrome palette */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( col1[0] < col1[1] )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
/* make the darker color black */
|
2010-06-10 18:43:12 +00:00
|
|
|
mask = 0xff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mask = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* raster data */
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < bmpinfo.h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
2010-06-10 18:43:12 +00:00
|
|
|
bmp_pad_reset();
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; 8 * i < bmpinfo.w; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
|
|
|
b ^= mask;
|
2017-11-27 11:54:18 +00:00
|
|
|
*bm_index( bm, i * 8, y ) |= ( (potrace_word) b )
|
|
|
|
<< ( 8 * ( BM_WORDSIZE - 1 - ( i % BM_WORDSIZE ) ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY( bmp_pad( f ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x002: /* 2-bit to 8-bit palettes */
|
2010-06-10 18:43:12 +00:00
|
|
|
case 0x003:
|
|
|
|
case 0x004:
|
|
|
|
case 0x005:
|
|
|
|
case 0x006:
|
|
|
|
case 0x007:
|
|
|
|
case 0x008:
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < bmpinfo.h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
2010-06-10 18:43:12 +00:00
|
|
|
bmp_pad_reset();
|
|
|
|
bitbuf = 0; /* bit buffer: bits in buffer are high-aligned */
|
|
|
|
n = 0; /* number of bits currently in bitbuffer */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( x = 0; x < bmpinfo.w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
if( n < bmpinfo.bits )
|
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
2017-11-27 11:54:18 +00:00
|
|
|
bitbuf |= b << ( INTBITS - 8 - n );
|
2010-06-10 18:43:12 +00:00
|
|
|
n += 8;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
b = bitbuf >> ( INTBITS - bmpinfo.bits );
|
2010-06-10 18:43:12 +00:00
|
|
|
bitbuf <<= bmpinfo.bits;
|
|
|
|
n -= bmpinfo.bits;
|
2017-11-27 11:54:18 +00:00
|
|
|
BM_UPUT( bm, x, y, COLTABLE( b ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY( bmp_pad( f ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x010: /* 16-bit encoding */
|
2010-06-10 18:43:12 +00:00
|
|
|
/* 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";
|
|
|
|
goto format_error;
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x018: /* 24-bit encoding */
|
|
|
|
case 0x020: /* 32-bit encoding */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < bmpinfo.h; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
2010-06-10 18:43:12 +00:00
|
|
|
bmp_pad_reset();
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( x = 0; x < bmpinfo.w; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
|
2017-11-27 11:54:18 +00:00
|
|
|
c = ( ( c >> 16 ) & 0xff ) + ( ( c >> 8 ) & 0xff ) + ( c & 0xff );
|
|
|
|
BM_UPUT( bm, x, y, c > 3 * threshold * 255 ? 0 : 1 );
|
2015-12-03 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY( bmp_pad( f ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x320: /* 32-bit encoding with bitfields */
|
2017-11-27 11:54:18 +00:00
|
|
|
|
|
|
|
if( bmpinfo.RedMask == 0 || bmpinfo.GreenMask == 0 || bmpinfo.BlueMask == 0 )
|
|
|
|
{
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
redshift = lobit( bmpinfo.RedMask );
|
|
|
|
greenshift = lobit( bmpinfo.GreenMask );
|
|
|
|
blueshift = lobit( bmpinfo.BlueMask );
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = 0; y < bmpinfo.h; y++ )
|
2015-12-03 15:46:17 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
2015-12-03 15:46:17 +00:00
|
|
|
bmp_pad_reset();
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( x = 0; x < bmpinfo.w; x++ )
|
2015-12-03 15:46:17 +00:00
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, bmpinfo.bits / 8, &c ) );
|
2017-11-27 11:54:18 +00:00
|
|
|
c = ( ( c & bmpinfo.RedMask ) >> redshift )
|
|
|
|
+ ( ( c & bmpinfo.GreenMask ) >> greenshift )
|
|
|
|
+ ( ( c & bmpinfo.BlueMask ) >> blueshift );
|
|
|
|
BM_UPUT( bm, x, y, c > 3 * threshold * 255 ? 0 : 1 );
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY( bmp_pad( f ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x204: /* 4-bit runlength compressed encoding (RLE4) */
|
2015-12-03 15:46:17 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2016-02-28 17:04:51 +00:00
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b > 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
/* repeat count */
|
2017-11-27 11:54:18 +00:00
|
|
|
col[0] = COLTABLE( ( c >> 4 ) & 0xf );
|
2016-02-28 17:04:51 +00:00
|
|
|
col[1] = COLTABLE( c & 0xf );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < b && x < bmpinfo.w; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w || y >= bmpinfo.h )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
BM_PUT( bm, x, y, col[i & 1] );
|
2010-06-10 18:43:12 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == 0 )
|
|
|
|
{
|
|
|
|
/* end of line */
|
|
|
|
y++;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
else if( c == 1 )
|
|
|
|
{
|
|
|
|
/* end of bitmap */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( c == 2 )
|
|
|
|
{
|
|
|
|
/* "delta": skip pixels in x and y directions */
|
2016-02-28 17:04:51 +00:00
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
|
2015-12-03 15:46:17 +00:00
|
|
|
x += b;
|
|
|
|
y += c;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* verbatim segment */
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < c; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
if( ( i & 1 ) == 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w || y >= bmpinfo.h )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
BM_PUT( bm, x, y, COLTABLE( ( b >> ( 4 - 4 * ( i & 1 ) ) ) & 0xf ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( ( c + 1 ) & 2 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
/* pad to 16-bit boundary */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
case 0x108: /* 8-bit runlength compressed encoding (RLE8) */
|
2015-12-03 15:46:17 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2016-02-28 17:04:51 +00:00
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) ); /* opcode */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &c ) ); /* argument */
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( b > 0 )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
/* repeat count */
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < b; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w || y >= bmpinfo.h )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
BM_PUT( bm, x, y, COLTABLE( c ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == 0 )
|
|
|
|
{
|
|
|
|
/* end of line */
|
|
|
|
y++;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
else if( c == 1 )
|
|
|
|
{
|
|
|
|
/* end of bitmap */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( c == 2 )
|
|
|
|
{
|
|
|
|
/* "delta": skip pixels in x and y directions */
|
2016-02-28 17:04:51 +00:00
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) ); /* x offset */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &c ) ); /* y offset */
|
2015-12-03 15:46:17 +00:00
|
|
|
x += b;
|
|
|
|
y += c;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* verbatim segment */
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < c; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
if( x >= bmpinfo.w || y >= bmpinfo.h )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
realheight = y + 1;
|
|
|
|
BM_PUT( bm, x, y, COLTABLE( b ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c & 1 )
|
|
|
|
{
|
|
|
|
/* pad input to 16-bit boundary */
|
|
|
|
TRY_EOF( bmp_readint( f, 1, &b ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-02-28 17:04:51 +00:00
|
|
|
} /* switch */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* skip any potential junk after the data section, but don't
|
|
|
|
* complain in case EOF is encountered */
|
|
|
|
bmp_forward( f, bmpinfo.FileSize );
|
|
|
|
|
|
|
|
free( coltable );
|
2017-11-27 11:54:18 +00:00
|
|
|
|
|
|
|
if( bmpinfo.topdown )
|
|
|
|
{
|
|
|
|
bm_flip( bm );
|
|
|
|
}
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
*bmp = bm;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
eof:
|
2017-11-27 11:54:18 +00:00
|
|
|
TRY_STD( bm_resize( bm, realheight ) );
|
2010-06-10 18:43:12 +00:00
|
|
|
free( coltable );
|
2017-11-27 11:54:18 +00:00
|
|
|
|
|
|
|
if( bmpinfo.topdown )
|
|
|
|
{
|
|
|
|
bm_flip( bm );
|
|
|
|
}
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
*bmp = bm;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
format_error:
|
|
|
|
try_error:
|
|
|
|
free( coltable );
|
2016-02-28 17:04:51 +00:00
|
|
|
bm_free( bm );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !bm_read_error )
|
|
|
|
{
|
|
|
|
bm_read_error = "invalid bmp file";
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
return -2;
|
|
|
|
|
|
|
|
std_error:
|
|
|
|
free( coltable );
|
2016-02-28 17:04:51 +00:00
|
|
|
bm_free( bm );
|
2010-06-10 18:43:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* output pbm format */
|
|
|
|
|
|
|
|
void bm_writepbm( FILE* f, potrace_bitmap_t* bm )
|
|
|
|
{
|
|
|
|
int w, h, bpr, y, i, c;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
w = bm->w;
|
|
|
|
h = bm->h;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
bpr = ( w + 7 ) / 8;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
fprintf( f, "P4\n%d %d\n", w, h );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = h - 1; y >= 0; y-- )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
for( i = 0; i < bpr; i++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
c = ( *bm_index( bm, i * 8, y ) >> ( 8 * ( BM_WORDSIZE - 1 - ( i % BM_WORDSIZE ) ) ) )
|
|
|
|
& 0xff;
|
2010-06-10 18:43:12 +00:00
|
|
|
fputc( c, f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* output - for primitive debugging purposes only! */
|
|
|
|
|
|
|
|
/* print bitmap to screen */
|
|
|
|
int bm_print( FILE* f, potrace_bitmap_t* bm )
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int xx, yy;
|
|
|
|
int d;
|
|
|
|
int sw, sh;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
sw = bm->w < 79 ? bm->w : 79;
|
2017-11-27 11:54:18 +00:00
|
|
|
sh = bm->w < 79 ? bm->h : bm->h * sw * 44 / ( 79 * bm->w );
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( yy = sh - 1; yy >= 0; yy-- )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
for( xx = 0; xx < sw; xx++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
d = 0;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2017-11-27 11:54:18 +00:00
|
|
|
for( x = xx * bm->w / sw; x < ( xx + 1 ) * bm->w / sw; x++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
2017-11-27 11:54:18 +00:00
|
|
|
for( y = yy * bm->h / sh; y < ( yy + 1 ) * bm->h / sh; y++ )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
if( BM_GET( bm, x, y ) )
|
|
|
|
{
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc( d ? '*' : ' ', f );
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc( '\n', f );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|