KiCad PCB EDA Suite
greymap.h File Reference
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  greymap_s
 

Macros

#define gm_scanline(gm, y)   ( ( gm )->map + (ptrdiff_t) ( y ) * (ptrdiff_t) ( gm )->dy )
 
#define gm_index(gm, x, y)   ( gm_scanline( gm, y ) + ( x ) )
 
#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 ) += (gm_sample_t) ( 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 ) = (gm_sample_t) ( 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)
 
#define GM_MODE_NONZERO   1
 
#define GM_MODE_ODD   2
 
#define GM_MODE_POSITIVE   3
 
#define GM_MODE_NEGATIVE   4
 

Typedefs

typedef signed short int gm_sample_t
 
typedef struct greymap_s greymap_t
 

Functions

greymap_tgm_new (int w, int h)
 
greymap_tgm_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, const char *comment, int raw, int mode, double gamma)
 
int gm_print (FILE *f, greymap_t *gm)
 

Variables

const char * gm_read_error
 

Macro Definition Documentation

#define GM_BGET (   gm,
  x,
 
)
Value:
( ( gm )->w == 0 || ( gm )->h == 0 ? 0 : GM_UGET( gm, gm_bound( x, ( gm )->w ), \
gm_bound( y, ( gm )->h ) ) )
#define gm_bound(x, m)
Definition: greymap.h:36
#define GM_UGET(gm, x, y)
Definition: greymap.h:37

Definition at line 45 of file greymap.h.

#define gm_bound (   x,
 
)    ( ( x ) < 0 ? 0 : ( x ) >= ( m ) ? (m) - 1 : ( x ) )

Definition at line 36 of file greymap.h.

#define GM_GET (   gm,
  x,
 
)    ( gm_safe( gm, x, y ) ? GM_UGET( gm, x, y ) : 0 )

Definition at line 41 of file greymap.h.

Referenced by gm_print().

#define GM_INC (   gm,
  x,
  y,
 
)    ( gm_safe( gm, x, y ) ? GM_UINC( gm, x, y, b ) : 0 )

Definition at line 42 of file greymap.h.

Referenced by incrow(), render_close(), and render_lineto().

#define gm_index (   gm,
  x,
 
)    ( gm_scanline( gm, y ) + ( x ) )

Definition at line 33 of file greymap.h.

#define GM_INV (   gm,
  x,
 
)    ( gm_safe( gm, x, y ) ? GM_UINV( gm, x, y ) : 0 )

Definition at line 43 of file greymap.h.

#define GM_MODE_NEGATIVE   4

Definition at line 56 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_NONZERO   1

Definition at line 53 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_ODD   2

Definition at line 54 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_POSITIVE   3

Definition at line 55 of file greymap.h.

Referenced by gm_writepgm().

#define GM_PUT (   gm,
  x,
  y,
 
)    ( gm_safe( gm, x, y ) ? GM_UPUT( gm, x, y, b ) : 0 )

Definition at line 44 of file greymap.h.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().

#define gm_safe (   gm,
  x,
 
)    ( (int) ( x ) >= 0 && (int) ( x ) < ( gm )->w && (int) ( y ) >= 0 && (int) ( y ) < ( gm )->h )

Definition at line 34 of file greymap.h.

#define gm_scanline (   gm,
 
)    ( ( gm )->map + (ptrdiff_t) ( y ) * (ptrdiff_t) ( gm )->dy )

Definition at line 32 of file greymap.h.

Referenced by gm_dup(), and gm_flip().

#define GM_UGET (   gm,
  x,
 
)    ( *gm_index( gm, x, y ) )

Definition at line 37 of file greymap.h.

Referenced by gm_writepgm().

#define GM_UINC (   gm,
  x,
  y,
 
)    ( *gm_index( gm, x, y ) += (gm_sample_t) ( b ) )

Definition at line 38 of file greymap.h.

#define GM_UINV (   gm,
  x,
 
)    ( *gm_index( gm, x, y ) = 255 - *gm_index( gm, x, y ) )

Definition at line 39 of file greymap.h.

#define GM_UPUT (   gm,
  x,
  y,
 
)    ( *gm_index( gm, x, y ) = (gm_sample_t) ( b ) )

Definition at line 40 of file greymap.h.

Referenced by gm_clear(), gm_readbody_bmp(), and gm_readbody_pnm().

Typedef Documentation

typedef signed short int gm_sample_t

Definition at line 13 of file greymap.h.

typedef struct greymap_s greymap_t

Definition at line 27 of file greymap.h.

Function Documentation

void gm_clear ( greymap_t gm,
int  b 
)

Definition at line 155 of file greymap.cpp.

References greymap_s::base, gm_size(), GM_UPUT, greymap_s::h, and greymap_s::w.

