2020-05-14 14:35:08 +02:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/graphql/models"
|
2021-02-16 11:27:28 +01:00
|
|
|
"github.com/photoview/photoview/api/scanner/image_helpers"
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/utils"
|
2021-02-16 11:27:28 +01:00
|
|
|
"github.com/pkg/errors"
|
2020-07-11 13:13:31 +02:00
|
|
|
"gopkg.in/vansante/go-ffprobe.v2"
|
2021-03-27 21:14:42 +01:00
|
|
|
|
|
|
|
_ "github.com/strukturag/libheif/go/heif"
|
2020-05-14 14:35:08 +02:00
|
|
|
)
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
func EncodeThumbnail(inputPath string, outputPath string) (*image_helpers.PhotoDimensions, error) {
|
2020-08-15 12:24:54 +02:00
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
inputImage, err := imaging.Open(inputPath, imaging.AutoOrientation(true))
|
2020-08-15 12:24:54 +02:00
|
|
|
if err != nil {
|
2021-03-27 21:14:42 +01:00
|
|
|
return nil, err
|
2020-08-15 12:24:54 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
dimensions := image_helpers.PhotoDimensionsFromRect(inputImage.Bounds())
|
|
|
|
dimensions = dimensions.ThumbnailScale()
|
2020-08-15 12:24:54 +02:00
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
thumbImage := imaging.Resize(inputImage, dimensions.Width, dimensions.Height, imaging.NearestNeighbor)
|
|
|
|
if err = encodeImageJPEG(thumbImage, outputPath, 60); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dimensions, nil
|
2020-05-14 15:17:23 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
func encodeImageJPEG(image image.Image, outputPath string, jpegQuality int) error {
|
2020-05-15 15:23:21 +02:00
|
|
|
photo_file, err := os.Create(outputPath)
|
2020-05-14 14:35:08 +02:00
|
|
|
if err != nil {
|
2020-05-15 15:23:21 +02:00
|
|
|
return errors.Wrapf(err, "could not create file: %s", outputPath)
|
2020-05-14 14:35:08 +02:00
|
|
|
}
|
|
|
|
defer photo_file.Close()
|
|
|
|
|
2020-05-14 15:17:23 +02:00
|
|
|
err = jpeg.Encode(photo_file, image, &jpeg.Options{Quality: jpegQuality})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-14 14:35:08 +02:00
|
|
|
|
2020-05-14 15:17:23 +02:00
|
|
|
return nil
|
2020-05-14 14:35:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
// EncodeMediaData is used to easily decode media data, with a cache so expensive operations are not repeated
|
|
|
|
type EncodeMediaData struct {
|
|
|
|
media *models.Media
|
|
|
|
_photoImage image.Image
|
|
|
|
_contentType *MediaType
|
|
|
|
_videoMetadata *ffprobe.ProbeData
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:35:08 +02:00
|
|
|
// ContentType reads the image to determine its content type
|
2020-07-11 13:13:31 +02:00
|
|
|
func (img *EncodeMediaData) ContentType() (*MediaType, error) {
|
2020-05-14 14:35:08 +02:00
|
|
|
if img._contentType != nil {
|
|
|
|
return img._contentType, nil
|
|
|
|
}
|
|
|
|
|
2020-07-11 14:05:06 +02:00
|
|
|
imgType, err := getMediaType(img.media.Path)
|
2020-05-14 14:35:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:17:23 +02:00
|
|
|
img._contentType = imgType
|
|
|
|
return imgType, nil
|
2020-05-14 14:35:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
func (img *EncodeMediaData) EncodeHighRes(outputPath string) error {
|
2020-05-15 16:36:02 +02:00
|
|
|
contentType, err := img.ContentType()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !contentType.isSupported() {
|
|
|
|
return errors.New("could not convert photo as file format is not supported")
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:40:37 +01:00
|
|
|
// Use darktable if there is no counterpart JPEG file to use instead
|
|
|
|
if contentType.isRaw() && img.media.CounterpartPath == nil {
|
2020-05-17 21:49:29 +02:00
|
|
|
if DarktableCli.IsInstalled() {
|
2020-07-10 14:26:19 +02:00
|
|
|
err := DarktableCli.EncodeJpeg(img.media.Path, outputPath, 70)
|
2020-05-15 16:36:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("could not convert photo as no RAW converter was found")
|
|
|
|
}
|
2020-05-17 16:08:58 +02:00
|
|
|
} else {
|
2021-03-27 21:14:42 +01:00
|
|
|
image, err := img.photoImage()
|
2020-05-17 16:08:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-15 16:36:02 +02:00
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
encodeImageJPEG(image, outputPath, 70)
|
2020-05-15 16:36:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
// photoImage reads and decodes the image file and saves it in a cache so the photo in only decoded once
|
|
|
|
func (img *EncodeMediaData) photoImage() (image.Image, error) {
|
2020-05-14 14:35:08 +02:00
|
|
|
if img._photoImage != nil {
|
|
|
|
return img._photoImage, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:40:37 +01:00
|
|
|
var photoPath string
|
|
|
|
if img.media.CounterpartPath != nil {
|
|
|
|
photoPath = *img.media.CounterpartPath
|
|
|
|
} else {
|
|
|
|
photoPath = img.media.Path
|
2020-05-14 14:35:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 21:14:42 +01:00
|
|
|
photoImg, err := img.decodeImage(photoPath)
|
2020-05-14 14:35:08 +02:00
|
|
|
if err != nil {
|
2020-05-15 16:36:02 +02:00
|
|
|
return nil, utils.HandleError("image decoding", err)
|
2020-05-14 14:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
img._photoImage = photoImg
|
|
|
|
return img._photoImage, nil
|
|
|
|
}
|
2021-03-27 21:14:42 +01:00
|
|
|
|
|
|
|
func (img *EncodeMediaData) decodeImage(imagePath string) (image.Image, error) {
|
|
|
|
file, err := os.Open(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to open file to decode image (%s)", imagePath)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
mediaType, err := img.ContentType()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to get media content type needed to decode it (%s)", imagePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
var decodedImage image.Image
|
|
|
|
|
|
|
|
if *mediaType == TypeHeic {
|
|
|
|
decodedImage, _, err = image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to decode HEIF image (%s)", imagePath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
decodedImage, err = imaging.Decode(file, imaging.AutoOrientation(true))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to decode image (%s)", imagePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodedImage, nil
|
|
|
|
}
|