Work on ApngDecoder.kt, IHDR encoding

This commit is contained in:
Oupson 2021-02-24 11:10:56 +01:00
parent 4defb5d515
commit b584d6d013
2 changed files with 21 additions and 21 deletions

View File

@ -860,32 +860,32 @@ class ApngDecoder {
* @return [ByteArray] The generated IHDR. * @return [ByteArray] The generated IHDR.
*/ */
private fun generateIhdr(ihdrOfApng: ByteArray, width: Int, height: Int): ByteArray { private fun generateIhdr(ihdrOfApng: ByteArray, width: Int, height: Int): ByteArray {
val ihdr = ArrayList<Byte>() val ihdr = ByteArray(0xD + 4 + 4 + 4) // 0xD (IHDR body length) + 4 (0x0, 0x0, 0x0, 0xD : the chunk length) + 4 : IHDR + 4 : CRC
// We need a body var to know body length and generate crc
val ihdrBody = ArrayList<Byte>()
// Add chunk body length // Add chunk body length
ihdr.addAll(Utils.uIntToByteArray(ihdrOfApng.size).asList()) System.arraycopy(Utils.uIntToByteArray(0xD), 0, ihdr, 0, 4)
// We need a body var to know body length and generate crc
val ihdrBody = ByteArray(0xD + 4) // 0xD (IHDR body length) + 4 : IHDR
// Add IHDR // Add IHDR
ihdrBody.addAll( System.arraycopy(Utils.IHDR, 0, ihdrBody, 0, 4)
arrayOf(
0x49.toByte(),
0x48.toByte(),
0x44.toByte(),
0x52.toByte()
)
)
// Add the max width and height // Add the max width and height
ihdrBody.addAll(Utils.uIntToByteArray(width).asList()) System.arraycopy(Utils.uIntToByteArray(width), 0, ihdrBody, 4, 4)
ihdrBody.addAll(Utils.uIntToByteArray(height).asList()) System.arraycopy(Utils.uIntToByteArray(height), 0, ihdrBody, 8, 4)
// Add complicated stuff like depth color ... // Add complicated stuff like depth color ...
// If you want correct png you need same parameters. Good solution is to create new png. // If you want correct png you need same parameters.
ihdrBody.addAll(ihdrOfApng.copyOfRange(8, 13).asList()) System.arraycopy(ihdrOfApng, 8, ihdrBody, 12, 5)
// Generate CRC // Generate CRC
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size) crC32.update(ihdrBody, 0, 0xD + 4)
ihdr.addAll(ihdrBody)
ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList()) System.arraycopy(ihdrBody, 0, ihdr, 4, 0xD + 4)
return ihdr.toByteArray() System.arraycopy(Utils.uIntToByteArray(crC32.value.toInt()), 0, ihdr, 0xD + 4 + 4, 4)
return ihdr
} }
} }
} }

View File

@ -149,7 +149,7 @@ class KotlinFragment : Fragment() {
} }
override fun onError(error: Exception) { override fun onError(error: Exception) {
Log.e(TAG, "Error : $error") Log.e(TAG, "Error when decoding apng", error)
} }
}) })
} }