Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
testing jpeg-ls compression rates
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();
})();
@Hashbrown777

This comment has been minimized.

Copy link
Owner Author

@Hashbrown777 Hashbrown777 commented Jun 25, 2021

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment