Functions renaming.

This commit is contained in:
Oupson 2020-12-06 19:57:26 +01:00
parent f80c395174
commit 0ee959ee7a
6 changed files with 126 additions and 128 deletions

View File

@ -11,13 +11,12 @@ import oupson.apng.utils.Utils
import oupson.apng.utils.Utils.Companion.isApng import oupson.apng.utils.Utils.Companion.isApng
import oupson.apng.utils.Utils.Companion.isPng import oupson.apng.utils.Utils.Companion.isPng
import oupson.apng.utils.Utils.Companion.pngSignature import oupson.apng.utils.Utils.Companion.pngSignature
import oupson.apng.utils.Utils.Companion.to4Bytes
import oupson.apng.utils.Utils.Companion.uIntFromBytesBigEndian import oupson.apng.utils.Utils.Companion.uIntFromBytesBigEndian
import oupson.apng.utils.Utils.Companion.uIntToByteArray
import java.io.InputStream import java.io.InputStream
import java.util.* import java.util.*
import java.util.zip.CRC32 import java.util.zip.CRC32
// TODO REWRITE
@Deprecated("Deprecated, Use ApngEncoder and ApngDecoder instead", level = DeprecationLevel.WARNING) @Deprecated("Deprecated, Use ApngEncoder and ApngDecoder instead", level = DeprecationLevel.WARNING)
class APNGDisassembler { class APNGDisassembler {
private var png: ArrayList<Byte>? = null private var png: ArrayList<Byte>? = null
@ -103,7 +102,7 @@ class APNGDisassembler {
// We need a body var to know body length and generate crc // We need a body var to know body length and generate crc
val ihdrBody = ArrayList<Byte>() val ihdrBody = ArrayList<Byte>()
// Add chunk body length // Add chunk body length
ihdr.addAll(to4Bytes(ihdrOfApng.body.size).asList()) ihdr.addAll(uIntToByteArray(ihdrOfApng.body.size).asList())
// Add IHDR // Add IHDR
ihdrBody.addAll( ihdrBody.addAll(
byteArrayOf( byteArrayOf(
@ -114,8 +113,8 @@ class APNGDisassembler {
).asList() ).asList()
) )
// Add the max width and height // Add the max width and height
ihdrBody.addAll(to4Bytes(width).asList()) ihdrBody.addAll(uIntToByteArray(width).asList())
ihdrBody.addAll(to4Bytes(height).asList()) ihdrBody.addAll(uIntToByteArray(height).asList())
// 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. Good solution is to create new png.
ihdrBody.addAll(ihdrOfApng.body.copyOfRange(8, 13).asList()) ihdrBody.addAll(ihdrOfApng.body.copyOfRange(8, 13).asList())
@ -123,7 +122,7 @@ class APNGDisassembler {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size) crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody) ihdr.addAll(ihdrBody)
ihdr.addAll(to4Bytes(crC32.value.toInt()).asList()) ihdr.addAll(uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray() return ihdr.toByteArray()
} }
@ -142,14 +141,14 @@ class APNGDisassembler {
name.contentEquals(Utils.fcTL) -> { name.contentEquals(Utils.fcTL) -> {
if (png == null) { if (png == null) {
cover?.let { cover?.let {
it.addAll(to4Bytes(0).asList()) it.addAll(uIntToByteArray(0).asList())
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
it.addAll(iend.asList()) it.addAll(iend.asList())
it.addAll(to4Bytes(crC32.value.toInt()).asList()) it.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.cover = BitmapFactory.decodeByteArray(it.toByteArray(), 0, it.size) apng.cover = BitmapFactory.decodeByteArray(it.toByteArray(), 0, it.size)
} }
png = ArrayList() png = ArrayList()
@ -179,14 +178,14 @@ class APNGDisassembler {
} }
} else { } else {
// Add IEND body length : 0 // Add IEND body length : 0
png?.addAll(to4Bytes(0).asList()) png?.addAll(uIntToByteArray(0).asList())
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
png?.addAll(iend.asList()) png?.addAll(iend.asList())
png?.addAll(to4Bytes(crC32.value.toInt()).asList()) png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.frames.add( apng.frames.add(
Frame( Frame(
png!!.toByteArray(), png!!.toByteArray(),
@ -221,14 +220,14 @@ class APNGDisassembler {
} }
name.contentEquals(Utils.IEND) -> { name.contentEquals(Utils.IEND) -> {
if (isApng) { if (isApng) {
png?.addAll(to4Bytes(0).asList()) png?.addAll(uIntToByteArray(0).asList())
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
png?.addAll(iend.asList()) png?.addAll(iend.asList())
png?.addAll(to4Bytes(crC32.value.toInt()).asList()) png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.frames.add( apng.frames.add(
Frame( Frame(
png!!.toByteArray(), png!!.toByteArray(),
@ -243,14 +242,14 @@ class APNGDisassembler {
) )
} else { } else {
cover?.let { cover?.let {
it.addAll(to4Bytes(0).asList()) it.addAll(uIntToByteArray(0).asList())
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
it.addAll(iend.asList()) it.addAll(iend.asList())
it.addAll(to4Bytes(crC32.value.toInt()).asList()) it.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.cover = BitmapFactory.decodeByteArray(it.toByteArray(), 0, it.size) apng.cover = BitmapFactory.decodeByteArray(it.toByteArray(), 0, it.size)
} }
apng.isApng = false apng.isApng = false
@ -273,7 +272,7 @@ class APNGDisassembler {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
cover?.addAll(body) cover?.addAll(body)
cover?.addAll(to4Bytes(crC32.value.toInt()).asList()) cover?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
} else { } else {
// Find the chunk length // Find the chunk length
val bodySize = uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt)) val bodySize = uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt))
@ -285,13 +284,13 @@ class APNGDisassembler {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
png?.addAll(body) png?.addAll(body)
png?.addAll(to4Bytes(crC32.value.toInt()).asList()) png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
} }
} }
name.contentEquals(Utils.fdAT) -> { name.contentEquals(Utils.fdAT) -> {
// Find the chunk length // Find the chunk length
val bodySize = uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt)) val bodySize = uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt))
png?.addAll(to4Bytes(bodySize - 4).asList()) png?.addAll(uIntToByteArray(bodySize - 4).asList())
val body = ArrayList<Byte>() val body = ArrayList<Byte>()
body.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList()) body.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList())
// Get image bytes // Get image bytes
@ -299,7 +298,7 @@ class APNGDisassembler {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
png?.addAll(body) png?.addAll(body)
png?.addAll(to4Bytes(crC32.value.toInt()).asList()) png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
} }
name.contentEquals(Utils.plte) -> { name.contentEquals(Utils.plte) -> {
plte = byteArray plte = byteArray

View File

@ -12,8 +12,6 @@ import oupson.apng.utils.Utils
import oupson.apng.utils.Utils.Companion.getBlendOp import oupson.apng.utils.Utils.Companion.getBlendOp
import oupson.apng.utils.Utils.Companion.getDisposeOp import oupson.apng.utils.Utils.Companion.getDisposeOp
import oupson.apng.utils.Utils.Companion.pngSignature import oupson.apng.utils.Utils.Companion.pngSignature
import oupson.apng.utils.Utils.Companion.to2Bytes
import oupson.apng.utils.Utils.Companion.to4Bytes
import java.io.File import java.io.File
import java.util.zip.CRC32 import java.util.zip.CRC32
@ -106,39 +104,39 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList()) fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number // Add the frame number
fcTL.addAll(to4Bytes(seq).asList()) fcTL.addAll(Utils.uIntToByteArray(seq).asList())
// foreach fcTL or fdAT we must increment seq // foreach fcTL or fdAT we must increment seq
seq++ seq++
// Add width and height // Add width and height
fcTL.addAll(to4Bytes(frames[0].width).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].width).asList())
fcTL.addAll(to4Bytes(frames[0].height).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].height).asList())
// Add offsets // Add offsets
fcTL.addAll(to4Bytes(frames[0].xOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[0].yOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].yOffsets).asList())
// Set frame delay // Set frame delay
fcTL.addAll(to2Bytes(frames[0].delay.toInt()).asList()) fcTL.addAll(Utils.uShortToArray(frames[0].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList()) fcTL.addAll(Utils.uShortToArray(1000).asList())
// Add DisposeOp and BlendOp // Add DisposeOp and BlendOp
fcTL.add(getDisposeOp(frames[0].disposeOp).toByte()) fcTL.add(getDisposeOp(frames[0].disposeOp))
fcTL.add(getBlendOp(frames[0].blendOp).toByte()) fcTL.add(getBlendOp(frames[0].blendOp))
// Create CRC // Create CRC
val crc = CRC32() val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size) crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL) framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion // endregion
// region idat // region idat
frames[0].idat.IDATBody.forEach { frames[0].idat.IDATBody.forEach {
val idat = ArrayList<Byte>() val idat = ArrayList<Byte>()
// Add the chunk body length // Add the chunk body length
framesByte.addAll(to4Bytes(it.size).asList()) framesByte.addAll(Utils.uIntToByteArray(it.size).asList())
// Add IDAT // Add IDAT
idat.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList()) idat.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList())
// Add chunk body // Add chunk body
@ -147,7 +145,7 @@ class Apng {
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(idat.toByteArray(), 0, idat.size) crc1.update(idat.toByteArray(), 0, idat.size)
framesByte.addAll(idat) framesByte.addAll(idat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
} }
// endregion // endregion
res.addAll(framesByte) res.addAll(framesByte)
@ -159,13 +157,13 @@ class Apng {
idat.parse(PngEncoder().encode(cover!!, true, 1)) idat.parse(PngEncoder().encode(cover!!, true, 1))
idat.IDATBody.forEach { idat.IDATBody.forEach {
val idatByteArray = ArrayList<Byte>() val idatByteArray = ArrayList<Byte>()
framesByte.addAll(to4Bytes(it.size).asList()) framesByte.addAll(Utils.uIntToByteArray(it.size).asList())
idatByteArray.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList()) idatByteArray.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList())
idatByteArray.addAll(it.asList()) idatByteArray.addAll(it.asList())
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(idatByteArray.toByteArray(), 0, idatByteArray.size) crc1.update(idatByteArray.toByteArray(), 0, idatByteArray.size)
framesByte.addAll(idatByteArray) framesByte.addAll(idatByteArray)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
} }
// endregion // endregion
@ -177,40 +175,40 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList()) fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number // Add the frame number
fcTL.addAll(to4Bytes(seq).asList()) fcTL.addAll(Utils.uIntToByteArray(seq).asList())
seq++ seq++
// Add width and height // Add width and height
fcTL.addAll(to4Bytes(frames[0].width).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].width).asList())
fcTL.addAll(to4Bytes(frames[0].height).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].height).asList())
fcTL.addAll(to4Bytes(frames[0].xOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[0].yOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[0].yOffsets).asList())
// Set frame delay // Set frame delay
fcTL.addAll(to2Bytes(frames[0].delay.toInt()).asList()) fcTL.addAll(Utils.uShortToArray(frames[0].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList()) fcTL.addAll(Utils.uShortToArray(1000).asList())
// Add DisposeOp and BlendOp // Add DisposeOp and BlendOp
fcTL.add(getDisposeOp(frames[0].disposeOp).toByte()) fcTL.add(getDisposeOp(frames[0].disposeOp))
fcTL.add(getBlendOp(frames[0].blendOp).toByte()) fcTL.add(getBlendOp(frames[0].blendOp))
// Generate CRC // Generate CRC
val crc = CRC32() val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size) crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL) framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion // endregion
// region fdat // region fdat
frames[0].idat.IDATBody.forEach { frames[0].idat.IDATBody.forEach {
val fdat = ArrayList<Byte>() val fdat = ArrayList<Byte>()
// Add the chunk body length // Add the chunk body length
framesByte.addAll(to4Bytes(it.size + 4).asList()) framesByte.addAll(Utils.uIntToByteArray(it.size + 4).asList())
// Add fdat // Add fdat
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList()) fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
fdat.addAll(to4Bytes(seq).asList()) fdat.addAll(Utils.uIntToByteArray(seq).asList())
seq++ seq++
// Add chunk body // Add chunk body
fdat.addAll(it.asList()) fdat.addAll(it.asList())
@ -218,7 +216,7 @@ class Apng {
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(fdat.toByteArray(), 0, fdat.size) crc1.update(fdat.toByteArray(), 0, fdat.size)
framesByte.addAll(fdat) framesByte.addAll(fdat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
} }
// endregion // endregion
res.addAll(framesByte) res.addAll(framesByte)
@ -234,26 +232,26 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList()) fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Frame number // Frame number
fcTL.addAll(to4Bytes(seq).asList()) fcTL.addAll(Utils.uIntToByteArray(seq).asList())
seq++ seq++
// width and height // width and height
fcTL.addAll(to4Bytes(frames[i].width).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[i].width).asList())
fcTL.addAll(to4Bytes(frames[i].height).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[i].height).asList())
fcTL.addAll(to4Bytes(frames[i].xOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[i].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[i].yOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(frames[i].yOffsets).asList())
// Set frame delay // Set frame delay
fcTL.addAll(to2Bytes(frames[i].delay.toInt()).asList()) fcTL.addAll(Utils.uShortToArray(frames[i].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList()) fcTL.addAll(Utils.uShortToArray(1000).asList())
fcTL.add(getDisposeOp(frames[i].disposeOp).toByte()) fcTL.add(getDisposeOp(frames[i].disposeOp))
fcTL.add(getBlendOp(frames[i].blendOp).toByte()) fcTL.add(getBlendOp(frames[i].blendOp))
val crc = CRC32() val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size) crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL) framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion // endregion
// region fdAT // region fdAT
@ -261,12 +259,12 @@ class Apng {
frames[i].idat.IDATBody.forEach { frames[i].idat.IDATBody.forEach {
val fdat = ArrayList<Byte>() val fdat = ArrayList<Byte>()
// Add IDAT size of frame + 4 byte of the seq // Add IDAT size of frame + 4 byte of the seq
framesByte.addAll(to4Bytes(it.size + 4).asList()) framesByte.addAll(Utils.uIntToByteArray(it.size + 4).asList())
// Add fdAT // Add fdAT
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList()) fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
// Add Sequence number // Add Sequence number
// ! THIS IS NOT FRAME NUMBER // ! THIS IS NOT FRAME NUMBER
fdat.addAll(to4Bytes(seq).asList()) fdat.addAll(Utils.uIntToByteArray(seq).asList())
// Increase seq // Increase seq
seq++ seq++
fdat.addAll(it.asList()) fdat.addAll(it.asList())
@ -274,7 +272,7 @@ class Apng {
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(fdat.toByteArray(), 0, fdat.size) crc1.update(fdat.toByteArray(), 0, fdat.size)
framesByte.addAll(fdat) framesByte.addAll(fdat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList()) framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
} }
// endregion // endregion
res.addAll(framesByte) res.addAll(framesByte)
@ -282,14 +280,14 @@ class Apng {
if (frames.isNotEmpty()) { if (frames.isNotEmpty()) {
// Add IEND body length : 0 // Add IEND body length : 0
res.addAll(to4Bytes(0).asList()) res.addAll(Utils.uIntToByteArray(0).asList())
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
res.addAll(iend.asList()) res.addAll(iend.asList())
res.addAll(to4Bytes(crC32.value.toInt()).asList()) res.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return res.toByteArray() return res.toByteArray()
} else { } else {
throw NoFrameException() throw NoFrameException()
@ -328,13 +326,13 @@ class Apng {
} }
// Add chunk body length // Add chunk body length
ihdr.addAll(to4Bytes(frames[0].ihdr.body.size).asList()) ihdr.addAll(Utils.uIntToByteArray(frames[0].ihdr.body.size).asList())
// Add IHDR // Add IHDR
ihdrBody.addAll(byteArrayOf(0x49.toByte(), 0x48.toByte(), 0x44.toByte(), 0x52.toByte()).asList()) ihdrBody.addAll(byteArrayOf(0x49.toByte(), 0x48.toByte(), 0x44.toByte(), 0x52.toByte()).asList())
// Add the max width and height // Add the max width and height
ihdrBody.addAll(to4Bytes(maxWidth!!).asList()) ihdrBody.addAll(Utils.uIntToByteArray(maxWidth!!).asList())
ihdrBody.addAll(to4Bytes(maxHeight!!).asList()) ihdrBody.addAll(Utils.uIntToByteArray(maxHeight!!).asList())
// 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. Good solution is to create new png.
@ -344,7 +342,7 @@ class Apng {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size) crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody) ihdr.addAll(ihdrBody)
ihdr.addAll(to4Bytes(crC32.value.toInt()).asList()) ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray() return ihdr.toByteArray()
} }
@ -357,22 +355,22 @@ class Apng {
val actl = ArrayList<Byte>() val actl = ArrayList<Byte>()
// Add length bytes // Add length bytes
res.addAll(to4Bytes(8).asList()) res.addAll(Utils.uIntToByteArray(8).asList())
// Add acTL // Add acTL
actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList()) actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList())
// Add number of frames // Add number of frames
actl.addAll(to4Bytes(frames.size).asList()) actl.addAll(Utils.uIntToByteArray(frames.size).asList())
// Number of repeat, 0 to infinite // Number of repeat, 0 to infinite
actl.addAll(to4Bytes(0).asList()) actl.addAll(Utils.uIntToByteArray(0).asList())
res.addAll(actl) res.addAll(actl)
// generate crc // generate crc
val crc = CRC32() val crc = CRC32()
crc.update(actl.toByteArray(), 0, actl.size) crc.update(actl.toByteArray(), 0, actl.size)
res.addAll(to4Bytes(crc.value.toInt()).asList()) res.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
return res return res
} }

