Java 类java.awt.image.Raster 实例源码

项目:imageio-jnr    文件:TurboJpegImageWriter.java   
@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
  RenderedImage img = image.getRenderedImage();
  if (stream == null) {
    throw new IOException("Set an output first!");
  }
  if (param == null) {
    param = getDefaultWriteParam();
  }
  Rectangle sourceRegion = new Rectangle(0, 0, img.getWidth(), img.getHeight());
  if (param.getSourceRegion() != null) {
    sourceRegion = sourceRegion.intersection(param.getSourceRegion());
  }
  Raster raster = img.getData(sourceRegion);
  int quality = 85;
  if (param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
    quality = (int) (param.getCompressionQuality() * 100);
  }
  try {
    stream.write(lib.encode(raster, quality).array());
  } catch (TurboJpegException e) {
    throw new IOException(e);
  }
}
项目:NoMoreOversleeps    文件:WebcamDefaultDevice.java   
@Override
public BufferedImage getImage()
{

    ByteBuffer buffer = this.getImageBytes();

    if (buffer == null)
    {
        LOG.error("Images bytes buffer is null!");
        return null;
    }

    byte[] bytes = new byte[this.size.width * this.size.height * 3];
    byte[][] data = new byte[][] { bytes };

    buffer.get(bytes);

    DataBufferByte dbuf = new DataBufferByte(data, bytes.length, OFFSET);
    WritableRaster raster = Raster.createWritableRaster(this.smodel, dbuf, null);

    BufferedImage bi = new BufferedImage(this.cmodel, raster, false, null);
    bi.flush();

    return bi;
}
项目:jdk8u-jdk    文件:ByteBandedRaster.java   
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ByteBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY+startY, width, 1, tdata);
        }
    }
项目:OpenJSharp    文件:GradientPaintContext.java   
static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
    if (cached != null) {
        Raster cras = (Raster) cached.get();
        if (cras != null) {
            int cw = cras.getWidth();
            int ch = cras.getHeight();
            int iw = ras.getWidth();
            int ih = ras.getHeight();
            if (cw >= iw && ch >= ih) {
                return;
            }
            if (cw * ch >= iw * ih) {
                return;
            }
        }
    }
    cachedModel = cm;
    cached = new WeakReference<>(ras);
}
项目:OpenJSharp    文件:GradientPaintContext.java   
/**
 * Return a Raster containing the colors generated for the graphics
 * operation.
 * @param x,y,w,h The area in device space for which colors are
 * generated.
 */
