Java Code Examples for java.awt.image.DataBuffer#getElem()

The following examples show how to use java.awt.image.DataBuffer#getElem() . 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: SymbolSample.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
private static ByteProcessor createBuffer (BufferedImage img)
{
    DataBuffer dataBuffer = img.getData().getDataBuffer();
    ByteProcessor buf = new ByteProcessor(img.getWidth(), img.getHeight());

    for (int y = 0, h = img.getHeight(); y < h; y++) {
        for (int x = 0, w = img.getWidth(); x < w; x++) {
            int index = x + (y * w);
            int elem = dataBuffer.getElem(index);

            // ShapeSymbol instances use alpha channel as the pixel level
            // With 0 as totally transparent so background (255)
            // And with 255 as totally opaque so foreground (0)
            int val = 255 - (elem >>> 24);
            buf.set(x, y, val);
        }
    }

    // binarize
    buf.threshold(216);

    return buf;
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static Image makeTestImage(DataBuffer dataBuffer, ColorModel colorModel, int w, int h, int transIdx) {
  // DataBufferByte dataBufferByte = null;
  // if (dataBuffer instanceof DataBufferByte) {
  //   dataBufferByte = (DataBufferByte) dataBuffer;
  // } else {
  //   System.out.println("No DataBufferByte");
  // }
  // byte data[] = dataBufferByte.getData();
  BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      int arrayIndex = x + y * w;
      // int colorIndex = Byte.toUnsignedInt(data[arrayIndex]);
      int colorIndex = dataBuffer.getElem(arrayIndex);
      if (transIdx == colorIndex) {
        buf.setRGB(x, y, Color.RED.getRGB()); // 0xFF_FF_00_00);
      } else {
        buf.setRGB(x, y, colorModel.getRGB(colorIndex));
      }
    }
  }
  return buf;
}
 
Example 3
Source File: PixelTests.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 4
Source File: PixelTests.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 5
Source File: PixelTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 6
Source File: PixelTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 7
Source File: PixelTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 8
Source File: PixelTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 9
Source File: Grib2DataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private float[] getData41(RandomAccessFile raf, Grib2Drs.Type0 gdrs) throws IOException {
  int nb = gdrs.numberOfBits;
  int D = gdrs.decimalScaleFactor;
  float DD = (float) java.lang.Math.pow((double) 10, (double) D);
  float R = gdrs.referenceValue;
  int E = gdrs.binaryScaleFactor;
  float EE = (float) java.lang.Math.pow(2.0, (double) E);

  // LOOK: can # datapoints differ from bitmap and data ?
  // dataPoints are number of points encoded, it could be less than the
  // totalNPoints in the grid record if bitMap is used, otherwise equal
  float[] data = new float[totalNPoints];

  // no data to decode, set to reference value
  if (nb == 0) {
    Arrays.fill(data, R);
    return data;
  }

  // Y * 10**D = R + (X1 + X2) * 2**E
  // E = binary scale factor
  // D = decimal scale factor
  // R = reference value
  // X1 = 0
  // X2 = scaled encoded value
  // data[ i ] = (R + ( X1 + X2) * EE)/DD ;

  byte[] buf = new byte[dataLength - 5];
  raf.readFully(buf);
  InputStream in = new ByteArrayInputStream(buf);
  BufferedImage image = ImageIO.read(in);

  if (nb != image.getColorModel().getPixelSize()) {
    logger.debug("PNG pixel size {} disagrees with grib number of bits {}", image.getColorModel().getPixelSize(), nb);
  }

  DataBuffer db = image.getRaster().getDataBuffer();
  if (bitmap == null) {
    for (int i = 0; i < dataNPoints; i++) {
      data[i] = (R + db.getElem(i) * EE) / DD;
    }
  } else {
    for (int bitPt = 0, dataPt = 0; bitPt < totalNPoints; bitPt++) {
      if (GribNumbers.testBitIsSet(bitmap[bitPt / 8], bitPt % 8)) {
        data[bitPt] = (R + db.getElem(dataPt++) * EE) / DD;
      } else {
        data[bitPt] = staticMissingValue;
      }
    }
  }

  return data;
}
 
Example 10
Source File: PixelTests.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 11
Source File: PixelTests.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 12
Source File: PixelTests.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 13
Source File: PixelTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 14
Source File: ImageUtils.java    From RemoteSupportTool with Apache License 2.0 4 votes vote down vote up
public static boolean equals(BufferedImage image1, BufferedImage image2) {
    final int image1Width = image1.getWidth();
    final int image1Height = image1.getHeight();

    final int image2Width = image2.getWidth();
    final int image2Height = image2.getHeight();

    if (image1Width != image2Width || image1Height != image2Height)
        return false;

    final DataBuffer image1Buffer = image1.getData().getDataBuffer();
    final DataBuffer image2Buffer = image2.getData().getDataBuffer();

    final int image1BufferSize = image1Buffer.getSize();
    final int image2BufferSize = image2Buffer.getSize();
    if (image1BufferSize != image2BufferSize)
        return false;

    if (image1Buffer instanceof DataBufferInt && image2Buffer instanceof DataBufferInt) {
        // compare according to https://stackoverflow.com/a/11006984
        final DataBufferInt image1BufferInt = (DataBufferInt) image1Buffer;
        final DataBufferInt image2BufferInt = (DataBufferInt) image2Buffer;
        if (image1BufferInt.getNumBanks() != image2BufferInt.getNumBanks())
            return false;

        for (int bank = 0; bank < image1BufferInt.getNumBanks(); bank++) {
            int[] actual = image1BufferInt.getData(bank);
            int[] expected = image2BufferInt.getData(bank);
            if (!Arrays.equals(actual, expected))
                return false;
        }
    } else {
        // compare according to https://stackoverflow.com/a/51497360
        for (int i = 0; i < image1BufferSize; i++) {
            if (image1Buffer.getElem(i) != image2Buffer.getElem(i)) {
                return false;
            }
        }
    }

    //for (int x = 0; x < image1Width; x++) {
    //    for (int y = 0; y < image1Height; y++) {
    //        if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
    //            return false;
    //        }
    //    }
    //}

    return true;
}
 
Example 15
Source File: PixelTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 16
Source File: PixelTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}
 
Example 17
Source File: PixelTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void runTest(Object context, int numReps) {
    DataBuffer db = ((Context) context).db;
    do {
        db.getElem(numReps&7);
    } while (--numReps > 0);
}