Search in sources :

Example 41 with DataBufferInt

use of java.awt.image.DataBufferInt in project android_frameworks_base by crdroidandroid.

the class ShadowPainter method createDropShadow.

/**
     * Creates a drop shadow of a given image and returns a new image which shows the input image on
     * top of its drop shadow.
     * <p/>
     * <b>NOTE: If the shape is rectangular and opaque, consider using {@link
     * #drawRectangleShadow(Graphics2D, int, int, int, int)} instead.</b>
     *
     * @param source the source image to be shadowed
     * @param shadowSize the size of the shadow in pixels
     * @param shadowOpacity the opacity of the shadow, with 0=transparent and 1=opaque
     * @param shadowRgb the RGB int to use for the shadow color
     *
     * @return a new image with the source image on top of its shadow when shadowSize > 0 or the
     * source image otherwise
     */
// Imported code
@SuppressWarnings({ "SuspiciousNameCombination", "UnnecessaryLocalVariable" })
public static BufferedImage createDropShadow(BufferedImage source, int shadowSize, float shadowOpacity, int shadowRgb) {
    if (shadowSize <= 0) {
        return source;
    }
    // This code is based on
    //      http://www.jroller.com/gfx/entry/non_rectangular_shadow
    BufferedImage image;
    int width = source.getWidth();
    int height = source.getHeight();
    image = new BufferedImage(width + SHADOW_SIZE, height + SHADOW_SIZE, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.drawImage(image, shadowSize, shadowSize, null);
    int dstWidth = image.getWidth();
    int dstHeight = image.getHeight();
    int left = (shadowSize - 1) >> 1;
    int right = shadowSize - left;
    int xStart = left;
    int xStop = dstWidth - right;
    int yStart = left;
    int yStop = dstHeight - right;
    shadowRgb &= 0x00FFFFFF;
    int[] aHistory = new int[shadowSize];
    int historyIdx;
    int aSum;
    int[] dataBuffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    int lastPixelOffset = right * dstWidth;
    float sumDivider = shadowOpacity / shadowSize;
    // horizontal pass
    for (int y = 0, bufferOffset = 0; y < dstHeight; y++, bufferOffset = y * dstWidth) {
        aSum = 0;
        historyIdx = 0;
        for (int x = 0; x < shadowSize; x++, bufferOffset++) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[x] = a;
            aSum += a;
        }
        bufferOffset -= right;
        for (int x = xStart; x < xStop; x++, bufferOffset++) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;
            // subtract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];
            // get the latest pixel
            a = dataBuffer[bufferOffset + right] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;
            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
    // vertical pass
    for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
        aSum = 0;
        historyIdx = 0;
        for (int y = 0; y < shadowSize; y++, bufferOffset += dstWidth) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[y] = a;
            aSum += a;
        }
        bufferOffset -= lastPixelOffset;
        for (int y = yStart; y < yStop; y++, bufferOffset += dstWidth) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;
            // subtract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];
            // get the latest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;
            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
    g2.drawImage(source, null, 0, 0);
    g2.dispose();
    return image;
}
Also used : DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 42 with DataBufferInt

use of java.awt.image.DataBufferInt in project vcell by virtualcell.

the class ImagePaneModel method updateViewPortImage.

/**
 * Gets the zoomImage property (java.awt.Image) value.
 * @return The zoomImage property value.
 */
