class Potracer::Bitmap

The Bitmap is Potracer's representation of a bitmap in memory

Public Class Methods

new(width=250, height=250, bits='000...', map='RGB') click to toggle source

Creat a new Bitmap

  • width - width of the bitmap to be traced

  • height - height of the bitmap to be traced

  • bits - bitmap data. Can be a multi-dimensional array or a string of pixel data

  • map - how pixel data is mapped if bits is a string

static VALUE
bitmap_new (int argc, VALUE *argv, VALUE klass)
{
  int i, j, m;
  unsigned char *bits;
  potrace_bitmap_t *bm;
  VALUE bdata, row;

  bdata = Data_Make_Struct(klass, potrace_bitmap_t, bitmap_mark, bitmap_free, bm);
  bm->w = (argc > 0) ? NUM2INT(argv[0]) : BM_WIDTH;
  bm->h = (argc > 1) ? NUM2INT(argv[1]) : BM_HEIGHT;
  bm->dy = (bm->w + BM_WORDBITS - 1) / BM_WORDBITS;
  bm->map = ALLOC_N(potrace_word, bm->dy * bm->h * BM_WORDSIZE);
  memset(bm->map, 0, bm->dy * bm->h * BM_WORDSIZE);

  if (argc > 2) {
    if (T_STRING == TYPE(argv[2])) {
      bits = (unsigned char *)StringValuePtr(argv[2]);
      m = strlen(StringValuePtr(argv[3]));
      for (i = 0; i < bm->h; i++) {
        for (j = 0; j < bm->w; j++) {
          BM_PUT(bm, j, i, is_colored(bits, i, j, bm->w, m));
        }
      }
    } else {
      for (i = 0; i < bm->h; i++) {
        row = rb_ary_entry(argv[2], (long)i);
        for (j = 0; j < bm->w; j++) {
          BM_PUT(bm, j, i, NUM2INT(rb_ary_entry(row, (long)j)));
        }
      }
    }
  }

  return bdata;
}

Public Instance Methods

height click to toggle source

Get the height in pixels

static VALUE
bitmap_get_height (VALUE klass)
{
  potrace_bitmap_t *bm;
  Data_Get_Struct(klass, potrace_bitmap_t, bm);
  return rb_int_new(bm->h);
}
to_a click to toggle source

Retrieve the bitmap data as a multi-dimensional array of 1s and 0s

static VALUE
bitmap_as_array (VALUE klass)
{
  int i, j;
  potrace_bitmap_t *bm;
  VALUE result, row;
  Data_Get_Struct(klass, potrace_bitmap_t, bm);
  result = rb_ary_new2((long)bm->h);
  for (i = 0; i < bm->h; i++) {
    row = rb_ary_new2((long)bm->w);
    for (j = 0; j < bm->w; j++) {
      rb_ary_store(row, (long)j, rb_int_new(BM_GET(bm, j, i)));
    }
    rb_ary_store(result, (long)i, row);
  }

  return result;
}
width click to toggle source

Get the width in pixels

static VALUE
bitmap_get_width (VALUE klass)
{
  potrace_bitmap_t *bm;
  Data_Get_Struct(klass, potrace_bitmap_t, bm);
  return rb_int_new(bm->w);
}