Java Code Examples for java.awt.image.DataBuffer#TYPE_SHORT

The following examples show how to use java.awt.image.DataBuffer#TYPE_SHORT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColConvTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static String getDTName(int dType) {
    switch(dType) {
        case DataBuffer.TYPE_BYTE:
            return "TYPE_BYTE";
        case DataBuffer.TYPE_DOUBLE:
            return "TYPE_DOUBLE";
        case DataBuffer.TYPE_FLOAT:
            return "TYPE_FLOAT";
        case DataBuffer.TYPE_INT:
            return "TYPE_INT";
        case DataBuffer.TYPE_SHORT:
            return "TYPE_SHORT";
        case DataBuffer.TYPE_USHORT:
            return "TYPE_USHORT";
        case DataBuffer.TYPE_UNDEFINED:
            return "TYPE_UNDEFINED";
    }
    return "UNKNOWN";
}
 
Example 2
Source File: SignedColorModel.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public WritableRaster createCompatibleWritableRaster(final int w,
	final int h)
{
	if (pixelBits == 16) {
		final int[] bandOffsets = new int[nChannels];
		for (int i = 0; i < nChannels; i++)
			bandOffsets[i] = i;

		final SampleModel m = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w,
			h, nChannels, w * nChannels, bandOffsets);
		final DataBuffer db = new DataBufferShort(w * h, nChannels);
		return Raster.createWritableRaster(m, db, null);
	}
	return helper.createCompatibleWritableRaster(w, h);
}
 
Example 3
Source File: PixelConverter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int rgbToPixel(int rgb, ColorModel cm) {
    Object obj = cm.getDataElements(rgb, null);
    switch (cm.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        byte[] bytearr = (byte[]) obj;
        int pix = 0;

        switch(bytearr.length) {
        default: // bytearr.length >= 4
            pix = bytearr[3] << 24;
            // FALLSTHROUGH
        case 3:
            pix |= (bytearr[2] & 0xff) << 16;
            // FALLSTHROUGH
        case 2:
            pix |= (bytearr[1] & 0xff) << 8;
            // FALLSTHROUGH
        case 1:
            pix |= (bytearr[0] & 0xff);
        }

        return pix;
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        short[] shortarr = (short[]) obj;

        return (((shortarr.length > 1) ? shortarr[1] << 16 : 0) |
                shortarr[0] & 0xffff);
    case DataBuffer.TYPE_INT:
        return ((int[]) obj)[0];
    default:
        return rgb;
    }
}
 
Example 4
Source File: GeneralRenderer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d,
                                        SurfaceData sData)
{
    ColorModel dstCM = sData.getColorModel();

    Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);

    XORComposite comp = (XORComposite)sg2d.getComposite();
    int xorrgb = comp.getXorColor().getRGB();
    Object xorPixel = dstCM.getDataElements(xorrgb, null);

    switch (dstCM.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        return new XorPixelWriter.ByteData(srcPixel, xorPixel);
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        return new XorPixelWriter.ShortData(srcPixel, xorPixel);
    case DataBuffer.TYPE_INT:
        return new XorPixelWriter.IntData(srcPixel, xorPixel);
    case DataBuffer.TYPE_FLOAT:
        return new XorPixelWriter.FloatData(srcPixel, xorPixel);
    case DataBuffer.TYPE_DOUBLE:
        return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
    default:
        throw new InternalError("Unsupported XOR pixel type");
    }
}
 
Example 5
Source File: GeneralRenderer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d,
                                        SurfaceData sData)
{
    ColorModel dstCM = sData.getColorModel();

    Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);

    XORComposite comp = (XORComposite)sg2d.getComposite();
    int xorrgb = comp.getXorColor().getRGB();
    Object xorPixel = dstCM.getDataElements(xorrgb, null);

    switch (dstCM.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        return new XorPixelWriter.ByteData(srcPixel, xorPixel);
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        return new XorPixelWriter.ShortData(srcPixel, xorPixel);
    case DataBuffer.TYPE_INT:
        return new XorPixelWriter.IntData(srcPixel, xorPixel);
    case DataBuffer.TYPE_FLOAT:
        return new XorPixelWriter.FloatData(srcPixel, xorPixel);
    case DataBuffer.TYPE_DOUBLE:
        return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
    default:
        throw new InternalError("Unsupported XOR pixel type");
    }
}
 
