diff --git a/JavaDecoder/app/build.gradle b/JavaDecoder/app/build.gradle new file mode 100644 index 00000000..8c030ab2 --- /dev/null +++ b/JavaDecoder/app/build.gradle @@ -0,0 +1,43 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.1" + + defaultConfig { + applicationId "com.wolt.blurhash" + minSdkVersion 16 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + dexOptions { + preDexLibraries = false + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation project(':library') + + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.2.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} \ No newline at end of file diff --git a/JavaDecoder/app/proguard-rules.pro b/JavaDecoder/app/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/JavaDecoder/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/JavaDecoder/app/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java b/JavaDecoder/app/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java new file mode 100644 index 00000000..474d20f3 --- /dev/null +++ b/JavaDecoder/app/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.wolt.blurhash; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.wolt.blurhash", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/JavaDecoder/app/src/main/AndroidManifest.xml b/JavaDecoder/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..e68dcf1e --- /dev/null +++ b/JavaDecoder/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/java/com/wolt/blurhash/MainActivity.java b/JavaDecoder/app/src/main/java/com/wolt/blurhash/MainActivity.java new file mode 100644 index 00000000..d9782944 --- /dev/null +++ b/JavaDecoder/app/src/main/java/com/wolt/blurhash/MainActivity.java @@ -0,0 +1,39 @@ +package com.wolt.blurhash; + +import androidx.appcompat.app.AppCompatActivity; + +import android.graphics.Bitmap; +import android.os.Bundle; +import android.os.SystemClock; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.TextView; + +import com.wolt.blurhash.decoder.BlurHashDecoder; + +/** + * @author Ilanthirayan Paramanathan + * @version 2.0.0 + * @since 22nd of November 2020 + */ +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + EditText etInput = findViewById(R.id.etInput); + TextView tvDecode = findViewById(R.id.tvDecode); + ImageView ivResult = findViewById(R.id.ivResult); + TextView ivResultTime = findViewById(R.id.ivResultTime); + + tvDecode.setOnClickListener(view -> { + long tStart = SystemClock.elapsedRealtime(); + Bitmap bitmap = BlurHashDecoder.getInstance().decode(etInput.getText().toString(), 20, 12, 1.0F, true); + long tEnd = SystemClock.elapsedRealtime(); + long tRes = tEnd - tStart; // time in milliseconds + ivResult.setImageBitmap(bitmap); + ivResultTime.setText(String.format("Time : %dms", tRes)); + }); + } +} \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/JavaDecoder/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..2b068d11 --- /dev/null +++ b/JavaDecoder/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/drawable/ic_launcher_background.xml b/JavaDecoder/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..07d5da9c --- /dev/null +++ b/JavaDecoder/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/JavaDecoder/app/src/main/res/layout/activity_main.xml b/JavaDecoder/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..3b22c57d --- /dev/null +++ b/JavaDecoder/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + diff --git a/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 00000000..eca70cfe --- /dev/null +++ b/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 00000000..eca70cfe --- /dev/null +++ b/JavaDecoder/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher.png b/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 00000000..a571e600 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 00000000..61da551c Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher.png b/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 00000000..c41dd285 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 00000000..db5080a7 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 00000000..6dba46da Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 00000000..da31a871 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 00000000..15ac6817 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 00000000..b216f2d3 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 00000000..f25a4197 Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 00000000..e96783cc Binary files /dev/null and b/JavaDecoder/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/JavaDecoder/app/src/main/res/values-night/themes.xml b/JavaDecoder/app/src/main/res/values-night/themes.xml new file mode 100644 index 00000000..b1630e68 --- /dev/null +++ b/JavaDecoder/app/src/main/res/values-night/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/values/colors.xml b/JavaDecoder/app/src/main/res/values/colors.xml new file mode 100644 index 00000000..a6abb83c --- /dev/null +++ b/JavaDecoder/app/src/main/res/values/colors.xml @@ -0,0 +1,14 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + + #29b6f6 + #0086c3 + #444444 + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/values/strings.xml b/JavaDecoder/app/src/main/res/values/strings.xml new file mode 100644 index 00000000..012ffa78 --- /dev/null +++ b/JavaDecoder/app/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + BlurHash + BlurHash string + Decode! + \ No newline at end of file diff --git a/JavaDecoder/app/src/main/res/values/themes.xml b/JavaDecoder/app/src/main/res/values/themes.xml new file mode 100644 index 00000000..0557a0b9 --- /dev/null +++ b/JavaDecoder/app/src/main/res/values/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/JavaDecoder/app/src/test/java/com/wolt/blurhash/ExampleUnitTest.java b/JavaDecoder/app/src/test/java/com/wolt/blurhash/ExampleUnitTest.java new file mode 100644 index 00000000..bbcc2800 --- /dev/null +++ b/JavaDecoder/app/src/test/java/com/wolt/blurhash/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.wolt.blurhash; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/JavaDecoder/build.gradle b/JavaDecoder/build.gradle new file mode 100644 index 00000000..4d24be2c --- /dev/null +++ b/JavaDecoder/build.gradle @@ -0,0 +1,24 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + repositories { + google() + jcenter() + } + dependencies { + classpath "com.android.tools.build:gradle:4.1.1" + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} \ No newline at end of file diff --git a/JavaDecoder/gradle.properties b/JavaDecoder/gradle.properties new file mode 100644 index 00000000..52f5917c --- /dev/null +++ b/JavaDecoder/gradle.properties @@ -0,0 +1,19 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app"s APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Automatically convert third-party libraries to use AndroidX +android.enableJetifier=true \ No newline at end of file diff --git a/JavaDecoder/gradlew b/JavaDecoder/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/JavaDecoder/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/JavaDecoder/library/build.gradle b/JavaDecoder/library/build.gradle new file mode 100644 index 00000000..56eb6683 --- /dev/null +++ b/JavaDecoder/library/build.gradle @@ -0,0 +1,38 @@ +plugins { + id 'com.android.library' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.1" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles "consumer-rules.pro" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.2.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} \ No newline at end of file diff --git a/JavaDecoder/library/consumer-rules.pro b/JavaDecoder/library/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/JavaDecoder/library/proguard-rules.pro b/JavaDecoder/library/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/JavaDecoder/library/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/JavaDecoder/library/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java b/JavaDecoder/library/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java new file mode 100644 index 00000000..6d788f7c --- /dev/null +++ b/JavaDecoder/library/src/androidTest/java/com/wolt/blurhash/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.wolt.blurhash; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.wolt.blurhash.test", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/JavaDecoder/library/src/main/AndroidManifest.xml b/JavaDecoder/library/src/main/AndroidManifest.xml new file mode 100644 index 00000000..dd1f6ec7 --- /dev/null +++ b/JavaDecoder/library/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/JavaDecoder/library/src/main/java/com/wolt/blurhash/decoder/BlurHashDecoder.java b/JavaDecoder/library/src/main/java/com/wolt/blurhash/decoder/BlurHashDecoder.java new file mode 100644 index 00000000..a9b8d42d --- /dev/null +++ b/JavaDecoder/library/src/main/java/com/wolt/blurhash/decoder/BlurHashDecoder.java @@ -0,0 +1,204 @@ +package com.wolt.blurhash.decoder; + +import android.graphics.Bitmap; +import android.graphics.Color; +import android.os.Build; + +import androidx.annotation.Nullable; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Ilanthirayan Paramanathan + * @version 2.0.0 + * @since 22nd of November 2020 + */ +public class BlurHashDecoder { + + // cache Math.cos() calculations to improve performance. + // The number of calculations can be huge for many bitmaps: width * height * numCompX * numCompY * 2 * nBitmaps + // the cache is enabled by default, it is recommended to disable it only when just a few images are displayed + private final HashMap cacheCosinesX = new HashMap<>(); + private final HashMap cacheCosinesY = new HashMap<>(); + private static Map charMap = new HashMap(); + + private static final BlurHashDecoder INSTANCE = new BlurHashDecoder(); + + public static BlurHashDecoder getInstance() { + return INSTANCE; + } + + private BlurHashDecoder() { + } + + static { + Character[] characters = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z', '#', '$', '%', '*', '+', ',', '-', '.', ':', ';', '=', '?', '@', '[', ']', '^', '_', '{', '|', '}', '~' + }; + for (int i = 0; i < characters.length; i++) { + charMap.put(characters[i], i); + } + } + + /** + * Clear calculations stored in memory cache. + * The cache is not big, but will increase when many image sizes are used, + * if the app needs memory it is recommended to clear it. + */ + private void clearCache() { + cacheCosinesX.clear(); + cacheCosinesY.clear(); + } + + /** + * Decode a blur hash into a new bitmap. + * + * @param useCache use in memory cache for the calculated math, reused by images with same size. + * if the cache does not exist yet it will be created and populated with new calculations. + * By default it is true. + */ + public Bitmap decode(@Nullable String blurHash, int width, int height, float punch, boolean useCache) { + if (blurHash == null || blurHash.length() <= 6) { + return null; + } + + int numCompEnc = decode83(blurHash, 0, 1); + int numCompX = numCompEnc % 9 + 1; + int numCompY = numCompEnc / 9 + 1; + if (blurHash.length() != 4 + 2 * numCompX * numCompY) { + return null; + } else { + int maxAcEnc = this.decode83(blurHash, 1, 2); + float maxAc = (float)(maxAcEnc + 1) / 166.0F; + float[][] colors = new float[numCompX * numCompY][]; + + for(int i = 0; i < numCompX * numCompY; ++i) { + if (i == 0) { + int colorEnc = decode83(blurHash, 2, 6); + colors [i] = decodeDc(colorEnc); + } else { + int from = 4 + i * 2; + int colorEnc = decode83(blurHash, from, from + 2); + colors [i] = decodeAc(colorEnc, maxAc * punch); + } + } + return composeBitmap(width, height, numCompX, numCompY, colors, useCache); + } + } + + private int decode83(String str, int from, int to) { + int result = 0; + for (int i = from; i < to; i++) { + int index = charMap.get(str.charAt(i)); + if (index != -1) { + result = result * 83 + index; + } + } + return result; + } + + + private float[] decodeDc(int colorEnc) { + int r = colorEnc >> 16; + int g = colorEnc >> 8 & 255; + int b = colorEnc & 255; + return new float[]{sRGBToLinear(r), sRGBToLinear(g), sRGBToLinear(b)}; + } + + private float sRGBToLinear(double colorEnc) { + float v = (float)colorEnc / 255.0F; + if (v <= 0.04045F) { + return v / 12.92F; + } else { + return (float)Math.pow((v + 0.055F) / 1.055F, 2.4F); + } + } + + private float[] decodeAc(int value, float maxAc) { + int r = value / 361; + int g = (value / 19) % 19; + int b = value % 19; + return new float[]{ + signedPow2((r - 9) / 9.0F) * maxAc, + signedPow2((g - 9) / 9.0F) * maxAc, + signedPow2((b - 9) / 9.0F) * maxAc + }; + } + + private float signedPow2(float value) { + return Math.copySign((float)Math.pow((double)value, (double)2.0F), value); + } + + private Bitmap composeBitmap(int width, int height, int numCompX, int numCompY, float[][] colors , boolean useCache) { + // use an array for better performance when writing pixel colors + int[] imageArray = new int[width * height]; + boolean calculateCosX = !useCache || !cacheCosinesX.containsKey(width * numCompX); + double[] cosinesX = getArrayForCosinesX(calculateCosX, width, numCompX); + boolean calculateCosY = !useCache || !cacheCosinesY.containsKey(height * numCompY); + double[] cosinesY = getArrayForCosinesY(calculateCosY, height, numCompY); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + float r = 0.0F; + float g = 0.0F; + float b = 0.0F; + for (int j = 0; j < numCompY; j++) { + for (int i = 0; i < numCompX; i++) { + double cosX = getCos(cosinesX, calculateCosX, i, numCompX, x, width); + double cosY = getCos(cosinesY, calculateCosY, j, numCompY, y, height); + float basis = (float)(cosX * cosY); + float[] color = colors[j * numCompX + i]; + r += color[0] * basis; + g += color[1] * basis; + b += color[2] * basis; + } + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + imageArray[x + width * y] = Color.rgb(linearToSRGB(r), linearToSRGB(g), linearToSRGB(b)); + } else { + imageArray[x + width * y] = Color.argb(255, linearToSRGB(r), linearToSRGB(g), linearToSRGB(b)); + } + } + } + return Bitmap.createBitmap(imageArray, width, height, Bitmap.Config.ARGB_8888); + } + + private double[] getArrayForCosinesY(boolean calculate, int height, int numCompY) { + if (calculate) { + double[] cosinesY = new double[height * numCompY]; + cacheCosinesY.put(height * numCompY, cosinesY); + return cosinesY; + } else { + return (double[]) cacheCosinesY.get(height * numCompY); + } + } + + private double[] getArrayForCosinesX(boolean calculate, int width, int numCompX) { + if (calculate) { + double[] cosinesX = new double[width * numCompX]; + cacheCosinesX.put(width * numCompX, cosinesX); + return cosinesX; + } else { + return (double[]) cacheCosinesX.get(width * numCompX); + } + } + + private double getCos(double[] getCos, boolean calculate, int x, int numComp, int y, int size) { + if (calculate) { + getCos[x + numComp * y] = Math.cos(Math.PI * y * x / size); + } + return getCos[x + numComp * y]; + } + + private int linearToSRGB(double value) { + double v = Math.max(0, Math.min(1, value)); + if (v <= 0.0031308F) { + return (int) (v * 12.92F * 255.0F + 0.5F); + } else { + return (int) ((1.055F * Math.pow(v, (1 / 2.4F)) - 0.055F) * 255 + 0.5F); + } + } +} diff --git a/JavaDecoder/library/src/test/java/com/wolt/blurhash/ExampleUnitTest.java b/JavaDecoder/library/src/test/java/com/wolt/blurhash/ExampleUnitTest.java new file mode 100644 index 00000000..bbcc2800 --- /dev/null +++ b/JavaDecoder/library/src/test/java/com/wolt/blurhash/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.wolt.blurhash; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/JavaDecoder/settings.gradle b/JavaDecoder/settings.gradle new file mode 100644 index 00000000..c8b1ba68 --- /dev/null +++ b/JavaDecoder/settings.gradle @@ -0,0 +1,3 @@ +include ':library' +include ':app' +rootProject.name = "Java Decoder" \ No newline at end of file