fyp/logic/models/test.go

77 lines
1.8 KiB
Go
Raw Normal View History

2023-10-06 09:45:47 +01:00
package models
import (
"image"
"image/color"
_ "image/png"
"os"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
2024-04-12 20:36:23 +01:00
func TestImgForModel(c *Context, model *BaseModel, path string) (result bool) {
2023-10-06 09:45:47 +01:00
result = false
infile, err := os.Open(path)
if err != nil {
2023-10-22 23:02:39 +01:00
c.Logger.Errorf("Failed to read image for model with id %s\nErr:%s", model.Id, err)
2023-10-06 09:45:47 +01:00
return
}
defer infile.Close()
src, format, err := image.Decode(infile)
if err != nil {
2023-10-22 23:02:39 +01:00
c.Logger.Errorf("Failed to decode image for model with id %s\nErr:%s", model.Id, err)
2023-10-06 09:45:47 +01:00
return
}
var model_color string
bounds := src.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
switch src.ColorModel() {
2023-10-22 23:02:39 +01:00
case color.Gray16Model:
fallthrough
2023-10-06 09:45:47 +01:00
case color.GrayModel:
model_color = "greyscale"
2023-10-22 23:02:39 +01:00
case color.NRGBAModel:
fallthrough
case color.YCbCrModel:
model_color = "rgb"
2023-10-06 09:45:47 +01:00
default:
2023-10-22 23:02:39 +01:00
c.Logger.Error("Do not know how to handle this color model")
2023-10-06 09:45:47 +01:00
if src.ColorModel() == color.RGBA64Model {
c.Logger.Info("Color is rgb")
2023-10-22 23:02:39 +01:00
} else if src.ColorModel() == color.NRGBA64Model {
c.Logger.Info("Color is nrgb 64")
2023-10-06 09:45:47 +01:00
} else if src.ColorModel() == color.AlphaModel {
c.Logger.Info("Color is alpha")
} else if src.ColorModel() == color.CMYKModel {
c.Logger.Info("Color is cmyk")
} else {
c.Logger.Info("Other so assuming color")
}
2023-10-22 23:02:39 +01:00
return
2023-10-06 09:45:47 +01:00
}
2023-10-22 23:02:39 +01:00
if StringToImageMode(model_color) != model.ImageMode {
c.Logger.Warn("Color Mode does not match with model color mode", model_color, model.ImageMode)
return
}
2023-10-06 09:45:47 +01:00
2023-10-22 23:02:39 +01:00
if height != model.Height || width != model.Width {
c.Logger.Warn("Image size does not match model size", width, height, model.Width, model.Height)
return
}
2023-10-06 09:45:47 +01:00
2023-10-22 23:02:39 +01:00
if format != model.Format {
c.Logger.Warn("Image format does not match model", format, model.Format)
return
}
2023-10-06 09:45:47 +01:00
return true
}