Search in sources :

Example 21 with Raster

use of java.awt.image.Raster in project jdk8u_jdk by JetBrains.

the class GeneralCompositePipe method renderPathTile.

/**
    * GeneralCompositePipe.renderPathTile works with custom composite operator
    * provided by an application
    */
public void renderPathTile(Object ctx, byte[] atile, int offset, int tilesize, int x, int y, int w, int h) {
    TileContext context = (TileContext) ctx;
    PaintContext paintCtxt = context.paintCtxt;
    CompositeContext compCtxt = context.compCtxt;
    SunGraphics2D sg = context.sunG2D;
    Raster srcRaster = paintCtxt.getRaster(x, y, w, h);
    ColorModel paintModel = paintCtxt.getColorModel();
    Raster dstRaster;
    Raster dstIn;
    WritableRaster dstOut;
    SurfaceData sd = sg.getSurfaceData();
    dstRaster = sd.getRaster(x, y, w, h);
    if (dstRaster instanceof WritableRaster && atile == null) {
        dstOut = (WritableRaster) dstRaster;
        dstOut = dstOut.createWritableChild(x, y, w, h, 0, 0, null);
        dstIn = dstOut;
    } else {
        dstIn = dstRaster.createChild(x, y, w, h, 0, 0, null);
        dstOut = dstIn.createCompatibleWritableRaster();
    }
    compCtxt.compose(srcRaster, dstIn, dstOut);
    if (dstRaster != dstOut && dstOut.getParent() != dstRaster) {
        if (dstRaster instanceof WritableRaster && atile == null) {
            ((WritableRaster) dstRaster).setDataElements(x, y, dstOut);
        } else {
            ColorModel cm = sg.getDeviceColorModel();
            BufferedImage resImg = new BufferedImage(cm, dstOut, cm.isAlphaPremultiplied(), null);
            SurfaceData resData = BufImgSurfaceData.createData(resImg);
            if (atile == null) {
                Blit blit = Blit.getFromCache(resData.getSurfaceType(), CompositeType.SrcNoEa, sd.getSurfaceType());
                blit.Blit(resData, sd, AlphaComposite.Src, null, 0, 0, x, y, w, h);
            } else {
                MaskBlit blit = MaskBlit.getFromCache(resData.getSurfaceType(), CompositeType.SrcNoEa, sd.getSurfaceType());
                blit.MaskBlit(resData, sd, AlphaComposite.Src, null, 0, 0, x, y, w, h, atile, offset, tilesize);
            }
        }
    }
}
Also used : SurfaceData(sun.java2d.SurfaceData) BufImgSurfaceData(sun.awt.image.BufImgSurfaceData) CompositeContext(java.awt.CompositeContext) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) MaskBlit(sun.java2d.loops.MaskBlit) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Blit(sun.java2d.loops.Blit) MaskBlit(sun.java2d.loops.MaskBlit) PaintContext(java.awt.PaintContext) SunGraphics2D(sun.java2d.SunGraphics2D) BufferedImage(java.awt.image.BufferedImage)

Example 22 with Raster

use of java.awt.image.Raster in project jdk8u_jdk by JetBrains.

the class XorCopyArgbToAny method Blit.

public void Blit(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int srcx, int srcy, int dstx, int dsty, int w, int h) {
    Raster srcRast = src.getRaster(srcx, srcy, w, h);
    ColorModel srcCM = src.getColorModel();
    Raster dstRast = dst.getRaster(dstx, dsty, w, h);
    IntegerComponentRaster icr = (IntegerComponentRaster) dstRast;
    int[] dstPix = icr.getDataStorage();
    Region roi = CustomComponent.getRegionOfInterest(src, dst, clip, srcx, srcy, dstx, dsty, w, h);
    SpanIterator si = roi.getSpanIterator();
    Object srcPix = null;
    int dstScan = icr.getScanlineStride();
    // assert(icr.getPixelStride() == 1);
    srcx -= dstx;
    srcy -= dsty;
    int[] span = new int[4];
    while (si.nextSpan(span)) {
        int rowoff = icr.getDataOffset(0) + span[1] * dstScan + span[0];
        for (int y = span[1]; y < span[3]; y++) {
            int off = rowoff;
            for (int x = span[0]; x < span[2]; x++) {
                srcPix = srcRast.getDataElements(x + srcx, y + srcy, srcPix);
                dstPix[off++] = srcCM.getRGB(srcPix);
            }
            rowoff += dstScan;
        }
    }
    // Pixels in the dest were modified directly, we must
    // manually notify the raster that it was modified
    icr.markDirty();
// REMIND: We need to do something to make sure that dstRast
// is put back to the destination (as in the native Release
// function)
// src.releaseRaster(srcRast);  // NOP?
// dst.releaseRaster(dstRast);
}
Also used : SpanIterator(sun.java2d.pipe.SpanIterator) ColorModel(java.awt.image.ColorModel) Raster(java.awt.image.Raster) IntegerComponentRaster(sun.awt.image.IntegerComponentRaster) WritableRaster(java.awt.image.WritableRaster) IntegerComponentRaster(sun.awt.image.IntegerComponentRaster) Region(sun.java2d.pipe.Region)

