That fuck shit the fascists are using
1/**
2 * Source: https://github.com/hsch/blurhash-java
3 *
4 * Copyright (c) 2019 Hendrik Schnepel
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22package org.tm.archive.blurhash;
23
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26
27import androidx.annotation.NonNull;
28import androidx.annotation.Nullable;
29
30import java.io.InputStream;
31
32import static org.tm.archive.blurhash.BlurHashUtil.linearTosRGB;
33import static org.tm.archive.blurhash.BlurHashUtil.max;
34import static org.tm.archive.blurhash.BlurHashUtil.sRGBToLinear;
35import static org.tm.archive.blurhash.BlurHashUtil.signPow;
36
37public final class BlurHashEncoder {
38
39 private BlurHashEncoder() {
40 }
41
42 public static @Nullable String encode(InputStream inputStream) {
43 BitmapFactory.Options options = new BitmapFactory.Options();
44 options.inSampleSize = 16;
45
46 Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
47 if (bitmap == null) return null;
48
49 String hash = encode(bitmap);
50
51 bitmap.recycle();
52
53 return hash;
54 }
55
56 public static @Nullable String encode(@NonNull Bitmap bitmap) {
57 return encode(bitmap, 4, 3);
58 }
59
60 static String encode(Bitmap bitmap, int componentX, int componentY) {
61 int width = bitmap.getWidth();
62 int height = bitmap.getHeight();
63 int[] pixels = new int[width * height];
64 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
65 return encode(pixels, width, height, componentX, componentY);
66 }
67
68 private static String encode(int[] pixels, int width, int height, int componentX, int componentY) {
69
70 if (componentX < 1 || componentX > 9 || componentY < 1 || componentY > 9) {
71 throw new IllegalArgumentException("Blur hash must have between 1 and 9 components");
72 }
73 if (width * height != pixels.length) {
74 throw new IllegalArgumentException("Width and height must match the pixels array");
75 }
76
77 double[][] factors = new double[componentX * componentY][3];
78 for (int j = 0; j < componentY; j++) {
79 for (int i = 0; i < componentX; i++) {
80 double normalisation = i == 0 && j == 0 ? 1 : 2;
81 applyBasisFunction(pixels, width, height,
82 normalisation, i, j,
83 factors, j * componentX + i);
84 }
85 }
86
87 char[] hash = new char[1 + 1 + 4 + 2 * (factors.length - 1)]; // size flag + max AC + DC + 2 * AC components
88
89 long sizeFlag = componentX - 1 + (componentY - 1) * 9;
90 Base83.encode(sizeFlag, 1, hash, 0);
91
92 double maximumValue;
93 if (factors.length > 1) {
94 double actualMaximumValue = max(factors, 1, factors.length);
95 double quantisedMaximumValue = Math.floor(Math.max(0, Math.min(82, Math.floor(actualMaximumValue * 166 - 0.5))));
96 maximumValue = (quantisedMaximumValue + 1) / 166;
97 Base83.encode(Math.round(quantisedMaximumValue), 1, hash, 1);
98 } else {
99 maximumValue = 1;
100 Base83.encode(0, 1, hash, 1);
101 }
102
103 double[] dc = factors[0];
104 Base83.encode(encodeDC(dc), 4, hash, 2);
105
106 for (int i = 1; i < factors.length; i++) {
107 Base83.encode(encodeAC(factors[i], maximumValue), 2, hash, 6 + 2 * (i - 1));
108 }
109 return new String(hash);
110 }
111
112 private static void applyBasisFunction(int[] pixels, int width, int height,
113 double normalisation, int i, int j,
114 double[][] factors, int index)
115 {
116 double r = 0, g = 0, b = 0;
117 for (int x = 0; x < width; x++) {
118 for (int y = 0; y < height; y++) {
119 double basis = normalisation
120 * Math.cos((Math.PI * i * x) / width)
121 * Math.cos((Math.PI * j * y) / height);
122 int pixel = pixels[y * width + x];
123 r += basis * sRGBToLinear((pixel >> 16) & 0xff);
124 g += basis * sRGBToLinear((pixel >> 8) & 0xff);
125 b += basis * sRGBToLinear( pixel & 0xff);
126 }
127 }
128 double scale = 1.0 / (width * height);
129 factors[index][0] = r * scale;
130 factors[index][1] = g * scale;
131 factors[index][2] = b * scale;
132 }
133
134 private static long encodeDC(double[] value) {
135 long r = linearTosRGB(value[0]);
136 long g = linearTosRGB(value[1]);
137 long b = linearTosRGB(value[2]);
138 return (r << 16) + (g << 8) + b;
139 }
140
141 private static long encodeAC(double[] value, double maximumValue) {
142 double quantR = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[0] / maximumValue, 0.5) * 9 + 9.5))));
143 double quantG = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[1] / maximumValue, 0.5) * 9 + 9.5))));
144 double quantB = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[2] / maximumValue, 0.5) * 9 + 9.5))));
145 return Math.round(quantR * 19 * 19 + quantG * 19 + quantB);
146 }
147
148}