public Raster getRaster(int x, int y, int w, int h) {
    double rowrel = (x - x1) * dx + (y - y1) * dy;

    Raster rast = saved;
    if (rast == null || rast.getWidth() < w || rast.getHeight() < h) {
        rast = getCachedRaster(model, w, h);
        saved = rast;
    }
    IntegerComponentRaster irast = (IntegerComponentRaster) rast;
    int off = irast.getDataOffset(0);
    int adjust = irast.getScanlineStride() - w;
    int[] pixels = irast.getDataStorage();

    if (cyclic) {
        cycleFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    } else {
        clipFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    }

    irast.markDirty();

    return rast;
}
项目:jdk8u-jdk    文件:ShortHistogramTest.java   
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
项目:OpenJSharp    文件:ColorPaintContext.java   
public synchronized Raster getRaster(int x, int y, int w, int h) {
    WritableRaster t = savedTile;

    if (t == null || w > t.getWidth() || h > t.getHeight()) {
        t = getColorModel().createCompatibleWritableRaster(w, h);
        IntegerComponentRaster icr = (IntegerComponentRaster) t;
        Arrays.fill(icr.getDataStorage(), color);
        // Note - markDirty is probably unnecessary since icr is brand new
        icr.markDirty();
        if (w <= 64 && h <= 64) {
            savedTile = t;
        }
    }

    return t;
}
项目:OpenJSharp    文件:Blit.java   
public void Blit(SurfaceData srcData,
                 SurfaceData dstData,
                 Composite comp,
                 Region clip,
                 int srcx, int srcy,
                 int dstx, int dsty,
                 int width, int height)
{
    ColorModel srcCM = srcData.getColorModel();
    ColorModel dstCM = dstData.getColorModel();
    // REMIND: Should get RenderingHints from sg2d
    CompositeContext ctx = comp.createContext(srcCM, dstCM,
                                              new RenderingHints(null));
    Raster srcRas = srcData.getRaster(srcx, srcy, width, height);
    WritableRaster dstRas =
        (WritableRaster) dstData.getRaster(dstx, dsty, width, height);

    if (clip == null) {
        clip = Region.getInstanceXYWH(dstx, dsty, width, height);
    }
    int span[] = {dstx, dsty, dstx+width, dsty+height};
    SpanIterator si = clip.getSpanIterator(span);
    srcx -= dstx;
    srcy -= dsty;
    while (si.nextSpan(span)) {
        int w = span[2] - span[0];
        int h = span[3] - span[1];
        Raster tmpSrcRas = srcRas.createChild(srcx + span[0], srcy + span[1],
                                              w, h, 0, 0, null);
        WritableRaster tmpDstRas = dstRas.createWritableChild(span[0], span[1],
                                                              w, h, 0, 0, null);
        ctx.compose(tmpSrcRas, tmpDstRas, tmpDstRas);
    }
    ctx.dispose();
}
项目:jdk8u-jdk    文件:TexturePaintContext.java   
/**
 * Return a Raster containing the colors generated for the graphics
 * operation.
 * @param x,y,w,h The area in device space for which colors are
 * generated.
 */
public Raster getRaster(int x, int y, int w, int h) {
    if (outRas == null ||
        outRas.getWidth() < w ||
        outRas.getHeight() < h)
    {
        // If h==1, we will probably get lots of "scanline" rects
        outRas = makeRaster((h == 1 ? Math.max(w, maxWidth) : w), h);
    }
    double X = mod(xOrg + x * incXAcross + y * incXDown, bWidth);
    double Y = mod(yOrg + x * incYAcross + y * incYDown, bHeight);

    setRaster((int) X, (int) Y, fractAsInt(X), fractAsInt(Y),
              w, h, bWidth, bHeight,
              colincx, colincxerr,
              colincy, colincyerr,
              rowincx, rowincxerr,
              rowincy, rowincyerr);

    SunWritableRaster.markDirty(outRas);

    return outRas;
}
项目:OpenJSharp    文件:ShortComponentRaster.java   
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortComponentRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
项目:openjdk-jdk10    文件:ByteBandedRaster.java   
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ByteBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY+startY, width, 1, tdata);
        }
    }
项目:tf-ispn-demo    文件:MnistListener.java   
/**
 * Converts raw data into JPG image and encode it into Base64 string for sending it to the JS client
 * 
 * @param rawImg
 *            raw image bytes
 * @return Base64 encoded string with JPG image
 */
