Example usage for java.awt.image SampleModel getNumBands

List of usage examples for java.awt.image SampleModel getNumBands

Introduction

In this page you can find the example usage for java.awt.image SampleModel getNumBands.

Prototype

public final int getNumBands() 

Source Link

Document

Returns the total number of bands of image data.

Usage

From source file:omr.jai.TestImage3.java

public static void print(PlanarImage pi) {
        // Show the image dimensions and coordinates.
        System.out.print("Image Dimensions: ");
        System.out.print(pi.getWidth() + "x" + pi.getHeight() + " pixels");

        // Remember getMaxX and getMaxY return the coordinate of the next point!
        System.out.println(" (from " + pi.getMinX() + "," + pi.getMinY() + " to " + (pi.getMaxX() - 1) + ","
                + (pi.getMaxY() - 1) + ")");
        if ((pi.getNumXTiles() != 1) || (pi.getNumYTiles() != 1)) { // Is it tiled?
            // Tiles number, dimensions and coordinates.
            System.out.print("Tiles: ");
            System.out.print(pi.getTileWidth() + "x" + pi.getTileHeight() + " pixels" + " (" + pi.getNumXTiles()
                    + "x" + pi.getNumYTiles() + " tiles)");
            System.out.print(" (from " + pi.getMinTileX() + "," + pi.getMinTileY() + " to " + pi.getMaxTileX() + ","
                    + pi.getMaxTileY() + ")");
            System.out.println(" offset: " + pi.getTileGridXOffset() + "," + pi.getTileGridXOffset());
        }/*  ww w  . j  a v  a 2 s  . com*/

        // Display info about the SampleModel of the image.
        SampleModel sm = pi.getSampleModel();
        System.out.println("Number of bands: " + sm.getNumBands());
        System.out.print("Data type: ");
        switch (sm.getDataType()) {
        case DataBuffer.TYPE_BYTE:
            System.out.println("byte");
            break;
        case DataBuffer.TYPE_SHORT:
            System.out.println("short");
            break;
        case DataBuffer.TYPE_USHORT:
            System.out.println("ushort");
            break;
        case DataBuffer.TYPE_INT:
            System.out.println("int");
            break;
        case DataBuffer.TYPE_FLOAT:
            System.out.println("float");
            break;
        case DataBuffer.TYPE_DOUBLE:
            System.out.println("double");
            break;
        case DataBuffer.TYPE_UNDEFINED:
            System.out.println("undefined");
            break;
        }

        // Display info about the ColorModel of the image.
        ColorModel cm = pi.getColorModel();
        if (cm != null) {
            System.out.println("Number of color components: " + cm.getNumComponents());
            System.out.println("Bits per pixel: " + cm.getPixelSize());
            System.out.print("Image Transparency: ");
            switch (cm.getTransparency()) {
            case Transparency.OPAQUE:
                System.out.println("opaque");
                break;
            case Transparency.BITMASK:
                System.out.println("bitmask");
                break;
            case Transparency.TRANSLUCENT:
                System.out.println("translucent");
                break;
            }
        } else
            System.out.println("No color model.");
    }

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

/**
 * A convenience method to create an instance of
 * <code>ComponentColorModel</code> suitable for use with the given
 * <code>SampleModel</code>. The <code>SampleModel</code> should have a data
 * type of <code>DataBuffer.TYPE_BYTE</code>, <code>TYPE_USHORT</code>, or
 * <code>TYPE_INT</code> and between 1 and 4 bands. Depending on the number
 * of bands of the <code>SampleModel</code>, either a gray, gray+alpha, rgb,
 * or rgb+alpha <code>ColorModel</code> is returned.
 *///  ww w .jav  a 2 s .  co m
public static ColorModel createComponentColorModel(final SampleModel sm) {
    final int type = sm.getDataType();
    final int bands = sm.getNumBands();
    ComponentColorModel cm = null;

    if (type == DataBuffer.TYPE_BYTE) {
        switch (bands) {
        case 1:
            cm = colorModelGray8;
            break;
        case 2:
            cm = colorModelGrayAlpha8;
            break;
        case 3:
            cm = colorModelRGB8;
            break;
        case 4:
            cm = colorModelRGBA8;
            break;
        }
    } else if (type == DataBuffer.TYPE_USHORT) {
        switch (bands) {
        case 1:
            cm = colorModelGray16;
            break;
        case 2:
            cm = colorModelGrayAlpha16;
            break;
        case 3:
            cm = colorModelRGB16;
            break;
        case 4:
            cm = colorModelRGBA16;
            break;
        }
    } else if (type == DataBuffer.TYPE_INT) {
        switch (bands) {
        case 1:
            cm = colorModelGray32;
            break;
        case 2:
            cm = colorModelGrayAlpha32;
            break;
        case 3:
            cm = colorModelRGB32;
            break;
        case 4:
            cm = colorModelRGBA32;
            break;
        }
    }

    return cm;
}

