Don't panic on invalid image data

- If there is any image data error, just output an error message
- Also print out the filepath, so the user can delete the invalid image

fixes #11
This commit is contained in:
David Vogel 2022-07-16 16:59:32 +02:00
parent 51dabd544a
commit f27a954bd1

View File

@ -1,4 +1,4 @@
// Copyright (c) 2019-2020 David Vogel // Copyright (c) 2019-2022 David Vogel
// //
// This software is released under the MIT License. // This software is released under the MIT License.
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"log"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -67,7 +68,7 @@ func loadImages(path string, scaleDivider int) ([]imageTile, error) {
// Stitch takes a list of tiles and stitches them together. // Stitch takes a list of tiles and stitches them together.
// The destImage shouldn't be too large, or it gets too slow. // The destImage shouldn't be too large, or it gets too slow.
func Stitch(tiles []imageTile, destImage *image.RGBA) error { func Stitch(tiles []imageTile, destImage *image.RGBA) error {
intersectTiles := []*imageTile{} //intersectTiles := []*imageTile{}
images := []*image.RGBA{} images := []*image.RGBA{}
// Get only the tiles that intersect with the destination image bounds. // Get only the tiles that intersect with the destination image bounds.
@ -75,11 +76,12 @@ func Stitch(tiles []imageTile, destImage *image.RGBA) error {
for i, tile := range tiles { for i, tile := range tiles {
if tile.OffsetBounds().Overlaps(destImage.Bounds()) { if tile.OffsetBounds().Overlaps(destImage.Bounds()) {
tilePtr := &tiles[i] tilePtr := &tiles[i]
intersectTiles = append(intersectTiles, tilePtr)
img, err := tilePtr.GetImage() img, err := tilePtr.GetImage()
if err != nil { if err != nil {
return fmt.Errorf("Couldn't get image: %w", err) log.Printf("Couldn't load image tile %s: %v", tile.String(), err)
continue
} }
//intersectTiles = append(intersectTiles, tilePtr)
imgCopy := *img imgCopy := *img
imgCopy.Rect = imgCopy.Rect.Add(tile.offset) imgCopy.Rect = imgCopy.Rect.Add(tile.offset)
images = append(images, &imgCopy) images = append(images, &imgCopy)
@ -217,11 +219,12 @@ func Compare(tiles []imageTile, bounds image.Rectangle) error {
for i, tile := range tiles { for i, tile := range tiles {
if tile.OffsetBounds().Overlaps(bounds) { if tile.OffsetBounds().Overlaps(bounds) {
tilePtr := &tiles[i] tilePtr := &tiles[i]
intersectTiles = append(intersectTiles, tilePtr)
img, err := tilePtr.GetImage() img, err := tilePtr.GetImage()
if err != nil { if err != nil {
return fmt.Errorf("Couldn't get image: %w", err) log.Printf("Couldn't load image tile %s: %v", tile.String(), err)
continue
} }
intersectTiles = append(intersectTiles, tilePtr)
imgCopy := *img imgCopy := *img
imgCopy.Rect = imgCopy.Rect.Add(tile.offset) imgCopy.Rect = imgCopy.Rect.Add(tile.offset)
images = append(images, &imgCopy) images = append(images, &imgCopy)