diff --git a/app/build.gradle b/app/build.gradle index 45bf9c5..8c91cc1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/example/fancywork/MainActivity.kt b/app/src/main/java/com/example/fancywork/MainActivity.kt index 23dc963..5956c4b 100644 --- a/app/src/main/java/com/example/fancywork/MainActivity.kt +++ b/app/src/main/java/com/example/fancywork/MainActivity.kt @@ -7,9 +7,13 @@ import androidx.appcompat.app.AppCompatActivity // obviously we haven't done anything yet class MainActivity : AppCompatActivity() { + lateinit var colors: List>> + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + // Reading thread colors dictionary from resources. + colors = PixelizationAlgorithm.getThreadColors(resources) } // todo for butten download image diff --git a/app/src/main/java/com/example/fancywork/PixelizationAlgorithm.kt b/app/src/main/java/com/example/fancywork/PixelizationAlgorithm.kt new file mode 100644 index 0000000..3c65b73 --- /dev/null +++ b/app/src/main/java/com/example/fancywork/PixelizationAlgorithm.kt @@ -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 { + companion object { + // Method for getting thread colors from resources. + fun getThreadColors(resources: Resources): List>> { + 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>> { + 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(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 { + return Triple( + (color shr 16) and 0xff, + (color shr 8) and 0xff, + color and 0xff + ) + } + + private fun findDistance(x: Triple, colorsAv: Triple): 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)) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/raw/colors.csv b/app/src/main/res/raw/colors.csv new file mode 100644 index 0000000..385133d --- /dev/null +++ b/app/src/main/res/raw/colors.csv @@ -0,0 +1,425 @@ +DMC,Red,Green,Blue +208,159,72,178 +209,191,116,202 +210,208,149,228 +211,222,179,235 +221,145,41,52 +223,195,92,118 +224,225,148,157 +225,243,198,208 +256,207,91,82 +300,121,48,34 +301,175,76,33 +3011,114,97,50 +3012,143,124,64 +3013,172,161,104 +3021,87,66,57 +3022,142,130,113 +3023,172,157,141 +3024,195,190,186 +3031,83,56,51 +3032,163,133,107 +3033,220,197,185 +304,190,15,43 +3041,144,99,126 +3042,181,142,175 +3045,187,139,88 +3046,209,178,108 +3047,228,212,150 +3051,92,90,53 +3052,128,132,89 +3053,150,153,105 +3064,207,128,108 +307,229,207,7 +3072,207,206,199 +3078,236,227,140 +309,206,29,83 +310,42,38,40 +311,58,65,107 +312,66,84,139 +315,145,64,89 +316,199,107,157 +317,103,96,111 +318,157,151,172 +319,50,77,52 +320,99,136,93 +321,202,5,32 +322,94,119,181 +326,200,16,58 +327,124,47,119 +3325,161,184,221 +3326,243,143,182 +3328,223,70,85 +333,116,65,169 +334,110,136,185 +3340,244,80,77 +3341,244,124,127 +3345,61,85,44 +3346,93,116,58 +3347,108,133,70 +3348,158,178,96 +335,234,68,128 +3350,207,45,106 +3354,236,138,183 +336,59,55,96 +3362,82,93,65 +3363,105,126,89 +3364,138,151,98 +3371,57,47,48 +340,153,129,212 +341,164,163,214 +347,216,36,50 +349,206,8,43 +350,233,66,65 +351,241,93,91 +352,245,144,137 +353,246,187,176 +355,174,48,48 +3607,215,63,151 +3608,239,122,199 +3609,242,152,219 +367,78,116,77 +368,135,166,118 +3685,158,42,91 +3687,202,64,125 +3688,241,162,203 +3689,241,162,203 +369,182,208,163 +370,156,127,72 +3705,240,50,93 +3706,248,83,139 +3708,195,9,28 +371,156,130,74 +3712,235,95,110 +3713,246,187,205 +3716,244,145,193 +372,175,155,103 +3721,166,58,73 +3722,183,78,98 +3726,159,80,105 +3727,215,140,177 +3731,217,67,130 +3733,235,106,159 +3740,125,78,99 +3743,206,179,207 +3746,129,92,187 +3747,198,193,229 +3750,56,67,97 +3752,169,186,209 +3753,191,206,224 +3755,137,166,207 +3756,220,228,234 +3760,90,134,171 +3761,172,211,222 +3765,60,109,142 +3766,135,187,198 +3768,95,115,120 +3770,246,224,207 +3772,174,96,90 +3773,212,139,128 +3774,239,197,182 +3776,214,104,52 +3777,156,35,45 +3778,223,116,106 +3779,240,168,161 +3781,109,74,60 +3782,185,154,132 +3787,108,90,79 +3790,131,99,83 +3799,76,65,71 +3801,243,29,72 +3802,127,47,76 +3803,158,42,91 +3804,218,15,116 +3806,236,85,167 +3807,109,102,170 +3808,46,92,90 +3809,59,114,123 +3810,87,144,154 +3811,171,212,212 +3812,18,154,106 +3813,157,187,176 +3814,62,142,108 +3815,86,133,102 +3816,112,158,133 +3817,149,184,164 +3818,16,97,43 +3819,191,193,24 +3820,222,161,29 +3821,228,180,53 +3822,239,202,91 +3823,238,223,174 +3824,245,142,145 +3825,243,150,113 +3826,179,84,40 +3827,229,155,94 +3828,188,131,76 +3829,174,109,44 +3830,197,69,63 +3831,205,36,75 +3832,229,52,106 +3833,237,89,138 +3834,120,48,101 +3835,155,80,142 +3836,192,119,178 +3837,142,50,164 +3838,109,105,189 +3839,130,132,206 +3840,168,183,233 +3841,184,203,225 +3842,60,87,134 +3843,19,127,202 +3844,7,146,178 +3845,11,182,211 +3846,55,200,225 +3847,42,108,89 +3848,68,141,122 +3849,101,166,150 +3850,13,148,80 +3851,76,184,126 +3852,207,136,14 +3853,225,106,37 +3854,238,145,74 +3855,241,194,125 +3856,238,172,127 +3857,125,50,48 +3858,148,69,71 +3859,198,114,108 +3860,139,91,100 +3861,167,123,133 +3862,136,89,71 +3863,159,109,92 +3864,194,151,137 +3865,248,247,245 +3866,229,215,209 +400,147,53,28 +402,235,138,95 +407,199,124,118 +413,86,82,91 +414,124,114,134 +415,180,175,193 +420,162,100,54 +422,208,160,100 +433,125,63,46 +434,158,81,48 +435,185,110,55 +436,208,140,84 +437,218,159,108 +444,225,181,7 +445,231,232,89 +451,147,116,125 +452,182,149,162 +453,205,183,186 +469,86,100,42 +470,116,133,44 +471,132,143,72 +472,174,185,96 +498,184,15,40 +500,48,67,58 +501,76,107,95 +502,104,139,124 +503,135,171,159 +504,179,209,193 +517,76,110,163 +518,104,154,179 +519,145,185,210 +520,74,91,69 +522,129,140,115 +523,150,156,129 +524,161,164,135 +535,93,86,87 +543,229,197,188 +550,96,25,97 +552,150,55,157 +553,170,78,176 +554,212,135,210 +561,59,107,85 +562,90,148,101 +563,135,193,147 +564,160,209,166 +580,92,99,38 +581,123,137,43 +597,113,167,173 +598,151,198,197 +600,209,6,88 +601,225,7,109 +602,234,32,132 +603,241,76,163 +604,242,119,191 +605,245,153,213 +606,225,2,14 +608,242,5,40 +610,114,86,64 +611,143,106,78 +612,174,141,100 +613,207,184,160 +632,146,69,62 +640,139,113,90 +642,160,139,116 +644,203,186,165 +645,111,100,96 +646,125,117,114 +647,153,145,133 +648,178,170,164 +666,230,5,25 +676,230,183,109 +677,236,214,157 +680,178,117,50 +699,9,105,35 +700,33,128,43 +701,49,140,46 +702,74,156,47 +703,108,177,56 +704,128,184,43 +712,240,228,213 +718,197,21,117 +720,222,60,20 +721,238,84,42 +722,244,123,83 +725,234,173,32 +726,241,199,28 +727,239,219,94 +729,205,143,72 +730,110,90,28 +731,129,110,22 +732,125,105,20 +733,162,140,34 +734,181,160,63 +738,214,171,132 +739,230,199,169 +740,234,192,7 +741,242,106,6 +742,243,139,23 +743,242,179,56 +744,242,201,104 +745,240,211,143 +746,246,242,210 +747,198,233,235 +754,240,180,165 +758,234,151,136 +760,244,122,143 +761,244,159,176 +762,215,211,226 +772,185,211,149 +775,198,214,228 +776,244,160,193 +778,222,159,180 +780,162,75,33 +781,180,100,17 +782,188,108,14 +783,207,124,33 +791,71,47,122 +792,94,77,158 +793,119,119,177 +794,146,159,204 +796,56,45,132 +797,72,67,150 +798,82,97,183 +799,121,141,201 +800,166,191,223 +801,117,61,47 +806,87,133,165 +807,101,160,173 +809,142,158,216 +813,127,163,201 +814,140,28,50 +815,159,28,42 +816,173,33,49 +817,195,9,28 +818,247,204,223 +819,246,223,232 +820,48,34,120 +822,226,210,189 +823,50,43,69 +824,62,79,144 +825,69,97,161 +826,93,129,184 +827,165,196,224 +828,186,214,224 +829,111,72,38 +830,122,81,38 +831,139,102,36 +832,164,122,37 +833,177,140,55 +834,199,165,79 +838,95,58,52 +839,109,69,61 +840,147,104,91 +841,171,130,120 +842,206,173,163 +844,80,71,69 +869,134,82,47 +890,37,64,39 +891,242,15,91 +892,245,44,111 +893,245,67,137 +894,246,109,174 +895,51,75,46 +898,101,57,47 +899,238,89,152 +900,200,22,39 +902,129,34,53 +904,64,100,39 +905,74,123,26 +906,81,150,8 +907,124,172,13 +909,9,117,36 +910,21,136,44 +911,49,157,63 +912,83,179,97 +913,107,191,110 +915,155,16,75 +917,180,31,110 +918,163,50,37 +919,176,49,28 +920,184,58,32 +921,208,79,39 +922,231,103,53 +924,67,84,91 +926,126,140,145 +927,163,177,177 +928,187,193,194 +930,70,81,108 +931,104,121,150 +932,141,158,184 +934,56,56,44 +935,67,71,52 +936,75,78,45 +937,75,93,40 +938,84,50,47 +939,50,38,59 +943,38,165,102 +945,239,180,153 +946,231,24,19 +947,234,13,35 +948,243,209,200 +950,235,184,171 +951,241,202,182 +954,131,203,127 +955,167,226,160 +956,245,44,139 +957,248,125,190 +958,79,187,142 +959,103,199,161 +961,233,75,135 +962,240,100,161 +963,246,185,220 +964,151,224,199 +966,152,198,154 +970,244,69,10 +972,236,146,6 +973,234,192,7 +975,141,61,40 +976,203,106,44 +977,216,130,65 +986,53,94,47 +987,79,113,61 +988,101,137,68 +989,115,153,83 +991,47,122,91 +992,94,173,136 +993,116,192,153 +995,16,108,190 +996,75,166,231 +b5200,244,244,246 +blanc,247,247,249