I am trying to use the Java Packbits algorithm to compress and decompress an array. I've thoroughly researched on google how to use it (step-by-step explanation) but it's all quite complicated. I was wondering if anyone out there can explain it to me? I have already declared my array...
Since Packbits is a compressions scheme (or so Wikipedia tells me) there isn't "an" algorithm for it. There are probably several libraries that use this compression scheme. Which are you trying to get to work, and what have you tried so far?
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I am trying to compress an array that comes from a computers RS232 port so it can be transferred to a USB port. On top of that I have to decompress it and display the array in a txt format (i.e. Notepad etc). I declared my array in Netbeans (just so I can learn how to use Packbits & not worry about importing it just yet) and now need to learn how to compress and decompress it.
Does that make sense?
This is for my coursework rewrites for uni.
Just to reiterate, I am not looking for someone to do this for me, I want to learn how to do it myself, just a little confused about how to use Packbits
I suggest that you first work out how the PackBits compression algorithm works, before you work on decompression. PackBits decompression logic will make you go "huh?" unless you have seen compression at work.
Joined: Jul 28, 2012
yes, that's my first step... Just confused how it actually works. I was wondering if anyone has used Packbits before and can explain how it works!
Well, if you can't understand it by reading the information already on the internet, I am not sure I can do any better. I'll try though
Packbits works by taking a series of repeated bytes, and representing it with 2 bytes. The first byte contains the number of times the byte is repeated. The next byte contains the actual bytes. So, for example 0xEAEAEAEAEA would be converted to 0x05EA. The second byte contains the actual byte (EA). the first byte indicates that EA has to be repeated 5 times.
To decompress a packbits stream, all you do is read first byte, read second byte. Put the second byte into the output the number of times the first byte tells you to. Look at the Wiki page, it has code.
Packbits works well when there are large swaths of repeated data in the file. For example, a lot of icons usually have only a couple of colors, and there are big areas of the icon that are colored the same way. Also, you can decompress on the fly. You don't need the whole file in memory to decompress. That's why they use Packbits in TIFF format, and TIFF format is popular for icons.
Also, another advantage of packbits for using icons is that it is easy to perform a lot of image manipulation without decompressing it the icon. For example, if you want to "scale up" an icon, you just take every other byte and multiply it, and then repeat each row several times. Merging 2 images is also possible, but little difficult.
Anyways, I digress
Joined: Oct 13, 2005
Thank you for the information, Jayesh A Lalwani
That confirms what I thought all along, that this is a difficult question which ought to be somewhere other than “beginning”. Moving!
Joined: Jul 28, 2012
Thanks for the information Jayesh A Lawlwani, I completely understand what it's supposed to do. If you type "java packbits compression" on google, the first link that pops up downloads a text file with multiple lines of code for Packbits. I copied and pasted it into Netbeans and have figured out the general outline of how it tries to compress a file via RLE. I have copied that onto a new Java class and have extracted the main compression section (not worrying about decompression yet). It's all set up:
Now how do I (in layman's terms) "apply" this to my array?? :S
P.S. Sorry if I'm not supposed to include so much code in this post, I'm new to the ranch
Your code is too complicated in my tiny brain . I do see a problem though. In line 71, you are accessing an array that hasn't been initialized yet. You created the array
in line 61.
You might want to think about building modularly. The way I apporach a problem like this is go from a higher level of abstraction to lower, and build functions as I go deeper and deeper. Write out psuedo code before you implement it
So, for example, I start off with this psuedo code
This is the psuedo code that represents your algorithm at a very high level. Implement this in Java. DOn;'t worry about how operations like findOutputLen, read Number Of Reps From Input, read Data Byte from Input and add d To Output n number of times will work. Just implement them as functions that maybe return some dummy data. Make sure it compiles, runs and does what you want it to do. Then take one of those functions, and break it down further. Rinse repeat.
Joined: Jul 28, 2012
...and that's a really good way of looking at it. I'll definitely give that a try!