-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
6,651 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{ | ||
inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs/24.05"; | ||
flake-utils.url = "github:numtide/flake-utils"; | ||
webp-src = { | ||
url = "github:webmproject/libwebp/d2e245ea9e959a5a79e1db0ed2085206947e98f2"; | ||
flake = false; | ||
}; | ||
}; | ||
outputs = | ||
{ | ||
self, | ||
nixpkgs, | ||
flake-utils, | ||
webp-src, | ||
}: | ||
flake-utils.lib.eachDefaultSystem ( | ||
system: | ||
let | ||
pkgs = nixpkgs.legacyPackages.${system}; | ||
in | ||
with pkgs; | ||
rec { | ||
packages = rec { | ||
default = webp-squoosh; | ||
webp-squoosh = stdenv.mkDerivation { | ||
name = "mozjpeg-squoosh"; | ||
# Only copy files that are actually relevant to avoid unnecessary | ||
# cache invalidations. | ||
src = runCommand "src" { } '' | ||
mkdir $out | ||
cp -r ${./.}/enc $out/ | ||
cp -r ${./.}/dec $out/ | ||
cp ${./.}/Makefile $out/ | ||
''; | ||
nativeBuildInputs = [ | ||
emscripten | ||
webp | ||
]; | ||
WEBP = webp; | ||
dontConfigure = true; | ||
buildPhase = '' | ||
export HOME=$TMPDIR | ||
emmake make -j$(nproc) | ||
''; | ||
installPhase = '' | ||
mkdir -p $out | ||
cp -r enc dec $out | ||
''; | ||
}; | ||
webp = stdenv.mkDerivation { | ||
name = "webp"; | ||
src = webp-src; | ||
nativeBuildInputs = [ | ||
# autoconf | ||
# automake | ||
# libtool | ||
emscripten | ||
# pkg-config | ||
cmake | ||
]; | ||
configurePhase = '' | ||
# $HOME is required for Emscripten to work. | ||
# See: https://nixos.org/manual/nixpkgs/stable/#emscripten | ||
export HOME=$TMPDIR | ||
mkdir -p $TMPDIR/build | ||
emcmake cmake \ | ||
-DCMAKE_INSTALL_PREFIX=$out \ | ||
-DCMAKE_DISABLE_FIND_PACKAGE_Threads=1 \ | ||
-DWEBP_BUILD_ANIM_UTILS=0 \ | ||
-DWEBP_BUILD_CWEBP=0 \ | ||
-DWEBP_BUILD_DWEBP=0 \ | ||
-DWEBP_BUILD_GIF2WEBP=0 \ | ||
-DWEBP_BUILD_IMG2WEBP=0 \ | ||
-DWEBP_BUILD_VWEBP=0 \ | ||
-DWEBP_BUILD_WEBPINFO=0 \ | ||
-DWEBP_BUILD_WEBPMUX=0 \ | ||
-DWEBP_BUILD_EXTRAS=0 \ | ||
-B $TMPDIR/build \ | ||
. | ||
''; | ||
buildPhase = '' | ||
export HOME=$TMPDIR | ||
cd $TMPDIR/build | ||
emmake make V=1 -j$(nproc) --trace | ||
''; | ||
installPhase = '' | ||
cd $TMPDIR/build | ||
make install | ||
''; | ||
dontFixup = true; | ||
}; | ||
installScript = writeShellScriptBin "install.sh" '' | ||
${pkgs.coreutils}/bin/rm -rf wasm_build | ||
${pkgs.coreutils}/bin/mkdir -p wasm_build | ||
${pkgs.rsync}/bin/rsync --chmod=u+w -r ${webp-squoosh}/* wasm_build/ | ||
''; | ||
}; | ||
apps = { | ||
install = { | ||
type = "app"; | ||
program = "${packages.installScript}/bin/install.sh"; | ||
}; | ||
}; | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# WebP decoder | ||
|
||
- Source: <https://github.com/webmproject/libwebp> | ||
- Version: v1.0.2 | ||
|
||
## Example | ||
|
||
See `example.html` | ||
|
||
## API | ||
|
||
### `int version()` | ||
|
||
Returns the version of libwebp as a number. va.b.c is encoded as 0x0a0b0c | ||
|
||
### `RawImage decode(std::string buffer)` | ||
|
||
Decodes the given webp buffer into raw RGBA. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<script src='webp_dec.js'></script> | ||
<script> | ||
async function loadFile(src) { | ||
const resp = await fetch(src); | ||
return await resp.arrayBuffer(); | ||
} | ||
|
||
webp_dec().then(async module => { | ||
console.log('Version:', module.version().toString(16)); | ||
const image = await loadFile('../../example.webp'); | ||
const imageData = module.decode(image); | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = imageData.width; | ||
canvas.height = imageData.height; | ||
document.body.appendChild(canvas); | ||
const ctx = canvas.getContext('2d'); | ||
ctx.putImageData(imageData, 0, 0); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <string> | ||
#include "emscripten/bind.h" | ||
#include "emscripten/val.h" | ||
#include "webp/decode.h" | ||
#include "webp/demux.h" | ||
|
||
using namespace emscripten; | ||
|
||
int version() { | ||
return WebPGetDecoderVersion(); | ||
} | ||
|
||
thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray"); | ||
thread_local const val ImageData = val::global("ImageData"); | ||
|
||
val decode(std::string buffer) { | ||
int width, height; | ||
std::unique_ptr<uint8_t[]> rgba( | ||
WebPDecodeRGBA((const uint8_t*)buffer.c_str(), buffer.size(), &width, &height)); | ||
return rgba ? ImageData.new_( | ||
Uint8ClampedArray.new_(typed_memory_view(width * height * 4, rgba.get())), | ||
width, height) | ||
: val::null(); | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(my_module) { | ||
function("decode", &decode); | ||
function("version", &version); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface WebPModule extends EmscriptenWasm.Module { | ||
decode(data: BufferSource): ImageData | null; | ||
} | ||
|
||
declare var moduleFactory: EmscriptenWasm.ModuleFactory<WebPModule>; | ||
|
||
export default moduleFactory; |
Oops, something went wrong.