public int getAlpha(Object inData) {
   DataBuffer buffer = Buffers.createBufferFromData(transferType, inData, getNumComponents());
   int shift = 8 - getComponentSize(getNumColorComponents());
   int alpha = buffer.getElem(getNumColorComponents());
   if (shift >= 0) return alpha << shift;
   return alpha >> (-shift);
 }
  /**
   * Sets all samples for a rectangle of pixels from an int array containing one sample per array
   * element. ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds.
   *
   * @param x The X coordinate of the upper left pixel location.
   * @param y The Y coordinate of the upper left pixel location.
   * @param w The width of the pixel rectangle.
   * @param h The height of the pixel rectangle.
   * @param iArray The input samples in an int array.
   * @param data The DataBuffer containing the image data.
   * @see #getPixels(int, int, int, int, int[], DataBuffer)
   */
  public void setPixels(int x, int y, int w, int h, int iArray[], DataBuffer data) {
    int x1 = x + w;
    int y1 = y + h;

    if (x < 0
        || x >= width
        || w > width
        || x1 < 0
        || x1 > width
        || y < 0
        || y >= height
        || h > height
        || y1 < 0
        || y1 > height) {
      throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
    }

    int lineOffset = y * scanlineStride + x;
    int srcOffset = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int value = data.getElem(lineOffset + j);
        for (int k = 0; k < numBands; k++) {
          value &= ~bitMasks[k];
          int srcValue = iArray[srcOffset++];
          value |= ((srcValue << bitOffsets[k]) & bitMasks[k]);
        }
        data.setElem(lineOffset + j, value);
      }
      lineOffset += scanlineStride;
    }
  }
Example #3
0
 /**
  * Returns a width- and height-aligned image representation sharing data w/ {@link #image}.
  *
  * @param width
  * @param height
  * @return
  * @throws IllegalArgumentException if requested size exceeds image size
  */
 public BufferedImage getAlignedImage(int width, int height) throws IllegalArgumentException {
   if (width * height > image.getWidth() * image.getHeight()) {
     throw new IllegalArgumentException(
         "Requested size exceeds image size: "
             + width
             + "x"
             + height
             + " > "
             + image.getWidth()
             + "x"
             + image.getHeight());
   }
   if (width == image.getWidth() && height == image.getHeight()) {
     return image;
   } else {
     final ColorModel cm = image.getColorModel();
     final WritableRaster raster0 = image.getRaster();
     final DataBuffer dataBuffer = raster0.getDataBuffer();
     final SinglePixelPackedSampleModel sppsm0 =
         (SinglePixelPackedSampleModel) raster0.getSampleModel();
     final SinglePixelPackedSampleModel sppsm1 =
         new SinglePixelPackedSampleModel(
             dataBuffer.getDataType(),
             width,
             height,
             width /* scanLineStride */,
             sppsm0.getBitMasks());
     final WritableRaster raster1 = WritableRaster.createWritableRaster(sppsm1, dataBuffer, null);
     return new BufferedImage(cm, raster1, cm.isAlphaPremultiplied(), null);
   }
 }
 @Override
 public void renderPadPixel(
     final ImageMask imageMask, final DataBuffer data, final int pixelIndex) {
   if (imageMask.isPadPixel(data.getElem(pixelIndex))) {
     data.setElem(pixelIndex, 0x00000000);
   }
 }