public void updateViewPortImage() {
    Dimension currentDimension = getDimension();
    boolean bUpdateImage = true;
    if (getSourceData() == null || (getDisplayAdapterService() == null && getSourceData().needsColorConversion()) || getViewport() == null || currentDimension == null) {
        // We don't have enough info to update image
        return;
    }
    // Enforce image always fills viewport
    // if(changeZoomToFillViewport() == true){
    // return;
    // }
    // 
    // Make a new viewPort image
    // Find out where the Viewport points to in the sourceData and the final DimensionedData data
    // Account for the image edges being inside the viewport
    int viewPortImageXStart = viewPortImageIndex(fieldViewport.x, currentDimension.width);
    int viewPortXStart = viewPortIndex(fieldViewport.x, fieldViewport.width, currentDimension.width);
    int viewPortImageXEnd = viewPortImageXStart + Math.min(currentDimension.width - viewPortImageXStart, fieldViewport.width - viewPortXStart);
    // 
    int viewPortImageYStart = viewPortImageIndex(fieldViewport.y, currentDimension.height);
    int viewPortYStart = viewPortIndex(fieldViewport.y, fieldViewport.height, currentDimension.height);
    int viewPortImageYEnd = viewPortImageYStart + Math.min(currentDimension.height - viewPortImageYStart, fieldViewport.height - viewPortYStart);
    // 
    // Calculate the starting index into the sourceData and DimensionedData
    int viewPortStartIndex = viewPortXStart + viewPortYStart * fieldViewport.width;
    int sourceStartIndex = fieldSourceData.getStartIndex() + convertToImageIndex(viewPortImageXStart) * fieldSourceData.getXIncrement() + convertToImageIndex(viewPortImageYStart) * fieldSourceData.getYIncrement();
    // 
    // The viewPortImage internal buffer we will write to directly
    // We can do this because our bufferimage was made to have an int[] as its internal buffer
    int[] viewport = ((DataBufferInt) (getViewPortImage().getRaster().getDataBuffer())).getData();
    // fill viewport with background color
    java.util.Arrays.fill(viewport, getBackgroundColor().getRGB());
    // 
    if (viewPortImageXStart != -1 && viewPortXStart != -1 && viewPortImageYStart != -1 && viewPortYStart != -1) {
        // The image is visible inside the viewport so fill it in
        int lastRowIndex = 0;
        int modFactorCompareX = 0;
        int modFactorX = 0;
        int modFactorCompareY = 0;
        if (fieldMode == NORMAL_MODE) {
            modFactorCompareX = fieldZoom * 2;
            modFactorX = viewPortImageXStart % modFactorCompareX;
            modFactorCompareY = fieldZoom * 2;
        } else if (fieldMode == MESH_MODE) {
            if (getSourceData().getXSize() == 1) {
                // guaranteed not to use next column
                modFactorCompareX = 5000;
                modFactorX = viewPortImageXStart % modFactorCompareX;
            } else {
                modFactorCompareX = 2 * fieldZoom;
                modFactorX = (viewPortImageXStart + fieldZoom) % modFactorCompareX;
            }
            if (getSourceData().getYSize() == 1) {
                // guaranteed not to use next row
                modFactorCompareY = 5000;
            } else {
                modFactorCompareY = 2 * fieldZoom;
            }
        }
        // 
        boolean bMakeNewRow = true;
        for (int y = viewPortImageYStart; y < viewPortImageYEnd; y += 1) {
            // 
            if (fieldMode == NORMAL_MODE) {
                bMakeNewRow = (((y % modFactorCompareY) == 0) || (y == viewPortImageYStart));
            } else if (fieldMode == MESH_MODE) {
                bMakeNewRow = ((((y + fieldZoom) % modFactorCompareY) == 0) || (y == viewPortImageYStart));
            }
            // 
            if (bMakeNewRow) {
                switch(getSourceData().getType()) {
                    case SourceDataInfo.INT_RGB_TYPE:
                        updateRowFromIntRGB(viewport, viewPortImageXStart, viewPortImageXEnd, modFactorX, modFactorCompareX, sourceStartIndex, viewPortStartIndex);
                        break;
                    case SourceDataInfo.INDEX_TYPE:
                        updateRowFromColorIndex(viewport, viewPortImageXStart, viewPortImageXEnd, modFactorX, modFactorCompareX, sourceStartIndex, viewPortStartIndex);
                        break;
                    case SourceDataInfo.RAW_VALUE_TYPE:
                        updateRowFromRawValues(viewport, viewPortImageXStart, viewPortImageXEnd, modFactorX, modFactorCompareX, sourceStartIndex, viewPortStartIndex);
                        break;
                }
                // 
                lastRowIndex = viewPortStartIndex;
                sourceStartIndex += fieldSourceData.getYIncrement();
            } else {
                System.arraycopy(viewport, lastRowIndex, viewport, viewPortStartIndex, viewPortImageXEnd - viewPortImageXStart);
            }
            // 
            viewPortStartIndex += fieldViewport.width;
        }
        // 
        setViewPortImage(getViewPortImage());
    }
}
Also used : Dimension(java.awt.Dimension) DataBufferInt(java.awt.image.DataBufferInt) Point(java.awt.Point)

Example 43 with DataBufferInt

use of java.awt.image.DataBufferInt in project vcell by virtualcell.

the class PDEOffscreenRenderer method getPixelsRGB.

/**
 * Insert the method's description here.
 * Creation date: (3/1/2001 11:54:44 PM)
 * @return int[]
 */
