Skip to content

Commit

Permalink
Added the option to generate the webp file without alpha. (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squadella committed Jul 13, 2023
1 parent 0d869fc commit 3bd7f51
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
Expand Up @@ -28,7 +28,7 @@ public class CWebpHandler extends WebpHandler {
* <p>
* Binaries can be downloaded here:
* https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
*
* <p>
* This method is executed automatically but can also be invoked manually to check
* for any installation errors.
*/
Expand All @@ -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 {
Expand All @@ -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<String> commands = new ArrayList<>();
Expand All @@ -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());
Expand Down
Expand Up @@ -19,46 +19,61 @@ 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) {
this.z = z;
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);
}
}
Expand Up @@ -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]

Expand Down
Binary file added scrimage-webp/src/test/resources/alpha.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scrimage-webp/src/test/resources/noAlpha.webp
Binary file not shown.

0 comments on commit 3bd7f51

Please sign in to comment.