Example #5
0
    private long sizeOf(BufferedImage image) {
      if (image == null) {
        return 0L;
      }

      DataBuffer dataBuffer = image.getRaster().getDataBuffer();
      int dataTypeSize;
      switch (dataBuffer.getDataType()) {
        case DataBuffer.TYPE_BYTE:
          dataTypeSize = 1;
          break;
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
          dataTypeSize = 2;
          break;
        case DataBuffer.TYPE_INT:
        case DataBuffer.TYPE_FLOAT:
          dataTypeSize = 4;
          break;
        case DataBuffer.TYPE_DOUBLE:
        case DataBuffer.TYPE_UNDEFINED:
        default:
          dataTypeSize = 8;
          break;
      }
      return dataBuffer.getSize() * dataTypeSize;
    }
  public Object getDataElements(int rgb, Object pixel) {
    // Convert rgb to [0.0, 1.0] sRGB values.
    float[] rgbFloats = {
      ((rgb >> 16) & 0xff) / 255.0F, ((rgb >> 8) & 0xff) / 255.0F, ((rgb >> 0) & 0xff) / 255.0F
    };

    // Convert from rgb to color space components.
    float[] data = cspace.fromRGB(rgbFloats);
    DataBuffer buffer = Buffers.createBuffer(transferType, pixel, getNumComponents());
    int numColors = getNumColorComponents();

    if (hasAlpha()) {
      float alpha = ((rgb >> 24) & 0xff) / 255.0F;

      /* If color model has alpha and should be premultiplied, multiply
      color space components with alpha value. */
      if (isAlphaPremultiplied()) {
        for (int i = 0; i < numColors; i++) data[i] *= alpha;
      }
      // Scale the alpha sample to the correct number of bits.
      alpha *= (1 << (bits[numColors] - 1));
      // Arrange the alpha sample in the output array.
      buffer.setElemFloat(numColors, alpha);
    }
    for (int i = 0; i < numColors; i++) {
      // Scale the color samples to the correct number of bits.
      float value = data[i] * (1 << (bits[i] - 1));
      // Arrange the color samples in the output array.
      buffer.setElemFloat(i, value);
    }
    return Buffers.getData(buffer);
  }
 public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
   int type = getTransferType();
   Object ret = null;
   switch (type) {
     case DataBuffer.TYPE_BYTE:
       {
         byte[] in = (byte[]) obj;
         if (in == null) in = new byte[1];
         in[0] = (byte) data.getElem(x + y * scanlineStride);
         ret = in;
       }
       break;
     case DataBuffer.TYPE_USHORT:
       {
         short[] in = (short[]) obj;
         if (in == null) in = new short[1];
         in[0] = (short) data.getElem(x + y * scanlineStride);
         ret = in;
       }
       break;
     case DataBuffer.TYPE_INT:
       {
         int[] in = (int[]) obj;
         if (in == null) in = new int[1];
         in[0] = data.getElem(x + y * scanlineStride);
         ret = in;
       }
       break;
   }
   return ret;
 }
 /**
  * Create Byte type raster iterator to follow from minX, minY raster and rectangle intersection
  * coordinate.
  *
  * @param raster will be followed by this iterator.
  * @param subArea {@code Rectangle} which define read iterator area.
  * @throws IllegalArgumentException if subArea don't intersect raster boundary.
  */
 DefaultDirectFloatIterator(final Raster raster, final Rectangle subArea) {
   super(raster, subArea);
   final DataBuffer databuf = raster.getDataBuffer();
   assert (databuf.getDataType() == DataBuffer.TYPE_FLOAT)
       : "raster data or not Byte type: " + databuf;
   this.currentDataArray = ((DataBufferFloat) databuf).getData();
 }
 /**
  * Sets the sample value for a band for the pixel at (x, y) in the specified data buffer.
  *
  * @param x the x-coordinate of the pixel.
  * @param y the y-coordinate of the pixel.
  * @param b the band (in the range <code>0</code> to <code>getNumBands() - 1</code>).
  * @param s the sample value.
  * @param data the data buffer (<code>null</code> not permitted).
  * @throws NullPointerException if <code>data</code> is <code>null</code>.
  */
 public void setSample(int x, int y, int b, int s, DataBuffer data) {
   int offset = scanlineStride * y + x;
   int samples = data.getElem(offset);
   int bitMask = bitMasks[b];
   samples &= ~bitMask;
   samples |= (s << bitOffsets[b]) & bitMask;
   data.setElem(offset, samples);
 }
  public Object getDataElements(int[] components, int offset, Object obj) {
    DataBuffer buffer = Buffers.createBuffer(transferType, obj, getNumComponents());
    int numComponents = getNumComponents();

    for (int i = 0; i < numComponents; i++) buffer.setElem(i, components[offset++]);

    return Buffers.getData(buffer);
  }
 /**
  * Sets a sample in the specified band for the pixel located at (x,y) in the DataBuffer using an
  * int for input. ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in
  * bounds.
  *
  * @param x The X coordinate of the pixel location.
  * @param y The Y coordinate of the pixel location.
  * @param b The band to set.
  * @param s The input sample as an int.
  * @param data The DataBuffer containing the image data.
  * @see #getSample(int, int, int, DataBuffer)
  */
 public void setSample(int x, int y, int b, int s, DataBuffer data) {
   // Bounds check for 'b' will be performed automatically
   if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
     throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
   }
   int value = data.getElem(y * scanlineStride + x);
   value &= ~bitMasks[b];
   value |= (s << bitOffsets[b]) & bitMasks[b];
   data.setElem(y * scanlineStride + x, value);
 }
  public int[] getComponents(Object pixel, int[] components, int offset) {
    DataBuffer buffer = Buffers.createBuffer(transferType, pixel, getNumComponents());
    int numComponents = getNumComponents();

    if (components == null) components = new int[numComponents + offset];

    for (int i = 0; i < numComponents; i++) components[offset++] = buffer.getElem(i);

    return components;
  }
 /**
  * Decode JBIG2 data using Java ImageIO library.
  *
  * <p>{@inheritDoc}
  */
 public void decode(
     InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
     throws IOException {
   /**
    * A working JBIG2 ImageIO plugin is needed to decode JBIG2 encoded streams. The following is
    * known to be working. It can't be bundled with PDFBox because of an incompatible license.
    * http://code.google.com/p/jbig2-imageio/
    */
   Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JBIG2");
   if (!readers.hasNext()) {
     LOG.error("Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.");
     return;
   }
   ImageReader reader = readers.next();
   COSDictionary decodeP = (COSDictionary) options.getDictionaryObject(COSName.DECODE_PARMS);
   COSInteger bits = (COSInteger) options.getDictionaryObject(COSName.BITS_PER_COMPONENT);
   COSStream st = null;
   if (decodeP != null) {
     st = (COSStream) decodeP.getDictionaryObject(COSName.JBIG2_GLOBALS);
   }
   if (st != null) {
     reader.setInput(
         ImageIO.createImageInputStream(
             new SequenceInputStream(st.getFilteredStream(), compressedData)));
   } else {
     reader.setInput(ImageIO.createImageInputStream(compressedData));
   }
   BufferedImage bi = reader.read(0);
   reader.dispose();
   if (bi != null) {
     // I am assuming since JBIG2 is always black and white
     // depending on your renderer this might or might be needed
     if (bi.getColorModel().getPixelSize() != bits.intValue()) {
       if (bits.intValue() != 1) {
         LOG.error("Do not know how to deal with JBIG2 with more than 1 bit");
         return;
       }
       BufferedImage packedImage =
           new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
       Graphics graphics = packedImage.getGraphics();
       graphics.drawImage(bi, 0, 0, null);
       graphics.dispose();
       bi = packedImage;
     }
     DataBuffer dBuf = bi.getData().getDataBuffer();
     if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
       result.write(((DataBufferByte) dBuf).getData());
     } else {
       LOG.error("Image data buffer not of type byte but type " + dBuf.getDataType());
     }
   } else {
     LOG.error("Something went wrong when decoding the JBIG2 encoded datastream.");
   }
 }
 /**
  * Sets a pixel in the DataBuffer using an int array of samples for input.
  * ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds.
  *
  * @param x The X coordinate of the pixel location.
  * @param y The Y coordinate of the pixel location.
  * @param iArray The input samples in an int array.
  * @param data The DataBuffer containing the image data.
  * @see #getPixel(int, int, int[], DataBuffer)
  */
 public void setPixel(int x, int y, int iArray[], DataBuffer data) {
   if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
     throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
   }
   int lineOffset = y * scanlineStride + x;
   int value = data.getElem(lineOffset);
   for (int i = 0; i < numBands; i++) {
     value &= ~bitMasks[i];
     value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]);
   }
   data.setElem(lineOffset, value);
 }
 @Override
 public void renderPixelBand(
     final DataBuffer data,
     final int pixelIndex,
     final ImageInputStream imageInputStream,
     final int bandIndex)
     throws IOException {
   data.setElem(
       pixelIndex,
       ALPHA_MASK
           | data.getElem(pixelIndex)
           | (imageInputStream.read() << bandMapping.get(bandIndex)));
 }
