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
|
// Copyright 2016 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.
//go:build example
// +build example
//
// This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
// install this example program. Use "go run main.go" to run it or "go install
// -tags=example" to install it.
// Imageview is a basic image viewer. Supported image formats include BMP, GIF,
// JPEG, PNG, TIFF and WEBP.
package main
import (
"fmt"
"image"
"log"
"os"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/exp/shiny/widget"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
// TODO: scrolling, such as when images are larger than the window.
func decode(filename string) (image.Image, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
m, _, err := image.Decode(f)
if err != nil {
return nil, fmt.Errorf("could not decode %s: %v", filename, err)
}
return m, nil
}
func main() {
log.SetFlags(0)
driver.Main(func(s screen.Screen) {
if len(os.Args) < 2 {
log.Fatal("no image file specified")
}
// TODO: view multiple images.
src, err := decode(os.Args[1])
if err != nil {
log.Fatal(err)
}
w := widget.NewSheet(widget.NewImage(src, src.Bounds()))
if err := widget.RunWindow(s, w, &widget.RunWindowOptions{
NewWindowOptions: screen.NewWindowOptions{
Title: "ImageView Shiny Example",
},
}); err != nil {
log.Fatal(err)
}
})
}
|