Search in sources :

Example 41 with DataBufferByte

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

the class ImageUtil method setUnpackedBinaryData.

/**
     * Copies data into the packed array of the <code>Raster</code>
     * from an array of unpacked data of the form returned by
     * <code>getUnpackedBinaryData()</code>.
     *
     * <p> If the data are binary, then the target bit will be set if
     * and only if the corresponding byte is non-zero.
     *
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
     * <code>false</code> with the <code>SampleModel</code> of the
     * supplied <code>Raster</code> as argument.
     */
public static void setUnpackedBinaryData(byte[] bdata, WritableRaster raster, Rectangle rect) {
    SampleModel sm = raster.getSampleModel();
    if (!isBinary(sm)) {
        throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
    }
    int rectX = rect.x;
    int rectY = rect.y;
    int rectWidth = rect.width;
    int rectHeight = rect.height;
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dx = rectX - raster.getSampleModelTranslateX();
    int dy = rectY - raster.getSampleModelTranslateY();
    MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel) sm;
    int lineStride = mpp.getScanlineStride();
    int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
    int bitOffset = mpp.getBitOffset(dx);
    int k = 0;
    if (dataBuffer instanceof DataBufferByte) {
        byte[] data = ((DataBufferByte) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 8 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 8] |= (byte) (0x00000001 << (7 - bOffset & 7));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    } else if (dataBuffer instanceof DataBufferShort || dataBuffer instanceof DataBufferUShort) {
        short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 16 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 16] |= (short) (0x00000001 << (15 - bOffset % 16));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    } else if (dataBuffer instanceof DataBufferInt) {
        int[] data = ((DataBufferInt) dataBuffer).getData();
        for (int y = 0; y < rectHeight; y++) {
            int bOffset = eltOffset * 32 + bitOffset;
            for (int x = 0; x < rectWidth; x++) {
                if (bdata[k++] != (byte) 0) {
                    data[bOffset / 32] |= (int) (0x00000001 << (31 - bOffset % 32));
                }
                bOffset++;
            }
            eltOffset += lineStride;
        }
    }
}
Also used : DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) Point(java.awt.Point) DataBuffer(java.awt.image.DataBuffer)

Example 42 with DataBufferByte

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

the class PNGImageReader method createRaster.

private WritableRaster createRaster(int width, int height, int bands, int scanlineStride, int bitDepth) {
    DataBuffer dataBuffer;
    WritableRaster ras = null;
    Point origin = new Point(0, 0);
    if ((bitDepth < 8) && (bands == 1)) {
        dataBuffer = new DataBufferByte(height * scanlineStride);
        ras = Raster.createPackedRaster(dataBuffer, width, height, bitDepth, origin);
    } else if (bitDepth <= 8) {
        dataBuffer = new DataBufferByte(height * scanlineStride);
        ras = Raster.createInterleavedRaster(dataBuffer, width, height, scanlineStride, bands, bandOffsets[bands], origin);
    } else {
        dataBuffer = new DataBufferUShort(height * scanlineStride);
        ras = Raster.createInterleavedRaster(dataBuffer, width, height, scanlineStride, bands, bandOffsets[bands], origin);
    }
    return ras;
}
Also used : WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer)

Example 43 with DataBufferByte

use of java.awt.image.DataBufferByte in project poi by apache.

the class TestFontRendering method bug55902mixedFontWithChineseCharacters.

// @Ignore2("This fails on some systems because fonts are rendered slightly different")
@Test
public void bug55902mixedFontWithChineseCharacters() throws IOException, FontFormatException {
    // font files need to be downloaded first via
    // ant test-scratchpad-download-resources
    String[][] fontFiles = { // Calibri is not available on *nix systems, so we need to use another similar free font
    { "build/scratchpad-test-resources/Cabin-Regular.ttf", "mapped", "Calibri" }, // for the junit test not all chars are rendered
    { "build/scratchpad-test-resources/mona.ttf", "fallback", "Cabin" } };
    // setup fonts (especially needed, when run under *nix systems)
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Map<String, String> fontMap = new HashMap<String, String>();
    Map<String, String> fallbackMap = new HashMap<String, String>();
    for (String[] fontFile : fontFiles) {
        File f = new File(fontFile[0]);
        assumeTrue("necessary font file " + f.getName() + " not downloaded.", f.exists());
        Font font = Font.createFont(Font.TRUETYPE_FONT, f);
        ge.registerFont(font);
        Map<String, String> map = ("mapped".equals(fontFile[1]) ? fontMap : fallbackMap);
        map.put(fontFile[2], font.getFamily());
    }
    InputStream is = slTests.openResourceAsStream("bug55902-mixedFontChineseCharacters.ppt");
    HSLFSlideShow ss = new HSLFSlideShow(is);
    is.close();
    Dimension pgsize = ss.getPageSize();
    HSLFSlide slide = ss.getSlides().get(0);
    // render it
    double zoom = 1;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);
    BufferedImage imgActual = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = imgActual.createGraphics();
    graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
    graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    graphics.setTransform(at);
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    slide.draw(graphics);
    BufferedImage imgExpected = ImageIO.read(slTests.getFile("bug55902-mixedChars.png"));
    DataBufferByte expectedDB = (DataBufferByte) imgExpected.getRaster().getDataBuffer();
    DataBufferByte actualDB = (DataBufferByte) imgActual.getRaster().getDataBuffer();
    byte[] expectedData = expectedDB.getData(0);
    byte[] actualData = actualDB.getData(0);
    // allow to find out what the actual difference is in CI where this fails currently
    if (!Arrays.equals(expectedData, actualData)) {
        ImageIO.write(imgActual, "PNG", TempFile.createTempFile("TestFontRendering", ".png"));
    }
    assertArrayEquals("Expected to have matching raster-arrays, but found differences", expectedData, actualData);
    ss.close();
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) GraphicsEnvironment(java.awt.GraphicsEnvironment) DataBufferByte(java.awt.image.DataBufferByte) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) AffineTransform(java.awt.geom.AffineTransform) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 44 with DataBufferByte

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

