find compression mode of JPEG image - java

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.

Related

Creating videos with Java2D

I'm in the process of writing a Java and FFmpeg based video editor, and I'm trying to find a library that would allow me to create a video from frames rendered via Java2D.
By 'video' I mean in a standard format (preferably vp8/webm, but anything common should be alright). It would be a plus if there was some facility for modifying preexisting videos as well, but that may need to be left to ffmpeg. Audio isn't needed as I'll mainly be working with ffmpeg for that.
The obvious solution would be to save each frame as an image and have ffmpeg combine them - but I worry about performance and quality using this method. Additionally, some tests showed that even short videos (less than 5 minutes) at 1280x720 resolution would be pretty massive in size - which I'd like to avoid if possible. Working a little more directly with compressed formats rather than huge batches of image files would certainly be preferred, or at least some method that isn't too hungry for disk space.
I'm not against homebrew solutions either (I'm already writing the ffmpeg bindings from scratch), but I don't know how practical it would be to write my own vp8 encoder for something that seems like it should be fairly simple.
Any suggestions on where to go with this? Or is the best solution to generate a individual image files and combine them later?
Thanks!
Have you had a look at Xuggler? It should be able to encode videos in the way you describe, though I haven't tried producing videos from a series of BufferedImages it should be possible.
It sits on top of ffmpeg and is pretty powerful with what it can achieve - it's not the easiest API to start with but there are a number of good tutorials around.

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.

How can I do LZW decoding in 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.

Java applet screen capture to a video

I wanted to find out how can one capture screencast using java. I know that using Robot class one can get a screenshot but how do I go about capturing it as a video and then uploading it to the server? How exactly would that work?
ideas?
With a pure Java solution, I doubt that it will work, but it depends of course on what your interpretation of "video".
On my desktop with a 1920x1200 resolution, I am able to get about 20 frames per second when using the Java Robot to capture the entire screen. Since each image contains >6 MByte of uncompressed data, I would need more than 1 Gbps bandwidth to transmit the raw data of these images to a server. Most probably, requiring so much bandwidth is not acceptable, so you either have to decrease the number of frames per second or apply some kind of compression to the images.
One possibility is to compress each image using one of the image formats supported by ImageIO. The size of the compressed images will of course depend heavily on what is actually shown on the screen, but the performance of the compressors is not particularly good. Compressing to PNG ought to give the best lossless compression ratio for most desktop content, but at least my computer is only able to process just about 2 frames per second. Using the JPEG compressor with default quality settings reaches about 5 frames per second.
Using common video codecs through an abstraction layer like jffmpeg will probably achieve both better performance and better compression ratio, but I doubt that mainstream video codecs like WMV or H.264 are suitable for common desktop content.
If you really require a pure Java solution (and are not able to use any of the available standalone software, which do what you're asking for), I would make an attempt to implement my own, simple compression algorithm. With common desktop activity, there ought to be very little difference between most consecutive screen shots, so what might work quite well is to transmit the first frame completely and after that implement an algorithm to roughly detect rectangles, in which changes have been made and then transmit only these combined with JPG or preferrably (quality) PNG compression.
Or use Xuggler, a better wrapper for FFmpeg in Java. In fact, the code for capturing the screen and encoding the video is one of the standard tutorials.
I'm also curious about this. https://www.screencast.com/ is currently doing just this with a pure java (or at least straight out of the browser) experience.
You can just use something like Java to a native FFMPEG build, and execute the command line at runtime. Here is an applet that I made that does just that: http://have2chat.net/screencast/
I have downloaded the main capture *.JAR file for the Screencast-O-Matic.com. To download the file:
Go to http://screencast-o-matic.com/jars/ScreencastOMaticRun-1.0.5.jar
Save the file
Extract the contents (I DO NOT intend to use this commercially!)

Reading RAW images from Java

Canon/Nikon/other cameras save raw output of their sensor in some of their proprietary formats (.CR2, whatever). Is there any Java library designed to read them and convert into manageable BufferedImages?
I don't reqlly care here about fully customizable conversion similar to ufraw or imagemagick, rather something simple that "just works" for rendering simple previews of such images.
I've been where you are, and I feel for you. Your best bet is to use an Adobe or dcraw-based program to create thumbnails automatically. Temporary DNG files using Adobe's converter may be easier to use.
IF you insist on doing it in Java, you're about to run into a mountain of pain. RAW formats change often, have all sorts of crazy nuances and are intentionally hard to work with. Camera makers want you to use THEIR RAW conversion software, to show the camera's abilities at its best and screw Adobe. The guy behind dcraw found that some are camera manufacturers even use encryption now!
The existing Java libraries are poor -- JRawIO has improved since I last looked at it, but it supports only a fraction of the formats that dcraw does. In addition to the listed libraries, the imagero library may provide the ability to display a thumbnail for your image.
From personal experience, don't even think about writing your own RAW file reader.
I tried to do this with a very simple RAW format once (just a solid array of sensor data, 12 bits per pixel). The dcraw source translates badly to Java. You haven't seen such a nightmare of bit-fiddling ever. Took me days to debug problems with byte alignment and endian-ness.
jrawio is a plugin for Java Image I/O. With it you can read the raster data, the thumbnails and the metadata from the raw image file.
nef and cr2 already contains preview images in jpeg. just find the right offset and the right length to extract it...
Laurent Clevy # lclevy.free.fr/raw
Laurent
Unless you want to write you own file parser/loader (sounds fun imho ;) ), perhaps JMagick will help you. I haven't tried it and it might not work given your target platform since JMagick uses JNI.
UPDATE: dcraw looks like a good resource/reference

Categories

Resources