Example usage for java.awt.image BufferedImage TYPE_USHORT_GRAY

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

Introduction

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

Prototype

int TYPE_USHORT_GRAY

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

Click Source Link

Document

Represents an unsigned short grayscale image, non-indexed).

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.WHITE);/* w w  w  . j  a  v  a  2  s  .c o m*/
    g2d.fillRect(0, 0, 100, 100);
    g2d.setColor(new Color(255, 123, 0));

    g2d.drawLine(100, 100, 1, 1);

    g2d.dispose();
    BufferedImage scaledImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_USHORT_GRAY);

    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(bi, 0, 0, 1000, 1000, null);
    graphics2D.dispose();
    ImageIO.write(scaledImage, "png", new File("c:/Java_Dev/Test.png"));
    bi.flush();
}

From source file:ImageBouncer.java

public void setImageType(String s) {
    int type = BufferedImage.TYPE_CUSTOM;
    if (s.equals("TYPE_INT_RGB"))
        type = BufferedImage.TYPE_INT_RGB;
    else if (s.equals("TYPE_INT_ARGB"))
        type = BufferedImage.TYPE_INT_ARGB;
    else if (s.equals("TYPE_INT_ARGB_PRE"))
        type = BufferedImage.TYPE_INT_ARGB_PRE;
    else if (s.equals("TYPE_3BYTE_BGR"))
        type = BufferedImage.TYPE_3BYTE_BGR;
    else if (s.equals("TYPE_BYTE_GRAY"))
        type = BufferedImage.TYPE_BYTE_GRAY;
    else if (s.equals("TYPE_USHORT_GRAY"))
        type = BufferedImage.TYPE_USHORT_GRAY;
    else if (s.equals("TYPE_USHORT_555_RGB"))
        type = BufferedImage.TYPE_USHORT_565_RGB;
    else if (s.equals("TYPE_USHORT_565_RGB"))
        type = BufferedImage.TYPE_USHORT_565_RGB;
    else {//from   w ww  . j  a v  a  2 s .  c o m
        System.out.println("Unrecognized type.");
        return;
    }
    image = makeBufferedImage(mOriginalImage, type);
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Write an image showing the heatmap of ssim values, per window
 * @param pValues sequence of SSIM values
 * @param pHeight number of SSIM windows across
 * @param pWidth number of SSIM windows down
 * @param pFilename filename to save the image to (png)
 *//*w ww.  j  a  va 2  s .c om*/
private static BufferedImage dumpSSIMHeatMap(final double[] pValues, final int pHeight, final int pWidth,
        final String pFilename) {
    BufferedImage heatMap = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_USHORT_GRAY);

    final int maxPixelValue = (int) Math.pow(2, heatMap.getColorModel().getPixelSize()) - 1;

    int pixel = 0;
    for (int height = 0; height < pHeight; height++) {
        for (int width = 0; width < pWidth; width++) {
            pixel = ((int) (maxPixelValue * pValues[(height * pWidth) + width]));
            if (pixel < 0) {
                pixel = 0;
            }
            heatMap.setRGB(width, height, pixel);
        }
    }

    //only write to file if filename is non-null
    if (pFilename != null) {
        try {
            ImageIO.write(heatMap, "png", new File(pFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    return heatMap;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the PSNR between two files/*from   ww  w  . j ava  2 s  .  c  o  m*/
 * @param pOne first image to compare
 * @param pTwo second image to compare
 * @return calculated psnr
 */
public static double calcPSNR(final File pOne, final File pTwo) {
    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;

    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    final double psnr = calcPSNR(oneA, twoA, greyscale);

    return psnr;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Calculate the SSIM between two files/*from www .jav a2  s. c  o m*/
 * @param pOne first image to compare
 * @param pTwo second image to compare
 * @param pHeatMapFilename ssim heat map image filename (can be null)
 * @param pMin list for return value - ssim minimum (can be null)
 * @param pVariance list for return value - ssim variance (can be null)
 * @return calculated ssim
 */
public static double calcSSIM(final File pOne, final File pTwo, final String pHeatMapFilename,
        List<Double> pMin, List<Double> pVariance) {

    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final int width = imageOne.getWidth();
    final int height = imageOne.getHeight();
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;

    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return -1;
    }

    //getRGB only returns 8 bits per component, so what about 16-bit images? 
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    final double ssim = calcSSIM(oneA, twoA, width, height, greyscale, pHeatMapFilename, pMin, pVariance);

    return ssim;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Compare two files, according to parameters passed via command line
 * @param pOne first file to compare/*from  w  w w.  ja v  a2 s.  c om*/
 * @param pTwo second file to compare
 * @param pHeatMapImage file to save ssim heat map image to
 * @param pCalcSSIM whether or not to calculate ssim
 * @param pCalcPSNR whether or not to calculate psnr
 */
private static void compare(final File pOne, final File pTwo, final String pHeatMapImage,
        final boolean pCalcSSIM, final boolean pCalcPSNR) {

    //just load the images once and use the internal methods for calculating ssim/psnr
    long time = System.currentTimeMillis();
    BufferedImage imageOne = null;
    try {
        imageOne = Imaging.getBufferedImage(pOne);
    } catch (IOException e) {
        printError(pOne, false, false, pTwo, false);
        return;
    } catch (NullPointerException e) {
        printError(pOne, false, false, pTwo, false);
        return;
    } catch (ImageReadException e) {
        printError(pOne, false, false, pTwo, false);
        return;
    }
    final long oneLoadTime = System.currentTimeMillis() - time;
    //getRGB only returns 8 bits per component, so what about 16-bit images?
    final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0,
            imageOne.getWidth());
    final int width = imageOne.getWidth();
    final int height = imageOne.getHeight();
    final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY
            || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY);
    imageOne = null;
    time = System.currentTimeMillis();
    BufferedImage imageTwo = null;
    try {
        imageTwo = Imaging.getBufferedImage(pTwo);
    } catch (IOException e) {
        printError(pOne, true, true, pTwo, false);
        return;
    } catch (NullPointerException e) {
        printError(pOne, true, true, pTwo, false);
        return;
    } catch (ImageReadException e) {
        printError(pOne, true, true, pTwo, false);
        return;
    }
    final long twoLoadTime = System.currentTimeMillis() - time;

    //getRGB only returns 8 bits per component, so what about 16-bit images?
    final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0,
            imageTwo.getWidth());
    imageTwo = null;

    //calculate psnr if wanted
    time = System.currentTimeMillis();
    double psnr = 0;
    long psnrCalc = 0;
    if (pCalcPSNR) {
        psnr = calcPSNR(oneA, twoA, greyscale);
        psnrCalc = System.currentTimeMillis() - time;
    }

    //calculate ssim if wanted
    time = System.currentTimeMillis();
    List<Double> ssimMin = new LinkedList<Double>();
    List<Double> ssimVariance = new LinkedList<Double>();
    double ssim = 0;
    long ssimCalc = 0;
    if (pCalcSSIM) {
        ssim = calcSSIM(oneA, twoA, width, height, greyscale, pHeatMapImage, ssimMin, ssimVariance);
        ssimCalc = System.currentTimeMillis() - time;
    }

    System.out.println("<dissimilar version=\"" + version + "\">");
    System.out.println("     <file loadTimeMS=\"" + oneLoadTime + "\">" + pOne + "</file>");
    System.out.println("     <file loadTimeMS=\"" + twoLoadTime + "\">" + pTwo + "</file>");
    if (pCalcSSIM) {
        System.out.println("     <ssim calcTimeMS=\"" + ssimCalc + "\">");
        if (ssim > 0) {
            System.out.println("          <mean>" + new DecimalFormat("0.0000000").format(ssim) + "</mean>");
            System.out.println(
                    "          <min>" + new DecimalFormat("0.0000000").format(ssimMin.get(0)) + "</min>");
            System.out.println("          <variance>"
                    + new DecimalFormat("0.0000000").format(ssimVariance.get(0)) + "</variance>");
        } else {
            System.out.println("failed");
        }
        System.out.println("     </ssim>");
    }
    if (pCalcPSNR) {
        System.out.println("     <psnr calcTimeMS=\"" + psnrCalc + "\">"
                + new DecimalFormat("0.0000").format(psnr) + "</psnr>");
    }
    System.out.println("</dissimilar>");

}

From source file:org.codice.alliance.imaging.chip.transformer.CatalogOutputAdapter.java

private void setImageDataFields(BufferedImage chip, ImageSegment chipImageSegment) throws IOException {

    int[] componentSizes = chip.getColorModel().getComponentSize();
    int pixelSize = chip.getColorModel().getPixelSize();

    switch (chip.getType()) {
    case BufferedImage.TYPE_BYTE_GRAY:
    case BufferedImage.TYPE_USHORT_GRAY:
    case BufferedImage.TYPE_BYTE_BINARY:
        setMonochrome(chipImageSegment, componentSizes[0], pixelSize);
        break;// ww  w. j  a v  a  2  s.c om
    case BufferedImage.TYPE_3BYTE_BGR:
    case BufferedImage.TYPE_INT_BGR:
        setImageFieldHelper(chipImageSegment, PixelValueType.INTEGER, ImageRepresentation.RGBTRUECOLOUR,
                componentSizes[0], pixelSize / 3, new String[] { "B", "G", "R" });
        break;
    case BufferedImage.TYPE_4BYTE_ABGR:
    case BufferedImage.TYPE_4BYTE_ABGR_PRE:
        setImageFieldHelper(chipImageSegment, PixelValueType.INTEGER, ImageRepresentation.RGBTRUECOLOUR,
                componentSizes[0], pixelSize / 4, new String[] { "B", "G", "R" });
        break;
    case BufferedImage.TYPE_INT_ARGB_PRE:
    case BufferedImage.TYPE_INT_ARGB:
        setARGB(chipImageSegment, componentSizes[0], pixelSize);
        break;
    case BufferedImage.TYPE_INT_RGB:
    case BufferedImage.TYPE_USHORT_555_RGB:
        setRGB(chipImageSegment, componentSizes[0], pixelSize);
        break;
    case BufferedImage.TYPE_CUSTOM:
        if (componentSizes.length == 1) {
            setMonochrome(chipImageSegment, componentSizes[0], pixelSize);
        } else if (componentSizes.length == 3) {
            setRGB(chipImageSegment, componentSizes[0], pixelSize);
        } else if (componentSizes.length == 4) {
            setARGB(chipImageSegment, componentSizes[0], pixelSize);
        } else {
            throw new IOException(
                    "unsupported color model for image type CUSTOM, only monochrome and 32-bit argb are supported");
        }
        break;
    case BufferedImage.TYPE_BYTE_INDEXED:
        setImageFieldHelper(chipImageSegment, PixelValueType.INTEGER, ImageRepresentation.RGBLUT,
                componentSizes[0], pixelSize, new String[] { "LU" });
        break;
    case BufferedImage.TYPE_USHORT_565_RGB:
        // don't know how to handle this one, since the bitsPerPixelPerBand is not consistent
        break;
    default:
        throw new IOException("unsupported image data type: type=" + chip.getType());
    }
}

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 v a  2s .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;
}