Skip to content

Commit

Permalink
Merge pull request #30 from owulveryck/color-screenshot
Browse files Browse the repository at this point in the history
Screenshot in color
  • Loading branch information
owulveryck authored Dec 22, 2021
2 parents 196f5b6 + 08485f3 commit b5a6f09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
7 changes: 6 additions & 1 deletion internal/client/grabber.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func (g *Grabber) GetScreenshot(w http.ResponseWriter, r *http.Request) {
tick := time.NewTicker(1 * time.Second)
select {
case img := <-g.imageC:
m := createTransparentImage(img)
var m *image.RGBA
if g.conf.Colorize {
m = createTransparentImage(colorize(img))
} else {
m = createTransparentImage(img)
}
w.Header().Add("Content-Type", "image/png")
if err := png.Encode(w, m); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
33 changes: 22 additions & 11 deletions internal/client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,32 @@ func (g *Grabber) imageHandler(ctx context.Context) {
}
}

func createTransparentImage(img *image.Gray) *image.RGBA {
func createTransparentImage(img image.Image) *image.RGBA {
mask := image.NewAlpha(img.Bounds())
//Direct pixel access for performance
for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ {
yp := (y - img.Rect.Min.Y) * img.Stride
for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ {
r := img.Pix[yp+(x-img.Rect.Min.X)]
mask.Pix[yp+(x-img.Rect.Min.X)] = uint8(255 - r)

m := image.NewRGBA(img.Bounds())
switch img := img.(type) {
case *image.Gray:
for i := 0; i < len(img.Pix); i++ {
mask.Pix[i] = uint8(255 - img.Pix[i])
}
draw.Draw(m, m.Bounds(), image.Transparent, image.Point{}, draw.Src)
draw.DrawMask(m, img.Bounds(), img, image.Point{}, mask, image.Point{}, draw.Over)
return m
case *image.RGBA:
for i := 0; i < len(img.Pix)/4; i++ {
if img.Pix[i*4] == 255 &&
img.Pix[i*4+1] == 255 &&
img.Pix[i*4+2] == 255 &&
img.Pix[i*4+3] == 255 {
img.Pix[i*4+3] = 0
}
}
return img
default:
panic("unhandled image")
}
m := image.NewRGBA(img.Bounds())
draw.Draw(m, m.Bounds(), image.Transparent, image.Point{}, draw.Src)

draw.DrawMask(m, img.Bounds(), img, image.Point{}, mask, image.Point{}, draw.Over)
return m
}

func colorize(img *image.Gray) *image.RGBA {
Expand Down

0 comments on commit b5a6f09

Please sign in to comment.