156 {
157  ptrdiff_t size = gm_size( gm );
158  int x, y;
159 
160  if( b == 0 )
161  {
162  memset( gm->base, 0, size );
163  }
164  else
165  {
166  for( y = 0; y < gm->h; y++ )
167  {
168  for( x = 0; x < gm->w; x++ )
169  {
170  GM_UPUT( gm, x, y, b );
171  }
172  }
173  }
174 }
int h
Definition: greymap.h:21
int w
Definition: greymap.h:20
static ptrdiff_t gm_size(const greymap_t *gm)
Definition: greymap.cpp:71
#define GM_UPUT(gm, x, y, b)
Definition: greymap.h:40
gm_sample_t * base
Definition: greymap.h:24
greymap_t* gm_dup ( greymap_t gm)

Definition at line 134 of file greymap.cpp.

References greymap_s::dy, gm_new(), gm_scanline, greymap_s::h, and greymap_s::w.

135 {
136  greymap_t* gm1 = gm_new( gm->w, gm->h );
137  int y;
138 
139  if( !gm1 )
140  {
141  return NULL;
142  }
143 
144  for( y = 0; y < gm->h; y++ )
145  {
146  memcpy( gm_scanline( gm1, y ), gm_scanline( gm, y ),
147  (size_t) gm1->dy * sizeof( gm_sample_t ) );
148  }
149 
150  return gm1;
151 }
int h
Definition: greymap.h:21
#define gm_scanline(gm, y)
Definition: greymap.h:32
int w
Definition: greymap.h:20
greymap_t * gm_new(int w, int h)
Definition: greymap.cpp:79
int dy
Definition: greymap.h:22
signed short int gm_sample_t
Definition: greymap.h:13
void gm_free ( greymap_t gm)

Definition at line 122 of file greymap.cpp.

References greymap_s::base.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().

123 {
124  if( gm )
125  {
126  free( gm->base );
127  }
128 
129  free( gm );
130 }
gm_sample_t * base
Definition: greymap.h:24
greymap_t* gm_new ( int  w,
int  h 
)

Definition at line 79 of file greymap.cpp.

References greymap_s::base, greymap_s::dy, getsize(), greymap_s::h, greymap_s::map, and greymap_s::w.

Referenced by gm_dup(), gm_readbody_bmp(), and gm_readbody_pnm().

80 {
81  greymap_t* gm;
82  int dy = w;
83  ptrdiff_t size;
84 
85  size = getsize( dy, h );
86 
87  if( size < 0 )
88  {
89  errno = ENOMEM;
90  return NULL;
91  }
92 
93  if( size == 0 )
94  {
95  size = 1; /* make surecmalloc() doesn't return NULL */
96  }
97 
98  gm = (greymap_t*) malloc( sizeof( greymap_t ) );
99 
100  if( !gm )
101  {
102  return NULL;
103  }
104 
105  gm->w = w;
106  gm->h = h;
107  gm->dy = dy;
108  gm->base = (gm_sample_t*) calloc( 1, size );
109 
110  if( !gm->base )
111  {
112  free( gm );
113  return NULL;
114  }
115 
116  gm->map = gm->base;
117  return gm;
118 }
int h
Definition: greymap.h:21
static ptrdiff_t getsize(int dy, int h)
Definition: greymap.cpp:46
int w
Definition: greymap.h:20
gm_sample_t * base
Definition: greymap.h:24
gm_sample_t * map
Definition: greymap.h:25
int dy
Definition: greymap.h:22
signed short int gm_sample_t
Definition: greymap.h:13
int gm_print ( FILE *  f,
greymap_t gm 
)

Definition at line 1430 of file greymap.cpp.

References GM_GET, greymap_s::h, and greymap_s::w.

1431 {
1432  int x, y;
1433  int xx, yy;
1434  int d, t;
1435  int sw, sh;
1436 
1437  sw = gm->w < 79 ? gm->w : 79;
1438  sh = gm->w < 79 ? gm->h : gm->h * sw * 44 / ( 79 * gm->w );
1439 
1440  for( yy = sh - 1; yy >= 0; yy-- )
1441  {
1442  for( xx = 0; xx < sw; xx++ )
1443  {
1444  d = 0;
1445  t = 0;
1446 
1447  for( x = xx * gm->w / sw; x < ( xx + 1 ) * gm->w / sw; x++ )
1448  {
1449  for( y = yy * gm->h / sh; y < ( yy + 1 ) * gm->h / sh; y++ )
1450  {
1451  d += GM_GET( gm, x, y );
1452  t += 256;
1453  }
1454  }
1455 
1456  fputc( "*#=- "[5 * d / t], f ); /* what a cute trick :) */
1457  }
1458 
1459  fputc( '\n', f );
1460  }
1461 
1462  return 0;
1463 }
int h
Definition: greymap.h:21
int w
Definition: greymap.h:20
#define GM_GET(gm, x, y)
Definition: greymap.h:41
int gm_read ( FILE *  f,
greymap_t **  gmp 
)

