Added framework for image decoding

This commit is contained in:
Samuel Hawksby-Robinson 2020-09-30 23:33:47 +01:00 committed by Andrea Maria Piana
parent 17f0bac6ae
commit f85553b6ca
3 changed files with 46 additions and 6 deletions

View File

@ -1,3 +1,4 @@
package images
// TODO find the best location to put the new files
const ImageDir = "assets/"

View File

@ -3,6 +3,7 @@ package images
import (
"image"
"image/jpeg"
"io"
"os"
)
@ -11,12 +12,24 @@ func Get(fileName string) (image.Image, error) {
if err != nil {
return nil, err
}
defer file.Close()
switch getFileType(file) {
case JPEG:
err = decodeJpeg(file)
break
case PNG:
err = decodePng(file)
break
case WEBP:
err = decodeWebp(file)
break
}
img, err := jpeg.Decode(file)
if err != nil {
return nil, err
}
file.Close()
return img, nil
}
@ -28,13 +41,40 @@ func Render(img image.Image, imgDetail *Details) error {
}
defer out.Close()
o := new(jpeg.Options)
o.Quality = imgDetail.Quality
jpeg.Encode(out, img, o)
err = renderJpeg(out, img, imgDetail)
if err != nil {
return err
}
fi, _ := out.Stat()
imgDetail.SizeFile = fi.Size()
return nil
}
func getFileType(file *os.File) FileType {
// TODO
return JPEG
}
func decodeJpeg(file *os.File) error {
// TODO
return nil
}
func decodePng(file *os.File) error {
// TODO
return nil
}
func decodeWebp(file *os.File) error {
// TODO
return nil
}
func renderJpeg(w io.Writer, m image.Image, imgDetail *Details) error {
o := new(jpeg.Options)
o.Quality = imgDetail.Quality
return jpeg.Encode(w, m, o)
}

View File

@ -22,7 +22,6 @@ type Details struct {
SizeFile int64
Quality int
FileName string
FileType FileType
Properties string
}