Example 6
Source File: GeneralRenderer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d,
                                        SurfaceData sData)
{
    ColorModel dstCM = sData.getColorModel();

    Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);

    XORComposite comp = (XORComposite)sg2d.getComposite();
    int xorrgb = comp.getXorColor().getRGB();
    Object xorPixel = dstCM.getDataElements(xorrgb, null);

    switch (dstCM.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        return new XorPixelWriter.ByteData(srcPixel, xorPixel);
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        return new XorPixelWriter.ShortData(srcPixel, xorPixel);
    case DataBuffer.TYPE_INT:
        return new XorPixelWriter.IntData(srcPixel, xorPixel);
    case DataBuffer.TYPE_FLOAT:
        return new XorPixelWriter.FloatData(srcPixel, xorPixel);
    case DataBuffer.TYPE_DOUBLE:
        return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
    default:
        throw new InternalError("Unsupported XOR pixel type");
    }
}
 
Example 7
Source File: GeneralRenderer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d,
                                        SurfaceData sData)
{
    ColorModel dstCM = sData.getColorModel();

    Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);

    XORComposite comp = (XORComposite)sg2d.getComposite();
    int xorrgb = comp.getXorColor().getRGB();
    Object xorPixel = dstCM.getDataElements(xorrgb, null);

    switch (dstCM.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        return new XorPixelWriter.ByteData(srcPixel, xorPixel);
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        return new XorPixelWriter.ShortData(srcPixel, xorPixel);
    case DataBuffer.TYPE_INT:
        return new XorPixelWriter.IntData(srcPixel, xorPixel);
    case DataBuffer.TYPE_FLOAT:
        return new XorPixelWriter.FloatData(srcPixel, xorPixel);
    case DataBuffer.TYPE_DOUBLE:
        return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
    default:
        throw new InternalError("Unsupported XOR pixel type");
    }
}
 
Example 8
Source File: PixelConverter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("fallthrough")
public int rgbToPixel(int rgb, ColorModel cm) {
    Object obj = cm.getDataElements(rgb, null);
    switch (cm.getTransferType()) {
    case DataBuffer.TYPE_BYTE:
        byte[] bytearr = (byte[]) obj;
        int pix = 0;

        switch(bytearr.length) {
        default: // bytearr.length >= 4
            pix = bytearr[3] << 24;
            // FALLSTHROUGH
        case 3:
            pix |= (bytearr[2] & 0xff) << 16;
            // FALLSTHROUGH
        case 2:
            pix |= (bytearr[1] & 0xff) << 8;
            // FALLSTHROUGH
        case 1:
            pix |= (bytearr[0] & 0xff);
        }

        return pix;
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
        short[] shortarr = (short[]) obj;

        return (((shortarr.length > 1) ? shortarr[1] << 16 : 0) |
                shortarr[0] & 0xffff);
    case DataBuffer.TYPE_INT:
        return ((int[]) obj)[0];
    default:
        return rgb;
    }
}
 
Example 9
Source File: ImageTypeSpecifier.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public Grayscale(int bits,
                 int dataType,
                 boolean isSigned,
                 boolean hasAlpha,
                 boolean isAlphaPremultiplied)
{
    if (bits != 1 && bits != 2 && bits != 4 &&
        bits != 8 && bits != 16)
    {
        throw new IllegalArgumentException("Bad value for bits!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT)
    {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    if (bits > 8 && dataType == DataBuffer.TYPE_BYTE) {
        throw new IllegalArgumentException
            ("Too many bits for dataType!");
    }

    this.bits = bits;
    this.dataType = dataType;
    this.isSigned = isSigned;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits == 16 &&
         (dataType == DataBuffer.TYPE_SHORT ||
          dataType == DataBuffer.TYPE_USHORT))) {
        // Use component color model & sample model

        int numBands = hasAlpha ? 2 : 1;
        int transparency =
            hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;


        int[] nBits = new int[numBands];
        nBits[0] = bits;
        if (numBands == 2) {
            nBits[1] = bits;
        }
        this.colorModel =
            new ComponentColorModel(colorSpace,
                                    nBits,
                                    hasAlpha,
                                    isAlphaPremultiplied,
                                    transparency,
                                    dataType);

        int[] bandOffsets = new int[numBands];
        bandOffsets[0] = 0;
        if (numBands == 2) {
            bandOffsets[1] = 1;
        }

        int w = 1;
        int h = 1;
        this.sampleModel =
            new PixelInterleavedSampleModel(dataType,
                                            w, h,
                                            numBands, w*numBands,
                                            bandOffsets);
    } else {
        int numEntries = 1 << bits;
        byte[] arr = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            arr[i] = (byte)(i*255/(numEntries - 1));
        }
        this.colorModel =
            new IndexColorModel(bits, numEntries, arr, arr, arr);

        this.sampleModel =
            new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
    }
}
 
