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

Added the algorithm and thread colors dictionary #17

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'io.uuddlrlrba:close-pixelate:1.2.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/example/fancywork/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import androidx.appcompat.app.AppCompatActivity

// obviously we haven't done anything yet
class MainActivity : AppCompatActivity() {
lateinit var colors: List<Pair<String, Triple<Int, Int, Int>>>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Reading thread colors dictionary from resources.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше запускать отдельное корутине/потоке

colors = PixelizationAlgorithm.getThreadColors(resources)
}

// todo for butten download image
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.example.fancywork

import android.content.res.Resources
import android.graphics.Bitmap
import io.uuddlrlrba.closepixelate.Pixelate
import io.uuddlrlrba.closepixelate.PixelateLayer
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow

class PixelizationAlgorithm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class PixelizationAlgorithm {
    companion object {
object PixelizationAlgorithm {
} ```

companion object {
// Method for getting thread colors from resources.
fun getThreadColors(resources: Resources): List<Pair<String, Triple<Int, Int, Int>>> {
val stream = resources.openRawResource(R.raw.colors)
val colors = stream
.bufferedReader()
.readLines()
.drop(1)
.map { x -> x.split(",") }
.map { x -> x[0] to Triple(x[1].toInt(), x[2].toInt(), x[3].toInt()) }
stream.close()
return colors
}

// This method makes a pixelated bitmap from image bitmap and provides an array of thread codes.
fun getPixelsFromImage(
bitmap: Bitmap,
pixelSize: Int,
colors: List<Pair<String, Triple<Int, Int, Int>>>):
Pair<Bitmap, Array<Array<String?>>> {
val pixelatedBitmap = Pixelate.fromBitmap(
bitmap,
PixelateLayer.Builder(PixelateLayer.Shape.Square)
.setSize(pixelSize.toFloat())
.setEnableDominantColors(true)
.build()
)
val pixelatedWidth = ceil(bitmap.width.toDouble() / pixelSize).toInt()
val pixelatedHeight = ceil(bitmap.height.toDouble() / pixelSize).toInt()
val threadCodes = Array(pixelatedWidth) {
arrayOfNulls<String>(pixelatedHeight)
}
val bitmapColors = IntArray(pixelatedWidth * pixelatedHeight)
for (i in 0 until bitmap.width step pixelSize)
for (j in 0 until bitmap.height step pixelSize) {
val pixel = pixelatedBitmap.getPixel(i, j)
val pixelColor = colorToTriple(pixel)
val mainColor = colors.minByOrNull { x -> findDistance(x.second, pixelColor) }!!
val mainRGB = (mainColor.second.first shl 16) +
(mainColor.second.second shl 8) + mainColor.second.third
threadCodes[i / pixelSize][j / pixelSize] = mainColor.first
bitmapColors[j / pixelSize * pixelatedWidth + i / pixelSize] = mainRGB
}
val resultBitmap =
Bitmap.createBitmap(bitmapColors, pixelatedWidth, pixelatedHeight, Bitmap.Config.RGB_565)
return resultBitmap to threadCodes
}

private fun colorToTriple(color: Int): Triple<Int, Int, Int> {
return Triple(
(color shr 16) and 0xff,
(color shr 8) and 0xff,
color and 0xff
)
}

private fun findDistance(x: Triple<Int, Int, Int>, colorsAv: Triple<Int, Int, Int>): Double {
return (((1 + max(x.first, colorsAv.first)).toDouble() / (1 + min(
x.first,
colorsAv.first
))).pow(2)
+ ((1 + max(x.second, colorsAv.second)).toDouble() / (1 + min(
x.second,
colorsAv.second
))).pow(2)
+ ((1 + max(x.third, colorsAv.third)).toDouble() / (1 + min(
x.third,
colorsAv.third
))).pow(2))
}
}
}
Loading