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

Added the option to use multithreading when encoding webp #287

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public byte[] convert(byte[] bytes,
int q,
int z,
boolean lossless,
boolean withoutAlpha) throws IOException {
boolean withoutAlpha,
boolean multiThread) throws IOException {
Path input = Files.createTempFile("input", "webp").toAbsolutePath();
Path output = Files.createTempFile("to_webp", "webp").toAbsolutePath();
try {
Files.write(input, bytes, StandardOpenOption.CREATE);
convert(input, output, m, q, z, lossless, withoutAlpha);
convert(input, output, m, q, z, lossless, withoutAlpha, multiThread);
return Files.readAllBytes(output);
} finally {
try {
Expand All @@ -72,8 +73,8 @@ private void convert(Path input,
int q,
int z,
boolean lossless,
boolean withoutAlpha) throws IOException {

boolean withoutAlpha,
boolean multiThread) throws IOException {
Path stdout = Files.createTempFile("stdout", "webp");
List<String> commands = new ArrayList<>();
commands.add(binary.toAbsolutePath().toString());
Expand All @@ -95,6 +96,9 @@ private void convert(Path input,
if (withoutAlpha) {
commands.add("-noalpha");
}
if (multiThread) {
commands.add("-mt");
}
commands.add(input.toAbsolutePath().toString());
commands.add("-o");
commands.add(target.toAbsolutePath().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public class WebpWriter implements ImageWriter {
private final int m;
private final boolean lossless;
private final boolean noAlpha;
private final boolean multiThread;

public WebpWriter() {
z = -1;
q = -1;
m = -1;
lossless = false;
noAlpha = false;
multiThread = false;
}

public WebpWriter(int z, int q, int m, boolean lossless) {
Expand All @@ -35,6 +37,7 @@ public WebpWriter(int z, int q, int m, boolean lossless) {
this.m = m;
this.lossless = lossless;
this.noAlpha = false;
this.multiThread = false;
}

public WebpWriter(int z, int q, int m, boolean lossless, boolean noAlpha) {
Expand All @@ -43,6 +46,16 @@ public WebpWriter(int z, int q, int m, boolean lossless, boolean noAlpha) {
this.m = m;
this.lossless = lossless;
this.noAlpha = noAlpha;
this.multiThread = false;
}

public WebpWriter(int z, int q, int m, boolean lossless, boolean noAlpha, boolean multiThread) {
this.z = z;
this.q = q;
this.m = m;
this.lossless = lossless;
this.noAlpha = noAlpha;
this.multiThread = multiThread;
}

public WebpWriter withLossless() {
Expand All @@ -53,6 +66,10 @@ public WebpWriter withoutAlpha() {
return new WebpWriter(z, q, m, lossless, true);
}

public WebpWriter withMultiThread() {
return new WebpWriter(z, q, m, lossless, noAlpha, multiThread);
}

public WebpWriter withQ(int q) {
if (q < 0) throw new IllegalArgumentException("q must be between 0 and 100");
if (q > 100) throw new IllegalArgumentException("q must be between 0 and 100");
Expand All @@ -73,7 +90,7 @@ public WebpWriter withZ(int z) {

@Override
public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {
byte[] bytes = handler.convert(image.bytes(PngWriter.NoCompression), m, q, z, lossless, noAlpha);
byte[] bytes = handler.convert(image.bytes(PngWriter.NoCompression), m, q, z, lossless, noAlpha, multiThread);
out.write(bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class WebpTest : FunSpec() {
javaClass.getResourceAsStream("/noAlpha.webp").readBytes()
}

test("render with multi thread") {
val webpWriter = WebpWriter.MAX_LOSSLESS_COMPRESSION.withMultiThread()
ImmutableImage.loader().fromResource("/spacedock.jpg").scale(0.5)
.bytes(webpWriter) shouldBe
javaClass.getResourceAsStream("/spacedock.webp").readBytes()
}

test("dwebp should capture error on failure") {
val dwebpPath = WebpHandler.getBinaryPaths("dwebp")[2]

Expand Down
Loading