Skip to content

Commit

Permalink
add doc comments and fix golint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Dec 10, 2018
1 parent 54675d5 commit 8da350b
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 177 deletions.
5 changes: 3 additions & 2 deletions bindings/gotrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package gotraceb
import (
"crypto/sha1"
"fmt"
gtr "github.com/dennwc/gotrace"
"image"
"image/color"
_ "image/png"
"os"
"testing"
"unsafe"

gtr "github.com/dennwc/gotrace"
)

func TestWordSize(t *testing.T) {
if uintptr(wordSize) != unsafe.Sizeof(uint(0))*8 {
t.Fatalf("%d vs %s", wordSize, unsafe.Sizeof(uint(0))*8)
t.Fatalf("%d vs %d", wordSize, unsafe.Sizeof(uint(0))*8)
}
}

Expand Down
23 changes: 14 additions & 9 deletions bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ var (
allBits = ^Word(0)
)

// Word packs multiple bits of a bitmap.
type Word uint

// NewBitmap creates a new bitmap with given dimensions.
func NewBitmap(w, h int) *Bitmap {
dy := 0
if w != 0 {
Expand All @@ -24,10 +26,9 @@ func NewBitmap(w, h int) *Bitmap {
}
}

// Internal bitmap format. The n-th scanline starts at scanline(n) =
// (map + n*dy). Raster data is stored as a sequence of potrace_words
// (NOT bytes). The leftmost bit of scanline n is the most significant
// bit of scanline(n)[0].
// Bitmap is an internal bitmap format. The n-th scanline starts at scanline(n) = (map + n*dy).
// Raster data is stored as a sequence of potrace_words (NOT bytes).
// The leftmost bit of scanline n is the most significant bit of scanline(n)[0].
type Bitmap struct {
W, H int // width and height, in pixels
Dy int // words per scanline (not bytes)
Expand All @@ -37,12 +38,16 @@ type Bitmap struct {
func (bm *Bitmap) scanline(y int) []Word { return bm.Map[y*bm.Dy : (y+1)*bm.Dy] }
func (bm *Bitmap) index(x, y int) *Word { return &(bm.Map[Word(y*bm.Dy)+Word(x)/wordBits]) }
func (bm *Bitmap) mask(x int) Word { return hiBit >> (Word(x) & (wordBits - 1)) }

// Get a bitmap value at given coordinates.
func (bm *Bitmap) Get(x, y int) bool {
if x >= 0 && x < bm.W && y >= 0 && y < bm.H {
return ((*bm.index(x, y)) & bm.mask(x)) != 0
}
return false
}

// Set a bitmap value at given coordinates.
func (bm *Bitmap) Set(x, y int, v bool) {
if x >= 0 && x < bm.W && y >= 0 && y < bm.H {
if v {
Expand All @@ -53,7 +58,7 @@ func (bm *Bitmap) Set(x, y int, v bool) {
}
}

// clear the given bitmap. Set all bits to c.
// Clear the given bitmap. Set all bits to c.
func (bm *Bitmap) Clear(c bool) {
if !c {
for i := range bm.Map {
Expand All @@ -69,9 +74,9 @@ func (bm *Bitmap) Clear(c bool) {
}
}

// duplicate the given bitmap
func (b *Bitmap) Clone() *Bitmap {
b2 := NewBitmap(b.W, b.H)
copy(b2.Map, b.Map)
// Clone duplicates the given bitmap.
func (bm *Bitmap) Clone() *Bitmap {
b2 := NewBitmap(bm.W, bm.H)
copy(b2.Map, bm.Map)
return b2
}
Loading

0 comments on commit 8da350b

Please sign in to comment.