From 4e807e8b06db4fb36b3b53920dbe43d33d50cfcc Mon Sep 17 00:00:00 2001 From: Oupson Date: Sun, 6 Dec 2020 23:32:39 +0100 Subject: [PATCH] Working on exceptions Content description of fabs --- .../java/oupson/apng/encoder/ApngEncoder.kt | 15 +++++------- .../oupson/apng/exceptions/customException.kt | 23 ++++++++++++++++++- .../java/oupson/apng/imageUtils/PngEncoder.kt | 1 - .../src/main/java/oupson/apng/utils/Utils.kt | 3 +-- .../src/main/res/layout/activity_creator.xml | 1 + .../src/main/res/layout/activity_main.xml | 1 + app-test/src/main/res/values/ids.xml | 2 -- app-test/src/main/res/values/strings.xml | 2 ++ 8 files changed, 33 insertions(+), 15 deletions(-) delete mode 100644 app-test/src/main/res/values/ids.xml diff --git a/apng_library/src/main/java/oupson/apng/encoder/ApngEncoder.kt b/apng_library/src/main/java/oupson/apng/encoder/ApngEncoder.kt index e313482..01e5ca5 100644 --- a/apng_library/src/main/java/oupson/apng/encoder/ApngEncoder.kt +++ b/apng_library/src/main/java/oupson/apng/encoder/ApngEncoder.kt @@ -222,16 +222,12 @@ class ApngEncoder( disposeOp: Utils.Companion.DisposeOp = Utils.Companion.DisposeOp.APNG_DISPOSE_OP_NONE ) { if (currentFrame == 0) { - if (btm.width != width) - throw InvalidFrameSizeException("Width of first frame must be equal to width of APNG. (${btm.width} != $width)") - if (btm.height != height) - throw InvalidFrameSizeException("Height of first frame must be equal to height of APNG. (${btm.height} != $height)") + if (btm.width != width || btm.height != height) + throw InvalidFrameSizeException(btm.width, btm.height, width, height, currentFrame == 0) } - if (btm.width > width) - throw InvalidFrameSizeException("Frame width must be inferior or equal at the animation width") - else if (btm.height > height) - throw InvalidFrameSizeException("Frame height must be inferior or equal at the animation height") + if (btm.width > width || btm.height > height) + throw InvalidFrameSizeException(btm.width, btm.height, width, height, currentFrame == 0) if (firstFrameInAnim || currentFrame != 0) writeFCTL(btm, delay, disposeOp, blendOp, xOffsets, yOffsets) @@ -373,8 +369,9 @@ class ApngEncoder( * to conserve memory, this method grabs as many rows as will * fit into 32K bytes, or the whole image; whichever is less. * + * @param image The frame to encode * - * @return true if no errors; false if error grabbing pixels + * @return [Boolean] true if no errors; false if error grabbing pixels */ private fun writeImageData(image: Bitmap): Boolean { var rowsLeft = height // number of rows remaining to write diff --git a/apng_library/src/main/java/oupson/apng/exceptions/customException.kt b/apng_library/src/main/java/oupson/apng/exceptions/customException.kt index 88e9791..0efe032 100644 --- a/apng_library/src/main/java/oupson/apng/exceptions/customException.kt +++ b/apng_library/src/main/java/oupson/apng/exceptions/customException.kt @@ -4,6 +4,27 @@ class NoFrameException : Exception() class NotPngException : Exception() class NotApngException : Exception() class BadCRCException : Exception() + +// TODO BETTER MESSAGES class BadApngException(override val message: String? = null) : Exception() -class InvalidFrameSizeException(override val message: String?) : Exception() \ No newline at end of file +class InvalidFrameSizeException(animationWidth : Int, animationHeight : Int, frameWidth : Int, frameHeight : Int, isFirstFrame : Boolean) : Exception() { + override val message: String = when { + animationWidth != frameWidth && isFirstFrame -> { + "Width of first frame must be equal to width of APNG ($animationWidth != $frameWidth)." + } + frameHeight != frameHeight && isFirstFrame -> { + "Height of first frame must be equal to height of APNG ($animationHeight != $frameHeight)." + } + frameWidth > animationWidth -> { + "Frame width must be inferior or equal at the animation width ($animationWidth < $frameWidth)." + } + frameHeight > animationHeight -> { + "Frame height must be inferior or equal at the animation height ($animationHeight < $frameHeight)." + } + else -> { + "Unknown problem" + } + } + +} \ No newline at end of file diff --git a/apng_library/src/main/java/oupson/apng/imageUtils/PngEncoder.kt b/apng_library/src/main/java/oupson/apng/imageUtils/PngEncoder.kt index 7275ae9..e66571d 100644 --- a/apng_library/src/main/java/oupson/apng/imageUtils/PngEncoder.kt +++ b/apng_library/src/main/java/oupson/apng/imageUtils/PngEncoder.kt @@ -9,7 +9,6 @@ import java.util.zip.DeflaterOutputStream import kotlin.math.max import kotlin.math.min -// TODO FIND A BETTER SOLUTION /** * Taken from http://catcode.com/pngencoder/com/keypoint/PngEncoder.java */ diff --git a/apng_library/src/main/java/oupson/apng/utils/Utils.kt b/apng_library/src/main/java/oupson/apng/utils/Utils.kt index 7e3fd66..001c040 100644 --- a/apng_library/src/main/java/oupson/apng/utils/Utils.kt +++ b/apng_library/src/main/java/oupson/apng/utils/Utils.kt @@ -24,11 +24,10 @@ class Utils { * @param byteArray APNG * @return True if is an APNG */ - // TODO OPTIMISE + @Deprecated("Will be removed with ApngAnimator and APNGDisassembler") fun isApng(byteArray: ByteArray): Boolean { if (!isPng(byteArray)) return false try { - val acTL = byteArrayOf(0x61, 0x63, 0x54, 0x4c) for (i in 8 until byteArray.size) { val it = byteArray.copyOfRange(i, i + 4) // if byteArray contain acTL diff --git a/app-test/src/main/res/layout/activity_creator.xml b/app-test/src/main/res/layout/activity_creator.xml index b0c8b8e..06f157c 100644 --- a/app-test/src/main/res/layout/activity_creator.xml +++ b/app-test/src/main/res/layout/activity_creator.xml @@ -18,6 +18,7 @@ android:clickable="true" android:focusable="true" app:tint="#fff" + android:contentDescription="@string/add_frame" app:backgroundTint="@color/secondary" app:layout_anchor="@id/creatorBottomAppBar" app:layout_anchorGravity="bottom|right|end" diff --git a/app-test/src/main/res/layout/activity_main.xml b/app-test/src/main/res/layout/activity_main.xml index 6f20fb4..34eb0c7 100644 --- a/app-test/src/main/res/layout/activity_main.xml +++ b/app-test/src/main/res/layout/activity_main.xml @@ -23,6 +23,7 @@ android:layout_height="wrap_content" android:src="@drawable/ic_create_white_24dp" android:text="@string/create" + android:contentDescription="@string/create_animation" app:tint="@android:color/white" app:backgroundTint="@color/secondary" app:layout_anchor="@id/bottomAppBar" diff --git a/app-test/src/main/res/values/ids.xml b/app-test/src/main/res/values/ids.xml deleted file mode 100644 index a6b3dae..0000000 --- a/app-test/src/main/res/values/ids.xml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/app-test/src/main/res/values/strings.xml b/app-test/src/main/res/values/strings.xml index ce2957f..fb0a81e 100644 --- a/app-test/src/main/res/values/strings.xml +++ b/app-test/src/main/res/values/strings.xml @@ -17,6 +17,8 @@ Done Clear First frame in animation + Add a frame to the animation + Create an animation open close