Example #16
0
  public final void parseData(int data[][][], BufferedImage bi, ImageContents imageContents) {
    DataBuffer buffer = bi.getRaster().getDataBuffer();

    PSDHeaderInfo header = imageContents.header;
    int width = header.Columns;
    int height = header.Rows;

    for (int y = 0; y < height; y++)
      for (int x = 0; x < width; x++) {
        int rgb = getRGB(data, x, y, imageContents);
        buffer.setElem(y * width + x, rgb);
      }
  }
  private float[] getRGBFloat(Object inData) {
    DataBuffer buffer = Buffers.createBufferFromData(transferType, inData, getNumComponents());
    int colors = getNumColorComponents();
    float[] data = new float[colors];

    // FIXME: unpremultiply data that is premultiplied
    for (int i = 0; i < colors; i++) {
      float maxValue = (1 << getComponentSize(i)) - 1;
      data[i] = buffer.getElemFloat(i) / maxValue;
    }
    float[] rgb = cspace.toRGB(data);
    return rgb;
  }
Example #18
0
  /**
   * Checks if the specified {@code SampleModel} is compatible with this {@code ColorModel}. If
   * {@code sm} is {@code null}, this method returns {@code false}.
   *
   * @param sm the specified {@code SampleModel}, or {@code null}
   * @return {@code true} if the specified {@code SampleModel} is compatible with this {@code
   *     ColorModel}; {@code false} otherwise.
   * @see SampleModel
   */
  public boolean isCompatibleSampleModel(SampleModel sm) {
    if (!(sm instanceof SinglePixelPackedSampleModel)) {
      return false;
    }

    // Must have the same number of components
    if (numComponents != sm.getNumBands()) {
      return false;
    }

    // Transfer type must be the same
    if (sm.getTransferType() != transferType) {
      return false;
    }

    SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm;
    // Now compare the specific masks
    int[] bitMasks = sppsm.getBitMasks();
    if (bitMasks.length != maskArray.length) {
      return false;
    }

    /* compare 'effective' masks only, i.e. only part of the mask
     * which fits the capacity of the transfer type.
     */
    int maxMask = (int) ((1L << DataBuffer.getDataTypeSize(transferType)) - 1);
    for (int i = 0; i < bitMasks.length; i++) {
      if ((maxMask & bitMasks[i]) != (maxMask & maskArray[i])) {
        return false;
      }
    }

    return true;
  }
  /**
   * Returns a ByteBuffer of BufferedImage data. Ensure BufferedImage is of 4BYTE_ABGR type. If
   * imageFormat is set to GL_RGBA, byte stream will be converted.
   */
  private ByteBuffer getByteBuffer(final BufferedImage _image) {
    final DataBuffer buffer = _image.getRaster().getDataBuffer();
    final int type = buffer.getDataType();

    if (type == DataBuffer.TYPE_BYTE) {
      final byte[] data = ((DataBufferByte) buffer).getData();
      if (imageFormat == GL3.GL_RGBA) {
        convertABGRtoRGBA(data);
      }

      return ByteBuffer.wrap(data);
    }

    System.out.println("Failed to determine DataBuffer type.");
    return null;
  }
  /**
   * Sets the samples for the pixel at (x, y) in the specified data buffer to the specified values.
   *
   * @param x the x-coordinate of the pixel.
   * @param y the y-coordinate of the pixel.
   * @param iArray the sample values (<code>null</code> not permitted).
   * @param data the data buffer (<code>null</code> not permitted).
   * @throws NullPointerException if either <code>iArray</code> or <code>data</code> is <code>null
   *     </code>.
   */
  public void setPixel(int x, int y, int[] iArray, DataBuffer data) {
    int offset = scanlineStride * y + x;

    int samples = 0;
    for (int b = 0; b < numBands; b++) samples |= (iArray[b] << bitOffsets[b]) & bitMasks[b];

    data.setElem(offset, samples);
  }
 /**
  * Returns as int the sample in a specified band for the pixel located at (x,y).
  * ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds.
  *
  * @param x The X coordinate of the pixel location.
  * @param y The Y coordinate of the pixel location.
  * @param b The band to return.
  * @param data The DataBuffer containing the image data.
  * @return the sample in a specified band for the specified pixel.
  * @see #setSample(int, int, int, int, DataBuffer)
  */
 public int getSample(int x, int y, int b, DataBuffer data) {
   // Bounds check for 'b' will be performed automatically
   if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
     throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
   }
   int sample = data.getElem(y * scanlineStride + x);
   return ((sample & bitMasks[b]) >>> bitOffsets[b]);
 }
  /**
   * This method provides a real tie point grid from a 'tie point band'.
   *
   * @param band - the 'tie point band'
   * @return TiePointGrid
   */
  public static TiePointGrid getTpgFromBand(Band band) {
    final DataBuffer dataBuffer = band.getSourceImage().getData().getDataBuffer();
    float[] tpgData = new float[dataBuffer.getSize()];
    for (int i = 0; i < dataBuffer.getSize(); i++) {
      tpgData[i] = dataBuffer.getElemFloat(i);
    }

    return new TiePointGrid(
        band.getName(),
        band.getSceneRasterWidth(),
        band.getSceneRasterHeight(),
        0.0f,
        0.0f,
        1.0f,
        1.0f,
        tpgData);
  }
  /**
   * Returns data for a single pixel in a primitive array of type TransferType. For a
   * SinglePixelPackedSampleModel, the array will have one element, and the type will be the same as
   * the storage data type. Generally, obj should be passed in as null, so that the Object will be
   * created automatically and will be of the right primitive data type.
   *
   * <p>The following code illustrates transferring data for one pixel from DataBuffer <code>db1
   * </code>, whose storage layout is described by SinglePixelPackedSampleModel <code>sppsm1</code>,
   * to DataBuffer <code>db2</code>, whose storage layout is described by
   * SinglePixelPackedSampleModel <code>sppsm2</code>. The transfer will generally be more efficient
   * than using getPixel/setPixel.
   *
   * <pre>
   *       SinglePixelPackedSampleModel sppsm1, sppsm2;
   *       DataBufferInt db1, db2;
   *       sppsm2.setDataElements(x, y, sppsm1.getDataElements(x, y, null,
   *                              db1), db2);
   * </pre>
   *
   * Using getDataElements/setDataElements to transfer between two DataBuffer/SampleModel pairs is
   * legitimate if the SampleModels have the same number of bands, corresponding bands have the same
   * number of bits per sample, and the TransferTypes are the same.
   *
   * <p>If obj is non-null, it should be a primitive array of type TransferType. Otherwise, a
   * ClassCastException is thrown. An ArrayIndexOutOfBoundsException may be thrown if the
   * coordinates are not in bounds, or if obj is non-null and is not large enough to hold the pixel
   * data.
   *
   * @param x The X coordinate of the pixel location.
   * @param y The Y coordinate of the pixel location.
   * @param obj If non-null, a primitive array in which to return the pixel data.
   * @param data The DataBuffer containing the image data.
   * @return the data for the specified pixel.
   * @see #setDataElements(int, int, Object, DataBuffer)
   */
  public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
    // Bounds check for 'b' will be performed automatically
    if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
      throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
    }

    int type = getTransferType();

    switch (type) {
      case DataBuffer.TYPE_BYTE:
        byte[] bdata;

        if (obj == null) bdata = new byte[1];
        else bdata = (byte[]) obj;

        bdata[0] = (byte) data.getElem(y * scanlineStride + x);

        obj = (Object) bdata;
        break;

      case DataBuffer.TYPE_USHORT:
        short[] sdata;

        if (obj == null) sdata = new short[1];
        else sdata = (short[]) obj;

        sdata[0] = (short) data.getElem(y * scanlineStride + x);

        obj = (Object) sdata;
        break;

      case DataBuffer.TYPE_INT:
        int[] idata;

        if (obj == null) idata = new int[1];
        else idata = (int[]) obj;

        idata[0] = data.getElem(y * scanlineStride + x);

        obj = (Object) idata;
        break;
    }

    return obj;
  }
  // Create an appropriate array of bits, given a colorspace (ie, number of
  // bands), size of the storage data type, and presence of an alpha band.
  private static int[] findBits(ColorSpace colorSpace, int transferType, boolean hasAlpha) {
    int[] bits;
    if (hasAlpha) bits = new int[colorSpace.getNumComponents() + 1];
    else bits = new int[colorSpace.getNumComponents()];

    Arrays.fill(bits, DataBuffer.getDataTypeSize(transferType));

    return bits;
  }
  /**
   * Returns an array containing the samples for the pixel at (x, y) in the specified data buffer.
   * If <code>iArray</code> is not <code>null</code>, it will be populated with the sample values
   * and returned as the result of this function (this avoids allocating a new array instance).
   *
   * @param x the x-coordinate of the pixel.
   * @param y the y-coordinate of the pixel.
   * @param iArray an array to populate with the sample values and return as the result (if <code>
   *     null</code>, a new array will be allocated).
   * @param data the data buffer (<code>null</code> not permitted).
   * @return The pixel sample values.
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
  public int[] getPixel(int x, int y, int[] iArray, DataBuffer data) {
    int offset = scanlineStride * y + x;
    if (iArray == null) iArray = new int[numBands];
    int samples = data.getElem(offset);

    for (int b = 0; b < numBands; b++) iArray[b] = (samples & bitMasks[b]) >>> bitOffsets[b];

    return iArray;
  }
  /**
   * Sets the samples in the specified band for the specified rectangle of pixels from an int array
   * containing one sample per array element. ArrayIndexOutOfBoundsException may be thrown if the
   * coordinates are not in bounds.
   *
   * @param x The X coordinate of the upper left pixel location.
   * @param y The Y coordinate of the upper left pixel location.
   * @param w The width of the pixel rectangle.
   * @param h The height of the pixel rectangle.
   * @param b The band to set.
   * @param iArray The input samples in an int array.
   * @param data The DataBuffer containing the image data.
   * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
   */
  public void setSamples(int x, int y, int w, int h, int b, int iArray[], DataBuffer data) {
    // Bounds check for 'b' will be performed automatically
    if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
      throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
    }
    int lineOffset = y * scanlineStride + x;
    int srcOffset = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int value = data.getElem(lineOffset + j);
        value &= ~bitMasks[b];
        int sample = iArray[srcOffset++];
        value |= ((int) sample << bitOffsets[b]) & bitMasks[b];
        data.setElem(lineOffset + j, value);
      }
      lineOffset += scanlineStride;
    }
  }
