From 3bd7f51a5f69f49d5c4a99915e2018ec56359c9c Mon Sep 17 00:00:00 2001 From: Pierre Bouniol Date: Thu, 13 Jul 2023 04:01:47 +0200 Subject: [PATCH] Added the option to generate the webp file without alpha. (#270) --- .../sksamuel/scrimage/webp/CWebpHandler.java | 13 +++++++--- .../sksamuel/scrimage/webp/WebpWriter.java | 23 +++++++++++++++--- .../com/sksamuel/scrimage/webp/WebpTest.kt | 7 ++++++ scrimage-webp/src/test/resources/alpha.png | Bin 0 -> 228 bytes scrimage-webp/src/test/resources/noAlpha.webp | Bin 0 -> 152 bytes 5 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 scrimage-webp/src/test/resources/alpha.png create mode 100644 scrimage-webp/src/test/resources/noAlpha.webp 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 0000000000000000000000000000000000000000..440992da166d482b282e653fcd8674cb71e2f364 GIT binary patch literal 228 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4u_bxCyDx`7I;J! zGca%qgD@k*tT_@uLG}_)Usv|$Y*Ip^(pQ;hs{@4!JzX3_Brf+}aO67Vz|;CL->q9r zd}>qpM}d3)6^i5@dND TWGga(mNR&|`njxgN@xNALHbc= literal 0 HcmV?d00001 diff --git a/scrimage-webp/src/test/resources/noAlpha.webp b/scrimage-webp/src/test/resources/noAlpha.webp new file mode 100644 index 0000000000000000000000000000000000000000..9a22c7f25fc6d32391b4122832e007fb8d1e11a0 GIT binary patch literal 152 zcmV;J0B8SFNk&GH00012MM6+kP&goj0001x0sx%>DhdD!00ICsC9nc?GMES?3>0_b zcaUw!0RH^J4v$N`4~Db|>ZiF1G17)(%Ez#cyG2XQxXDeZ%IP*s26~Y9vf1v3ge~Q| zSZwgq&_=ce661R!sQhi^xL$Y@Y5TvK6T?1l{y3%>lyHyxTk#z=U-aDHe+MRUf5kIy GzJLHo^g{;# literal 0 HcmV?d00001