photoprism/internal/thumb/sizes.go

141 lines
4.2 KiB
Go

package thumb
import "github.com/disintegration/imaging"
var (
SizePrecached = 2048
SizeUncached = 7680
JpegQuality = 95
JpegQualitySmall = 80
Filter = ResampleLanczos
)
func MaxSize() int {
if SizePrecached > SizeUncached {
return SizePrecached
}
return SizeUncached
}
func InvalidSize(size int) bool {
return size < 0 || size > MaxSize()
}
const (
ResampleBlackman ResampleFilter = "blackman"
ResampleLanczos ResampleFilter = "lanczos"
ResampleCubic ResampleFilter = "cubic"
ResampleLinear ResampleFilter = "linear"
)
type ResampleFilter string
func (a ResampleFilter) Imaging() imaging.ResampleFilter {
switch a {
case ResampleBlackman:
return imaging.Blackman
case ResampleLanczos:
return imaging.Lanczos
case ResampleCubic:
return imaging.CatmullRom
case ResampleLinear:
return imaging.Linear
default:
return imaging.Lanczos
}
}
const (
ResampleFillCenter ResampleOption = iota
ResampleFillTopLeft
ResampleFillBottomRight
ResampleFit
ResampleCrop
ResampleResize
ResampleNearestNeighbor
ResampleDefault
ResamplePng
)
type ResampleOption int
var ResampleMethods = map[ResampleOption]string{
ResampleFillCenter: "center",
ResampleFillTopLeft: "left",
ResampleFillBottomRight: "right",
ResampleFit: "fit",
ResampleCrop: "crop",
ResampleResize: "resize",
}
type Size struct {
Use string `json:"use"`
Source string `json:"-"`
Width int `json:"w"`
Height int `json:"h"`
Public bool `json:"-"`
Options []ResampleOption `json:"-"`
}
type SizeMap map[string]Size
var Sizes = SizeMap{
"tile_50": {"Lists", "tile_500", 50, 50, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
"tile_100": {"Maps", "tile_500", 100, 100, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
"crop_160": {"FaceNet", "", 160, 160, false, []ResampleOption{ResampleCrop, ResampleDefault}},
"tile_224": {"TensorFlow, Mosaic", "tile_500", 224, 224, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
"tile_500": {"Tiles", "", 500, 500, false, []ResampleOption{ResampleFillCenter, ResampleDefault}},
"colors": {"Color Detection", "fit_720", 3, 3, false, []ResampleOption{ResampleResize, ResampleNearestNeighbor, ResamplePng}},
"left_224": {"TensorFlow", "fit_720", 224, 224, false, []ResampleOption{ResampleFillTopLeft, ResampleDefault}},
"right_224": {"TensorFlow", "fit_720", 224, 224, false, []ResampleOption{ResampleFillBottomRight, ResampleDefault}},
"fit_720": {"Mobile, TV", "", 720, 720, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_1280": {"Mobile, HD Ready TV", "fit_2048", 1280, 1024, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_1920": {"Mobile, Full HD TV", "fit_2048", 1920, 1200, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_2048": {"Tablets, Cinema 2K", "", 2048, 2048, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_2560": {"Quad HD, Retina Display", "", 2560, 1600, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_3840": {"Ultra HD", "", 3840, 2400, false, []ResampleOption{ResampleFit, ResampleDefault}}, // Deprecated in favor of fit_4096
"fit_4096": {"Ultra HD, Retina 4K", "", 4096, 4096, true, []ResampleOption{ResampleFit, ResampleDefault}},
"fit_7680": {"8K Ultra HD 2, Retina 6K", "", 7680, 4320, true, []ResampleOption{ResampleFit, ResampleDefault}},
}
var DefaultSizes = []string{
"fit_7680",
"fit_4096",
"fit_2560",
"fit_2048",
"fit_1920",
"fit_1280",
"fit_720",
"right_224",
"left_224",
"colors",
"tile_500",
"tile_224",
"tile_100",
"tile_50",
}
// Find returns the largest default thumbnail type for the given size limit.
func Find(limit int) (name string, result Size) {
for _, name = range DefaultSizes {
t := Sizes[name]
if t.Width <= limit && t.Height <= limit {
return name, t
}
}
return "", Size{}
}
// Uncached tests if thumbnail type exceeds the cached thumbnails size limit.
func (s Size) Uncached() bool {
return s.Width > SizePrecached || s.Height > SizePrecached
}
// ExceedsLimit tests if thumbnail type is too large, and can not be rendered at all.
func (s Size) ExceedsLimit() bool {
return s.Width > MaxSize() || s.Height > MaxSize()
}