the class ROIMultiPaintManager method sampleAnalyticIntoImage.

private void sampleAnalyticIntoImage(OverlayEditorPanelJAI.ImgSubVolHelper imgSubVolHelper) {
    try {
        Extent extent = (editedGeometryAttributes == null ? originalExtent : editedGeometryAttributes.extent);
        Origin orig = (editedGeometryAttributes == null ? originalOrigin : editedGeometryAttributes.origin);
        int numX = roiComposite[0].getWidth();
        int numY = roiComposite[0].getHeight();
        int numZ = roiComposite.length;
        int dim = (roiComposite.length > 1 ? 1 : 0) + (roiComposite[0].getHeight() > 1 ? 1 : 0) + 1;
        double cX = calcCoord(imgSubVolHelper.getMousePoint().x, numX, orig.getX(), extent.getX());
        double cY = calcCoord(imgSubVolHelper.getMousePoint().y, numY, orig.getY(), extent.getY());
        double cZ = calcCoord(imgSubVolHelper.getZCenter(), numZ, orig.getZ(), extent.getZ());
        Coordinate center = new Coordinate(cX, cY, cZ);
        AnalyticSubVolume tempSV = GeometrySubVolumePanel.createAnalyticSubVolume(overlayEditorPanelJAI, dim, center, "tempSV");
        tempSV.rebind();
        for (int k = 0; k < numZ; k++) {
            double coordZ = calcCoord(k, numZ, orig.getZ(), extent.getZ());
            for (int j = 0; j < numY; j++) {
                double coordY = calcCoord(j, numY, orig.getY(), extent.getY());
                for (int i = 0; i < numX; i++) {
                    double coordX = calcCoord(i, numX, orig.getX(), extent.getX());
                    if (tempSV.isInside(coordX, coordY, coordZ, null)) {
                        ((DataBufferByte) roiComposite[k].getRaster().getDataBuffer()).getData()[j * numX + i] = (byte) (imgSubVolHelper.getCurrentSubVolHandle().getContrastColorIndex());
                    }
                }
            }
        }
    } catch (UserCancelException uce) {
    // ignore
    } catch (Exception e) {
        DialogUtils.showErrorDialog(overlayEditorPanelJAI, e.getClass().getName() + " " + e.getMessage());
    }
}
Also used : Origin(org.vcell.util.Origin) Extent(org.vcell.util.Extent) Coordinate(org.vcell.util.Coordinate) UserCancelException(org.vcell.util.UserCancelException) DataBufferByte(java.awt.image.DataBufferByte) AnalyticSubVolume(cbit.vcell.geometry.AnalyticSubVolume) Point(java.awt.Point) UtilCancelException(org.vcell.util.UtilCancelException) UserCancelException(org.vcell.util.UserCancelException)

Example 45 with DataBufferByte

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

the class ROIMultiPaintManager method applyHighlightToROI.

private void applyHighlightToROI(ROIMultiPaintManager.ComboboxROIName currentComboboxROIName, boolean bOverWrite) {
    UShortImage[] roiZ = overlayEditorPanelJAI.getHighliteInfo().getRoiImages();
    // Update composite ROI
    int roiColorIndex = currentComboboxROIName.getContrastColorIndex();
    for (int i = 0; i < roiZ.length; i++) {
        short[] pixels = roiZ[i].getPixels();
        byte[] compositePixels = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
        for (int j = 0; j < pixels.length; j++) {
            if (pixels[j] != 0) {
                compositePixels[j] = (bOverWrite ? (byte) roiColorIndex : (compositePixels[j] == 0 ? (byte) roiColorIndex : compositePixels[j]));
            }
        }
    }
    overlayEditorPanelJAI.setAllROICompositeImage(roiComposite, OverlayEditorPanelJAI.FRAP_DATA_UPDATEROI_WITHHIGHLIGHT_PROPERTY);
    overlayEditorPanelJAI.setHighliteInfo(null, OverlayEditorPanelJAI.FRAP_DATA_UPDATEROI_WITHHIGHLIGHT_PROPERTY);
}
Also used : UShortImage(cbit.vcell.VirtualMicroscopy.UShortImage) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point)

Aggregations

DataBufferByte (java.awt.image.DataBufferByte)64 BufferedImage (java.awt.image.BufferedImage)38 Point (java.awt.Point)36 WritableRaster (java.awt.image.WritableRaster)18 DataBufferInt (java.awt.image.DataBufferInt)14 DataBuffer (java.awt.image.DataBuffer)13 DataBufferShort (java.awt.image.DataBufferShort)10 UShortImage (cbit.vcell.VirtualMicroscopy.UShortImage)8 Rectangle (java.awt.Rectangle)8 SampleModel (java.awt.image.SampleModel)8 DataBufferUShort (java.awt.image.DataBufferUShort)7 IndexColorModel (java.awt.image.IndexColorModel)7 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 Graphics2D (java.awt.Graphics2D)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 UserCancelException (org.vcell.util.UserCancelException)6 UtilCancelException (org.vcell.util.UtilCancelException)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)5 Hashtable (java.util.Hashtable)5 VCImage (cbit.image.VCImage)4