public void testCreateGrayscale() {
    // create a 8-bit grayscale ImageTypeSpecifier
    ImageTypeSpecifier type = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, true);

    ColorModel model = type.getColorModel();
    assertEquals(
        "Failed to return the colorspace type",
        ColorSpace.TYPE_GRAY,
        model.getColorSpace().getType());
    assertEquals("Failed to return the transparency", Transparency.OPAQUE, model.getTransparency());
    assertEquals(
        "Failed to return the transfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the pixel size", 8, model.getPixelSize());

    // create a 16-bit grayscale AlphaPremultiplied ImageTypeSpecifier
    type = ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true, false);

    model = type.getColorModel();
    assertEquals(
        "Failed to return the colorspace type",
        ColorSpace.TYPE_GRAY,
        model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the transfer type", DataBuffer.TYPE_USHORT, model.getTransferType());
    assertEquals("Failed to return the pixel size", 32, model.getPixelSize());
  }
  public void testCreateFromBufferedImageType() {
    for (int i = BufferedImage.TYPE_INT_RGB; i <= BufferedImage.TYPE_BYTE_INDEXED; i++) {
      BufferedImage bi = new BufferedImage(1, 1, i);
      ColorModel expected = bi.getColorModel();

      ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(i);
      ColorModel actual = typeSpecifier.getColorModel();
      String msg =
          "Failed to create correct ImageTypeSpecifier, bufferedImageType = " + Integer.toString(i);
      assertEquals(msg, expected.getClass(), actual.getClass());
      assertEquals(msg, expected.getColorSpace(), actual.getColorSpace());
      assertEquals(msg, expected.getTransferType(), actual.getTransferType());
      assertEquals(msg, expected.getTransparency(), actual.getTransparency());
    }
  }
  public ColorModelNode(String name, ColorModel colorModel) {
    super(name, colorModel);
    addClassChild();

    ColorSpace colorSpace = colorModel.getColorSpace();
    add(new ColorSpaceNode(colorSpace));

    int numColorComponents = colorModel.getNumColorComponents();
    addIntChild("numColorComponents", numColorComponents);

    int numComponents = colorModel.getNumComponents();
    addIntChild("numComponents", numComponents);

    boolean hasAlpha = colorModel.hasAlpha();
    addBooleanChild("hasAlpha", hasAlpha);

    int pixelSize = colorModel.getPixelSize();
    addIntChild("pixelSize", pixelSize);

    int transferType = colorModel.getTransferType();
    String transferTypeDescription = getTransferTypeDescription(transferType);
    addStringChild("transferType", transferTypeDescription);

    int transparency = colorModel.getTransparency();
    addStringChild("transparency", getTransparencyDescription(transparency));

    boolean isRGB = isRgbColorModel(colorModel);
    addBooleanChild("isRGB", isRGB);

    boolean isBGR = isBgrColorModel(colorModel);
    addBooleanChild("isBGR", isBGR);
  }
  public void testCreateBanded() {
    int[] bankIndices = new int[] {0, 1, 2};
    int[] bandOffsets = new int[] {1, 1, 1};
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int dataType = DataBuffer.TYPE_BYTE;
    boolean hasAlpha = false;

    ImageTypeSpecifier typeSpecifier =
        ImageTypeSpecifier.createBanded(
            colorSpace, bankIndices, bandOffsets, dataType, hasAlpha, false);

    ColorModel colorModel = typeSpecifier.getColorModel();
    assertEquals(
        "Failed to create with the correct colorspace type",
        ColorSpace.TYPE_RGB,
        colorModel.getColorSpace().getType());
    assertEquals(
        "Failed to create with the correct transparency",
        Transparency.OPAQUE,
        colorModel.getTransparency());
    assertEquals(
        "Failed to create with the correcttransfer type",
        DataBuffer.TYPE_BYTE,
        colorModel.getTransferType());

    BandedSampleModel sampleModel = (BandedSampleModel) typeSpecifier.getSampleModel();
    assertArrayEquals(
        "Failed to create with the correct bankIndices", bankIndices, sampleModel.getBankIndices());
    assertArrayEquals(
        "Failed to create with the correct bankIndices", bandOffsets, sampleModel.getBandOffsets());
  }
  public void testCreateIndexed() {
    byte[] redLUT = new byte[] {1, 10};
    byte[] greenLUT = new byte[] {2, 20};
    byte[] blueLUT = new byte[] {3, 30};
    byte[] alphaLUT = new byte[] {4, 40};

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createIndexed(
            redLUT, greenLUT, blueLUT, alphaLUT, 1, DataBuffer.TYPE_BYTE);
    ColorModel model = type.getColorModel();

    assertEquals(
        "Failed to return the colorspace", ColorSpace.TYPE_RGB, model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the tranfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the red color component", 1, model.getRed(0));
    assertEquals("Failed to return the red color component", 10, model.getRed(1));
    assertEquals("Failed to return the green color component", 2, model.getGreen(0));
    assertEquals("Failed to return the green color component", 20, model.getGreen(1));
    assertEquals("Failed to return the blue color component", 3, model.getBlue(0));
    assertEquals("Failed to return the blue color component", 30, model.getBlue(1));
    assertEquals("Failed to return the alpha color component", 4, model.getAlpha(0));
    assertEquals("Failed to return the alpha color component", 40, model.getAlpha(1));
  }
  public void testCreateInterleaved() {
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] bandOffsets = new int[] {1, 2, 3};
    int dataType = DataBuffer.TYPE_BYTE;

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createInterleaved(colorSpace, bandOffsets, dataType, false, false);
    ColorModel colorModel = type.getColorModel();
    PixelInterleavedSampleModel sampleModel = (PixelInterleavedSampleModel) type.getSampleModel();

    // validate the colorModel
    assertEquals(
        "Failed to create with the correct colorspace type",
        ColorSpace.TYPE_RGB,
        colorModel.getColorSpace().getType());
    assertEquals(
        "Failed to create with the correct transparency",
        Transparency.OPAQUE,
        colorModel.getTransparency());
    assertEquals(
        "Failed to create with the correct transfer type",
        DataBuffer.TYPE_BYTE,
        colorModel.getTransferType());

    // validate the sampleModel
    assertTrue(
        "The sampleModel and colorModel are not compatible",
        colorModel.isCompatibleSampleModel(sampleModel));
    assertArrayEquals(
        "Failed to create with the correct bandOffsets", bandOffsets, sampleModel.getBandOffsets());
    assertEquals("Failed to create with the correct pixel stride", 3, sampleModel.getPixelStride());
  }
 /**
  * Creates a SurfaceData object representing an off-screen buffer (either a plain surface or
  * Texture).
  */
 public static D3DSurfaceData createData(
     D3DGraphicsConfig gc, int width, int height, ColorModel cm, Image image, int type) {
   if (type == RT_TEXTURE) {
     boolean isOpaque = cm.getTransparency() == Transparency.OPAQUE;
     int cap = isOpaque ? CAPS_RT_TEXTURE_OPAQUE : CAPS_RT_TEXTURE_ALPHA;
     if (!gc.getD3DDevice().isCapPresent(cap)) {
       type = RT_PLAIN;
     }
   }
   D3DSurfaceData ret = null;
   try {
     ret =
         new D3DSurfaceData(
             null, gc, width, height, image, cm, 0, SWAP_DISCARD, VSYNC_DEFAULT, type);
   } catch (InvalidPipeException ipe) {
     // try again - we might have ran out of vram, and rt textures
     // could take up more than a plain surface, so it might succeed
     if (type == RT_TEXTURE) {
       // If a RT_TEXTURE was requested do not attempt to create a
       // plain surface. (note that RT_TEXTURE can only be requested
       // from a VI so the cast is safe)
       if (((SunVolatileImage) image).getForcedAccelSurfaceType() != RT_TEXTURE) {
         type = RT_PLAIN;
         ret =
             new D3DSurfaceData(
                 null, gc, width, height, image, cm, 0, SWAP_DISCARD, VSYNC_DEFAULT, type);
       }
     }
   }
   return ret;
 }
  /**
   * Tests capability to write GIF image.
   *
   * @throws IOException If an error occured while writting the image.
   */
  @Test
  public void testGIFImageWrite() throws IOException {
    assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus());
    // Get the image of the world with transparency.
    ImageWorker worker = new ImageWorker(worldDEMImage);
    show(worker, "Input GIF");
    RenderedImage image = worker.getRenderedImage();
    ColorModel cm = image.getColorModel();
    assertTrue("wrong color model", cm instanceof IndexColorModel);
    assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency());
    // Writes it out as GIF on a file using index color model with
    final File outFile = TestData.temp(this, "temp.gif");
    worker.writeGIF(outFile, "LZW", 0.75f);

    // Read it back
    final ImageWorker readWorker = new ImageWorker(ImageIO.read(outFile));
    show(readWorker, "GIF to file");
    image = readWorker.getRenderedImage();
    cm = image.getColorModel();
    assertTrue("wrong color model", cm instanceof IndexColorModel);
    assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency());

    // Write on an output streams.
    final OutputStream os = new FileOutputStream(outFile);
    worker = new ImageWorker(worldImage);
    worker.forceIndexColorModelForGIF(true);
    worker.writeGIF(os, "LZW", 0.75f);

    // Read it back.
    readWorker.setImage(ImageIO.read(outFile));
    show(readWorker, "GIF to output stream");
    image = readWorker.getRenderedImage();
    cm = image.getColorModel();
    assertTrue("wrong color model", cm instanceof IndexColorModel);
    assertEquals("wrong transparency model", Transparency.BITMASK, cm.getTransparency());
    assertEquals(
        "wrong transparent color index", 255, ((IndexColorModel) cm).getTransparentPixel());
    outFile.delete();
  }
 public ByteFilter(
     ByteInterleavedRaster srcRas, ColorModel cm, AffineTransform xform, int maxw) {
   super(
       (cm.getTransparency() == Transparency.OPAQUE ? xrgbmodel : argbmodel),
       xform,
       srcRas.getWidth(),
       srcRas.getHeight(),
       maxw);
   this.inPalette = new int[256];
   ((IndexColorModel) cm).getRGBs(this.inPalette);
   this.srcRas = srcRas;
   this.inData = srcRas.getDataStorage();
   this.inSpan = srcRas.getScanlineStride();
   this.inOff = srcRas.getDataOffset(0);
 }
  /** Tests the conversion between RGB and indexed color model. */
  @Test
  public void testRGB2Palette() {
    assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus());
    final ImageWorker worker = new ImageWorker(worldImage);
    show(worker, "Input file");
    worker.forceIndexColorModelForGIF(true);

    // Convert to to index color bitmask
    ColorModel cm = worker.getRenderedImage().getColorModel();
    assertTrue("wrong color model", cm instanceof IndexColorModel);
    assertEquals("wrong transparency model", Transparency.BITMASK, cm.getTransparency());
    assertEquals("wrong transparency index", 255, ((IndexColorModel) cm).getTransparentPixel());
    show(worker, "Paletted bitmask");

    // Go back to rgb.
    worker.forceComponentColorModel();
    cm = worker.getRenderedImage().getColorModel();
    assertTrue("wrong color model", cm instanceof ComponentColorModel);
    assertEquals("wrong bands number", 4, cm.getNumComponents());

    show(worker, "RGB translucent");
    assertEquals("wrong transparency model", Transparency.TRANSLUCENT, cm.getTransparency());
    show(worker, "RGB translucent");
  }
  @Test
  public void test16BitGIF() throws Exception {
    // the resource has been compressed since the palette is way larger than the image itself,
    // and the palette does not get compressed
    InputStream gzippedStream =
        ImageWorkerTest.class.getResource("test-data/sf-sfdem.tif.gz").openStream();
    GZIPInputStream is = new GZIPInputStream(gzippedStream);
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(is);
      ImageReader reader = new TIFFImageReaderSpi().createReaderInstance(iis);
      reader.setInput(iis);
      BufferedImage bi = reader.read(0);
      if (TestData.isInteractiveTest()) {
        ImageIOUtilities.visualize(bi, "before");
      }
      reader.dispose();
      iis.close();
      IndexColorModel icm = (IndexColorModel) bi.getColorModel();
      assertEquals(65536, icm.getMapSize());

      final File outFile = TestData.temp(this, "temp.gif");
      ImageWorker worker = new ImageWorker(bi);
      worker.writeGIF(outFile, "LZW", 0.75f);

      // Read it back.
      bi = ImageIO.read(outFile);
      if (TestData.isInteractiveTest()) {
        ImageIOUtilities.visualize(bi, "after");
      }
      ColorModel cm = bi.getColorModel();
      assertTrue("wrong color model", cm instanceof IndexColorModel);
      assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency());
      final IndexColorModel indexColorModel = (IndexColorModel) cm;
      assertEquals("wrong transparent color index", -1, indexColorModel.getTransparentPixel());
      assertEquals("wrong component size", 8, indexColorModel.getComponentSize(0));
      outFile.delete();
    } finally {
      is.close();
    }
  }
  public void setColorModel(ColorModel model) {
    if (src != null) {
      src.checkSecurity(null, false);
    }
    srcModel = model;

    // Check to see if model is INT_RGB
    if (model instanceof IndexColorModel) {
      if (model.getTransparency() == model.TRANSLUCENT) {
        // REMIND:
        // Probably need to composite anyway so force ARGB
        cmodel = ColorModel.getRGBdefault();
        srcLUT = null;
      } else {
        IndexColorModel icm = (IndexColorModel) model;
        numSrcLUT = icm.getMapSize();
        srcLUT = new int[Math.max(numSrcLUT, 256)];
        icm.getRGBs(srcLUT);
        srcLUTtransIndex = icm.getTransparentPixel();
        cmodel = model;
      }
    } else {
      if (cmodel == null) {
        cmodel = model;
        srcLUT = null;
      } else if (model instanceof DirectColorModel) {
        // If it is INT_RGB or INT_ARGB, use the model
        DirectColorModel dcm = (DirectColorModel) model;
        if ((dcm.getRedMask() == 0xff0000)
            && (dcm.getGreenMask() == 0xff00)
            && (dcm.getBlueMask() == 0x00ff)) {
          cmodel = model;
          srcLUT = null;
        }
      }
    }

    isSameCM = (cmodel == model);
  }
  public static void main(String args[]) {
    ColorModel cm =
        new ColorModel(32) {
          public int getAlpha(int pixel) {
            return 255;
          }

          public int getBlue(int pixel) {
            return 255;
          }

          public int getGreen(int pixel) {
            return 255;
          }

          public int getRed(int pixel) {
            return 255;
          }
        };

    cm.hasAlpha();
    cm.isAlphaPremultiplied();
    cm.getTransferType();
    cm.getPixelSize();
    cm.getComponentSize();
    cm.getComponentSize();
    cm.getTransparency();
    cm.getNumComponents();
    cm.getNumColorComponents();
    cm.getRed(20);
    cm.getGreen(20);
    cm.getBlue(20);
    cm.getAlpha(20);
    cm.getRGB(20);
    cm.isAlphaPremultiplied();
    cm.isAlphaPremultiplied();

    cm = ColorModel.getRGBdefault();
  }
  protected 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 = ArrayUtil.toArray(bytesList.toArray(new Byte[bytesList.size()]));

      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 = ArrayUtil.toArray(bytesList.toArray(new Byte[bytesList.size()]));

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

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

    return renderedImage;
  }
  public void setPixels(
      int x, int y, int w, int h, ColorModel model, int pix[], int off, int scansize) {
    int lineOff = off;
    int poff;

    if (src != null) {
      src.checkSecurity(null, false);
    }

    // REMIND: What if the model doesn't fit in default color model?
    synchronized (this) {
      if (bimage == null) {
        if (cmodel == null) {
          cmodel = model;
        }
        createBufferedImage();
      }

      int[] storage = new int[w];
      int yoff;
      int pixel;

      if (cmodel instanceof IndexColorModel) {
        // REMIND: Right now we don't support writing back into ICM
        // images.
        convertToRGB();
      }

      if ((model == cmodel) && (biRaster instanceof IntegerComponentRaster)) {
        IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;

        if (off == 0 && scansize == w) {
          iraster.setDataElements(x, y, w, h, pix);
        } else {
          // Need to pack the data
          for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
            System.arraycopy(pix, lineOff, storage, 0, w);
            iraster.setDataElements(x, yoff, w, 1, storage);
          }
        }
      } else {
        if (model.getTransparency() != model.OPAQUE && cmodel.getTransparency() == cmodel.OPAQUE) {
          convertToRGB();
        }

        if (isDefaultBI) {
          IntegerComponentRaster iraster = (IntegerComponentRaster) biRaster;
          int[] data = iraster.getDataStorage();
          if (cmodel.equals(model)) {
            int sstride = iraster.getScanlineStride();
            int doff = y * sstride + x;
            for (yoff = 0; yoff < h; yoff++, lineOff += scansize) {
              System.arraycopy(pix, lineOff, data, doff, w);
              doff += sstride;
            }
            // Note: manual modification of pixels, mark the
            // raster as changed
            iraster.markDirty();
          } else {
            for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
              poff = lineOff;
              for (int i = 0; i < w; i++) {
                storage[i] = model.getRGB(pix[poff++]);
              }
              iraster.setDataElements(x, yoff, w, 1, storage);
            }
          }

          availinfo |= ImageObserver.SOMEBITS;
        } else {
          Object tmp = null;

          for (yoff = y; yoff < y + h; yoff++, lineOff += scansize) {
            poff = lineOff;
            for (int xoff = x; xoff < x + w; xoff++) {
              pixel = model.getRGB(pix[poff++]);
              tmp = cmodel.getDataElements(pixel, tmp);
              biRaster.setDataElements(xoff, yoff, tmp);
            }
          }
          availinfo |= ImageObserver.SOMEBITS;
        }
      }
    }

    // Can't do this here since we might need to transform/clip
    // the region
    if (((availinfo & ImageObserver.FRAMEBITS) == 0)) {
      newInfo(image, ImageObserver.SOMEBITS, x, y, w, h);
    }
  }
