Example usage for java.awt.image BufferedImage TYPE_3BYTE_BGR

List of usage examples for java.awt.image BufferedImage TYPE_3BYTE_BGR

Introduction

In this page you can find the example usage for java.awt.image BufferedImage TYPE_3BYTE_BGR.

Prototype

int TYPE_3BYTE_BGR

To view the source code for java.awt.image BufferedImage TYPE_3BYTE_BGR.

Click Source Link

Document

Represents an image with 8-bit RGB color components, corresponding to a Windows-style BGR color model) with the colors Blue, Green, and Red stored in 3 bytes.

Usage

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static RegularField bufferedImage2RegularField(BufferedImage inImage, boolean vFlip) {
    if (inImage == null) {
        return null;
    }/*from  ww  w.  j  a va  2  s  .c o  m*/

    int[] dims = new int[2];
    dims[0] = inImage.getWidth();
    dims[1] = inImage.getHeight();

    RegularField field = new RegularField(dims);

    WritableRaster raster = inImage.getRaster();
    byte[][] samples = null;
    int[][] samples32 = null;
    int i = 0;
    switch (inImage.getType()) {
    case BufferedImage.TYPE_BYTE_GRAY:
        samples = new byte[1][];
        samples[0] = new byte[dims[0] * dims[1]];
        if (vFlip) {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i++] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                }
            }
        } else {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i++] = (byte) raster.getSample(x, y, 0);
                }
            }
        }
        field.addData(DataArray.create(samples[0], 1, "grayscaleData"));
        break;
    case BufferedImage.TYPE_USHORT_GRAY:
        samples32 = new int[1][];
        samples32[0] = new int[dims[0] * dims[1]];
        if (vFlip) {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples32[0][i++] = (int) raster.getSample(x, dims[1] - y - 1, 0);
                }
            }
        } else {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples32[0][i++] = (int) raster.getSample(x, y, 0);
                }
            }
        }
        field.addData(DataArray.create(samples32[0], 1, "grayscaleData"));
        break;
    case BufferedImage.TYPE_INT_RGB:
        samples = new byte[3][];
        samples[0] = new byte[dims[0] * dims[1]];
        samples[1] = new byte[dims[0] * dims[1]];
        samples[2] = new byte[dims[0] * dims[1]];
        if (vFlip) {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                    samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1);
                    samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2);
                    i++;
                }
            }
        } else {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, y, 0);
                    samples[1][i] = (byte) raster.getSample(x, y, 1);
                    samples[2][i] = (byte) raster.getSample(x, y, 2);
                    i++;
                }
            }
        }
        field.addData(DataArray.create(samples[0], 1, "redData"));
        field.addData(DataArray.create(samples[1], 1, "greenData"));
        field.addData(DataArray.create(samples[2], 1, "blueData"));
        break;
    case BufferedImage.TYPE_3BYTE_BGR:
    case BufferedImage.TYPE_INT_BGR:
        samples = new byte[3][];
        samples[0] = new byte[dims[0] * dims[1]];
        samples[1] = new byte[dims[0] * dims[1]];
        samples[2] = new byte[dims[0] * dims[1]];
        if (vFlip) {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                    samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1);
                    samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2);
                    i++;
                }
            }
        } else {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, y, 0);
                    samples[1][i] = (byte) raster.getSample(x, y, 1);
                    samples[2][i] = (byte) raster.getSample(x, y, 2);
                    i++;
                }
            }
        }
        field.addData(DataArray.create(samples[0], 1, "blueData"));
        field.addData(DataArray.create(samples[1], 1, "greenData"));
        field.addData(DataArray.create(samples[2], 1, "redData"));
        break;
    case BufferedImage.TYPE_INT_ARGB:
        samples = new byte[4][];
        samples[0] = new byte[dims[0] * dims[1]];
        samples[1] = new byte[dims[0] * dims[1]];
        samples[2] = new byte[dims[0] * dims[1]];
        samples[3] = new byte[dims[0] * dims[1]];
        for (int y = 0; y < dims[1]; y++) {
            for (int x = 0; x < dims[0]; x++) {
                samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1);
                samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2);
                samples[3][i] = (byte) raster.getSample(x, dims[1] - y - 1, 3);
                i++;
            }
        }
        field.addData(DataArray.create(samples[0], 1, "redData"));
        field.addData(DataArray.create(samples[1], 1, "greenData"));
        field.addData(DataArray.create(samples[2], 1, "blueData"));
        field.addData(DataArray.create(samples[3], 1, "alphaData"));
        break;
    case BufferedImage.TYPE_4BYTE_ABGR:
        samples = new byte[4][];
        samples[0] = new byte[dims[0] * dims[1]];
        samples[1] = new byte[dims[0] * dims[1]];
        samples[2] = new byte[dims[0] * dims[1]];
        samples[3] = new byte[dims[0] * dims[1]];
        for (int y = 0; y < dims[1]; y++) {
            for (int x = 0; x < dims[0]; x++) {
                samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1);
                samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2);
                samples[3][i] = (byte) raster.getSample(x, dims[1] - y - 1, 3);
                i++;
            }
        }
        field.addData(DataArray.create(samples[0], 1, "alphaData"));
        field.addData(DataArray.create(samples[1], 1, "redData"));
        field.addData(DataArray.create(samples[2], 1, "greenData"));
        field.addData(DataArray.create(samples[3], 1, "blueData"));
        break;
    default:
        BufferedImage newImg = new BufferedImage(inImage.getWidth(), inImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = newImg.createGraphics();
        g2d.drawImage(inImage, null, 0, 0);
        g2d.dispose();
        raster = newImg.getRaster();
        samples = new byte[3][];
        samples[0] = new byte[dims[0] * dims[1]];
        samples[1] = new byte[dims[0] * dims[1]];
        samples[2] = new byte[dims[0] * dims[1]];
        if (vFlip) {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, dims[1] - y - 1, 0);
                    samples[1][i] = (byte) raster.getSample(x, dims[1] - y - 1, 1);
                    samples[2][i] = (byte) raster.getSample(x, dims[1] - y - 1, 2);
                    i++;
                }
            }
        } else {
            for (int y = 0; y < dims[1]; y++) {
                for (int x = 0; x < dims[0]; x++) {
                    samples[0][i] = (byte) raster.getSample(x, y, 0);
                    samples[1][i] = (byte) raster.getSample(x, y, 1);
                    samples[2][i] = (byte) raster.getSample(x, y, 2);
                    i++;
                }
            }
        }
        field.addData(DataArray.create(samples[0], 1, "redData"));
        field.addData(DataArray.create(samples[1], 1, "greenData"));
        field.addData(DataArray.create(samples[2], 1, "blueData"));
    }

    float[][] affine = new float[4][3];
    for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
            affine[j][k] = 0.0f;
            if (j == k)
                affine[j][k] = 1.0f;
        }
    }

    affine[3][0] = -(float) dims[0] / 2.0f;
    affine[3][1] = -(float) dims[1] / 2.0f;
    affine[3][2] = 0.0f;
    field.setAffine(affine);
    return field;
}