Example 23 with Raster

use of java.awt.image.Raster in project graphhopper by graphhopper.

the class CGIARProvider method getEle.

@Override
public double getEle(double lat, double lon) {
    // no data we can avoid the trouble
    if (lat > 60 || lat < -60)
        return 0;
    lat = (int) (lat * precision) / precision;
    lon = (int) (lon * precision) / precision;
    String name = getFileName(lat, lon);
    HeightTile demProvider = cacheData.get(name);
    if (demProvider == null) {
        if (!cacheDir.exists())
            cacheDir.mkdirs();
        int minLat = down(lat);
        int minLon = down(lon);
        // less restrictive against boundary checking
        demProvider = new HeightTile(minLat, minLon, WIDTH, degree * precision, degree);
        demProvider.setCalcMean(calcMean);
        cacheData.put(name, demProvider);
        DataAccess heights = getDirectory().find(name + ".gh");
        demProvider.setHeights(heights);
        boolean loadExisting = false;
        try {
            loadExisting = heights.loadExisting();
        } catch (Exception ex) {
            logger.warn("cannot load " + name + ", error: " + ex.getMessage());
        }
        if (!loadExisting) {
            String tifName = name + ".tif";
            String zippedURL = baseUrl + "/" + name + ".zip";
            File file = new File(cacheDir, new File(zippedURL).getName());
            // get zip file if not already in cacheDir - unzip later and in-memory only!
            if (!file.exists()) {
                try {
                    int max = 3;
                    for (int trial = 0; trial < max; trial++) {
                        try {
                            downloader.downloadFile(zippedURL, file.getAbsolutePath());
                            break;
                        } catch (SocketTimeoutException ex) {
                            // just try again after a little nap
                            Thread.sleep(sleep);
                            if (trial >= max - 1)
                                throw ex;
                            continue;
                        } catch (IOException ex) {
                            demProvider.setSeaLevel(true);
                            // use small size on disc and in-memory
                            heights.setSegmentSize(100).create(10).flush();
                            return 0;
                        }
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
            // short == 2 bytes
            heights.create(2 * WIDTH * WIDTH);
            // logger.info("start decoding");
            // decode tiff data
            Raster raster;
            SeekableStream ss = null;
            try {
                InputStream is = new FileInputStream(file);
                ZipInputStream zis = new ZipInputStream(is);
                // find tif file in zip
                ZipEntry entry = zis.getNextEntry();
                while (entry != null && !entry.getName().equals(tifName)) {
                    entry = zis.getNextEntry();
                }
                ss = SeekableStream.wrapInputStream(zis, true);
                TIFFImageDecoder imageDecoder = new TIFFImageDecoder(ss, new TIFFDecodeParam());
                raster = imageDecoder.decodeAsRaster();
            } catch (Exception e) {
                throw new RuntimeException("Can't decode " + tifName, e);
            } finally {
                if (ss != null)
                    Helper.close(ss);
            }
            // logger.info("start converting to our format");
            final int height = raster.getHeight();
            final int width = raster.getWidth();
            int x = 0, y = 0;
            try {
                for (y = 0; y < height; y++) {
                    for (x = 0; x < width; x++) {
                        short val = (short) raster.getPixel(x, y, (int[]) null)[0];
                        if (val < -1000 || val > 12000)
                            val = Short.MIN_VALUE;
                        heights.setShort(2 * (y * WIDTH + x), val);
                    }
                }
                heights.flush();
            // TODO remove tifName and zip?
            } catch (Exception ex) {
                throw new RuntimeException("Problem at x:" + x + ", y:" + y, ex);
            }
        }
    // loadExisting
    }
    if (demProvider.isSeaLevel())
        return 0;
    return demProvider.getHeight(lat, lon);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Raster(java.awt.image.Raster) ZipEntry(java.util.zip.ZipEntry) SeekableStream(org.apache.xmlgraphics.image.codec.util.SeekableStream) IOException(java.io.IOException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) FileInputStream(java.io.FileInputStream) DataAccess(com.graphhopper.storage.DataAccess) ZipInputStream(java.util.zip.ZipInputStream) SocketTimeoutException(java.net.SocketTimeoutException) TIFFImageDecoder(org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder) File(java.io.File) TIFFDecodeParam(org.apache.xmlgraphics.image.codec.tiff.TIFFDecodeParam)

Example 24 with Raster

use of java.awt.image.Raster in project jna by java-native-access.

the class AlphaMaskDemo method updateW32.

private void updateW32(boolean a, boolean i) {
    User32 user = User32.INSTANCE;
    GDI32 gdi = GDI32.INSTANCE;
    HWND hWnd = null;
    if (!alphaWindow.isDisplayable()) {
        alphaWindow.pack();
        hWnd = getHwnd(alphaWindow);
        int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
        flags |= WinUser.WS_EX_LAYERED;
        user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags);
        Window parent = alphaWindow.getOwner();
        Point where = parent.getLocationOnScreen();
        where.translate(parent.getWidth(), 0);
        alphaWindow.setLocation(where);
    } else {
        hWnd = getHwnd(alphaWindow);
    }
    if (i) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        HDC screenDC = user.GetDC(null);
        HDC memDC = gdi.CreateCompatibleDC(screenDC);
        HBITMAP hBitmap = null;
        HANDLE oldBitmap = null;
        try {
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            BITMAPINFO bmi = new BITMAPINFO();
            bmi.bmiHeader.biWidth = w;
            bmi.bmiHeader.biHeight = h;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 32;
            bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
            bmi.bmiHeader.biSizeImage = w * h * 4;
            PointerByReference ppbits = new PointerByReference();
            hBitmap = gdi.CreateDIBSection(memDC, bmi, WinGDI.DIB_RGB_COLORS, ppbits, null, 0);
            oldBitmap = gdi.SelectObject(memDC, hBitmap);
            Pointer pbits = ppbits.getValue();
            Raster raster = buf.getData();
            int[] pixel = new int[4];
            int[] bits = new int[w * h];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    raster.getPixel(x, h - y - 1, pixel);
                    int alpha = (pixel[3] & 0xFF) << 24;
                    int red = (pixel[2] & 0xFF);
                    int green = (pixel[1] & 0xFF) << 8;
                    int blue = (pixel[0] & 0xFF) << 16;
                    bits[x + y * w] = alpha | red | green | blue;
                }
            }
            pbits.write(0, bits, 0, bits.length);
            SIZE size = new SIZE();
            size.cx = w;
            size.cy = h;
            POINT loc = new POINT();
            loc.x = alphaWindow.getX();
            loc.y = alphaWindow.getY();
            POINT srcLoc = new POINT();
            BLENDFUNCTION blend = new BLENDFUNCTION();
            blend.SourceConstantAlpha = (byte) (alpha * 255);
            blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
            user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC, srcLoc, 0, blend, WinUser.ULW_ALPHA);
        } finally {
            user.ReleaseDC(null, screenDC);
            if (hBitmap != null) {
                gdi.SelectObject(memDC, oldBitmap);
                gdi.DeleteObject(hBitmap);
            }
            gdi.DeleteDC(memDC);
        }
    } else if (a) {
        BLENDFUNCTION blend = new BLENDFUNCTION();
        blend.SourceConstantAlpha = (byte) (alpha * 255);
        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
        user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA);
    }
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) GDI32(com.sun.jna.platform.win32.GDI32) BLENDFUNCTION(com.sun.jna.platform.win32.WinUser.BLENDFUNCTION) HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) Raster(java.awt.image.Raster) SIZE(com.sun.jna.platform.win32.WinUser.SIZE) Pointer(com.sun.jna.Pointer) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) PointerByReference(com.sun.jna.ptr.PointerByReference) User32(com.sun.jna.platform.win32.User32) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) POINT(com.sun.jna.platform.win32.WinDef.POINT) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 25 with Raster

