Java java.awt.image.RasterFormatException 代码实例

・26 分钟阅读

以下是展示如何使用java.awt.image.RasterFormatException的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Creates a Writable subRaster given a region of the Raster. The x and y
 * coordinates specify the horizontal and vertical offsets
 * from the upper-left corner of this Raster to the upper-left corner
 * of the subRaster.  A subset of the bands of the parent Raster may
 * be specified.  If this is null, then all the bands are present in the
 * subRaster. A translation to the subRaster may also be specified.
 * Note that the subRaster will reference the same
 * DataBuffer as the parent Raster, but using different offsets.
 * @param x               X offset.
 * @param y               Y offset.
 * @param width           Width (in pixels) of the subraster.
 * @param height          Height (in pixels) of the subraster.
 * @param x0              Translated X origin of the subraster.
 * @param y0              Translated Y origin of the subraster.
 * @param bandList        Array of band indices.
 * @exception RasterFormatException
 *            if the specified bounding box is outside of the parent Raster.
 */
public WritableRaster createWritableChild(int x, int y,
                                          int width, int height,
                                          int x0, int y0,
                                          int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x+width < x) || (x+width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y+height < y) || (y+height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm,
                                   dataBuffer,
                                   new Rectangle(x0, y0, width, height),
                                   new Point(sampleModelTranslateX+deltaX,
                                             sampleModelTranslateY+deltaY),
                                   this);
}
 

实例 2


/**
 * Creates a Writable subRaster given a region of the Raster. The x and y
 * coordinates specify the horizontal and vertical offsets
 * from the upper-left corner of this Raster to the upper-left corner
 * of the subRaster.  A subset of the bands of the parent Raster may
 * be specified.  If this is null, then all the bands are present in the
 * subRaster. A translation to the subRaster may also be specified.
 * Note that the subRaster will reference the same
 * DataBuffer as the parent Raster, but using different offsets.
 * @param x               X offset.
 * @param y               Y offset.
 * @param width           Width (in pixels) of the subraster.
 * @param height          Height (in pixels) of the subraster.
 * @param x0              Translated X origin of the subraster.
 * @param y0              Translated Y origin of the subraster.
 * @param bandList        Array of band indices.
 * @exception RasterFormatException
 *            if the specified bounding box is outside of the parent Raster.
 */
public WritableRaster createWritableChild(int x, int y,
                                          int width, int height,
                                          int x0, int y0,
                                          int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x+width < x) || (x+width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y+height < y) || (y+height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm,
                                   dataBuffer,
                                   new Rectangle(x0, y0, width, height),
                                   new Point(sampleModelTranslateX+deltaX,
                                             sampleModelTranslateY+deltaY),
                                   this);
}
 

实例 3


public void drawParameterText(Graphics2D g2d, String text, Color backgroundColor, int x, int y)
{
        mParamGraphics.setBackground(backgroundColor);
        Rectangle2D text_size = mParamFont.getStringBounds(text, mFontRenderContext);
        int baseline = (int)(mParamFontLineMetrics.getAscent());
        mParamGraphics.setClip(0, 0, (int)text_size.getWidth(), mParamImage.getHeight());
        mParamGraphics.clearRect(0, 0, (int)text_size.getWidth(), mParamImage.getHeight());
        mParamGraphics.drawString(text, 0, baseline);
        Shape previous_clip = g2d.getClip();
        g2d.clipRect(x-baseline, y-(int)text_size.getWidth(), x-baseline+mParamImage.getHeight(), (int)text_size.getWidth());
        try
        {
                g2d.drawImage(mParamImage, mParamImageOp, x, y);
        }
        catch (RasterFormatException e)
        {
                // do nothing
        }
        g2d.setClip(previous_clip);
}
 

实例 4


