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

The following examples show how to use java.awt.image.DataBuffer#TYPE_USHORT . 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 jdk8u-dev-jdk 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: RenderedImageSrc.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the data read was originally signed in the specified
 * component, false if not. This method always returns false since PPM
 * data is always unsigned.
 *
 * @param c The index of the component, from 0 to N-1.
 *
 * @return always false, since PPM data is always unsigned.
 * */
public boolean isOrigSigned(int c) {
    if (isBinary) return true;

    // Check component index
    SampleModel sm = null;
    if (inputIsRaster)
        sm = raster.getSampleModel();
    else
        sm = src.getSampleModel();

    if (sm.getDataType() == DataBuffer.TYPE_USHORT ||
        sm.getDataType() == DataBuffer.TYPE_BYTE)
        return false;
    return true;
}
 
Example 3
Source File: ColConvTest.java    From jdk8u60 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 4
Source File: IncorrectSampleMaskTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    int[] dataTypes = new int[] {
        DataBuffer.TYPE_BYTE,
        DataBuffer.TYPE_USHORT,
        DataBuffer.TYPE_INT };

    for (int type : dataTypes) {
        doTest(type);
    }
}
 
Example 5
Source File: MrsImageInfo.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private static void printTileType(final MrsPyramidMetadata metadata, PrintStream out)
{
  out.print("Type: ");
  switch (metadata.getTileType())
  {
  case DataBuffer.TYPE_BYTE:
    out.println("byte");
    break;
  case DataBuffer.TYPE_FLOAT:
    out.println("float");
    break;
  case DataBuffer.TYPE_DOUBLE:
    out.println("double");
    break;
  case DataBuffer.TYPE_INT:
    out.println("int");
    break;
  case DataBuffer.TYPE_SHORT:
    out.println("short");
    break;
  case DataBuffer.TYPE_USHORT:
    out.println("unsigned short");
    break;
  default:
    break;
  }
}
 
Example 6
Source File: TIFFDecompressor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static int getDataTypeFromNumBits(int numBits, boolean isSigned) {
    int dataType;

    if (numBits <= 8) {
        dataType = DataBuffer.TYPE_BYTE;
    } else if (numBits <= 16) {
        dataType = isSigned ?
            DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
    } else {
        dataType = DataBuffer.TYPE_INT;
    }

    return dataType;
}
 
Example 7
Source File: MrsImageInfo.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private static void printNodata(final MrsPyramidMetadata metadata, PrintStream out)
{
  out.print("NoData: ");
  for (int band = 0; band < metadata.getBands(); band++)
  {
    if (band > 0)
    {
      out.print(", ");
    }
    switch (metadata.getTileType())
    {
    case DataBuffer.TYPE_BYTE:
      out.print(metadata.getDefaultValueByte(band));
      break;
    case DataBuffer.TYPE_FLOAT:
      out.print(metadata.getDefaultValueFloat(band));
      break;
    case DataBuffer.TYPE_DOUBLE:
      out.print(metadata.getDefaultValueDouble(band));
      break;
    case DataBuffer.TYPE_INT:
      out.print(metadata.getDefaultValueInt(band));
      break;
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT:
      out.print(metadata.getDefaultValueShort(band));
      break;
    default:
      break;
    }
  }
  out.println("");
}
 
Example 8
Source File: TIFFDecompressor.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static int getDataTypeFromNumBits(int numBits, boolean isSigned) {
    int dataType;

    if (numBits <= 8) {
        dataType = DataBuffer.TYPE_BYTE;
    } else if (numBits <= 16) {
        dataType = isSigned ?
            DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
    } else {
        dataType = DataBuffer.TYPE_INT;
    }

    return dataType;
}
 
Example 9
Source File: IncorrectSampleMaskTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    int[] dataTypes = new int[] {
        DataBuffer.TYPE_BYTE,
        DataBuffer.TYPE_USHORT,
        DataBuffer.TYPE_INT };

    for (int type : dataTypes) {
        doTest(type);
    }
}
 
Example 10
Source File: GeneralRenderer.java    From openjdk-jdk9 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 11
Source File: ImageTypeSpecifier.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Packed(ColorSpace colorSpace,
              int redMask,
              int greenMask,
              int blueMask,
              int alphaMask, // 0 if no alpha
              int transferType,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (colorSpace.getType() != ColorSpace.TYPE_RGB) {
        throw new IllegalArgumentException
            ("colorSpace is not of type TYPE_RGB!");
    }
    if (transferType != DataBuffer.TYPE_BYTE &&
        transferType != DataBuffer.TYPE_USHORT &&
        transferType != DataBuffer.TYPE_INT) {
        throw new IllegalArgumentException
            ("Bad value for transferType!");
    }
    if (redMask == 0 && greenMask == 0 &&
        blueMask == 0 && alphaMask == 0) {
        throw new IllegalArgumentException
            ("No mask has at least 1 bit set!");
    }
    this.colorSpace = colorSpace;
    this.redMask = redMask;
    this.greenMask = greenMask;
    this.blueMask = blueMask;
    this.alphaMask = alphaMask;
    this.transferType = transferType;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    int bits = 32;
    this.colorModel =
        new DirectColorModel(colorSpace,
                             bits,
                             redMask, greenMask, blueMask,
                             alphaMask, isAlphaPremultiplied,
                             transferType);
    this.sampleModel = colorModel.createCompatibleSampleModel(1, 1);
}
 
