| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"bufio" |
| 16 |
"fmt" |
| 17 |
"image" |
| 18 |
"image/draw" |
| 19 |
"image/png" |
| 20 |
"log" |
| 21 |
"os" |
| 22 |
|
| 23 |
"github.com/golang/freetype/raster" |
| 24 |
"golang.org/x/image/math/fixed" |
| 25 |
) |
| 26 |
|
| 27 |
func p(x, y int) fixed.Point26_6 { |
| 28 |
return fixed.Point26_6{ |
| 29 |
X: fixed.Int26_6(x * 64), |
| 30 |
Y: fixed.Int26_6(y * 64), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func main() { |
| 35 |
|
| 36 |
r := raster.NewRasterizer(50, 50) |
| 37 |
r.Start(p(5, 5)) |
| 38 |
r.Add1(p(5, 25)) |
| 39 |
r.Add2(p(5, 45), p(25, 45)) |
| 40 |
r.Add1(p(45, 45)) |
| 41 |
r.Add1(p(45, 44)) |
| 42 |
r.Add1(p(26, 44)) |
| 43 |
r.Add2(p(6, 44), p(6, 24)) |
| 44 |
r.Add1(p(6, 5)) |
| 45 |
r.Add1(p(5, 5)) |
| 46 |
|
| 47 |
|
| 48 |
const ( |
| 49 |
w = 600 |
| 50 |
h = 200 |
| 51 |
) |
| 52 |
rgba := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 53 |
draw.Draw(rgba, image.Rect(0, 0, w, h/2), image.Black, image.ZP, draw.Src) |
| 54 |
draw.Draw(rgba, image.Rect(0, h/2, w, h), image.White, image.ZP, draw.Src) |
| 55 |
mask := image.NewAlpha(image.Rect(0, 0, 50, 50)) |
| 56 |
painter := raster.NewAlphaSrcPainter(mask) |
| 57 |
gammas := []float64{1.0 / 10.0, 1.0 / 3.0, 1.0 / 2.0, 2.0 / 3.0, 4.0 / 5.0, 1.0, 5.0 / 4.0, 3.0 / 2.0, 2.0, 3.0, 10.0} |
| 58 |
for i, g := range gammas { |
| 59 |
draw.Draw(mask, mask.Bounds(), image.Transparent, image.ZP, draw.Src) |
| 60 |
r.Rasterize(raster.NewGammaCorrectionPainter(painter, g)) |
| 61 |
x, y := 50*i+25, 25 |
| 62 |
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.White, image.ZP, mask, image.ZP, draw.Over) |
| 63 |
y += 100 |
| 64 |
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.Black, image.ZP, mask, image.ZP, draw.Over) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
outFile, err := os.Create("out.png") |
| 69 |
if err != nil { |
| 70 |
log.Println(err) |
| 71 |
os.Exit(1) |
| 72 |
} |
| 73 |
defer outFile.Close() |
| 74 |
b := bufio.NewWriter(outFile) |
| 75 |
err = png.Encode(b, rgba) |
| 76 |
if err != nil { |
| 77 |
log.Println(err) |
| 78 |
os.Exit(1) |
| 79 |
} |
| 80 |
err = b.Flush() |
| 81 |
if err != nil { |
| 82 |
log.Println(err) |
| 83 |
os.Exit(1) |
| 84 |
} |
| 85 |
fmt.Println("Wrote out.png OK.") |
| 86 |
} |