Skip to content

Commit

Permalink
#223 support webp binary in multiple os at once (#225)
Browse files Browse the repository at this point in the history
* support multiple platform

* add /webp_binaries/binary as primary search when loading webp binary
  • Loading branch information
SightStudio committed Apr 25, 2021
1 parent 035eab4 commit 79f26b2
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 11 deletions.
20 changes: 12 additions & 8 deletions docs/webp.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

Scrimage provides support for webp through the `scrimage-webp` module. To use webp, add this module to your build.

This module uses the `dwebp` and `cwebp` binaries, created by Google. The `scrimage-webp` module comes with the linux_x64
binaries already included (see
required [copyright notice](https://github.com/sksamuel/scrimage/blob/master/scrimage-webp/src/main/resources/dist_webp_binaries/LICENSE))
.

If you don't wish to use the embedded binaries (eg, you need macos or windows binaries), then you
can [download other versions](https://developers.google.com/speed/webp) and place them on your classpath
at `/web_binaries/dwebp` or `/web_binaries/cwebp`.
> animated gif to animated webp is not currently supported in this module.
This module uses the `dwebp` and `cwebp` binaries, created by Google. The `scrimage-webp` module comes with the
linux_x64, window_x64, mac-10.15 binaries already included (see required [copyright notice](https://github.com/sksamuel/scrimage/blob/master/scrimage-webp/src/main/resources/dist_webp_binaries/LICENSE)).

If you don't wish to use the embedded binaries, then you can [download other versions](https://developers.google.com/speed/webp) and place them
on your classpath at `/web_binaries/{osName}/dwebp` or `/web_binaries/{osName}/cwebp`.

`{osName}` must be one of `window`, `linux`, `mac`. ie `/web_binaries/window/cwebp`.

or just place your binaries into `/web_binaries/dwebp` or `/web_binaries/cwebp`.
then scrimage will use `/web_binaries/{binary}` regardless of the binaries which is in os specific directory.

Then you should be able to read webp files by using the `ImageLoader` as normal:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CWebpHandler extends WebpHandler {
* https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
*/
private static void installCWebp() throws IOException {
installBinary(binary, "/webp_binaries/cwebp", "/dist_webp_binaries/cwebp");
installBinary(binary, getBinaryPath("cwebp"));
}

public byte[] convert(byte[] bytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DWebpHandler extends WebpHandler {
* https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
*/
private static void installDWebp() throws IOException {
installBinary(binary, "/webp_binaries/dwebp", "/dist_webp_binaries/dwebp");
installBinary(binary, getBinaryPath("dwebp"));
}

public byte[] convert(byte[] bytes) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ protected static void installBinary(Path output, String... sources) throws IOExc
throw new IOException("Could not locate webp binary at " + Arrays.toString(sources));
}

protected static String[] getBinaryPath(String binaryName) {
String os = "linux";

if(SystemUtils.IS_OS_WINDOWS) {
os = "window";
} else if(SystemUtils.IS_OS_MAC) {
os = "mac";
}

return new String[] {
"/webp_binaries/" + binaryName,
"/webp_binaries/" + os + "/" + binaryName,
"/dist_webp_binaries/" + os + "/" + binaryName,
};
}

private static boolean setExecutable(Path output) throws IOException {
try {
return new ProcessBuilder("chmod", "+x", output.toAbsolutePath().toString())
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class WebpTest : FunSpec() {
}

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

shouldThrow<IOException> {
ImmutableImage.loader().fromResource("/dist_webp_binaries/dwebp")
ImmutableImage.loader().fromResource(dwebpPath)
}.message.shouldContain("BITSTREAM_ERROR")
}
}
Expand Down

0 comments on commit 79f26b2

Please sign in to comment.