From source file:org.freecine.filmscan.ScanStrip.java

/**
 Try to find perforation corners using (modified) Hough transform. After the
 hough transform, matching pairs of top and bottom corners are found and
 clustered into pointClusterws list./*from   w ww  .  j  ava  2  s  .co m*/
 */
void houghTransform() {

    // Siebel transform of stripImage
    KernelJAI sxKernel = new KernelJAI(3, 3,
            new float[] { -1.0f, 0.0f, 1.0f, -2.0f, 0.0f, 2.0f, -1.0f, 0.0f, 1.0f });
    KernelJAI syKernel = new KernelJAI(3, 3,
            new float[] { -1.0f, -2.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 2.0f, 1.0f });

    RenderedImage dblImg = FormatDescriptor.create(stripImage, DataBuffer.TYPE_DOUBLE, null);
    RenderedImage sxImg = ConvolveDescriptor.create(dblImg, sxKernel, null);
    RenderedImage syImg = ConvolveDescriptor.create(dblImg, syKernel, null);

    SampleModel sm = sxImg.getSampleModel();
    int nbands = sm.getNumBands();
    double[] sxPixel = new double[nbands];
    double[] syPixel = new double[nbands];

    /*
     We are interested only in the left side of the strip as the 
     perforations are there
     */
    Rectangle perfArea = new Rectangle(0, 0, stripImage.getWidth() / 4, stripImage.getHeight());
    RectIter sxIter = RectIterFactory.create(sxImg, perfArea);
    RectIter syIter = RectIterFactory.create(syImg, perfArea);

    int width = (int) perfArea.getWidth();
    int height = (int) perfArea.getHeight();

    /*
     We use 2 accumulators - one for detecting the upper right corner,
     one for lower right corner. As the original is huge and the detaile we 
     are looking for are tiny, we use a sliding window that stores only the 
     relevant part of accumulator.
     */
    int accumHeight = (int) maxCornerRadius + 2;
    int[][] startAccum = new int[(int) (maxCornerRadius - minCornerRadius)][width * accumHeight];
    int[][] endAccum = new int[(int) (maxCornerRadius - minCornerRadius)][width * accumHeight];

    List<Point> startCorners = new ArrayList<Point>();
    List<Point> endCorners = new ArrayList<Point>();
    int y = 0;
    int maxVal = 0;
    if (analysisListener != null) {
        analysisListener.scanAnalysisProgress(0, height);
    }
    while (!sxIter.nextLineDone() && !syIter.nextLineDone()) {
        if (y % 1000 == 0 && y > 0) {
            System.out.println("" + y + " lines analyzed");
        }
        sxIter.startPixels();
        syIter.startPixels();
        int x = 0;
        while (!sxIter.nextPixelDone() && !syIter.nextPixelDone()) {
            sxIter.getPixel(sxPixel);
            syIter.getPixel(syPixel);
            double isq = sxPixel[0] * sxPixel[0] + syPixel[0] * syPixel[0];
            if (isq > EDGE_MIN_GRADIENT * EDGE_MIN_GRADIENT) {
                // This seems like a border
                if (syPixel[0] <= 0 && sxPixel[0] >= 0) {
                    // Upper right corner candidate
                    double intensity = Math.sqrt(isq);
                    for (double r = minCornerRadius; r < maxCornerRadius; r += 1.0) {
                        double cx = (double) x - r * sxPixel[0] / intensity;
                        double cy = (double) y - r * syPixel[0] / intensity;
                        if (cx > 0.0) {
                            int accumLine = (int) cy % accumHeight;
                            startAccum[(int) (r - minCornerRadius)][(int) cx + width * accumLine]++;
                            if (startAccum[(int) (r - minCornerRadius)][(int) cx
                                    + width * accumLine] > maxVal) {
                                maxVal = startAccum[(int) (r - minCornerRadius)][(int) cx + width * accumLine];
                            }
                        }
                    }
                }
                if (syPixel[0] >= 0 && sxPixel[0] >= 0) {
                    // Lower right corner candidate
                    double intensity = Math.sqrt(isq);
                    for (double r = minCornerRadius; r < maxCornerRadius; r += 1.0) {
                        double cx = (double) x - r * sxPixel[0] / intensity;
                        double cy = (double) y - r * syPixel[0] / intensity;
                        if (cx > 0.0 && cy > 0.0) {
                            int accumLine = (int) cy % accumHeight;
                            endAccum[(int) (r - minCornerRadius)][(int) cx + width * accumLine]++;
                            if (endAccum[(int) (r - minCornerRadius)][(int) cx + width * accumLine] > maxVal) {
                                maxVal = endAccum[(int) (r - minCornerRadius)][(int) cx + width * accumLine];
                            }
                        }
                    }
                }
            }
            x++;
        }
        y++;

        /*
         1 line processed - check if there are corner candidates in the 
         accumulator line we are going to overwrite
         */
        int y2 = y - accumHeight;
        int l = y % accumHeight;
        if (y2 > 0) {
            for (int n = 0; n < perfArea.getWidth(); n++) {
                for (int r = 0; r < (int) (maxCornerRadius - minCornerRadius); r++) {
                    if (startAccum[r][n + width * l] >= CORNER_MIN_HOUGH) {
                        // Is this a local maxima?
                        int val = startAccum[r][n + width * l];
                        if (val == getLocalMaxima(startAccum, r, n, y, width)) {
                            startCorners.add(new Point(n, y));
                            System.out.println(String.format("Found corner, quality = %d, r = %d, (%d, %d)",
                                    val, r, n, y));
                            // imageDataSingleArray[n+width*y] = (byte) 0xff;
                        }
                    }
                    if (endAccum[r][n + width * l] > CORNER_MIN_HOUGH) {
                        // Is this a local maxima?
                        int val = endAccum[r][n + width * l];
                        if (val == getLocalMaxima(endAccum, r, n, y2, width)) {
                            endCorners.add(new Point(n, y2));
                            System.out.println(String.format("Found end corner, quality = %d, r = %d, (%d, %d)",
                                    val, r, n, y2));
                            // imageDataSingleArray[n+width*y2] = (byte) 0x80;
                        }
                    }
                }
            }
        }
        // Zero the line just analyzed - it will be reused for the next line
        for (int n = 0; n < perfArea.getWidth(); n++) {
            for (int r = 0; r < (int) (maxCornerRadius - minCornerRadius); r++) {
                startAccum[r][n + width * (y % accumHeight)] = 0;
                endAccum[r][n + width * (y % accumHeight)] = 0;
            }
        }
        if ((y % 100 == 1) && analysisListener != null) {
            analysisListener.scanAnalysisProgress(y - 1, height);
        }
    }

    if (analysisListener != null) {
        analysisListener.scanAnalysisProgress(height, height);
    }

    /*
     Find perforations, i.e. pairs of start and end corners that are within
     the specified range from each other
     */
    for (Point sp : startCorners) {
        for (Point ep : endCorners) {
            if (ep.y - sp.y > CC_MAX_DIST) {
                break;
            }
            if (Math.abs(ep.x - sp.x) < 10 && ep.y - sp.y > CC_MIN_DIST) {
                Perforation p = new Perforation();
                p.x = (ep.x + sp.x) >> 1;
                p.y = (ep.y + sp.y) >> 1;
                // imageDataSingleArray[p.x+width*p.y] = (byte) 0x40;
                addPointToCluster(p.x, p.y);
            }
        }
    }

    System.out.println(String.format("%d clusters:", pointClusters.size()));
    for (PointCluster c : pointClusters) {
        System.out.println(
                String.format("  (%d, %d) %d points", c.getCentroidX(), c.getCentroidY(), c.getPointCount()));
        // imageDataSingleArray[c.getCentroidX()+width*c.getCentroidY()] = (byte) 0xff;
    }

}

