Example usage for java.awt.image BufferedImage getType

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

Introduction

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

Prototype

public int getType() 

Source Link

Document

Returns the image type.

Usage

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

public static BufferedImage sphericalMapping(BufferedImage img, double f) {
    if (img == null) {
        return null;
    }//from ww w  . ja v a  2s.c  o m

    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage out = new BufferedImage(w, h, img.getType());
    //System.out.println("w:"+w+", h:"+h);

    int x0 = (int) Math.floor(w / 2) + 1;
    int y0 = (int) Math.floor(h / 2) + 1;

    double tmax = Math.atan2((double) (w - x0), f);
    double tmin = Math.atan2(-((double) x0), f);
    double tstep = (tmax - tmin) / ((double) w);

    double fimax = Math.atan2((double) (h - y0), Math.sqrt(f * f));
    double fimin = Math.atan2(-(double) y0, Math.sqrt(f * f));
    double fistep = (fimax - fimin) / ((double) h);
    //System.out.println("fimax:"+fimax+", fimin:"+fimin);

    double theta, tantheta, costheta, tanfi, phi;
    int x, y;

    for (int t = 0; t < w; t++) {
        theta = tmin + (double) t * tstep;
        tantheta = Math.tan(theta);
        x = (int) Math.round(f * tantheta) + x0;
        for (int fi = 0; fi < h; fi++) {
            //nearest neighbour---------------------------------------
            phi = fimin + (double) fi * fistep;
            tanfi = Math.tan(phi);
            //x = (int)Math.round(f*tantheta) + x0;
            y = (int) Math.round(Math.sqrt((x - x0) * (x - x0) + f * f) * tanfi) + y0;
            if (x >= 0 && y >= 0 && x < w && y < h) {
                //piksel nowy x,y = piksel stary xd,yd
                out.setRGB(t, fi, img.getRGB(x, y));
            }
            //---------------------------------------------------------
        }
    }
    return out;
}

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

public static BufferedImage resizeImage(BufferedImage img, double xScale, double yScale, int type) {
    if (img == null) {
        return null;
    }//from   w  w  w. jav  a2s  .c  o m

    if (xScale <= 0 || yScale <= 0) {
        return null;
    }

    int w = img.getWidth();
    int h = img.getHeight();

    int neww = (int) (((double) w) * xScale);
    int newh = (int) (((double) h) * yScale);

    BufferedImage out = new BufferedImage(neww, newh, img.getType());

    AffineTransform tr = new AffineTransform();
    tr.scale(xScale, yScale);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    op.filter(img, out);
    return out;
}

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

