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

Color errors inside images created with jai

    Details

      Description

      FULL PRODUCT VERSION :
      Error appears first time with JDK 6u18 and is still happen with JDK6u22

      ADDITIONAL OS VERSION INFORMATION :
      Platform independed

      A DESCRIPTION OF THE PROBLEM :
      The problem seems not to be jai related, because the program not changed and the result differs when executed with u17 and u18.

      Use jai to merge some images. With u17 the result is as expected, with u18 some colors inside the image which should be red turned into blue.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just compile and execute the given sourcecode. (java advanced imaging api is required)

      Because I couldnt find option to append attachments to this bugreport, I added the nessesary files and the results created with 6u17 and 6u18:

      http://tfromm.com/imagebug/

      Here the result created with JDK 6 u 17:
      http://tfromm.com/imagebug/result.u17.png

      And the broken one with u18:
      http://tfromm.com/imagebug/result.u18.png


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------


      import javax.imageio.ImageIO;
      import javax.media.jai.LookupTableJAI;
      import javax.media.jai.PlanarImage;
      import javax.media.jai.RasterFactory;
      import javax.media.jai.TiledImage;
      import javax.media.jai.operator.LookupDescriptor;
      import javax.media.jai.operator.MosaicDescriptor;
      import javax.media.jai.operator.TranslateDescriptor;
      import java.awt.*;
      import java.awt.image.*;
      import java.io.File;
      import java.io.FileInputStream;
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      import java.util.Properties;

      public class ImageBug {

          public static void main(String[] args){
              try {
                  File[] images = new File[]{ new File("zh_TW.png"), new File("vi_VN.png"), new File("zh_CN.png") };
                  float x = 0;
                  float y = 0;
                  List<RenderedImage> renderedImages = new ArrayList<RenderedImage>();

                  Properties spriteProperties = new Properties();

                  for(File file : images){
                      FileInputStream fis = new FileInputStream(file);

                      try {
                          RenderedImage renderedImage = ImageIO.read(fis);

                          int height = renderedImage.getHeight();
                          int width = renderedImage.getWidth();

                          renderedImage = convert(renderedImage);

                          renderedImage = TranslateDescriptor.create(
                                  renderedImage, x, y, null, null);

                          renderedImages.add(renderedImage);

                          String key = file.toString();

                          String value = (int)y + "," + height + "," + width;

                          spriteProperties.setProperty(key, value);

                          y += renderedImage.getHeight();
                      } finally {
                          fis.close();
                      }
                  }

                  RenderedImage renderedImage = MosaicDescriptor.create(
                          renderedImages.toArray(
                                  new RenderedImage[renderedImages.size()]),
                          MosaicDescriptor.MOSAIC_TYPE_OVERLAY, null, null, null, null,
                          null);

      ImageIO.write(renderedImage, "png", new File("result.png"));

              } catch (Throwable t){
                  t.printStackTrace();
              }
          }
          private static final int _NUM_OF_BANDS = 4;
          
          protected static RenderedImage convert(RenderedImage renderedImage) throws Exception {

              int height = renderedImage.getHeight();
              int width = renderedImage.getWidth();

              SampleModel sampleModel = renderedImage.getSampleModel();
              ColorModel colorModel = renderedImage.getColorModel();

              Raster raster = renderedImage.getData();

              DataBuffer dataBuffer = raster.getDataBuffer();

              if (colorModel instanceof IndexColorModel) {
                  IndexColorModel indexColorModel = (IndexColorModel)colorModel;

                  int mapSize = indexColorModel.getMapSize();

                  byte[][] data = new byte[4][mapSize];

                  indexColorModel.getReds(data[0]);
                  indexColorModel.getGreens(data[1]);
                  indexColorModel.getBlues(data[2]);
                  indexColorModel.getAlphas(data[3]);

                  LookupTableJAI lookupTableJAI = new LookupTableJAI(data);

                  renderedImage = LookupDescriptor.create(
                      renderedImage, lookupTableJAI, null);
              }
              else if (sampleModel.getNumBands() == 2) {
                  List<Byte> bytesList = new ArrayList<Byte>(
                      height * width * _NUM_OF_BANDS);

                  List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);

                  for (int i = 0; i < dataBuffer.getSize(); i++) {
                      int mod = (i + 1) % 2;

                      int elemPos = i;

                      if (mod == 0) {
                          tempBytesList.add((byte)dataBuffer.getElem(elemPos - 1));
                          tempBytesList.add((byte)dataBuffer.getElem(elemPos - 1));
                      }

                      tempBytesList.add((byte)dataBuffer.getElem(elemPos));

                      if (mod == 0) {
                          Collections.reverse(tempBytesList);

                          bytesList.addAll(tempBytesList);

                          tempBytesList.clear();
                      }
                  }

                  byte[] data = new byte[bytesList.size()];
                  for(int i=0, size=data.length; i < size; i++){
                      data[i] = bytesList.get(i);
                  }

                  DataBuffer newDataBuffer = new DataBufferByte(data, data.length);

                  renderedImage = createRenderedImage(
                      renderedImage, height, width, newDataBuffer);
              }
              else if (colorModel.getTransparency() != Transparency.TRANSLUCENT) {
                  List<Byte> bytesList = new ArrayList<Byte>(
                      height * width * _NUM_OF_BANDS);

                  List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);

                  for (int i = 0; i < dataBuffer.getSize(); i++) {
                      int mod = (i + 1) % 3;

                      int elemPos = i;

                      tempBytesList.add((byte)dataBuffer.getElem(elemPos));

                      if (mod == 0) {
                          tempBytesList.add((byte)255);

                          Collections.reverse(tempBytesList);

                          bytesList.addAll(tempBytesList);

                          tempBytesList.clear();
                      }
                  }

                  byte[] data = new byte[bytesList.size()];
                  for(int i=0, size=data.length; i < size; i++){
                      data[i] = bytesList.get(i);
                  }

                  DataBuffer newDataBuffer = new DataBufferByte(data, data.length);

                  renderedImage = createRenderedImage(
                      renderedImage, height, width, newDataBuffer);
              }

              return renderedImage;
          }

          protected static RenderedImage createRenderedImage(
                  RenderedImage renderedImage, int height, int width,
                  DataBuffer dataBuffer) {

              SampleModel sampleModel =
                      RasterFactory.createPixelInterleavedSampleModel(
                              DataBuffer.TYPE_BYTE, width, height, _NUM_OF_BANDS);
              ColorModel colorModel = PlanarImage.createColorModel(sampleModel);

              TiledImage tiledImage = new TiledImage(
                      0, 0, width, height, 0, 0, sampleModel, colorModel);

              Raster raster = RasterFactory.createWritableRaster(
                      sampleModel, dataBuffer, new Point(0, 0));

              tiledImage.setData(raster);

              return tiledImage;
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Can't find workaround at the moment :-(

      I stuck at the moment with u16, but I need some Win7 fixes done in later JDK releases.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: