Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

修改 练习:切片(moretypes/18) 章节的引用问题 #381

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/moretypes/exercise-slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package main

import "golang.org/x/tour/pic"
import "github.com/Go-zh/tour/pic"

func Pic(dx, dy int) [][]uint8 {
}
Expand Down
43 changes: 43 additions & 0 deletions pic/pic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pic // import "github.com/Go-zh/tour/pic"

import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/png"
)

func Show(f func(int, int) [][]uint8) {
const (
dx = 256
dy = 256
)
data := f(dx, dy)
m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
v := data[y][x]
i := y*m.Stride + x*4
m.Pix[i] = v
m.Pix[i+1] = v
m.Pix[i+2] = 255
m.Pix[i+3] = 255
}
}
ShowImage(m)
}

func ShowImage(m image.Image) {
var buf bytes.Buffer
err := png.Encode(&buf, m)
if err != nil {
panic(err)
}
enc := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println("IMAGE:" + enc)
}