Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to encode existing WebP image files #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repositories {
}

dependencies {
implementation "com.twelvemonkeys.imageio:imageio-webp:3.11.0"
testImplementation "junit:junit:4.12"
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/trbl/blurhash/Base83.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ static int decode(String value) {

private Base83() {
}
}
}
23 changes: 19 additions & 4 deletions src/main/java/io/trbl/blurhash/BlurHash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.trbl.blurhash;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

import static io.trbl.blurhash.Utils.*;

Expand All @@ -16,8 +18,8 @@ private static void applyBasisFunction(int[] pixels, int width, int height,
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);
* 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);
Expand Down Expand Up @@ -64,10 +66,23 @@ public static String encode(BufferedImage bufferedImage) {
public static String encode(BufferedImage bufferedImage, int componentX, int componentY) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int[] pixels = bufferedImage.getRGB(0, 0, width, height, null, 0, width);
int[] pixels = getRGBArrayFromImage(bufferedImage);
return encode(pixels, width, height, componentX, componentY);
}

private static int[] getRGBArrayFromImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
return pixels;
}

public static BufferedImage decodeWebPImage(byte[] webpData) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(webpData);
return ImageIO.read(inputStream);
}

/**
* Calculates the blur hash from the given pixels.
*
Expand Down Expand Up @@ -124,4 +139,4 @@ public static String encode(int[] pixels, int width, int height, int componentX,

private BlurHash() {
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/trbl/blurhash/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ static double max(double[][] values, int from, int endExclusive) {

private Utils() {
}
}
}