Created
June 25, 2021 07:54
-
-
Save Hashbrown777/59f5123dc3a015f358561bfc70d298f9 to your computer and use it in GitHub Desktop.
testing jpeg-ls compression rates
This file contains hidden or 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
| const fs = require('fs'); | |
| const child_process = require('child_process'); | |
| const charls = new Promise((resolve) => { | |
| const charlsJS = require('./charlsjs.js'); //https://github.com/chafey/charls-js/raw/master/dist/charlsjs.js | |
| //in same directory: https://github.com/chafey/charls-js/blob/master/dist/charlsjs.wasm?raw=true | |
| charlsJS.onRuntimeInitialized = () => { resolve(charlsJS); }; | |
| }); | |
| (async () => { | |
| const file = 'image5.png'; | |
| const encoder = new (await charls).JpegLSEncoder(); | |
| encoder.setNearLossless(0); | |
| console.log((await fs.promises.stat(file)).size); | |
| const info = await new Promise(async (resolve, reject) => { | |
| let error = ''; | |
| try { | |
| const identify = child_process.spawn( | |
| 'identify', [ | |
| '-format', '{"width":%w, "height":%h, "bitsPerSample":%z, "componentCount":3}', | |
| '-' | |
| ] | |
| ); | |
| let json = ''; | |
| identify | |
| .stdout | |
| .on('data', (data) => { json += data; }) | |
| .on('end', () => { resolve(JSON.parse(json)); }) | |
| ; | |
| identify.stderr.on('data', (data) => { error += data; }); | |
| fs.createReadStream(file).pipe(identify.stdin); | |
| } | |
| catch (e) { | |
| if (error) | |
| console.err(error); | |
| reject(e); | |
| } | |
| }); | |
| console.log(info); | |
| await new Promise(async (resolve, reject) => { | |
| let error = ''; | |
| try { | |
| const convert = child_process.spawn('convert', ['png:-', 'bgr:-']); | |
| const bytes = encoder.getDecodedBuffer(info); | |
| console.log(bytes.length); | |
| let index = 0; | |
| convert | |
| .stdout | |
| .on('data', (data) => { | |
| data.copy(bytes, index); | |
| index += data.length; | |
| }) | |
| .on('end', () => { | |
| console.log(index, bytes.length); | |
| resolve(); | |
| }) | |
| ; | |
| convert.stderr.on('data', (data) => { error += data; }); | |
| fs.createReadStream(file).pipe(convert.stdin); | |
| } | |
| catch (e) { | |
| if (error) | |
| console.err(error); | |
| reject(e); | |
| } | |
| }); | |
| encoder.encode(); | |
| console.log(encoder.getEncodedBuffer().length); | |
| await fs.promises.writeFile( | |
| 'image5.jls', | |
| encoder.getEncodedBuffer(), | |
| {encoding:null} | |
| ); | |
| encoder.delete(); | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jpeg-2000 (via imagemagick, graphicsmagick does lossy j2 compression) wins by a factor of 1.6 (over IrfanView's jls plugin implementation, see below)
maybe charls-js is broken, it just outputs files twice as big as PNG (~1/3 the size of the RGB raw, but still 1.7x larger than IrfanView's jls output!)