Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* omg it’s compiling

* example actually works

* Expose compression level options

* Disable crypto and path module emulation in webpack

* Update README

* Remove small image

* Use -O3 on optipng

* Free memory after copy

* Handle unexpected file reader return types

* Rename level label to effort
  • Loading branch information
surma authored and jakearchibald committed Sep 4, 2018
1 parent 8c08838 commit 1c7f536
Show file tree
Hide file tree
Showing 19 changed files with 1,832 additions and 10 deletions.
Binary file added codecs/example_palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions codecs/optipng/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
*.o
26 changes: 26 additions & 0 deletions codecs/optipng/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# OptiPNG

- Source: <https://sourceforge.net/project/optipng>
- Version: v0.7.7

## Dependencies

- Docker

## Example

See `example.html`

## API

### `int version()`

Returns the version of optipng as a number. va.b.c is encoded as 0x0a0b0c

### `ArrayBuffer compress(std::string buffer, {level})`;

`compress` will re-compress the given PNG image via `buffer`. `level` is a number between 0 and 7.

### `void free_result()`

Frees the result created by `compress()`.
80 changes: 80 additions & 0 deletions codecs/optipng/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

set -e

export PREFIX="/src/build"
export CFLAGS="-I${PREFIX}/include/"
export CPPFLAGS="-I${PREFIX}/include/"
export LDFLAGS="-L${PREFIX}/lib/"

apt-get update
apt-get install -qqy autoconf libtool

echo "============================================="
echo "Compiling zlib"
echo "============================================="
test -n "$SKIP_ZLIB" || (
cd node_modules/zlib
emconfigure ./configure --prefix=${PREFIX}/
emmake make
emmake make install
)
echo "============================================="
echo "Compiling zlib done"
echo "============================================="

echo "============================================="
echo "Compiling libpng"
echo "============================================="
test -n "$SKIP_LIBPNG" || (
cd node_modules/libpng
autoreconf -i
emconfigure ./configure --with-zlib-prefix=${PREFIX}/ --prefix=${PREFIX}/
emmake make
emmake make install
)
echo "============================================="
echo "Compiling libpng done"
echo "============================================="

echo "============================================="
echo "Compiling optipng"
echo "============================================="
(
emcc \
-O3 \
-Wno-implicit-function-declaration \
-I ${PREFIX}/include \
-I node_modules/optipng/src/opngreduc \
-I node_modules/optipng/src/pngxtern \
-I node_modules/optipng/src/cexcept \
-I node_modules/optipng/src/gifread \
-I node_modules/optipng/src/pnmio \
-I node_modules/optipng/src/minitiff \
--std=c99 -c \
node_modules/optipng/src/opngreduc/*.c \
node_modules/optipng/src/pngxtern/*.c \
node_modules/optipng/src/gifread/*.c \
node_modules/optipng/src/minitiff/*.c \
node_modules/optipng/src/pnmio/*.c \
node_modules/optipng/src/optipng/*.c

emcc \
--bind -O3 \
-s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME="optipng"' \
-I ${PREFIX}/include \
-I node_modules/optipng/src/opngreduc \
-I node_modules/optipng/src/pngxtern \
-I node_modules/optipng/src/cexcept \
-I node_modules/optipng/src/gifread \
-I node_modules/optipng/src/pnmio \
-I node_modules/optipng/src/minitiff \
-o "optipng.js" \
--std=c++11 \
optipng.cpp \
*.o \
${PREFIX}/lib/libz.so ${PREFIX}/lib/libpng.a
)
echo "============================================="
echo "Compiling optipng done"
echo "============================================="
19 changes: 19 additions & 0 deletions codecs/optipng/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<script src='optipng.js'></script>
<script>
const Module = optipng();

Module.onRuntimeInitialized = async _ => {
console.log('Version:', Module.version().toString(16));
const image = await fetch('../example_palette.png').then(r => r.arrayBuffer());
const newImage = Module.compress(image, {level: 3});
console.log('done');
Module.free_result();

console.log(`Old size: ${image.byteLength}, new size: ${newImage.byteLength} (${newImage.byteLength/image.byteLength*100}%)`);
const blobURL = URL.createObjectURL(new Blob([newImage], {type: 'image/png'}));
const img = document.createElement('img');
img.src = blobURL;
document.body.appendChild(img);
};
</script>
51 changes: 51 additions & 0 deletions codecs/optipng/optipng.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "emscripten/bind.h"
#include "emscripten/val.h"

#include <stdio.h>

using namespace emscripten;

extern "C" int main(int argc, char *argv[]);

int version() {
// FIXME (@surma): Haven’t found a version in optipng :(
return 0;
}

struct OptiPngOpts {
int level;
};

uint8_t* result;
val compress(std::string png, OptiPngOpts opts) {
FILE* infile = fopen("input.png", "wb");
fwrite(png.c_str(), png.length(), 1, infile);
fflush(infile);
fclose(infile);

char optlevel[8];
sprintf(&optlevel[0], "-o%d", opts.level);
char* args[] = {"optipng", optlevel, "-out", "output.png", "input.png"};
main(5, args);

FILE *outfile = fopen("output.png", "rb");
fseek(outfile, 0, SEEK_END);
int fsize = ftell(outfile);
result = (uint8_t*) malloc(fsize);
fseek(outfile, 0, SEEK_SET);
fread(result, fsize, 1, outfile);
return val(typed_memory_view(fsize, result));
}

void free_result() {
free(result);
}

EMSCRIPTEN_BINDINGS(my_module) {
value_object<OptiPngOpts>("OptiPngOpts")
.field("level", &OptiPngOpts::level);

function("version", &version);
function("compress", &compress);
function("free_result", &free_result);
}
10 changes: 10 additions & 0 deletions codecs/optipng/optipng.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {EncodeOptions} from "src/codecs/optipng/encoder";

export interface OptiPngModule extends EmscriptenWasm.Module {
compress(data: BufferSource, opts: EncodeOptions): Uint8Array;
free_result(): void;
}

export default function(opts: EmscriptenWasm.ModuleOpts): OptiPngModule;


24 changes: 24 additions & 0 deletions codecs/optipng/optipng.js

Large diffs are not rendered by default.

Binary file added codecs/optipng/optipng.wasm
Binary file not shown.
Loading

0 comments on commit 1c7f536

Please sign in to comment.