Skip to content

Commit

Permalink
Merge pull request #15 from rigon/orientation
Browse files Browse the repository at this point in the history
FIX: show correct image orientation
  • Loading branch information
rigon committed Apr 19, 2023
2 parents db3664a + e136be6 commit 7ffbb2f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 25 deletions.
7 changes: 5 additions & 2 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const DB_NAME_SUFFIX = "-cache.db"

var dbInfo = DbInfo{
Version: 3,
Version: 4,
}

type DbInfo struct {
Expand Down Expand Up @@ -126,7 +126,10 @@ func (c Cache) FillPhotosInfo(album *Album) (err error) {
var data Photo
key := album.Name + ":" + photo.Title
err := c.store.TxGet(tx, key, &data)
if err == nil {
// Does not require update
if err == nil && data.Title == photo.Title && data.Thumb == photo.Thumb &&
len(data.Files) == len(photo.Files) { // Validate some fields

*photo = data
continue
}
Expand Down
48 changes: 33 additions & 15 deletions server/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import (

"github.com/dustin/go-humanize"
"github.com/mholt/goexif2/exif"
"github.com/mholt/goexif2/tiff"
)

type File struct {
Type string `json:"type"`
MIME string `json:"mime"`
Url string `json:"url"`
Path string `json:"-"`
Ext string `json:"-"`
Width int `json:"width"` // Image Width
Height int `json:"height"` // Image Height
Date time.Time `json:"date"` // Image Date taken
Location GPSLocation `json:"location"` // Image location
Type string `json:"type"`
MIME string `json:"mime"`
Url string `json:"url"`
Path string `json:"-"`
Ext string `json:"-"`
Width int `json:"width"` // Image Width
Height int `json:"height"` // Image Height
Date time.Time `json:"date"` // Image Date taken
Location GPSLocation `json:"location"` // Image location
Orientation Orientation `json:"-"` // Image orientation
}

type FileExtendedInfo struct {
Expand Down Expand Up @@ -101,15 +103,31 @@ func (file *File) ExtractInfo() error {

switch file.Type {
case "image":
_, cfg, exif, err := ExtractImageConfigOpened(f)
_, cfg, exifInfo, err := ExtractImageConfigOpened(f)
file.Width = cfg.Width
file.Height = cfg.Height
if err == nil {
// If date is available from EXIF
file.Date, _ = exif.DateTime()
file.Date, _ = exifInfo.DateTime()
// If GPS Location is available from EXIF
file.Location.Lat, file.Location.Long, err = exif.LatLong()
file.Location.Present = (err == nil) // Present if no errors
file.Location.Lat, file.Location.Long, err = exifInfo.LatLong()
file.Location.Present = (err == nil) // Location present if no errors
// Image orientation
orient, err := exifInfo.Get(exif.Orientation)
if err == nil && orient.Count == 1 && orient.Format() == tiff.IntVal {
orientInt, _ := orient.Int(0)
file.Orientation = Orientation(orientInt)
// Dimensions are swapped due to orientation
switch file.Orientation {
case orientationRotate90,
orientationRotate270,
orientationTranspose,
orientationTransverse:
file.Width, file.Height = file.Height, file.Width
}
} else {
file.Orientation = orientationUnspecified // tag not present
}
} else {
// File Modification Date otherwise
fileInfo, err := os.Stat(file.Path)
Expand Down Expand Up @@ -186,7 +204,7 @@ func (file *File) Convert(w *bufio.Writer) error {
}

// Decode original image
img, err := DecodeImage(file.Path)
img, err := DecodeImage(file.Path, file.Orientation)
if err != nil {
return err
}
Expand All @@ -208,7 +226,7 @@ func (file File) CreateThumbnail(thumbpath string, w io.Writer) (err error) {
switch file.Type {
case "image":
// Decode original image
img, err = DecodeImage(file.Path)
img, err = DecodeImage(file.Path, file.Orientation)
case "video":
// Get a frame from the video
img, err = GetVideoFrame(file.Path)
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ require (
github.com/3d0c/gmf v0.0.0-20220906170454-be727bc5b56c
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786
github.com/bluele/gcache v0.0.2
github.com/disintegration/imaging v1.6.2
github.com/dustin/go-humanize v1.0.1
github.com/gofiber/adaptor/v2 v2.2.0
github.com/gofiber/fiber/v2 v2.43.0
github.com/mholt/goexif2 v0.0.0-20230302025153-4d89d35092b2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/pflag v1.0.5
github.com/timshannon/bolthold v0.0.0-20210913165410-232392fc8a6a
Expand Down
5 changes: 3 additions & 2 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/bluele/gcache v0.0.2 h1:WcbfdXICg7G/DGBh1PFfcirkWOQV+v077yF1pSy3DGw=
github.com/bluele/gcache v0.0.2/go.mod h1:m15KV+ECjptwSPxKhOhQoAFQVtUFjTVkc3H8o0t/fp0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
Expand All @@ -29,8 +31,6 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mholt/goexif2 v0.0.0-20230302025153-4d89d35092b2 h1:X3++r/H/MWe/xsHuk+UsoDu76bQNeZGcL2olWd/6rEU=
github.com/mholt/goexif2 v0.0.0-20230302025153-4d89d35092b2/go.mod h1:YWzGbKDCg7bkYyCs7ekYbG5gk/nQGkiZl0eU3aCMpt8=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
Expand Down Expand Up @@ -77,6 +77,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
45 changes: 41 additions & 4 deletions server/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"gitlab.com/golang-utils/image2/jpeg"

_ "github.com/adrium/goheif"
"github.com/disintegration/imaging"
"github.com/mholt/goexif2/exif"
"github.com/nfnt/resize"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/vp8"
Expand All @@ -30,6 +30,23 @@ type writerSkipper struct {
bytesToSkip int
}

// From: https://github.com/disintegration/imaging/blob/d40f48ce0f098c53ab1fcd6e0e402da682262da5/io.go#L285
// orientation is an EXIF flag that specifies the transformation
// that should be applied to image to display it correctly.
type Orientation int

const (
orientationUnspecified = 0
orientationNormal = 1
orientationFlipH = 2
orientationRotate180 = 3
orientationFlipV = 4
orientationTranspose = 5
orientationRotate270 = 6
orientationTransverse = 7
orientationRotate90 = 8
)

func (w *writerSkipper) Write(data []byte) (int, error) {
if w.bytesToSkip <= 0 {
return w.w.Write(data)
Expand Down Expand Up @@ -81,7 +98,7 @@ func EncodeImage(w io.Writer, image image.Image, exifData []byte) error {
return jpeg.Encode(writer, image, nil)
}

func DecodeImage(filepath string) (image.Image, error) {
func DecodeImage(filepath string, orientation Orientation) (image.Image, error) {
// Open input file image
fin, err := os.Open(filepath)
if err != nil {
Expand All @@ -93,6 +110,26 @@ func DecodeImage(filepath string) (image.Image, error) {
if err != nil {
return nil, fmt.Errorf("error decoding image type %s: %v", format, err)
}

// From: https://github.com/disintegration/imaging/blob/d40f48ce0f098c53ab1fcd6e0e402da682262da5/io.go#L424
switch orientation {
case orientationNormal:
case orientationFlipH:
img = imaging.FlipH(img)
case orientationFlipV:
img = imaging.FlipV(img)
case orientationRotate90:
img = imaging.Rotate90(img)
case orientationRotate180:
img = imaging.Rotate180(img)
case orientationRotate270:
img = imaging.Rotate270(img)
case orientationTranspose:
img = imaging.Transpose(img)
case orientationTransverse:
img = imaging.Transverse(img)
}

return img, nil
}

Expand Down Expand Up @@ -142,14 +179,14 @@ func CreateThumbnailFromImage(img image.Image, thumbpath string, w io.Writer) er
defer fout.Close()

// Resize image for thumbnail size
resized := resize.Resize(0, 200, img, resize.Lanczos3)
img = imaging.Resize(img, 0, 200, imaging.Lanczos)

// Encode thumbnail
var mw io.Writer = fout
if w != nil {
mw = io.MultiWriter(w, fout)
}
err = EncodeImage(mw, resized, nil)
err = EncodeImage(mw, img, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestDecodeEncodeImage(t *testing.T) {
img, err := DecodeImage("tests/album1/image3.heic")
img, err := DecodeImage("tests/album1/image3.heic", orientationUnspecified)
if err != nil {
t.Errorf("Failed decode image: %s\n", err)
return
Expand Down

0 comments on commit 7ffbb2f

Please sign in to comment.