Example 10
Source File: ImageTypeSpecifier.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public Grayscale(int bits,
                 int dataType,
                 boolean isSigned,
                 boolean hasAlpha,
                 boolean isAlphaPremultiplied)
{
    if (bits != 1 && bits != 2 && bits != 4 &&
        bits != 8 && bits != 16)
    {
        throw new IllegalArgumentException("Bad value for bits!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT)
    {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    if (bits > 8 && dataType == DataBuffer.TYPE_BYTE) {
        throw new IllegalArgumentException
            ("Too many bits for dataType!");
    }

    this.bits = bits;
    this.dataType = dataType;
    this.isSigned = isSigned;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits == 16 &&
         (dataType == DataBuffer.TYPE_SHORT ||
          dataType == DataBuffer.TYPE_USHORT))) {
        // Use component color model & sample model

        int numBands = hasAlpha ? 2 : 1;
        int transparency =
            hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;


        int[] nBits = new int[numBands];
        nBits[0] = bits;
        if (numBands == 2) {
            nBits[1] = bits;
        }
        this.colorModel =
            new ComponentColorModel(colorSpace,
                                    nBits,
                                    hasAlpha,
                                    isAlphaPremultiplied,
                                    transparency,
                                    dataType);

        int[] bandOffsets = new int[numBands];
        bandOffsets[0] = 0;
        if (numBands == 2) {
            bandOffsets[1] = 1;
        }

        int w = 1;
        int h = 1;
        this.sampleModel =
            new PixelInterleavedSampleModel(dataType,
                                            w, h,
                                            numBands, w*numBands,
                                            bandOffsets);
    } else {
        int numEntries = 1 << bits;
        byte[] arr = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            arr[i] = (byte)(i*255/(numEntries - 1));
        }
        this.colorModel =
            new IndexColorModel(bits, numEntries, arr, arr, arr);

        this.sampleModel =
            new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
    }
}
 
Example 11
Source File: ImageTypeSpecifier.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Grayscale(int bits,
                 int dataType,
                 boolean isSigned,
                 boolean hasAlpha,
                 boolean isAlphaPremultiplied)
{
    if (bits != 1 && bits != 2 && bits != 4 &&
        bits != 8 && bits != 16)
    {
        throw new IllegalArgumentException("Bad value for bits!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT)
    {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    if (bits > 8 && dataType == DataBuffer.TYPE_BYTE) {
        throw new IllegalArgumentException
            ("Too many bits for dataType!");
    }

    this.bits = bits;
    this.dataType = dataType;
    this.isSigned = isSigned;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits == 16 &&
         (dataType == DataBuffer.TYPE_SHORT ||
          dataType == DataBuffer.TYPE_USHORT))) {
        // Use component color model & sample model

        int numBands = hasAlpha ? 2 : 1;
        int transparency =
            hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;


        int[] nBits = new int[numBands];
        nBits[0] = bits;
        if (numBands == 2) {
            nBits[1] = bits;
        }
        this.colorModel =
            new ComponentColorModel(colorSpace,
                                    nBits,
                                    hasAlpha,
                                    isAlphaPremultiplied,
                                    transparency,
                                    dataType);

        int[] bandOffsets = new int[numBands];
        bandOffsets[0] = 0;
        if (numBands == 2) {
            bandOffsets[1] = 1;
        }

        int w = 1;
        int h = 1;
        this.sampleModel =
            new PixelInterleavedSampleModel(dataType,
                                            w, h,
                                            numBands, w*numBands,
                                            bandOffsets);
    } else {
        int numEntries = 1 << bits;
        byte[] arr = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            arr[i] = (byte)(i*255/(numEntries - 1));
        }
        this.colorModel =
            new IndexColorModel(bits, numEntries, arr, arr, arr);

        this.sampleModel =
            new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
    }
}
 
