-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optipng #156
Merged
Merged
Optipng #156
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
83cb7dd
omg it’s compiling
surma 391da79
example actually works
surma 02d8e63
Expose compression level options
surma 647dadd
Disable crypto and path module emulation in webpack
surma 738b5fe
Update README
surma 9ca534a
Remove small image
surma f6ad10c
Use -O3 on optipng
surma 033ee3c
Free memory after copy
surma 708aab8
Handle unexpected file reader return types
surma 441d2bb
Rename level label to effort
surma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,2 @@ | ||
build/ | ||
*.o |
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,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()`. |
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,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 "=============================================" |
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,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> |
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,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); | ||
} |
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,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; | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lazy question: are there other options to expose, or is that all it offers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some more options, just wanted to get this off my table, honestly:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. We'll create an issue to explore these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#159