private String bufferAsJpgString(byte[] rawImg) {
    int[] pixels = new int[rawImg.length];
    for (int i = 0; i < rawImg.length; i++) {
        pixels[i] = (int) rawImg[i];
    }
    DataBufferInt buffer = new DataBufferInt(pixels, pixels.length);
    WritableRaster raster = Raster.createPackedRaster(buffer, IMG_SIZE, IMG_SIZE, IMG_SIZE, BAND_MASKS, null);
    ColorModel cm = ColorModel.getRGBdefault();
    BufferedImage image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);

    byte[] imgBytes = null;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        ImageIO.write(image, "JPG", baos);
        baos.flush();
        imgBytes = baos.toByteArray();
    } catch (IOException e) {
        // TODO log exception
    }

    byte[] encoded = Base64.getEncoder().encode(imgBytes);
    return new String(encoded);
}
项目:jdk8u-jdk    文件:BytePackedRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    // Check if we can use fast code
    if (!(inRaster instanceof BytePackedRaster) ||
        ((BytePackedRaster)inRaster).pixelBitStride != pixelBitStride) {
        super.setDataElements(x, y, inRaster);
        return;
    }

    int srcOffX = inRaster.getMinX();
    int srcOffY = inRaster.getMinY();
    int dstOffX = srcOffX + x;
    int dstOffY = srcOffY + y;
    int width = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height,
                    (BytePackedRaster)inRaster);
}
项目:OpenJSharp    文件:ShortInterleavedRaster.java   
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      REMIND: Do something faster!
//      if (inRaster instanceof ShortInterleavedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
项目:OpenJSharp    文件:ShortBandedRaster.java   
/**
     * Stores the Raster data at the specified location.
     * @param dstX The absolute X coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param dstY The absolute Y coordinate of the destination pixel
     * that will receive a copy of the upper-left pixel of the
     * inRaster
     * @param width      The number of pixels to store horizontally
     * @param height     The number of pixels to store vertically
     * @param inRaster   Raster of data to place at x,y location.
     */
    private void setDataElements(int dstX, int dstY,
                                 int width, int height,
                                 Raster inRaster) {
        // Assume bounds checking has been performed previously
        if (width <= 0 || height <= 0) {
            return;
        }

        // Write inRaster (minX, minY) to (dstX, dstY)

        int srcOffX = inRaster.getMinX();
        int srcOffY = inRaster.getMinY();
        Object tdata = null;

//      // REMIND: Do something faster!
//      if (inRaster instanceof ShortBandedRaster) {
//      }

        for (int startY=0; startY < height; startY++) {
            // Grab one scanline at a time
            tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
                                             width, 1, tdata);
            setDataElements(dstX, dstY + startY, width, 1, tdata);
        }
    }
项目:OpenJSharp    文件:EffectUtils.java   
/**
 * <p>Returns an array of pixels, stored as integers, from a
 * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
 * area defined by a location and two dimensions. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the source image
 * @param x the x location at which to start grabbing pixels
 * @param y the y location at which to start grabbing pixels
 * @param w the width of the rectangle of pixels to grab
 * @param h the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers
 *   otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static int[] getPixels(BufferedImage img,
                              int x, int y, int w, int h, int[] pixels) {
    if (w == 0 || h == 0) {
        return new int[0];
    }

    if (pixels == null) {
        pixels = new int[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        Raster raster = img.getRaster();
        return (int[]) raster.getDataElements(x, y, w, h, pixels);
    }

    // Unmanages the image
    return img.getRGB(x, y, w, h, pixels, 0, w);
}
项目:jdk8u-jdk    文件:GradientPaintContext.java   
/**
 * Return a Raster containing the colors generated for the graphics
 * operation.
 * @param x,y,w,h The area in device space for which colors are
 * generated.
 */
public Raster getRaster(int x, int y, int w, int h) {
    double rowrel = (x - x1) * dx + (y - y1) * dy;

    Raster rast = saved;
    if (rast == null || rast.getWidth() < w || rast.getHeight() < h) {
        rast = getCachedRaster(model, w, h);
        saved = rast;
    }
    IntegerComponentRaster irast = (IntegerComponentRaster) rast;
    int off = irast.getDataOffset(0);
    int adjust = irast.getScanlineStride() - w;
    int[] pixels = irast.getDataStorage();

    if (cyclic) {
        cycleFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    } else {
        clipFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    }

    irast.markDirty();

    return rast;
}
项目:openjdk-jdk10    文件:GradientPaintContext.java   
/**
 * Return a Raster containing the colors generated for the graphics
 * operation.
 * @param x,y,w,h The area in device space for which colors are
 * generated.
 */