use of java.awt.image.Raster in project intellij-code-outline by sitano.

the class CodeOutlineImage method moveDataDown.

/**
     * Copies data between the given images, starting at the bottom of the
     * images and copying then moving upwards line by line. This method is
     * suitable for when the two given images are subimages of the same image,
     * and their data may overlap.
     *
     * @param from the source image
     * @param to the destination image
     */
private static void moveDataDown(BufferedImage from, BufferedImage to) {
    Raster raster = from.getRaster();
    WritableRaster outRaster = to.getRaster();
    int width = outRaster.getWidth();
    int height = outRaster.getHeight();
    int startX = outRaster.getMinX();
    int startY = outRaster.getMinY();
    Object tdata = null;
    for (int i = startY + height - 1; i >= startY; i--) {
        tdata = raster.getDataElements(startX, i, width, 1, tdata);
        outRaster.setDataElements(startX, i, width, 1, tdata);
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster)

Aggregations

Raster (java.awt.image.Raster)43 WritableRaster (java.awt.image.WritableRaster)22 ColorModel (java.awt.image.ColorModel)16 BufferedImage (java.awt.image.BufferedImage)15 IndexColorModel (java.awt.image.IndexColorModel)7 SampleModel (java.awt.image.SampleModel)6 Point (java.awt.Point)5 Rectangle (java.awt.Rectangle)5 DirectColorModel (java.awt.image.DirectColorModel)4 ByteBuffer (java.nio.ByteBuffer)4 Paint (java.awt.Paint)3 DataBufferByte (java.awt.image.DataBufferByte)3 ByteComponentRaster (sun.awt.image.ByteComponentRaster)3 IntegerComponentRaster (sun.awt.image.IntegerComponentRaster)3 SurfaceData (sun.java2d.SurfaceData)3 CompositeContext (java.awt.CompositeContext)2 GradientPaint (java.awt.GradientPaint)2 Graphics (java.awt.Graphics)2 LinearGradientPaint (java.awt.LinearGradientPaint)2 PaintContext (java.awt.PaintContext)2