2020-05-01 11:53:41 +02:00
|
|
|
package thumb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-21 11:39:49 +01:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-01 11:53:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJpeg(t *testing.T) {
|
|
|
|
formats := []string{"bmp", "gif", "png", "tif"}
|
|
|
|
|
|
|
|
for _, ext := range formats {
|
|
|
|
t.Run(ext, func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2020-05-01 11:53:41 +02:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
2021-02-22 21:09:50 +01:00
|
|
|
img, err := Jpeg(src, dst, OrientationRotate270)
|
2020-05-01 11:53:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
2021-02-22 21:09:50 +01:00
|
|
|
assert.Equal(t, 67, bounds.Max.X)
|
|
|
|
assert.Equal(t, 100, bounds.Max.Y)
|
2020-05-01 11:53:41 +02:00
|
|
|
|
2021-03-03 14:44:30 +01:00
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationFlipH", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationFlipH)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationFlipV", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationFlipV)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationRotate90", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationRotate90)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 67, bounds.Max.X)
|
|
|
|
assert.Equal(t, 100, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationRotate180", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationRotate180)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationTranspose", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationTranspose)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 67, bounds.Max.X)
|
|
|
|
assert.Equal(t, 100, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationTransverse", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationTransverse)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 67, bounds.Max.X)
|
|
|
|
assert.Equal(t, 100, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationUnspecified", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationUnspecified)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("OrientationNormal", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, OrientationNormal)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid orientation", func(t *testing.T) {
|
|
|
|
src := "testdata/example." + ext
|
2022-04-15 09:42:07 +02:00
|
|
|
dst := "testdata/example." + ext + fs.ExtJPEG
|
2021-03-03 14:44:30 +01:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
img, err := Jpeg(src, dst, 500)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.FileExists(t, dst)
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
assert.Equal(t, 100, bounds.Max.X)
|
|
|
|
assert.Equal(t, 67, bounds.Max.Y)
|
|
|
|
|
2020-05-01 11:53:41 +02:00
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("foo", func(t *testing.T) {
|
|
|
|
src := "testdata/example.foo"
|
|
|
|
dst := "testdata/example.foo.jpg"
|
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
2021-02-22 21:09:50 +01:00
|
|
|
img, err := Jpeg(src, dst, OrientationFlipV)
|
2020-05-01 11:53:41 +02:00
|
|
|
|
|
|
|
assert.NoFileExists(t, dst)
|
|
|
|
|
|
|
|
if img != nil {
|
|
|
|
t.Fatal("img should be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|