Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pofulu committed Jul 1, 2021
0 parents commit eda82a6
Show file tree
Hide file tree
Showing 53 changed files with 4,749 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/
.vscode/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
8 changes: 8 additions & 0 deletions Assets/Blurhash.Unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions Assets/Blurhash.Unity/Base83.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Blurhash.Core
{
/// <summary>
/// Contains methods to encode or decode integers to Base83-Strings
/// </summary>
public static class Base83
{
internal const string Charset = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~";

private static readonly IReadOnlyDictionary<char, BigInteger> ReverseLookup;

static Base83()
{
// Build inverse lookup table for fast decoding
var charIndices = new Dictionary<char, BigInteger>();
var index = 0;
foreach (var c in Charset)
{
charIndices[c] = index;
index++;
}

ReverseLookup = charIndices;
}

/// <summary>
/// Encodes a number into its Base83-representation
/// </summary>
/// <param name="number">The number to encode</param>
/// <param name="length">The length of the Base83-string</param>
/// <returns>The Base83-representation of the number</returns>
public static string EncodeBase83(this BigInteger number, int length)
{
var result = new char[length];
foreach (var i in Enumerable.Range(1, length))
{
var digit = (int)((number / BigInteger.Pow(83,length - i)) % 83);
result[i - 1] = Charset[digit];
}

return new string(result);
}

/// <summary>
/// Encodes a number into its Base83-representation
/// </summary>
/// <param name="number">The number to encode</param>
/// <param name="length">The length of the Base83-string</param>
/// <returns>The Base83-representation of the number</returns>
public static string EncodeBase83(this int number, int length)
{
return ((BigInteger) number).EncodeBase83(length);
}

/// <summary>
/// Decodes an <code>IEnumerable</code> of Base83-characters into the integral value it represents
/// </summary>
/// <param name="base83Data">The characters to decode</param>
/// <returns>The decoded value as integer</returns>
public static BigInteger DecodeBase83Integer(this IEnumerable<char> base83Data)
{
var characters = base83Data as char[] ?? base83Data.ToArray();

if (!characters.IsBase83String())
throw new ArgumentException("The given string contains invalid characters.", nameof(base83Data));

var result = (BigInteger)0;
foreach (var c in characters)
{
var digit = ReverseLookup[c];
result *= 83;
result += digit;
}

return result;
}

/// <summary>
/// Checks whether a given String is a valid Base83-String.
/// </summary>
/// <param name="stringToCheck">The string to check</param>
/// <returns><code>true</code>, if the string only contains valid Base83-characters; <code>false</code> otherwise</returns>
public static bool IsBase83String(this IEnumerable<char> stringToCheck)
{
// The string is a Base83 string, when all chars are contained in the inverse lookup table
return stringToCheck.All(ReverseLookup.ContainsKey);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Blurhash.Unity/Base83.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions Assets/Blurhash.Unity/BlurHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

using Blurhash.Core;
using UnityEngine;

namespace Blurhash.Unity
{
public static class BlurHash
{
public static string EncodeToBlurHash(Texture2D texture2d, int componentsX = 4, int componentsY = 3)
{
return BlurHashEncoder.Instance.EncodeToBlurHash(texture2d, componentsX, componentsY);
}

public static Texture2D DecodeToTexture2D(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
{
return BlurHashDecoder.Instance.DecodeToTexture2D(blurhash, outputWidth, outputHeight, punch);
}

public static Color32[] DecodeToColor32(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
{
return BlurHashDecoder.Instance.DecodeToColor32(blurhash, outputWidth, outputHeight, punch);
}

class BlurHashEncoder : CoreEncoder
{
private static BlurHashEncoder instance;
public static BlurHashEncoder Instance
{
get
{
if (instance == null)
{
instance = new BlurHashEncoder();
}

return instance;
}
}

public string EncodeToBlurHash(Texture2D texture2d, int componentsX, int componentsY)
{
var pixels = new Pixel[texture2d.width, texture2d.height];

for (int y = 0; y < texture2d.height; y++)
{
for (int x = 0; x < texture2d.width; x++)
{
var color = texture2d.GetPixel(x, texture2d.height - 1 - y);
pixels[x, y] = new Pixel(color.r, color.g, color.b);
}
}

return CoreEncode(pixels, componentsX, componentsY);
}
}

class BlurHashDecoder : CoreDecoder
{
private static BlurHashDecoder instance;
public static BlurHashDecoder Instance
{
get
{
if (instance == null)
{
instance = new BlurHashDecoder();
}

return instance;
}
}

public Texture2D DecodeToTexture2D(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
{
var texture = new Texture2D(outputWidth, outputHeight);
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels32(DecodeToColor32(blurhash, outputWidth, outputHeight, punch));
texture.Apply();

return texture;
}

public Color32[] DecodeToColor32(string blurhash, int outputWidth, int outputHeight, double punch = 1.0)
{
var result = CoreDecode(blurhash, outputWidth, outputHeight, punch);
var colors = new Color32[outputWidth * outputHeight];

for (int y = 0; y < outputHeight; y++)
{
for (int x = 0; x < outputWidth; x++)
{
var color = result[x, outputHeight - y - 1];
colors[outputWidth * y + x] = new Color((float)color.Red, (float)color.Green, (float)color.Blue);
}
}

return colors;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Blurhash.Unity/BlurHash.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/Blurhash.Unity/Coordinate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Blurhash.Core
{
/// <summary>
/// Represents a 2D-coordinate
/// </summary>
public class Coordinate
{
public int X;
public int Y;

public Coordinate(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Blurhash.Unity/Coordinate.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eda82a6

Please sign in to comment.