2021-12-09 07:00:39 +01:00
|
|
|
package thumb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2022-07-06 23:01:54 +02:00
|
|
|
|
2021-12-09 07:00:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2022-04-01 21:14:22 +02:00
|
|
|
// StandardRGB configures whether colors in the Apple Display P3 color space should be converted to standard RGB.
|
|
|
|
var StandardRGB = true
|
|
|
|
|
2021-12-09 07:00:39 +01:00
|
|
|
// Open loads an image from disk, rotates it, and converts the color profile if necessary.
|
|
|
|
func Open(fileName string, orientation int) (result image.Image, err error) {
|
2022-07-06 23:01:54 +02:00
|
|
|
// Filename missing?
|
2021-12-09 07:00:39 +01:00
|
|
|
if fileName == "" {
|
|
|
|
return result, fmt.Errorf("filename missing")
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Resolve symlinks.
|
|
|
|
if fileName, err = fs.Resolve(fileName); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-12-09 07:00:39 +01:00
|
|
|
// Open JPEG?
|
2022-04-15 09:42:07 +02:00
|
|
|
if StandardRGB && fs.FileType(fileName) == fs.ImageJPEG {
|
2021-12-09 07:00:39 +01:00
|
|
|
return OpenJpeg(fileName, orientation)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open file with imaging function.
|
|
|
|
img, err := imaging.Open(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:01:54 +02:00
|
|
|
// Adjust orientation.
|
2021-12-09 07:00:39 +01:00
|
|
|
if orientation > 1 {
|
|
|
|
img = Rotate(img, orientation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|