tour.golang.org#36 pic.Show 实现的功能

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

tour.golang.org#36 The functionality implemented by pic.Show

问题

在这个例子中,pic.Show(Pic)语句的作用是将通过Pic函数生成的图像显示出来。Pic函数接受两个参数dxdy,表示图像的宽度和高度。它返回一个二维的uint8类型的切片,表示图像的像素值。pic.Show函数会将这个像素值切片转换为图像并显示出来。

当你运行这个例子时,它会在标准输出上打印一些字符,这些字符实际上是表示图像的文本表示形式。如果你想要在图形界面中显示图像,你可以尝试在本地运行这段代码,或者使用支持图形界面的在线Go编译器。

英文:

In tour.golang.org , exercice 36 , what this statement pic.Show(Pic) supposed to do?

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. var p = make([]([]uint8), dy)
  5. for i := 0; i < len(p); i++ {
  6. p[i] = make([]uint8, dx)
  7. for j := 0; j < len(p[i]); j++ {
  8. p[i][j] = uint8((i + j) / 2)
  9. }
  10. }
  11. return p
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }

when I run the example, it just print some characters on the standard output, isn’t supposed to show some picture?

答案1

得分: 7

pic.Show() 创建图像并将其编码为 base64。以下是代码:

  1. func Show(f func(int, int) [][]uint8) {
  2. const (
  3. dx = 256
  4. dy = 256
  5. )
  6. data := f(dx, dy)
  7. m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
  8. for y := 0; y < dy; y++ {
  9. for x := 0; x < dx; x++ {
  10. v := data[y][x]
  11. i := y*m.Stride + x*4
  12. m.Pix[i] = v
  13. m.Pix[i+1] = v
  14. m.Pix[i+2] = 255
  15. m.Pix[i+3] = 255
  16. }
  17. }
  18. ShowImage(m)
  19. }
  20. func ShowImage(m image.Image) {
  21. var buf bytes.Buffer
  22. err := png.Encode(&buf, m)
  23. if err != nil {
  24. panic(err)
  25. }
  26. enc := base64.StdEncoding.EncodeToString(buf.Bytes())
  27. fmt.Println("IMAGE:" + enc)
  28. }

有趣的是,它最终打印出字符串 "IMAGE:",后面跟着 base64 编码的字符串。似乎 play.golang.org 解析输出并将其转换为 img 标签。可以尝试以下示例:http://play.golang.org/p/RZPqp164eS

英文:

pic.Show() creates the image and encodes it as base64. Here is the code:

  1. func Show(f func(int, int) [][]uint8) {
  2. const (
  3. dx = 256
  4. dy = 256
  5. )
  6. data := f(dx, dy)
  7. m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
  8. for y := 0; y &lt; dy; y++ {
  9. for x := 0; x &lt; dx; x++ {
  10. v := data[y][x]
  11. i := y*m.Stride + x*4
  12. m.Pix[i] = v
  13. m.Pix[i+1] = v
  14. m.Pix[i+2] = 255
  15. m.Pix[i+3] = 255
  16. }
  17. }
  18. ShowImage(m)
  19. }
  20. func ShowImage(m image.Image) {
  21. var buf bytes.Buffer
  22. err := png.Encode(&amp;buf, m)
  23. if err != nil {
  24. panic(err)
  25. }
  26. enc := base64.StdEncoding.EncodeToString(buf.Bytes())
  27. fmt.Println(&quot;IMAGE:&quot; + enc)
  28. }

Funny thing is it ends up printing the string "IMAGE:" followed by the base64 encoded string. Seems that play.golang.org parses the output converting this to an img tag. Try the following example: http://play.golang.org/p/RZPqp164eS

huangapple
  • 本文由 发表于 2013年9月1日 06:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/18553246.html
匿名

发表评论

匿名网友

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

确定