Package java.awt.image

Examples of java.awt.image.IndexColorModel


            return new DirectColorModel(bits, rmask, gmask, bmask, amask);

        } else if (ImageUtil.isBinary(sm)) {
            byte[] comp = new byte[] { (byte)0x00, (byte)0xFF };

            return new IndexColorModel(1, 2, comp, comp, comp);

        } else // unable to create an suitable ColorModel
            return null;
        }
    }
View Full Code Here


            }
            if (sampleModel.getNumBands() != 1) {
                throw new RuntimeException();
            }

            IndexColorModel icm = (IndexColorModel)colorModel;
            int size = icm.getMapSize();
           
            redPalette = new byte[size];
            greenPalette = new byte[size];
            bluePalette = new byte[size];
            alphaPalette = new byte[size];
           
            icm.getReds(redPalette);
            icm.getGreens(greenPalette);
            icm.getBlues(bluePalette);
            icm.getAlphas(alphaPalette);

            this.bpp = 1;

            if (param == null) {
                param = createGrayParam(redPalette,
View Full Code Here

        if (colorModel instanceof IndexColorModel) {
            //
            // Need to expand the indexed data to components.
            // The convertToIntDiscrete method is used to perform this.
            //
            IndexColorModel icm = (IndexColorModel)colorModel;
            bi = icm.convertToIntDiscrete(bi.getRaster(), false);

            if(bi.getSampleModel().getNumBands() == 4) {
                //
                // Without copying data create a BufferedImage which has
                // only the RGB bands, not the alpha band.
View Full Code Here

        isInitialized = true;

        // Transform the colormap if the operation is accelerated.
        if(isColormapAccelerated) {
            // Cast the target ColorModel.
            IndexColorModel icm = (IndexColorModel)dstCM;

            // Get the size and allocate the array.
            int mapSize = icm.getMapSize();
            byte[][] colormap = new byte[3][mapSize];

            // Load the colormap.
            icm.getReds(colormap[0]);
            icm.getGreens(colormap[1]);
            icm.getBlues(colormap[2]);

            // Transform the colormap.
            transformColormap(colormap);

            // Clamp the colormap if necessary. In the Sun implementation
            // of IndexColorModel, getComponentSize() always returns 8 so
            // no clamping will be performed.
            for(int b = 0; b < 3; b++) {
                int maxComponent = 0xFF >> (8 - icm.getComponentSize(b));
                if(maxComponent < 255) {
                    byte[] map = colormap[b];
                    for(int i = 0; i < mapSize; i++) {
                        if((map[i] & 0xFF) > maxComponent) {
                            map[i] = (byte)maxComponent;
                        }
                    }
                }
            }

            // Cache references to individual color component arrays.
            byte[] reds = colormap[0];
            byte[] greens = colormap[1];
            byte[] blues = colormap[2];

            // Create the RGB array.
            int[] rgb = new int[mapSize];

            // Copy the colormap into the RGB array.
            if(icm.hasAlpha()) {
                byte[] alphas = new byte[mapSize];
                icm.getAlphas(alphas);
                for(int i = 0; i < mapSize; i++) {
                    rgb[i] =
                        ((alphas[i] & 0xFF) << 24) |
                        ((reds[i] & 0xFF)   << 16) |
                        ((greens[i] & 0xFF) <<  8) |
                        (blues[i] & 0xFF);
                }
            } else {
                for(int i = 0; i < mapSize; i++) {
                    rgb[i] =
                        ((reds[i] & 0xFF)   << 16) |
                        ((greens[i] & 0xFF) <<  8) |
                        (blues[i] & 0xFF);
                }
            }

            // Create the new ColorModel.
            colorModel= new IndexColorModel(icm.getPixelSize(), mapSize,
                                            rgb, 0, icm.hasAlpha(),
                                            icm.getTransparentPixel(),
                                            sampleModel.getTransferType());
        }
    }
View Full Code Here

        }

        // Check whether the image is white-is-zero.
        boolean isWhiteZero = false;
        if(im.getColorModel() instanceof IndexColorModel) {
            IndexColorModel icm = (IndexColorModel)im.getColorModel();
            isWhiteZero =
                (icm.getRed(0) + icm.getGreen(0) + icm.getBlue(0)) >
                (icm.getRed(1) + icm.getGreen(1) + icm.getBlue(1));
        }

        // Get the line stride, bytes per row, and data array.
        int lineStride =
            ((MultiPixelPackedSampleModel)sm).getScanlineStride();
View Full Code Here

        if (!transparentColorFlag) {
            if (ImageCodec.isIndicesForGrayscale(r, g, b))
                colorModel = ImageCodec.createComponentColorModel(sampleModel);
            else
                colorModel = new IndexColorModel(bits, r.length, r, g, b);
        } else {
            colorModel =
    new IndexColorModel(bits, r.length, r, g, b, transparentColorIndex);
        }
    }
View Full Code Here

  // Default is using 24 bits per pixel.
  int bitsPerPixel = 24;
  boolean isPalette = false;
  int paletteEntries = 0;
  IndexColorModel icm = null;
  SampleModel sm = im.getSampleModel();
  int numBands = sm.getNumBands();

  ColorModel cm = im.getColorModel();

  if (numBands != 1 && numBands != 3) {
      throw new
    IllegalArgumentException(JaiI18N.getString("BMPImageEncoder1"));
  }
 
  int sampleSize[] = sm.getSampleSize();
  if (sampleSize[0] > 8) {
      throw new RuntimeException(JaiI18N.getString("BMPImageEncoder2"));
  }

  for (int i=1; i<sampleSize.length; i++) {
      if (sampleSize[i] != sampleSize[0]) {
    throw
        new RuntimeException(JaiI18N.getString("BMPImageEncoder3"));
      }
  }

  // Float and Double data cannot be written in a BMP format.
  int dataType = sm.getTransferType();
        if (dataType != DataBuffer.TYPE_BYTE &&
            !CodecUtils.isPackedByteImage(im)) {
            throw new RuntimeException(JaiI18N.getString("BMPImageEncoder0"));
        }

  // Number of bytes that a scanline for the image written out will have.
  int destScanlineBytes = w * numBands;
  int compression = 0;


  byte r[] = null, g[] = null, b[] = null, a[] = null;

  if (cm instanceof IndexColorModel) {

      isPalette = true;
      icm = (IndexColorModel)cm;
      paletteEntries = icm.getMapSize();

      if (paletteEntries <= 2) {

    bitsPerPixel = 1;
    destScanlineBytes = (int)Math.ceil((double)w/8.0);

      } else if (paletteEntries <= 16) {

    bitsPerPixel = 4;
    destScanlineBytes = (int)Math.ceil((double)w/2.0);

      } else if (paletteEntries <= 256) {

    bitsPerPixel = 8;

      } else {

    // Cannot be written as a Palette image. So write out as
    // 24 bit image.
    bitsPerPixel = 24;
    isPalette = false;
    paletteEntries = 0;
    destScanlineBytes = w * 3;
      }

      if (isPalette == true) {

    r = new byte[paletteEntries];
    g = new byte[paletteEntries];
    b = new byte[paletteEntries];
    a = new byte[paletteEntries];
   
    icm.getAlphas(a);
    icm.getReds(r);
    icm.getGreens(g);
    icm.getBlues(b);
      }

  } else {
     
      // Grey scale images
View Full Code Here

            colorModel instanceof IndexColorModel &&
            dataType != DataBuffer.TYPE_BYTE) {
            // Don't support (unsigned) short palette-color images.
      throw new RuntimeException(JaiI18N.getString("TIFFImageEncoder6"));
        }
  IndexColorModel icm = null;
  int sizeOfColormap = 0
  int colormap[] = null;

        // Set image type.
  int imageType = TIFF_UNSUPPORTED;
        int numExtraSamples = 0;
        int extraSampleType = EXTRA_SAMPLE_UNSPECIFIED;
        if(colorModel instanceof IndexColorModel) { // Bilevel or palette
            icm = (IndexColorModel)colorModel;
            int mapSize = icm.getMapSize();

            if(sampleSize[0] == 1 && numBands == 1) { // Bilevel image

    if (mapSize != 2) {
        throw new IllegalArgumentException(
          JaiI18N.getString("TIFFImageEncoder7"));
    }

    byte r[] = new byte[mapSize];
    icm.getReds(r);
    byte g[] = new byte[mapSize];
    icm.getGreens(g);
    byte b[] = new byte[mapSize];
    icm.getBlues(b);

    if ((r[0] & 0xff) == 0 &&
        (r[1] & 0xff) == 255 &&
        (g[0] & 0xff) == 0 &&
        (g[1] & 0xff) == 255 &&
        (b[0] & 0xff) == 0 &&
        (b[1] & 0xff) == 255) {

        imageType = TIFF_BILEVEL_BLACK_IS_ZERO;

    } else if ((r[0] & 0xff) == 255 &&
         (r[1] & 0xff) == 0 &&
         (g[0] & 0xff) == 255 &&
         (g[1] & 0xff) == 0 &&
         (b[0] & 0xff) == 255 &&
         (b[1] & 0xff) == 0) {
       
        imageType = TIFF_BILEVEL_WHITE_IS_ZERO;

    } else {
        imageType = TIFF_PALETTE;
    }

      } else if(numBands == 1) { // Non-bilevel image.
    // Palette color image.
    imageType = TIFF_PALETTE;
      }
  } else if(colorModel == null) {

            if(sampleSize[0] == 1 && numBands == 1) { // bilevel
                imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
            } else { // generic image
                imageType = TIFF_GENERIC;
                if(numBands > 1) {
                    numExtraSamples = numBands - 1;
                }
            }

        } else { // colorModel is non-null but not an IndexColorModel
            ColorSpace colorSpace = colorModel.getColorSpace();

            switch(colorSpace.getType()) {
            case ColorSpace.TYPE_CMYK:
                imageType = TIFF_CMYK;
                break;
            case ColorSpace.TYPE_GRAY:
                imageType = TIFF_GRAY;
                break;
            case ColorSpace.TYPE_Lab:
                imageType = TIFF_CIELAB;
                break;
            case ColorSpace.TYPE_RGB:
                if(compression == COMP_JPEG_TTN2 &&
                   encodeParam.getJPEGCompressRGBToYCbCr()) {
                    imageType = TIFF_YCBCR;
                } else {
                    imageType = TIFF_RGB;
                }
                break;
            case ColorSpace.TYPE_YCbCr:
                imageType = TIFF_YCBCR;
                break;
            default:
                imageType = TIFF_GENERIC; // generic
                break;
            }

            if(imageType == TIFF_GENERIC) {
                numExtraSamples = numBands - 1;
            } else if(numBands > 1) {
                numExtraSamples = numBands - colorSpace.getNumComponents();
            }

            if(numExtraSamples == 1 && colorModel.hasAlpha()) {
                extraSampleType = colorModel.isAlphaPremultiplied() ?
                    EXTRA_SAMPLE_ASSOCIATED_ALPHA :
                    EXTRA_SAMPLE_UNASSOCIATED_ALPHA;
            }
        }

        if(imageType == TIFF_UNSUPPORTED) {
            throw new RuntimeException(JaiI18N.getString("TIFFImageEncoder8"));
        }

        // Check JPEG compatibility.
        if(compression == COMP_JPEG_TTN2) {
            if(imageType == TIFF_PALETTE) {
                throw new RuntimeException(JaiI18N.getString("TIFFImageEncoder11"));
            } else if(!(sampleSize[0] == 8 &&
                        (imageType == TIFF_GRAY ||
                         imageType == TIFF_RGB ||
                         imageType == TIFF_YCBCR))) {
                throw new RuntimeException(JaiI18N.getString("TIFFImageEncoder9"));
            }
        }

        // Check bilevel encoding compatibility.
        if((imageType != TIFF_BILEVEL_WHITE_IS_ZERO &&
            imageType != TIFF_BILEVEL_BLACK_IS_ZERO) &&
           (compression == COMP_GROUP3_1D ||
            compression == COMP_GROUP3_2D ||
            compression == COMP_GROUP4)) {
            throw new RuntimeException(JaiI18N.getString("TIFFImageEncoder12"));
        }
 
  int photometricInterpretation = -1;
  switch (imageType) {

  case TIFF_BILEVEL_WHITE_IS_ZERO:
      photometricInterpretation = 0;
      break;

  case TIFF_BILEVEL_BLACK_IS_ZERO:
      photometricInterpretation = 1;
      break;

  case TIFF_GRAY:
        case TIFF_GENERIC:
      // Since the CS_GRAY colorspace is always of type black_is_zero
      photometricInterpretation = 1;
      break;

  case TIFF_PALETTE:
      photometricInterpretation = 3;

      icm = (IndexColorModel)colorModel;
      sizeOfColormap = icm.getMapSize();

      byte r[] = new byte[sizeOfColormap];
      icm.getReds(r);
      byte g[] = new byte[sizeOfColormap];
      icm.getGreens(g);
      byte b[] = new byte[sizeOfColormap];
      icm.getBlues(b);

      int redIndex = 0, greenIndex = sizeOfColormap;
      int blueIndex = 2 * sizeOfColormap;
      colormap = new int[sizeOfColormap * 3];
      for (int i=0; i<sizeOfColormap; i++) {
View Full Code Here

                                                tileHeight,
                                                sampleSize);
            if(imageType == TYPE_BILEVEL) {
                byte[] map = new byte[] {(byte)(isWhiteZero ? 255 : 0),
                                         (byte)(isWhiteZero ? 0 : 255)};
    colorModel = new IndexColorModel(1, 2, map, map, map);
            } else {
                colorModel =
                    ImageCodec.createGrayIndexColorModel(sampleModel,
                                                         !isWhiteZero);
            }
            break;

        case TYPE_GRAY:
        case TYPE_GRAY_ALPHA:
        case TYPE_RGB:
        case TYPE_RGB_ALPHA:
  case TYPE_CMYK:
            // Create a pixel interleaved SampleModel with decreasing
            // band offsets.
            int[] RGBOffsets = new int[numBands];
            if(compression == COMP_JPEG_TTN2) {
                for (int i=0; i<numBands; i++) {
                    RGBOffsets[i] = numBands - 1 - i;
                }
            } else {
                for (int i=0; i<numBands; i++) {
                    RGBOffsets[i] = i;
                }
            }
            sampleModel = createPixelInterleavedSampleModel(dataType,
                                                            tileWidth,
                                                            tileHeight,
                                                            numBands,
                                                            numBands*tileWidth,
                                                            RGBOffsets);

            if(imageType == TYPE_GRAY || imageType == TYPE_RGB) {
                colorModel =
                    ImageCodec.createComponentColorModel(sampleModel);
            } else if (imageType == TYPE_CMYK) {
    colorModel = ImageCodec.createComponentColorModel(sampleModel,
                  SimpleCMYKColorSpace.getInstance());
      } else { // hasAlpha
                // Transparency.OPAQUE signifies image data that is
                // completely opaque, meaning that all pixels have an alpha
                // value of 1.0. So the extra band gets ignored, which is
                // what we want.
                int transparency = Transparency.OPAQUE;
                if(extraSamples == 1 || extraSamples == 2) {
        // associated (premultiplied) alpha when == 1
        // unassociated alpha when ==2
        // Fix bug: 4699316
                    transparency = Transparency.TRANSLUCENT;
                }

                colorModel =
                    createAlphaComponentColorModel(dataType,
                                                   numBands,
                                                   extraSamples == 1,
               transparency);
            }
            break;

        case TYPE_GENERIC:
        case TYPE_YCBCR_SUB:
            // For this case we can't display the image, so we create a
            // SampleModel with increasing bandOffsets, and keep the
            // ColorModel as null, as there is no appropriate ColorModel.

            int[] bandOffsets = new int[numBands];
            for (int i=0; i<numBands; i++) {
                bandOffsets[i] = i;
            }

            sampleModel =
                createPixelInterleavedSampleModel(dataType,
                                                  tileWidth, tileHeight,
                                                  numBands, numBands * tileWidth,
                                                  bandOffsets);
            colorModel = null;
            break;

        case TYPE_PALETTE:
      // Get the colormap
      TIFFField cfield = getField(dir, TIFFImageDecoder.TIFF_COLORMAP,
          "Colormap");
      colormap = cfield.getAsChars();

      // Could be either 1 or 3 bands depending on whether we use
      // IndexColorModel or not.
      if (decodePaletteAsShorts) {
    numBands = 3;

    // If no SampleFormat tag was specified and if the
    // sampleSize is less than or equal to 8, then the
    // dataType was initially set to byte, but now we want to
    // expand the palette as shorts, so the dataType should
    // be ushort.
    if (dataType == DataBuffer.TYPE_BYTE) {
        dataType = DataBuffer.TYPE_USHORT;
    }

    // Data will have to be unpacked into a 3 band short image
    // as we do not have a IndexColorModel that can deal with
    // a colormodel whose entries are of short data type.
    sampleModel =
        RasterFactory.createPixelInterleavedSampleModel(dataType,
                    tileWidth,
                    tileHeight,
                    numBands);
    colorModel = ImageCodec.createComponentColorModel(sampleModel);

      } else {

    numBands = 1;

    if (sampleSize == 4) {
        // Pixel data will not be unpacked, will use MPPSM to store
        // packed data and IndexColorModel to do the unpacking.
        sampleModel =
      new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
              tileWidth,
              tileHeight,
              sampleSize);
    } else if (sampleSize == 8) {
        sampleModel =
      RasterFactory.createPixelInterleavedSampleModel(
                  DataBuffer.TYPE_BYTE,
                  tileWidth,
                  tileHeight,
                  numBands);
    } else if (sampleSize == 16) {

        // Here datatype has to be unsigned since we are storing
        // indices into the IndexColorModel palette. Ofcourse
        // the actual palette entries are allowed to be negative.
                    dataType = DataBuffer.TYPE_USHORT;
        sampleModel =
      RasterFactory.createPixelInterleavedSampleModel(
              DataBuffer.TYPE_USHORT,
              tileWidth,
              tileHeight,
              numBands);
    }

    int bandLength = colormap.length/3;
    byte r[] = new byte[bandLength];
    byte g[] = new byte[bandLength];
    byte b[] = new byte[bandLength];

    int gIndex = bandLength;
    int bIndex = bandLength * 2;

    if (dataType == DataBuffer.TYPE_SHORT) {

        for (int i=0; i<bandLength; i++) {
      r[i] = param.decodeSigned16BitsTo8Bits(
                  (short)colormap[i]);
      g[i] = param.decodeSigned16BitsTo8Bits(
                 (short)colormap[gIndex+i]);
      b[i] = param.decodeSigned16BitsTo8Bits(
                 (short)colormap[bIndex+i]);
        }

    } else {

        for (int i=0; i<bandLength; i++) {
      r[i] = param.decode16BitsTo8Bits(colormap[i] & 0xffff);
      g[i] = param.decode16BitsTo8Bits(colormap[gIndex+i] &
               0xffff);
      b[i] = param.decode16BitsTo8Bits(colormap[bIndex+i] &
               0xffff);
        }

    }

    colorModel = new IndexColorModel(sampleSize,
             bandLength, r, g, b);
      }
            break;

        default:
View Full Code Here

                }
            }
            return data;

        } else if (image.getColorModel() instanceof IndexColorModel) {
            IndexColorModel cmodel = (IndexColorModel)image.getColorModel();
            int size = cmodel.getMapSize();
            byte[] reds = new byte[size];
            byte[] greens = new byte[size];
            byte[] blues = new byte[size];
            cmodel.getReds(reds);
            cmodel.getGreens(greens);
            cmodel.getBlues(blues);
            RGB[] rgbs = new RGB[size];
            for (int ii = 0; ii < rgbs.length; ii++) {
                rgbs[ii] = new RGB(reds[ii] & 0xFF, greens[ii] & 0xFF, blues[ii] & 0xFF);
            }
            PaletteData palette = new PaletteData(rgbs);
            ImageData data = new ImageData(
                image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
            data.transparentPixel = cmodel.getTransparentPixel();
            WritableRaster raster = image.getRaster();
            int[] pixelArray = new int[1];
            for (int y = 0; y < data.height; y++) {
                for (int x = 0; x < data.width; x++) {
                    raster.getPixel(x, y, pixelArray);
                    data.setPixel(x, y, pixelArray[0]);
                }
            }
            return data;
        } else if (image.getColorModel() instanceof ComponentColorModel) {
            ComponentColorModel cmodel = (ComponentColorModel)image.getColorModel();
            PaletteData palette = new PaletteData(0x0000FF, 0x00FF00, 0xFF0000); // BGR
            ImageData data = new ImageData(image.getWidth(), image.getHeight(), 24, palette);
            if (cmodel.hasAlpha()) data.alphaData = new byte[image.getWidth() * image.getHeight()];
            WritableRaster raster = image.getRaster();
            int[] pixelArray = new int[4];
            for (int y = 0; y < data.height; y++) {
                for (int x = 0; x < data.width; x++) {
                    raster.getPixel(x, y, pixelArray);
View Full Code Here

TOP

Related Classes of java.awt.image.IndexColorModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.