loadimage.go (1605B)
1 // Copyright 2014 Hajime Hoshi 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build darwin freebsd js linux windows 16 // +build !android 17 // +build !ios 18 19 package ebitenutil 20 21 import ( 22 "image" 23 24 "github.com/hajimehoshi/ebiten/v2" 25 ) 26 27 // NewImageFromFile loads the file with path and returns ebiten.Image and image.Image. 28 // 29 // Image decoders must be imported when using NewImageFromFile. For example, 30 // if you want to load a PNG image, you'd need to add `_ "image/png"` to the import section. 31 // 32 // How to solve path depends on your environment. This varies on your desktop or web browser. 33 // Note that this doesn't work on mobiles. 34 // 35 // For productions, instead of using NewImageFromFile, it is safer to embed your resources, e.g., with github.com/rakyll/statik . 36 func NewImageFromFile(path string) (*ebiten.Image, image.Image, error) { 37 file, err := OpenFile(path) 38 if err != nil { 39 return nil, nil, err 40 } 41 defer func() { 42 _ = file.Close() 43 }() 44 img, _, err := image.Decode(file) 45 if err != nil { 46 return nil, nil, err 47 } 48 img2 := ebiten.NewImageFromImage(img) 49 return img2, img, err 50 }