Ejemplo n.º 1
0
func test(t *testing.T, draw sample) {
	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2d.NewGraphicContext(dest)
	// Draw Android logo
	output, err := draw(gc, "png")
	if err != nil {
		t.Errorf("Drawing %q failed: %v", output, err)
		return
	}
	// Save to png
	err = draw2d.SaveToPngFile(output, dest)
	if err != nil {
		t.Errorf("Saving %q failed: %v", output, err)
	}
}
Ejemplo n.º 2
0
func imgPng(w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-type", "image/png")

	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2d.NewGraphicContext(dest)

	// Draw sample
	android.Draw(gc, 65, 0)

	err := png.Encode(w, dest)
	if err != nil {
		return &appError{err, fmt.Sprintf("Can't encode: %s", err), 500}
	}

	return nil
}
Ejemplo n.º 3
0
// Draw uses the parameters in the hilbertImage and returns a Image
func (h *HilbertImage) Draw() (draw.Image, error) {

	// Create a Hilbert Curve Mapper
	s, err := hilbert.New(h.N)
	if err != nil {
		return nil, fmt.Errorf("Failed to create hilbert space: %s", err.Error())
	}

	img, err := h.createImage()
	if err != nil {
		return nil, err
	}

	gc := draw2d.NewGraphicContext(img)
	gc.Save()

	var lastX, lastY float64 = -1, -1
	for t := 0; t < h.N*h.N; t++ {

		// Map the 1D number into the 2D space
		x, y, err := s.Map(t)
		if err != nil {
			return nil, err
		}

		px1, py1 := h.toPixel(x, y)
		px2, py2 := h.toPixel(x+1, y+1)

		h.drawRectange(gc, px1, py1, px2, py2)
		h.drawText(gc, px1, py1, t)

		centerX, centerY := px1+h.SquareSize/2, py1+h.SquareSize/2
		if lastX != -1 && lastY != -1 {
			h.drawLine(gc, lastX, lastY, centerX, centerY)
		}

		lastX, lastY = centerX, centerY
	}

	gc.Restore()
	return img, nil
}
Ejemplo n.º 4
0
// NewWith returns a new image canvas created according to the specified
// options. The currently accepted options are UseWH,
// UseDPI, UseImage, and UseImageWithContext.
// Each of the options specifies the size of the canvas (UseWH, UseImage),
// the resolution of the canvas (UseDPI), or both (useImageWithContext).
// If size or resolution are not specified, defaults are used.
// It panics if size and resolution are overspecified (i.e., too many options are
// passed).
func NewWith(o ...option) *Canvas {
	c := new(Canvas)
	var g uint32
	for _, opt := range o {
		f := opt(c)
		if g&f != 0 {
			panic("incompatible options")
		}
		g |= f
	}
	if c.dpi == 0 {
		c.dpi = DefaultDPI
	}
	if c.w == 0 { // h should also == 0.
		if c.img == nil {
			c.w = DefaultWidth
			c.h = DefaultHeight
		} else {
			w := float64(c.img.Bounds().Max.X - c.img.Bounds().Min.X)
			h := float64(c.img.Bounds().Max.Y - c.img.Bounds().Min.Y)
			c.w = vg.Length(w/float64(c.dpi)) * vg.Inch
			c.h = vg.Length(h/float64(c.dpi)) * vg.Inch
		}
	}
	if c.img == nil {
		w := c.w / vg.Inch * vg.Length(c.dpi)
		h := c.h / vg.Inch * vg.Length(c.dpi)
		c.img = draw.Image(image.NewRGBA(image.Rect(0, 0, int(w+0.5), int(h+0.5))))
	}
	if c.gc == nil {
		h := float64(c.img.Bounds().Max.Y - c.img.Bounds().Min.Y)
		c.gc = draw2d.NewGraphicContext(c.img)
		c.gc.SetDPI(c.dpi)
		c.gc.Scale(1, -1)
		c.gc.Translate(0, -h)
	}
	draw.Draw(c.img, c.img.Bounds(), image.White, image.ZP, draw.Src)
	c.color = []color.Color{color.Black}
	vg.Initialize(c)
	return c
}