Compare commits
108 Commits
encode-in-
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
477a7a9e59 | ||
|
|
295ee9a40b | ||
|
|
2a37ed33b4 | ||
|
|
aa46c81fc5 | ||
|
|
e631427d15 | ||
|
|
5eb82c10b3 | ||
|
|
3f9db6312b | ||
|
|
7bf63cf417 | ||
|
|
e6d02c10bf | ||
|
|
db4ea76438 | ||
|
|
828b0928b9 | ||
|
|
fecb797afa | ||
|
|
9ed8002196 | ||
|
|
68c2716363 | ||
|
|
d0f2696c5e | ||
|
|
7717b2fafc | ||
|
|
4d7b008644 | ||
|
|
29452f61db | ||
|
|
82de026973 | ||
|
|
ada6b8738d | ||
|
|
961c6c35fb | ||
|
|
88f85456d3 | ||
|
|
ccc5db94f3 | ||
|
|
a4eb6bda70 | ||
|
|
58d862a2d7 | ||
|
|
e0dcf1b2c6 | ||
|
|
9ea6105a0a | ||
|
|
2df97c77c5 | ||
|
|
db395593db | ||
|
|
2d350e8f3d | ||
|
|
0cc05e5525 | ||
|
|
6a0abe6e2f | ||
|
|
5c79bd2a27 | ||
|
|
29816377d8 | ||
|
|
63a8cd8647 | ||
|
|
65aa678e15 | ||
|
|
de3bbb5422 | ||
|
|
457ca14451 | ||
|
|
c53f979561 | ||
|
|
604cc860d8 | ||
|
|
34665a4f20 | ||
|
|
2a448dbd74 | ||
|
|
51e9f8cc74 | ||
|
|
4228e38ad5 | ||
|
|
31c7660487 | ||
|
|
36e5d19cb4 | ||
|
|
77080c5f27 | ||
|
|
a76f98e85e | ||
|
|
09374e967b | ||
|
|
b881d32a19 | ||
|
|
e3fd54bfe0 | ||
|
|
4555b199fb | ||
|
|
5e84360324 | ||
|
|
11a9d3f1d5 | ||
|
|
233c739128 | ||
|
|
5f975015b6 | ||
|
|
f67a47857e | ||
|
|
7b02bf1a19 | ||
|
|
9cdc36e84b | ||
|
|
f4db952f56 | ||
|
|
835982541a | ||
|
|
f0f6b6cc80 | ||
|
|
63c9f9506d | ||
|
|
c466b224c0 | ||
|
|
ffa004acb3 | ||
|
|
4f1a0c8c30 | ||
|
|
bf402e4df8 | ||
|
|
41367c2708 | ||
|
|
194c73a258 | ||
|
|
00ff426b70 | ||
|
|
add8eccadf | ||
|
|
2adcbc5994 | ||
|
|
a580a21535 | ||
|
|
0c3e892102 | ||
|
|
08437e99e9 | ||
|
|
ef2778fc60 | ||
|
|
823d925948 | ||
|
|
b0bcfb6347 | ||
|
|
8b1c111093 | ||
|
|
fe464284ed | ||
|
|
6b36734b36 | ||
|
|
a48c63ebe7 | ||
|
|
ec392b3811 | ||
|
|
090d68fb3b | ||
|
|
88a2587640 | ||
|
|
3f571a54a5 | ||
|
|
91c99fa77b | ||
|
|
b64c41f4e7 | ||
|
|
39d152d535 | ||
|
|
ea606b5abb | ||
|
|
9baa3b7dc2 | ||
|
|
f06c756b7d | ||
|
|
f95afb7473 | ||
|
|
e085a984ae | ||
|
|
d0fe58d9e3 | ||
|
|
5f83f82e40 | ||
|
|
63fea7d3d6 | ||
|
|
916eeae072 | ||
|
|
576b58f6c0 | ||
|
|
7dd1252834 | ||
|
|
e460ed5b7f | ||
|
|
027c803e7f | ||
|
|
1201ee4ccf | ||
|
|
22cafce1c2 | ||
|
|
3102c8115e | ||
|
|
ca5dbf67e5 | ||
|
|
e9ecc53bbc | ||
|
|
e80667aebb |
5
.gitignore
vendored
@ -20,5 +20,6 @@ Python/build/
|
||||
*.o
|
||||
*.pyc
|
||||
|
||||
# Typescript
|
||||
typescript/node_modules/
|
||||
# Website
|
||||
Website/node_modules/
|
||||
Website/dist/
|
||||
|
||||
@ -1,2 +1,8 @@
|
||||
blurhash: blurhash_stb.c encode.c encode.h stb_image.h
|
||||
PROGRAM=blurhash
|
||||
|
||||
$(PROGRAM): blurhash_stb.c encode.c encode.h stb_image.h
|
||||
$(CC) -o $@ blurhash_stb.c encode.c -lm
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(PROGRAM)
|
||||
|
||||
40
Kotlin/.gitignore
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
# Built application files
|
||||
*.apk
|
||||
*.ap_
|
||||
|
||||
# Files for the Dalvik VM
|
||||
*.dex
|
||||
|
||||
# Java class files
|
||||
*.class
|
||||
|
||||
# Generated files
|
||||
bin/
|
||||
gen/
|
||||
|
||||
# Gradle files
|
||||
.gradle/
|
||||
build/
|
||||
|
||||
# Local configuration file (sdk path, etc)
|
||||
local.properties
|
||||
|
||||
# Proguard folder generated by Eclipse
|
||||
proguard/
|
||||
|
||||
# Log Files
|
||||
*.log
|
||||
|
||||
.idea/.workspace
|
||||
|
||||
# http://stackoverflow.com/questions/16736856/what-should-be-in-my-gitignore-for-an-android-studio-project
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/workspace.xml
|
||||
/.idea/libraries
|
||||
.DS_Store
|
||||
/build
|
||||
.idea
|
||||
**/*.iml
|
||||
*.hprof
|
||||
**/*.project
|
||||
@ -1,126 +0,0 @@
|
||||
package com.wolt.android.presentation.ui.helpers
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import com.wolt.android.WoltApp
|
||||
import java.lang.Math.*
|
||||
|
||||
/**
|
||||
* Created by mike on 31/07/2017.
|
||||
*/
|
||||
object BlurHashDecoder {
|
||||
|
||||
private val digitCharacters = arrayOf(
|
||||
'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', '#', '$', '%', '*', '+', ',', '-', '.',
|
||||
':', ';', '=', '?', '@', '[', ']', '^', '_', '{',
|
||||
'|', '}', '~'
|
||||
)
|
||||
|
||||
private val MAX_SIZE = 20
|
||||
|
||||
fun decode(blurHash: String?, width: Int, height: Int, punch: Float): Bitmap? {
|
||||
if (blurHash == null || blurHash.length < 6) {
|
||||
return null
|
||||
}
|
||||
|
||||
val sizeFlag = blurHash[0].toString().decode83()
|
||||
val numY = (sizeFlag / 9) + 1
|
||||
val numX = (sizeFlag % 9) + 1
|
||||
|
||||
val quantisedMaximumValue = blurHash[1].toString().decode83()
|
||||
val maximumValue = (quantisedMaximumValue + 1) / 166
|
||||
|
||||
if (blurHash.length != 4 + 2 * numX * numY) {
|
||||
return null
|
||||
}
|
||||
|
||||
val colors = (0..numX * numY - 1).map { i ->
|
||||
if (i == 0) {
|
||||
val value = blurHash.substring(2, 6).decode83()
|
||||
decodeDc(value)
|
||||
} else {
|
||||
val startIndex = 4 + i * 2
|
||||
val value = blurHash.substring(startIndex, startIndex + 2).decode83()
|
||||
decodeAc(value, maximumValue * punch)
|
||||
}
|
||||
}
|
||||
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||
|
||||
for (y in 0..height - 1) {
|
||||
for (x in 0..width - 1) {
|
||||
var r = 0f
|
||||
var g = 0f
|
||||
var b = 0f
|
||||
|
||||
for (j in 0..numY - 1) {
|
||||
for (i in 0..numX - 1) {
|
||||
val basis = (cos(PI * x * i / width) * cos(PI * y * j / height)).toFloat()
|
||||
val color = colors[i + j * numX]
|
||||
r += color[0] * basis
|
||||
g += color[1] * basis
|
||||
b += color[2] * basis
|
||||
}
|
||||
}
|
||||
|
||||
bitmap.setPixel(x, y, Color.rgb(linearToSRgb(r), linearToSRgb(g), linearToSRgb(b)))
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap
|
||||
}
|
||||
|
||||
@Suppress("LoopToCallChain")
|
||||
private fun String.decode83(): Int {
|
||||
var value: Int = 0
|
||||
for (i in 0..this.length - 1) {
|
||||
val digit = digitCharacters.indexOf(this[i])
|
||||
if (digit != -1) {
|
||||
value = value * 83 + digit
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
private fun decodeDc(value: Int): Array<Float> {
|
||||
val intR = value shr 16
|
||||
val intG = (value shr 8) and 255
|
||||
val intB = value and 255
|
||||
return arrayOf(sRgbToLinear(intR), sRgbToLinear(intG), sRgbToLinear(intB))
|
||||
}
|
||||
|
||||
fun sRgbToLinear(value: Int): Float {
|
||||
val v = value / 255f
|
||||
return if (v <= 0.04045) (v / 12.92f) else (pow((v + 0.055) / 1.055, 2.4).toFloat())
|
||||
}
|
||||
|
||||
private fun decodeAc(value: Int, maximumValue: Float): Array<Float> {
|
||||
val quantR = value / (19 * 19)
|
||||
val quantG = (value / 19) % 19
|
||||
val quantB = value % 19
|
||||
return arrayOf(
|
||||
signPow((quantR - 9) / 9.0, 2.0) * maximumValue,
|
||||
signPow((quantG - 9) / 9.0, 2.0) * maximumValue,
|
||||
signPow((quantB - 9) / 9.0, 2.0) * maximumValue
|
||||
)
|
||||
}
|
||||
|
||||
private fun signPow(value: Double, exp: Double) = copySign(pow(abs(value), exp), value).toFloat()
|
||||
|
||||
private fun linearToSRgb(value: Float): Int {
|
||||
val v = max(0f, min(1f, value))
|
||||
if (v <= 0.0031308f) {
|
||||
return (v * 12.92f * 255 + 0.5f).toInt()
|
||||
} else {
|
||||
return ((1.055f * pow(v.toDouble(), 1 / 2.4) - 0.055f) * 255 + 0.5f).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
29
Kotlin/build.gradle
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
buildscript {
|
||||
|
||||
ext.kotlin_version = '1.3.50'
|
||||
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.5.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
allprojects {
|
||||
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
29
Kotlin/demo/build.gradle
Normal file
@ -0,0 +1,29 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
|
||||
compileSdkVersion 29
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.wolt.blurhash"
|
||||
minSdkVersion 14
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':lib')
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
}
|
||||
21
Kotlin/demo/proguard-rules.pro
vendored
Normal file
@ -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
|
||||
21
Kotlin/demo/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.wolt.blurhashapp">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name="com.wolt.blurhashapp.MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@ -0,0 +1,24 @@
|
||||
package com.wolt.blurhashapp
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.wolt.blurhashkt.BlurHashDecoder
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
val etInput: EditText = findViewById(R.id.etInput)
|
||||
val ivResult: ImageView = findViewById(R.id.ivResult)
|
||||
findViewById<View>(R.id.tvDecode).setOnClickListener {
|
||||
val bitmap = BlurHashDecoder.decode(etInput.text.toString(), 20, 12)
|
||||
ivResult.setImageBitmap(bitmap)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillType="evenOdd"
|
||||
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="78.5885"
|
||||
android:endY="90.9159"
|
||||
android:startX="48.7653"
|
||||
android:startY="61.0927"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0"/>
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0"/>
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1"/>
|
||||
</vector>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="#ff009de0"/>
|
||||
<corners android:radius="8dp"/>
|
||||
</shape>
|
||||
171
Kotlin/demo/src/main/res/drawable/ic_launcher_background.xml
Normal file
@ -0,0 +1,171 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillColor="#26A69A"
|
||||
android:pathData="M0,0h108v108h-108z"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8"/>
|
||||
</vector>
|
||||
43
Kotlin/demo/src/main/res/layout/activity_main.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="24dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:gravity="center_horizontal"
|
||||
android:hint="@string/hint_blurhash"
|
||||
android:inputType="text"
|
||||
android:singleLine="true"
|
||||
android:text="LEHV6nWB2yk8pyo0adR*.7kCMdnj"
|
||||
android:textColor="@color/colorAccent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvDecode"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="12dp"
|
||||
android:background="@color/colorPrimary"
|
||||
android:elevation="8dp"
|
||||
android:paddingStart="12dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingEnd="12dp"
|
||||
android:paddingBottom="8dp"
|
||||
android:text="@string/title_button_decode"
|
||||
android:textColor="@color/colorAccent"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivResult"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:adjustViewBounds="true" />
|
||||
|
||||
</LinearLayout>
|
||||
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background"/>
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background"/>
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
|
||||
</adaptive-icon>
|
||||
BIN
Kotlin/demo/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 4.9 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 6.9 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 9.0 KiB |
BIN
Kotlin/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
7
Kotlin/demo/src/main/res/values/colors.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#29b6f6</color>
|
||||
<color name="colorPrimaryDark">#0086c3</color>
|
||||
<color name="colorAccent">#444444</color>
|
||||
</resources>
|
||||
|
||||
5
Kotlin/demo/src/main/res/values/strings.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
<string name="app_name">BlurHash</string>
|
||||
<string name="hint_blurhash">BlurHash string</string>
|
||||
<string name="title_button_decode">Decode!</string>
|
||||
</resources>
|
||||
9
Kotlin/demo/src/main/res/values/styles.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<resources>
|
||||
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
15
Kotlin/gradle.properties
Normal file
@ -0,0 +1,15 @@
|
||||
# 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.
|
||||
android.enableJetifier=true
|
||||
android.useAndroidX=true
|
||||
org.gradle.jvmargs=-Xmx1536m
|
||||
# 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
|
||||
BIN
Kotlin/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
6
Kotlin/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
#Mon Jul 01 10:02:38 EEST 2019
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
|
||||
172
Kotlin/gradlew
vendored
Executable file
@ -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" "$@"
|
||||
84
Kotlin/gradlew.bat
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS=
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
30
Kotlin/lib/build.gradle
Normal file
@ -0,0 +1,30 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
apply plugin: 'kotlin-android'
|
||||
|
||||
android {
|
||||
|
||||
compileSdkVersion 29
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 14
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
}
|
||||
21
Kotlin/lib/proguard-rules.pro
vendored
Normal file
@ -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
|
||||
1
Kotlin/lib/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
||||
<manifest package="com.wolt.blurhashkt" />
|
||||
122
Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt
Normal file
@ -0,0 +1,122 @@
|
||||
package com.wolt.blurhashkt
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.withSign
|
||||
|
||||
object BlurHashDecoder {
|
||||
|
||||
fun decode(blurHash: String?, width: Int, height: Int, punch: Float = 1f): Bitmap? {
|
||||
if (blurHash == null || blurHash.length < 6) {
|
||||
return null
|
||||
}
|
||||
val numCompEnc = decode83(blurHash, 0, 1)
|
||||
val numCompX = (numCompEnc % 9) + 1
|
||||
val numCompY = (numCompEnc / 9) + 1
|
||||
if (blurHash.length != 4 + 2 * numCompX * numCompY) {
|
||||
return null
|
||||
}
|
||||
val maxAcEnc = decode83(blurHash, 1, 2)
|
||||
val maxAc = (maxAcEnc + 1) / 166f
|
||||
val colors = Array(numCompX * numCompY) { i ->
|
||||
if (i == 0) {
|
||||
val colorEnc = decode83(blurHash, 2, 6)
|
||||
decodeDc(colorEnc)
|
||||
} else {
|
||||
val from = 4 + i * 2
|
||||
val colorEnc = decode83(blurHash, from, from + 2)
|
||||
decodeAc(colorEnc, maxAc * punch)
|
||||
}
|
||||
}
|
||||
return composeBitmap(width, height, numCompX, numCompY, colors)
|
||||
}
|
||||
|
||||
private fun decode83(str: String, from: Int = 0, to: Int = str.length): Int {
|
||||
var result = 0
|
||||
for (i in from until to) {
|
||||
val index = charMap[str[i]] ?: -1
|
||||
if (index != -1) {
|
||||
result = result * 83 + index
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun decodeDc(colorEnc: Int): FloatArray {
|
||||
val r = colorEnc shr 16
|
||||
val g = (colorEnc shr 8) and 255
|
||||
val b = colorEnc and 255
|
||||
return floatArrayOf(srgbToLinear(r), srgbToLinear(g), srgbToLinear(b))
|
||||
}
|
||||
|
||||
private fun srgbToLinear(colorEnc: Int): Float {
|
||||
val v = colorEnc / 255f
|
||||
return if (v <= 0.04045f) {
|
||||
(v / 12.92f)
|
||||
} else {
|
||||
((v + 0.055f) / 1.055f).pow(2.4f)
|
||||
}
|
||||
}
|
||||
|
||||
private fun decodeAc(value: Int, maxAc: Float): FloatArray {
|
||||
val r = value / (19 * 19)
|
||||
val g = (value / 19) % 19
|
||||
val b = value % 19
|
||||
return floatArrayOf(
|
||||
signedPow2((r - 9) / 9.0f) * maxAc,
|
||||
signedPow2((g - 9) / 9.0f) * maxAc,
|
||||
signedPow2((b - 9) / 9.0f) * maxAc
|
||||
)
|
||||
}
|
||||
|
||||
private fun signedPow2(value: Float) = value.pow(2f).withSign(value)
|
||||
|
||||
private fun composeBitmap(
|
||||
width: Int, height: Int,
|
||||
numCompX: Int, numCompY: Int,
|
||||
colors: Array<FloatArray>
|
||||
): Bitmap {
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||
for (y in 0 until height) {
|
||||
for (x in 0 until width) {
|
||||
var r = 0f
|
||||
var g = 0f
|
||||
var b = 0f
|
||||
for (j in 0 until numCompY) {
|
||||
for (i in 0 until numCompX) {
|
||||
val basis = (cos(PI * x * i / width) * cos(PI * y * j / height)).toFloat()
|
||||
val color = colors[j * numCompX + i]
|
||||
r += color[0] * basis
|
||||
g += color[1] * basis
|
||||
b += color[2] * basis
|
||||
}
|
||||
}
|
||||
bitmap.setPixel(x, y, Color.rgb(linearToSrgb(r), linearToSrgb(g), linearToSrgb(b)))
|
||||
}
|
||||
}
|
||||
return bitmap
|
||||
}
|
||||
|
||||
private fun linearToSrgb(value: Float): Int {
|
||||
val v = value.coerceIn(0f, 1f)
|
||||
return if (v <= 0.0031308f) {
|
||||
(v * 12.92f * 255f + 0.5f).toInt()
|
||||
} else {
|
||||
((1.055f * v.pow(1 / 2.4f) - 0.055f) * 255 + 0.5f).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
private val charMap = listOf(
|
||||
'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', '#', '$', '%', '*', '+', ',',
|
||||
'-', '.', ':', ';', '=', '?', '@', '[', ']', '^', '_', '{', '|', '}', '~'
|
||||
)
|
||||
.mapIndexed { i, c -> c to i }
|
||||
.toMap()
|
||||
|
||||
}
|
||||
1
Kotlin/settings.gradle
Normal file
@ -0,0 +1 @@
|
||||
include ':demo', ':lib'
|
||||
BIN
Media/BadScreenshot.png
Normal file
|
After Width: | Height: | Size: 269 KiB |
BIN
Media/GoodScreenshot.png
Normal file
|
After Width: | Height: | Size: 436 KiB |
|
Before Width: | Height: | Size: 134 KiB |
BIN
Media/WhyBlurHash.png
Normal file
|
After Width: | Height: | Size: 398 KiB |
48
Readme.md
@ -1,4 +1,4 @@
|
||||
# BlurHash
|
||||
# [BlurHash](http://blurha.sh)
|
||||
|
||||
BlurHash is a compact representation of a placeholder for an image.
|
||||
|
||||
@ -10,9 +10,11 @@ images into your data to show as placeholders?
|
||||
|
||||
BlurHash will solve your problems! How? Like this:
|
||||
|
||||
<img src="Media/WhyBlurHash.jpg" width="600">
|
||||
<img src="Media/WhyBlurHash.png" width="600">
|
||||
|
||||
## How does it work
|
||||
You can also see nice examples and try it out yourself at [blurha.sh](http://blurha.sh/)!
|
||||
|
||||
## How does it work?
|
||||
|
||||
In short, BlurHash takes an image, and gives you a short string (only 20-30 characters!) that represents the placeholder for this
|
||||
image. You do this on the backend of your service, and store the string along with the image. When you send data to your
|
||||
@ -38,10 +40,19 @@ So far, we have created these implementations:
|
||||
There is also an example app to play around with the algorithm.
|
||||
* [Kotlin](Kotlin) - A decoder implementation for Android.
|
||||
* [TypeScript](TypeScript) - Encoder and decoder implementations, and an example page to test.
|
||||
* [Python](https://github.com/creditornot/blurhash-python) - Integration of the C encoder code into Python.
|
||||
* [Python](https://github.com/woltapp/blurhash-python) - Integration of the C encoder code into Python.
|
||||
|
||||
These cover our use cases, but could probably use polishing, extending and improving. Perhaps you'd like to help?
|
||||
Which brings us to...
|
||||
These cover our use cases, but could probably use polishing, extending and improving. There are also these third party implementations that we know of:
|
||||
|
||||
* [Pure Python](https://github.com/halcy/blurhash-python) - Implementation of both the encoder and decoder in pure Python.
|
||||
* [One version in Go](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjJ9ueT9pXjAhXRw6YKHfrGBNcQFjAAegQIABAB&url=https%3A%2F%2Fgithub.com%2Fbbrks%2Fgo-blurhash&usg=AOvVaw2alZSHvT7HbublYbpNn9fY), and [another version in Go](https://github.com/buckket/go-blurhash).
|
||||
* [PHP](https://github.com/kornrunner/php-blurhash) - Encoder and decoder implementations in pure PHP.
|
||||
* [Java](https://github.com/hsch/blurhash-java) - Encoder implementation in Java.
|
||||
* [Clojure](https://github.com/siili-core/blurhash) - Encoder and decoder implementations in Clojure.
|
||||
* [Nim](https://github.com/SolitudeSF/blurhash) - Encoder and decoder implementation in pure Nim.
|
||||
* [Rust and WebAssembly](https://github.com/fpapado/blurhash-rust-wasm) - Encoder and decoder implementations in Rust. Distributed as both native Rust and WebAssembly packages.
|
||||
|
||||
Perhaps you'd like to help extend this list? Which brings us to...
|
||||
|
||||
## Contributing
|
||||
|
||||
@ -56,6 +67,13 @@ You can file a pull request with us, or you can start your own repo and project
|
||||
|
||||
If you do want to contribute to this project, we have a [code of conduct](CodeOfConduct.md).
|
||||
|
||||
## Users
|
||||
|
||||
Who uses BlurHash? Here are some projects we know about:
|
||||
|
||||
* [Wolt](http://wolt.com/) - We are of course using it ourselves. BlurHashes are used in the mobile clients on iOS and Android, as well as on the web, as placeholders during image loading.
|
||||
* [Mastodon](https://github.com/tootsuite/mastodon) - The Mastodon decentralised social media network uses BlurHashes both as loading placeholders, as well as for hiding media marked as sensitive.
|
||||
|
||||
## Good Questions
|
||||
|
||||
### How fast is encoding? Decoding?
|
||||
@ -89,6 +107,14 @@ and larger values will make it stronger. This is basically a design parameter, w
|
||||
|
||||
Technically, what it does is scale the AC components up or down.
|
||||
|
||||
### Is this only useful as an image loading placeholder?
|
||||
|
||||
Well, that is what it was designed for originally, but it turns out to be useful for a few other things:
|
||||
|
||||
* Masking images without having to use expensive blurs - [Mastodon](http://github.com/tootsuite/mastodon) uses it for this.
|
||||
* The data representation makes it quite easy to extract colour averages of the image for different areas. You can easily find approximations of things like the average colour of the top edge of the image, or of a corner. There is some code in the Swift BlurHashKit implementation to experiment with this. Also, the average colour of the entire image is just the DC component and can be decoded even without implementing any of the more complicated DCT stuff.
|
||||
* We have been meaning to try to implement tinted drop shadows for UI elements by using the BlurHash and extending the borders. Haven't actually had time to implement this yet though.
|
||||
|
||||
### Why base 83?
|
||||
|
||||
First, 83 seems to be about how many low-ASCII characters you can find that are safe for use in all of JSON, HTML and shells.
|
||||
@ -104,7 +130,7 @@ option. It might also be awkward to copy-paste, depending on OS capabilities.
|
||||
|
||||
If you think it can be done and is worth it, though, do make your own version and show us! We'd love to see it in action.
|
||||
|
||||
### What about other basis represenations than DCT?
|
||||
### What about other basis representations than DCT?
|
||||
|
||||
This is something we'd *love* to try. The DCT looks quite ugly when you increase the number of components, probably because
|
||||
the shape of the basis functions becomes too visible. Using a different basis with more aesthetically pleasing shape might be
|
||||
@ -118,8 +144,14 @@ to see what you can come up with!
|
||||
|
||||
* [Dag Ågren](https://github.com/DagAgren) - Original algorithm design, Swift and C implementations
|
||||
* [Mykhailo Shchurov](https://github.com/shchurov) - Kotlin decoder implementation
|
||||
* [Olli Mahlamäki](https://github.com/omahlama) - TypeScript decoder and encoder implemenations
|
||||
* [Hang Duy Khiem](https://github.com/hangduykhiem) - Android demo app
|
||||
* [Olli Mahlamäki](https://github.com/omahlama) - TypeScript decoder and encoder implementations
|
||||
* [Atte Lautanala](https://github.com/lautat) - Python integration
|
||||
* [Lorenz Diener](https://github.com/halcy) - Pure Python implementation
|
||||
* [Boris Momčilović](https://github.com/kornrunner) - Pure PHP implementation
|
||||
* [Hendrik Schnepel](https://github.com/hsch) - Java encoder implementation
|
||||
* [Tuomo Virolainen](https://github.com/tvirolai) - Clojure implementation
|
||||
* [Fotis Papadogeorgopoulos](https://github.com/fpapado) - Rust and WebAssembly implementation
|
||||
* _Your name here?_
|
||||
|
||||
## License
|
||||
|
||||
@ -22,6 +22,18 @@
|
||||
1B19DF602015E72C00D8FCD7 /* pic6.png in Resources */ = {isa = PBXBuildFile; fileRef = 1B19DF5F2015E72B00D8FCD7 /* pic6.png */; };
|
||||
1B2BA1CA1F0E5EE3006057C1 /* encode.c in Sources */ = {isa = PBXBuildFile; fileRef = 1B2BA1C81F0E5EE3006057C1 /* encode.c */; };
|
||||
1B2BA1CB1F0E5EEA006057C1 /* blurhash_stb.c in Sources */ = {isa = PBXBuildFile; fileRef = 1B2BA1C41F0E5ED6006057C1 /* blurhash_stb.c */; };
|
||||
1B6C71FD2272453D000D3BB1 /* BlurHash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60701FF40A2F00E42DD7 /* BlurHash.swift */; };
|
||||
1B6C71FE2272453D000D3BB1 /* FromString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60741FF40AF200E42DD7 /* FromString.swift */; };
|
||||
1B6C71FF2272453D000D3BB1 /* ToString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60721FF40AEA00E42DD7 /* ToString.swift */; };
|
||||
1B6C72002272453D000D3BB1 /* FromUIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60781FF40C5800E42DD7 /* FromUIImage.swift */; };
|
||||
1B6C72012272453D000D3BB1 /* ToUIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60761FF40C4C00E42DD7 /* ToUIImage.swift */; };
|
||||
1B6C72022272453D000D3BB1 /* Generation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BC34D7F20098E2F00A17481 /* Generation.swift */; };
|
||||
1B6C72032272453D000D3BB1 /* TupleMaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BC34D7D20098E1500A17481 /* TupleMaths.swift */; };
|
||||
1B6C72042272453D000D3BB1 /* ColourSpace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA607C1FF422AE00E42DD7 /* ColourSpace.swift */; };
|
||||
1B6C72052272453D000D3BB1 /* StringCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA607A1FF40D8900E42DD7 /* StringCoding.swift */; };
|
||||
1B6C72062272453D000D3BB1 /* ColourProbes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BDE56D02011EC5F00569DCB /* ColourProbes.swift */; };
|
||||
1B83DED122D88D1500CAA12F /* EscapeSequences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B83DED022D88D1500CAA12F /* EscapeSequences.swift */; };
|
||||
1B83DED222D88D1500CAA12F /* EscapeSequences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B83DED022D88D1500CAA12F /* EscapeSequences.swift */; };
|
||||
1BAA606D1FF40A0800E42DD7 /* BlurHashEncode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B49CD261EC47243006F8E7D /* BlurHashEncode.swift */; };
|
||||
1BAA606E1FF40A0B00E42DD7 /* BlurHashDecode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B49CD281EC4724C006F8E7D /* BlurHashDecode.swift */; };
|
||||
1BAA60711FF40A2F00E42DD7 /* BlurHash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BAA60701FF40A2F00E42DD7 /* BlurHash.swift */; };
|
||||
@ -69,6 +81,15 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 1;
|
||||
};
|
||||
1B6C71F42272451E000D3BB1 /* CopyFiles */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = "include/$(PRODUCT_NAME)";
|
||||
dstSubfolderSpec = 16;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@ -110,6 +131,9 @@
|
||||
1B49CD1F1EC4721A006F8E7D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
1B49CD261EC47243006F8E7D /* BlurHashEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashEncode.swift; sourceTree = "<group>"; };
|
||||
1B49CD281EC4724C006F8E7D /* BlurHashDecode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashDecode.swift; sourceTree = "<group>"; };
|
||||
1B6C71F122724500000D3BB1 /* BlurHashKit copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "BlurHashKit copy-Info.plist"; path = "/Users/dag/Code/Toot/BlurHash/BlurHashKit copy-Info.plist"; sourceTree = "<absolute>"; };
|
||||
1B6C71F62272451E000D3BB1 /* libBlurHashKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libBlurHashKit.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
1B83DED022D88D1500CAA12F /* EscapeSequences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EscapeSequences.swift; sourceTree = "<group>"; };
|
||||
1BAA60701FF40A2F00E42DD7 /* BlurHash.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurHash.swift; sourceTree = "<group>"; };
|
||||
1BAA60721FF40AEA00E42DD7 /* ToString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToString.swift; sourceTree = "<group>"; };
|
||||
1BAA60741FF40AF200E42DD7 /* FromString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FromString.swift; sourceTree = "<group>"; };
|
||||
@ -165,6 +189,13 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
1B6C71F32272451E000D3BB1 /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
@ -279,6 +310,7 @@
|
||||
1BEFFFC820C000DE00187F3F /* License.txt */,
|
||||
1BEFFFC320BFE34800187F3F /* Readme.md */,
|
||||
1B49CD1C1EC4721A006F8E7D /* Products */,
|
||||
1B6C71F122724500000D3BB1 /* BlurHashKit copy-Info.plist */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
@ -288,6 +320,7 @@
|
||||
1B49CD1B1EC4721A006F8E7D /* BlurHashKit.framework */,
|
||||
1B0A250C1EC5E90C00F25F08 /* BlurHashTest.app */,
|
||||
1B2BA1BD1F0E5EC5006057C1 /* blurhash */,
|
||||
1B6C71F62272451E000D3BB1 /* libBlurHashKit.a */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
@ -308,6 +341,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
1BAA60701FF40A2F00E42DD7 /* BlurHash.swift */,
|
||||
1B83DED022D88D1500CAA12F /* EscapeSequences.swift */,
|
||||
1BAA60741FF40AF200E42DD7 /* FromString.swift */,
|
||||
1BAA60721FF40AEA00E42DD7 /* ToString.swift */,
|
||||
1BAA60781FF40C5800E42DD7 /* FromUIImage.swift */,
|
||||
@ -433,13 +467,31 @@
|
||||
productReference = 1B49CD1B1EC4721A006F8E7D /* BlurHashKit.framework */;
|
||||
productType = "com.apple.product-type.framework";
|
||||
};
|
||||
1B6C71F52272451E000D3BB1 /* libBlurHashKit */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 1B6C71FA2272451E000D3BB1 /* Build configuration list for PBXNativeTarget "libBlurHashKit" */;
|
||||
buildPhases = (
|
||||
1B6C71F22272451E000D3BB1 /* Sources */,
|
||||
1B6C71F32272451E000D3BB1 /* Frameworks */,
|
||||
1B6C71F42272451E000D3BB1 /* CopyFiles */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = libBlurHashKit;
|
||||
productName = libBlurHash;
|
||||
productReference = 1B6C71F62272451E000D3BB1 /* libBlurHashKit.a */;
|
||||
productType = "com.apple.product-type.library.static";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
1B49CD121EC4721A006F8E7D /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0830;
|
||||
DefaultBuildSystemTypeForWorkspace = Latest;
|
||||
LastSwiftUpdateCheck = 1020;
|
||||
LastUpgradeCheck = 0930;
|
||||
ORGANIZATIONNAME = "Dag Ågren";
|
||||
TargetAttributes = {
|
||||
@ -460,6 +512,11 @@
|
||||
LastSwiftMigration = 0920;
|
||||
ProvisioningStyle = Automatic;
|
||||
};
|
||||
1B6C71F52272451E000D3BB1 = {
|
||||
CreatedOnToolsVersion = 10.2;
|
||||
DevelopmentTeam = M9KXCWYWYR;
|
||||
ProvisioningStyle = Automatic;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 1B49CD151EC4721A006F8E7D /* Build configuration list for PBXProject "BlurHash" */;
|
||||
@ -467,6 +524,7 @@
|
||||
developmentRegion = English;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
English,
|
||||
en,
|
||||
Base,
|
||||
);
|
||||
@ -476,6 +534,7 @@
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
1B49CD1A1EC4721A006F8E7D /* BlurHashKit */,
|
||||
1B6C71F52272451E000D3BB1 /* libBlurHashKit */,
|
||||
1B0A250B1EC5E90C00F25F08 /* BlurHashTest */,
|
||||
1B2BA1BC1F0E5EC5006057C1 /* blurhash */,
|
||||
);
|
||||
@ -541,6 +600,7 @@
|
||||
1BAA60751FF40AF200E42DD7 /* FromString.swift in Sources */,
|
||||
1BAA607B1FF40D8900E42DD7 /* StringCoding.swift in Sources */,
|
||||
1BAA60771FF40C4C00E42DD7 /* ToUIImage.swift in Sources */,
|
||||
1B83DED122D88D1500CAA12F /* EscapeSequences.swift in Sources */,
|
||||
1BAA60731FF40AEA00E42DD7 /* ToString.swift in Sources */,
|
||||
1BC34D8020098E2F00A17481 /* Generation.swift in Sources */,
|
||||
1BDE56D12011EC5F00569DCB /* ColourProbes.swift in Sources */,
|
||||
@ -548,6 +608,24 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
1B6C71F22272451E000D3BB1 /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
1B6C71FE2272453D000D3BB1 /* FromString.swift in Sources */,
|
||||
1B6C72022272453D000D3BB1 /* Generation.swift in Sources */,
|
||||
1B6C72032272453D000D3BB1 /* TupleMaths.swift in Sources */,
|
||||
1B6C72002272453D000D3BB1 /* FromUIImage.swift in Sources */,
|
||||
1B6C72042272453D000D3BB1 /* ColourSpace.swift in Sources */,
|
||||
1B6C72052272453D000D3BB1 /* StringCoding.swift in Sources */,
|
||||
1B83DED222D88D1500CAA12F /* EscapeSequences.swift in Sources */,
|
||||
1B6C71FF2272453D000D3BB1 /* ToString.swift in Sources */,
|
||||
1B6C71FD2272453D000D3BB1 /* BlurHash.swift in Sources */,
|
||||
1B6C72062272453D000D3BB1 /* ColourProbes.swift in Sources */,
|
||||
1B6C72012272453D000D3BB1 /* ToUIImage.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXTargetDependency section */
|
||||
@ -585,7 +663,6 @@
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
DEVELOPMENT_TEAM = 6GE6LAAR8V;
|
||||
INFOPLIST_FILE = BlurHashTest/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.wolt.BlurHashTest;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
@ -600,7 +677,6 @@
|
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||
DEVELOPMENT_TEAM = 6GE6LAAR8V;
|
||||
INFOPLIST_FILE = BlurHashTest/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.wolt.BlurHashTest;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
@ -680,7 +756,7 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.3;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
@ -736,7 +812,7 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.3;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
@ -759,7 +835,6 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = BlurHashKit/Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.wolt.BlurHashKit;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
@ -781,7 +856,6 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = BlurHashKit/Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.wolt.BlurHashKit;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
@ -790,6 +864,45 @@
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
1B6C71FB2272451E000D3BB1 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_IDENTITY = "iPhone Developer";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = M9KXCWYWYR;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
||||
MTL_FAST_MATH = YES;
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = BlurHashKit;
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
1B6C71FC2272451E000D3BB1 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_IDENTITY = "iPhone Developer";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = M9KXCWYWYR;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
MTL_FAST_MATH = YES;
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = BlurHashKit;
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
@ -829,6 +942,15 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
1B6C71FA2272451E000D3BB1 /* Build configuration list for PBXNativeTarget "libBlurHashKit" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
1B6C71FB2272451E000D3BB1 /* Debug */,
|
||||
1B6C71FC2272451E000D3BB1 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = 1B49CD121EC4721A006F8E7D /* Project object */;
|
||||
|
||||
@ -1,21 +1,21 @@
|
||||
import Foundation
|
||||
|
||||
public extension BlurHash {
|
||||
public func linearRGB(atX: Float) -> (Float, Float, Float) {
|
||||
extension BlurHash {
|
||||
public func linearRgb(atX: Float) -> (Float, Float, Float) {
|
||||
return components[0].enumerated().reduce((0, 0, 0)) { (sum, xEnumerated) in
|
||||
let (x, component) = xEnumerated
|
||||
return sum + component * cos(Float.pi * Float(x) * atX)
|
||||
}
|
||||
}
|
||||
|
||||
public func linearRGB(atY: Float) -> (Float, Float, Float) {
|
||||
public func linearRgb(atY: Float) -> (Float, Float, Float) {
|
||||
return components.enumerated().reduce((0, 0, 0)) { (sum, yEnumerated) in
|
||||
let (y, xComponents) = yEnumerated
|
||||
return sum + xComponents[0] * cos(Float.pi * Float(y) * atY)
|
||||
}
|
||||
}
|
||||
|
||||
public func linearRGB(at position: (Float, Float)) -> (Float, Float, Float) {
|
||||
public func linearRgb(at position: (Float, Float)) -> (Float, Float, Float) {
|
||||
return components.enumerated().reduce((0, 0, 0)) { (sum, yEnumerated) in
|
||||
let (y, xComponents) = yEnumerated
|
||||
return xComponents.enumerated().reduce(sum) { (sum, xEnumerated) in
|
||||
@ -25,30 +25,48 @@ public extension BlurHash {
|
||||
}
|
||||
}
|
||||
|
||||
public var averageLinearRGB: (Float, Float, Float) {
|
||||
public func linearRgb(from upperLeft: (Float, Float), to lowerRight: (Float, Float)) -> (Float, Float, Float) {
|
||||
return components.enumerated().reduce((0, 0, 0)) { (sum, yEnumerated) in
|
||||
let (y, xComponents) = yEnumerated
|
||||
return xComponents.enumerated().reduce(sum) { (sum, xEnumerated) in
|
||||
let (x, component) = xEnumerated
|
||||
let horizontalAverage: Float = x == 0 ? 1 : (sin(Float.pi * Float(x) * lowerRight.0) - sin(Float.pi * Float(x) * upperLeft.0)) / (Float(x) * Float.pi * (lowerRight.0 - upperLeft.0))
|
||||
let veritcalAverage: Float = y == 0 ? 1 : (sin(Float.pi * Float(y) * lowerRight.1) - sin(Float.pi * Float(y) * upperLeft.1)) / (Float(y) * Float.pi * (lowerRight.1 - upperLeft.1))
|
||||
return sum + component * horizontalAverage * veritcalAverage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func linearRgb(at upperLeft: (Float, Float), size: (Float, Float)) -> (Float, Float, Float) {
|
||||
return linearRgb(from: upperLeft, to: (upperLeft.0 + size.0, upperLeft.1 + size.1))
|
||||
}
|
||||
|
||||
public var averageLinearRgb: (Float, Float, Float) {
|
||||
return components[0][0]
|
||||
}
|
||||
|
||||
public var leftEdgeLinearRGB: (Float, Float, Float) { return linearRGB(atX: 0) }
|
||||
public var rightEdgeLinearRGB: (Float, Float, Float) { return linearRGB(atX: 1) }
|
||||
public var topEdgeLinearRGB: (Float, Float, Float) { return linearRGB(atY: 0) }
|
||||
public var bottomEdgeLinearRGB: (Float, Float, Float) { return linearRGB(atY: 1) }
|
||||
public var topLeftCornerLinearRGB: (Float, Float, Float) { return linearRGB(at: (0, 0)) }
|
||||
public var topRightCornerLinearRGB: (Float, Float, Float) { return linearRGB(at: (1, 0)) }
|
||||
public var bottomLeftCornerLinearRGB: (Float, Float, Float) { return linearRGB(at: (0, 1)) }
|
||||
public var bottomRightCornerLinearRGB: (Float, Float, Float) { return linearRGB(at: (1, 1)) }
|
||||
public var leftEdgeLinearRgb: (Float, Float, Float) { return linearRgb(atX: 0) }
|
||||
public var rightEdgeLinearRgb: (Float, Float, Float) { return linearRgb(atX: 1) }
|
||||
public var topEdgeLinearRgb: (Float, Float, Float) { return linearRgb(atY: 0) }
|
||||
public var bottomEdgeLinearRgb: (Float, Float, Float) { return linearRgb(atY: 1) }
|
||||
public var topLeftCornerLinearRgb: (Float, Float, Float) { return linearRgb(at: (0, 0)) }
|
||||
public var topRightCornerLinearRgb: (Float, Float, Float) { return linearRgb(at: (1, 0)) }
|
||||
public var bottomLeftCornerLinearRgb: (Float, Float, Float) { return linearRgb(at: (0, 1)) }
|
||||
public var bottomRightCornerLinearRgb: (Float, Float, Float) { return linearRgb(at: (1, 1)) }
|
||||
}
|
||||
|
||||
public extension BlurHash {
|
||||
public func isDark(linearRGB rgb: (Float, Float, Float)) -> Bool {
|
||||
extension BlurHash {
|
||||
public func isDark(linearRgb rgb: (Float, Float, Float)) -> Bool {
|
||||
return rgb.0 * 0.299 + rgb.1 * 0.587 + rgb.2 * 0.114 < 0.5
|
||||
}
|
||||
|
||||
public var isDark: Bool { return isDark(linearRGB: averageLinearRGB) }
|
||||
public var isDark: Bool { return isDark(linearRgb: averageLinearRgb) }
|
||||
|
||||
public func isDark(atX x: Float) -> Bool { return isDark(linearRGB: linearRGB(atX: x)) }
|
||||
public func isDark(atY y: Float) -> Bool { return isDark(linearRGB: linearRGB(atY: y)) }
|
||||
public func isDark(at position: (Float, Float)) -> Bool { return isDark(linearRGB: linearRGB(at: position)) }
|
||||
public func isDark(atX x: Float) -> Bool { return isDark(linearRgb: linearRgb(atX: x)) }
|
||||
public func isDark(atY y: Float) -> Bool { return isDark(linearRgb: linearRgb(atY: y)) }
|
||||
public func isDark(at position: (Float, Float)) -> Bool { return isDark(linearRgb: linearRgb(at: position)) }
|
||||
public func isDark(from upperLeft: (Float, Float), to lowerRight: (Float, Float)) -> Bool { return isDark(linearRgb: linearRgb(from: upperLeft, to: lowerRight)) }
|
||||
public func isDark(at upperLeft: (Float, Float), size: (Float, Float)) -> Bool { return isDark(linearRgb: linearRgb(at: upperLeft, size: size)) }
|
||||
|
||||
public var isLeftEdgeDark: Bool { return isDark(atX: 0) }
|
||||
public var isRightEdgeDark: Bool { return isDark(atX: 1) }
|
||||
|
||||
36
Swift/BlurHashKit/EscapeSequences.swift
Normal file
@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
|
||||
extension BlurHash {
|
||||
var twoByThreeEscapeSequence: String {
|
||||
let areas: [(from: (Float, Float), to: (Float, Float))] = [
|
||||
(from: (0, 0), to: (0.333, 0.5)),
|
||||
(from: (0, 0.5), to: (0.333, 1.0)),
|
||||
(from: (0.333, 0), to: (0.666, 0.5)),
|
||||
(from: (0.333, 0.5), to: (0.666, 1.0)),
|
||||
(from: (0.666, 0), to: (1.0, 0.5)),
|
||||
(from: (0.666, 0.5), to: (1.0, 1.0)),
|
||||
]
|
||||
|
||||
let rgb: [(Float, Float, Float)] = areas.map { area in
|
||||
linearRgb(from: area.from, to: area.to)
|
||||
}
|
||||
|
||||
let maxRgb: (Float, Float, Float) = rgb.reduce((-Float.infinity, -Float.infinity, -Float.infinity), max)
|
||||
let minRgb: (Float, Float, Float) = rgb.reduce((Float.infinity, Float.infinity, Float.infinity), min)
|
||||
|
||||
let positiveScale: (Float, Float, Float) = ((1, 1, 1) - averageLinearRgb) / (maxRgb - averageLinearRgb)
|
||||
let negativeScale: (Float, Float, Float) = averageLinearRgb / (averageLinearRgb - minRgb)
|
||||
let scale: (Float, Float, Float) = min(positiveScale, negativeScale)
|
||||
|
||||
let scaledRgb: [(Float, Float, Float)] = rgb.map { rgb in
|
||||
return (rgb - averageLinearRgb) * scale + averageLinearRgb
|
||||
}
|
||||
|
||||
let c = scaledRgb.map { rgb in
|
||||
return (linearTosRGB(rgb.0) / 51) * 36 + (linearTosRGB(rgb.1) / 51) * 6 + (linearTosRGB(rgb.2) / 51) + 16
|
||||
}
|
||||
|
||||
return "\u{1b}[38;5;\(c[1]);48;5;\(c[0])m▄\u{1b}[38;5;\(c[3]);48;5;\(c[2])m▄\u{1b}[38;5;\(c[5]);48;5;\(c[4])m▄\u{1b}[m"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import UIKit
|
||||
|
||||
public extension BlurHash {
|
||||
extension BlurHash {
|
||||
public init(horizontalGradientFrom leftColour: UIColor, to rightColour: UIColor) {
|
||||
let average = (leftColour.linear + rightColour.linear) / 2
|
||||
let difference = (leftColour.linear - rightColour.linear) / 2
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import UIKit
|
||||
|
||||
public extension BlurHash {
|
||||
extension BlurHash {
|
||||
public func cgImage(size: CGSize) -> CGImage? {
|
||||
let width = Int(size.width)
|
||||
let height = Int(size.height)
|
||||
@ -42,22 +42,40 @@ public extension BlurHash {
|
||||
return cgImage
|
||||
}
|
||||
|
||||
public func cgImage(numberOfPixels: Int = 1024, originalSize size: CGSize) -> CGImage? {
|
||||
let width: CGFloat
|
||||
let height: CGFloat
|
||||
if size.width > size.height {
|
||||
width = floor(sqrt(CGFloat(numberOfPixels) * size.width / size.height) + 0.5)
|
||||
height = floor(CGFloat(numberOfPixels) / width + 0.5)
|
||||
} else {
|
||||
height = floor(sqrt(CGFloat(numberOfPixels) * size.height / size.width) + 0.5)
|
||||
width = floor(CGFloat(numberOfPixels) / height + 0.5)
|
||||
}
|
||||
return cgImage(size: CGSize(width: width, height: height))
|
||||
}
|
||||
|
||||
public func image(size: CGSize) -> UIImage? {
|
||||
guard let cgImage = cgImage(size: size) else { return nil }
|
||||
return UIImage(cgImage: cgImage)
|
||||
}
|
||||
|
||||
public func image(numberOfPixels: Int = 1024, originalSize size: CGSize) -> UIImage? {
|
||||
let width: CGFloat
|
||||
let height: CGFloat
|
||||
if size.width > size.height {
|
||||
width = floor(sqrt(CGFloat(numberOfPixels)) * size.width / size.height + 0.5)
|
||||
height = floor(CGFloat(numberOfPixels) / width + 0.5)
|
||||
} else {
|
||||
height = floor(sqrt(CGFloat(numberOfPixels)) * size.height / size.width + 0.5)
|
||||
width = floor(CGFloat(numberOfPixels) / height + 0.5)
|
||||
}
|
||||
return image(size: CGSize(width: width, height: height))
|
||||
guard let cgImage = cgImage(numberOfPixels: numberOfPixels, originalSize: size) else { return nil }
|
||||
return UIImage(cgImage: cgImage)
|
||||
}
|
||||
}
|
||||
|
||||
@objc extension UIImage {
|
||||
public convenience init?(blurHash string: String, size: CGSize, punch: Float = 1) {
|
||||
guard let blurHash = BlurHash(string: string),
|
||||
let cgImage = blurHash.punch(punch).cgImage(size: size) else { return nil }
|
||||
self.init(cgImage: cgImage)
|
||||
}
|
||||
|
||||
public convenience init?(blurHash string: String, numberOfPixels: Int = 1024, originalSize size: CGSize, punch: Float = 1) {
|
||||
guard let blurHash = BlurHash(string: string),
|
||||
let cgImage = blurHash.punch(punch).cgImage(numberOfPixels: numberOfPixels, originalSize: size) else { return nil }
|
||||
self.init(cgImage: cgImage)
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,10 @@ func -(lhs: (Float, Float, Float), rhs: (Float, Float, Float)) -> (Float, Float,
|
||||
return (lhs.0 - rhs.0, lhs.1 - rhs.1, lhs.2 - rhs.2)
|
||||
}
|
||||
|
||||
func *(lhs: (Float, Float, Float), rhs: (Float, Float, Float)) -> (Float, Float, Float) {
|
||||
return (lhs.0 * rhs.0, lhs.1 * rhs.1, lhs.2 * rhs.2)
|
||||
}
|
||||
|
||||
func *(lhs: (Float, Float, Float), rhs: Float) -> (Float, Float, Float) {
|
||||
return (lhs.0 * rhs, lhs.1 * rhs, lhs.2 * rhs)
|
||||
}
|
||||
@ -16,6 +20,10 @@ func *(lhs: Float, rhs: (Float, Float, Float)) -> (Float, Float, Float) {
|
||||
return (lhs * rhs.0, lhs * rhs.1, lhs * rhs.2)
|
||||
}
|
||||
|
||||
func /(lhs: (Float, Float, Float), rhs: (Float, Float, Float)) -> (Float, Float, Float) {
|
||||
return (lhs.0 / rhs.0, lhs.1 / rhs.1, lhs.2 / rhs.2)
|
||||
}
|
||||
|
||||
func /(lhs: (Float, Float, Float), rhs: Float) -> (Float, Float, Float) {
|
||||
return (lhs.0 / rhs, lhs.1 / rhs, lhs.2 / rhs)
|
||||
}
|
||||
@ -35,3 +43,11 @@ func *=(lhs: inout (Float, Float, Float), rhs: Float) {
|
||||
func /=(lhs: inout (Float, Float, Float), rhs: Float) {
|
||||
lhs = lhs / rhs
|
||||
}
|
||||
|
||||
func min(_ a: (Float, Float, Float), _ b: (Float, Float, Float)) -> (Float, Float, Float) {
|
||||
return (min(a.0, b.0), min(a.1, b.1), min(a.2, b.2))
|
||||
}
|
||||
|
||||
func max(_ a: (Float, Float, Float), _ b: (Float, Float, Float)) -> (Float, Float, Float) {
|
||||
return (max(a.0, b.0), max(a.1, b.1), max(a.2, b.2))
|
||||
}
|
||||
|
||||
@ -2,13 +2,13 @@
|
||||
|
||||
## Standalone decoder and encoder
|
||||
|
||||
[BlurHashDecoder.swift](BlurHashDecoder.swift) and [BlurHashEncoder.swift](BlurHashEncoder.swift) contain a decoder
|
||||
[BlurHashDecode.swift](BlurHashDecode.swift) and [BlurHashEncode.swift](BlurHashEncode.swift) contain a decoder
|
||||
and encoder for BlurHash to and from `UIImage`. Both files are completeiy standalone, and can simply be copied into your
|
||||
project directly.
|
||||
|
||||
### Decoding
|
||||
|
||||
[BlurHashDecoder.swift](BlurHashDecoder.swift) implements the following extension on `UIImage`:
|
||||
[BlurHashDecode.swift](BlurHashDecode.swift) implements the following extension on `UIImage`:
|
||||
|
||||
public convenience init?(blurHash: String, size: CGSize, punch: Float = 1)
|
||||
|
||||
@ -21,7 +21,7 @@ The parameters are:
|
||||
|
||||
### Encoding
|
||||
|
||||
[BlurHashEncoder.swift](BlurHashEncoder.swift) implements the following extension on `UIImage`:
|
||||
[BlurHashEncode.swift](BlurHashEncode.swift) implements the following extension on `UIImage`:
|
||||
|
||||
public func blurHash(numberOfComponents components: (Int, Int)) -> String?
|
||||
|
||||
|
||||
2
TypeScript/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
dist/
|
||||
9
TypeScript/.prettierrc
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"printWidth": 80,
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["*.ts"],
|
||||
"options": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
10
TypeScript/CHANGELOG.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## 1.1.2 (June 29, 2019)
|
||||
|
||||
- added `isBlurhashValid()` utility
|
||||
|
||||
## 1.1.1 (June 29, 2019)
|
||||
|
||||
- fixed incorrect type declaration path in package.json
|
||||
- improved error handling
|
||||
78
TypeScript/README.md
Normal file
@ -0,0 +1,78 @@
|
||||
# blurhash
|
||||
|
||||
[](https://npmjs.org/package/blurhash)
|
||||
[](https://npmjs.org/package/blurhash)
|
||||
|
||||
> JavaScript encoder and decoder for the [Wolt BlurHash](https://github.com/woltapp/blurhash) algorithm
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install --save blurhash
|
||||
```
|
||||
|
||||
See [react-blurhash](https://github.com/woltapp/react-blurhash) to use blurhash with React.
|
||||
|
||||
## API
|
||||
|
||||
### `decode(blurhash: string, width: number, height: number, punch?: number) => Uint8ClampedArray`
|
||||
|
||||
> Decodes a blurhash string to pixels
|
||||
|
||||
#### Example
|
||||
|
||||
```js
|
||||
import { decode } from "blurhash";
|
||||
|
||||
const pixels = decode("LEHV6nWB2yk8pyo0adR*.7kCMdnj", 32, 32);
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
const imageData = ctx.createImageData(width, height);
|
||||
imageData.data.set(pixels);
|
||||
ctx.putImageData(imageData, 0, 0);
|
||||
document.body.append(canvas);
|
||||
```
|
||||
|
||||
### `encode(pixels: Uint8ClampedArray, width: number, height: number, componentX: number, componentY: number) => string`
|
||||
|
||||
> Encodes pixels to a blurhash string
|
||||
|
||||
```js
|
||||
import { encode } from "blurhash";
|
||||
|
||||
const loadImage = async src =>
|
||||
new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = (...args) => reject(args);
|
||||
img.src = src;
|
||||
});
|
||||
|
||||
const getImageData = image => {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = image.width;
|
||||
canvas.height = image.height;
|
||||
const context = canvas.getContext("2d");
|
||||
context.drawImage(image, 0, 0);
|
||||
return context.getImageData(0, 0, image.width, image.height);
|
||||
};
|
||||
|
||||
const encodeImageToBlurhash = async imageUrl => {
|
||||
const image = await loadImage(imageUrl);
|
||||
const imageData = getImageData(image);
|
||||
return encode(imageData.data, imageData.width, imageData.height, 4, 4);
|
||||
};
|
||||
```
|
||||
|
||||
### `isBlurhashValid(blurhash: string) => { result: boolean; errorReason?: string }`
|
||||
|
||||
```js
|
||||
import { isBlurhashValid } from "blurhash";
|
||||
|
||||
const validRes = isBlurhashValid("LEHV6nWB2yk8pyo0adR*.7kCMdnj");
|
||||
// { result: true }
|
||||
|
||||
const invalidRes = isBlurhashValid("???");
|
||||
// { result: false, errorReason: "The blurhash string must be at least 6 characters" }
|
||||
```
|
||||
@ -1 +0,0 @@
|
||||
# BlurHash in TypeScript
|
||||
2
TypeScript/dist/decode.d.ts
vendored
@ -1,2 +0,0 @@
|
||||
declare const decode: (blurhash: string, width: number, height: number, punch?: number) => Uint8ClampedArray;
|
||||
export default decode;
|
||||
76
TypeScript/dist/decode.js
vendored
@ -1,76 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var base83_1 = require("./base83");
|
||||
var utils_1 = require("./utils");
|
||||
var decodeDC = function (value) {
|
||||
var intR = value >> 16;
|
||||
var intG = (value >> 8) & 255;
|
||||
var intB = value & 255;
|
||||
return [utils_1.sRGBToLinear(intR), utils_1.sRGBToLinear(intG), utils_1.sRGBToLinear(intB)];
|
||||
};
|
||||
var decodeAC = function (value, maximumValue) {
|
||||
var quantR = Math.floor(value / (19 * 19));
|
||||
var quantG = Math.floor(value / 19) % 19;
|
||||
var quantB = value % 19;
|
||||
var rgb = [
|
||||
utils_1.signPow((quantR - 9) / 9, 2.0) * maximumValue,
|
||||
utils_1.signPow((quantG - 9) / 9, 2.0) * maximumValue,
|
||||
utils_1.signPow((quantB - 9) / 9, 2.0) * maximumValue,
|
||||
];
|
||||
return rgb;
|
||||
};
|
||||
var decode = function (blurhash, width, height, punch) {
|
||||
punch = punch | 1;
|
||||
if (blurhash.length < 6) {
|
||||
console.error('too short blurhash');
|
||||
return null;
|
||||
}
|
||||
var sizeFlag = base83_1.decode83(blurhash[0]);
|
||||
var numY = Math.floor(sizeFlag / 9) + 1;
|
||||
var numX = (sizeFlag % 9) + 1;
|
||||
var quantisedMaximumValue = base83_1.decode83(blurhash[1]);
|
||||
var maximumValue = (quantisedMaximumValue + 1) / 166;
|
||||
if (blurhash.length !== 4 + 2 * numX * numY) {
|
||||
console.error('blurhash length mismatch', blurhash.length, 4 + 2 * numX * numY);
|
||||
return null;
|
||||
}
|
||||
var colors = new Array(numX * numY);
|
||||
for (var i = 0; i < colors.length; i++) {
|
||||
if (i === 0) {
|
||||
var value = base83_1.decode83(blurhash.substring(2, 6));
|
||||
colors[i] = decodeDC(value);
|
||||
}
|
||||
else {
|
||||
var value = base83_1.decode83(blurhash.substring(4 + i * 2, 6 + i * 2));
|
||||
colors[i] = decodeAC(value, maximumValue * punch);
|
||||
}
|
||||
}
|
||||
var bytesPerRow = width * 4;
|
||||
var pixels = new Uint8ClampedArray(bytesPerRow * height);
|
||||
for (var y = 0; y < height; y++) {
|
||||
for (var x = 0; x < width; x++) {
|
||||
var r = 0;
|
||||
var g = 0;
|
||||
var b = 0;
|
||||
for (var j = 0; j < numY; j++) {
|
||||
for (var i = 0; i < numX; i++) {
|
||||
var basis = Math.cos(Math.PI * x * i / width) * Math.cos(Math.PI * y * j / height);
|
||||
var color = colors[i + j * numX];
|
||||
r += color[0] * basis;
|
||||
g += color[1] * basis;
|
||||
b += color[2] * basis;
|
||||
}
|
||||
}
|
||||
var intR = utils_1.linearTosRGB(r);
|
||||
var intG = utils_1.linearTosRGB(g);
|
||||
var intB = utils_1.linearTosRGB(b);
|
||||
pixels[4 * x + 0 + y * bytesPerRow] = intR;
|
||||
pixels[4 * x + 1 + y * bytesPerRow] = intG;
|
||||
pixels[4 * x + 2 + y * bytesPerRow] = intB;
|
||||
pixels[4 * x + 3 + y * bytesPerRow] = 255; // alpha
|
||||
}
|
||||
}
|
||||
return pixels;
|
||||
};
|
||||
exports.default = decode;
|
||||
//# sourceMappingURL=decode.js.map
|
||||
1
TypeScript/dist/decode.js.map
vendored
@ -1 +0,0 @@
|
||||
{"version":3,"file":"decode.js","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":";;AAAA,mCAAoC;AACpC,iCAA8D;AAE9D,IAAM,QAAQ,GAAG,UAAC,KAAa;IAC7B,IAAM,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;IACzB,IAAM,IAAI,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC;IAChC,IAAM,IAAI,GAAG,KAAK,GAAG,GAAG,CAAC;IACzB,MAAM,CAAC,CAAC,oBAAY,CAAC,IAAI,CAAC,EAAE,oBAAY,CAAC,IAAI,CAAC,EAAE,oBAAY,CAAC,IAAI,CAAC,CAAC,CAAC;AACtE,CAAC,CAAC;AAEF,IAAM,QAAQ,GAAG,UAAC,KAAa,EAAE,YAAoB;IACnD,IAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IAC7C,IAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IAC3C,IAAM,MAAM,GAAG,KAAK,GAAG,EAAE,CAAC;IAE1B,IAAM,GAAG,GAAG;QACV,eAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,YAAY;QAC7C,eAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,YAAY;QAC7C,eAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,YAAY;KAC9C,CAAC;IAEF,MAAM,CAAC,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,IAAM,MAAM,GAAG,UAAC,QAAgB,EAAE,KAAa,EAAE,MAAc,EAAE,KAAc;IAC7E,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC;IAElB,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QACxB,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACpC,MAAM,CAAC,IAAI,CAAC;IACd,CAAC;IAED,IAAM,QAAQ,GAAG,iBAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IACvC,IAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAC1C,IAAM,IAAI,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAEhC,IAAM,qBAAqB,GAAG,iBAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,IAAM,YAAY,GAAG,CAAC,qBAAqB,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC;IAEvD,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;QAC5C,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC;QAChF,MAAM,CAAC,IAAI,CAAC;IACd,CAAC;IAED,IAAM,MAAM,GAAG,IAAI,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IACtC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACZ,IAAM,KAAK,GAAG,iBAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACjD,MAAM,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;QAAC,IAAI,CAAC,CAAC;YACN,IAAM,KAAK,GAAG,iBAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACjE,MAAM,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,KAAK,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;IAED,IAAM,WAAW,GAAG,KAAK,GAAG,CAAC,CAAC;IAC9B,IAAM,MAAM,GAAG,IAAI,iBAAiB,CAAC,WAAW,GAAG,MAAM,CAAC,CAAC;IAE3D,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAChC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/B,IAAI,CAAC,GAAG,CAAC,CAAC;YACV,IAAI,CAAC,GAAG,CAAC,CAAC;YACV,IAAI,CAAC,GAAG,CAAC,CAAC;YAEV,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;gBAC9B,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;oBAC9B,IAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;oBACrF,IAAI,KAAK,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;oBACjC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;oBACtB,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;oBACtB,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;gBACxB,CAAC;YACH,CAAC;YAED,IAAI,IAAI,GAAG,oBAAY,CAAC,CAAC,CAAC,CAAC;YAC3B,IAAI,IAAI,GAAG,oBAAY,CAAC,CAAC,CAAC,CAAC;YAC3B,IAAI,IAAI,GAAG,oBAAY,CAAC,CAAC,CAAC,CAAC;YAE3B,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,IAAI,CAAC;YAC3C,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,IAAI,CAAC;YAC3C,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,IAAI,CAAC;YAC3C,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC,QAAQ;QACrD,CAAC;IACH,CAAC;IACD,MAAM,CAAC,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
||||
2
TypeScript/dist/index.d.ts
vendored
@ -1,2 +0,0 @@
|
||||
export { default as decode } from './decode';
|
||||
export { default as encode } from './encode';
|
||||
7
TypeScript/dist/index.js
vendored
@ -1,7 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var decode_1 = require("./decode");
|
||||
exports.decode = decode_1.default;
|
||||
var encode_1 = require("./encode");
|
||||
exports.encode = encode_1.default;
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
TypeScript/dist/index.js.map
vendored
@ -1 +0,0 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,mCAA6C;AAApC,0BAAA,OAAO,CAAU;AAC1B,mCAA6C;AAApC,0BAAA,OAAO,CAAU"}
|
||||
4
TypeScript/dist/utils.d.ts
vendored
@ -1,4 +0,0 @@
|
||||
export declare const sRGBToLinear: (value: number) => number;
|
||||
export declare const linearTosRGB: (value: number) => number;
|
||||
export declare const sign: (n: number) => 1 | -1;
|
||||
export declare const signPow: (val: number, exp: number) => number;
|
||||
23
TypeScript/dist/utils.js
vendored
@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.sRGBToLinear = function (value) {
|
||||
var v = value / 255;
|
||||
if (v <= 0.04045) {
|
||||
return v / 12.92;
|
||||
}
|
||||
else {
|
||||
return Math.pow((v + 0.055) / 1.055, 2.4);
|
||||
}
|
||||
};
|
||||
exports.linearTosRGB = function (value) {
|
||||
var v = Math.max(0, Math.min(1, value));
|
||||
if (v <= 0.0031308) {
|
||||
return Math.round(v * 12.92 * 255 + 0.5);
|
||||
}
|
||||
else {
|
||||
return Math.round((1.055 * Math.pow(v, 1 / 2.4) - 0.055) * 255 + 0.5);
|
||||
}
|
||||
};
|
||||
exports.sign = function (n) { return (n < 0 ? -1 : 1); };
|
||||
exports.signPow = function (val, exp) { return exports.sign(val) * Math.pow(Math.abs(val), exp); };
|
||||
//# sourceMappingURL=utils.js.map
|
||||
1
TypeScript/dist/utils.js.map
vendored
@ -1 +0,0 @@
|
||||
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;AAAa,QAAA,YAAY,GAAG,UAAC,KAAa;IACxC,IAAI,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC;IACpB,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC;QACjB,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC;IACnB,CAAC;IAAC,IAAI,CAAC,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC,CAAC;AAEW,QAAA,YAAY,GAAG,UAAC,KAAa;IACxC,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IACxC,EAAE,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC;QACnB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,KAAK,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;IAC3C,CAAC;IAAC,IAAI,CAAC,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;IACxE,CAAC;AACH,CAAC,CAAC;AAEW,QAAA,IAAI,GAAG,UAAC,CAAS,IAAK,OAAA,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAhB,CAAgB,CAAC;AAEvC,QAAA,OAAO,GAAG,UAAC,GAAW,EAAE,GAAW,IAAK,OAAA,YAAI,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAxC,CAAwC,CAAC"}
|
||||
@ -1,22 +1,37 @@
|
||||
{
|
||||
"name": "blurhash",
|
||||
"version": "1.0.0",
|
||||
"description": "Decoder for the Wolt BlurHash alrgorithm.",
|
||||
"version": "1.1.3",
|
||||
"description": "Encoder and decoder for the Wolt BlurHash algorithm.",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.t.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/woltapp/blurhash/tree/master/TypeScript"
|
||||
},
|
||||
"homepage": "http://blurhash.com",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"demo": "webpack-dev-server"
|
||||
"prepublishOnly": "npm run build",
|
||||
"build": "npm run ts",
|
||||
"demo": "webpack-dev-server --mode development",
|
||||
"prettier": "prettier src/**/*.ts",
|
||||
"prettier-fix": "npm run prettier -- --write",
|
||||
"ts": "tsc",
|
||||
"ts:watch": "npm run ts -- --noEmit --watch"
|
||||
},
|
||||
"keywords": [
|
||||
"blurhash"
|
||||
"blurhash",
|
||||
"blur",
|
||||
"hash",
|
||||
"image"
|
||||
],
|
||||
"author": "omahlama",
|
||||
"license": "ISC",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"ts-loader": "3.2.0",
|
||||
"typescript": "2.6.2",
|
||||
"webpack": "3.10.0",
|
||||
"webpack-dev-server": "2.9.7"
|
||||
"prettier": "1.18.2",
|
||||
"ts-loader": "6.0.4",
|
||||
"typescript": "3.5.3",
|
||||
"webpack": "4.38.0",
|
||||
"webpack-cli": "^3.3.6",
|
||||
"webpack-dev-server": "3.7.2"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,30 +1,104 @@
|
||||
const digitCharacters = [
|
||||
"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", "#", "$", "%", "*", "+", ",", "-", ".",
|
||||
":", ";", "=", "?", "@", "[", "]", "^", "_", "{",
|
||||
"|", "}", "~",
|
||||
]
|
||||
"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",
|
||||
"#",
|
||||
"$",
|
||||
"%",
|
||||
"*",
|
||||
"+",
|
||||
",",
|
||||
"-",
|
||||
".",
|
||||
":",
|
||||
";",
|
||||
"=",
|
||||
"?",
|
||||
"@",
|
||||
"[",
|
||||
"]",
|
||||
"^",
|
||||
"_",
|
||||
"{",
|
||||
"|",
|
||||
"}",
|
||||
"~"
|
||||
];
|
||||
|
||||
export const decode83 = (str: String) => {
|
||||
let value = 0;
|
||||
for(let i=0; i<str.length; i++) {
|
||||
const c = str[i];
|
||||
const digit = digitCharacters.indexOf(c);
|
||||
value = value * 83 + digit;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
let value = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const c = str[i];
|
||||
const digit = digitCharacters.indexOf(c);
|
||||
value = value * 83 + digit;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
export const encode83 = (n: number, length: number): string => {
|
||||
var result = ""
|
||||
for(let i=1; i<=length; i++) {
|
||||
let digit = (Math.floor(n) / Math.pow(83, length-i)) % 83;
|
||||
result += digitCharacters[Math.floor(digit)];
|
||||
}
|
||||
return result
|
||||
}
|
||||
var result = "";
|
||||
for (let i = 1; i <= length; i++) {
|
||||
let digit = (Math.floor(n) / Math.pow(83, length - i)) % 83;
|
||||
result += digitCharacters[Math.floor(digit)];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
@ -1,5 +1,42 @@
|
||||
import { decode83 } from './base83';
|
||||
import { sRGBToLinear, signPow, linearTosRGB } from './utils';
|
||||
import { decode83 } from "./base83";
|
||||
import { sRGBToLinear, signPow, linearTosRGB } from "./utils";
|
||||
import { ValidationError } from "./error";
|
||||
|
||||
/**
|
||||
* Returns an error message if invalid or undefined if valid
|
||||
* @param blurhash
|
||||
*/
|
||||
const validateBlurhash = (blurhash: string) => {
|
||||
if (!blurhash || blurhash.length < 6) {
|
||||
throw new ValidationError(
|
||||
"The blurhash string must be at least 6 characters"
|
||||
);
|
||||
}
|
||||
|
||||
const sizeFlag = decode83(blurhash[0]);
|
||||
const numY = Math.floor(sizeFlag / 9) + 1;
|
||||
const numX = (sizeFlag % 9) + 1;
|
||||
|
||||
if (blurhash.length !== 4 + 2 * numX * numY) {
|
||||
throw new ValidationError(
|
||||
`blurhash length mismatch: length is ${
|
||||
blurhash.length
|
||||
} but it should be ${4 + 2 * numX * numY}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export const isBlurhashValid = (
|
||||
blurhash: string
|
||||
): { result: boolean; errorReason?: string } => {
|
||||
try {
|
||||
validateBlurhash(blurhash);
|
||||
} catch (error) {
|
||||
return { result: false, errorReason: error.message };
|
||||
}
|
||||
|
||||
return { result: true };
|
||||
};
|
||||
|
||||
const decodeDC = (value: number) => {
|
||||
const intR = value >> 16;
|
||||
@ -16,19 +53,21 @@ const decodeAC = (value: number, maximumValue: number) => {
|
||||
const rgb = [
|
||||
signPow((quantR - 9) / 9, 2.0) * maximumValue,
|
||||
signPow((quantG - 9) / 9, 2.0) * maximumValue,
|
||||
signPow((quantB - 9) / 9, 2.0) * maximumValue,
|
||||
signPow((quantB - 9) / 9, 2.0) * maximumValue
|
||||
];
|
||||
|
||||
return rgb;
|
||||
};
|
||||
|
||||
const decode = (blurhash: string, width: number, height: number, punch?: number) => {
|
||||
punch = punch | 1;
|
||||
const decode = (
|
||||
blurhash: string,
|
||||
width: number,
|
||||
height: number,
|
||||
punch?: number
|
||||
) => {
|
||||
validateBlurhash(blurhash);
|
||||
|
||||
if (blurhash.length < 6) {
|
||||
console.error('too short blurhash');
|
||||
return null;
|
||||
}
|
||||
punch = punch | 1;
|
||||
|
||||
const sizeFlag = decode83(blurhash[0]);
|
||||
const numY = Math.floor(sizeFlag / 9) + 1;
|
||||
@ -37,12 +76,8 @@ const decode = (blurhash: string, width: number, height: number, punch?: number)
|
||||
const quantisedMaximumValue = decode83(blurhash[1]);
|
||||
const maximumValue = (quantisedMaximumValue + 1) / 166;
|
||||
|
||||
if (blurhash.length !== 4 + 2 * numX * numY) {
|
||||
console.error('blurhash length mismatch', blurhash.length, 4 + 2 * numX * numY);
|
||||
return null;
|
||||
}
|
||||
|
||||
const colors = new Array(numX * numY);
|
||||
|
||||
for (let i = 0; i < colors.length; i++) {
|
||||
if (i === 0) {
|
||||
const value = decode83(blurhash.substring(2, 6));
|
||||
@ -64,7 +99,9 @@ const decode = (blurhash: string, width: number, height: number, punch?: number)
|
||||
|
||||
for (let j = 0; j < numY; j++) {
|
||||
for (let i = 0; i < numX; i++) {
|
||||
const basis = Math.cos(Math.PI * x * i / width) * Math.cos(Math.PI * y * j / height);
|
||||
const basis =
|
||||
Math.cos((Math.PI * x * i) / width) *
|
||||
Math.cos((Math.PI * y * j) / height);
|
||||
let color = colors[i + j * numX];
|
||||
r += color[0] * basis;
|
||||
g += color[1] * basis;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { decode83, encode83 } from "./base83";
|
||||
import { encode83 } from "./base83";
|
||||
import { sRGBToLinear, signPow, linearTosRGB } from "./utils";
|
||||
import { ValidationError } from "./error";
|
||||
|
||||
type NumberTriplet = [number, number, number];
|
||||
|
||||
@ -9,7 +10,7 @@ const multiplyBasisFunction = (
|
||||
pixels: Uint8ClampedArray,
|
||||
width: number,
|
||||
height: number,
|
||||
basisFunction: ((i: number, j: number) => number)
|
||||
basisFunction: (i: number, j: number) => number
|
||||
): NumberTriplet => {
|
||||
let r = 0;
|
||||
let g = 0;
|
||||
@ -71,10 +72,10 @@ const encode = (
|
||||
componentY: number
|
||||
): string => {
|
||||
if (componentX < 1 || componentX > 9 || componentY < 1 || componentY > 9) {
|
||||
throw new Error("BlurHash must have between 1 and 9 components");
|
||||
throw new ValidationError("BlurHash must have between 1 and 9 components");
|
||||
}
|
||||
if (width * height * 4 !== pixels.length) {
|
||||
throw new Error("Width and height must match the pixels array");
|
||||
throw new ValidationError("Width and height must match the pixels array");
|
||||
}
|
||||
|
||||
let factors: Array<[number, number, number]> = [];
|
||||
@ -87,8 +88,8 @@ const encode = (
|
||||
height,
|
||||
(i: number, j: number) =>
|
||||
normalisation *
|
||||
Math.cos(Math.PI * x * i / width) *
|
||||
Math.cos(Math.PI * y * j / height)
|
||||
Math.cos((Math.PI * x * i) / width) *
|
||||
Math.cos((Math.PI * y * j) / height)
|
||||
);
|
||||
factors.push(factor);
|
||||
}
|
||||
7
TypeScript/src/error.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export class ValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "ValidationError";
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
export { default as decode } from './decode';
|
||||
export { default as encode } from './encode';
|
||||
export { default as decode, isBlurhashValid } from "./decode";
|
||||
export { default as encode } from "./encode";
|
||||
export * from "./error";
|
||||
|
||||
@ -18,4 +18,5 @@ export const linearTosRGB = (value: number) => {
|
||||
|
||||
export const sign = (n: number) => (n < 0 ? -1 : 1);
|
||||
|
||||
export const signPow = (val: number, exp: number) => sign(val) * Math.pow(Math.abs(val), exp);
|
||||
export const signPow = (val: number, exp: number) =>
|
||||
sign(val) * Math.pow(Math.abs(val), exp);
|
||||
|
||||
@ -1,13 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"declaration": true,
|
||||
"outDir": "./dist/",
|
||||
"sourceMap": true,
|
||||
"noImplicitAny": true,
|
||||
},
|
||||
"include": [
|
||||
"src/index.ts"
|
||||
]
|
||||
}
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"lib": ["es5", "dom"],
|
||||
"declaration": true,
|
||||
"outDir": "./dist/",
|
||||
"sourceMap": true,
|
||||
"noImplicitAny": true
|
||||
},
|
||||
"include": ["src/index.ts"]
|
||||
}
|
||||
|
||||
4221
TypeScript/yarn.lock
11
Website/.babelrc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"@babel/preset-env",
|
||||
{
|
||||
"targets": "> 0.25%, not dead",
|
||||
"modules": false
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
31
Website/.prettierrc
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"printWidth": 100,
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["*.js"],
|
||||
"options": {
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": ["webpack.config.js"],
|
||||
"options": {
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": ["*.css", "*.scss"],
|
||||
"options": {
|
||||
"parser": "scss",
|
||||
"singleQuote": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": "*.json",
|
||||
"options": {
|
||||
"parser": "json"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
9
Website/Readme.md
Normal file
@ -0,0 +1,9 @@
|
||||
# blurha.sh
|
||||
|
||||
Website for blurhash lives here.
|
||||
|
||||
## Developing
|
||||
|
||||
`npm start` runs the development server
|
||||
|
||||
`npm deploy` builds the site, moves the contents into gh-pages branch and pushes it.
|
||||