2020-05-10 07:11:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/draw"
|
|
|
|
|
|
|
|
"github.com/nfnt/resize"
|
2020-05-10 15:40:56 +00:00
|
|
|
"opslag.de/schobers/tins2020/img"
|
2020-05-10 07:11:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func crop(path string, crop rect, size *point) error {
|
2020-05-10 15:40:56 +00:00
|
|
|
src, err := img.DecodeImage(path)
|
2020-05-10 07:11:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcRect := image.Rect(crop.min.x, crop.min.y, crop.max.x, crop.max.y)
|
|
|
|
dstRect := image.Rect(0, 0, crop.max.x-crop.min.x, crop.max.y-crop.min.y)
|
|
|
|
dst := image.NewRGBA(dstRect)
|
|
|
|
draw.Draw(dst, dstRect, src, srcRect.Min, draw.Src)
|
|
|
|
if size == nil {
|
2020-05-10 15:40:56 +00:00
|
|
|
return img.EncodePNG(path, dst)
|
2020-05-10 07:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resized := resize.Resize(uint(size.x), uint(size.y), dst, resize.Bilinear)
|
2020-05-10 15:40:56 +00:00
|
|
|
return img.EncodePNG(path, resized)
|
2020-05-10 07:11:08 +00:00
|
|
|
}
|