@@ -384,19 +384,21 @@ private static void calculateScaling(
384
384
return ;
385
385
}
386
386
387
- final float exactScaleFactor ;
387
+ int orientedSourceWidth = sourceWidth ;
388
+ int orientedSourceHeight = sourceHeight ;
389
+ // If we're rotating the image +-90 degrees, we need to downsample accordingly so the image
390
+ // width is decreased to near our target's height and the image height is decreased to near
391
+ // our target width.
392
+ //noinspection SuspiciousNameCombination
388
393
if (degreesToRotate == 90 || degreesToRotate == 270 ) {
389
- // If we're rotating the image +-90 degrees, we need to downsample accordingly so the image
390
- // width is decreased to near our target's height and the image height is decreased to near
391
- // our target width.
392
- //noinspection SuspiciousNameCombination
393
- exactScaleFactor =
394
- downsampleStrategy .getScaleFactor (sourceHeight , sourceWidth , targetWidth , targetHeight );
395
- } else {
396
- exactScaleFactor =
397
- downsampleStrategy .getScaleFactor (sourceWidth , sourceHeight , targetWidth , targetHeight );
394
+ orientedSourceWidth = sourceHeight ;
395
+ orientedSourceHeight = sourceWidth ;
398
396
}
399
397
398
+ final float exactScaleFactor =
399
+ downsampleStrategy .getScaleFactor (
400
+ orientedSourceWidth , orientedSourceHeight , targetWidth , targetHeight );
401
+
400
402
if (exactScaleFactor <= 0f ) {
401
403
throw new IllegalArgumentException (
402
404
"Cannot scale with factor: "
@@ -414,18 +416,19 @@ private static void calculateScaling(
414
416
+ targetHeight
415
417
+ "]" );
416
418
}
419
+
417
420
SampleSizeRounding rounding =
418
421
downsampleStrategy .getSampleSizeRounding (
419
- sourceWidth , sourceHeight , targetWidth , targetHeight );
422
+ orientedSourceWidth , orientedSourceHeight , targetWidth , targetHeight );
420
423
if (rounding == null ) {
421
424
throw new IllegalArgumentException ("Cannot round with null rounding" );
422
425
}
423
426
424
- int outWidth = round (exactScaleFactor * sourceWidth );
425
- int outHeight = round (exactScaleFactor * sourceHeight );
427
+ int outWidth = round (exactScaleFactor * orientedSourceWidth );
428
+ int outHeight = round (exactScaleFactor * orientedSourceHeight );
426
429
427
- int widthScaleFactor = sourceWidth / outWidth ;
428
- int heightScaleFactor = sourceHeight / outHeight ;
430
+ int widthScaleFactor = orientedSourceWidth / outWidth ;
431
+ int heightScaleFactor = orientedSourceHeight / outHeight ;
429
432
430
433
int scaleFactor =
431
434
rounding == SampleSizeRounding .MEMORY
@@ -458,26 +461,26 @@ private static void calculateScaling(
458
461
// After libjpegturbo's native rounding, skia does a secondary scale using floor
459
462
// (integer division). Here we replicate that logic.
460
463
int nativeScaling = Math .min (powerOfTwoSampleSize , 8 );
461
- powerOfTwoWidth = (int ) Math .ceil (sourceWidth / (float ) nativeScaling );
462
- powerOfTwoHeight = (int ) Math .ceil (sourceHeight / (float ) nativeScaling );
464
+ powerOfTwoWidth = (int ) Math .ceil (orientedSourceWidth / (float ) nativeScaling );
465
+ powerOfTwoHeight = (int ) Math .ceil (orientedSourceHeight / (float ) nativeScaling );
463
466
int secondaryScaling = powerOfTwoSampleSize / 8 ;
464
467
if (secondaryScaling > 0 ) {
465
468
powerOfTwoWidth = powerOfTwoWidth / secondaryScaling ;
466
469
powerOfTwoHeight = powerOfTwoHeight / secondaryScaling ;
467
470
}
468
471
} else if (imageType == ImageType .PNG || imageType == ImageType .PNG_A ) {
469
- powerOfTwoWidth = (int ) Math .floor (sourceWidth / (float ) powerOfTwoSampleSize );
470
- powerOfTwoHeight = (int ) Math .floor (sourceHeight / (float ) powerOfTwoSampleSize );
472
+ powerOfTwoWidth = (int ) Math .floor (orientedSourceWidth / (float ) powerOfTwoSampleSize );
473
+ powerOfTwoHeight = (int ) Math .floor (orientedSourceHeight / (float ) powerOfTwoSampleSize );
471
474
} else if (imageType == ImageType .WEBP || imageType == ImageType .WEBP_A ) {
472
475
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
473
- powerOfTwoWidth = Math .round (sourceWidth / (float ) powerOfTwoSampleSize );
474
- powerOfTwoHeight = Math .round (sourceHeight / (float ) powerOfTwoSampleSize );
476
+ powerOfTwoWidth = Math .round (orientedSourceWidth / (float ) powerOfTwoSampleSize );
477
+ powerOfTwoHeight = Math .round (orientedSourceHeight / (float ) powerOfTwoSampleSize );
475
478
} else {
476
- powerOfTwoWidth = (int ) Math .floor (sourceWidth / (float ) powerOfTwoSampleSize );
477
- powerOfTwoHeight = (int ) Math .floor (sourceHeight / (float ) powerOfTwoSampleSize );
479
+ powerOfTwoWidth = (int ) Math .floor (orientedSourceWidth / (float ) powerOfTwoSampleSize );
480
+ powerOfTwoHeight = (int ) Math .floor (orientedSourceHeight / (float ) powerOfTwoSampleSize );
478
481
}
479
- } else if (sourceWidth % powerOfTwoSampleSize != 0
480
- || sourceHeight % powerOfTwoSampleSize != 0 ) {
482
+ } else if (orientedSourceWidth % powerOfTwoSampleSize != 0
483
+ || orientedSourceHeight % powerOfTwoSampleSize != 0 ) {
481
484
// If we're not confident the image is in one of our types, fall back to checking the
482
485
// dimensions again. inJustDecodeBounds decodes do obey inSampleSize.
483
486
int [] dimensions = getDimensions (is , options , decodeCallbacks , bitmapPool );
@@ -488,8 +491,8 @@ private static void calculateScaling(
488
491
powerOfTwoWidth = dimensions [0 ];
489
492
powerOfTwoHeight = dimensions [1 ];
490
493
} else {
491
- powerOfTwoWidth = sourceWidth / powerOfTwoSampleSize ;
492
- powerOfTwoHeight = sourceHeight / powerOfTwoSampleSize ;
494
+ powerOfTwoWidth = orientedSourceWidth / powerOfTwoSampleSize ;
495
+ powerOfTwoHeight = orientedSourceHeight / powerOfTwoSampleSize ;
493
496
}
494
497
495
498
double adjustedScaleFactor =
@@ -517,6 +520,8 @@ private static void calculateScaling(
517
520
+ "x"
518
521
+ sourceHeight
519
522
+ "]"
523
+ + ", degreesToRotate: "
524
+ + degreesToRotate
520
525
+ ", target: ["
521
526
+ targetWidth
522
527
+ "x"
0 commit comments