From source file:org.geotools.gce.imagecollection.ImageCollectionReader.java

/**
 * Creates a {@link GridCoverage} for the provided {@link PlanarImage} using
 * the {@link #raster2Model} that was provided for this coverage.
 * //w  w w. j  a v  a  2s.  co m
 * <p>
 * This method is vital when working with coverages that have a raster to
 * model transformation that is not a simple scale and translate.
 * 
 * @param image
 *            contains the data for the coverage to create.
 * @param raster2Model
 *            is the {@link MathTransform} that maps from the raster space
 *            to the model space.
 * @return a {@link GridCoverage}
 * @throws IOException
 */
protected final GridCoverage createCoverage(PlanarImage image, MathTransform raster2Model) throws IOException {

    // creating bands
    final SampleModel sm = image.getSampleModel();
    final ColorModel cm = image.getColorModel();
    final int numBands = sm.getNumBands();
    final GridSampleDimension[] bands = new GridSampleDimension[numBands];
    // setting bands names.

    for (int i = 0; i < numBands; i++) {
        final ColorInterpretation colorInterpretation = TypeMap.getColorInterpretation(cm, i);
        if (colorInterpretation == null)
            throw new IOException("Unrecognized sample dimension type");
        Category[] categories = null;
        bands[i] = new GridSampleDimension(colorInterpretation.name(), categories, null).geophysics(true);
    }
    // creating coverage
    if (raster2Model != null) {
        return coverageFactory.create(coverageName, image, crs, raster2Model, bands, null, null);
    }
    return coverageFactory.create(coverageName, image, new GeneralEnvelope(originalEnvelope), bands, null,
            null);

}

