Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAINTROID-556-ADDED NEW SHAPES #1334

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ object FileIO {
PNG("png"),
JPG("jpg"),
ORA("ora"),
CATROBAT("catrobat-image");
CATROBAT("catrobat-image"),
GIF("gif");

fun toExtension(): String = ".$value"
}
Expand Down Expand Up @@ -132,7 +133,7 @@ object FileIO {
}

@Throws(IOException::class)
fun saveBitmapToUri(uri: Uri, bitmap: Bitmap?, context: Context): Uri {
fun <MainActivity> saveBitmapToUri(uri: Uri, bitmap: Bitmap?, context: Context): Uri {
val uid = UUID.randomUUID()
val cachedImageUri = saveBitmapToCache(bitmap, context as MainActivity, uid.toString())
var cachedFile: File? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@
*/
package org.catrobat.paintroid

import android.graphics.Bitmap
import org.catrobat.paintroid.ui.DrawingSurface
import java.io.File
import java.lang.IllegalArgumentException

@SuppressWarnings("ThrowingExceptionsWithoutMessageOrCause")
class PaintroidApplication private constructor() {
companion object {
@JvmStatic
var cacheDir: File? = null
}
class PaintroidApplication() {
object DrawingSurface {
fun copyBitmap(): Bitmap {

init {
throw IllegalArgumentException()
return Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
}
}
}





//@JvmStatic
// var cacheDir: File? = null
// }
//
//
// init {
// throw IllegalArgumentException()
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ interface MainActivityContracts {
fun checkForTemporaryFile(): Boolean

fun setColorHistoryAfterLoadImage(colorHistory: ColorHistory?)
fun showGifInformationDialog()
}

interface Model {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package org.catrobat.paintroid.dialog



import android.graphics.Bitmap
import android.graphics.Color
import java.io.BufferedOutputStream
import java.io.FileOutputStream
import java.io.IOException

class AnimatedGifEncoder {

private var width: Int = 0
private var height: Int = 0
private lateinit var indexedPixels: ByteArray
private var colorDepth: Int = 0
private lateinit var colorTab: ByteArray
private lateinit var usedEntry: BooleanArray
private var palSize: Int = 0
private var dispose: Int = -1
private var closeStream: Boolean = false
private var firstFrame: Boolean = true
private var sample: Int = 10
private var delay: Int = 0
private lateinit var outputStream: FileOutputStream
private lateinit var bufferedOutputStream: BufferedOutputStream
private var transIndex: Int = 0
private var started: Boolean = false

fun start(fileOutputStream: FileOutputStream) {
if (!started) {
outputStream = fileOutputStream
bufferedOutputStream = BufferedOutputStream(outputStream)
writeString("GIF89a") // header
started = true
}
}

fun setDelay(ms: Int) {
delay = Math.round(ms / 10.0f)
}

fun addFrame(image: Bitmap) {
if (!started || image == null) return

try {
if (!firstFrame) {
writeByte(0x21)
writeByte(0xF9)
writeByte(4)
writeByte((dispose shl 2) or 0x01)
writeShort(delay)
writeByte(transIndex)
writeByte(0)
}
val transparent = -1
val width = image.width
val height = image.height
if (width != this.width || height != this.height) {
throw IllegalArgumentException("Gif frame dimension mismatch")
}
val pixels = IntArray(width * height)
image.getPixels(pixels, 0, width, 0, 0, width, height)
if (firstFrame) {
indexedPixels = ByteArray(width * height)
usedEntry = BooleanArray(256)
var nPix = 0
for (i in 0 until height) {
for (j in 0 until width) {
val idx = i * width + j
val c = pixels[idx]
val r = Color.red(c)
val g = Color.green(c)
val b = Color.blue(c)
if (r == 255 && g == 255 && b == 255) {
transIndex = idx
}
if (!usedEntry[c]) {
usedEntry[c] = true
nPix++
}
indexedPixels[idx] = c.toByte()
}
}
palSize = nPix
colorDepth = (Math.log(palSize.toDouble()) / Math.log(2.0) + 0.5).toInt()
if (transIndex != -1) {
transIndex = findClosest(transIndex)
}
}
// Further processing logic for pixels and palette
// Write pixels to GIF
// Write palette to GIF
// Write extension blocks if not first frame
firstFrame = false
} catch (e: IOException) {
e.printStackTrace()
}
}

fun finish() {
if (!started) return

try {
bufferedOutputStream.write(0x3b) // gif trailer
bufferedOutputStream.flush()
if (closeStream) {
bufferedOutputStream.close()
}
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
started = false
}

private fun writeByte(value: Int) {
bufferedOutputStream.write(value)
}

private fun writeShort(value: Int) {
bufferedOutputStream.write(value and 0xff)
bufferedOutputStream.write((value shr 8) and 0xff)
}

private fun writeString(string: String) {
for (i in 0 until string.length) {
bufferedOutputStream.write(string[i].toInt())
}
}

private fun findClosest(color: Int): Int {
var minpos = 0
var dmin = 256 * 256 * 256
val len = colorTab.size
for (i in 0 until len step 3) {
val dr = Color.red(color) - (colorTab[i].toInt() and 0xff)
val dg = Color.green(color) - (colorTab[i + 1].toInt() and 0xff)
val db = Color.blue(color) - (colorTab[i + 2].toInt() and 0xff)
val d = dr * dr + dg * dg + db * db
val index = i / 3
if (usedEntry[index] && d < dmin) {
dmin = d
minpos = index
}
}
return minpos

}
}

Loading