forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
158 lines (134 loc) · 5.05 KB
/
image.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package canvas
import (
"image"
"io"
"io/ioutil"
"path/filepath"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
)
// ImageFill defines the different type of ways an image can stretch to fill its space.
type ImageFill int
const (
// ImageFillStretch will scale the image to match the Size() values.
// This is the default and does not maintain aspect ratio.
ImageFillStretch ImageFill = iota
// ImageFillContain makes the image fit within the object Size(),
// centrally and maintaining aspect ratio.
// There may be transparent sections top and bottom or left and right.
ImageFillContain //(Fit)
// ImageFillOriginal ensures that the container grows to the pixel dimensions
// required to fit the original image. The aspect of the image will be maintained so,
// as with ImageFillContain there may be transparent areas around the image.
// Note that the minSize may be smaller than the image dimensions if scale > 1.
ImageFillOriginal
)
// ImageScale defines the different scaling filters used to scaling images
type ImageScale int32
const (
// ImageScaleSmooth will scale the image using ApproxBiLinear filter (or GL equivalent)
ImageScaleSmooth ImageScale = 0
// ImageScalePixels will scale the image using NearestNeighbor filter (or GL equivalent)
ImageScalePixels ImageScale = 1
// ImageScaleFastest will scale the image using hardware GPU if available
//
// Since: 2.0
ImageScaleFastest ImageScale = 2
)
// Declare conformity with CanvasObject interface
var _ fyne.CanvasObject = (*Image)(nil)
// Image describes a drawable image area that can render in a Fyne canvas
// The image may be a vector or a bitmap representation and it will fill the area.
// The fill mode can be changed by setting FillMode to a different ImageFill.
type Image struct {
baseObject
// one of the following sources will provide our image data
File string // Load the image from a file
Resource fyne.Resource // Load the image from an in-memory resource
Image image.Image // Specify a loaded image to use in this canvas object
Translucency float64 // Set a translucency value > 0.0 to fade the image
FillMode ImageFill // Specify how the image should expand to fill or fit the available space
ScaleMode ImageScale // Specify the type of scaling interpolation applied to the image
}
// Alpha is a convenience function that returns the alpha value for an image
// based on its Translucency value. The result is 1.0 - Translucency.
func (i *Image) Alpha() float64 {
return 1.0 - i.Translucency
}
// Resize on an image will scale the content or reposition it according to FillMode.
// It will normally cause a Refresh to ensure the pixels are recalculated.
func (i *Image) Resize(s fyne.Size) {
i.baseObject.Resize(s)
Refresh(i)
}
// Refresh causes this object to be redrawn in it's current state
func (i *Image) Refresh() {
Refresh(i)
}
// NewImageFromFile creates a new image from a local file.
// Images returned from this method will scale to fit the canvas object.
// The method for scaling can be set using the Fill field.
func NewImageFromFile(file string) *Image {
return &Image{
File: file,
}
}
// NewImageFromURI creates a new image from named resource.
// File URIs will read the file path and other schemes will download the data into a resource.
// Images returned from this method will scale to fit the canvas object.
// The method for scaling can be set using the Fill field.
//
// Since: 2.0
func NewImageFromURI(uri fyne.URI) *Image {
if uri.Scheme() == "file" && len(uri.String()) > 7 {
return &Image{
File: uri.String()[7:],
}
}
var read io.ReadCloser
read, err := storage.Reader(uri) // attempt unknown file type
if err != nil {
fyne.LogError("Failed to open image URI", err)
return nil
}
defer read.Close()
return NewImageFromReader(read, filepath.Base(uri.String()))
}
// NewImageFromReader creates a new image from a data stream.
// The name parameter is required to uniquely identify this image (for caching etc).
// If the image in this io.Reader is an SVG, the name should end ".svg".
// Images returned from this method will scale to fit the canvas object.
// The method for scaling can be set using the Fill field.
//
// Since: 2.0
func NewImageFromReader(read io.Reader, name string) *Image {
data, err := ioutil.ReadAll(read)
if err != nil {
fyne.LogError("Unable to read image data", err)
return nil
}
res := &fyne.StaticResource{
StaticName: name,
StaticContent: data,
}
return &Image{
Resource: res,
}
}
// NewImageFromResource creates a new image by loading the specified resource.
// Images returned from this method will scale to fit the canvas object.
// The method for scaling can be set using the Fill field.
func NewImageFromResource(res fyne.Resource) *Image {
return &Image{
Resource: res,
}
}
// NewImageFromImage returns a new Image instance that is rendered from the Go
// image.Image passed in.
// Images returned from this method will scale to fit the canvas object.
// The method for scaling can be set using the Fill field.
func NewImageFromImage(img image.Image) *Image {
return &Image{
Image: img,
}
}