Skip to content

Commit

Permalink
Add customizable B/W threshold conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonBrandao committed May 15, 2023
1 parent 901ea82 commit 8accd4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 4 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ func (e *Epd) Bounds() image.Rectangle {
}

// Converts an image into a buffer array ready to be sent to the display.
// Due to the display only supporting 2 colors, Black and White, the white color
// (r = 0, g = 0, b = 0) is preserved. Everything else is converted to black.
// Due to the display only supporting 2 colors a threshold is applied to convert the image to pure black and white.
// The returned buffer is ready to be sent using UpdateFrame.
func (e *Epd) GetBuffer(img image.Image) []byte {
func (e *Epd) GetBuffer(img image.Image, threshold uint8) []byte {
buffer := make([]byte, e.bufferSize)

for y := 0; y < e.bounds.Dy(); y++ {
Expand All @@ -94,7 +93,7 @@ func (e *Epd) GetBuffer(img image.Image) []byte {

// Iterate and append over the next 8 pixels
for px := 0; px < PIXEL_SIZE; px++ {
if isBlack(img.At(x+px, y)) {
if isBlack(img.At(x+px, y), threshold) {
pixel |= (0x80 >> px)
}
}
Expand Down Expand Up @@ -135,7 +134,7 @@ func (e *Epd) UpdateFrameAndRefresh(buffer []byte) {

// Allows to easily send an image.Image directly to the screen.
func (e *Epd) DisplayImage(img image.Image) {
buffer := e.GetBuffer(img)
buffer := e.GetBuffer(img, 199)

e.UpdateFrameAndRefresh(buffer)
}
Expand Down
6 changes: 2 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"time"
)

func isBlack(c color.Color) bool {
r, g, b, _ := c.RGBA()

return r == 0 && g == 0 && b == 0
func isBlack(c color.Color, threshold uint8) bool {
return color.GrayModel.Convert(c).(color.Gray).Y < threshold
}

func splitInChunks(data []byte) [][]byte {
Expand Down

0 comments on commit 8accd4b

Please sign in to comment.