public int[] getPixelsRGB(int imageScale, int membrScale, int meshMode, int volVarMembrOutlineThickness) {
    if (getServerPDEDataContext().getDataIdentifier().getVariableType().equals(VariableType.VOLUME) || getServerPDEDataContext().getDataIdentifier().getVariableType().equals(VariableType.POSTPROCESSING)) {
        CartesianMesh mesh = getServerPDEDataContext().getCartesianMesh();
        MeshDisplayAdapter meshDisplayAdapter = new MeshDisplayAdapter(mesh);
        BufferedImage bufferedImage = getScaledRGBVolume(mesh, meshMode, imageScale, false);
        // 
        if (!getServerPDEDataContext().getDataIdentifier().getVariableType().equals(VariableType.POSTPROCESSING) && volVarMembrOutlineThickness > 0) {
            cbit.vcell.geometry.CurveRenderer curveRenderer = new cbit.vcell.geometry.CurveRenderer(getDisplayAdapterService());
            curveRenderer.setNormalAxis(getNormalAxis());
            org.vcell.util.Origin origin = mesh.getOrigin();
            org.vcell.util.Extent extent = mesh.getExtent();
            curveRenderer.setWorldOrigin(new org.vcell.util.Coordinate(origin.getX(), origin.getY(), origin.getZ()));
            Coordinate pixeldelta = getPixelDelta(extent, mesh, meshMode, imageScale);
            curveRenderer.setWorldDelta(new org.vcell.util.Coordinate(pixeldelta.getX(), pixeldelta.getY(), pixeldelta.getZ()));
            Hashtable<SampledCurve, int[]> curvesAndMembraneIndexes = meshDisplayAdapter.getCurvesAndMembraneIndexes(getNormalAxis(), getSlice());
            if (curvesAndMembraneIndexes != null) {
                Curve[] curves = (Curve[]) org.vcell.util.BeanUtils.getArray(curvesAndMembraneIndexes.keys(), Curve.class);
                for (int i = 0; curves != null && i < curves.length; i++) {
                    curveRenderer.addCurve(curves[i]);
                    curveRenderer.renderPropertySegmentColors(curves[i], null);
                    curveRenderer.renderPropertyLineWidthMultiplier(curves[i], volVarMembrOutlineThickness);
                }
                Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
                // must be false or could get more than 256 colors
                curveRenderer.setAntialias(false);
                curveRenderer.draw(g);
            }
        }
        return ((DataBufferInt) bufferedImage.getData().getDataBuffer()).getData();
    } else if (getServerPDEDataContext().getDataIdentifier().getVariableType().equals(VariableType.MEMBRANE)) {
        CartesianMesh mesh = getServerPDEDataContext().getCartesianMesh();
        MeshDisplayAdapter meshDisplayAdapter = new MeshDisplayAdapter(mesh);
        BufferedImage bufferedImage = getScaledRGBVolume(mesh, meshMode, imageScale, true);
        // 
        // apply curve renderer
        // 
        cbit.vcell.geometry.CurveRenderer curveRenderer = new cbit.vcell.geometry.CurveRenderer(getDisplayAdapterService());
        curveRenderer.setNormalAxis(getNormalAxis());
        org.vcell.util.Origin origin = mesh.getOrigin();
        org.vcell.util.Extent extent = mesh.getExtent();
        curveRenderer.setWorldOrigin(new org.vcell.util.Coordinate(origin.getX(), origin.getY(), origin.getZ()));
        Coordinate pixeldelta = getPixelDelta(extent, mesh, meshMode, imageScale);
        curveRenderer.setWorldDelta(new org.vcell.util.Coordinate(pixeldelta.getX(), pixeldelta.getY(), pixeldelta.getZ()));
        Hashtable<SampledCurve, int[]> curvesAndMembraneIndexes = meshDisplayAdapter.getCurvesAndMembraneIndexes(getNormalAxis(), getSlice());
        if (curvesAndMembraneIndexes != null) {
            Curve[] curves = (Curve[]) org.vcell.util.BeanUtils.getArray(curvesAndMembraneIndexes.keys(), Curve.class);
            for (int i = 0; curves != null && i < curves.length; i++) {
                curveRenderer.addCurve(curves[i]);
                curveRenderer.renderPropertySegmentColors(curves[i], getCurveColors(curvesAndMembraneIndexes, curves[i], meshDisplayAdapter));
                curveRenderer.renderPropertyLineWidthMultiplier(curves[i], membrScale);
            }
            Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
            curveRenderer.setAntialias(false);
            curveRenderer.draw(g);
        }
        return ((DataBufferInt) bufferedImage.getData().getDataBuffer()).getData();
    } else {
        throw new RuntimeException("unsupported VariableType " + getServerPDEDataContext().getDataIdentifier().getVariableType());
    }
}
Also used : Extent(org.vcell.util.Extent) DataBufferInt(java.awt.image.DataBufferInt) BufferedImage(java.awt.image.BufferedImage) MeshDisplayAdapter(cbit.vcell.solvers.MeshDisplayAdapter) SampledCurve(cbit.vcell.geometry.SampledCurve) Hashtable(java.util.Hashtable) Curve(cbit.vcell.geometry.Curve) SampledCurve(cbit.vcell.geometry.SampledCurve) Extent(org.vcell.util.Extent) Graphics2D(java.awt.Graphics2D) Coordinate(org.vcell.util.Coordinate) CartesianMesh(cbit.vcell.solvers.CartesianMesh) Coordinate(org.vcell.util.Coordinate)

Example 44 with DataBufferInt

