B4J Code Snippet LZ4 Fast Compress and Decompress

Hi there...

I am in search for faster ways to compress/decompress images... bytes in general...

So i ve done some work... not much, i am sharing here with you guys..

Need some Inline java:
B4X:
#if java

import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4FastDecompressor;
import java.util.Arrays;

private static int decompressedLength;
private static LZ4Factory factory = LZ4Factory.fastestInstance();
private static LZ4Compressor compressor = factory.fastCompressor();

public static byte[] compress(byte[] src, int srcLen) {
    decompressedLength = srcLen;
    int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
    byte[] compressed = new byte[maxCompressedLength];
    int compressLen = compressor.compress(src, 0, decompressedLength, compressed, 0, maxCompressedLength);
    byte[] finalCompressedArray = Arrays.copyOf(compressed, compressLen);
    return finalCompressedArray;
}

private static LZ4FastDecompressor decompressor = factory.fastDecompressor();

public static byte[] decompress(byte[] finalCompressedArray, int decompressedLength) {
    byte[] restored = new byte[decompressedLength];
    restored = decompressor.decompress(finalCompressedArray, decompressedLength);
    return restored;
}

#end if

some subs:
B4X:
#AdditionalJar: lz4-java-1.8.0
..
Private jo As JavaObject
...
public Sub Compress(bytes1() As Byte,llnt As Int) As Byte()
    Return jo.RunMethod("compress", Array(bytes1,llnt))
End Sub

public Sub UnCompress(bytes1() As Byte,llnt As Int) As Byte()
    Return jo.RunMethod("decompress", Array(bytes1,llnt))
End Sub

And you can use it like this:
B4X:
'aa can be a byte array you wanna compress...

    Dim bb() As Byte=Compress(aa,aa.Length)
    Log("New Size: " & bb.Length)

    Dim cc() As Byte=unCompress(bb,aa.Length) 'You must know the uncompressed size !!! caution !!!
    Log("New Size Restored: " & cc.Length)
You will need lz4-java... download from here..

Hope helps...
 

kimstudio

Active Member
Licensed User
Longtime User
@Magma Could you share your experience on the average compression speed for a normal size image for example 1920*1080? then we know what application scenarios we can use this fast compression method. Thanks.
 

Magma

Expert
Licensed User
Longtime User
@Magma Could you share your experience on the average compression speed for a normal size image for example 1920*1080? then we know what application scenarios we can use this fast compression method. Thanks.
Yes... ofcourse...

Have in mind that compressing a picture... with LZ4... is not usable... you must uncompress...
but is a lot faster than zlib... gzip.. when using at Fastest Mode :) at compress and uncompress !
check here...

As compression not making magic... but is good enough... for example one uncompressed image-raw at 1920x1080 is about ~8MB... with LZ4... goes at 150~250kb but have in mind that is loseless... if you uncompressed it... you can have 100% quality !

and decompressing very fast !

ps1: In real scenarios... zlib for 8MB... can take about 10ms... and this... want about 1-2ms.. but you can't say exactly because CPU is at middle...
ps2: actually is not only for "images"... is for bytes... so for everything !
 
Last edited:

kimstudio

Active Member
Licensed User
Longtime User
8MB compression speed 1-2ms is amazing, compression ratio to 150-250kb is crazy for lossless, just guess the image has almost empty/same color background and not too much details (edges) at all, but still will be very useful. Thanks for the lib!
 

Magma

Expert
Licensed User
Longtime User
8MB compression speed 1-2ms is amazing, compression ratio to 150-250kb is crazy for lossless, just guess the image has almost empty/same color background and not too much details (edges) at all, but still will be very useful. Thanks for the lib!
Didn't make a lot... was easy - actually everything is easy with B4X ;) ... LZ4 creators make a lot of job...

ps: picture... was desktop capture... including the window of b4j and a lot of my wallpaper :) so... yes wasn;t every pixel... different... color - other test with almost every pixel different... was 3MB from 8MB but... who gonna make "noisy-pictures".... ???
 
Top