public static BufferedImage rotateImage(BufferedImage img, double angle, int type, Color fillBgColor) {
    if (img == null) {
        return null;
    }/*from  w  w w.  ja  va 2 s. c  o  m*/

    if (angle > 360.0 || angle < -360) {
        angle = angle % 360.0;
    }

    if (angle < 0) {
        angle = 360 + angle;
    }

    if (angle == 0.0 || angle == 360.0) {
        return img;
    }

    //System.out.println("angle="+angle);

    int w = img.getWidth();
    int h = img.getHeight();

    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta,w/2,h/2);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    BufferedImage out = op.filter(img,null);
     */
    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta, w/2.0, h/2.0);
    AffineTransform translationTransform = findTranslation(tr, img);
    tr.preConcatenate(translationTransform);
    BufferedImageOp op = new AffineTransformOp(tr, type);
            
    BufferedImage out = op.filter(img,null);
     */
    BufferedImage out = null;
    if (angle == 90.0 || angle == 180.0 || angle == 270.0) {
        switch ((int) angle) {
        case 90:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(h - y - 1, x, img.getRGB(x, y));
                }
            }
            break;
        case 180:
            out = new BufferedImage(w, h, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(w - x - 1, h - y - 1, img.getRGB(x, y));
                }
            }
            break;
        case 270:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(y, w - x - 1, img.getRGB(x, y));
                }
            }
            break;
        }
    } else {
        double theta = angle * Math.PI / 180.0;
        int neww = w, newh = h;
        double dx = 0.0, dy = 0.0;
        double s = Math.sin(theta);
        double c = Math.cos(theta);
        if (angle > 0.0 && angle < 90.0) {
            neww = (int) Math.round(((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s + ((double) h) * c);
            dx = ((double) h) * s;
            dy = 0.0;
        } else if (angle > 90.0 && angle < 180.0) {
            neww = (int) Math.round(-((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c + ((double) h) * s;
            dy = -((double) h) * c;
        } else if (angle > 180.0 && angle < 270.0) {
            neww = (int) Math.round(-((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c;
            dy = -((double) w) * s - ((double) h) * c;
        } else if (angle > 270.0 && angle < 360.0) {
            neww = (int) Math.round(((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s + ((double) h) * c);
            dx = 0.0;
            dy = -((double) w) * s;
        }

        AffineTransform tr = new AffineTransform();
        tr.translate(dx, dy);
        tr.rotate(theta);
        BufferedImageOp op = new AffineTransformOp(tr, type);
        out = new BufferedImage(neww, newh, img.getType());
        Graphics2D g2d = (Graphics2D) out.getGraphics();
        Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
        g2d.setPaint(fillBgColor);
        g2d.fill(clear);
        op.filter(img, out);
    }
    return out;
}

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

public static BufferedImage translateImage(BufferedImage img, double dx, double dy, int type,
        Color fillBgColor) {/*from   www .java  2  s . c  om*/
    if (img == null) {
        return null;
    }

    int w = img.getWidth();
    int h = img.getHeight();

    BufferedImage out = null;
    AffineTransform tr = new AffineTransform();
    tr.translate(dx, dy);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    out = new BufferedImage(w, h, img.getType());
    Graphics2D g2d = (Graphics2D) out.getGraphics();
    Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
    g2d.setPaint(fillBgColor);
    g2d.fill(clear);
    op.filter(img, out);
    return out;
}

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  w  w  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:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage invert(BufferedImage inImg) {
    if (inImg == null) {
        return null;
    }// ww  w  .j a v a2s.c om

    int width = inImg.getWidth();
    int height = inImg.getHeight();
    BufferedImage outImg = new BufferedImage(width, height, inImg.getType());
    WritableRaster outRaster = outImg.getRaster();
    WritableRaster inRaster = inImg.getRaster();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            for (int i = 0; i < outRaster.getNumBands(); i++) {
                outRaster.setSample(x, y, i, 255 - inRaster.getSample(x, y, i));
            }
        }
    }

    return outImg;
}

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

public static void makeTransparent(BufferedImage img, Color trColor) {
    int w = img.getWidth();
    int h = img.getHeight();
    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
        return;/*w  w w.  j a v a2s . c o m*/
    }

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (img.getRaster().getSample(x, y, 0) == trColor.getRed()
                    && img.getRaster().getSample(x, y, 1) == trColor.getGreen()
                    && img.getRaster().getSample(x, y, 2) == trColor.getBlue()) {
                img.getRaster().setSample(x, y, 3, 0);
            }
        }
    }
}

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

public static BufferedImage copyImage(BufferedImage in) {
    BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
    for (int i = 0; i < in.getRaster().getNumBands(); i++) {
        out.getRaster().setSamples(0, 0, out.getWidth(), out.getHeight(), i,
                in.getRaster().getSamples(0, 0, in.getWidth(), in.getHeight(), i, (int[]) null));
    }//from w w  w. j a  va  2 s.  c  o  m
    return out;
}

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

public static BufferedImage flipImageHorizontal(BufferedImage img) {
    if (img == null) {
        return null;
    }/*w  w  w .j a  va 2  s  .c  o  m*/

    if (img.getType() == 0) {
        img = convertToARGB(img);
    }
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
    for (int y = 0; y < out.getHeight(); y++) {
        for (int x = 0; x < out.getWidth(); x++) {
            out.setRGB(x, y, img.getRGB(img.getWidth() - x - 1, y));
        }
    }
    return out;
}

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

public static BufferedImage flipImageVertical(BufferedImage img) {
    if (img == null) {
        return null;
    }/*w  w w.  ja  v  a 2 s  . c  o m*/

    if (img.getType() == 0) {
        img = convertToARGB(img);
    }
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
    for (int y = 0; y < out.getHeight(); y++) {
        for (int x = 0; x < out.getWidth(); x++) {
            out.setRGB(x, y, img.getRGB(x, img.getHeight() - y - 1));
        }
    }
    return out;
}