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"
|
2014-09-21 17:30:19 +02:00
|
|
|
"errors"
|
2014-09-10 18:58:12 +02:00
|
|
|
"image"
|
|
|
|
"image/png"
|
2014-09-16 01:13:12 +02:00
|
|
|
"log"
|
2014-09-10 18:58:12 +02:00
|
|
|
"os"
|
2014-09-21 17:30:19 +02:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2014-09-10 18:58:12 +02:00
|
|
|
)
|
|
|
|
|
2014-10-21 16:11:20 +02:00
|
|
|
var rrand uint32
|
|
|
|
var rrandmu sync.Mutex
|
2014-09-21 17:30:19 +02:00
|
|
|
|
|
|
|
func reseed() uint32 {
|
|
|
|
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextSuffix() string {
|
2014-10-21 16:11:20 +02:00
|
|
|
rrandmu.Lock()
|
|
|
|
r := rrand
|
2014-09-21 17:30:19 +02:00
|
|
|
if r == 0 {
|
|
|
|
r = reseed()
|
|
|
|
}
|
|
|
|
r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
2014-10-21 16:11:20 +02:00
|
|
|
rrand = r
|
|
|
|
rrandmu.Unlock()
|
2014-09-21 17:30:19 +02:00
|
|
|
return strconv.Itoa(int(1e9 + r%1e9))[1:]
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:58:12 +02:00
|
|
|
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
|
|
|
|
2014-09-21 17:30:19 +02:00
|
|
|
func tmpName(tmpl string) (string, error) {
|
|
|
|
tmpPre := tmpl + ".tmp"
|
|
|
|
nconflict := 0
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
tmp := tmpPre + nextSuffix()
|
|
|
|
if _, err := os.Stat(tmp); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return tmp, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if nconflict++; nconflict > 10 {
|
|
|
|
nconflict = 0
|
2014-10-21 16:11:20 +02:00
|
|
|
rrand = reseed()
|
2014-09-21 17:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("Cannot create temp name")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveAsPNGAtomic(path string, img image.Image) (err error) {
|
|
|
|
|
|
|
|
var tmpPath string
|
|
|
|
if tmpPath, err = tmpName(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Still a bit racy
|
|
|
|
if err = SaveAsPNG(tmpPath, img); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return os.Rename(tmpPath, path)
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-07-20 14:19:41 +02:00
|
|
|
//log.Printf("WARN: %s\n", err)
|
2014-09-16 01:13:12 +02:00
|
|
|
return image.White
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
reader := bufio.NewReader(file)
|
|
|
|
var img image.Image
|
|
|
|
if img, err = png.Decode(reader); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: %s\n", err)
|
2014-09-16 01:13:12 +02:00
|
|
|
return image.White
|
|
|
|
}
|
|
|
|
return img
|
|
|
|
}
|