golang/freetype

Can't get raster.MonochromePainter work

ksimka opened this issue · 2 comments

I'm trying to use raster.MonochromePainter, but looks like either it's broken or I'm doing something totally wrong.

I'm using draw2dimg, but that actually doesn't matter, I don't think it's their bug since the only difference is raster.Painter implementation.

Here is an example with simple RGBAPainter.

package main

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
    "github.com/golang/freetype/raster"
)

type mPainter struct {
    *raster.MonochromePainter
}
func (mp mPainter) SetColor(color color.Color) {
    mp.MonochromePainter.Painter.(*raster.RGBAPainter).SetColor(color)
}

func main() {
    im := image.NewRGBA(image.Rect(0, 0, 100, 100))

    drawPoly := func(col color.RGBA, xys ...int) {
//      painter := raster.NewRGBAPainter(im)
//      painter.SetColor(col)
//      mpainter := mPainter{raster.NewMonochromePainter(painter)}

        gc := draw2dimg.NewGraphicContext(im)
//      gc := draw2dimg.NewGraphicContextWithPainter(im, mpainter)
        gc.SetFillColor(col)
        gc.SetStrokeColor(col)
        gc.SetLineWidth(0)

        gc.MoveTo(float64(xys[0]), float64(xys[1]))
        for i := 2; i < len(xys); i += 2 {
            gc.LineTo(float64(xys[i]), float64(xys[i+1]))
        }
        gc.LineTo(float64(xys[0]), float64(xys[1]))
        gc.Close()
        gc.Fill()
    }

    drawPoly(color.RGBA{0, 200, 0, 150}, 10, 10, 60, 10, 60, 60, 10, 60)
    drawPoly(color.RGBA{200, 0, 0, 150}, 20, 20, 70, 20, 70, 70, 20, 70)

    draw2dimg.SaveToPngFile("test.png", im)
}

It outputs expected image.

test

And here is an example with MonochromePainter which description is

// A MonochromePainter wraps another Painter, quantizing each Span's alpha to
// be either fully opaque or fully transparent.
package main

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
    "github.com/golang/freetype/raster"
)

type mPainter struct {
    *raster.MonochromePainter
}
func (mp mPainter) SetColor(color color.Color) {
    mp.MonochromePainter.Painter.(*raster.RGBAPainter).SetColor(color)
}

func main() {
    im := image.NewRGBA(image.Rect(0, 0, 100, 100))

    drawPoly := func(col color.RGBA, xys ...int) {
        painter := raster.NewRGBAPainter(im)
        painter.SetColor(col)
        mpainter := mPainter{raster.NewMonochromePainter(painter)}

//      gc := draw2dimg.NewGraphicContext(im)
        gc := draw2dimg.NewGraphicContextWithPainter(im, mpainter)
        gc.SetFillColor(col)
        gc.SetStrokeColor(col)
        gc.SetLineWidth(0)

        gc.MoveTo(float64(xys[0]), float64(xys[1]))
        for i := 2; i < len(xys); i += 2 {
            gc.LineTo(float64(xys[i]), float64(xys[i+1]))
        }
        gc.LineTo(float64(xys[0]), float64(xys[1]))
        gc.Close()
        gc.Fill()
    }

    drawPoly(color.RGBA{0, 200, 0, 150}, 10, 10, 60, 10, 60, 60, 10, 60)
    drawPoly(color.RGBA{200, 0, 0, 150}, 20, 20, 70, 20, 70, 70, 20, 70)

    draw2dimg.SaveToPngFile("test.png", im)
}

It outputs empty (white backgroung) image. And even if I change alpha of fill color to 255, nothing changes. Actually, with any color I get white image like all the things are transparent.

Can you please help me with this? I can try to debug if you tell me where to look.

I'm not sure, but I think it really was a bug, and I solved it via #22 . Now all works as expected.

Solved via #22.