Definition at line 378 of file greymap.cpp.

References fgetc_ws(), gm_readbody_bmp(), and gm_readbody_pnm().

379 {
380  int magic[2];
381 
382  /* read magic number. We ignore whitespace and comments before the
383  * magic, for the benefit of concatenated files in P1-P3 format.
384  * Multiple P1-P3 images in a single file are not formally allowed
385  * by the PNM standard, but there is no harm in being lenient. */
386 
387  magic[0] = fgetc_ws( f );
388 
389  if( magic[0] == EOF )
390  {
391  /* files which contain only comments and whitespace count as "empty" */
392  return -3;
393  }
394 
395  magic[1] = fgetc( f );
396 
397  if( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' )
398  {
399  return gm_readbody_pnm( f, gmp, magic[1] );
400  }
401 
402  if( magic[0] == 'B' && magic[1] == 'M' )
403  {
404  return gm_readbody_bmp( f, gmp );
405  }
406 
407  return -4;
408 }
static int gm_readbody_bmp(FILE *f, greymap_t **gmp)
Definition: greymap.cpp:834
static int gm_readbody_pnm(FILE *f, greymap_t **gmp, int magic)
Definition: greymap.cpp:415
static int fgetc_ws(FILE *f)
Definition: greymap.cpp:257
int gm_writepgm ( FILE *  f,
greymap_t gm,
const char *  comment,
int  raw,
int  mode,
double  gamma 
)

Definition at line 1325 of file greymap.cpp.

References GM_MODE_NEGATIVE, GM_MODE_NONZERO, GM_MODE_ODD, GM_MODE_POSITIVE, GM_UGET, greymap_s::h, mod, and greymap_s::w.

1326 {
1327  int x, y, v;
1328  int gammatable[256];
1329 
1330  /* prepare gamma correction lookup table */
1331  if( gamma != 1.0 )
1332  {
1333  gammatable[0] = 0;
1334 
1335  for( v = 1; v < 256; v++ )
1336  {
1337  gammatable[v] = (int) ( 255 * exp( log( v / 255.0 ) / gamma ) + 0.5 );
1338  }
1339  }
1340  else
1341  {
1342  for( v = 0; v < 256; v++ )
1343  {
1344  gammatable[v] = v;
1345  }
1346  }
1347 
1348  fprintf( f, raw ? "P5\n" : "P2\n" );
1349 
1350  if( comment && *comment )
1351  {
1352  fprintf( f, "# %s\n", comment );
1353  }
1354 
1355  fprintf( f, "%d %d 255\n", gm->w, gm->h );
1356 
1357  for( y = gm->h - 1; y >= 0; y-- )
1358  {
1359  for( x = 0; x < gm->w; x++ )
1360  {
1361  v = GM_UGET( gm, x, y );
1362 
1363  if( mode == GM_MODE_NONZERO )
1364  {
1365  if( v > 255 )
1366  {
1367  v = 510 - v;
1368  }
1369 
1370  if( v < 0 )
1371  {
1372  v = 0;
1373  }
1374  }
1375  else if( mode == GM_MODE_ODD )
1376  {
1377  v = mod( v, 510 );
1378 
1379  if( v > 255 )
1380  {
1381  v = 510 - v;
1382  }
1383  }
1384  else if( mode == GM_MODE_POSITIVE )
1385  {
1386  if( v < 0 )
1387  {
1388  v = 0;
1389  }
1390  else if( v > 255 )
1391  {
1392  v = 255;
1393  }
1394  }
1395  else if( mode == GM_MODE_NEGATIVE )
1396  {
1397  v = 510 - v;
1398 
1399  if( v < 0 )
1400  {
1401  v = 0;
1402  }
1403  else if( v > 255 )
1404  {
1405  v = 255;
1406  }
1407  }
1408 
1409  v = gammatable[v];
1410 
1411  if( raw )
1412  {
1413  fputc( v, f );
1414  }
1415  else
1416  {
1417  fprintf( f, x == gm->w - 1 ? "%d\n" : "%d ", v );
1418  }
1419  }
1420  }
1421 
1422  return 0;
1423 }
int h
Definition: greymap.h:21
#define GM_MODE_NEGATIVE
Definition: greymap.h:56
int w
Definition: greymap.h:20
#define GM_MODE_NONZERO
Definition: greymap.h:53
#define GM_MODE_ODD
Definition: greymap.h:54
#define GM_MODE_POSITIVE
Definition: greymap.h:55
#define mod(a, n)
Definition: greymap.cpp:24
#define GM_UGET(gm, x, y)
Definition: greymap.h:37

Variable Documentation

const char* gm_read_error

Definition at line 376 of file greymap.cpp.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().