diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java
index 751eebda..0871312f 100644
--- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java
+++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java
@@ -28,7 +28,7 @@ public class CWebpHandler extends WebpHandler {
*
* Binaries can be downloaded here:
* https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
- *
+ *
* This method is executed automatically but can also be invoked manually to check
* for any installation errors.
*/
@@ -40,12 +40,13 @@ public byte[] convert(byte[] bytes,
int m,
int q,
int z,
- boolean lossless) throws IOException {
+ boolean lossless,
+ boolean withoutAlpha) 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);
+ convert(input, output, m, q, z, lossless, withoutAlpha);
return Files.readAllBytes(output);
} finally {
try {
@@ -64,7 +65,8 @@ private void convert(Path input,
int m,
int q,
int z,
- boolean lossless) throws IOException {
+ boolean lossless,
+ boolean withoutAlpha) throws IOException {
Path stdout = Files.createTempFile("stdout", "webp");
List commands = new ArrayList<>();
@@ -84,6 +86,9 @@ private void convert(Path input,
if (lossless) {
commands.add("-lossless");
}
+ if (withoutAlpha) {
+ commands.add("-noalpha");
+ }
commands.add(input.toAbsolutePath().toString());
commands.add("-o");
commands.add(target.toAbsolutePath().toString());
diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpWriter.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpWriter.java
index 312077c8..9091fb42 100644
--- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpWriter.java
+++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpWriter.java
@@ -19,12 +19,14 @@ public class WebpWriter implements ImageWriter {
private final int q;
private final int m;
private final boolean lossless;
+ private final boolean noAlpha;
public WebpWriter() {
z = -1;
q = -1;
m = -1;
lossless = false;
+ noAlpha = false;
}
public WebpWriter(int z, int q, int m, boolean lossless) {
@@ -32,33 +34,46 @@ public WebpWriter(int z, int q, int m, boolean lossless) {
this.q = q;
this.m = m;
this.lossless = lossless;
+ this.noAlpha = false;
+ }
+
+ public WebpWriter(int z, int q, int m, boolean lossless, boolean noAlpha) {
+ this.z = z;
+ this.q = q;
+ this.m = m;
+ this.lossless = lossless;
+ this.noAlpha = noAlpha;
}
public WebpWriter withLossless() {
return new WebpWriter(z, q, m, true);
}
+ public WebpWriter withoutAlpha() {
+ return new WebpWriter(z, q, m, lossless, true);
+ }
+
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");
- return new WebpWriter(z, q, m, lossless);
+ return new WebpWriter(z, q, m, lossless, noAlpha);
}
public WebpWriter withM(int m) {
if (m < 0) throw new IllegalArgumentException("m must be between 0 and 6");
if (m > 6) throw new IllegalArgumentException("m must be between 0 and 6");
- return new WebpWriter(z, q, m, lossless);
+ return new WebpWriter(z, q, m, lossless, noAlpha);
}
public WebpWriter withZ(int z) {
if (z < 0) throw new IllegalArgumentException("z must be between 0 and 9");
if (z > 9) throw new IllegalArgumentException("z must be between 0 and 9");
- return new WebpWriter(z, q, m, lossless);
+ return new WebpWriter(z, q, m, lossless, noAlpha);
}
@Override
public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {
- byte[] bytes = handler.convert(image.bytes(PngWriter.NoCompression), m, q, z, lossless);
+ byte[] bytes = handler.convert(image.bytes(PngWriter.NoCompression), m, q, z, lossless, noAlpha);
out.write(bytes);
}
}
diff --git a/scrimage-webp/src/test/kotlin/com/sksamuel/scrimage/webp/WebpTest.kt b/scrimage-webp/src/test/kotlin/com/sksamuel/scrimage/webp/WebpTest.kt
index eb677573..ec95d456 100644
--- a/scrimage-webp/src/test/kotlin/com/sksamuel/scrimage/webp/WebpTest.kt
+++ b/scrimage-webp/src/test/kotlin/com/sksamuel/scrimage/webp/WebpTest.kt
@@ -23,6 +23,13 @@ class WebpTest : FunSpec() {
javaClass.getResourceAsStream("/spacedock.webp").readBytes()
}
+ test("no alpha") {
+ val webpWriter = WebpWriter.DEFAULT.withoutAlpha()
+ ImmutableImage.loader().fromResource("/alpha.png")
+ .bytes(webpWriter) shouldBe
+ javaClass.getResourceAsStream("/noAlpha.webp").readBytes()
+ }
+
test("dwebp should capture error on failure") {
val dwebpPath = WebpHandler.getBinaryPaths("dwebp")[2]
diff --git a/scrimage-webp/src/test/resources/alpha.png b/scrimage-webp/src/test/resources/alpha.png
new file mode 100644
index 00000000..440992da
Binary files /dev/null and b/scrimage-webp/src/test/resources/alpha.png differ
diff --git a/scrimage-webp/src/test/resources/noAlpha.webp b/scrimage-webp/src/test/resources/noAlpha.webp
new file mode 100644
index 00000000..9a22c7f2
Binary files /dev/null and b/scrimage-webp/src/test/resources/noAlpha.webp differ