• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Packbits Algorithm

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Daniel Ward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic