Gif to JPEG Conversion with Java1.3

Hi,
I am finding the best and fastest way of converting a GIF Image to JPEG, with the restriction that I am using JDK1.3
I have tried using JAI but at times the images returned are broken. And Black and White images cannot be converted at all. Can someone please please help fix this problem.
Here is the code that doesnt function well.

Also, when this code is loaded on a HP-UX box, it throws a register error stating something is already registered.

****************
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import org.apache.log4j.Logger;

import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;
import com.tmobile.wfe.idc.web.cache.CacheItem;
import com.tmobile.wfe.idc.web.cache.HTTPCache;

/**
 * Cache which automatically converts source images into JPEGs.
 * Uses JAI classes to perform the conversion (so needs jai_core.jar and jai_codec.jar).
 */
public class ImageTranscoderCache extends HTTPCache {

      /** Log4j logger. */
      private static Logger log = Logger.getLogger(ImageTranscoderCache.class);

      /** Mime-type for GIF images */
      public static final String GIF_MIME_TYPE = "image/gif";

      /** Mime-type for JPEG images */
      public static final String JPEG_MIME_TYPE = "image/jpeg";
      
      /** Quality setting for JPEG compression. */
      private float jpegQuality;
      
      public ImageTranscoderCache(int expiryMinutes, int maxSize,
            String username, String password, float jpegQuality)
      {
            super(expiryMinutes, maxSize, username, password);
            this.jpegQuality = jpegQuality;
      }
      
      /**
       * Converts GIF images into JPEGs, leaving other types of media unchanged.
       * Uses the HTTPCache superclass's createCacheItem method to actually fetch the
       * contents of the target URL (as a byte[] array), which both simplifies things here,
       * and ensures that any enhancements to that method get used here.
       * TODO Monochrome GIFs don't seem to get work, so left alone.  Fix!
       */
      protected CacheItem createCacheItem(String itemId) throws IOException {
            // Get the source image data
            CacheItem sourceCacheItem = super.createCacheItem(itemId);

            // Return the cached item unchanged if it isn't a GIF (doesn't start with GIF mimetype).
            if (sourceCacheItem.getMime_type().indexOf(GIF_MIME_TYPE) != 0) {
                  log.debug("The MIME Type is: " + sourceCacheItem.getMime_type());
                  log.debug("Not a GIF: " + itemId);
                  return sourceCacheItem;
            }
            
            // Read the source image as a JAI object
            final byte[] sourceData = sourceCacheItem.getData();
            
            if (sourceData == null || sourceData.length == 0) {
                  throw new IOException("No image data read for: " + itemId);
            }
            RenderedOp sourceImage = JAI.create("stream", new ByteArraySeekableStream(sourceData));

            // If monochrome then leave unchanged (since JAI then seems to generate a JPEG that
            // browsers can't understand).
            // TODO Fix this!  Try changing the colour rep?
            if (sourceImage.getColorModel().getPixelSize() == 1) {
                  log.debug("Monochrome, so leaving unchanged");
                  return sourceCacheItem;
            }
            
            // Then convert it to a JPEG
        JPEGEncodeParam encodeParam = new JPEGEncodeParam();
        encodeParam.setQuality(jpegQuality);  // TODO change this to 1.0F high quality!
       
        // Write it to a byte array
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", outStream, encodeParam);
        encoder.encode(sourceImage);
            outStream.close();      // close actually does nothing, so don't need finally
            
            // And return the converted image
            final byte[] jpegData = outStream.toByteArray();
            log.debug("Converted to JPEG.  Was " + sourceData.length + " bytes, now "
                        + jpegData.length);
            return createCacheItem(itemId, jpegData, JPEG_MIME_TYPE);
      }
}
WilkhuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareCommented:
Hi, I gave up to do all those conversions myself,
it's possible to call a good tool from JAVA.
F.i. irfan@linux.tuwien.ac.at  is a good freeware tool from anything to anything and commandline-enabled,
so you can call it from JAVA.
<*>
0
WilkhuAuthor Commented:
I am calling the conversion from a servlet and caching the result hence I cannot use commandline.

Are there any other external jars etc that have been developed that I can use to perform this conversion under Java1.3?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

WilkhuAuthor Commented:
I have the following environment restrictions:
HP-UX running jdk1.3 hence, I need some freeware that I can use in my application to convert the images.

If there are commercially available jars that you may be aware of, I would also look into this.
0
sciuriwareCommented:
Even a servlet can practise             Runtime.exec("..........");
<*>
0
sciuriwareCommented:
Sorry, mine was an MSWindows utility.
<*>
0
WilkhuAuthor Commented:
Does anyone know of any commercial API's available to go image conversion using Java1.3? (Apart from the ones that dont function well like the JAI etc)
0
WilkhuAuthor Commented:
Yes, I have seen this list but is there one that has been recommended or used successfully for these converstions?
What is highly used by people to solve this issue? (Apart from upgrading to jdk1.4)
0
girionisCommented:
Not sure what else you could do. The links I posted are the only solutions I know (apart from upgrading).
0
WilkhuAuthor Commented:
Do you know which one works though?
0
sciuriwareCommented:
I guess geocities is the best choice in your situation.
There is not very much left.
Even google doesn't list anything else.
<*>
0
WilkhuAuthor Commented:
OK, Here is another final question related to this. If I am running a server with Java 1.3 but also have Java 1.4 installed on the same server, is it possible to have one web application running on the weblogic instance(instance running jdk1.3) to use jdk1.4 which is available on the server.
0
girionisCommented:
No.
0
sciuriwareCommented:
It should be possible to launch a JAVA program that runs on another JAVA release
on the same machine, if only you can keep the environments separated
and if you can provide the right path settings to it.
Note: it's not possible to CALL such a program from another JVM.
<*>
0
WilkhuAuthor Commented:
Yeap, I thought so. The weblogic instance is running the 1.3 JVM hence it would not be possible for the application to call 1.4 libraries.

Still not sure which library to use. Have read the geocities site but cannot seem to find the best one that does the job. Any ideas?
0
sciuriwareCommented:
Per definition:
to call a JAVA program from a JAVA program is:      class.method(....);
to launch ditto is:                                                  Runtime.exec(<path to the other java>, <path to that program>);
<*>
0
WilkhuAuthor Commented:
sciuriware,
You sent the following email address: F.i. irfan@linux.tuwien.ac.at  

How to I get hold on more information about this tool?


0
WilkhuAuthor Commented:
Trying to look for this freeware tool you mentioned but cannot seem to get to it. Can you please provide the direct link or information.
0
sciuriwareCommented:
Hey! I never noticed that!!!!
If he developed it on LINUX it will surely run on UNIXes.
I do use GIMP on LINUX but that's not directly(!) availavle on HPUX.
The credits list:
                http://members.chello.at/irfan.skiljan/   :: home address in Austria,
                http://www.ryansimmons.com/users/irfanview/  :: USA contacts,
                http://www.irfanview.com/  :: for Luxembourg.
I used this version 3.36 over 2 years now, so, maybe things have changed a lot.
<*>
0
WilkhuAuthor Commented:
The software seems to be supported only on the following platforms:
Windows 9x/ME/NT/2000/XP/2003.

So wont be of much use for me using HP-UX
0
WilkhuAuthor Commented:
Still looking for some support for a solution.....
Anyone can help?
0
girionisCommented:
Try the link in my first post, it should do the trick.
0
girionisCommented:
I think that my fisrt comment answers the question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.