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

Go to the source code of this file.

Classes

struct  greymap_s
 

Macros

#define gm_index(gm, x, y)   (&(gm)->map[(x) + (y) * (ptrdiff_t) (gm)->w])
 
#define gm_safe(gm, x, y)
 
#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_MODE_NONZERO   1
 
#define GM_MODE_ODD   2
 
#define GM_MODE_POSITIVE   3
 
#define GM_MODE_NEGATIVE   4
 

Typedefs

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, 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,
 
)    GM_UGET( gm, gm_bound( x, gm->w ), gm_bound( y, gm->h ) )

Definition at line 41 of file greymap.h.

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

Definition at line 32 of file greymap.h.

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

Definition at line 37 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 38 of file greymap.h.

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

#define gm_index (   gm,
  x,
 
)    (&(gm)->map[(x) + (y) * (ptrdiff_t) (gm)->w])

Definition at line 28 of file greymap.h.

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

Definition at line 39 of file greymap.h.

#define GM_MODE_NEGATIVE   4

Definition at line 50 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_NONZERO   1

Definition at line 47 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_ODD   2

Definition at line 48 of file greymap.h.

Referenced by gm_writepgm().

#define GM_MODE_POSITIVE   3

Definition at line 49 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 40 of file greymap.h.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().

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

Definition at line 29 of file greymap.h.

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

Definition at line 33 of file greymap.h.

Referenced by gm_writepgm().

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

Definition at line 34 of file greymap.h.

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

Definition at line 35 of file greymap.h.

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

Definition at line 36 of file greymap.h.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().

Typedef Documentation

typedef struct greymap_s greymap_t

Definition at line 23 of file greymap.h.

Function Documentation

void gm_clear ( greymap_t gm,
int  b 
)

Definition at line 95 of file greymap.cpp.

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

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().

96 {
97  int i;
98 
99  if( b==0 )
100  {
101  memset( gm->map, 0, gm->w * gm->h * sizeof(signed short int) );
102  }
103  else
104  {
105  for( i = 0; i<gm->w * gm->h; i++ )
106  {
107  gm->map[i] = b;
108  }
109  }
110 }
int h
Definition: greymap.h:20
signed short int * map
Definition: greymap.h:21
int w
Definition: greymap.h:19
greymap_t* gm_dup ( greymap_t gm)

Definition at line 80 of file greymap.cpp.

References gm_new(), greymap_s::h, greymap_s::map, and greymap_s::w.

81 {
82  greymap_t* gm1 = gm_new( gm->w, gm->h );
83 
84  if( !gm1 )
85  {
86  return NULL;
87  }
88 
89  memcpy( gm1->map, gm->map, gm->w * gm->h * sizeof(signed short int) );
90  return gm1;
91 }
int h
Definition: greymap.h:20
signed short int * map
Definition: greymap.h:21
int w
Definition: greymap.h:19
greymap_t * gm_new(int w, int h)
Definition: greymap.cpp:34
void gm_free ( greymap_t gm)

Definition at line 68 of file greymap.cpp.

References greymap_s::map.

Referenced by gm_readbody_pnm().

69 {
70  if( gm )
71  {
72  free( gm->map );
73  }
74 
75  free( gm );
76 }
signed short int * map
Definition: greymap.h:21
greymap_t* gm_new ( int  w,
int  h 
)

Definition at line 34 of file greymap.cpp.

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

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

35 {
36  greymap_t* gm;
37  ptrdiff_t size = (ptrdiff_t) w * (ptrdiff_t) h * (ptrdiff_t) sizeof(signed short int);
38 
39  /* check for overflow error */
40  if( size < 0 || size / w / h != sizeof(signed short int) )
41  {
42  errno = ENOMEM;
43  return NULL;
44  }
45 
46  gm = (greymap_t*) malloc( sizeof(greymap_t) );
47 
48  if( !gm )
49  {
50  return NULL;
51  }
52 
53  gm->w = w;
54  gm->h = h;
55  gm->map = (signed short int*) malloc( size );
56 
57  if( !gm->map )
58  {
59  free( gm );
60  return NULL;
61  }
62 
63  return gm;
64 }
int h
Definition: greymap.h:20
signed short int * map
Definition: greymap.h:21
int w
Definition: greymap.h:19
int gm_print ( FILE *  f,
greymap_t gm 
)

Definition at line 1249 of file greymap.cpp.

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

1250 {
1251  int x, y;
1252  int xx, yy;
1253  int d, t;
1254  int sw, sh;
1255 
1256  sw = gm->w < 79 ? gm->w : 79;
1257  sh = gm->w < 79 ? gm->h : gm->h * sw * 44 / (79 * gm->w);
1258 
1259  for( yy = sh - 1; yy>=0; yy-- )
1260  {
1261  for( xx = 0; xx<sw; xx++ )
1262  {
1263  d = 0;
1264  t = 0;
1265 
1266  for( x = xx * gm->w / sw; x<(xx + 1) * gm->w / sw; x++ )
1267  {
1268  for( y = yy * gm->h / sh; y<(yy + 1) * gm->h / sh; y++ )
1269  {
1270  d += GM_GET( gm, x, y );
1271  t += 256;
1272  }
1273  }
1274 
1275  fputc( "*#=- "[5 * d / t], f ); /* what a cute trick :) */
1276  }
1277 
1278  fputc( '\n', f );
1279  }
1280 
1281  return 0;
1282 }
int h
Definition: greymap.h:20
int w
Definition: greymap.h:19
#define GM_GET(gm, x, y)
Definition: greymap.h:37
int gm_read ( FILE *  f,
greymap_t **  gmp 
)

