summaryrefslogtreecommitdiff
path: root/devdocs/go/image%2Fdraw%2Findex.html
blob: b44b2f3c9d3c63048591b972e925cd49e4730b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<h1> Package draw  </h1>     <ul id="short-nav">
<li><code>import "image/draw"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package draw provides image composition functions. </p>
<p>See "The Go image/draw package" for an introduction to this package: <a href="https://golang.org/doc/articles/image_draw.html">https://golang.org/doc/articles/image_draw.html</a> </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Draw">func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)</a></li>
<li><a href="#DrawMask">func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)</a></li>
<li><a href="#Drawer">type Drawer</a></li>
<li><a href="#Image">type Image</a></li>
<li><a href="#Op">type Op</a></li>
<li> <a href="#Op.Draw">func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)</a>
</li>
<li><a href="#Quantizer">type Quantizer</a></li>
<li><a href="#RGBA64Image">type RGBA64Image</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Drawer_floydSteinberg">Drawer (FloydSteinberg)</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>draw.go</span>  </p>   <h2 id="Draw">func <span>Draw</span>  </h2> <pre data-language="go">func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)</pre> <p>Draw calls <a href="#DrawMask">DrawMask</a> with a nil mask. </p>
<h2 id="DrawMask">func <span>DrawMask</span>  </h2> <pre data-language="go">func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)</pre> <p>DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r in dst with the result of a Porter-Duff composition. A nil mask is treated as opaque. </p>
<h2 id="Drawer">type <span>Drawer</span>  <span title="Added in Go 1.2">1.2</span> </h2> <p>Drawer contains the <a href="#Draw">Draw</a> method. </p>
<pre data-language="go">type Drawer interface {
    // Draw aligns r.Min in dst with sp in src and then replaces the
    // rectangle r in dst with the result of drawing src on dst.
    Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}</pre> <p>FloydSteinberg is a <a href="#Drawer">Drawer</a> that is the <a href="#Src">Src</a> <a href="#Op">Op</a> with Floyd-Steinberg error diffusion. </p>
<pre data-language="go">var FloydSteinberg Drawer = floydSteinberg{}</pre>    <h4 id="example_Drawer_floydSteinberg"> <span class="text">Example (FloydSteinberg)</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
const width = 130
const height = 50

im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})
for x := 0; x &lt; width; x++ {
    for y := 0; y &lt; height; y++ {
        dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255
        var gray uint8
        if dist &gt; 255 {
            gray = 255
        } else {
            gray = uint8(dist)
        }
        im.SetGray(x, y, color.Gray{Y: 255 - gray})
    }
}
pi := image.NewPaletted(im.Bounds(), []color.Color{
    color.Gray{Y: 255},
    color.Gray{Y: 160},
    color.Gray{Y: 70},
    color.Gray{Y: 35},
    color.Gray{Y: 0},
})

draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.Point{})
shade := []string{" ", "░", "▒", "▓", "█"}
for i, p := range pi.Pix {
    fmt.Print(shade[p])
    if (i+1)%width == 0 {
        fmt.Print("\n")
    }
}
</pre>   <h2 id="Image">type <span>Image</span>  </h2> <p>Image is an image.Image with a Set method to change a single pixel. </p>
<pre data-language="go">type Image interface {
    image.Image
    Set(x, y int, c color.Color)
}</pre> <h2 id="Op">type <span>Op</span>  </h2> <p>Op is a Porter-Duff compositing operator. </p>
<pre data-language="go">type Op int</pre> <pre data-language="go">const (
    // Over specifies ``(src in mask) over dst''.
    Over Op = iota
    // Src specifies ``src in mask''.
    Src
)</pre> <h3 id="Op.Draw">func (Op) <span>Draw</span>  <span title="Added in Go 1.2">1.2</span> </h3> <pre data-language="go">func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)</pre> <p>Draw implements the <a href="#Drawer">Drawer</a> interface by calling the Draw function with this <a href="#Op">Op</a>. </p>
<h2 id="Quantizer">type <span>Quantizer</span>  <span title="Added in Go 1.2">1.2</span> </h2> <p>Quantizer produces a palette for an image. </p>
<pre data-language="go">type Quantizer interface {
    // Quantize appends up to cap(p) - len(p) colors to p and returns the
    // updated palette suitable for converting m to a paletted image.
    Quantize(p color.Palette, m image.Image) color.Palette
}</pre> <h2 id="RGBA64Image">type <span>RGBA64Image</span>  <span title="Added in Go 1.17">1.17</span> </h2> <p>RGBA64Image extends both the <a href="#Image">Image</a> and <span>image.RGBA64Image</span> interfaces with a SetRGBA64 method to change a single pixel. SetRGBA64 is equivalent to calling Set, but it can avoid allocations from converting concrete color types to the <span>color.Color</span> interface type. </p>
<pre data-language="go">type RGBA64Image interface {
    image.RGBA64Image
    Set(x, y int, c color.Color)
    SetRGBA64(x, y int, c color.RGBA64)
}</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/image/draw/" class="_attribution-link">http://golang.org/pkg/image/draw/</a>
  </p>
</div>