Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
as-com committed Jun 20, 2016
1 parent 44a22db commit a533fac
Show file tree
Hide file tree
Showing 6 changed files with 752 additions and 102 deletions.
2 changes: 1 addition & 1 deletion build-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ emconfigure ./configure --without-simd --without-turbojpeg CFLAGS="-O3"
emmake make
cp .libs/cjpeg ../cjpeg.bc
cd ..
emcc -O3 --pre-js src/pre.js --post-js src/post.js --memory-init-file 0 cjpeg.bc -o src/cjpeg.js -L'./mozjpeg/.libs' -ljpeg
emcc -O3 --closure 1 --pre-js src/pre.js -s "EXPORTED_RUNTIME_METHODS=[]" -s ELIMINATE_DUPLICATE_FUNCTIONS=1 --memory-init-file 0 cjpeg.bc -o src/cjpeg.js -L'./mozjpeg/.libs' -ljpeg
Binary file added out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
697 changes: 683 additions & 14 deletions src/cjpeg.js

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions src/post.js

This file was deleted.

136 changes: 64 additions & 72 deletions src/pre.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,76 @@
// UMD
(function(root, factory) {
if (typeof define === 'function' && define["amd"]) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module["exports"]) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module["exports"] = factory();
} else {
// Browser globals (root is window)
root["returnExports"] = factory();
function toUint8Array(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
}(this, function() {
var x = {};
return view;
}

function toUint8Array(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return view;
function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}

function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
var stdout = "";
var stderr = "";

x["encode"] = function(file, options) {
// file: Buffer containing file in PPM, PGM, BMP, or Targa format.
// options: hash containing options to pass to cjpeg
// returns: file contents?
module.exports = function(file, options) {
// file: Buffer containing file in PPM, PGM, BMP, or Targa format.
// options: hash containing options to pass to cjpeg
// returns: file contents?

var stdout = "";
var stderr = "";
var args = ["-outfile", "/output.jpg"];
stdout = "";
stderr = "";

for (var key in options) {
if (!options.hasOwnProperty(key)) continue;
var args = ["-outfile", "/output.jpg"];

if (options[key]) {
args.push("-" + key);
if (typeof options[key] !== "boolean") {
// option has a value
args.push(String(options[key]));
}
for (var key in options) {
if (!options.hasOwnProperty(key)) continue;

if (options[key]) {
args.push("-" + key);
if (typeof options[key] !== "boolean") {
// option has a value
args.push(String(options[key]));
}
}
}

args.push("/input");

FS.writeFile("/input", toUint8Array(file), {
encoding: "binary"
});

Module["callMain"](args);

var file;
try {
file = FS.readFile("/output.jpg");
} catch (e) {
return new Error("No output: " + stderr);
}
//console.log(file);
return {
"data": toBuffer(file.buffer),
"stderr": stderr
};
}

args.push("/input");
var Module = {
"noInitialRun" : true,
"noExitRuntime" : true,
"print": function(text) {
stdout += text;
},
"printErr": function(text) {
stderr += text;
},
"ENVIRONMENT": "SHELL"
}

var Module = {
"print": function(text) {
stdout += text;
},
"printErr": function(text) {
stderr += text;
},
"preRun": [function() {
FS.writeFile("/input", toUint8Array(file), {
encoding: "binary"
});
}],
"arguments": args,
// "postRun": [ function () {
// var file;
// try {
// file = FS.readFile("/output.jpg");
// } catch (e) {
// cb(e, null, stdout, stderr);
// return;
// }
// //console.log(file);
// cb(null, toBuffer(file.buffer));
// }],
"ENVIRONMENT": "SHELL" // maximum compatibility?
};
4 changes: 4 additions & 0 deletions testorig.ppm

Large diffs are not rendered by default.

0 comments on commit a533fac

Please sign in to comment.