Definition at line 239 of file greymap.cpp.

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

240 {
241  int magic[2];
242 
243  /* read magic number. We ignore whitespace and comments before the
244  * magic, for the benefit of concatenated files in P1-P3 format.
245  * Multiple P1-P3 images in a single file are not formally allowed
246  * by the PNM standard, but there is no harm in being lenient. */
247 
248  magic[0] = fgetc_ws( f );
249 
250  if( magic[0] == EOF )
251  {
252  /* files which contain only comments and whitespace count as "empty" */
253  return -3;
254  }
255 
256  magic[1] = fgetc( f );
257 
258  if( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' )
259  {
260  return gm_readbody_pnm( f, gmp, magic[1] );
261  }
262 
263  if( magic[0] == 'B' && magic[1] == 'M' )
264  {
265  return gm_readbody_bmp( f, gmp );
266  }
267 
268  return -4;
269 }
static int gm_readbody_bmp(FILE *f, greymap_t **gmp)
Definition: greymap.cpp:680
static int gm_readbody_pnm(FILE *f, greymap_t **gmp, int magic)
Definition: greymap.cpp:276
static int fgetc_ws(FILE *f)
Definition: greymap.cpp:118
int gm_writepgm ( FILE *  f,
greymap_t gm,
char *  comment,
int  raw,
int  mode,
double  gamma 
)

Definition at line 1144 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.

1145 {
1146  int x, y, v;
1147  int gammatable[256];
1148 
1149  /* prepare gamma correction lookup table */
1150  if( gamma != 1.0 )
1151  {
1152  gammatable[0] = 0;
1153 
1154  for( v = 1; v<256; v++ )
1155  {
1156  gammatable[v] = (int) (255 * exp( log( v / 255.0 ) / gamma ) + 0.5);
1157  }
1158  }
1159  else
1160  {
1161  for( v = 0; v<256; v++ )
1162  {
1163  gammatable[v] = v;
1164  }
1165  }
1166 
1167  fprintf( f, raw ? "P5\n" : "P2\n" );
1168 
1169  if( comment && *comment )
1170  {
1171  fprintf( f, "# %s\n", comment );
1172  }
1173 
1174  fprintf( f, "%d %d 255\n", gm->w, gm->h );
1175 
1176  for( y = gm->h - 1; y>=0; y-- )
1177  {
1178  for( x = 0; x<gm->w; x++ )
1179  {
1180  v = GM_UGET( gm, x, y );
1181 
1182  if( mode == GM_MODE_NONZERO )
1183  {
1184  if( v > 255 )
1185  {
1186  v = 510 - v;
1187  }
1188 
1189  if( v < 0 )
1190  {
1191  v = 0;
1192  }
1193  }
1194  else if( mode == GM_MODE_ODD )
1195  {
1196  v = mod( v, 510 );
1197 
1198  if( v > 255 )
1199  {
1200  v = 510 - v;
1201  }
1202  }
1203  else if( mode == GM_MODE_POSITIVE )
1204  {
1205  if( v < 0 )
1206  {
1207  v = 0;
1208  }
1209  else if( v > 255 )
1210  {
1211  v = 255;
1212  }
1213  }
1214  else if( mode == GM_MODE_NEGATIVE )
1215  {
1216  v = 510 - v;
1217 
1218  if( v < 0 )
1219  {
1220  v = 0;
1221  }
1222  else if( v > 255 )
1223  {
1224  v = 255;
1225  }
1226  }
1227 
1228  v = gammatable[v];
1229 
1230  if( raw )
1231  {
1232  fputc( v, f );
1233  }
1234  else
1235  {
1236  fprintf( f, x == gm->w - 1 ? "%d\n" : "%d ", v );
1237  }
1238  }
1239  }
1240 
1241  return 0;
1242 }
int h
Definition: greymap.h:20
#define GM_MODE_NEGATIVE
Definition: greymap.h:50
int w
Definition: greymap.h:19
#define GM_MODE_NONZERO
Definition: greymap.h:47
#define GM_MODE_ODD
Definition: greymap.h:48
#define GM_MODE_POSITIVE
Definition: greymap.h:49
#define mod(a, n)
Definition: greymap.cpp:24
#define GM_UGET(gm, x, y)
Definition: greymap.h:33

Variable Documentation

const char* gm_read_error

Definition at line 237 of file greymap.cpp.

Referenced by gm_readbody_bmp(), and gm_readbody_pnm().