From source file:org.geotools.gce.imagemosaic.RasterLayerResponse.java

/**
 * This method is responsible for creating a coverage from the supplied {@link RenderedImage}.
 * /*from   www .  j ava2 s  . com*/
 * @param image
 * @return
 * @throws IOException
 */
private GridCoverage2D prepareCoverage(RenderedImage image) throws IOException {

    // creating bands
    final SampleModel sm = image.getSampleModel();
    final ColorModel cm = image.getColorModel();
    final int numBands = sm.getNumBands();
    final GridSampleDimension[] bands = new GridSampleDimension[numBands];
    Set<String> bandNames = new HashSet<String>();
    // setting bands names.
    for (int i = 0; i < numBands; i++) {
        ColorInterpretation colorInterpretation = null;
        String bandName = null;
        if (cm != null) {
            // === color interpretation
            colorInterpretation = TypeMap.getColorInterpretation(cm, i);
            if (colorInterpretation == null) {
                throw new IOException("Unrecognized sample dimension type");
            }

            bandName = colorInterpretation.name();
            if (bandNames.contains(bandName)) {// make sure we create no duplicate band names
                bandName = "Band" + (i + 1);
            }
        } else { // no color model
            bandName = "Band" + (i + 1);
            colorInterpretation = ColorInterpretation.UNDEFINED;
        }

        // sample dimension type
        final SampleDimensionType st = TypeMap.getSampleDimensionType(sm, i);

        // set some no data values, as well as Min and Max values
        final double noData;
        double min = -Double.MAX_VALUE, max = Double.MAX_VALUE;
        if (backgroundValues != null) {
            // sometimes background values are not specified as 1 per each band, therefore we need to be careful
            noData = backgroundValues[backgroundValues.length > i ? i : 0];
        } else {
            if (st.compareTo(SampleDimensionType.REAL_32BITS) == 0)
                noData = Float.NaN;
            else if (st.compareTo(SampleDimensionType.REAL_64BITS) == 0)
                noData = Double.NaN;
            else if (st.compareTo(SampleDimensionType.SIGNED_16BITS) == 0) {
                noData = Short.MIN_VALUE;
                min = Short.MIN_VALUE;
                max = Short.MAX_VALUE;
            } else if (st.compareTo(SampleDimensionType.SIGNED_32BITS) == 0) {
                noData = Integer.MIN_VALUE;

                min = Integer.MIN_VALUE;
                max = Integer.MAX_VALUE;
            } else if (st.compareTo(SampleDimensionType.SIGNED_8BITS) == 0) {
                noData = -128;
                min = -128;
                max = 127;
            } else {
                //unsigned
                noData = 0;
                min = 0;

                // compute max
                if (st.compareTo(SampleDimensionType.UNSIGNED_1BIT) == 0)
                    max = 1;
                else if (st.compareTo(SampleDimensionType.UNSIGNED_2BITS) == 0)
                    max = 3;
                else if (st.compareTo(SampleDimensionType.UNSIGNED_4BITS) == 0)
                    max = 7;
                else if (st.compareTo(SampleDimensionType.UNSIGNED_8BITS) == 0)
                    max = 255;
                else if (st.compareTo(SampleDimensionType.UNSIGNED_16BITS) == 0)
                    max = 65535;
                else if (st.compareTo(SampleDimensionType.UNSIGNED_32BITS) == 0)
                    max = Math.pow(2, 32) - 1;

            }

        }
        bands[i] = new SimplifiedGridSampleDimension(bandName, st, colorInterpretation, noData, min, max, 1, //no scale 
                0, //no offset
                null).geophysics(true);
    }

    // creating the final coverage by keeping into account the fact that we
    Map<String, String> properties = null;
    if (granulesPaths != null) {
        properties = new HashMap<String, String>();
        properties.put(AbstractGridCoverage2DReader.FILE_SOURCE_PROPERTY, granulesPaths);
    }

    return coverageFactory.create(rasterManager.getCoverageIdentifier(), image,
            new GridGeometry2D(new GridEnvelope2D(PlanarImage.wrapRenderedImage(image).getBounds()),
                    PixelInCell.CELL_CORNER, finalGridToWorldCorner,
                    this.mosaicBBox.getCoordinateReferenceSystem(), hints),
            bands, null, properties);

}