Exemple #16
0
  /*
   * Return the color model to be used with this BufferedImage and
   * transform.
   */
  private ColorModel getTransformColorModel(
      SunGraphics2D sg, BufferedImage bImg, AffineTransform tx) {
    ColorModel cm = bImg.getColorModel();
    ColorModel dstCM = cm;

    if (tx.isIdentity()) {
      return dstCM;
    }
    int type = tx.getType();
    boolean needTrans = ((type & (tx.TYPE_MASK_ROTATION | tx.TYPE_GENERAL_TRANSFORM)) != 0);
    if (!needTrans && type != tx.TYPE_TRANSLATION && type != tx.TYPE_IDENTITY) {
      double[] mtx = new double[4];
      tx.getMatrix(mtx);
      // Check out the matrix.  A non-integral scale will force ARGB
      // since the edge conditions cannot be guaranteed.
      needTrans = (mtx[0] != (int) mtx[0] || mtx[3] != (int) mtx[3]);
    }

    if (sg.renderHint != SunHints.INTVAL_RENDER_QUALITY) {
      if (cm instanceof IndexColorModel) {
        Raster raster = bImg.getRaster();
        IndexColorModel icm = (IndexColorModel) cm;
        // Just need to make sure that we have a transparent pixel
        if (needTrans && cm.getTransparency() == cm.OPAQUE) {
          // Fix 4221407
          if (raster instanceof sun.awt.image.BytePackedRaster) {
            dstCM = ColorModel.getRGBdefault();
          } else {
            double[] matrix = new double[6];
            tx.getMatrix(matrix);
            if (matrix[1] == 0. && matrix[2] == 0. && matrix[4] == 0. && matrix[5] == 0.) {
              // Only scaling so do not need to create
            } else {
              int mapSize = icm.getMapSize();
              if (mapSize < 256) {
                int[] cmap = new int[mapSize + 1];
                icm.getRGBs(cmap);
                cmap[mapSize] = 0x0000;
                dstCM =
                    new IndexColorModel(
                        icm.getPixelSize(),
                        mapSize + 1,
                        cmap,
                        0,
                        true,
                        mapSize,
                        DataBuffer.TYPE_BYTE);
              } else {
                dstCM = ColorModel.getRGBdefault();
              }
            } /* if (matrix[0] < 1.f ...) */
          } /* raster instanceof sun.awt.image.BytePackedRaster */
        } /* if (cm.getTransparency() == cm.OPAQUE) */
      } /* if (cm instanceof IndexColorModel) */ else if (needTrans
          && cm.getTransparency() == cm.OPAQUE) {
        // Need a bitmask transparency
        // REMIND: for now, use full transparency since no loops
        // for bitmask
        dstCM = ColorModel.getRGBdefault();
      }
    } /* if (sg.renderHint == RENDER_QUALITY) */ else {

      if (cm instanceof IndexColorModel || (needTrans && cm.getTransparency() == cm.OPAQUE)) {
        // Need a bitmask transparency
        // REMIND: for now, use full transparency since no loops
        // for bitmask
        dstCM = ColorModel.getRGBdefault();
      }
    }

    return dstCM;
  }
  public Iterator getImageTypes(int imageIndex) throws IIOException {
    List l; // List of ImageTypeSpecifiers

    Integer imageIndexInteger = new Integer(imageIndex);
    if (imageTypeMap.containsKey(imageIndexInteger)) {
      // Return the cached ITS List.
      l = (List) imageTypeMap.get(imageIndexInteger);
    } else {
      // Create a new ITS List.
      l = new ArrayList(1);

      // Create the ITS and cache if for later use so that this method
      // always returns an Iterator containing the same ITS objects.
      seekToImage(imageIndex);
      ImageTypeSpecifier itsRaw =
          TIFFDecompressor.getRawImageTypeSpecifier(
              photometricInterpretation,
              compression,
              samplesPerPixel,
              bitsPerSample,
              sampleFormat,
              extraSamples,
              colorMap);

      // Check for an ICCProfile field.
      TIFFField iccProfileField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_ICC_PROFILE);

      // If an ICCProfile field is present change the ImageTypeSpecifier
      // to use it if the data layout is component type.
      if (iccProfileField != null && itsRaw.getColorModel() instanceof ComponentColorModel) {
        // Create a ColorSpace from the profile.
        byte[] iccProfileValue = iccProfileField.getAsBytes();
        ICC_Profile iccProfile = ICC_Profile.getInstance(iccProfileValue);
        ICC_ColorSpace iccColorSpace = new ICC_ColorSpace(iccProfile);

        // Get the raw sample and color information.
        ColorModel cmRaw = itsRaw.getColorModel();
        ColorSpace csRaw = cmRaw.getColorSpace();
        SampleModel smRaw = itsRaw.getSampleModel();

        // Get the number of samples per pixel and the number
        // of color components.
        int numBands = smRaw.getNumBands();
        int numComponents = iccColorSpace.getNumComponents();

        // Replace the ColorModel with the ICC ColorModel if the
        // numbers of samples and color components are amenable.
        if (numBands == numComponents || numBands == numComponents + 1) {
          // Set alpha flags.
          boolean hasAlpha = numComponents != numBands;
          boolean isAlphaPre = hasAlpha && cmRaw.isAlphaPremultiplied();

          // Create a ColorModel of the same class and with
          // the same transfer type.
          ColorModel iccColorModel =
              new ComponentColorModel(
                  iccColorSpace,
                  cmRaw.getComponentSize(),
                  hasAlpha,
                  isAlphaPre,
                  cmRaw.getTransparency(),
                  cmRaw.getTransferType());

          // Prepend the ICC profile-based ITS to the List. The
          // ColorModel and SampleModel are guaranteed to be
          // compatible as the old and new ColorModels are both
          // ComponentColorModels with the same transfer type
          // and the same number of components.
          l.add(new ImageTypeSpecifier(iccColorModel, smRaw));

          // Append the raw ITS to the List if and only if its
          // ColorSpace has the same type and number of components
          // as the ICC ColorSpace.
          if (csRaw.getType() == iccColorSpace.getType()
              && csRaw.getNumComponents() == iccColorSpace.getNumComponents()) {
            l.add(itsRaw);
          }
        } else { // ICCProfile not compatible with SampleModel.
          // Append the raw ITS to the List.
          l.add(itsRaw);
        }
      } else { // No ICCProfile field or raw ColorModel not component.
        // Append the raw ITS to the List.
        l.add(itsRaw);
      }

      // Cache the ITS List.
      imageTypeMap.put(imageIndexInteger, l);
    }

    return l.iterator();
  }