Example #27
0
  public void encodeWBMP(RenderedImage renderedImage, OutputStream os) throws IOException {

    BufferedImage bufferedImage = getBufferedImage(renderedImage);

    SampleModel sampleModel = bufferedImage.getSampleModel();

    int type = sampleModel.getDataType();

    if ((bufferedImage.getType() != BufferedImage.TYPE_BYTE_BINARY)
        || (type < DataBuffer.TYPE_BYTE)
        || (type > DataBuffer.TYPE_INT)
        || (sampleModel.getNumBands() != 1)
        || (sampleModel.getSampleSize(0) != 1)) {

      BufferedImage binaryImage =
          new BufferedImage(
              bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);

      Graphics graphics = binaryImage.getGraphics();

      graphics.drawImage(bufferedImage, 0, 0, null);

      renderedImage = binaryImage;
    }

    if (!ImageIO.write(renderedImage, "wbmp", os)) {

      // See http://www.jguru.com/faq/view.jsp?EID=127723

      os.write(0);
      os.write(0);
      os.write(toMultiByte(bufferedImage.getWidth()));
      os.write(toMultiByte(bufferedImage.getHeight()));

      DataBuffer dataBuffer = bufferedImage.getData().getDataBuffer();

      int size = dataBuffer.getSize();

      for (int i = 0; i < size; i++) {
        os.write((byte) dataBuffer.getElem(i));
      }
    }
  }
 /**
  * This method provides a rescaled tie point grid from a 'tie point band'.
  *
  * @param band - the 'tie point band'
  * @param rescaledWidth - width of the rescaled TPG
  * @param rescaledHeight - height of the rescaled TPG
  * @return TiePointGrid
  */
 public static TiePointGrid getRescaledTpgFromBand(
     Band band, int rescaledWidth, int rescaledHeight) {
   final DataBuffer dataBuffer = band.getSourceImage().getData().getDataBuffer();
   float[] tpgData = new float[rescaledWidth * rescaledHeight];
   if (rescaledWidth * rescaledHeight > band.getSceneRasterWidth() * band.getSceneRasterHeight()) {
     throw new OperatorException("Cannot create TPG - width*height too large.");
   }
   int tpgIndex = 0;
   for (int j = 0; j < rescaledHeight; j++) {
     for (int i = 0; i < rescaledWidth; i++) {
       tpgData[rescaledWidth * j + i] = dataBuffer.getElemFloat(tpgIndex);
       tpgIndex++;
     }
     for (int i = rescaledWidth; i < band.getSceneRasterWidth(); i++) {
       tpgIndex++;
     }
   }
   return new TiePointGrid(
       band.getName(), rescaledWidth, rescaledHeight, 0.0f, 0.0f, 1.0f, 1.0f, tpgData);
 }
 private static void addTpg(Product targetProduct, Band scaledBand, String name) {
   DataBuffer dataBuffer;
   float[] tpgData;
   TiePointGrid tpg;
   dataBuffer = scaledBand.getSourceImage().getData().getDataBuffer();
   tpgData = new float[dataBuffer.getSize()];
   for (int i = 0; i < dataBuffer.getSize(); i++) {
     tpgData[i] = dataBuffer.getElemFloat(i);
   }
   tpg =
       new TiePointGrid(
           name,
           scaledBand.getSceneRasterWidth(),
           scaledBand.getSceneRasterHeight(),
           0.0f,
           0.0f,
           1.0f,
           1.0f,
           tpgData);
   targetProduct.addTiePointGrid(tpg);
 }
