Small improvements
This commit is contained in:
parent
ec67f6e09f
commit
4391b6a3ed
|
@ -9,9 +9,8 @@ import oupson.apng.chunks.fcTL
|
||||||
import oupson.apng.exceptions.NotApngException
|
import oupson.apng.exceptions.NotApngException
|
||||||
import java.util.zip.CRC32
|
import java.util.zip.CRC32
|
||||||
|
|
||||||
class APNGDisassembler() {
|
class APNGDisassembler {
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun disassemble(byteArray: ByteArray) : Apng {
|
fun disassemble(byteArray: ByteArray) : Apng {
|
||||||
val pngList = ArrayList<Frame>()
|
val pngList = ArrayList<Frame>()
|
||||||
var png: ArrayList<Byte>? = null
|
var png: ArrayList<Byte>? = null
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class PnnQuantizer {
|
||||||
protected int width, height;
|
protected int width, height;
|
||||||
protected int pixels[] = null;
|
protected int pixels[] = null;
|
||||||
private Integer m_transparentColor;
|
private Integer m_transparentColor;
|
||||||
private Map<Integer, short[]> closestMap = new HashMap();
|
private HashMap<Integer, short[]> closestMap = new HashMap();
|
||||||
|
|
||||||
public PnnQuantizer(String fname) throws IOException {
|
public PnnQuantizer(String fname) throws IOException {
|
||||||
fromBitmap(fname);
|
fromBitmap(fname);
|
||||||
|
@ -156,7 +156,7 @@ public class PnnQuantizer {
|
||||||
/* Merge bins which increase error the least */
|
/* Merge bins which increase error the least */
|
||||||
int extbins = maxbins - nMaxColors;
|
int extbins = maxbins - nMaxColors;
|
||||||
for (int i = 0; i < extbins; ) {
|
for (int i = 0; i < extbins; ) {
|
||||||
Pnnbin tb = null;
|
Pnnbin tb;
|
||||||
/* Use heap to find which bins to merge */
|
/* Use heap to find which bins to merge */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int b1 = heap[1];
|
int b1 = heap[1];
|
||||||
|
@ -253,7 +253,7 @@ public class PnnQuantizer {
|
||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
|
|
||||||
private short closestColorIndex(final Integer[] palette, final int[] squares3, final int c)
|
private short closestColorIndex(final Integer[] palette, final int c)
|
||||||
{
|
{
|
||||||
short k = 0;
|
short k = 0;
|
||||||
short[] closest = new short[5];
|
short[] closest = new short[5];
|
||||||
|
@ -407,7 +407,7 @@ public class PnnQuantizer {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (int i = 0; i < qPixels.length; i++)
|
for (int i = 0; i < qPixels.length; i++)
|
||||||
qPixels[i] = palette[closestColorIndex(palette, squares3, pixels[i])];
|
qPixels[i] = palette[closestColorIndex(palette, pixels[i])];
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -2,39 +2,28 @@ package oupson.apng
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import java.io.BufferedInputStream
|
import java.io.BufferedInputStream
|
||||||
import java.io.ByteArrayOutputStream
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
class Loader {
|
class Loader {
|
||||||
companion object {
|
companion object {
|
||||||
|
@Throws(IOException::class)
|
||||||
fun load(context: Context, url: URL): ByteArray {
|
fun load(context: Context, url: URL): ByteArray {
|
||||||
val currenDir = context.filesDir
|
val currentDir = context.filesDir
|
||||||
val fileTXT = File(currenDir, "apngLoader.txt")
|
val fileTXT = File(currentDir, "apngLoader.txt")
|
||||||
var filePNG = File(currenDir, "apngLoader.png")
|
val filePNG = File(currentDir, "apngLoader.png")
|
||||||
if (fileTXT.exists() && url.toString() == fileTXT.readText()) {
|
return if (fileTXT.exists() && url.toString() == fileTXT.readText()) {
|
||||||
return filePNG.readBytes()
|
filePNG.readBytes()
|
||||||
} else {
|
} else {
|
||||||
try {
|
val connection = url.openConnection()
|
||||||
val connection = url.openConnection()
|
connection.connect()
|
||||||
connection.connect()
|
val input = BufferedInputStream(connection.getInputStream())
|
||||||
val input = BufferedInputStream(connection.getInputStream())
|
val bytes = input.readBytes()
|
||||||
val output = ByteArrayOutputStream()
|
input.close()
|
||||||
val data = ByteArray(1024)
|
fileTXT.writeText(url.toString())
|
||||||
var count = 0
|
filePNG.writeBytes(bytes)
|
||||||
while ({ count = input.read(data); count }() != -1) {
|
bytes
|
||||||
output.write(data, 0, count)
|
|
||||||
}
|
|
||||||
output.flush()
|
|
||||||
output.close()
|
|
||||||
input.close()
|
|
||||||
fileTXT.writeText(url.toString())
|
|
||||||
filePNG.writeBytes(output.toByteArray())
|
|
||||||
return output.toByteArray()
|
|
||||||
} catch (e: IOException) {
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,12 +34,11 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
doAsync {
|
doAsync {
|
||||||
Loader.load(applicationContext, URL(imageUrl)).apply {
|
Loader.load(applicationContext, URL(imageUrl)).apply {
|
||||||
val a = APNGDisassembler.disassemble(this)
|
val a = APNGDisassembler.disassemble(this)
|
||||||
a.reduceSize(100, false, 60)
|
a.reduceSize(100, false, 60)
|
||||||
File(File(Environment.getExternalStorageDirectory(), "Documents"), "apng.png").writeBytes(a.toByteArray())
|
File(File(Environment.getExternalStorageDirectory(), "Download"), "apng.png").writeBytes(a.toByteArray())
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
toast("Converted ! ")
|
toast("Converted ! ")
|
||||||
}
|
}
|
||||||
|
@ -47,18 +46,13 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
this.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||||
internal var progress = 0
|
|
||||||
|
|
||||||
// When Progress value changed.
|
|
||||||
override fun onProgressChanged(seekBar: SeekBar, progressValue: Int, fromUser: Boolean) {
|
override fun onProgressChanged(seekBar: SeekBar, progressValue: Int, fromUser: Boolean) {
|
||||||
progress = progressValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notification that the user has started a touch gesture.
|
|
||||||
override fun onStartTrackingTouch(seekBar: SeekBar) {
|
override fun onStartTrackingTouch(seekBar: SeekBar) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notification that the user has finished a touch gesture
|
|
||||||
override fun onStopTrackingTouch(seekBar: SeekBar) {
|
override fun onStopTrackingTouch(seekBar: SeekBar) {
|
||||||
Log.e("TAG" , (seekBar.progress.toFloat() / 100f).toString())
|
Log.e("TAG" , (seekBar.progress.toFloat() / 100f).toString())
|
||||||
animator.speed = (seekBar.progress.toFloat() / 100f)
|
animator.speed = (seekBar.progress.toFloat() / 100f)
|
||||||
|
|
Loading…
Reference in New Issue