在Golang的draw2d包中填充一个像素。

huangapple go评论73阅读模式
英文:

Fill one pixel in Golang draw2d package

问题

有没有一种方法可以在Golang中逐个像素填充图像,最好使用draw2d包?

例如,可以使用stroke()命令绘制一条线,如下所示(来自他们的入门页面):

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"

    "code.google.com/p/draw2d/draw2d"
    "image"
    "image/png"
)

func saveToPngFile(filePath string, m image.Image) {
    f, err := os.Create(filePath)
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    defer f.Close()
    b := bufio.NewWriter(f)
    err = png.Encode(b, m)
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    err = b.Flush()
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    fmt.Printf("Wrote %s OK.\n", filePath)
}

func main() {
    i := image.NewRGBA(image.Rect(0, 0, 200, 200))
    gc := draw2d.NewGraphicContext(i)
    gc.MoveTo(10.0, 10.0)
    gc.LineTo(100.0, 10.0)
    gc.Stroke()
    saveToPngFile("TestPath.png", i)
}

但是,我如何只填充一个像素,而不是连接两个点?Draw2d并不是必需的,只是我认为它可能是最简单的方法。

英文:

Is there a way to draw an image by filling one pixel at a time in Golang, preferably using the draw2d package?

For example, one can draw a line by using the stroke() command as such (from their getting started page):

 package main

impenter code hereort (
        "bufio"
        "fmt"
        "log"
        "os"

        "code.google.com/p/draw2d/draw2d"
        "image"
        "image/png"
)

func saveToPngFile(filePath string, m image.Image) {
        f, err := os.Create(filePath)
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        defer f.Close()
        b := bufio.NewWriter(f)
        err = png.Encode(b, m)
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        err = b.Flush()
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        fmt.Printf("Wrote %s OK.\n", filePath)
}

func main() {
        i := image.NewRGBA(image.Rect(0, 0, 200, 200))
        gc := draw2d.NewGraphicContext(i)
        gc.MoveTo(10.0, 10.0)
        gc.LineTo(100.0, 10.0)
        gc.Stroke()
        saveToPngFile("TestPath.png", i)
}

But how could I fill just one pixel, instead of connecting the 2 points? Draw2d is not a necessity, it is simply what I thought would be easiest.

答案1

得分: 5

在像draw2d或Cairo这样的矢量图形库中,很少使用像素寻址,因为模型不是位图。相反,矢量图形模型让你在一个抽象的欧几里得空间中绘制线条等操作。这样可以实现设备无关的绘图,并且避免了像素寻址,因为没有像素的概念。

然而,draw2d底层使用了标准的image包,该包允许像素寻址,例如使用Set函数

如果你需要处理大量像素,可能会导致速度较慢。你可以学习一下栅格操作。

英文:

In a vector graphics library like draw2d or Cairo, there is rarely pixel addressing because the model is not a raster of bits. Instead, the vector graphics model has you do things like draw lines in an abstract Euclidean space. This allows device independent drawing and prevents pixel addressing because there are no pixels.

However, draw2d has the standard Package image underlying it, which does allow pixel addressing as with the function Set.

If you are doing many pixels, expect it to be slow. And maybe learn about raster operations.

huangapple
  • 本文由 发表于 2014年3月31日 01:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/22747001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定