[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: gEDA-user: Silkscreen Logo



> It's a bitmap image.  I got it off their website by taking a screen shot.

I have this program that converts PBM bitmaps to PCBs...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

typedef unsigned char byte;

typedef struct {
  int width;
  int rwidth;
  int height;
  byte **pix;
  byte **npix;
} PBM;

static PBM *pbm;

int
read_pbm_int (FILE *f)
{
  int c, rv = 0;
  while ((c = fgetc (f)) != EOF)
    {
      if (c == '#')
	do { c = fgetc (f); } while (c != '\n');
      if (isdigit (c))
	{
	  rv = c - '0';
	  while ((c = fgetc(f)) != EOF)
	    {
	      if (!isdigit (c))
		return rv;
	      rv *= 10;
	      rv += c - '0';
	    }
	  return rv;
	}
    }
}

PBM *
load_pbm (char *filename)
{
  int w, h, d;
  int i, x, y;
  FILE *f;
  PBM *pbm;

  f = fopen(filename, "rb");
  if (!f)
    {
      perror(filename);
      exit(1);
    }
  if (fgetc (f) != 'P'
      || fgetc (f) != '4')
    {
      printf("%s not a binary PBM file\n", filename);
      exit(1);
    }
  w = read_pbm_int (f);
  h = read_pbm_int (f);
  pbm = (PBM *)malloc (sizeof(PBM));
  pbm->width = w;
  pbm->height = h;
  pbm->pix = (byte **)malloc(w * sizeof (byte *));
  pbm->npix = (byte **)malloc(w * sizeof (byte *));
  for (x=0; x<w; x++)
    pbm->pix[x] = (byte *)malloc(h);
  for (x=0; x<w; x++)
    pbm->npix[x] = (byte *)malloc(h);
  for (y=0; y<h; y++)
    for (x=0; x<(w+7)/8; x++)
      pbm->pix[x][y] = fgetc(f);
  fclose(f);
  return pbm;
}

static int
get_pixel (int x, int y)
{
  if (x < 0 || x >= pbm->width)
    return 0;
  if (y < 0 || y >= pbm->height)
    return 0;
  if (pbm->pix[x/8][y] & (0x80 >> (x%8)))
    return 1;
  return 0;
}

static void
put_pixel (int x, int y, int v)
{
  if (x < 0 || x >= pbm->width)
    return;
  if (y < 0 || y >= pbm->height)
    return;
  if (v)
    pbm->npix[x/8][y] |= (0x80 >> (x%8));
  else
    pbm->npix[x/8][y] &= ~(0x80 >> (x%8));
}

static void
set_new ()
{
  int x;
  for (x=0; x<pbm->width; x++)
    memcpy (pbm->npix[x], pbm->pix[x], pbm->height);
}

static void
set_old ()
{
  int x;
  for (x=0; x<pbm->width; x++)
    memcpy (pbm->pix[x], pbm->npix[x], pbm->height);
}

static void
write_pbm (char *outname)
{
  int x, y;
  FILE *out = stdout;
  if (outname)
    out = fopen(outname, "wb");
  fprintf (out, "P4\n%d %d\n", pbm->width, pbm->height);
  for (y=0; y<pbm->height; y++)
    for (x=0; x<(pbm->width+7)/8; x++)
      fputc (pbm->pix[x][y], out);
  if (outname)
    fclose (out);
}

static int
edge (int x, int y)
{
  int ix, iy;
  for (ix=-2; ix<=2; ix++)
    for (iy=-2; iy<=2; iy++)
      if (get_pixel (x+ix, y+iy) == 0)
    return 1;
  return 0;
}

static void
invert ()
{
  int x, y;
  set_new ();
  for (x=1; x<pbm->width-1; x++)
    for (y=1; y<pbm->height-1; y++)
      put_pixel (x, y, ! get_pixel (x, y));
  set_old ();
}

static void
strip_edges ()
{
  int x, y;
  set_new ();
  for (x=1; x<pbm->width-1; x++)
    for (y=1; y<pbm->height-1; y++)
      if (get_pixel (x, y))
	{
	  if (edge (x, y))
	    put_pixel (x, y, 0);
	}
  set_old ();
}

static void
half_mask ()
{
  int x, y;
  set_new ();
  for (x=1; x<pbm->width-1; x++)
    for (y=1; y<pbm->height-1; y++)
      if (get_pixel (x, y))
	{
	  if (!edge (x, y))
	    if ((x^y) & 1)
	      put_pixel (x, y, 0);
	}
  set_old ();
}

int
main(int argc, char **argv)
{
  pbm = load_pbm (argv[1]);

  invert ();

  //strip_edges ();
  //strip_edges ();

  //half_mask ();

  write_pbm (argv[2]);

  return 0;
}


_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user