Skip to content

Commit ec9a135

Browse files
authored
Merge pull request #58 from pushd/fix_dither_fill
[FRAM-555] Mimic Android CenterCrop behavior when dithering with ResizeFill
2 parents c8f22d7 + 414fe23 commit ec9a135

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

processing/dither.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,30 @@ func dither(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOption
1919
return nil
2020
}
2121

22-
// Resize image to desired dimensions, retaining aspect, before dithering
23-
// Usually smaller images are returned to the frame for upscaling, but in the dithering case we want to
24-
// dither the image only after it's been resized to be bounded within the [po.Width X po.Height] box
25-
// we trust that anything rendering to the frame itself will have the proper aspect defined
26-
22+
// If the frame scales the image itself after dithering the artifacts will look terrible.
23+
// For ResizeFit we may add black bars around the dithered image further down
24+
// For ResizeFill we Mimic the CENTER_CROP resizing behavior prior to dithering
25+
// https://developer.android.com/reference/android/widget/ImageView.ScaleType#CENTER_CROP
2726
widthScale := float64(po.Width) / float64(img.Width())
2827
heightScale := float64(po.Height) / float64(img.Height())
29-
minScale := math.Min(widthScale, heightScale)
30-
if err := img.Resize(minScale, minScale); err != nil {
28+
scaleSize := math.Min(widthScale, heightScale) // ResizeFit
29+
30+
if po.ResizingType == options.ResizeFill {
31+
scaleSize = math.Max(widthScale, heightScale)
32+
}
33+
34+
// in either case we want to scale for dithering
35+
if err := img.Resize(scaleSize, scaleSize); err != nil {
3136
return err
3237
}
3338

39+
if po.ResizingType == options.ResizeFill {
40+
gravity := options.GravityOptions{Type: options.GravityCenter}
41+
if err := cropImage(img, po.Width, po.Height, &gravity, 1.0); err != nil {
42+
return err
43+
}
44+
}
45+
3446
// Get a snapshot of current image
3547
if err := img.CopyMemory(); err != nil {
3648
return err

0 commit comments

Comments
 (0)