Example 12
Source File: ImageTypeSpecifier.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Banded(ColorSpace colorSpace,
              int[] bankIndices,
              int[] bandOffsets,
              int dataType,
              boolean hasAlpha,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (bankIndices == null) {
        throw new IllegalArgumentException("bankIndices == null!");
    }
    if (bandOffsets == null) {
        throw new IllegalArgumentException("bandOffsets == null!");
    }
    if (bankIndices.length != bandOffsets.length) {
        throw new IllegalArgumentException
            ("bankIndices.length != bandOffsets.length!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT &&
        dataType != DataBuffer.TYPE_FLOAT &&
        dataType != DataBuffer.TYPE_DOUBLE) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    int numBands = colorSpace.getNumComponents() +
        (hasAlpha ? 1 : 0);
    if (bandOffsets.length != numBands) {
        throw new IllegalArgumentException
            ("bandOffsets.length is wrong!");
    }

    this.colorSpace = colorSpace;
    this.bankIndices = (int[])bankIndices.clone();
    this.bandOffsets = (int[])bandOffsets.clone();
    this.dataType = dataType;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    this.colorModel =
        ImageTypeSpecifier.createComponentCM(colorSpace,
                                             bankIndices.length,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);

    int w = 1;
    int h = 1;
    this.sampleModel = new BandedSampleModel(dataType,
                                             w, h,
                                             w,
                                             bankIndices,
                                             bandOffsets);
}
 
Example 13
Source File: ImageTypeSpecifier.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Interleaved(ColorSpace colorSpace,
                   int[] bandOffsets,
                   int dataType,
                   boolean hasAlpha,
                   boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (bandOffsets == null) {
        throw new IllegalArgumentException("bandOffsets == null!");
    }
    int numBands = colorSpace.getNumComponents() +
        (hasAlpha ? 1 : 0);
    if (bandOffsets.length != numBands) {
        throw new IllegalArgumentException
            ("bandOffsets.length is wrong!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT &&
        dataType != DataBuffer.TYPE_FLOAT &&
        dataType != DataBuffer.TYPE_DOUBLE) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    this.colorSpace = colorSpace;
    this.bandOffsets = (int[])bandOffsets.clone();
    this.dataType = dataType;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    this.colorModel =
        ImageTypeSpecifier.createComponentCM(colorSpace,
                                             bandOffsets.length,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);

    int minBandOffset = bandOffsets[0];
    int maxBandOffset = minBandOffset;
    for (int i = 0; i < bandOffsets.length; i++) {
        int offset = bandOffsets[i];
        minBandOffset = Math.min(offset, minBandOffset);
        maxBandOffset = Math.max(offset, maxBandOffset);
    }
    int pixelStride = maxBandOffset - minBandOffset + 1;

    int w = 1;
    int h = 1;
    this.sampleModel =
        new PixelInterleavedSampleModel(dataType,
                                        w, h,
                                        pixelStride,
                                        w*pixelStride,
                                        bandOffsets);
}
 
Example 14
Source File: ByteArrayUtils.java    From mrgeo with Apache License 2.0 4 votes vote down vote up
public static void swapBytes(byte[] bytes, int datatype, int offset)
{
  byte tmp;
  int i = offset;

  switch (datatype)
  {
  case DataBuffer.TYPE_BYTE:
    break;  // nothing to do
  case DataBuffer.TYPE_SHORT:
  case DataBuffer.TYPE_USHORT:
    // 2 byte value... 1, 2 becomes 2, 1  (swap byte 1 with 2)
    while (i + 1 < bytes.length)
    {
      // swap 0 & 1
      tmp = bytes[i];
      bytes[i] = bytes[i + 1];
      bytes[i + 1] = tmp;
      i += 2;
    }
    break;
  // 4 byte value... 1, 2, 3, 4 becomes 4, 3, 2, 1  (swap bytes 1 & 4, 2 & 3)
  case DataBuffer.TYPE_FLOAT:
  case DataBuffer.TYPE_INT:
    while (i + 3 < bytes.length)
    {
      // swap 0 & 3
      tmp = bytes[i];
      bytes[i] = bytes[i + 3];
      bytes[i + 3] = tmp;

      // swap 1 & 2
      tmp = bytes[i + 1];
      bytes[i + 1] = bytes[i + 2];
      bytes[i + 2] = tmp;
      i += 4;
    }
    break;
  case DataBuffer.TYPE_DOUBLE:
    // 8 byte value... 1, 2, 3, 4, 5, 6, 7 becomes 7, 6, 5, 4, 3, 2, 1
    // (swap bytes 1 & 8, 2 & 7, 3 & 6, 4 & 5)
    while (i + 7 < bytes.length)
    {
      // swap 0 & 7
      tmp = bytes[i];
      bytes[i] = bytes[i + 7];
      bytes[i + 7] = tmp;

      // swap 1 & 6
      tmp = bytes[i + 1];
      bytes[i + 1] = bytes[i + 6];
      bytes[i + 6] = tmp;

      // swap 2 & 5
      tmp = bytes[i + 2];
      bytes[i + 2] = bytes[i + 5];
      bytes[i + 5] = tmp;

      // swap 3 $ 4
      tmp = bytes[i + 3];
      bytes[i + 3] = bytes[i + 4];
      bytes[i + 4] = tmp;

      i += 8;
    }

    break;
  }
}
 
Example 15
Source File: ImageTypeSpecifier.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Banded(ColorSpace colorSpace,
              int[] bankIndices,
              int[] bandOffsets,
              int dataType,
              boolean hasAlpha,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (bankIndices == null) {
        throw new IllegalArgumentException("bankIndices == null!");
    }
    if (bandOffsets == null) {
        throw new IllegalArgumentException("bandOffsets == null!");
    }
    if (bankIndices.length != bandOffsets.length) {
        throw new IllegalArgumentException
            ("bankIndices.length != bandOffsets.length!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT &&
        dataType != DataBuffer.TYPE_FLOAT &&
        dataType != DataBuffer.TYPE_DOUBLE) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    int numBands = colorSpace.getNumComponents() +
        (hasAlpha ? 1 : 0);
    if (bandOffsets.length != numBands) {
        throw new IllegalArgumentException
            ("bandOffsets.length is wrong!");
    }

    this.colorSpace = colorSpace;
    this.bankIndices = (int[])bankIndices.clone();
    this.bandOffsets = (int[])bandOffsets.clone();
    this.dataType = dataType;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    this.colorModel =
        ImageTypeSpecifier.createComponentCM(colorSpace,
                                             bankIndices.length,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);

    int w = 1;
    int h = 1;
    this.sampleModel = new BandedSampleModel(dataType,
                                             w, h,
                                             w,
                                             bankIndices,
                                             bandOffsets);
}
 
Example 16
Source File: ImageTypeSpecifier.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public Indexed(byte[] redLUT,
               byte[] greenLUT,
               byte[] blueLUT,
               byte[] alphaLUT,
               int bits,
               int dataType) {
    if (redLUT == null || greenLUT == null || blueLUT == null) {
        throw new IllegalArgumentException("LUT is null!");
    }
    if (bits != 1 && bits != 2 && bits != 4 &&
        bits != 8 && bits != 16) {
        throw new IllegalArgumentException("Bad value for bits!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    if ((bits > 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits > 16 && dataType != DataBuffer.TYPE_INT)) {
        throw new IllegalArgumentException
            ("Too many bits for dataType!");
    }

    int len = 1 << bits;
    if (redLUT.length != len ||
        greenLUT.length != len ||
        blueLUT.length != len ||
        (alphaLUT != null && alphaLUT.length != len)) {
        throw new IllegalArgumentException("LUT has improper length!");
    }
    this.redLUT = (byte[])redLUT.clone();
    this.greenLUT = (byte[])greenLUT.clone();
    this.blueLUT = (byte[])blueLUT.clone();
    if (alphaLUT != null) {
        this.alphaLUT = (byte[])alphaLUT.clone();
    }
    this.bits = bits;
    this.dataType = dataType;

    if (alphaLUT == null) {
        this.colorModel = new IndexColorModel(bits,
                                              redLUT.length,
                                              redLUT,
                                              greenLUT,
                                              blueLUT);
    } else {
        this.colorModel = new IndexColorModel(bits,
                                              redLUT.length,
                                              redLUT,
                                              greenLUT,
                                              blueLUT,
                                              alphaLUT);
    }

    if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits == 16 &&
         (dataType == DataBuffer.TYPE_SHORT ||
          dataType == DataBuffer.TYPE_USHORT))) {
        int[] bandOffsets = { 0 };
        this.sampleModel =
            new PixelInterleavedSampleModel(dataType,
                                            1, 1, 1, 1,
                                            bandOffsets);
    } else {
        this.sampleModel =
            new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
    }
}
 
Example 17
Source File: ImageTypeSpecifier.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public Banded(ColorSpace colorSpace,
              int[] bankIndices,
              int[] bandOffsets,
              int dataType,
              boolean hasAlpha,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (bankIndices == null) {
        throw new IllegalArgumentException("bankIndices == null!");
    }
    if (bandOffsets == null) {
        throw new IllegalArgumentException("bandOffsets == null!");
    }
    if (bankIndices.length != bandOffsets.length) {
        throw new IllegalArgumentException
            ("bankIndices.length != bandOffsets.length!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT &&
        dataType != DataBuffer.TYPE_FLOAT &&
        dataType != DataBuffer.TYPE_DOUBLE) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    int numBands = colorSpace.getNumComponents() +
        (hasAlpha ? 1 : 0);
    if (bandOffsets.length != numBands) {
        throw new IllegalArgumentException
            ("bandOffsets.length is wrong!");
    }

    this.colorSpace = colorSpace;
    this.bankIndices = (int[])bankIndices.clone();
    this.bandOffsets = (int[])bandOffsets.clone();
    this.dataType = dataType;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    this.colorModel =
        ImageTypeSpecifier.createComponentCM(colorSpace,
                                             bankIndices.length,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);

    int w = 1;
    int h = 1;
    this.sampleModel = new BandedSampleModel(dataType,
                                             w, h,
                                             w,
                                             bankIndices,
                                             bandOffsets);
}
 
Example 18
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Gets the pixel type of the given image.
 *
 * @return One of the following types:
 *         <ul>
 *         <li>FormatReader.INT8</li>
 *         <li>FormatReader.UINT8</li>
 *         <li>FormatReader.INT16</li>
 *         <li>FormatReader.UINT16</li>
 *         <li>FormatReader.INT32</li>
 *         <li>FormatReader.UINT32</li>
 *         <li>FormatReader.FLOAT</li>
 *         <li>FormatReader.DOUBLE</li>
 *         <li>-1 (unknown type)</li>
 *         </ul>
 */
public static int getPixelType(final BufferedImage image) {
	final Raster raster = image.getRaster();
	if (raster == null) return -1;
	final DataBuffer buffer = raster.getDataBuffer();
	if (buffer == null) return -1;

	if (buffer instanceof SignedByteBuffer) {
		return FormatTools.INT8;
	}
	else if (buffer instanceof SignedShortBuffer) {
		return FormatTools.INT16;
	}
	else if (buffer instanceof UnsignedIntBuffer) {
		return FormatTools.UINT32;
	}

	final int type = buffer.getDataType();
	final int imageType = image.getType();
	switch (type) {
		case DataBuffer.TYPE_BYTE:
			return FormatTools.UINT8;
		case DataBuffer.TYPE_DOUBLE:
			return FormatTools.DOUBLE;
		case DataBuffer.TYPE_FLOAT:
			return FormatTools.FLOAT;
		case DataBuffer.TYPE_INT:
			if (imageType == BufferedImage.TYPE_INT_RGB ||
				imageType == BufferedImage.TYPE_INT_BGR ||
				imageType == BufferedImage.TYPE_INT_ARGB)
			{
				return FormatTools.UINT8;
			}
			if (buffer instanceof UnsignedIntBuffer) {
				return FormatTools.UINT32;
			}
			return FormatTools.INT32;
		case DataBuffer.TYPE_SHORT:
			return FormatTools.INT16;
		case DataBuffer.TYPE_USHORT:
			if (imageType == BufferedImage.TYPE_USHORT_555_RGB ||
				imageType == BufferedImage.TYPE_USHORT_565_RGB)
			{
				return FormatTools.UINT8;
			}
			return FormatTools.UINT16;
		default:
			return -1;
	}
}
 
Example 19
Source File: ImageTypeSpecifier.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Banded(ColorSpace colorSpace,
              int[] bankIndices,
              int[] bandOffsets,
              int dataType,
              boolean hasAlpha,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (bankIndices == null) {
        throw new IllegalArgumentException("bankIndices == null!");
    }
    if (bandOffsets == null) {
        throw new IllegalArgumentException("bandOffsets == null!");
    }
    if (bankIndices.length != bandOffsets.length) {
        throw new IllegalArgumentException
            ("bankIndices.length != bandOffsets.length!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT &&
        dataType != DataBuffer.TYPE_INT &&
        dataType != DataBuffer.TYPE_FLOAT &&
        dataType != DataBuffer.TYPE_DOUBLE) {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    int numBands = colorSpace.getNumComponents() +
        (hasAlpha ? 1 : 0);
    if (bandOffsets.length != numBands) {
        throw new IllegalArgumentException
            ("bandOffsets.length is wrong!");
    }

    this.colorSpace = colorSpace;
    this.bankIndices = bankIndices.clone();
    this.bandOffsets = bandOffsets.clone();
    this.dataType = dataType;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    this.colorModel =
        ImageTypeSpecifier.createComponentCM(colorSpace,
                                             bankIndices.length,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);

    int w = 1;
    int h = 1;
    this.sampleModel = new BandedSampleModel(dataType,
                                             w, h,
                                             w,
                                             bankIndices,
                                             bandOffsets);
}
 
Example 20
Source File: ImageTypeSpecifier.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Grayscale(int bits,
                 int dataType,
                 boolean isSigned,
                 boolean hasAlpha,
                 boolean isAlphaPremultiplied)
{
    if (bits != 1 && bits != 2 && bits != 4 &&
        bits != 8 && bits != 16)
    {
        throw new IllegalArgumentException("Bad value for bits!");
    }
    if (dataType != DataBuffer.TYPE_BYTE &&
        dataType != DataBuffer.TYPE_SHORT &&
        dataType != DataBuffer.TYPE_USHORT)
    {
        throw new IllegalArgumentException
            ("Bad value for dataType!");
    }
    if (bits > 8 && dataType == DataBuffer.TYPE_BYTE) {
        throw new IllegalArgumentException
            ("Too many bits for dataType!");
    }

    this.bits = bits;
    this.dataType = dataType;
    this.isSigned = isSigned;
    this.hasAlpha = hasAlpha;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) ||
        (bits == 16 &&
         (dataType == DataBuffer.TYPE_SHORT ||
          dataType == DataBuffer.TYPE_USHORT))) {
        // Use component color model & sample model

        int numBands = hasAlpha ? 2 : 1;
        int transparency =
            hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;


        int[] nBits = new int[numBands];
        nBits[0] = bits;
        if (numBands == 2) {
            nBits[1] = bits;
        }
        this.colorModel =
            new ComponentColorModel(colorSpace,
                                    nBits,
                                    hasAlpha,
                                    isAlphaPremultiplied,
                                    transparency,
                                    dataType);

        int[] bandOffsets = new int[numBands];
        bandOffsets[0] = 0;
        if (numBands == 2) {
            bandOffsets[1] = 1;
        }

        int w = 1;
        int h = 1;
        this.sampleModel =
            new PixelInterleavedSampleModel(dataType,
                                            w, h,
                                            numBands, w*numBands,
                                            bandOffsets);
    } else {
        int numEntries = 1 << bits;
        byte[] arr = new byte[numEntries];
        for (int i = 0; i < numEntries; i++) {
            arr[i] = (byte)(i*255/(numEntries - 1));
        }
        this.colorModel =
            new IndexColorModel(bits, numEntries, arr, arr, arr);

        this.sampleModel =
            new MultiPixelPackedSampleModel(dataType, 1, 1, bits);
    }
}