From source file:TextureByReference.java

static void printType(BufferedImage bImage) {
    int type = bImage.getType();
    if (type == BufferedImage.TYPE_4BYTE_ABGR) {
        System.out.println("TYPE_4BYTE_ABGR");
    } else if (type == BufferedImage.TYPE_INT_ARGB) {
        System.out.println("TYPE_INT_ARGB");
    } else if (type == BufferedImage.TYPE_3BYTE_BGR) {
        System.out.println("TYPE_3BYTE_BGR");
    } else if (type == BufferedImage.TYPE_CUSTOM) {
        System.out.println("TYPE_CUSTOM");
    } else//from  ww  w .  j a v a 2  s . c o m
        System.out.println(type);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) {
    if (paint instanceof Color) {
        final Color color = (Color) paint;
        final int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setFillOpacity(alpha / 255.00f);
                    fillGState[alpha] = gs;
                }//from www .  j  a v a 2 s.c  o  m
                cb.setGState(gs);
            }
            cb.setColorFill(color);
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setStrokeOpacity(alpha / 255.0f);
                    strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
            }
            cb.setColorStroke(color);
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2);
        final PdfShadingPattern pat = new PdfShadingPattern(shading);
        if (fill) {
            cb.setShadingFill(pat);
        } else {
            cb.setShadingStroke(pat);
        }
    } else if (paint instanceof TexturePaint) {
        try {
            final TexturePaint tp = (TexturePaint) paint;
            final BufferedImage img = tp.getImage();
            final Rectangle2D rect = tp.getAnchorRect();
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
            final AffineTransform inverse = this.normalizeMatrix();
            inverse.translate(rect.getX(), rect.getY());
            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
            final double[] mx = new double[6];
            inverse.getMatrix(mx);
            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                    (float) mx[5]);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }

        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    } else {
        try {
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            final BufferedImage img = new BufferedImage((int) width, (int) height, type);
            final Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            final AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            if (invert) {
                final AffineTransform tx = new AffineTransform();
                tx.scale(1, -1);
                tx.translate(-xoffset, -yoffset);
                g.drawImage(img, tx, null);
            }
            g.dispose();
            // g = null;
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(width, height);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }
        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    }
}