2022-04-01 21:14:22 +02:00
|
|
|
package thumb
|
|
|
|
|
|
|
|
import (
|
2023-06-29 10:34:51 +02:00
|
|
|
"bytes"
|
2022-04-01 21:14:22 +02:00
|
|
|
"fmt"
|
|
|
|
"image"
|
2023-06-29 10:34:51 +02:00
|
|
|
"io"
|
2022-04-01 21:14:22 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-06-29 10:34:51 +02:00
|
|
|
"syscall"
|
2022-04-01 21:14:22 +02:00
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2023-06-29 10:34:51 +02:00
|
|
|
"github.com/mandykoh/prism/meta"
|
2022-04-01 21:14:22 +02:00
|
|
|
"github.com/mandykoh/prism/meta/autometa"
|
2023-06-29 10:34:51 +02:00
|
|
|
"golang.org/x/sys/unix"
|
2022-04-01 21:14:22 +02:00
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-04-01 21:14:22 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/colors"
|
|
|
|
)
|
|
|
|
|
2023-06-29 10:34:51 +02:00
|
|
|
var (
|
|
|
|
EOI = []byte{0xff, 0xd9}
|
|
|
|
)
|
|
|
|
|
|
|
|
func decode(reader io.Reader, logName string) (md *meta.Data, img image.Image, err error) {
|
|
|
|
// Read color metadata.
|
|
|
|
md, imgStream, err := autometa.Load(reader)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("thumb: %s in %s (read color metadata)", err, logName)
|
|
|
|
img, err = imaging.Decode(reader)
|
|
|
|
} else {
|
|
|
|
img, err = imaging.Decode(imgStream)
|
|
|
|
}
|
|
|
|
return md, img, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func attemptRepair(fileReader *os.File) (io.Reader, error) {
|
|
|
|
fi, err := fileReader.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s trying to stat() file", err)
|
|
|
|
}
|
|
|
|
size := int(fi.Size())
|
|
|
|
b, err := unix.Mmap(int(fileReader.Fd()), 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_PRIVATE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s while mmap()ing file", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for missing EOI.
|
|
|
|
if !bytes.Equal(b[size-len(EOI):size], EOI) {
|
|
|
|
b = append(b, EOI...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.NewReader(b), nil
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:14:22 +02:00
|
|
|
// OpenJpeg loads a JPEG image from disk, rotates it, and converts the color profile if necessary.
|
2023-06-29 10:34:51 +02:00
|
|
|
func OpenJpeg(fileName string, orientation int) (image.Image, error) {
|
2022-04-01 21:14:22 +02:00
|
|
|
if fileName == "" {
|
2023-06-29 10:34:51 +02:00
|
|
|
return nil, fmt.Errorf("filename missing")
|
2022-04-01 21:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
logName := clean.Log(filepath.Base(fileName))
|
2022-04-01 21:14:22 +02:00
|
|
|
|
|
|
|
// Open file.
|
|
|
|
fileReader, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
2023-06-29 10:34:51 +02:00
|
|
|
return nil, err
|
2022-04-01 21:14:22 +02:00
|
|
|
}
|
|
|
|
defer fileReader.Close()
|
|
|
|
|
2022-06-25 02:16:42 +02:00
|
|
|
// Reset file offset.
|
|
|
|
// see https://github.com/golang/go/issues/45902#issuecomment-1007953723
|
2023-06-29 10:34:51 +02:00
|
|
|
if _, err := fileReader.Seek(0, 0); err != nil {
|
|
|
|
return nil, fmt.Errorf("%s on seek", err)
|
2022-06-25 02:16:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 10:34:51 +02:00
|
|
|
md, img, err := decode(fileReader, logName)
|
2022-04-01 21:14:22 +02:00
|
|
|
if err != nil {
|
2023-06-29 10:34:51 +02:00
|
|
|
log.Warnf("%s during initial decoding attempt", err)
|
|
|
|
repaired, err := attemptRepair(fileReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s while trying to recover image", err)
|
|
|
|
}
|
|
|
|
if md, img, err = decode(repaired, logName); err != nil {
|
|
|
|
return nil, fmt.Errorf("%s while decoding after recovery attempt", err)
|
|
|
|
}
|
2022-04-01 21:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read ICC profile and convert colors if possible.
|
|
|
|
if md != nil {
|
|
|
|
if iccProfile, err := md.ICCProfile(); err != nil || iccProfile == nil {
|
|
|
|
// Do nothing.
|
2022-07-06 23:01:54 +02:00
|
|
|
log.Tracef("thumb: %s has no color profile", logName)
|
2022-04-01 21:14:22 +02:00
|
|
|
} else if profile, err := iccProfile.Description(); err == nil && profile != "" {
|
2022-07-06 23:01:54 +02:00
|
|
|
log.Tracef("thumb: %s has color profile %s", logName, clean.Log(profile))
|
2022-04-01 21:14:22 +02:00
|
|
|
switch {
|
|
|
|
case colors.ProfileDisplayP3.Equal(profile):
|
|
|
|
img = colors.ToSRGB(img, colors.ProfileDisplayP3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Adjust orientation.
|
2022-04-01 21:14:22 +02:00
|
|
|
if orientation > 1 {
|
|
|
|
img = Rotate(img, orientation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|