mediaapi/thumbnailer: Make comparison code more readable

This commit is contained in:
Robert Swain 2017-06-05 16:23:07 +02:00
parent a1e62a2b0b
commit 2825b4143b

View file

@ -104,9 +104,7 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM
continue continue
} }
metrics := calcThumbnailMetrics(thumbnail.ThumbnailSize, thumbnail.MediaMetadata, desired) metrics := calcThumbnailMetrics(thumbnail.ThumbnailSize, thumbnail.MediaMetadata, desired)
// Note: the result will be -1 for better than, 0 for the same as and 1 for worse than. Take better results only. if isBetter := metrics.betterThan(chosenMetrics, desired.ResizeMethod == "crop"); isBetter {
result := compareThumbnailMetrics(metrics, chosenMetrics, desired.ResizeMethod == "crop")
if result == -1 {
chosenMetrics = metrics chosenMetrics = metrics
chosenThumbnail = thumbnail chosenThumbnail = thumbnail
} }
@ -117,9 +115,7 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM
continue continue
} }
metrics := calcThumbnailMetrics(thumbnailSize, nil, desired) metrics := calcThumbnailMetrics(thumbnailSize, nil, desired)
// Note: the result will be -1 for better than, 0 for the same as and 1 for worse than. Take better results only. if isBetter := metrics.betterThan(chosenMetrics, desired.ResizeMethod == "crop"); isBetter {
result := compareThumbnailMetrics(metrics, chosenMetrics, desired.ResizeMethod == "crop")
if result == -1 {
chosenMetrics = metrics chosenMetrics = metrics
chosenThumbnailSize = &types.ThumbnailSize{ chosenThumbnailSize = &types.ThumbnailSize{
Width: thumbnailSize.Width, Width: thumbnailSize.Width,
@ -368,15 +364,15 @@ func boolToInt(b bool) int {
return 0 return 0
} }
func compareThumbnailMetrics(a thumbnailMetrics, b thumbnailMetrics, desiredCrop bool) int { func (a thumbnailMetrics) betterThan(b thumbnailMetrics, desiredCrop bool) bool {
// preference means returning -1 // preference means returning -1
// prefer images that are not smaller // prefer images that are not smaller
// e.g. isSmallerDiff > 0 means b is smaller than desired and a is not smaller // e.g. isSmallerDiff > 0 means b is smaller than desired and a is not smaller
if a.isSmaller > b.isSmaller { if a.isSmaller > b.isSmaller {
return 1 return false
} else if a.isSmaller < b.isSmaller { } else if a.isSmaller < b.isSmaller {
return -1 return true
} }
// prefer aspect ratios closer to desired only if desired cropped // prefer aspect ratios closer to desired only if desired cropped
@ -384,33 +380,33 @@ func compareThumbnailMetrics(a thumbnailMetrics, b thumbnailMetrics, desiredCrop
// desired scaled only accepts scaled images // desired scaled only accepts scaled images
if desiredCrop { if desiredCrop {
if a.aspect > b.aspect { if a.aspect > b.aspect {
return 1 return false
} else if a.aspect < b.aspect { } else if a.aspect < b.aspect {
return -1 return true
} }
} }
// prefer closer in size // prefer closer in size
if a.size > b.size { if a.size > b.size {
return 1 return false
} else if a.size < b.size { } else if a.size < b.size {
return -1 return true
} }
// prefer images using the same method // prefer images using the same method
// e.g. methodMismatchDiff > 0 means b's method is different from desired and a's matches the desired method // e.g. methodMismatchDiff > 0 means b's method is different from desired and a's matches the desired method
if a.methodMismatch > b.methodMismatch { if a.methodMismatch > b.methodMismatch {
return 1 return false
} else if a.methodMismatch < b.methodMismatch { } else if a.methodMismatch < b.methodMismatch {
return -1 return true
} }
// prefer smaller files // prefer smaller files
if a.fileSize > b.fileSize { if a.fileSize > b.fileSize {
return 1 return false
} else if a.fileSize < b.fileSize { } else if a.fileSize < b.fileSize {
return -1 return true
} }
return 0 return false
} }