2014-09-10 18:58:12 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-09-14 00:31:28 +02:00
|
|
|
package common
|
2014-09-10 18:58:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
2014-09-16 01:13:12 +02:00
|
|
|
"log"
|
2014-09-10 18:58:12 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SaveAsPNG(path string, img image.Image) (err error) {
|
|
|
|
var file *os.File
|
|
|
|
if file, err = os.Create(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
err = png.Encode(writer, img)
|
|
|
|
writer.Flush()
|
|
|
|
file.Close()
|
|
|
|
return
|
|
|
|
}
|
2014-09-16 01:13:12 +02:00
|
|
|
|
|
|
|
func LoadPNG(path string) image.Image {
|
|
|
|
var err error
|
|
|
|
var file *os.File
|
|
|
|
if file, err = os.Open(path); err != nil {
|
|
|
|
//log.Printf("WARN: %s", err)
|
|
|
|
return image.White
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
reader := bufio.NewReader(file)
|
|
|
|
var img image.Image
|
|
|
|
if img, err = png.Decode(reader); err != nil {
|
|
|
|
log.Printf("WARN: %s", err)
|
|
|
|
return image.White
|
|
|
|
}
|
|
|
|
return img
|
|
|
|
}
|