Debian Codegraph @

shiny/example/imageview/main.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build example
6 // +build example
7
8 //
9 // This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
10 // install this example program. Use "go run main.go" to run it or "go install
11 // -tags=example" to install it.
12
13 // Imageview is a basic image viewer. Supported image formats include BMP, GIF,
14 // JPEG, PNG, TIFF and WEBP.
15 package main
16
17 import (
18 "fmt"
19 "image"
20 "log"
21 "os"
22
23 "golang.org/x/exp/shiny/driver"
24 "golang.org/x/exp/shiny/screen"
25 "golang.org/x/exp/shiny/widget"
26
27 _ "image/gif"
28 _ "image/jpeg"
29 _ "image/png"
30
31 _ "golang.org/x/image/bmp"
32 _ "golang.org/x/image/tiff"
33 _ "golang.org/x/image/webp"
34 )
35
36 // TODO: scrolling, such as when images are larger than the window.
37
38 func decode(filename string) (image.Image, error) {
39 f, err := os.Open(filename)
40 if err != nil {
41 return nil, err
42 }
43 defer f.Close()
44 m, _, err := image.Decode(f)
45 if err != nil {
46 return nil, fmt.Errorf("could not decode %s: %v", filename, err)
47 }
48 return m, nil
49 }
50
51 func main() {
52 log.SetFlags(0)
53 driver.Main(func(s screen.Screen) {
54 if len(os.Args) < 2 {
55 log.Fatal("no image file specified")
56 }
57 // TODO: view multiple images.
58 src, err := decode(os.Args[1])
59 if err != nil {
60 log.Fatal(err)
61 }
62 w := widget.NewSheet(widget.NewImage(src, src.Bounds()))
63 if err := widget.RunWindow(s, w, &widget.RunWindowOptions{
64 NewWindowOptions: screen.NewWindowOptions{
65 Title: "ImageView Shiny Example",
66 },
67 }); err != nil {
68 log.Fatal(err)
69 }
70 })
71 }