Skip to content

Commit

Permalink
Added the option to use multithreading when encoding webp (#287)
Browse files Browse the repository at this point in the history
Co-authored-by: shinyook.lee <shinyook.lee@navercorp.com>
  • Loading branch information
leeshinyook and shinyook.lee authored Apr 23, 2024
1 parent 6e78255 commit 450b958
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
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

0 comments on commit 450b958

Please sign in to comment.