@Override
public void actionPerformed(ActionEvent e) {
    try {
        if(!ImageComponents.isActiveLayerImageLayer()) {
            // TODO we could try to rasterize text layers
            if(!Build.CURRENT.isRobotTest()) {
                Messages.showNotImageLayerError();
            }
            return;
        }
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        BufferedImage activeImage = source.getImage();
        Transferable imageTransferable = new ImageTransferable(activeImage);
        try {
            // Sun Jan 04 08:00:30 CET 2015, robot test:
            // java.awt.image.RasterFormatException: Incorrect scanline stride: 12
            // at sun.awt.image.ByteComponentRaster.verify(ByteComponentRaster.java:894)
            // at sun.awt.image.ByteComponentRaster.<init>(ByteComponentRaster.java:201)
            // https://bugs.openjdk.java.net/browse/JDK-8041558
            clipboard.setContents(imageTransferable, null);
        } catch (RasterFormatException rfe) {
            rfe.printStackTrace();
            BufferedImageNode node = new BufferedImageNode(activeImage);
            String s = node.toDetailedString();
            System.out.println(String.format("CopyAction::RasterFormatException in actionPerformed: s = '%s'", s));
        }
    } catch (Exception ex) {
        Messages.showException(ex);
    }
}
 

实例 5


/**
 * Creates a Writable subRaster given a region of the Raster. The x and y
 * coordinates specify the horizontal and vertical offsets
 * from the upper-left corner of this Raster to the upper-left corner
 * of the subRaster.  A subset of the bands of the parent Raster may
 * be specified.  If this is null, then all the bands are present in the
 * subRaster. A translation to the subRaster may also be specified.
 * Note that the subRaster will reference the same
 * DataBuffer as the parent Raster, but using different offsets.
 * @param x               X offset.
 * @param y               Y offset.
 * @param width           Width (in pixels) of the subraster.
 * @param height          Height (in pixels) of the subraster.
 * @param x0              Translated X origin of the subraster.
 * @param y0              Translated Y origin of the subraster.
 * @param bandList        Array of band indices.
 * @exception RasterFormatException
 *            if the specified bounding box is outside of the parent Raster.
 */
public WritableRaster createWritableChild(int x, int y,
                                          int width, int height,
                                          int x0, int y0,
                                          int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x+width < x) || (x+width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y+height < y) || (y+height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm,
                                   dataBuffer,
                                   new Rectangle(x0, y0, width, height),
                                   new Point(sampleModelTranslateX+deltaX,
                                             sampleModelTranslateY+deltaY),
                                   this);
}
 

实例 6


/**
 * Sets the iterator to the next line in the image,
 * and returns {@code true} if the bottom row of the bounding rectangle has been passed.
 */
public boolean nextLineDone() {
    boolean check = src.nextLineDone();
    if (check == dst.nextLineDone()) {
        return check;
    }
    throw new RasterFormatException(ERROR);
}
 

实例 7


/**
 * Sets all values in the specified bank to the specified value.
 *
 * @param buffer The databuffer in which to set all sample values.
 * @param bank   Index of the bank to set.
 * @param value  The value.
 */
private static void fill(final DataBuffer buffer, final int bank, final int value) {
    switch (buffer.getDataType()) {
        case DataBuffer.TYPE_BYTE :  Arrays.fill(((DataBufferByte)  buffer).getData(bank),  (byte)value); break;
        case DataBuffer.TYPE_SHORT:  Arrays.fill(((DataBufferShort) buffer).getData(bank), (short)value); break;
        case DataBuffer.TYPE_USHORT: Arrays.fill(((DataBufferUShort)buffer).getData(bank), (short)value); break;
        case DataBuffer.TYPE_INT:    Arrays.fill(((DataBufferInt)   buffer).getData(bank),   (int)value); break;
        case DataBuffer.TYPE_FLOAT:  Arrays.fill(((DataBufferFloat) buffer).getData(bank), (float)value); break;
        case DataBuffer.TYPE_DOUBLE: Arrays.fill(((DataBufferDouble)buffer).getData(bank),(double)value); break;
        default: throw new RasterFormatException(String.valueOf(buffer));
    }
}
 

