77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
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
|
|
case color.GrayModel:
|
|
model_color = "greyscale"
|
|
case color.NRGBAModel:
|
|
fallthrough
|
|
case color.YCbCrModel:
|
|
model_color = "rgb"
|
|
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.NRGBA64Model {
|
|
c.Logger.Info("Color is nrgb 64")
|
|
} 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
|
|
}
|
|
|
|
return true
|
|
}
|