load fails: wrong rounding and division by zero with fitCenter()
- Dominant language
- Java
- Stars
- 35k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 8
Description
**Glide Version**: 4.8.0
**Issue details / Repro steps / Use case background**:
Very similar to issue #3420
Now `Downsampler.calculateScaling` computes target dimensions, and can get to width or height of zero, and immediately divides by this, resulting in load error.
I suggest that the computations always produce Math.max(1, ...) so that rounding to zero never happens:
```
Downsampler.java:
private static int round(double value) {
return Math.max(1, (int) (value + 0.5d));
}
}
```
**Glide load line / `GlideModule` (if any) / list Adapter code (if any)**:
```java
GlideApp.with(act).load(file).fitCenter().into(v)
```
**Stack trace / LogCat**:
```ruby
java.lang.ArithmeticException: divide by zero
at com.bumptech.glide.load.resource.bitmap.Downsampler.calculateScaling(Downsampler.java:372)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeFromWrappedStreams(Downsampler.java:245)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:206)
at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:62)
at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:18)
at com.bumptech.glide.load.resource.bitmap.BitmapDrawableDecoder.decode(BitmapDrawableDecoder.java:58)
at com.bumptech.glide.load.engine.DecodePath.decodeResourceWithList(DecodePath.java:72)
at com.bumptech.glide.load.engine.DecodePath.decodeResource(DecodePath.java:55)
at com.bumptech.glide.load.engine.DecodePath.decode(DecodePath.java:45)
at com.bumptech.glide.load.engine.LoadPath.loadWithExceptionList(LoadPath.java:58)
at com.bumptech.glide.load.engine.LoadPath.load(LoadPath.java:43)
at com.bumptech.glide.load.engine.DecodeJob.runLoadPath(DecodeJob.java:507)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromFetcher(DecodeJob.java:472)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromData(DecodeJob.java:458)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromRetrievedData(DecodeJob.java:410)
at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherReady(DecodeJob.java:379)
at com.bumptech.glide.load.engine.SourceGenerator.onDataReady(SourceGenerator.java:112)
```
Testing image 20000x1 pixels

Contributor guide
Research direction
Start in Downsampler.java, especially calculateScaling and the round helper, and reproduce the failure with the reported 20000x1 image using fitCenter(). Done means extreme dimensions no longer round to zero or cause division by zero during loading; check the existing Downsampler behavior and tests before validating the fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100