View File

@ -133,7 +133,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(Utils.IEND, 0, Utils.IEND.size) crC32.update(Utils.IEND, 0, Utils.IEND.size)
it.addAll(Utils.IEND.asList()) it.addAll(Utils.IEND.asList())
it.addAll(Utils.to4Bytes(crC32.value.toInt())) it.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
/**APNGDisassembler.apng.cover = BitmapFactory.decodeByteArray( /**APNGDisassembler.apng.cover = BitmapFactory.decodeByteArray(
it.toByteArray(), it.toByteArray(),
0, 0,
@ -179,7 +179,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(Utils.IEND, 0, Utils.IEND.size) crC32.update(Utils.IEND, 0, Utils.IEND.size)
png.addAll(Utils.IEND.asList()) png.addAll(Utils.IEND.asList())
png.addAll(Utils.to4Bytes(crC32.value.toInt())) png.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
val btm = Bitmap.createBitmap( val btm = Bitmap.createBitmap(
maxWidth, maxWidth,
@ -289,7 +289,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(Utils.IEND, 0, Utils.IEND.size) crC32.update(Utils.IEND, 0, Utils.IEND.size)
png.addAll(Utils.IEND.asList()) png.addAll(Utils.IEND.asList())
png.addAll(Utils.to4Bytes(crC32.value.toInt())) png.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
val btm = Bitmap.createBitmap( val btm = Bitmap.createBitmap(
@ -372,7 +372,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(Utils.IEND, 0, Utils.IEND.size) crC32.update(Utils.IEND, 0, Utils.IEND.size)
it.addAll(Utils.IEND.asList()) it.addAll(Utils.IEND.asList())
it.addAll(Utils.to4Bytes(crC32.value.toInt())) it.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
inputStream.close() inputStream.close()
return BitmapDrawable( return BitmapDrawable(
context.resources, context.resources,
@ -414,7 +414,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
cover.addAll(body) cover.addAll(body)
cover.addAll(Utils.to4Bytes(crC32.value.toInt())) cover.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
} else { } else {
// Find the chunk length // Find the chunk length
val bodySize = val bodySize =
@ -432,13 +432,13 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
png.addAll(body) png.addAll(body)
png.addAll(Utils.to4Bytes(crC32.value.toInt())) png.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
} }
} }
name.contentEquals(Utils.fdAT) -> { name.contentEquals(Utils.fdAT) -> {
// Find the chunk length // Find the chunk length
val bodySize = Utils.uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt)) val bodySize = Utils.uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt))
png?.addAll(Utils.to4Bytes(bodySize - 4).asList()) png?.addAll(Utils.uIntToByteArray(bodySize - 4).asList())
val body = ArrayList<Byte>() val body = ArrayList<Byte>()
body.addAll(Utils.IDAT.asList()) body.addAll(Utils.IDAT.asList())
// Get image bytes // Get image bytes
@ -446,7 +446,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size) crC32.update(body.toByteArray(), 0, body.size)
png?.addAll(body) png?.addAll(body)
png?.addAll(Utils.to4Bytes(crC32.value.toInt())) png?.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
} }
name.contentEquals(Utils.plte) -> { name.contentEquals(Utils.plte) -> {
plte = byteArray plte = byteArray
@ -856,7 +856,7 @@ class ApngDecoder {
// We need a body var to know body length and generate crc // We need a body var to know body length and generate crc
val ihdrBody = ArrayList<Byte>() val ihdrBody = ArrayList<Byte>()
// Add chunk body length // Add chunk body length
ihdr.addAll(Utils.to4Bytes(ihdrOfApng.body.size)) ihdr.addAll(Utils.uIntToByteArray(ihdrOfApng.body.size).asList())
// Add IHDR // Add IHDR
ihdrBody.addAll( ihdrBody.addAll(
arrayOf( arrayOf(
@ -867,8 +867,8 @@ class ApngDecoder {
) )
) )
// Add the max width and height // Add the max width and height
ihdrBody.addAll(Utils.to4Bytes(width)) ihdrBody.addAll(Utils.uIntToByteArray(width).asList())
ihdrBody.addAll(Utils.to4Bytes(height)) ihdrBody.addAll(Utils.uIntToByteArray(height).asList())
// 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. Good solution is to create new png.
ihdrBody.addAll(ihdrOfApng.body.copyOfRange(8, 13).asList()) ihdrBody.addAll(ihdrOfApng.body.copyOfRange(8, 13).asList())
@ -876,7 +876,7 @@ class ApngDecoder {
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size) crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody) ihdr.addAll(ihdrBody)
ihdr.addAll(Utils.to4Bytes(crC32.value.toInt())) ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray() return ihdr.toByteArray()
} }
} }

View File

@ -88,30 +88,30 @@ class ApngEncoder(
i.toByteArray() i.toByteArray()
} }
// Add the chunk body length // Add the chunk body length
outputStream.write(Utils.to4BytesArray(idatBody.size)) outputStream.write(Utils.uIntToByteArray(idatBody.size))
// Generate CRC // Generate CRC
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(idatChunk, 0, idatChunk.size) crc1.update(idatChunk, 0, idatChunk.size)
outputStream.write(idatChunk) outputStream.write(idatChunk)
outputStream.write(Utils.to4BytesArray(crc1.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc1.value.toInt()))
} else { } else {
val fdat = ArrayList<Byte>().let { fdat -> val fdat = ArrayList<Byte>().let { fdat ->
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList()) fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
// Add fdat // Add fdat
fdat.addAll(Utils.to4Bytes(seq++).asList()) fdat.addAll(Utils.uIntToByteArray(seq++).asList())
// Add chunk body // Add chunk body
fdat.addAll(idatBody.asList()) fdat.addAll(idatBody.asList())
fdat.toByteArray() fdat.toByteArray()
} }
// Add the chunk body length // Add the chunk body length
outputStream.write(Utils.to4BytesArray(idatBody.size + 4)) outputStream.write(Utils.uIntToByteArray(idatBody.size + 4))
// Generate CRC // Generate CRC
val crc1 = CRC32() val crc1 = CRC32()
crc1.update(fdat, 0, fdat.size) crc1.update(fdat, 0, fdat.size)
outputStream.write(fdat) outputStream.write(fdat)
outputStream.write(Utils.to4BytesArray(crc1.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc1.value.toInt()))
} }
} }
frameIndex++ frameIndex++
@ -123,14 +123,14 @@ class ApngEncoder(
@Suppress("unused") @Suppress("unused")
fun writeEnd() { fun writeEnd() {
// Add IEND body length : 0 // Add IEND body length : 0
outputStream.write(Utils.to4BytesArray(0)) outputStream.write(Utils.uIntToByteArray(0))
// Add IEND // Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44) val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND // Generate crc for IEND
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(iend, 0, iend.size) crC32.update(iend, 0, iend.size)
outputStream.write(iend) outputStream.write(iend)
outputStream.write(Utils.to4BytesArray(crC32.value.toInt())) outputStream.write(Utils.uIntToByteArray(crC32.value.toInt()))
} }
/** /**
@ -154,8 +154,8 @@ class ApngEncoder(
ihdrBody.addAll(arrayListOf(0x49, 0x48, 0x44, 0x52)) ihdrBody.addAll(arrayListOf(0x49, 0x48, 0x44, 0x52))
// Add the max width and height // Add the max width and height
ihdrBody.addAll(Utils.to4Bytes(width).asList()) ihdrBody.addAll(Utils.uIntToByteArray(width).asList())
ihdrBody.addAll(Utils.to4Bytes(height).asList()) ihdrBody.addAll(Utils.uIntToByteArray(height).asList())
ihdrBody.add(8.toByte()) ihdrBody.add(8.toByte())
ihdrBody.add(6.toByte()) ihdrBody.add(6.toByte())
@ -168,7 +168,7 @@ class ApngEncoder(
val crC32 = CRC32() val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size) crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody) ihdr.addAll(ihdrBody)
ihdr.addAll(Utils.to4Bytes(crC32.value.toInt()).asList()) ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray() return ihdr.toByteArray()
} }
@ -187,16 +187,16 @@ class ApngEncoder(
actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList()) actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList())
// Add number of frames // Add number of frames
actl.addAll(Utils.to4Bytes(num).asList()) actl.addAll(Utils.uIntToByteArray(num).asList())
// Number of repeat, 0 to infinite // Number of repeat, 0 to infinite
actl.addAll(Utils.to4Bytes(0).asList()) actl.addAll(Utils.uIntToByteArray(0).asList())
res.addAll(actl) res.addAll(actl)
// generate crc // generate crc
val crc = CRC32() val crc = CRC32()
crc.update(actl.toByteArray(), 0, actl.size) crc.update(actl.toByteArray(), 0, actl.size)
res.addAll(Utils.to4Bytes(crc.value.toInt()).asList()) res.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
return res.toByteArray() return res.toByteArray()
} }
@ -210,28 +210,28 @@ class ApngEncoder(
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList()) fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number // Add the frame number
fcTL.addAll(Utils.to4Bytes(seq++).asList()) fcTL.addAll(Utils.uIntToByteArray(seq++).asList())
// Add width and height // Add width and height
fcTL.addAll(Utils.to4Bytes(btm.width).asList()) fcTL.addAll(Utils.uIntToByteArray(btm.width).asList())
fcTL.addAll(Utils.to4Bytes(btm.height).asList()) fcTL.addAll(Utils.uIntToByteArray(btm.height).asList())
// Add offsets // Add offsets
fcTL.addAll(Utils.to4Bytes(xOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(xOffsets).asList())
fcTL.addAll(Utils.to4Bytes(yOffsets).asList()) fcTL.addAll(Utils.uIntToByteArray(yOffsets).asList())
// Set frame delay // Set frame delay
fcTL.addAll(Utils.to2Bytes(delay.toInt().toShort()).asList()) fcTL.addAll(Utils.uShortToByteArray(delay.toInt().toShort()).asList())
fcTL.addAll(Utils.to2Bytes(1000.toShort()).asList()) fcTL.addAll(Utils.uShortToByteArray(1000.toShort()).asList())
// Add DisposeOp and BlendOp // Add DisposeOp and BlendOp
fcTL.add(Utils.getDisposeOp(disposeOp).toByte()) fcTL.add(Utils.getDisposeOp(disposeOp))
fcTL.add(Utils.getBlendOp(blendOp).toByte()) fcTL.add(Utils.getBlendOp(blendOp))
// Create CRC // Create CRC
val crc = CRC32() val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size) crc.update(fcTL.toByteArray(), 0, fcTL.size)
outputStream.write(fcTL.toByteArray()) outputStream.write(fcTL.toByteArray())
outputStream.write(Utils.to4BytesArray(crc.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc.value.toInt()))
} }
} }

View File

@ -253,7 +253,7 @@ class ExperimentalApngEncoder(
crc.reset() crc.reset()
crc.update(iend, 0, iend.size) crc.update(iend, 0, iend.size)
outputStream.write(iend) outputStream.write(iend)
outputStream.write(Utils.to4BytesArray(crc.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc.value.toInt()))
} }
/** /**
@ -264,8 +264,8 @@ class ExperimentalApngEncoder(
private fun writeHeader() { private fun writeHeader() {
writeInt4(13) writeInt4(13)
val header = Utils.IHDR val header = Utils.IHDR
.plus(Utils.to4BytesArray(width)) .plus(Utils.uIntToByteArray(width))
.plus(Utils.to4BytesArray(height)) .plus(Utils.uIntToByteArray(height))
.plus(8) // bit depth .plus(8) // bit depth
.plus(if (encodeAlpha) 6 else 2) // direct model .plus(if (encodeAlpha) 6 else 2) // direct model
.plus(0) // compression method .plus(0) // compression method
@ -309,15 +309,15 @@ class ExperimentalApngEncoder(
// Add acTL // Add acTL
val acTL = byteArrayOf(0x61, 0x63, 0x54, 0x4c) val acTL = byteArrayOf(0x61, 0x63, 0x54, 0x4c)
// Add number of frames // Add number of frames
.plus(Utils.to4BytesArray(num)) .plus(Utils.uIntToByteArray(num))
// Number of repeat, 0 to infinite // Number of repeat, 0 to infinite
.plus(Utils.to4BytesArray(repetitionCount)) .plus(Utils.uIntToByteArray(repetitionCount))
outputStream.write(acTL) outputStream.write(acTL)
// generate crc // generate crc
crc.reset() crc.reset()
crc.update(acTL, 0, acTL.size) crc.update(acTL, 0, acTL.size)
outputStream.write(Utils.to4BytesArray(crc.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc.value.toInt()))
} }
/** /**
@ -339,20 +339,20 @@ class ExperimentalApngEncoder(
// Add fcTL // Add fcTL
val fcTL = Utils.fcTL val fcTL = Utils.fcTL
// Add the frame number // Add the frame number
.plus(Utils.to4BytesArray(currentSeq++)) .plus(Utils.uIntToByteArray(currentSeq++))
// Add width and height // Add width and height
.plus(Utils.to4BytesArray(btm.width)) .plus(Utils.uIntToByteArray(btm.width))
.plus(Utils.to4BytesArray(btm.height)) .plus(Utils.uIntToByteArray(btm.height))
// Add offsets // Add offsets
.plus(Utils.to4BytesArray(xOffsets)) .plus(Utils.uIntToByteArray(xOffsets))
.plus(Utils.to4BytesArray(yOffsets)) .plus(Utils.uIntToByteArray(yOffsets))
// Set frame delay // Set frame delay
// TODO BETTER FRACTION // TODO BETTER FRACTION
.plus(Utils.to2Bytes(delay.toInt().toShort())) .plus(Utils.uShortToByteArray(delay.toInt().toShort()))
.plus(Utils.to2Bytes(1000.toShort())) .plus(Utils.uShortToByteArray(1000.toShort()))
// Add DisposeOp and BlendOp // Add DisposeOp and BlendOp
.plus(Utils.getDisposeOp(disposeOp)) .plus(Utils.getDisposeOp(disposeOp))
@ -364,7 +364,7 @@ class ExperimentalApngEncoder(
// Write all // Write all
outputStream.write(fcTL) outputStream.write(fcTL)
outputStream.write(Utils.to4BytesArray(crc.value.toInt())) outputStream.write(Utils.uIntToByteArray(crc.value.toInt()))
} }
/** /**
@ -468,7 +468,7 @@ class ExperimentalApngEncoder(
crc.update(Utils.IDAT) crc.update(Utils.IDAT)
} else { // Write a fdAT chunk, containing the current sequence number } else { // Write a fdAT chunk, containing the current sequence number
// Add fdat and sequence number // Add fdat and sequence number
val fdat = Utils.fdAT+ Utils.to4BytesArray(currentSeq++) val fdat = Utils.fdAT+ Utils.uIntToByteArray(currentSeq++)
outputStream.write(fdat) outputStream.write(fdat)
crc.update(fdat) crc.update(fdat)

View File

@ -141,8 +141,7 @@ class Utils {
* @param i The int * @param i The int
* @return [Array] 4 Bytes * @return [Array] 4 Bytes
*/ */
// TODO RENAME fun uShortToArray(i: Int): Array<Byte> {
fun to4Bytes(i: Int): Array<Byte> {
return arrayOf((i shr 24).toByte(), (i shr 16).toByte(), (i shr 8).toByte(), i.toByte()) return arrayOf((i shr 24).toByte(), (i shr 16).toByte(), (i shr 8).toByte(), i.toByte())
} }
@ -151,8 +150,7 @@ class Utils {
* @param i The int * @param i The int
* @return [ByteArray] 4 Bytes * @return [ByteArray] 4 Bytes
*/ */
// TODO RENAME fun uIntToByteArray(i: Int): ByteArray {
fun to4BytesArray(i: Int): ByteArray {
return byteArrayOf( return byteArrayOf(
(i shr 24).toByte(), (i shr 24).toByte(),
(i shr 16).toByte(), (i shr 16).toByte(),
@ -166,13 +164,16 @@ class Utils {
* @param i The int * @param i The int
* @return [ByteArray] 2 Bytes * @return [ByteArray] 2 Bytes
*/ */
// TODO RENAME fun uShortToByteArray(i: Int): ByteArray {
fun to2Bytes(i: Int): ByteArray {
return byteArrayOf((i shr 8).toByte(), i /*>> 0*/.toByte()) return byteArrayOf((i shr 8).toByte(), i /*>> 0*/.toByte())
} }
// TODO RENAME /**
fun to2Bytes(s: Short): ByteArray { * Generate a 2 bytes array from a short
* @param s The int
* @return [ByteArray] 2 Bytes
*/
fun uShortToByteArray(s: Short): ByteArray {
return byteArrayOf((s.toInt() shr 8 and 0x00FF).toByte(), (s and 0xFF).toByte()) return byteArrayOf((s.toInt() shr 8 and 0x00FF).toByte(), (s and 0xFF).toByte())
} }