-
Notifications
You must be signed in to change notification settings - Fork 6
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
6 changed files
with
752 additions
and
102 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
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 |
---|---|---|
@@ -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? | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.