实例 8


/**
 * Creates a Writable subRaster given a region of the Raster. The x and y
 * coordinates specify the horizontal and vertical offsets
 * from the upper-left corner of this Raster to the upper-left corner
 * of the subRaster.  A subset of the bands of the parent Raster may
 * be specified.  If this is null, then all the bands are present in the
 * subRaster. A translation to the subRaster may also be specified.
 * Note that the subRaster will reference the same
 * DataBuffer as the parent Raster, but using different offsets.
 * @param x               X offset.
 * @param y               Y offset.
 * @param width           Width (in pixels) of the subraster.
 * @param height          Height (in pixels) of the subraster.
 * @param x0              Translated X origin of the subraster.
 * @param y0              Translated Y origin of the subraster.
 * @param bandList        Array of band indices.
 * @exception RasterFormatException
 *            if the specified bounding box is outside of the parent Raster.
 */
public WritableRaster createWritableChild(int x, int y,
                                          int width, int height,
                                          int x0, int y0,
                                          int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x+width < x) || (x+width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y+height < y) || (y+height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm,
                                   dataBuffer,
                                   new Rectangle(x0, y0, width, height),
                                   new Point(sampleModelTranslateX+deltaX,
                                             sampleModelTranslateY+deltaY),
                                   this);
}
 

实例 9


@Override
protected void validate()
        {
        ResourceReference<Background> rb = piece.properties.get(PTile.BACKGROUND);
        Background b = rb == null ? null : rb.get();
        BufferedImage bi = b == null ? null : b.getDisplayImage();
        if (bi == null)
                image = EMPTY_IMAGE;
        else
                {
                Point p = piece.getBackgroundPosition();
                Dimension d = piece.getSize();
                try
                        {
                        image = bi.getSubimage(p.x,p.y,d.width,d.height);
                        }
                catch (RasterFormatException e)
                        {
                        image = EMPTY_IMAGE;
                        }
                }
        binVisual.setDepth(this,piece.getDepth());
        Rectangle r = new Rectangle(piece.getRoomPosition(),piece.getSize());
        setBounds(r);
        }
 

实例 10


/**
 * Sets the iterator to the next line in the image,
 * and returns {@code true} if the bottom row of the bounding rectangle has been passed.
 *
 * @return {@code true} if the iteration over lines is finished.
 */
@Override
public boolean nextLineDone() {
    boolean check = src.nextLineDone();
    if (check == dst.nextLineDone()) {
        return check;
    }
    throw new RasterFormatException(ERROR);
}
 

实例 11


/**
 * Creates a Writable subRaster given a region of the Raster. The x and y
 * coordinates specify the horizontal and vertical offsets
 * from the upper-left corner of this Raster to the upper-left corner
 * of the subRaster.  A subset of the bands of the parent Raster may
 * be specified.  If this is null, then all the bands are present in the
 * subRaster. A translation to the subRaster may also be specified.
 * Note that the subRaster will reference the same
 * DataBuffer as the parent Raster, but using different offsets.
 * @param x               X offset.
 * @param y               Y offset.
 * @param width           Width (in pixels) of the subraster.
 * @param height          Height (in pixels) of the subraster.
 * @param x0              Translated X origin of the subraster.
 * @param y0              Translated Y origin of the subraster.
 * @param bandList        Array of band indices.
 * @exception RasterFormatException
 *            if the specified bounding box is outside of the parent Raster.
 */
public WritableRaster createWritableChild(int x, int y,
                                          int width, int height,
                                          int x0, int y0,
                                          int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x+width < x) || (x+width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y+height < y) || (y+height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteInterleavedRaster(sm,
                                   dataBuffer,
                                   new Rectangle(x0, y0, width, height),
                                   new Point(sampleModelTranslateX+deltaX,
                                             sampleModelTranslateY+deltaY),
                                   this);
}
 

