jai-imageio
  1. jai-imageio
  2. JAI_IMAGEIO-7

jai imageIO stalls in native libarry call with Jpeg image

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: current
    • Fix Version/s: milestone 1
    • Component/s: www
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: PC

    • Issuezilla Id:
      7

      Description

      => With the JVM JDK 1.5
      => On Linux (RedHat ES3 and ES4) and Windows XP
      => For JAI 1.1.3 and 1.1.4 (daily build 11 december 2006)
      => For JAI-ImageIO 1.1 and 1.2 (daily build 12 decembre 2006)
      => Always reproducible

      => The expected behavior was to scale down the image
      => The observed behavior is : The thread enters the native lib and consumes up
      to 100% of the processor and finally makes the server unusable for uniprocessor
      linux. The only way to avoid a server crash is to kill the JVM. On windows with
      Bi-core CPUs, it sucks all the availabe ressource of one of the cores. It
      happens when handling a JPEG image.

      => Steps to reproduce the error :
      1/ Copy the following code
      2/ Get the image IMG_0036.JPG (link given below)
      3/ run the program with the following command line : java -jar ImageRescaler
      100 100 jpeg 1 IMG_0036.JPG

      //Code to copy to reproduce the error starts
      here////////////////////////////////////////////

      import java.awt.image.renderable.ParameterBlock;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;

      import javax.imageio.ImageIO;
      import javax.media.jai.Interpolation;
      import javax.media.jai.JAI;
      import javax.media.jai.ParameterBlockJAI;
      import javax.media.jai.PlanarImage;

      import com.sun.media.jai.codec.FileCacheSeekableStream;
      import com.sun.media.jai.codec.ImageCodec;
      import com.sun.media.jai.codec.ImageEncoder;
      import com.sun.media.jai.codec.JPEGEncodeParam;
      import com.sun.media.jai.codec.PNGEncodeParam;
      import com.sun.media.jai.codec.SeekableStream;
      import com.sun.media.jai.codec.TIFFEncodeParam;

      public class ImageRescalerBootstrap {

      static

      { JAI.enableDefaultTileCache(); }

      public static void main(String[] args) {
      if (args.length != 5)

      { System.out.println("usage: java [-jar <jar name>] ImageRescalerBootstrap <width> <height> <format> <quality> <file>"); System.out.println(" <width> Width in pixels of the target image"); System.out.println(" <height> Height in pixels of the target image"); System.out.println(" <format> The target format, which can be one of JPEG, PNG, GIF, TIFF, PSD"); System.out.println(" <quality> Quality - only available if using JPEG, be must be precised anyway"); System.out.println(" <file> Path to the file to transform, output file will be named <file>_out.<format>"); System.exit(0); }

      try

      { System.out.println("JAI " + JAI.getBuildVersion()); int width = Integer.parseInt(args[0]); int height = Integer.parseInt(args[1]); float quality = Float.parseFloat(args[3]); String format = args[2]; String path = args[4]; reWork(path, width, height, format, quality); }

      catch (Exception e)

      { System.out.println("Parameters are incorrect or an unknown exception happened"); e.printStackTrace(); }

      }

      public static boolean reWork(String filePath, int width, int height, String
      format, float quality) {
      InputStream inputStream = null;

      try

      { inputStream = new FileInputStream(filePath); }

      catch (IOException ioe)

      { ioe.printStackTrace(); }

      OutputStream outputStream = null;
      SeekableStream seekableStream = null;
      String outputFilePath = filePath + "_out." + format;

      try

      { File outputFile = new File(outputFilePath); outputFile.createNewFile(); outputStream = new FileOutputStream(outputFile); }

      catch (Exception ioe)

      { System.out.println("Could not write to Output file"); ioe.printStackTrace(); }

      try {
      seekableStream = new FileCacheSeekableStream(inputStream);
      ParameterBlockJAI pb = new ParameterBlockJAI("stream");
      pb.setParameter("stream", seekableStream);
      ImageIO.scanForPlugins();

      PlanarImage image = PlanarImage.wrapRenderedImage(ImageIO.read(seekableStream));

      System.out.print("Computing original Image dimensions... ");

      int originalWidth = image.getWidth();
      int originalHeight = image.getHeight();

      System.out.println("[ OK ]");

      if(originalWidth != 0 && originalHeight != 0 && originalWidth <= width &&
      originalHeight <= height)

      { //bunch of unrelated propriatery code, equivalent to do nothing }

      else {

      PlanarImage outimg = size(image, width, height, true);
      System.out.print("Rescaling image... " + filePath + "... ");

      ImageEncoder encoder = null;

      if (format.equalsIgnoreCase("jpeg"))

      { JPEGEncodeParam encodeParam = new JPEGEncodeParam(); encodeParam.setQuality(quality); encoder = ImageCodec.createImageEncoder(format, outputStream, encodeParam); }

      else if (format.equalsIgnoreCase("png"))

      { PNGEncodeParam encodeParam = new PNGEncodeParam.RGB(); encoder = ImageCodec.createImageEncoder(format, outputStream, encodeParam); }

      else if (format.equalsIgnoreCase("tiff") || format.equalsIgnoreCase("tif"))

      { TIFFEncodeParam encodeParam = new TIFFEncodeParam(); encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_NONE); encoder = ImageCodec.createImageEncoder(format, outputStream, encodeParam); }

      encoder.encode(/image/outimg);
      }

      System.out.println("[ OK ]");
      System.out.println("Operation Successfull... Output file was named " +
      outputFilePath);

      } catch(Exception e)

      { System.out.println("Unable to rescale the image from the file " + filePath); e.printStackTrace(); return false; }

      catch(Throwable t)

      { t.printStackTrace(); }

      finally {
      try

      { outputStream.flush(); }

      catch(Exception ignored) { }
      try

      { outputStream.close(); }

      catch(Exception ignored) { }
      try

      { inputStream.close(); }

      catch(Exception ignored) { }
      try

      { seekableStream.close(); }

      catch(Exception ignored) { }
      }

      return true;
      }

      //
      --------------------------------------------------------------------------------------------------------

      private static PlanarImage size(PlanarImage inImg, int width, int height,
      boolean maintainAspectRation) {
      Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
      int curwidth = inImg.getWidth();
      int curheight = inImg.getHeight();
      float sx = ((float)width / curwidth);
      float sy = ((float)height / curheight);
      if (maintainAspectRation) {
      // use the smallest scale if aspect ratio is to be maintained
      if (sy < sx)

      { sx = sy; }

      else

      { sy = sx; }

      }

      ParameterBlock params = new ParameterBlock();
      System.out.println("Adding Source...");

      params.addSource(inImg);
      params.add(sx); // x scale
      params.add(sy); // y scale
      params.add(0F); // x trans
      params.add(0F); // y trans
      params.add(interp); // interpolation method

      System.out.println("Starting 'scale' operation... ");

      PlanarImage outImg = JAI.create("scale", params);
      return outImg;
      }
      }
      // Code end here /////////////////////////////////////////////

      where IMG_0036.JPG is the image that can be found at :
      http://eric.guilhem.free.fr/IMG_0036.JPG
      It is this IMG_0036.JPG that makes native lib going defunct.

      The shape of the stack trace can be found at :
      http://eric.guilhem.free.fr/jai-thread-stall.PNG

        Activity

        Hide
        robertengels added a comment -

        Created an attachment (id=5)
        simple jpeg file that demonstrates problem

        Show
        robertengels added a comment - Created an attachment (id=5) simple jpeg file that demonstrates problem
        Hide
        bpb added a comment -
        Show
        bpb added a comment - bpb exported this issue to https://jai-imageio-core.dev.java.net/issues/show_bug.cgi?id=142 .
        Hide
        bpb added a comment -

        Moved to jai-imageio-core issue tracker.

        Show
        bpb added a comment - Moved to jai-imageio-core issue tracker.

          People

          • Assignee:
            jai-imageio-issues
            Reporter:
            redrico
          • Votes:
            2 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: