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.isPng
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.uIntToByteArray
import java.io.InputStream
import java.util.*
import java.util.zip.CRC32
// TODO REWRITE
@Deprecated("Deprecated, Use ApngEncoder and ApngDecoder instead", level = DeprecationLevel.WARNING)
class APNGDisassembler {
private var png: ArrayList<Byte>? = null
@ -103,7 +102,7 @@ class APNGDisassembler {
// We need a body var to know body length and generate crc
val ihdrBody = ArrayList<Byte>()
// Add chunk body length
ihdr.addAll(to4Bytes(ihdrOfApng.body.size).asList())
ihdr.addAll(uIntToByteArray(ihdrOfApng.body.size).asList())
// Add IHDR
ihdrBody.addAll(
byteArrayOf(
@ -114,8 +113,8 @@ class APNGDisassembler {
).asList()
)
// Add the max width and height
ihdrBody.addAll(to4Bytes(width).asList())
ihdrBody.addAll(to4Bytes(height).asList())
ihdrBody.addAll(uIntToByteArray(width).asList())
ihdrBody.addAll(uIntToByteArray(height).asList())
// Add complicated stuff like depth color ...
// If you want correct png you need same parameters. Good solution is to create new png.
ihdrBody.addAll(ihdrOfApng.body.copyOfRange(8, 13).asList())
@ -123,7 +122,7 @@ class APNGDisassembler {
val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody)
ihdr.addAll(to4Bytes(crC32.value.toInt()).asList())
ihdr.addAll(uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray()
}
@ -142,14 +141,14 @@ class APNGDisassembler {
name.contentEquals(Utils.fcTL) -> {
if (png == null) {
cover?.let {
it.addAll(to4Bytes(0).asList())
it.addAll(uIntToByteArray(0).asList())
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
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)
}
png = ArrayList()
@ -179,14 +178,14 @@ class APNGDisassembler {
}
} else {
// Add IEND body length : 0
png?.addAll(to4Bytes(0).asList())
png?.addAll(uIntToByteArray(0).asList())
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
png?.addAll(iend.asList())
png?.addAll(to4Bytes(crC32.value.toInt()).asList())
png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.frames.add(
Frame(
png!!.toByteArray(),
@ -221,14 +220,14 @@ class APNGDisassembler {
}
name.contentEquals(Utils.IEND) -> {
if (isApng) {
png?.addAll(to4Bytes(0).asList())
png?.addAll(uIntToByteArray(0).asList())
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
png?.addAll(iend.asList())
png?.addAll(to4Bytes(crC32.value.toInt()).asList())
png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
apng.frames.add(
Frame(
png!!.toByteArray(),
@ -243,14 +242,14 @@ class APNGDisassembler {
)
} else {
cover?.let {
it.addAll(to4Bytes(0).asList())
it.addAll(uIntToByteArray(0).asList())
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
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.isApng = false
@ -273,7 +272,7 @@ class APNGDisassembler {
val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size)
cover?.addAll(body)
cover?.addAll(to4Bytes(crC32.value.toInt()).asList())
cover?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
} else {
// Find the chunk length
val bodySize = uIntFromBytesBigEndian(byteArray.copyOfRange(i - 4, i).map(Byte::toInt))
@ -285,13 +284,13 @@ class APNGDisassembler {
val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size)
png?.addAll(body)
png?.addAll(to4Bytes(crC32.value.toInt()).asList())
png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
}
}
name.contentEquals(Utils.fdAT) -> {
// Find the chunk length
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>()
body.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList())
// Get image bytes
@ -299,7 +298,7 @@ class APNGDisassembler {
val crC32 = CRC32()
crC32.update(body.toByteArray(), 0, body.size)
png?.addAll(body)
png?.addAll(to4Bytes(crC32.value.toInt()).asList())
png?.addAll(uIntToByteArray(crC32.value.toInt()).asList())
}
name.contentEquals(Utils.plte) -> {
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.getDisposeOp
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.util.zip.CRC32
@ -106,39 +104,39 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number
fcTL.addAll(to4Bytes(seq).asList())
fcTL.addAll(Utils.uIntToByteArray(seq).asList())
// foreach fcTL or fdAT we must increment seq
seq++
// Add width and height
fcTL.addAll(to4Bytes(frames[0].width).asList())
fcTL.addAll(to4Bytes(frames[0].height).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].width).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].height).asList())
// Add offsets
fcTL.addAll(to4Bytes(frames[0].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[0].yOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].xOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].yOffsets).asList())
// Set frame delay
fcTL.addAll(to2Bytes(frames[0].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList())
fcTL.addAll(Utils.uShortToArray(frames[0].delay.toInt()).asList())
fcTL.addAll(Utils.uShortToArray(1000).asList())
// Add DisposeOp and BlendOp
fcTL.add(getDisposeOp(frames[0].disposeOp).toByte())
fcTL.add(getBlendOp(frames[0].blendOp).toByte())
fcTL.add(getDisposeOp(frames[0].disposeOp))
fcTL.add(getBlendOp(frames[0].blendOp))
// Create CRC
val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion
// region idat
frames[0].idat.IDATBody.forEach {
val idat = ArrayList<Byte>()
// Add the chunk body length
framesByte.addAll(to4Bytes(it.size).asList())
framesByte.addAll(Utils.uIntToByteArray(it.size).asList())
// Add IDAT
idat.addAll(byteArrayOf(0x49, 0x44, 0x41, 0x54).asList())
// Add chunk body
@ -147,7 +145,7 @@ class Apng {
val crc1 = CRC32()
crc1.update(idat.toByteArray(), 0, idat.size)
framesByte.addAll(idat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
}
// endregion
res.addAll(framesByte)
@ -159,13 +157,13 @@ class Apng {
idat.parse(PngEncoder().encode(cover!!, true, 1))
idat.IDATBody.forEach {
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(it.asList())
val crc1 = CRC32()
crc1.update(idatByteArray.toByteArray(), 0, idatByteArray.size)
framesByte.addAll(idatByteArray)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
}
// endregion
@ -177,40 +175,40 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number
fcTL.addAll(to4Bytes(seq).asList())
fcTL.addAll(Utils.uIntToByteArray(seq).asList())
seq++
// Add width and height
fcTL.addAll(to4Bytes(frames[0].width).asList())
fcTL.addAll(to4Bytes(frames[0].height).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].width).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].height).asList())
fcTL.addAll(to4Bytes(frames[0].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[0].yOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].xOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[0].yOffsets).asList())
// Set frame delay
fcTL.addAll(to2Bytes(frames[0].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList())
fcTL.addAll(Utils.uShortToArray(frames[0].delay.toInt()).asList())
fcTL.addAll(Utils.uShortToArray(1000).asList())
// Add DisposeOp and BlendOp
fcTL.add(getDisposeOp(frames[0].disposeOp).toByte())
fcTL.add(getBlendOp(frames[0].blendOp).toByte())
fcTL.add(getDisposeOp(frames[0].disposeOp))
fcTL.add(getBlendOp(frames[0].blendOp))
// Generate CRC
val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion
// region fdat
frames[0].idat.IDATBody.forEach {
val fdat = ArrayList<Byte>()
// Add the chunk body length
framesByte.addAll(to4Bytes(it.size + 4).asList())
framesByte.addAll(Utils.uIntToByteArray(it.size + 4).asList())
// Add fdat
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
fdat.addAll(to4Bytes(seq).asList())
fdat.addAll(Utils.uIntToByteArray(seq).asList())
seq++
// Add chunk body
fdat.addAll(it.asList())
@ -218,7 +216,7 @@ class Apng {
val crc1 = CRC32()
crc1.update(fdat.toByteArray(), 0, fdat.size)
framesByte.addAll(fdat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
}
// endregion
res.addAll(framesByte)
@ -234,26 +232,26 @@ class Apng {
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Frame number
fcTL.addAll(to4Bytes(seq).asList())
fcTL.addAll(Utils.uIntToByteArray(seq).asList())
seq++
// width and height
fcTL.addAll(to4Bytes(frames[i].width).asList())
fcTL.addAll(to4Bytes(frames[i].height).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[i].width).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[i].height).asList())
fcTL.addAll(to4Bytes(frames[i].xOffsets).asList())
fcTL.addAll(to4Bytes(frames[i].yOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[i].xOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(frames[i].yOffsets).asList())
// Set frame delay
fcTL.addAll(to2Bytes(frames[i].delay.toInt()).asList())
fcTL.addAll(to2Bytes(1000).asList())
fcTL.addAll(Utils.uShortToArray(frames[i].delay.toInt()).asList())
fcTL.addAll(Utils.uShortToArray(1000).asList())
fcTL.add(getDisposeOp(frames[i].disposeOp).toByte())
fcTL.add(getBlendOp(frames[i].blendOp).toByte())
fcTL.add(getDisposeOp(frames[i].disposeOp))
fcTL.add(getBlendOp(frames[i].blendOp))
val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size)
framesByte.addAll(fcTL)
framesByte.addAll(to4Bytes(crc.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
// endregion
// region fdAT
@ -261,12 +259,12 @@ class Apng {
frames[i].idat.IDATBody.forEach {
val fdat = ArrayList<Byte>()
// 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
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
// Add Sequence number
// ! THIS IS NOT FRAME NUMBER
fdat.addAll(to4Bytes(seq).asList())
fdat.addAll(Utils.uIntToByteArray(seq).asList())
// Increase seq
seq++
fdat.addAll(it.asList())
@ -274,7 +272,7 @@ class Apng {
val crc1 = CRC32()
crc1.update(fdat.toByteArray(), 0, fdat.size)
framesByte.addAll(fdat)
framesByte.addAll(to4Bytes(crc1.value.toInt()).asList())
framesByte.addAll(Utils.uIntToByteArray(crc1.value.toInt()).asList())
}
// endregion
res.addAll(framesByte)
@ -282,14 +280,14 @@ class Apng {
if (frames.isNotEmpty()) {
// Add IEND body length : 0
res.addAll(to4Bytes(0).asList())
res.addAll(Utils.uIntToByteArray(0).asList())
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
res.addAll(iend.asList())
res.addAll(to4Bytes(crC32.value.toInt()).asList())
res.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return res.toByteArray()
} else {
throw NoFrameException()
@ -328,13 +326,13 @@ class Apng {
}
// 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
ihdrBody.addAll(byteArrayOf(0x49.toByte(), 0x48.toByte(), 0x44.toByte(), 0x52.toByte()).asList())
// Add the max width and height
ihdrBody.addAll(to4Bytes(maxWidth!!).asList())
ihdrBody.addAll(to4Bytes(maxHeight!!).asList())
ihdrBody.addAll(Utils.uIntToByteArray(maxWidth!!).asList())
ihdrBody.addAll(Utils.uIntToByteArray(maxHeight!!).asList())
// Add complicated stuff like depth color ...
// 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()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody)
ihdr.addAll(to4Bytes(crC32.value.toInt()).asList())
ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray()
}
@ -357,22 +355,22 @@ class Apng {
val actl = ArrayList<Byte>()
// Add length bytes
res.addAll(to4Bytes(8).asList())
res.addAll(Utils.uIntToByteArray(8).asList())
// Add acTL
actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList())
// Add number of frames
actl.addAll(to4Bytes(frames.size).asList())
actl.addAll(Utils.uIntToByteArray(frames.size).asList())
// Number of repeat, 0 to infinite
actl.addAll(to4Bytes(0).asList())
actl.addAll(Utils.uIntToByteArray(0).asList())
res.addAll(actl)
// generate crc
val crc = CRC32()
crc.update(actl.toByteArray(), 0, actl.size)
res.addAll(to4Bytes(crc.value.toInt()).asList())
res.addAll(Utils.uIntToByteArray(crc.value.toInt()).asList())
return res
}

View File

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

View File

@ -88,30 +88,30 @@ class ApngEncoder(
i.toByteArray()
}
// Add the chunk body length
outputStream.write(Utils.to4BytesArray(idatBody.size))
outputStream.write(Utils.uIntToByteArray(idatBody.size))
// Generate CRC
val crc1 = CRC32()
crc1.update(idatChunk, 0, idatChunk.size)
outputStream.write(idatChunk)
outputStream.write(Utils.to4BytesArray(crc1.value.toInt()))
outputStream.write(Utils.uIntToByteArray(crc1.value.toInt()))
} else {
val fdat = ArrayList<Byte>().let { fdat ->
fdat.addAll(byteArrayOf(0x66, 0x64, 0x41, 0x54).asList())
// Add fdat
fdat.addAll(Utils.to4Bytes(seq++).asList())
fdat.addAll(Utils.uIntToByteArray(seq++).asList())
// Add chunk body
fdat.addAll(idatBody.asList())
fdat.toByteArray()
}
// Add the chunk body length
outputStream.write(Utils.to4BytesArray(idatBody.size + 4))
outputStream.write(Utils.uIntToByteArray(idatBody.size + 4))
// Generate CRC
val crc1 = CRC32()
crc1.update(fdat, 0, fdat.size)
outputStream.write(fdat)
outputStream.write(Utils.to4BytesArray(crc1.value.toInt()))
outputStream.write(Utils.uIntToByteArray(crc1.value.toInt()))
}
}
frameIndex++
@ -123,14 +123,14 @@ class ApngEncoder(
@Suppress("unused")
fun writeEnd() {
// Add IEND body length : 0
outputStream.write(Utils.to4BytesArray(0))
outputStream.write(Utils.uIntToByteArray(0))
// Add IEND
val iend = byteArrayOf(0x49, 0x45, 0x4E, 0x44)
// Generate crc for IEND
val crC32 = CRC32()
crC32.update(iend, 0, iend.size)
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))
// Add the max width and height
ihdrBody.addAll(Utils.to4Bytes(width).asList())
ihdrBody.addAll(Utils.to4Bytes(height).asList())
ihdrBody.addAll(Utils.uIntToByteArray(width).asList())
ihdrBody.addAll(Utils.uIntToByteArray(height).asList())
ihdrBody.add(8.toByte())
ihdrBody.add(6.toByte())
@ -168,7 +168,7 @@ class ApngEncoder(
val crC32 = CRC32()
crC32.update(ihdrBody.toByteArray(), 0, ihdrBody.size)
ihdr.addAll(ihdrBody)
ihdr.addAll(Utils.to4Bytes(crC32.value.toInt()).asList())
ihdr.addAll(Utils.uIntToByteArray(crC32.value.toInt()).asList())
return ihdr.toByteArray()
}
@ -187,16 +187,16 @@ class ApngEncoder(
actl.addAll(byteArrayOf(0x61, 0x63, 0x54, 0x4c).asList())
// Add number of frames
actl.addAll(Utils.to4Bytes(num).asList())
actl.addAll(Utils.uIntToByteArray(num).asList())
// Number of repeat, 0 to infinite
actl.addAll(Utils.to4Bytes(0).asList())
actl.addAll(Utils.uIntToByteArray(0).asList())
res.addAll(actl)
// generate crc
val crc = CRC32()
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()
}
@ -210,28 +210,28 @@ class ApngEncoder(
fcTL.addAll(byteArrayOf(0x66, 0x63, 0x54, 0x4c).asList())
// Add the frame number
fcTL.addAll(Utils.to4Bytes(seq++).asList())
fcTL.addAll(Utils.uIntToByteArray(seq++).asList())
// Add width and height
fcTL.addAll(Utils.to4Bytes(btm.width).asList())
fcTL.addAll(Utils.to4Bytes(btm.height).asList())
fcTL.addAll(Utils.uIntToByteArray(btm.width).asList())
fcTL.addAll(Utils.uIntToByteArray(btm.height).asList())
// Add offsets
fcTL.addAll(Utils.to4Bytes(xOffsets).asList())
fcTL.addAll(Utils.to4Bytes(yOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(xOffsets).asList())
fcTL.addAll(Utils.uIntToByteArray(yOffsets).asList())
// Set frame delay
fcTL.addAll(Utils.to2Bytes(delay.toInt().toShort()).asList())
fcTL.addAll(Utils.to2Bytes(1000.toShort()).asList())
fcTL.addAll(Utils.uShortToByteArray(delay.toInt().toShort()).asList())
fcTL.addAll(Utils.uShortToByteArray(1000.toShort()).asList())
// Add DisposeOp and BlendOp
fcTL.add(Utils.getDisposeOp(disposeOp).toByte())
fcTL.add(Utils.getBlendOp(blendOp).toByte())
fcTL.add(Utils.getDisposeOp(disposeOp))
fcTL.add(Utils.getBlendOp(blendOp))
// Create CRC
val crc = CRC32()
crc.update(fcTL.toByteArray(), 0, fcTL.size)
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.update(iend, 0, iend.size)
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() {
writeInt4(13)
val header = Utils.IHDR
.plus(Utils.to4BytesArray(width))
.plus(Utils.to4BytesArray(height))
.plus(Utils.uIntToByteArray(width))
.plus(Utils.uIntToByteArray(height))
.plus(8) // bit depth
.plus(if (encodeAlpha) 6 else 2) // direct model
.plus(0) // compression method
@ -309,15 +309,15 @@ class ExperimentalApngEncoder(
// Add acTL
val acTL = byteArrayOf(0x61, 0x63, 0x54, 0x4c)
// Add number of frames
.plus(Utils.to4BytesArray(num))
.plus(Utils.uIntToByteArray(num))
// Number of repeat, 0 to infinite
.plus(Utils.to4BytesArray(repetitionCount))
.plus(Utils.uIntToByteArray(repetitionCount))
outputStream.write(acTL)
// generate crc
crc.reset()
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
val fcTL = Utils.fcTL
// Add the frame number
.plus(Utils.to4BytesArray(currentSeq++))
.plus(Utils.uIntToByteArray(currentSeq++))
// Add width and height
.plus(Utils.to4BytesArray(btm.width))
.plus(Utils.to4BytesArray(btm.height))
.plus(Utils.uIntToByteArray(btm.width))
.plus(Utils.uIntToByteArray(btm.height))
// Add offsets
.plus(Utils.to4BytesArray(xOffsets))
.plus(Utils.to4BytesArray(yOffsets))
.plus(Utils.uIntToByteArray(xOffsets))
.plus(Utils.uIntToByteArray(yOffsets))
// Set frame delay
// TODO BETTER FRACTION
.plus(Utils.to2Bytes(delay.toInt().toShort()))
.plus(Utils.to2Bytes(1000.toShort()))
.plus(Utils.uShortToByteArray(delay.toInt().toShort()))
.plus(Utils.uShortToByteArray(1000.toShort()))
// Add DisposeOp and BlendOp
.plus(Utils.getDisposeOp(disposeOp))
@ -364,7 +364,7 @@ class ExperimentalApngEncoder(
// Write all
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)
} else { // Write a fdAT chunk, containing the current sequence number
// Add fdat and sequence number
val fdat = Utils.fdAT+ Utils.to4BytesArray(currentSeq++)
val fdat = Utils.fdAT+ Utils.uIntToByteArray(currentSeq++)
outputStream.write(fdat)
crc.update(fdat)

View File

@ -141,8 +141,7 @@ class Utils {
* @param i The int
* @return [Array] 4 Bytes
*/
// TODO RENAME
fun to4Bytes(i: Int): Array<Byte> {
fun uShortToArray(i: Int): Array<Byte> {
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
* @return [ByteArray] 4 Bytes
*/
// TODO RENAME
fun to4BytesArray(i: Int): ByteArray {
fun uIntToByteArray(i: Int): ByteArray {
return byteArrayOf(
(i shr 24).toByte(),
(i shr 16).toByte(),
@ -166,13 +164,16 @@ class Utils {
* @param i The int
* @return [ByteArray] 2 Bytes
*/
// TODO RENAME
fun to2Bytes(i: Int): ByteArray {
fun uShortToByteArray(i: Int): ByteArray {
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())
}