public Raster getRaster(int x, int y, int w, int h) {
    double rowrel = (x - x1) * dx + (y - y1) * dy;

    Raster rast = saved;
    if (rast == null || rast.getWidth() < w || rast.getHeight() < h) {
        rast = getCachedRaster(model, w, h);
        saved = rast;
    }
    IntegerComponentRaster irast = (IntegerComponentRaster) rast;
    int off = irast.getDataOffset(0);
    int adjust = irast.getScanlineStride() - w;
    int[] pixels = irast.getDataStorage();

    if (cyclic) {
        cycleFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    } else {
        clipFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
    }

    irast.markDirty();

    return rast;
}
项目:jdk8u-jdk    文件:EdgeNoOpCrash.java   
private static void crashTest() {
    Raster src = createSrcRaster();
    WritableRaster dst = createDstRaster();
    ConvolveOp op = createConvolveOp(ConvolveOp.EDGE_NO_OP);
    try {
        op.filter(src, dst);
    } catch (ImagingOpException e) {
        /*
         * The test pair of source and destination rasters
         * may cause failure of the medialib convolution routine,
         * so this exception is expected.
         *
         * The JVM crash is the only manifestation of this
         * test failure.
         */
    }
    System.out.println("Test PASSED.");
}
项目:openjdk-jdk10    文件:WDataTransferer.java   
/**
 * Translates either a byte array or an input stream which contain
 * platform-specific image data in the given format into an Image.
 */
@Override
protected Image platformImageBytesToImage(byte[] bytes, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return standardImageBytesToImage(bytes, mimeType);
    }

    int[] imageData = platformImageBytesToImageData(bytes, format);
    if (imageData == null) {
        throw new IOException("data translation failed");
    }

    int len = imageData.length - 2;
    int width = imageData[len];
    int height = imageData[len + 1];

    DataBufferInt buffer = new DataBufferInt(imageData, len);
    WritableRaster raster = Raster.createPackedRaster(buffer, width,
            height, width,
            bandmasks, null);

    return new BufferedImage(directColorModel, raster, false, null);
}
项目:GlitchKernel    文件:DataAsSound.java   
@Override
public byte[] glitchPixels(byte[] inputImageBytes) throws Exception 
{
    int audioBitRate = ((Integer) getPixelGlitchParameters().get("bitRateBlend")).intValue();
    float bitRateBlend = (float) audioBitRate / 10;
    if(bitRateBlend < 0.1F || bitRateBlend > 0.9F)
    {
        return null;
    }

    BufferedImage inputImage = ImageUtil.getImageFromBytes(inputImageBytes);
    InputStream imageInputStream = new ByteArrayInputStream(inputImageBytes);
    AudioInputStream distortionAudioStream = new AudioInputStream(imageInputStream, new AudioFormat(AudioFormat.Encoding.ULAW, ThreadLocalRandom.current().nextInt(8000,  20000), 8, 5, 9, ThreadLocalRandom.current().nextInt(8000,  20000), true), inputImageBytes.length);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    AudioSystem.write(distortionAudioStream, Type.WAVE, outputStream);
    BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    byte[] imageData = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData();
    System.arraycopy(outputStream.toByteArray(),0,imageData,0,outputStream.toByteArray().length);
    int[] abgrOffsets = {3, 2, 1, 0}; 
    DataBuffer outputBuffer = new DataBufferByte(imageData, imageData.length);
    WritableRaster raster = Raster.createInterleavedRaster(outputBuffer, inputImage.getWidth(), inputImage.getHeight(), 4 * inputImage.getWidth(), 4, abgrOffsets, null);
    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    BufferedImage rasterizedImage = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    rasterizedImage = resizeImage(rasterizedImage, inputImage.getWidth() * 4, inputImage.getHeight() * 4);
    Graphics2D g2d = rasterizedImage.createGraphics();
    g2d.setComposite(AlphaComposite.SrcOver.derive(bitRateBlend));
    g2d.drawImage(inputImage, 0, 0, null);
    g2d.dispose();
    rasterizedImage = rasterizedImage.getSubimage(0, 0, inputImage.getWidth(), inputImage.getHeight());
    return ImageUtil.getImageBytes(rasterizedImage);
}
项目:openjdk-jdk10    文件:EdgeNoOpCrash.java   
private static void crashTest() {
    Raster src = createSrcRaster();
    WritableRaster dst = createDstRaster();
    ConvolveOp op = createConvolveOp(ConvolveOp.EDGE_NO_OP);
    try {
        op.filter(src, dst);
    } catch (ImagingOpException e) {
        /*
         * The test pair of source and destination rasters
         * may cause failure of the medialib convolution routine,
         * so this exception is expected.
         *
         * The JVM crash is the only manifestation of this
         * test failure.
         */
    }
    System.out.println("Test PASSED.");
}
项目:Svg2AndroidXml    文件:AssetUtil.java   
/**
 * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image).
 *
 * @param source The source image.
 * @return A new, trimmed image, or the source image if no trim is performed.
 */