Example #30
0
  private void readImage(String file) {
    Util.c_log("reading " + file);

    BufferedImage img = null;
    try {
      img = ImageIO.read(new File(file));
    } catch (IOException e) {
      Util.c_log("ERROR: " + e.toString());
    }

    height = img.getHeight();
    width = img.getWidth();

    pixelShould = new byte[height][width][3];
    int[] result = new int[height * width * 3 + 1];

    // img.getData().getPixels(0, 0, height, width, result);
    DataBuffer buffer = img.getData().getDataBuffer();
    short k = 0;

    for (int i = 0; i < height * width; i++) {
      pixelShould[i % height][(int) ((float) i / (float) height)][R] =
          (byte) buffer.getElem(3 * i + R);
      pixelShould[i % height][(int) ((float) i / (float) height)][G] =
          (byte) buffer.getElem(3 * i + G);
      pixelShould[i % height][(int) ((float) i / (float) height)][B] =
          (byte) buffer.getElem(3 * i + B);
      // Log.debug("reading pixel: (" + i % height + "|" + (int) ((float) i / (float) height) + "):
      // [" + buffer.getElem(3 * i + R) + "|" + buffer.getElem(3 * i + G) + "|" + buffer.getElem(3 *
      // i + B) + "]");

      if ((float) i / (float) (height * width) * 100 > k + 10) {
        k += 10;
        Util.c_log(k + "%");
      }
    }
    img = null;
    Util.c_log("finished.");
  }