generated from vk-education/common
-
Notifications
You must be signed in to change notification settings - Fork 5
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
mrGnost
wants to merge
2
commits into
vk-education:master
Choose a base branch
from
mrGnost:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/example/fancywork/PixelizationAlgorithm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше запускать отдельное корутине/потоке