Example 12
Source File: ImageTypeSpecifier.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public Packed(ColorSpace colorSpace,
              int redMask,
              int greenMask,
              int blueMask,
              int alphaMask, // 0 if no alpha
              int transferType,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (colorSpace.getType() != ColorSpace.TYPE_RGB) {
        throw new IllegalArgumentException
            ("colorSpace is not of type TYPE_RGB!");
    }
    if (transferType != DataBuffer.TYPE_BYTE &&
        transferType != DataBuffer.TYPE_USHORT &&
        transferType != DataBuffer.TYPE_INT) {
        throw new IllegalArgumentException
            ("Bad value for transferType!");
    }
    if (redMask == 0 && greenMask == 0 &&
        blueMask == 0 && alphaMask == 0) {
        throw new IllegalArgumentException
            ("No mask has at least 1 bit set!");
    }
    this.colorSpace = colorSpace;
    this.redMask = redMask;
    this.greenMask = greenMask;
    this.blueMask = blueMask;
    this.alphaMask = alphaMask;
    this.transferType = transferType;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    int bits = 32;
    this.colorModel =
        new DirectColorModel(colorSpace,
                             bits,
                             redMask, greenMask, blueMask,
                             alphaMask, isAlphaPremultiplied,
                             transferType);
    this.sampleModel = colorModel.createCompatibleSampleModel(1, 1);
}
 
Example 13
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 14
Source File: ImageTypeSpecifier.java    From hottub 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 15
Source File: ImageTypeSpecifier.java    From jdk8u60 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 16
Source File: ImageTypeSpecifier.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public Packed(ColorSpace colorSpace,
              int redMask,
              int greenMask,
              int blueMask,
              int alphaMask, // 0 if no alpha
              int transferType,
              boolean isAlphaPremultiplied) {
    if (colorSpace == null) {
        throw new IllegalArgumentException("colorSpace == null!");
    }
    if (colorSpace.getType() != ColorSpace.TYPE_RGB) {
        throw new IllegalArgumentException
            ("colorSpace is not of type TYPE_RGB!");
    }
    if (transferType != DataBuffer.TYPE_BYTE &&
        transferType != DataBuffer.TYPE_USHORT &&
        transferType != DataBuffer.TYPE_INT) {
        throw new IllegalArgumentException
            ("Bad value for transferType!");
    }
    if (redMask == 0 && greenMask == 0 &&
        blueMask == 0 && alphaMask == 0) {
        throw new IllegalArgumentException
            ("No mask has at least 1 bit set!");
    }
    this.colorSpace = colorSpace;
    this.redMask = redMask;
    this.greenMask = greenMask;
    this.blueMask = blueMask;
    this.alphaMask = alphaMask;
    this.transferType = transferType;
    this.isAlphaPremultiplied = isAlphaPremultiplied;

    int bits = 32;
    this.colorModel =
        new DirectColorModel(colorSpace,
                             bits,
                             redMask, greenMask, blueMask,
                             alphaMask, isAlphaPremultiplied,
                             transferType);
    this.sampleModel = colorModel.createCompatibleSampleModel(1, 1);
}
 
Example 17
Source File: ImageTypeSpecifier.java    From TencentKona-8 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 18
Source File: OffScreenImageSource.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 19
Source File: ImageTypeSpecifier.java    From openjdk-8 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 20
Source File: LosslessFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Initialize the encoder and set all final fields
 */
PredictorEncoder(PDDocument document, BufferedImage image)
{
    this.document = document;
    this.image = image;

    // The raw count of components per pixel including optional alpha
    this.componentsPerPixel = image.getColorModel().getNumComponents();
    this.transferType = image.getRaster().getTransferType();
    this.bytesPerComponent = (transferType == DataBuffer.TYPE_SHORT
            || transferType == DataBuffer.TYPE_USHORT) ? 2 : 1;

    // Only the bytes we need in the output (excluding alpha)
    this.bytesPerPixel = image.getColorModel().getNumColorComponents() * bytesPerComponent;

    this.height = image.getHeight();
    this.width = image.getWidth();
    this.imageType = image.getType();
    this.hasAlpha = image.getColorModel().getNumComponents() != image.getColorModel()
            .getNumColorComponents();
    this.alphaImageData = hasAlpha ? new byte[width * height * bytesPerComponent] : null;

    // The rows have 1-byte encoding marker and width * BYTES_PER_PIXEL pixel-bytes
    int dataRowByteCount = width * bytesPerPixel + 1;
    this.dataRawRowNone = new byte[dataRowByteCount];
    this.dataRawRowSub = new byte[dataRowByteCount];
    this.dataRawRowUp = new byte[dataRowByteCount];
    this.dataRawRowAverage = new byte[dataRowByteCount];
    this.dataRawRowPaeth = new byte[dataRowByteCount];

    // Write the encoding markers
    dataRawRowNone[0] = 0;
    dataRawRowSub[0] = 1;
    dataRawRowUp[0] = 2;
    dataRawRowAverage[0] = 3;
    dataRawRowPaeth[0] = 4;

    // c | b
    // -----
    // a | x
    //
    // x => current pixel
    this.aValues = new byte[bytesPerPixel];
    this.cValues = new byte[bytesPerPixel];
    this.bValues = new byte[bytesPerPixel];
    this.xValues = new byte[bytesPerPixel];
    this.tmpResultValues = new byte[bytesPerPixel];
}