public static BufferedImage trimmedImage(BufferedImage source) {
    final int minAlpha = 1;
    final int srcWidth = source.getWidth();
    final int srcHeight = source.getHeight();
    Raster raster = source.getRaster();
    int l = srcWidth, t = srcHeight, r = 0, b = 0;
    int alpha, x, y;
    int[] pixel = new int[4];
    for (y = 0; y < srcHeight; y++) {
        for (x = 0; x < srcWidth; x++) {
            raster.getPixel(x, y, pixel);
            alpha = pixel[3];
            if (alpha >= minAlpha) {
                l = Math.min(x, l);
                t = Math.min(y, t);
                r = Math.max(x, r);
                b = Math.max(y, b);
            }
        }
    }
    if (l > r || t > b) {
        // No pixels, couldn't trim
        return source;
    }
    return source.getSubimage(l, t, r - l + 1, b - t + 1);
}
项目:OpenJSharp    文件:GradientPaintContext.java   
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
    if (cm == cachedModel) {
        if (cached != null) {
            Raster ras = (Raster) cached.get();
            if (ras != null &&
                ras.getWidth() >= w &&
                ras.getHeight() >= h)
            {
                cached = null;
                return ras;
            }
        }
    }
    return cm.createCompatibleWritableRaster(w, h);
}
项目:openjdk-jdk10    文件:SunToolkit.java   
public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
    BufferedImage bimage = getScaledIconImage(imageList, width, height);
    if (bimage == null) {
        return null;
    }
    Raster raster = bimage.getRaster();
    DataBuffer buffer = raster.getDataBuffer();
    return (DataBufferInt)buffer;
}
项目:openjdk-jdk10    文件:CustomCompositeTest.java   
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
项目:imageio-jnr    文件:OpenJp2ImageWriter.java   
@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
  RenderedImage img = image.getRenderedImage();
  if (param == null) {
    param = getDefaultWriteParam();
  }
  Rectangle sourceRegion = new Rectangle(0, 0, img.getWidth(), img.getHeight());
  if (param.getSourceRegion() != null) {
    sourceRegion = sourceRegion.intersection(param.getSourceRegion());
  }
  Raster raster = img.getData(sourceRegion);
  opj_cparameters cparams = ((OpenJp2ImageWriteParam) param).toNativeParams(lib);
  lib.encode(raster, this.wrapper, cparams);
}
项目:openjdk-jdk10    文件:BMPSubsamplingTest.java   
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel colorModel =
        new ComponentColorModel(cs, nBits,
                                false, false,
                                Transparency.OPAQUE,
                                DataBuffer.TYPE_BYTE);
    WritableRaster raster =
        Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                       w, h,
                                       w*3, 3,
                                       bOffs, null);
    return new BufferedImage(colorModel, raster, false, null);
}
项目:jdk8u-jdk    文件:IncorrectSampleMaskTest.java   
private static void doTest(int dataType) {
    int maxSize = DataBuffer.getDataTypeSize(dataType);
    System.out.println("Type size: " + maxSize);

    int theMask = (int)(1L << (maxSize + 2)) - 1;
    System.out.printf("theMask=%x\n", theMask);

    SinglePixelPackedSampleModel sm =
        new SinglePixelPackedSampleModel(dataType, w, h,
                                         new int[] { theMask });


    int[] sampleSize = sm.getSampleSize();
    for (int s : sampleSize) {
        if (s > maxSize) {
            throw new RuntimeException("Test failed: sample size is too big:" + s);
        }
    }

    System.out.println("Test medialib...");
    DataBuffer buf = createDataBuffer(dataType);

    WritableRaster wr = Raster.createWritableRaster(sm, buf, null);

    op.filter(wr, null);
    System.out.println("Test PASSED.");
}
项目:OpenJSharp    文件:ByteBandedRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinate is out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = inRaster.getMinX() + x;
    int dstOffY = inRaster.getMinY() + y;
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
项目:openjdk-jdk10    文件:TIFFRenderedImage.java   
public WritableRaster copyData(WritableRaster raster) {
    if (raster == null) {
        return read(new Rectangle(0, 0, getWidth(), getHeight()));
    } else {
        Raster src = read(raster.getBounds());
        raster.setRect(src);
        return raster;
    }
}
项目:openjdk-jdk10    文件:ByteComponentRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = inRaster.getMinX() + x;
    int dstOffY = inRaster.getMinY() + y;
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
项目:jdk8u-jdk    文件:Win32ColorModel24.java   
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
项目:OpenJSharp    文件:ShortInterleavedRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
项目:OpenJSharp    文件:ShortBandedRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
项目:openjdk-jdk10    文件:InvalidTransformParameterTest.java   
public static Raster testRasterTransform(Raster src, WritableRaster dst,
                                         AffineTransform transform) {
    AffineTransformOp op =
            new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);

    return op.filter(src, dst);
}
项目:openjdk-jdk10    文件:Blit.java   
public void Blit(SurfaceData srcData,
                 SurfaceData dstData,
                 Composite comp,
                 Region clip,
                 int srcx, int srcy,
                 int dstx, int dsty,
                 int width, int height)
{
    ColorModel srcCM = srcData.getColorModel();
    ColorModel dstCM = dstData.getColorModel();
    // REMIND: Should get RenderingHints from sg2d
    CompositeContext ctx = comp.createContext(srcCM, dstCM,
                                              new RenderingHints(null));
    Raster srcRas = srcData.getRaster(srcx, srcy, width, height);
    WritableRaster dstRas =
        (WritableRaster) dstData.getRaster(dstx, dsty, width, height);

    if (clip == null) {
        clip = Region.getInstanceXYWH(dstx, dsty, width, height);
    }
    int span[] = {dstx, dsty, dstx+width, dsty+height};
    SpanIterator si = clip.getSpanIterator(span);
    srcx -= dstx;
    srcy -= dsty;
    while (si.nextSpan(span)) {
        int w = span[2] - span[0];
        int h = span[3] - span[1];
        Raster tmpSrcRas = srcRas.createChild(srcx + span[0], srcy + span[1],
                                              w, h, 0, 0, null);
        WritableRaster tmpDstRas = dstRas.createWritableChild(span[0], span[1],
                                                              w, h, 0, 0, null);
        ctx.compose(tmpSrcRas, tmpDstRas, tmpDstRas);
    }
    ctx.dispose();
}
项目:openjdk-jdk10    文件:SingleTileRenderedImage.java   
/**
 * Constructs a SingleTileRenderedImage based on a Raster
 * and a ColorModel.
 *
 * @param ras A Raster that will define tile (0, 0) of the image.
 * @param colorModel A ColorModel that will serve as the image's
 *           ColorModel.
 */
public SingleTileRenderedImage(Raster ras, ColorModel colorModel) {
    this.ras = ras;

    this.tileGridXOffset = this.minX = ras.getMinX();
    this.tileGridYOffset = this.minY = ras.getMinY();
    this.tileWidth = this.width = ras.getWidth();
    this.tileHeight = this.height = ras.getHeight();
    this.sampleModel = ras.getSampleModel();
    this.colorModel = colorModel;
}
项目:jdk8u-jdk    文件:ByteComponentRaster.java   
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = inRaster.getMinX() + x;
    int dstOffY = inRaster.getMinY() + y;
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
项目:jdk8u-jdk    文件:IntegerComponentRaster.java   
/**
 * Stores the Raster data at the specified location.
 * The transferType of the inputRaster must match this raster.
 * An ArrayIndexOutOfBoundsException will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}