Working on ExperimentalApngEncoder.kt

This commit is contained in:
Oupson 2020-09-19 23:32:59 +02:00
parent f4e78be021
commit 2fd2d0e684
3 changed files with 21 additions and 13 deletions

View File

@ -2,7 +2,6 @@
package oupson.apng.encoder
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import oupson.apng.utils.Utils
@ -17,15 +16,13 @@ import kotlin.math.max
import kotlin.math.min
// TODO DOCUMENTATION
// TODO BITMAP ENCODING
// TODO BUFFER AND BUFFER DEACTIVATION WHEN BITMAP CONFIG DOES NOT CONTAIN AN ALPHA CHANNEL
// TODO JAVA OVERLOADS
class ExperimentalApngEncoder(
private var context : Context, // TODO REMOVE
private val outputStream: OutputStream,
private val width : Int,
private val height : Int,
numberOfFrames : Int) {
numberOfFrames : Int, private val encodeAlpha: Boolean = true, filter: Int = 0, compressionLevel: Int = 0) {
private var frameIndex = 0
private var seq = 0
@ -35,7 +32,7 @@ class ExperimentalApngEncoder(
/** The CRC value. */
private var crcValue: Long = 0
private var encodeAlpha = true
//private var encodeAlpha = true
/** The bytes-per-pixel. */
private var bytesPerPixel: Int = 0
@ -52,10 +49,6 @@ class ExperimentalApngEncoder(
/** The left bytes. */
private var leftBytes: ByteArray? = null
private val idatName : List<Byte> by lazy {
listOf(0x49.toByte(), 0x44.toByte(), 0x41.toByte(), 0x54.toByte())
}
companion object {
/** Constants for filter (NONE) */
private const val FILTER_NONE = 0
@ -72,6 +65,15 @@ class ExperimentalApngEncoder(
init {
this.filter = FILTER_NONE
if (filter <= FILTER_LAST) {
this.filter = filter
}
if (compressionLevel in 0..9) {
this.compressionLevel = compressionLevel
}
outputStream.write(Utils.pngSignature)
writeHeader()
outputStream.write(generateACTL(numberOfFrames))

View File

@ -55,6 +55,7 @@ class Utils {
)
}
// TODO DOC DISPOSE OP AND BLEND OP
enum class DisposeOp {
APNG_DISPOSE_OP_NONE,
APNG_DISPOSE_OP_BACKGROUND,

View File

@ -21,6 +21,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import oupson.apng.encoder.ExperimentalApngEncoder
import oupson.apng.utils.Utils
import oupson.apngcreator.BuildConfig
import oupson.apngcreator.R
import oupson.apngcreator.adapter.ImageAdapter
@ -121,7 +122,7 @@ class CreatorActivity : AppCompatActivity() {
if (BuildConfig.DEBUG)
Log.i(TAG, "MaxWidth : $maxWidth; MaxHeight : $maxHeight")
val encoder = ExperimentalApngEncoder(this@CreatorActivity, out, maxWidth, maxHeight, items.size)
val encoder = ExperimentalApngEncoder(out, maxWidth, maxHeight, items.size, compressionLevel = 9)
items.forEachIndexed { i, uri ->
if (BuildConfig.DEBUG)
Log.v(TAG, "Encoding frame $i")
@ -144,7 +145,8 @@ class CreatorActivity : AppCompatActivity() {
else
btm,
delay = uri.second.toFloat(),
usePngEncoder = true
usePngEncoder = true,
disposeOp = Utils.Companion.DisposeOp.APNG_DISPOSE_OP_NONE
)
//input.close()
} else {
@ -163,6 +165,9 @@ class CreatorActivity : AppCompatActivity() {
encoder.writeEnd()
out.close()
if (BuildConfig.DEBUG)
Log.v(TAG, "${f.length() / 1000}ko")
withContext(Dispatchers.Main) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = FileProvider.getUriForFile(
@ -199,7 +204,7 @@ class CreatorActivity : AppCompatActivity() {
str?.close()
}
val encoder = ExperimentalApngEncoder(this@CreatorActivity, out, maxWidth, maxHeight, items.size)
val encoder = ExperimentalApngEncoder(out, maxWidth, maxHeight, items.size, compressionLevel = 9)
items.forEach { uri ->
println("delay : ${uri.second.toFloat()}ms")
val str = contentResolver.openInputStream(uri.first) ?: return@forEach
@ -314,7 +319,7 @@ class CreatorActivity : AppCompatActivity() {
if (BuildConfig.DEBUG)
Log.i(TAG, "MaxWidth : $maxWidth; MaxHeight : $maxHeight")
val encoder = ExperimentalApngEncoder(this@CreatorActivity, out, maxWidth, maxHeight, items.size)
val encoder = ExperimentalApngEncoder(out, maxWidth, maxHeight, items.size, compressionLevel = 9)
items.forEach { uri ->
// println("delay : ${adapter?.delay?.get(i)?.toFloat() ?: 1000f}ms")
val str =