fyp/logic/models/test.go

74 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"
)
func testImgForModel(c *Context, model *BaseModel, path string) (result bool) {
result = false
infile, err := os.Open(path)
if err != nil {
c.Logger.Errorf("Failed to read image for model with id %s\nErr:%s", model.Id, err)
return
}
defer infile.Close()
src, format, err := image.Decode(infile)
if err != nil {
c.Logger.Errorf("Failed to decode image for model with id %s\nErr:%s", model.Id, err)
return
}
var model_color string
bounds := src.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
switch src.ColorModel() {
case color.Gray16Model: fallthrough
2023-10-06 09:45:47 +01:00
case color.GrayModel:
model_color = "greyscale"
case color.YCbCrModel:
model_color = "rgb"
2023-10-06 09:45:47 +01:00
default:
c.Logger.Error("Do not know how to handle this color model")
if src.ColorModel() == color.RGBA64Model {
c.Logger.Info("Color is rgb")
} else if src.ColorModel() == color.NRGBAModel {
c.Logger.Info("Color is nrgb")
} 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")
}
return
}
if (StringToImageMode(model_color) != model.ImageMode) {
c.Logger.Warn("Color Mode does not match with model color mode", model_color, model.ImageMode)
return
}
if height != model.Height || width != model.Width {
c.Logger.Warn("Image size does not match model size", width, height, model.Width, model.Height)
return
}
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
}