How can I do LZW decoding in Java? - java

I have a database which contains picture data stored as a binary blob. The documentation says the data is encoded using LZW. I thought that I could decode it using the Zip or GZip input streams found in the Java library, but it didn't work - I got an exception that said the format of the data is not correct.
From what I've read, the library uses DEFLATE, which is not LZW. Also, I've read about some licensing problems for using the LZW algorithm.
What can I use to decode the data? Is there a library? Do I have to implement it myself? What about the licensing problems?

I know the question is old, but I just wanted to add a great resource about LZW:
http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp
It's more specifically about the use of LZW in GIF images, but it explains the compression and decompression algorithms pretty well.

Here are a couple of links:
http://www.cs.sfu.ca/CC/365/li/squeeze/LZW.html
http://u.cs.biu.ac.il/~freskom1/AlgProg1/Progs/LZW.java
http://www.codeproject.com/KB/java/lzw.aspx
And there are others.
Indeed if the images are LZW compressed TIFF files, The Java Advanced Imaging API apparently supports decoding directly (though not encoding it seems).

You can also try with 7-Zip JBinding which uses the 7zip library internally. It's quite easy to use.

I went through a surprising amount of LZW implementations before finding one that worked for my case.
UncompressedInputStream from the BioJava project worked for me, when I needed to unpack a .pax file.

Related

Reading quantization tables of jpeg files on Android

A previous answer provides code for reading the quantization tables of jpeg files in Java. However it uses javax.imageio.ImageIO which, according to this answer, isn't available on the Android platform.
How can I read read the quantization tables of jpeg files on Android in Kotlin or the Java subset available on Android?
edit
I have found the korlibs/korim Kotlin library which seems to read the quantization tables. But I'm still a beginner with Kotlin so I haven't yet figured out how I go from a string with the path to the JPEG file to printing out the quantization table.
I am, unfortunately, not aware of such an API for Android.
However, parsing the JPEG quantization tables aren't that hard... You could probably have a look at my JPEGSegmentUtil and QuantizationTable (originally by Helmut Dersch) classes and adapt them to Android. The mentioned code is available under BSD license.
You would need to replace the javax.imageio.ImageInputStream with a different type, preferably something that implements java.io.DataInput and has proper seek/skip, replace javax.imageio.IIOException with normal IOException, and perhaps a few more tweaks.
If you only need to read quantization tables then the simplest solution would be to implement parsing yourself. Basic sample (in C#; but Java/Kotlin implementation is pretty similar): https://stackoverflow.com/a/46162800/136138
This conversation might also be useful: JPEG file quantization table definition
If you need to do more JPEG-related operations then look at JpegKit that wraps the native libjpeg-turbo library.

find compression mode of JPEG image

I was trying to open jpeg files in a Java program and noticed that neither ImageIO nor the Apache commons imaging library tools could open the images. The commons library showed me this error:
"Only sequential, baseline JPEGs are supported at the moment"
So, my image files are compressed in a way both libraries aren't able to read. I could make an ImageJ macro and transform all the images first but I would like to just use my program and not something extra.
Is there a way to find the compression mode in jpeg or even a java library that can read jpegs in several modes?
Thanks in advance
Suddenly it works with the ImageIO standard Class. Don't ask me why. Thanks for your help guys.
You need to get some tool that will allow you to dump a JPEG stream. There are a number of them out there.
What you are looking for is the start of frame marker.
FFC0 indicates baseline sequential.
FFC1 indicates extended sequential. It doesn't take much more code to do extended sequential than baseline. It is puzzling why a decoder would limit itself to baseline these day.
FFC2 is progressive.
There are others but those are the only ones you are likely to encounter and are widely supported.
You just need to find a tool that will save in baseline format. Finding one to read the other two formats is easy.

(JAVA) handling different compression types in zipstreams

I'm currently trying to build a function for hunting down files in nested zips. The general problem I can handle and I've got my code working correctly. The problem I'm having is related to compression formats.
Default zip files are not an issue, I open my zipinputstream, send it to my search function, check for each of the entries if it is a zip file and if so I wrap the inputstream in another zipinputstream and recurse. The problem comes when I have a non standard zip compression.
I would like to make this function much more robust: to be able to detect the compression format of the zip entry, wrap it in the correct deflater, and then wrap that in a zipinputstream so that I can recurse. The problem is I don't know how to decode the compression settings. I also don't know how to handle other zip compression formats.
Any advice on how to make this function more flexible would be helpful. I intend to integrate this into my own personal libraries so will be using this function from all sorts of directions in the future.
As far as I know, the only compression algorithm supported by java.util.zip is the standard one. But you can detect the compression algorithm using the ZipEntry getMehod() .
Adding support for other compression algorithms may require some work on your part - unless you can find a library with support for different algorithms. For instance, there is a library for 7zip's LZMA format, but the classes do not extend Decoder or ZipEntry - they just provide an alternative. You would need to extract the nested zip file uncompressed and then use something else to unzip it. This is probably not as clean as you would like.

JAI: Reading in 12 bit JPEG files

I'm brand new to Java Advanced Imaging, and the first stumbling block I've reached is the ability to read in a 12 bit, single band, greyscale JPEG file. I've seen references to it being possible with JAI, but no code or even suggestions about how it should be done. Could someone please help me out with either a helpful link or a short code snippet?
I've been using this tutorial so far, but it hasn't helped me on this issue.
Thanks.
JAI-ImageIO will register itself into Java's ImageIO api, so you should be able to use it just by having the jai-imageio jars in your classpath an calling the normal ImageIO methods, such as ImageIO.read(file).
The problem with jpeg might be that Java has a default jpeg reader in the IIORegistry, and you may have to select the right one manually using something like ImageIO.getImageReadersForFormatName().
Another thing with the more esoteric formats is that JAI ImageIO usually has two implementations - one pure Java and the other using native binary libraries, so make sure that you include the *lib-wrapper.dll (or whatever suits your particular OS) in the LD_LiBRARY_PATH or have the in the working directory of your program. The native implementation usually supports mode format variants than the pure-java one.
we usually read JPEG 12bit grayscale images using JAI + JAI Image I/O.
Additionally to previous answers, you need the native binary libraries to read JPEG 12bit, so mediaLib library is required.
You only need the "ImageRead" operation from Image I/O:
byte[] imageBytes = ...
RenderedOp readImage = JAI.create("ImageRead", new MemoryImageInputStream(imageBytes));
Whe usually read the image files from FTP, so get the byte[] and need the custom MemoryImageInputStream that wraps a byte[] into a ImageInputStream by subclassing ImageInputStreamImpl.

Compression and Decompression in java doesnot work fine for different programming language

If i compress and decompress data using java then it works fine
My if friend uses C#.NET to compress data while i am using java SO inthis case I can not decompress the same data.
I am using inflatter and deflater in java.
Thanks
Bapi
Use GZIPInputStream and GZIPOutputStream which are compatible with gzip format.
Note: each compression format is different (though similar in approach) and they have to be the same to work.
Inflator and Deflator are a cut down version of the GZIP format and I wouldn't expect it to work with anything another other than Java.
The java.util Deflator and Inflator support are the basic zip compression and decompression format implementations - they do not create a free standing archive file. Rather they create/read the data stream which corresponds to the data portion of one entry in an archive file.
To create an archive file readable by general compression utilities use either the java.util.GZIPXxx or java.util.ZipXxx classes.
Thanks for your support.
I changed my compression logic. I used GZIPInputStream both in java and in C#.NET
after this only. my problem is solved
Thanks
Bapi

Categories

Resources