mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-15 07:28:30 +00:00
Move blurhash to its own module.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package org.thoughtcrime.securesms.database
|
||||
|
||||
import android.net.Uri
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.thoughtcrime.securesms.attachments.UriAttachment
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import java.util.UUID
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ package org.thoughtcrime.securesms.attachments
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.util.Base64
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import java.util.UUID
|
||||
|
||||
@@ -8,10 +8,10 @@ import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import androidx.core.os.ParcelCompat
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.signal.core.util.UuidUtil
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import org.thoughtcrime.securesms.util.ParcelUtil
|
||||
|
||||
@@ -7,10 +7,10 @@ package org.thoughtcrime.securesms.attachments
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import org.signal.blurhash.BlurHashEncoder
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.signal.core.util.mebiBytes
|
||||
import org.signal.protos.resumableuploads.ResumableUpload
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHashEncoder
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority
|
||||
import org.thoughtcrime.securesms.util.MediaUtil
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment
|
||||
|
||||
@@ -3,9 +3,9 @@ package org.thoughtcrime.securesms.attachments
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import androidx.core.os.ParcelCompat
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
|
||||
@@ -3,8 +3,8 @@ package org.thoughtcrime.securesms.attachments
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.util.Base64
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import org.whispersystems.signalservice.api.InvalidMessageStructureException
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.thoughtcrime.securesms.attachments
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import org.thoughtcrime.securesms.util.MediaUtil
|
||||
|
||||
@@ -3,9 +3,9 @@ package org.thoughtcrime.securesms.attachments
|
||||
import android.net.Uri
|
||||
import android.os.Parcel
|
||||
import androidx.core.os.ParcelCompat
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
import java.util.Objects
|
||||
import java.util.UUID
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/**
|
||||
* Source: https://github.com/hsch/blurhash-java
|
||||
*
|
||||
* Copyright (c) 2019 Hendrik Schnepel
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
final class Base83 {
|
||||
|
||||
private static final int MAX_LENGTH = 90;
|
||||
|
||||
private static final char[]ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".toCharArray();
|
||||
|
||||
private static int indexOf(char[] a, char key) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i] == key) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void encode(long value, int length, char[] buffer, int offset) {
|
||||
int exp = 1;
|
||||
for (int i = 1; i <= length; i++, exp *= 83) {
|
||||
int digit = (int)(value / exp % 83);
|
||||
buffer[offset + length - i] = ALPHABET[digit];
|
||||
}
|
||||
}
|
||||
|
||||
static int decode(String value, int fromInclusive, int toExclusive) {
|
||||
int result = 0;
|
||||
char[] chars = value.toCharArray();
|
||||
for (int i = fromInclusive; i < toExclusive; i++) {
|
||||
result = result * 83 + indexOf(ALPHABET, chars[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static boolean isValid(@Nullable String value) {
|
||||
if (value == null) return false;
|
||||
final int length = value.length();
|
||||
|
||||
if (length == 0 || length > MAX_LENGTH) return false;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (indexOf(ALPHABET, value.charAt(i)) == -1) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private Base83() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A BlurHash is a compact string representation of a blurred image that we can use to show fast
|
||||
* image previews.
|
||||
*/
|
||||
public class BlurHash implements Parcelable {
|
||||
|
||||
private final String hash;
|
||||
|
||||
private BlurHash(@NonNull String hash) {
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
protected BlurHash(Parcel in) {
|
||||
hash = in.readString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static @Nullable BlurHash parseOrNull(@Nullable String hash) {
|
||||
if (Base83.isValid(hash)) {
|
||||
return new BlurHash(hash);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public @NonNull String getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BlurHash blurHash = (BlurHash) o;
|
||||
return Objects.equals(hash, blurHash.hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(hash);
|
||||
}
|
||||
|
||||
public static final Creator<BlurHash> CREATOR = new Creator<BlurHash>() {
|
||||
@Override
|
||||
public BlurHash createFromParcel(Parcel in) {
|
||||
return new BlurHash(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlurHash[] newArray(int size) {
|
||||
return new BlurHash[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
/**
|
||||
* Source: https://github.com/woltapp/blurhash
|
||||
*
|
||||
* Copyright (c) 2018 Wolt Enterprises
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.linearTosRGB;
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.sRGBToLinear;
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.signPow;
|
||||
|
||||
class BlurHashDecoder {
|
||||
|
||||
static @Nullable Bitmap decode(@Nullable String blurHash, int width, int height) {
|
||||
return decode(blurHash, width, height, 1f);
|
||||
}
|
||||
|
||||
static @Nullable Bitmap decode(@Nullable String blurHash, int width, int height, double punch) {
|
||||
|
||||
if (blurHash == null || blurHash.length() < 6) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int numCompEnc = Base83.decode(blurHash, 0, 1);
|
||||
int numCompX = (numCompEnc % 9) + 1;
|
||||
int numCompY = (numCompEnc / 9) + 1;
|
||||
|
||||
if (blurHash.length() != 4 + 2 * numCompX * numCompY) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int maxAcEnc = Base83.decode(blurHash, 1, 2);
|
||||
double maxAc = (maxAcEnc + 1) / 166f;
|
||||
double[][] colors = new double[numCompX * numCompY][];
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
if (i == 0) {
|
||||
int colorEnc = Base83.decode(blurHash, 2, 6);
|
||||
colors[i] = decodeDc(colorEnc);
|
||||
} else {
|
||||
int from = 4 + i * 2;
|
||||
int colorEnc = Base83.decode(blurHash, from, from + 2);
|
||||
colors[i] = decodeAc(colorEnc, maxAc * punch);
|
||||
}
|
||||
}
|
||||
|
||||
return composeBitmap(width, height, numCompX, numCompY, colors);
|
||||
}
|
||||
|
||||
private static double[] decodeDc(int colorEnc) {
|
||||
int r = colorEnc >> 16;
|
||||
int g = (colorEnc >> 8) & 255;
|
||||
int b = colorEnc & 255;
|
||||
return new double[] {sRGBToLinear(r),
|
||||
sRGBToLinear(g),
|
||||
sRGBToLinear(b)};
|
||||
}
|
||||
|
||||
private static double[] decodeAc(int value, double maxAc) {
|
||||
int r = value / (19 * 19);
|
||||
int g = (value / 19) % 19;
|
||||
int b = value % 19;
|
||||
return new double[]{
|
||||
signPow((r - 9) / 9.0f, 2f) * maxAc,
|
||||
signPow((g - 9) / 9.0f, 2f) * maxAc,
|
||||
signPow((b - 9) / 9.0f, 2f) * maxAc
|
||||
};
|
||||
}
|
||||
|
||||
private static Bitmap composeBitmap(int width, int height, int numCompX, int numCompY, double[][] colors) {
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
|
||||
double r = 0f;
|
||||
double g = 0f;
|
||||
double b = 0f;
|
||||
|
||||
for (int j = 0; j < numCompY; j++) {
|
||||
for (int i = 0; i < numCompX; i++) {
|
||||
double basis = (Math.cos(Math.PI * x * i / width) * Math.cos(Math.PI * y * j / height));
|
||||
double[] color = colors[j * numCompX + i];
|
||||
r += color[0] * basis;
|
||||
g += color[1] * basis;
|
||||
b += color[2] * basis;
|
||||
}
|
||||
}
|
||||
bitmap.setPixel(x, y, Color.rgb((int) linearTosRGB(r), (int) linearTosRGB(g), (int) linearTosRGB(b)));
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
/**
|
||||
* Source: https://github.com/hsch/blurhash-java
|
||||
*
|
||||
* Copyright (c) 2019 Hendrik Schnepel
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.linearTosRGB;
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.max;
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.sRGBToLinear;
|
||||
import static org.thoughtcrime.securesms.blurhash.BlurHashUtil.signPow;
|
||||
|
||||
public final class BlurHashEncoder {
|
||||
|
||||
private BlurHashEncoder() {
|
||||
}
|
||||
|
||||
public static @Nullable String encode(InputStream inputStream) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inSampleSize = 16;
|
||||
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
|
||||
if (bitmap == null) return null;
|
||||
|
||||
String hash = encode(bitmap);
|
||||
|
||||
bitmap.recycle();
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static @Nullable String encode(@NonNull Bitmap bitmap) {
|
||||
return encode(bitmap, 4, 3);
|
||||
}
|
||||
|
||||
static String encode(Bitmap bitmap, int componentX, int componentY) {
|
||||
int width = bitmap.getWidth();
|
||||
int height = bitmap.getHeight();
|
||||
int[] pixels = new int[width * height];
|
||||
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
|
||||
return encode(pixels, width, height, componentX, componentY);
|
||||
}
|
||||
|
||||
private static String encode(int[] pixels, int width, int height, int componentX, int componentY) {
|
||||
|
||||
if (componentX < 1 || componentX > 9 || componentY < 1 || componentY > 9) {
|
||||
throw new IllegalArgumentException("Blur hash must have between 1 and 9 components");
|
||||
}
|
||||
if (width * height != pixels.length) {
|
||||
throw new IllegalArgumentException("Width and height must match the pixels array");
|
||||
}
|
||||
|
||||
double[][] factors = new double[componentX * componentY][3];
|
||||
for (int j = 0; j < componentY; j++) {
|
||||
for (int i = 0; i < componentX; i++) {
|
||||
double normalisation = i == 0 && j == 0 ? 1 : 2;
|
||||
applyBasisFunction(pixels, width, height,
|
||||
normalisation, i, j,
|
||||
factors, j * componentX + i);
|
||||
}
|
||||
}
|
||||
|
||||
char[] hash = new char[1 + 1 + 4 + 2 * (factors.length - 1)]; // size flag + max AC + DC + 2 * AC components
|
||||
|
||||
long sizeFlag = componentX - 1 + (componentY - 1) * 9;
|
||||
Base83.encode(sizeFlag, 1, hash, 0);
|
||||
|
||||
double maximumValue;
|
||||
if (factors.length > 1) {
|
||||
double actualMaximumValue = max(factors, 1, factors.length);
|
||||
double quantisedMaximumValue = Math.floor(Math.max(0, Math.min(82, Math.floor(actualMaximumValue * 166 - 0.5))));
|
||||
maximumValue = (quantisedMaximumValue + 1) / 166;
|
||||
Base83.encode(Math.round(quantisedMaximumValue), 1, hash, 1);
|
||||
} else {
|
||||
maximumValue = 1;
|
||||
Base83.encode(0, 1, hash, 1);
|
||||
}
|
||||
|
||||
double[] dc = factors[0];
|
||||
Base83.encode(encodeDC(dc), 4, hash, 2);
|
||||
|
||||
for (int i = 1; i < factors.length; i++) {
|
||||
Base83.encode(encodeAC(factors[i], maximumValue), 2, hash, 6 + 2 * (i - 1));
|
||||
}
|
||||
return new String(hash);
|
||||
}
|
||||
|
||||
private static void applyBasisFunction(int[] pixels, int width, int height,
|
||||
double normalisation, int i, int j,
|
||||
double[][] factors, int index)
|
||||
{
|
||||
double r = 0, g = 0, b = 0;
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
double basis = normalisation
|
||||
* Math.cos((Math.PI * i * x) / width)
|
||||
* Math.cos((Math.PI * j * y) / height);
|
||||
int pixel = pixels[y * width + x];
|
||||
r += basis * sRGBToLinear((pixel >> 16) & 0xff);
|
||||
g += basis * sRGBToLinear((pixel >> 8) & 0xff);
|
||||
b += basis * sRGBToLinear( pixel & 0xff);
|
||||
}
|
||||
}
|
||||
double scale = 1.0 / (width * height);
|
||||
factors[index][0] = r * scale;
|
||||
factors[index][1] = g * scale;
|
||||
factors[index][2] = b * scale;
|
||||
}
|
||||
|
||||
private static long encodeDC(double[] value) {
|
||||
long r = linearTosRGB(value[0]);
|
||||
long g = linearTosRGB(value[1]);
|
||||
long b = linearTosRGB(value[2]);
|
||||
return (r << 16) + (g << 8) + b;
|
||||
}
|
||||
|
||||
private static long encodeAC(double[] value, double maximumValue) {
|
||||
double quantR = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[0] / maximumValue, 0.5) * 9 + 9.5))));
|
||||
double quantG = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[1] / maximumValue, 0.5) * 9 + 9.5))));
|
||||
double quantB = Math.floor(Math.max(0, Math.min(18, Math.floor(signPow(value[2] / maximumValue, 0.5) * 9 + 9.5))));
|
||||
return Math.round(quantR * 19 * 19 + quantG * 19 + quantB);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
import com.bumptech.glide.load.Options;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
import com.bumptech.glide.load.model.ModelLoader;
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
||||
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
||||
import com.bumptech.glide.signature.ObjectKey;
|
||||
|
||||
public final class BlurHashModelLoader implements ModelLoader<BlurHash, BlurHash> {
|
||||
|
||||
private BlurHashModelLoader() {}
|
||||
|
||||
@Override
|
||||
public LoadData<BlurHash> buildLoadData(@NonNull BlurHash blurHash,
|
||||
int width,
|
||||
int height,
|
||||
@NonNull Options options)
|
||||
{
|
||||
return new LoadData<>(new ObjectKey(blurHash.getHash()), new BlurDataFetcher(blurHash));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handles(@NonNull BlurHash blurHash) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private final class BlurDataFetcher implements DataFetcher<BlurHash> {
|
||||
|
||||
private final BlurHash blurHash;
|
||||
|
||||
private BlurDataFetcher(@NonNull BlurHash blurHash) {
|
||||
this.blurHash = blurHash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super BlurHash> callback) {
|
||||
callback.onDataReady(blurHash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Class<BlurHash> getDataClass() {
|
||||
return BlurHash.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull DataSource getDataSource() {
|
||||
return DataSource.LOCAL;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<BlurHash, BlurHash> {
|
||||
@Override
|
||||
public @NonNull ModelLoader<BlurHash, BlurHash> build(@NonNull MultiModelLoaderFactory multiFactory) {
|
||||
return new BlurHashModelLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teardown() {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.load.Options;
|
||||
import com.bumptech.glide.load.ResourceDecoder;
|
||||
import com.bumptech.glide.load.engine.Resource;
|
||||
import com.bumptech.glide.load.resource.SimpleResource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BlurHashResourceDecoder implements ResourceDecoder<BlurHash, Bitmap> {
|
||||
|
||||
private static final int MAX_DIMEN = 20;
|
||||
|
||||
@Override
|
||||
public boolean handles(@NonNull BlurHash source, @NonNull Options options) throws IOException {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Resource<Bitmap> decode(@NonNull BlurHash source, int width, int height, @NonNull Options options) throws IOException {
|
||||
final int finalWidth;
|
||||
final int finalHeight;
|
||||
|
||||
if (width > height) {
|
||||
finalWidth = Math.min(width, MAX_DIMEN);
|
||||
finalHeight = (int) (finalWidth * height / (float) width);
|
||||
} else {
|
||||
finalHeight = Math.min(height, MAX_DIMEN);
|
||||
finalWidth = (int) (finalHeight * width / (float) height);
|
||||
}
|
||||
|
||||
return new SimpleResource<>(BlurHashDecoder.decode(source.getHash(), finalWidth, finalHeight));
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/**
|
||||
* Source: https://github.com/hsch/blurhash-java
|
||||
*
|
||||
* Copyright (c) 2019 Hendrik Schnepel
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package org.thoughtcrime.securesms.blurhash;
|
||||
|
||||
final class BlurHashUtil {
|
||||
|
||||
static double sRGBToLinear(long value) {
|
||||
double v = value / 255.0;
|
||||
if (v <= 0.04045) {
|
||||
return v / 12.92;
|
||||
} else {
|
||||
return Math.pow((v + 0.055) / 1.055, 2.4);
|
||||
}
|
||||
}
|
||||
|
||||
static long linearTosRGB(double value) {
|
||||
double v = Math.max(0, Math.min(1, value));
|
||||
if (v <= 0.0031308) {
|
||||
return (long)(v * 12.92 * 255 + 0.5);
|
||||
} else {
|
||||
return (long)((1.055 * Math.pow(v, 1 / 2.4) - 0.055) * 255 + 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
static double signPow(double val, double exp) {
|
||||
return Math.copySign(Math.pow(Math.abs(val), exp), val);
|
||||
}
|
||||
|
||||
static double max(double[][] values, int from, int endExclusive) {
|
||||
double result = Double.NEGATIVE_INFINITY;
|
||||
for (int i = from; i < endExclusive; i++) {
|
||||
for (int j = 0; j < values[i].length; j++) {
|
||||
double value = values[i][j];
|
||||
if (value > result) {
|
||||
result = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private BlurHashUtil() {
|
||||
}
|
||||
}
|
||||
@@ -38,17 +38,17 @@ import com.bumptech.glide.request.RequestOptions;
|
||||
import org.signal.core.util.concurrent.ListenableFuture;
|
||||
import org.signal.core.util.concurrent.SettableFuture;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.signal.glide.decryptableuri.DecryptableUri;
|
||||
import org.signal.glide.load.SignalDownsampleStrategy;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.thoughtcrime.securesms.components.transfercontrols.TransferControlView;
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable;
|
||||
import org.thoughtcrime.securesms.glide.targets.GlideBitmapListeningTarget;
|
||||
import org.thoughtcrime.securesms.glide.targets.GlideDrawableListeningTarget;
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
||||
import org.signal.glide.decryptableuri.DecryptableUri;
|
||||
import org.thoughtcrime.securesms.mms.ImageSlide;
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.mms.Slide;
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.bumptech.glide.Glide
|
||||
import okio.ByteString.Companion.toByteString
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.backup.MediaId
|
||||
import org.signal.core.models.backup.MediaName
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
@@ -77,7 +78,6 @@ import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.backup.v2.ArchivedMediaObject
|
||||
import org.thoughtcrime.securesms.backup.v2.exporters.ChatItemArchiveExporter
|
||||
import org.thoughtcrime.securesms.backup.v2.proto.BackupDebugInfo
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
||||
import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream
|
||||
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream
|
||||
|
||||
@@ -16,14 +16,14 @@ import com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder;
|
||||
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
||||
import com.bumptech.glide.load.resource.gif.StreamGifDecoder;
|
||||
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.signal.glide.blurhash.BlurHashModelLoader;
|
||||
import org.signal.glide.blurhash.BlurHashResourceDecoder;
|
||||
import org.signal.glide.common.io.InputStreamFactory;
|
||||
import org.signal.glide.load.resource.apng.decode.APNGDecoder;
|
||||
import org.thoughtcrime.securesms.badges.load.BadgeLoader;
|
||||
import org.thoughtcrime.securesms.badges.load.GiftBadgeModel;
|
||||
import org.thoughtcrime.securesms.badges.models.Badge;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHashModelLoader;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHashResourceDecoder;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoLoader;
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret;
|
||||
|
||||
@@ -21,11 +21,11 @@ import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
|
||||
import org.signal.libsignal.metadata.certificate.SenderCertificate;
|
||||
import org.signal.libsignal.zkgroup.InvalidInputException;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialPresentation;
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.thoughtcrime.securesms.TextSecureExpiredException;
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
||||
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.thoughtcrime.securesms.contactshare.Contact;
|
||||
import org.thoughtcrime.securesms.contactshare.ContactModelMapper;
|
||||
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
|
||||
|
||||
@@ -24,11 +24,11 @@ import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.signal.core.models.media.TransformProperties;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.signal.core.models.media.TransformProperties;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
public class ImageSlide extends Slide {
|
||||
|
||||
@@ -25,11 +25,11 @@ import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.signal.core.models.media.TransformProperties;
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.attachments.UriAttachment;
|
||||
import org.thoughtcrime.securesms.audio.AudioHash;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.signal.core.models.media.TransformProperties;
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable;
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
@@ -10,14 +10,12 @@ import kotlin.Pair;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.signal.blurhash.BlurHash;
|
||||
import org.signal.core.models.media.TransformProperties;
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* SlideFactory encapsulates logic related to constructing slides from a set of paramaeters as defined
|
||||
|
||||
@@ -17,8 +17,8 @@ import com.bumptech.glide.load.DataSource
|
||||
import com.bumptech.glide.load.engine.GlideException
|
||||
import com.bumptech.glide.request.RequestListener
|
||||
import com.bumptech.glide.request.target.Target
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.util.ContextUtil
|
||||
import org.thoughtcrime.securesms.util.visible
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ import android.widget.FrameLayout
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import com.bumptech.glide.Glide
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.util.getParcelableCompat
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.dependencies.AppDependencies
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
|
||||
import org.thoughtcrime.securesms.recipients.Recipient
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.thoughtcrime.securesms.stories
|
||||
import android.net.Uri
|
||||
import android.os.Parcelable
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.thoughtcrime.securesms.stories.viewer
|
||||
|
||||
import android.net.Uri
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
import org.thoughtcrime.securesms.stories.StoryTextPostModel
|
||||
|
||||
@@ -11,8 +11,8 @@ import com.bumptech.glide.load.DataSource
|
||||
import com.bumptech.glide.load.engine.GlideException
|
||||
import com.bumptech.glide.request.RequestListener
|
||||
import com.bumptech.glide.request.target.Target
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.stories.viewer.page.StoryCache
|
||||
import org.thoughtcrime.securesms.stories.viewer.page.StoryDisplay
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.thoughtcrime.securesms.stories.viewer.post
|
||||
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.model.databaseprotos.BodyRangeList
|
||||
import org.thoughtcrime.securesms.database.model.databaseprotos.StoryTextPost
|
||||
import org.thoughtcrime.securesms.linkpreview.LinkPreview
|
||||
|
||||
@@ -12,11 +12,11 @@ import com.bumptech.glide.load.engine.GlideException
|
||||
import com.bumptech.glide.request.RequestListener
|
||||
import com.bumptech.glide.request.target.Target
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.util.DimensionUnit
|
||||
import org.signal.glide.decryptableuri.DecryptableUri
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.animation.transitions.CrossfaderTransition
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
|
||||
import org.thoughtcrime.securesms.stories.StoryTextPostModel
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
@@ -22,8 +22,8 @@ import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.util.visible
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.thoughtcrime.securesms.testutil
|
||||
|
||||
import android.net.Uri
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.thoughtcrime.securesms.attachments.UriAttachment
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.database.AttachmentTable
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.thoughtcrime.securesms.database
|
||||
|
||||
import org.signal.blurhash.BlurHash
|
||||
import org.signal.core.models.media.TransformProperties
|
||||
import org.thoughtcrime.securesms.attachments.AttachmentId
|
||||
import org.thoughtcrime.securesms.attachments.Cdn
|
||||
import org.thoughtcrime.securesms.attachments.DatabaseAttachment
|
||||
import org.thoughtcrime.securesms.audio.AudioHash
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash
|
||||
import org.thoughtcrime.securesms.contactshare.Contact
|
||||
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch
|
||||
import org.thoughtcrime.securesms.database.documents.NetworkFailure
|
||||
|
||||
Reference in New Issue
Block a user