├── .gitignore ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main └── java │ └── io │ └── trbl │ └── blurhash │ ├── Base83.java │ ├── BlurHash.java │ └── Utils.java └── test ├── java └── io │ └── trbl │ └── blurhash │ ├── Base83Test.java │ └── BlurHashTest.java └── resources ├── 1x1.png ├── black.png ├── lorikeet.jpg └── white.png /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | .idea/ 3 | build/ 4 | out/ 5 | gradle.properties 6 | secring.gpg 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Hendrik Schnepel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BlurHash for Java 2 | 3 | ## About 4 | 5 | `io.trbl:blurhash` is a [BlurHash](https://blurha.sh) encoder implementation in Java. 6 | 7 | It's mostly based on the TypeScript implementation, 8 | with some influences from the Python implementation. 9 | 10 | ## How to use it 11 | 12 | Example usage: 13 | 14 | ```java 15 | BufferedImage image = ImageIO.read(new File("lorikeet.jpg")); 16 | String blurHash = BlurHash.encode(image); // UFDcT@_LNs#pVrIVX6Vu9]RRw[OXOZxaxWNH 17 | ``` 18 | 19 | Gradle dependency: 20 | 21 | ``` 22 | dependencies { 23 | implementation "io.trbl:blurhash:1.0.0" 24 | } 25 | ``` 26 | 27 | Maven dependency: 28 | ``` 29 | 30 | io.trbl 31 | blurhash 32 | 1.0.0 33 | 34 | ``` 35 | 36 | ## How to build it 37 | 38 | ``` 39 | ./gradlew build 40 | ``` 41 | 42 | ## How to release it 43 | 44 | Most information is available here: 45 | - https://central.sonatype.org/pages/ossrh-guide.html 46 | - https://central.sonatype.org/pages/gradle.html 47 | - https://central.sonatype.org/pages/working-with-pgp-signatures.html 48 | 49 | Some additional tips and tricks might be needed. 50 | 51 | Note that with recent GPG versions, 52 | you might need to export the secret keyring to a local file: 53 | ``` 54 | gpg --export-secret-keys > secring.gpg 55 | ``` 56 | 57 | Find your key ID: 58 | ``` 59 | gpg --list-secret-keys --keyid-format 0xSHORT 60 | ``` 61 | 62 | Dry-run the signing process: 63 | ``` 64 | ./gradlew signArchives 65 | ``` 66 | 67 | Make sure your key is available on a public server: 68 | ``` 69 | gpg --keyserver keyserver.ubuntu.com --send-keys ... 70 | ``` 71 | 72 | Finally, upload the package to staging like so: 73 | ``` 74 | ./gradlew clean uploadArchives 75 | ``` 76 | 77 | Manage staging repositories here: 78 | - https://oss.sonatype.org 79 | - https://issues.sonatype.org/ 80 | 81 | Remember that releasing the package is done by closing the staging repository. 82 | 83 | ## MIT License 84 | 85 | Copyright (c) 2019 Hendrik Schnepel 86 | 87 | Permission is hereby granted, free of charge, to any person obtaining a copy 88 | of this software and associated documentation files (the "Software"), to deal 89 | in the Software without restriction, including without limitation the rights 90 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91 | copies of the Software, and to permit persons to whom the Software is 92 | furnished to do so, subject to the following conditions: 93 | 94 | The above copyright notice and this permission notice shall be included in all 95 | copies or substantial portions of the Software. 96 | 97 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 98 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 99 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 100 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 101 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 102 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 103 | SOFTWARE. 104 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "java" 3 | id "maven" 4 | id "signing" 5 | } 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | testImplementation "junit:junit:4.12" 13 | } 14 | 15 | task javadocJar(type: Jar) { 16 | classifier "javadoc" 17 | from javadoc 18 | } 19 | 20 | task sourcesJar(type: Jar) { 21 | classifier "sources" 22 | from sourceSets.main.allSource 23 | } 24 | 25 | artifacts { 26 | archives javadocJar, sourcesJar 27 | } 28 | 29 | signing { 30 | sign configurations.archives 31 | } 32 | 33 | uploadArchives { 34 | repositories { 35 | mavenDeployer { 36 | beforeDeployment { 37 | MavenDeployment deployment -> signing.signPom(deployment) 38 | } 39 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 40 | authentication(userName: ossrhUsername, password: ossrhPassword) 41 | } 42 | pom.project { 43 | group = "io.trbl" 44 | artifactId = "blurhash" 45 | archivesBaseName = "blurhash" 46 | version = "1.0.0" 47 | name "BlurHash for Java" 48 | description "A BlurHash encoder implementation in Java" 49 | url "https://github.com/hsch/blurhash-java" 50 | scm { 51 | url "https://github.com/hsch/blurhash-java" 52 | connection "scm:git:https://github.com/hsch/blurhash-java.git" 53 | developerConnection "scm:git:git@github.com:hsch/blurhash-java.git" 54 | } 55 | licenses { 56 | license { 57 | name "MIT" 58 | url "https://opensource.org/licenses/MIT" 59 | } 60 | } 61 | developers { 62 | developer { 63 | id "hendrik" 64 | name "Hendrik" 65 | email "hendrik@h5k.org" 66 | } 67 | } 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsch/blurhash-java/b64183d9a4aeab1ed1766f34d4f673bfafd334bc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jul 13 16:26:16 AEST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/java/io/trbl/blurhash/Base83.java: -------------------------------------------------------------------------------- 1 | package io.trbl.blurhash; 2 | 3 | final class Base83 { 4 | 5 | static final char[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~" 6 | .toCharArray(); 7 | 8 | private static int indexOf(char[] a, char key) { 9 | for (int i = 0; i < a.length; i++) { 10 | if (a[i] == key) { 11 | return i; 12 | } 13 | } 14 | return -1; 15 | } 16 | 17 | static String encode(long value, int length) { 18 | char[] buffer = new char[length]; 19 | encode(value, length, buffer, 0); 20 | return new String(buffer); 21 | } 22 | 23 | static void encode(long value, int length, char[] buffer, int offset) { 24 | int exp = 1; 25 | for (int i = 1; i <= length; i++, exp *= 83) { 26 | int digit = (int)(value / exp % 83); 27 | buffer[offset + length - i] = ALPHABET[digit]; 28 | } 29 | } 30 | 31 | static int decode(String value) { 32 | int result = 0; 33 | char[] chars = value.toCharArray(); 34 | for (int i = 0; i < chars.length; i++) { 35 | result = result * 83 + indexOf(ALPHABET, chars[i]); 36 | } 37 | return result; 38 | } 39 | 40 | private Base83() { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/io/trbl/blurhash/BlurHash.java: -------------------------------------------------------------------------------- 1 | package io.trbl.blurhash; 2 | 3 | import java.awt.image.BufferedImage; 4 | 5 | import static io.trbl.blurhash.Utils.*; 6 | 7 | /** 8 | * Utility methods to calculate blur hashes. 9 | */ 10 | public final class BlurHash { 11 | 12 | private static void applyBasisFunction(int[] pixels, int width, int height, 13 | double normalisation, int i, int j, 14 | double[][] factors, int index) { 15 | double r = 0, g = 0, b = 0; 16 | for (int x = 0; x < width; x++) { 17 | for (int y = 0; y < height; y++) { 18 | double basis = normalisation 19 | * Math.cos((Math.PI * i * x) / width) 20 | * Math.cos((Math.PI * j * y) / height); 21 | int pixel = pixels[y * width + x]; 22 | r += basis * sRGBToLinear((pixel >> 16) & 0xff); 23 | g += basis * sRGBToLinear((pixel >> 8) & 0xff); 24 | b += basis * sRGBToLinear( pixel & 0xff); 25 | } 26 | } 27 | double scale = 1.0 / (width * height); 28 | factors[index][0] = r * scale; 29 | factors[index][1] = g * scale; 30 | factors[index][2] = b * scale; 31 | } 32 | 33 | private static long encodeDC(double[] value) { 34 | long r = linearTosRGB(value[0]); 35 | long g = linearTosRGB(value[1]); 36 | long b = linearTosRGB(value[2]); 37 | return (r << 16) + (g << 8) + b; 38 | } 39 | 40 | private static long encodeAC(double[] value, double maximumValue) { 41 | double quantR = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[0] / maximumValue, 0.5) * 9 + 9.5)))); 42 | double quantG = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[1] / maximumValue, 0.5) * 9 + 9.5)))); 43 | double quantB = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[2] / maximumValue, 0.5) * 9 + 9.5)))); 44 | return Math.round(quantR * 19 * 19 + quantG * 19 + quantB); 45 | } 46 | 47 | /** 48 | * Calculates the blur hash from the given image with 4x4 components. 49 | * 50 | * @param bufferedImage the image 51 | * @return the blur hash 52 | */ 53 | public static String encode(BufferedImage bufferedImage) { 54 | return encode(bufferedImage, 4, 4); 55 | } 56 | 57 | /** 58 | * Calculates the blur hash from the given image. 59 | * @param bufferedImage the image 60 | * @param componentX number of components in the x dimension 61 | * @param componentY number of components in the y dimension 62 | * @return the blur hash 63 | */ 64 | public static String encode(BufferedImage bufferedImage, int componentX, int componentY) { 65 | int width = bufferedImage.getWidth(); 66 | int height = bufferedImage.getHeight(); 67 | int[] pixels = bufferedImage.getRGB(0, 0, width, height, null, 0, width); 68 | return encode(pixels, width, height, componentX, componentY); 69 | } 70 | 71 | /** 72 | * Calculates the blur hash from the given pixels. 73 | * 74 | * @param pixels width * height pixels, encoded as RGB integers (0xAARRGGBB) 75 | * @param width width of the bitmap 76 | * @param height height of the bitmap 77 | * @param componentX number of components in the x dimension 78 | * @param componentY number of components in the y dimension 79 | * @return the blur hash 80 | */ 81 | public static String encode(int[] pixels, int width, int height, int componentX, int componentY) { 82 | 83 | if (componentX < 1 || componentX > 9 || componentY < 1 || componentY > 9) { 84 | throw new IllegalArgumentException("Blur hash must have between 1 and 9 components"); 85 | } 86 | if (width * height != pixels.length) { 87 | throw new IllegalArgumentException("Width and height must match the pixels array"); 88 | } 89 | 90 | double[][] factors = new double[componentX * componentY][3]; 91 | for (int j = 0; j < componentY; j++) { 92 | for (int i = 0; i < componentX; i++) { 93 | double normalisation = i == 0 && j == 0 ? 1 : 2; 94 | applyBasisFunction(pixels, width, height, 95 | normalisation, i, j, 96 | factors, j * componentX + i); 97 | } 98 | } 99 | 100 | char[] hash = new char[1 + 1 + 4 + 2 * (factors.length - 1)]; // size flag + max AC + DC + 2 * AC components 101 | 102 | long sizeFlag = componentX - 1 + (componentY - 1) * 9; 103 | Base83.encode(sizeFlag, 1, hash, 0); 104 | 105 | double maximumValue; 106 | if (factors.length > 1) { 107 | double actualMaximumValue = max(factors, 1, factors.length); 108 | double quantisedMaximumValue = Math.floor(Math.max(0, Math.min(82, Math.floor(actualMaximumValue * 166 - 0.5)))); 109 | maximumValue = (quantisedMaximumValue + 1) / 166; 110 | Base83.encode(Math.round(quantisedMaximumValue), 1, hash, 1); 111 | } else { 112 | maximumValue = 1; 113 | Base83.encode(0, 1, hash, 1); 114 | } 115 | 116 | double[] dc = factors[0]; 117 | Base83.encode(encodeDC(dc), 4, hash, 2); 118 | 119 | for (int i = 1; i < factors.length; i++) { 120 | Base83.encode(encodeAC(factors[i], maximumValue), 2, hash, 6 + 2 * (i - 1)); 121 | } 122 | return new String(hash); 123 | } 124 | 125 | private BlurHash() { 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/io/trbl/blurhash/Utils.java: -------------------------------------------------------------------------------- 1 | package io.trbl.blurhash; 2 | 3 | final class Utils { 4 | 5 | static double sRGBToLinear(long value) { 6 | double v = value / 255.0; 7 | if (v <= 0.04045) { 8 | return v / 12.92; 9 | } else { 10 | return Math.pow((v + 0.055) / 1.055, 2.4); 11 | } 12 | } 13 | 14 | static long linearTosRGB(double value) { 15 | double v = Math.max(0, Math.min(1, value)); 16 | if (v <= 0.0031308) { 17 | return (long)(v * 12.92 * 255 + 0.5); 18 | } else { 19 | return (long)((1.055 * Math.pow(v, 1 / 2.4) - 0.055) * 255 + 0.5); 20 | } 21 | } 22 | 23 | static double signPow(double val, double exp) { 24 | return Math.copySign(Math.pow(Math.abs(val), exp), val); 25 | } 26 | 27 | static double max(double[][] values, int from, int endExclusive) { 28 | double result = Double.NEGATIVE_INFINITY; 29 | for (int i = from; i < endExclusive; i++) { 30 | for (int j = 0; j < values[i].length; j++) { 31 | double value = values[i][j]; 32 | if (value > result) { 33 | result = value; 34 | } 35 | } 36 | } 37 | return result; 38 | } 39 | 40 | private Utils() { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/io/trbl/blurhash/Base83Test.java: -------------------------------------------------------------------------------- 1 | package io.trbl.blurhash; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class Base83Test { 8 | 9 | @Test 10 | public void testSingleDigits() { 11 | for (int i = 0; i < 83; ++i) { 12 | String expected = new String(Base83.ALPHABET, i, 1); 13 | assertEquals(i + " encodes", expected, Base83.encode(i, 1)); 14 | } 15 | } 16 | 17 | @Test 18 | public void test0000() { 19 | assertEquals("0000", Base83.encode(0, 4)); 20 | } 21 | 22 | @Test 23 | public void test0001() { 24 | assertEquals("0001", Base83.encode(1, 4)); 25 | } 26 | 27 | @Test 28 | public void test0010() { 29 | assertEquals("0010", Base83.encode(83, 4)); 30 | } 31 | 32 | @Test 33 | public void test0011() { 34 | assertEquals("0011", Base83.encode(83 + 1, 4)); 35 | } 36 | 37 | @Test 38 | public void test00X0() { 39 | assertEquals("00~0", Base83.encode(83 * 82, 4)); 40 | } 41 | 42 | @Test 43 | public void test0100() { 44 | assertEquals("0100", Base83.encode(83 * 83, 4)); 45 | } 46 | 47 | @Test 48 | public void test00XXEncode() { 49 | assertEquals("00~~", Base83.encode(83 * 82 + 82, 4)); 50 | } 51 | 52 | @Test 53 | public void test0XXXDecode() { 54 | assertEquals( 82 55 | + 82 * 83 56 | + 82 * 83 * 83, Base83.decode("0~~~")); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/io/trbl/blurhash/BlurHashTest.java: -------------------------------------------------------------------------------- 1 | package io.trbl.blurhash; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.imageio.ImageIO; 6 | import java.awt.image.BufferedImage; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | 12 | public class BlurHashTest { 13 | 14 | @Test 15 | public void testBlack() throws Exception { 16 | String blurHash = BlurHash.encode(getBufferedImage("/black.png")); 17 | assertEquals("U00000fQfQfQfQfQfQfQfQfQfQfQfQfQfQfQ", blurHash); 18 | } 19 | 20 | @Test 21 | public void test1x1() throws Exception { 22 | String blurHash = BlurHash.encode(getBufferedImage("/1x1.png")); 23 | assertEquals("U~TSUA~q~q~q~q~q~q~q~q~q~q~q~q~q~q~q", blurHash); 24 | } 25 | 26 | @Test 27 | public void testWhite() throws Exception { 28 | String blurHash = BlurHash.encode(getBufferedImage("/white.png")); 29 | assertEquals("U2TSUA~qfQ~q~qj[fQj[fQfQfQfQ~qj[fQj[", blurHash); 30 | } 31 | 32 | @Test 33 | public void testLorikeet() throws Exception { 34 | String blurHash = BlurHash.encode(getBufferedImage("/lorikeet.jpg")); 35 | assertEquals("UFDcT@_LNs#pVrIVX6Vu9]RRw[OXOZxaxWNH", blurHash); 36 | // Python: UFDcT@_LNr#pVrIVX6Vu9]RRw[OYOZxaxWNH 37 | // ^ ^ 38 | } 39 | 40 | private BufferedImage getBufferedImage(String filename) throws IOException { 41 | try (InputStream inputStream = getClass().getResourceAsStream(filename)) { 42 | return ImageIO.read(inputStream); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/resources/1x1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsch/blurhash-java/b64183d9a4aeab1ed1766f34d4f673bfafd334bc/src/test/resources/1x1.png -------------------------------------------------------------------------------- /src/test/resources/black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsch/blurhash-java/b64183d9a4aeab1ed1766f34d4f673bfafd334bc/src/test/resources/black.png -------------------------------------------------------------------------------- /src/test/resources/lorikeet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsch/blurhash-java/b64183d9a4aeab1ed1766f34d4f673bfafd334bc/src/test/resources/lorikeet.jpg -------------------------------------------------------------------------------- /src/test/resources/white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsch/blurhash-java/b64183d9a4aeab1ed1766f34d4f673bfafd334bc/src/test/resources/white.png --------------------------------------------------------------------------------