实例 12


public static void loadImageChecked(Image img, Component observer) {
  int status;
  for (int failure = 0x80 | 0x40; ((status = observer.checkImage(img,
      observer)) & failure) == 0;) {
    System.out.println((new StringBuilder(String.valueOf(img
        .getWidth(observer)))).append(" ").append(status & 1).append("/")
        .append(img.getHeight(observer)).append(" ").append(status & 2)
        .toString());
    if ((status & 1) > 0 && (status & 2) > 0) {
      observer.prepareImage(img, observer);
      return;
    }
    try {
      Thread.sleep(100L);
    } catch (InterruptedException interruptedexception) {}
  }
  String strStatus = "ABORT";
  if ((status & 0x40) != 0)
    strStatus = "ERROR";
  throw new RasterFormatException((new StringBuilder(
      "Illegal image Format (checkImage: ")).append(strStatus).append(")")
      .toString());
}
 

实例 13


private RgbaAccessor getAccessor(Raster raster, ColorModel cm) {
    RgbaAccessor accessor;
    if (cm instanceof DirectColorModel && cm.getTransferType() == DataBuffer.TYPE_INT) {
        DirectColorModel dcm = (DirectColorModel) cm;
        // check the RGB and BGR masks
        if (dcm.getRedMask() == 0x00FF0000 && dcm.getGreenMask() == 0x0000FF00
                && dcm.getBlueMask() == 0x000000FF
                && (dcm.getNumComponents() == 3 || dcm.getAlphaMask() == 0xFF000000)) {
            accessor = new IntegerRgbAccessor(raster, cm.hasAlpha());
        } else if (dcm.getRedMask() == 0x000000FF && dcm.getGreenMask() == 0x0000FF00
                && dcm.getBlueMask() == 0x00FF0000
                && (dcm.getNumComponents() == 3 || dcm.getAlphaMask() == 0xFF000000)) {
            accessor = new IntegerBgrAccessor(raster, cm.hasAlpha());
        } else {
            throw new RasterFormatException("Color model " + cm
                    + " is not supported, cannot perform color blending on it");
        }
    } else if (cm instanceof ComponentColorModel && cm.getNumColorComponents() == 3) {
        accessor = new ByteRgbAccessor(raster, cm.hasAlpha());
    } else {
        throw new RasterFormatException("Color model " + cm
                + " is not supported, cannot perform color blending on it");
    }
    if (!cm.isAlphaPremultiplied()) {
        accessor = new PremultiplyAccessor(accessor);
    }
    return accessor;
}
 

实例 14


/**
 * {@inheritDoc}
 */
public CompositeContext createContext(ColorModel srcColorModel,
                                      ColorModel dstColorModel,
                                      RenderingHints hints) {
    if (isRgbColorModel(srcColorModel) && isRgbColorModel(dstColorModel)) {
        return new BlendingRgbContext(this);
    } else if (isBgrColorModel(srcColorModel) && isBgrColorModel(dstColorModel)) {
        return new BlendingBgrContext(this);
    }
    throw new RasterFormatException("Incompatible color models");
}
 

实例 15


/**
 * Construct an ImageBuilder instance
 * @param width the width of the image to be built
 * @param height the height of the image to be built
 * @param hasAlpha indicates whether the image has an alpha channel
 * (the selection of alpha channel does not change the memory
 * requirements for the ImageBuilder or resulting BufferedImage.
 */
public ImageBuilder(final int width, final int height, final boolean hasAlpha) {
    if(width<=0){
        throw new RasterFormatException("zero or negative width value");
    }
    if(height<=0){
        throw new RasterFormatException("zero or negative height value");
    }
    data = new int[width * height];
    this.width = width;
    this.height = height;
    this.hasAlpha = hasAlpha;
}
 
讨论
淘淘あ西西 profile image