@@ -128,35 +128,33 @@ internal object BitmapUtils {
128128 uri : Uri ,
129129 reqWidth : Int ,
130130 reqHeight : Int ,
131- ): BitmapSampled {
132- return try {
133- val resolver = context.contentResolver
134- // First decode with inJustDecodeBounds=true to check dimensions
135- val options = decodeImageForOption(resolver, uri)
136- if (options.outWidth == - 1 && options.outHeight == - 1 ) throw RuntimeException (" File is not a picture" )
137- // Calculate inSampleSize
138- options.inSampleSize = max(
139- calculateInSampleSizeByRequestedSize(
140- width = options.outWidth,
141- height = options.outHeight,
142- reqWidth = reqWidth,
143- reqHeight = reqHeight,
144- ),
145- calculateInSampleSizeByMaxTextureSize(
146- width = options.outWidth,
147- height = options.outHeight,
148- ),
149- )
150- // Decode bitmap with inSampleSize set
151- val bitmap = decodeImage(
152- resolver = resolver,
153- uri = uri,
154- options = options,
155- )
156- BitmapSampled (bitmap, options.inSampleSize)
157- } catch (e: Exception ) {
158- throw CropException .FailedToLoadBitmap (uri, e.message)
159- }
131+ ): BitmapSampled = try {
132+ val resolver = context.contentResolver
133+ // First decode with inJustDecodeBounds=true to check dimensions
134+ val options = decodeImageForOption(resolver, uri)
135+ if (options.outWidth == - 1 && options.outHeight == - 1 ) throw RuntimeException (" File is not a picture" )
136+ // Calculate inSampleSize
137+ options.inSampleSize = max(
138+ calculateInSampleSizeByRequestedSize(
139+ width = options.outWidth,
140+ height = options.outHeight,
141+ reqWidth = reqWidth,
142+ reqHeight = reqHeight,
143+ ),
144+ calculateInSampleSizeByMaxTextureSize(
145+ width = options.outWidth,
146+ height = options.outHeight,
147+ ),
148+ )
149+ // Decode bitmap with inSampleSize set
150+ val bitmap = decodeImage(
151+ resolver = resolver,
152+ uri = uri,
153+ options = options,
154+ )
155+ BitmapSampled (bitmap, options.inSampleSize)
156+ } catch (e: Exception ) {
157+ throw CropException .FailedToLoadBitmap (uri, e.message)
160158 }
161159
162160 /* *
@@ -325,58 +323,42 @@ internal object BitmapUtils {
325323 /* *
326324 * Get left value of the bounding rectangle of the given points.
327325 */
328- fun getRectLeft (points : FloatArray ): Float {
329- return min(min(min(points[0 ], points[2 ]), points[4 ]), points[6 ])
330- }
326+ fun getRectLeft (points : FloatArray ): Float = min(min(min(points[0 ], points[2 ]), points[4 ]), points[6 ])
331327
332328 /* *
333329 * Get top value of the bounding rectangle of the given points.
334330 */
335- fun getRectTop (points : FloatArray ): Float {
336- return min(min(min(points[1 ], points[3 ]), points[5 ]), points[7 ])
337- }
331+ fun getRectTop (points : FloatArray ): Float = min(min(min(points[1 ], points[3 ]), points[5 ]), points[7 ])
338332
339333 /* *
340334 * Get right value of the bounding rectangle of the given points.
341335 */
342- fun getRectRight (points : FloatArray ): Float {
343- return max(max(max(points[0 ], points[2 ]), points[4 ]), points[6 ])
344- }
336+ fun getRectRight (points : FloatArray ): Float = max(max(max(points[0 ], points[2 ]), points[4 ]), points[6 ])
345337
346338 /* *
347339 * Get bottom value of the bounding rectangle of the given points.
348340 */
349- fun getRectBottom (points : FloatArray ): Float {
350- return max(max(max(points[1 ], points[3 ]), points[5 ]), points[7 ])
351- }
341+ fun getRectBottom (points : FloatArray ): Float = max(max(max(points[1 ], points[3 ]), points[5 ]), points[7 ])
352342
353343 /* *
354344 * Get width of the bounding rectangle of the given points.
355345 */
356- fun getRectWidth (points : FloatArray ): Float {
357- return getRectRight(points) - getRectLeft(points)
358- }
346+ fun getRectWidth (points : FloatArray ): Float = getRectRight(points) - getRectLeft(points)
359347
360348 /* *
361349 * Get height of the bounding rectangle of the given points.
362350 */
363- fun getRectHeight (points : FloatArray ): Float {
364- return getRectBottom(points) - getRectTop(points)
365- }
351+ fun getRectHeight (points : FloatArray ): Float = getRectBottom(points) - getRectTop(points)
366352
367353 /* *
368354 * Get horizontal center value of the bounding rectangle of the given points.
369355 */
370- fun getRectCenterX (points : FloatArray ): Float {
371- return (getRectRight(points) + getRectLeft(points)) / 2f
372- }
356+ fun getRectCenterX (points : FloatArray ): Float = (getRectRight(points) + getRectLeft(points)) / 2f
373357
374358 /* *
375359 * Get vertical center value of the bounding rectangle of the given points.
376360 */
377- fun getRectCenterY (points : FloatArray ): Float {
378- return (getRectBottom(points) + getRectTop(points)) / 2f
379- }
361+ fun getRectCenterY (points : FloatArray ): Float = (getRectBottom(points) + getRectTop(points)) / 2f
380362
381363 /* *
382364 * Get a rectangle for the given 4 points (x0,y0,x1,y1,x2,y2,x3,y3) by finding the min/max 2
@@ -457,7 +439,7 @@ internal object BitmapUtils {
457439 ): Uri {
458440 val newUri = customOutputUri ? : buildUri(context, compressFormat)
459441
460- return context.contentResolver.openOutputStream(newUri, WRITE_AND_TRUNCATE ).use {
442+ return context.contentResolver.openOutputStream(newUri, WRITE_AND_TRUNCATE )!! .use {
461443 bitmap.compress(compressFormat, compressQuality, it)
462444 newUri
463445 }
@@ -701,14 +683,12 @@ internal object BitmapUtils {
701683 * Decode image from uri using "inJustDecodeBounds" to get the image dimensions.
702684 */
703685 @Throws(FileNotFoundException ::class )
704- private fun decodeImageForOption (resolver : ContentResolver , uri : Uri ): BitmapFactory .Options {
705- return resolver.openInputStream(uri).use {
706- val options = BitmapFactory .Options ()
707- options.inJustDecodeBounds = true
708- BitmapFactory .decodeStream(it, EMPTY_RECT , options)
709- options.inJustDecodeBounds = false
710- options
711- }
686+ private fun decodeImageForOption (resolver : ContentResolver , uri : Uri ): BitmapFactory .Options = resolver.openInputStream(uri).use {
687+ val options = BitmapFactory .Options ()
688+ options.inJustDecodeBounds = true
689+ BitmapFactory .decodeStream(it, EMPTY_RECT , options)
690+ options.inJustDecodeBounds = false
691+ options
712692 }
713693
714694 /* *
@@ -790,14 +770,14 @@ internal object BitmapUtils {
790770 * Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping.
791771 */
792772 private fun cropForRotatedImage (
793- bitmap : Bitmap ? ,
773+ bitmap : Bitmap ,
794774 cropPoints : FloatArray ,
795775 rect : Rect ,
796776 degreesRotated : Int ,
797777 fixAspectRatio : Boolean ,
798778 aspectRatioX : Int ,
799779 aspectRatioY : Int ,
800- ): Bitmap ? {
780+ ): Bitmap {
801781 var tempBitmap = bitmap
802782 if (degreesRotated % 90 != 0 ) {
803783 var adjLeft = 0
@@ -823,14 +803,14 @@ internal object BitmapUtils {
823803 }
824804 val bitmapTmp = tempBitmap
825805 tempBitmap = Bitmap .createBitmap(
826- bitmap!! ,
806+ bitmap,
827807 rect.left,
828808 rect.top,
829809 rect.width(),
830810 rect.height(),
831811 )
832812 if (bitmapTmp != tempBitmap) {
833- bitmapTmp? .recycle()
813+ bitmapTmp.recycle()
834814 }
835815 }
836816 return tempBitmap
@@ -888,22 +868,20 @@ internal object BitmapUtils {
888868 degrees : Int ,
889869 flipHorizontally : Boolean ,
890870 flipVertically : Boolean ,
891- ): Bitmap {
892- return if (degrees > 0 || flipHorizontally || flipVertically) {
893- val matrix = Matrix ()
894- matrix.setRotate(degrees.toFloat())
895- matrix.postScale(
896- (if (flipHorizontally) - 1 else 1 ).toFloat(),
897- (if (flipVertically) - 1 else 1 ).toFloat(),
898- )
899- val newBitmap = Bitmap .createBitmap(bitmap, 0 , 0 , bitmap.width, bitmap.height, matrix, false )
900- if (newBitmap != bitmap) {
901- bitmap.recycle()
902- }
903- newBitmap
904- } else {
905- bitmap
871+ ): Bitmap = if (degrees > 0 || flipHorizontally || flipVertically) {
872+ val matrix = Matrix ()
873+ matrix.setRotate(degrees.toFloat())
874+ matrix.postScale(
875+ (if (flipHorizontally) - 1 else 1 ).toFloat(),
876+ (if (flipVertically) - 1 else 1 ).toFloat(),
877+ )
878+ val newBitmap = Bitmap .createBitmap(bitmap, 0 , 0 , bitmap.width, bitmap.height, matrix, false )
879+ if (newBitmap != bitmap) {
880+ bitmap.recycle()
906881 }
882+ newBitmap
883+ } else {
884+ bitmap
907885 }
908886 // Only need to check for width since opengl textures are always squared
909887 // Keep track of the maximum texture size
0 commit comments