Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4622201

javax.imageio cannot compress JPEG images in JDK 1.4

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.1
    • 1.4.0
    • client-libs
    • 2d
    • hopper
    • x86
    • windows_nt



      Name: ddT132432 Date: 01/10/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      There is a bug in the
      com.sun.imageio.plugins.jpeg.JPEGImageWriter.write().
      In the lines

                  switch(param.getCompressionMode()) {
                  case ImageWriteParam.MODE_DISABLED:
                      throw new IIOException("JPEG compression cannot be disabled");
                  case ImageWriteParam.MODE_EXPLICIT:
                      float quality = param.getCompressionQuality();
                      qTables = new JPEGQTable[2];
      >>>>> qTables[0] =JPEGQTable.K1Div2Luminance.getScaledInstance(quality, true);
                      qTables[1] =JPEGQTable.K2Div2Chrominance.getScaledInstance(quality, true);
                      break;

      there is a call to
      JPEGQTable.K1Div2Luminance.getScaledInstance(quality, true);

      This method requires a quality value from 1 to 255.
      Unfortunately, quality is a value between 0 and 1.
      This restriction is enforced by
      javax.imageio.ImageWriteParam.setCompressionQuality().

      This means you can't compress jpeg images!

      The fix is to either modify
      com.sun.imageio.plugins.jpeg.JPEGImageWriter.write().
      or
      JPEGQTable.K1Div2Luminance.getScaledInstance()

      to translate the quality to some value between 1 and 255.

      Here is a test program if you need to test it out.
      Run the program with

          java bug1 <input-jpg-file> <output-jpg-file>
      <compression-factor>



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run the supplied test program with compression values
      from 0 to 1 using a jpeg image that's at least 50K in size.
      you will not be able to compress it very much.
      e.g. compression to 10K is impossible.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      using a compression value of .05 should result
      in a very small-sized image file.

      using a compression value of 1.0 should result
      in a very large-sized image file.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.image.*;
      import java.io.*;
      import java.util.*;
      import javax.swing.*;
      import javax.imageio.*;
      import javax.imageio.stream.*;

      public class bug1 {
          public static void main(String[] args) {
              // Create a image to save

              // Write generated image to a file
              try {
      //
                  RenderedImage rendImage = ImageIO.read(new File(args[0]));

                  File file = new File(args[1]);
                  file.delete();
                  ImageOutputStream ios = ImageIO.createImageOutputStream(file);
          
                  ImageWriter writer = null;
                  Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
                  if (iter.hasNext()) {
                      writer = (ImageWriter)iter.next();
                  }

                  writer.setOutput(ios);
                  ImageWriteParam iwparam = writer.getDefaultWriteParam();
                  iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
                  System.out.println(iwparam.getCompressionQuality()) ;
                  iwparam.setCompressionQuality(Float.parseFloat(args[2])) ;
                  writer.write(null, new IIOImage(rendImage, null, null), iwparam);
                  ios.flush();
                  writer.dispose();
                  ios.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }

      ---------- END SOURCE ----------
      (Review ID: 137909)
      ======================================================================

            campbell Christopher Campbell (Inactive)
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: