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

ColorConvertOp fails when destination image has IndexColorModel

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Bug is present in at least Java 11 and 17

      A DESCRIPTION OF THE PROBLEM :
      When a ColorConvertOp initialised using `new ColorConvertOp(RenderingHints)` is used with a destination image uses that IndexColorModel (eg. black-white image created with `new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY)`) the `ColorConvertOp.filter(BufferedImage, BufferedImage)` call will fail by throwing IllegalArgumentException.

      The `new ColorConvertOp(RenderingHints)` constructor says it works for converting BufferedImages, and has no "except when the destination uses IndexColorModel" clause. Neither does `ColorConvertOp.filter(BufferedImage, BufferedImage)` have caveats about IndexColorModel images.

      This is either a documentation bug, where it should be clearly stated that this special case is intended to not work. Or a code bug in the IndexColorModel special case code in `ColorConvertOp.filter(BufferedImage, BufferedImage)`.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Source ARGB converted to destination BW.
      ACTUAL -
      Exception in thread "main" java.lang.IllegalArgumentException: Destination ColorSpace is undefined
      at java.desktop/java.awt.image.ColorConvertOp.ICCBIFilter(ColorConvertOp.java:305)
      at java.desktop/java.awt.image.ColorConvertOp.filter(ColorConvertOp.java:277)
      at ColorConvertOpBug.main(ColorConvertOpBug.java:13)


      ---------- BEGIN SOURCE ----------
      import java.awt.image.BufferedImage;
      import java.awt.image.ColorConvertOp;

      public class ColorConvertOpBug
      {
        public static void main(String[] args)
        {
          BufferedImage src = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);
          BufferedImage dst = new BufferedImage(1,1,BufferedImage.TYPE_BYTE_BINARY);

          ColorConvertOp op = new ColorConvertOp(null);

          op.filter(src,dst);
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create the ColorConvertOp using `new ColorConvertOp(ColorSpace, ColorSpace, RenderingHints)` constructor instead. This does mean the ColorConvertOp can't be created before source and destination images are known.

      FREQUENCY : always


        1. ColorConvertOpBug.java
          0.4 kB
          Praveen Narayanaswamy

            jdv Jayathirth D V
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: