Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing a byte array in Java and decompressing in C

I currently have the following array in a Java program,

byte[] data = new byte[800];

and I'd like to compress it before sending it to a microcontroller over serial (115200 Baud). I would like to then decompress the array on the microcontroller in C. However, I'm not quite sure what the best way to do this is. Performance is an issue since the microcontroller is just an arduino so it can't be too memory/cpu intensive. The data is more or less random (edit I guess it's not really that random, see the edit below) I'd say since it represents a rgb color value for every 16 bits.

What would be the best way to compress this data? Any idea how much compression I could possibly get?

edit

Sorry about the lack of info. I need the compression to be lossless and I do only intend to send 800 bytes at a time. My issue is that 800 bytes won't transfer fast enough at the rate of 115200 baud that I am using. I was hoping I could shrink the size a little bit to improve speed.

Every two bytes looks like:

0RRRRRGGGGGBBBBB

Where R G and B bits represent the values for color channels red, green, and blue respectively. Every two bytes is then an individual LED on a 20x20 grid. I would imagine that many sets of two bytes would be identical since I frequently assign the same color codes to multiple LEDs. It may also be the case that RGB values are often > 15 since I typically use bright colors when I can (However, this might be a moot point since they are not all typically > 15 at once).

like image 769
Anon Avatar asked Nov 11 '10 21:11

Anon


People also ask

How to compress byte array in java?

You can use the Deflater and Inflater classes in the java. util. zip package to compress and decompress data in a byte array, respectively.

Can you compress byte array?

Compressing a byte array is a matter of recognizing repeating patterns within a byte sequence and devising a method that can represent the same underlying information to take advantage of these discovered repetitions.

Can we convert byte array to file in java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.

Are byte arrays immutable?

A bytearray is very similar to a regular python string ( str in python2. x, bytes in python3) but with an important difference, whereas strings are immutable, bytearray s are mutable, a bit like a list of single character strings.


1 Answers

If the data is "more or less random" then you won't have much luck compressing it, I'm afraid.

UPDATE

Given the new information, I bet you don't need 32k colours on your LED display. I'd imagine that a 1024- or 256-colour palette might be sufficient. Hence you could get away with a trivial compression scheme (simply map each word through a lookup table, or possibly just discard lsbs of each component), that would work even for completely uncorrelated pixel values.

like image 54
Oliver Charlesworth Avatar answered Nov 08 '22 18:11

Oliver Charlesworth