wood burning stoves 2.0*
The moose likes Java in General and the fly likes Packbits Algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Packbits Algorithm" Watch "Packbits Algorithm" New topic
Author

Packbits Algorithm

Daniel Ward
Greenhorn

Joined: Jul 28, 2012
Posts: 5
Hi all,

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...


Regards,


The fastest RacingSalmander of all time!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3596
    
  14

Hi Daniel, welcome to CodeRanch!

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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
And welcome to the Ranch
Daniel Ward
Greenhorn

Joined: Jul 28, 2012
Posts: 5
Thanks for the reply!

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


RacingSalmander
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

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.
Daniel Ward
Greenhorn

Joined: Jul 28, 2012
Posts: 5
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!
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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!
Daniel Ward
Greenhorn

Joined: Jul 28, 2012
Posts: 5
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




i
}



P.S. Sorry if I'm not supposed to include so much code in this post, I'm new to the ranch
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

What happens when you run this? It fails?

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.

Daniel Ward
Greenhorn

Joined: Jul 28, 2012
Posts: 5
I see,


...and that's a really good way of looking at it. I'll definitely give that a try!

Thank you!

Much Appreciated!


Dan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Packbits Algorithm
 
Similar Threads
How to check if the elements in one array are present in another array
Best Fit Algorithm
How to sort Arraylist without Sort method
Can i break the String to bits?
Playing around with 2D arrays