use of java.awt.image.DataBufferInt in project vcell by virtualcell.

the class PDEOffscreenRenderer method getScaledRGBVolume.

private BufferedImage getScaledRGBVolume(CartesianMesh mesh, int meshMode, int imageScale, boolean bBackground) {
    Dimension dim = FormatSpecificSpecs.getMeshDimensionUnscaled(getNormalAxis(), getServerPDEDataContext().getCartesianMesh());
    int width = (int) dim.getWidth();
    int height = (int) dim.getHeight();
    double[] volumeData = new double[width * height];
    if (!bBackground) {
        // if(bNeedsDefaultScaling){setDefaultScaling();}
        double notInDomainValue = getDisplayAdapterService().getValueDomain().getMin() - 1.0;
        double[] values = getServerPDEDataContext().getDataValues();
        int[] dataIndices = getServerPDEDataContext().getCartesianMesh().getVolumeSliceIndices(getNormalAxis(), getSlice());
        for (int i = 0; i < dataIndices.length; i++) {
            double value = values[dataIndices[i]];
            if (domainValid != null) {
                if (domainValid.get(dataIndices[i]) || domainValid.isEmpty()) {
                    volumeData[i] = value;
                } else {
                    volumeData[i] = notInDomainValue;
                }
            } else {
                volumeData[i] = value;
            }
        }
    }
    SourceDataInfo sourceDataInfo = new SourceDataInfo(SourceDataInfo.RAW_VALUE_TYPE, volumeData, mesh.getExtent(), mesh.getOrigin(), new Range(0, 1), 0, width, 1, height, width, 1, 0);
    ImagePaneModel imagePaneModel = new ImagePaneModel();
    imagePaneModel.setSourceData(sourceDataInfo);
    imagePaneModel.setBackgroundColor(Color.black);
    imagePaneModel.setDisplayAdapterService(getDisplayAdapterService());
    // getDisplayAdapterService().setActiveScaleRange(new Range(0,1));
    imagePaneModel.setMode(meshMode);
    imagePaneModel.setZoom(imageScale);
    imagePaneModel.setViewport(new Rectangle(imagePaneModel.getScaledLength(width), imagePaneModel.getScaledLength(height)));
    imagePaneModel.updateViewPortImage();
    if (bBackground) {
        int[] internalBuffer = ((DataBufferInt) (imagePaneModel.getViewPortImage().getRaster().getDataBuffer())).getData();
        Arrays.fill(internalBuffer, getDisplayAdapterService().getSpecialColors()[DisplayAdapterService.NULL_COLOR_OFFSET]);
    }
    BufferedImage bufferedImage = imagePaneModel.getViewPortImage();
    imagePaneModel.setDisplayAdapterService(null);
    return bufferedImage;
}
Also used : ImagePaneModel(cbit.image.ImagePaneModel) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) DataBufferInt(java.awt.image.DataBufferInt) SourceDataInfo(cbit.image.SourceDataInfo) Range(org.vcell.util.Range) BufferedImage(java.awt.image.BufferedImage)

Example 45 with DataBufferInt

use of java.awt.image.DataBufferInt in project vcell by virtualcell.

the class RasterExporter method createTextureImage.

private static BufferedImage createTextureImage(int imageSideLength) {
    int[] texImage = new int[imageSideLength * imageSideLength];
    int[] bitMasks = new int[] { 0xFF0000, 0xFF00, 0xFF, 0xFF000000 };
    SinglePixelPackedSampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, imageSideLength, imageSideLength, bitMasks);
    DataBufferInt db = new DataBufferInt(texImage, texImage.length);
    WritableRaster wr = Raster.createWritableRaster(sm, db, new Point());
    BufferedImage image = new BufferedImage(ColorModel.getRGBdefault(), wr, false, null);
    return image;
}
Also used : WritableRaster(java.awt.image.WritableRaster) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage)

Aggregations

DataBufferInt (java.awt.image.DataBufferInt)47 BufferedImage (java.awt.image.BufferedImage)29 DataBuffer (java.awt.image.DataBuffer)16 DataBufferByte (java.awt.image.DataBufferByte)14 Graphics2D (java.awt.Graphics2D)11 Point (java.awt.Point)11 WritableRaster (java.awt.image.WritableRaster)11 DataBufferShort (java.awt.image.DataBufferShort)10 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)10 SampleModel (java.awt.image.SampleModel)8 Rectangle (java.awt.Rectangle)6 DataBufferUShort (java.awt.image.DataBufferUShort)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 DirectColorModel (java.awt.image.DirectColorModel)5 IndexColorModel (java.awt.image.IndexColorModel)5 ColorModel (java.awt.image.ColorModel)4 ByteBuffer (java.nio.ByteBuffer)3 CartesianMesh (cbit.vcell.solvers.CartesianMesh)2 Dimension (java.awt.Dimension)2