Example usage for javax.imageio ImageReadParam setSourceSubsampling

List of usage examples for javax.imageio ImageReadParam setSourceSubsampling

Introduction

In this page you can find the example usage for javax.imageio ImageReadParam setSourceSubsampling.

Prototype

public void setSourceSubsampling(int sourceXSubsampling, int sourceYSubsampling, int subsamplingXOffset,
        int subsamplingYOffset) 

Source Link

Document

Specifies a decimation subsampling to apply on I/O.

Usage

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Creates the thumbnail./* www.  jav a  2  s  . c  om*/
 *
 * @param imageFile the image file
 * @param thumbFile the empty thumbnail file
 * @param thumbX the width of the thumbnail
 * @param thumbY the height of the thumbnail
 * @param imageName the image file name with extension
 * @param tolerance the percentage of tolerance before creating a thumbnail
 * @return true is the thumbnail has been created, false otherwise
 * @throws IOException if an error occurs
 */
private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName,
        double tolerance) throws IOException {

    FileInputStream fileis = null;
    ImageInputStream imageis = null;

    Iterator readers = null;

    try {

        readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1));
        if (readers == null || (!readers.hasNext())) {
            throw new IOException("File not supported");
        }

        ImageReader reader = (ImageReader) readers.next();

        fileis = new FileInputStream(imageFile);
        imageis = ImageIO.createImageInputStream(fileis);
        reader.setInput(imageis, true);

        // Determines thumbnail height, width and quality
        int thumbWidth = thumbX;
        int thumbHeight = thumbY;

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = reader.getWidth(0);
        int imageHeight = reader.getHeight(0);

        //
        // Don't create the thumbnail if the original file is smaller
        // than required size increased by % tolerance
        //
        if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {

            return false;
        }

        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        ImageReadParam param = reader.getDefaultReadParam();
        param.setSourceSubsampling(3, 3, 0, 0);

        BufferedImage bi = reader.read(0, param);

        Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null);

        FileOutputStream fileOutputStream = new FileOutputStream(thumbFile);
        ImageIO.write(thumbImage, "jpg", fileOutputStream);

        thumb.flush();
        thumbImage.flush();
        fileOutputStream.flush();
        fileOutputStream.close();
        graphics2D.dispose();

    } finally {
        if (fileis != null) {
            fileis.close();
        }
        if (imageis != null) {
            imageis.close();
        }
    }

    return true;
}

From source file:nitf.imagej.NITF_Reader.java

/**
 * Opens the requested NITF, displaying a Text Window with the metadata
 * contents, and an Image window for each image in the file
 * /*from  w  ww. jav a 2 s.c  om*/
 * @param dir
 * @param name
 */
public void openNITF(File file) {
    try {
        log.debug("Reading: " + file.getAbsolutePath());
        NITFReader imageReader = (NITFReader) ImageIOUtils.getImageReader("nitf", file);

        Record record = imageReader.getRecord();
        // print the record contents to a byte stream
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        record.print(new PrintStream(out));

        TextWindow resultsWindow = new TextWindow(file.getName() + " [metadata]", "",
                IJ.getInstance().getWidth(), 400);
        TextPanel textPanel = resultsWindow.getTextPanel();
        textPanel.append(out.toString());
        textPanel.setFont(new Font("Courier New", Font.PLAIN, 16));
        resultsWindow.setVisible(true);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        for (int i = 0; i < record.getImages().length; ++i) {
            ImageSubheader subheader = record.getImages()[i].getSubheader();
            try {
                int numCols = subheader.getNumCols().getIntData();
                int numRows = subheader.getNumRows().getIntData();
                int numBands = subheader.getBandCount();
                String irep = subheader.getImageRepresentation().getStringData().trim();
                int bitsPerPixel = subheader.getNumBitsPerPixel().getIntData();

                ImageReadParam readParam = imageReader.getDefaultReadParam();

                String downsampleString = "";

                // if the image is larger than 1.5 times the screen
                // width/height,
                // then just downsample right away
                if (numCols > (screenSize.width * 1.5) || numRows > (screenSize.height * 1.5)) {
                    int xTimes = (int) (numCols / (double) screenSize.width);
                    int yTimes = (int) (numRows / (double) screenSize.height);

                    downsampleString = " (downsampled from " + numCols + "x" + numRows;

                    // pick the min, in case it turns out to be a little
                    // strip
                    int pixelSkip = Math.min(xTimes, yTimes);
                    numCols = (int) Math.ceil(numCols / pixelSkip);
                    numRows = (int) Math.ceil(numCols / pixelSkip);

                    downsampleString += "   to   " + numCols + "x" + numRows + ")";
                    readParam.setSourceSubsampling(pixelSkip, pixelSkip, 0, 0);
                }

                if (irep.equals("RGB") && numBands == 3) {
                    BufferedImage image = imageReader.read(i, readParam);
                    ImagePlus imagePlus = new ImagePlus(
                            file.getName() + " [" + i + "] - RGB" + downsampleString, image);
                    imagePlus.show();
                } else {
                    ImageStack imageStack = new ImageStack(numCols, numRows);
                    // read each band, separately
                    for (int j = 0; j < numBands; ++j) {
                        if (bitsPerPixel == 16 || bitsPerPixel == 8 || bitsPerPixel == 32
                                || bitsPerPixel == 64) {
                            readParam.setSourceBands(new int[] { j });
                            BufferedImage image = imageReader.read(i, readParam);
                            imageStack.addSlice(file.getName() + " [" + i + "] Band " + j + downsampleString,
                                    getPixelsFromBufferedImage(image));
                        }
                    }
                    if (imageStack.getSize() > 0) {
                        ImagePlus imagePlus = new ImagePlus(file.getName() + " [" + i + "]" + downsampleString,
                                imageStack);
                        imagePlus.show();
                    }
                }
            } catch (Exception e) {
                log.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    } catch (Exception e) {
        log.debug(ExceptionUtils.getStackTrace(e));
        IJ.error(e.getMessage());
    }
}

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * This method is responsible for evaluating possible subsampling factors once the best resolution level has been found in case we have support
 * for overviews, or starting from the original coverage in case there are no overviews available.
 * /*from ww w. j a va  2  s . c o  m*/
 * @param readP the imageRead parameter to be set
 * @param requestedRes the requested resolutions from which to determine the decimation parameters.
 */
public static void setDecimationParameters(ImageReadParam readP, GridEnvelope baseGridRange,
        double[] requestedRes, double[] highestRes) {
    {
        if (readP == null || baseGridRange == null)
            throw new IllegalArgumentException("Specified parameters are null");
        final int w = baseGridRange.getSpan(0);
        final int h = baseGridRange.getSpan(1);

        // ///////////////////////////////////////////////////////////////
        // DECIMATION ON READING
        // Setting subsampling factors with some checkings
        // 1) the subsampling factors cannot be zero
        // 2) the subsampling factors cannot be such that the w or h are 0
        // ///////////////////////////////////////////////////////////////
        if (requestedRes == null) {
            readP.setSourceSubsampling(1, 1, 0, 0);
        } else {
            int subSamplingFactorX = (int) Math.floor(requestedRes[0] / highestRes[0]);
            subSamplingFactorX = (subSamplingFactorX == 0) ? 1 : subSamplingFactorX;

            while (((w / subSamplingFactorX) <= 0) && (subSamplingFactorX >= 0))
                subSamplingFactorX--;

            subSamplingFactorX = (subSamplingFactorX == 0) ? 1 : subSamplingFactorX;

            int subSamplingFactorY = (int) Math.floor(requestedRes[1] / highestRes[1]);
            subSamplingFactorY = (subSamplingFactorY == 0) ? 1 : subSamplingFactorY;

            while (((h / subSamplingFactorY) <= 0) && (subSamplingFactorY >= 0))
                subSamplingFactorY--;

            subSamplingFactorY = (subSamplingFactorY == 0) ? 1 : subSamplingFactorY;

            readP.setSourceSubsampling(subSamplingFactorX, subSamplingFactorY, 0, 0);
        }
    }
}

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * Prepares the read parameters for doing an {@link ImageReader#read(int, ImageReadParam)}.
 * /*from  www  .j a v a 2 s .  c o m*/
 * It sets the passed {@link ImageReadParam} in terms of decimation on reading using the provided requestedEnvelope and requestedDim to evaluate
 * the needed resolution.
 * 
 * @param overviewPolicy it can be one of {@link Hints#VALUE_OVERVIEW_POLICY_IGNORE}, {@link Hints#VALUE_OVERVIEW_POLICY_NEAREST},
 *        {@link Hints#VALUE_OVERVIEW_POLICY_QUALITY} or {@link Hints#VALUE_OVERVIEW_POLICY_SPEED}. It specifies the policy to compute the
 *        overviews level upon request.
 * @param readParam an instance of {@link ImageReadParam} for setting the subsampling factors.
 * @param requestedEnvelope the {@link GeneralEnvelope} we are requesting.
 * @param requestedDim the requested dimensions.
 * @param gridRange
 * @throws IOException
 * @throws TransformException
 */
public static void setReadParameters(OverviewPolicy overviewPolicy, ImageReadParam readParam,
        GeneralEnvelope requestedEnvelope, Rectangle requestedDim, double[] highestRes, GridEnvelope gridRange,
        PixelInCell pixelInCell) throws IOException, TransformException {
    double[] requestedRes = null;

    // //
    //
    // Initialize overview policy
    //
    // //
    if (overviewPolicy == null) {
        overviewPolicy = OverviewPolicy.NEAREST;
    }

    // //
    //
    // default values for subsampling
    //
    // //
    readParam.setSourceSubsampling(1, 1, 0, 0);

    // //
    //
    // requested to ignore overviews
    //
    // //
    if (overviewPolicy.equals(OverviewPolicy.IGNORE)) {
        return;
    }

    // //
    //
    // Resolution requested. I am here computing the resolution required
    // by the user.
    //
    // //
    if (requestedEnvelope != null) {
        final GridToEnvelopeMapper geMapper = new GridToEnvelopeMapper();
        geMapper.setEnvelope(requestedEnvelope);
        geMapper.setGridRange(new GeneralGridEnvelope(requestedDim, 2));
        geMapper.setPixelAnchor(pixelInCell);
        final AffineTransform transform = geMapper.createAffineTransform();
        requestedRes = CoverageUtilities.getResolution(transform);
    }

    if (requestedRes == null) {
        return;
    }

    // ////////////////////////////////////////////////////////////////////
    //
    // DECIMATION ON READING
    //
    // ////////////////////////////////////////////////////////////////////
    if (highestRes == null)
        throw new IllegalArgumentException("Unspecified highest Resolution");
    if ((requestedRes[0] > highestRes[0]) || (requestedRes[1] > highestRes[1])) {
        Utilities.setDecimationParameters(readParam, gridRange, requestedRes, highestRes);
    }
}

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

/**
* Load a specified a raster as a portion of the granule describe by this {@link GranuleDescriptor}.
* 
* @param imageReadParameters the {@link ImageReadParam} to use for reading.
* @param index the index to use for the {@link ImageReader}.
* @param cropBBox the bbox to use for cropping. 
* @param mosaicWorldToGrid the cropping grid to world transform.
* @param request the incoming request to satisfy.
* @param hints {@link Hints} to be used for creating this raster.
* @return a specified a raster as a portion of the granule describe by this {@link GranuleDescriptor}.
* @throws IOException in case an error occurs.
*///from w w w . j a  v a 2s . c om
public GranuleLoadingResult loadRaster(final ImageReadParam imageReadParameters, final int index,
        final ReferencedEnvelope cropBBox, final MathTransform2D mosaicWorldToGrid,
        final RasterLayerRequest request, final Hints hints) throws IOException {

    if (LOGGER.isLoggable(java.util.logging.Level.FINER)) {
        final String name = Thread.currentThread().getName();
        LOGGER.finer("Thread:" + name + " Loading raster data for granuleDescriptor " + this.toString());
    }
    ImageReadParam readParameters = null;
    int imageIndex;
    final boolean useFootprint = roiProvider != null
            && request.getFootprintBehavior() != FootprintBehavior.None;
    Geometry inclusionGeometry = useFootprint ? roiProvider.getFootprint() : null;
    final ReferencedEnvelope bbox = useFootprint
            ? new ReferencedEnvelope(granuleBBOX.intersection(inclusionGeometry.getEnvelopeInternal()),
                    granuleBBOX.getCoordinateReferenceSystem())
            : granuleBBOX;
    boolean doFiltering = false;
    if (filterMe && useFootprint) {
        doFiltering = Utils.areaIsDifferent(inclusionGeometry, baseGridToWorld, granuleBBOX);
    }

    // intersection of this tile bound with the current crop bbox
    final ReferencedEnvelope intersection = new ReferencedEnvelope(bbox.intersection(cropBBox),
            cropBBox.getCoordinateReferenceSystem());
    if (intersection.isEmpty()) {
        if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
            LOGGER.fine(new StringBuilder("Got empty intersection for granule ").append(this.toString())
                    .append(" with request ").append(request.toString())
                    .append(" Resulting in no granule loaded: Empty result").toString());
        }
        return null;
    }

    // check if the requested bbox intersects or overlaps the requested area 
    if (useFootprint && inclusionGeometry != null && !JTS.toGeometry(cropBBox).intersects(inclusionGeometry)) {
        if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
            LOGGER.fine(new StringBuilder("Got empty intersection for granule ").append(this.toString())
                    .append(" with request ").append(request.toString())
                    .append(" Resulting in no granule loaded: Empty result").toString());
        }
        return null;
    }

    ImageInputStream inStream = null;
    ImageReader reader = null;
    try {
        //
        //get info about the raster we have to read
        //

        // get a stream
        assert cachedStreamSPI != null : "no cachedStreamSPI available!";
        inStream = cachedStreamSPI.createInputStreamInstance(granuleUrl, ImageIO.getUseCache(),
                ImageIO.getCacheDirectory());
        if (inStream == null)
            return null;

        // get a reader and try to cache the relevant SPI
        if (cachedReaderSPI == null) {
            reader = ImageIOExt.getImageioReader(inStream);
            if (reader != null)
                cachedReaderSPI = reader.getOriginatingProvider();
        } else
            reader = cachedReaderSPI.createReaderInstance();
        if (reader == null) {
            if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
                LOGGER.warning(new StringBuilder("Unable to get s reader for granuleDescriptor ")
                        .append(this.toString()).append(" with request ").append(request.toString())
                        .append(" Resulting in no granule loaded: Empty result").toString());
            }
            return null;
        }
        // set input
        customizeReaderInitialization(reader, hints);
        reader.setInput(inStream);

        // Checking for heterogeneous granules
        if (request.isHeterogeneousGranules()) {
            // create read parameters
            readParameters = new ImageReadParam();

            //override the overviews controller for the base layer
            imageIndex = ReadParamsController.setReadParams(
                    request.spatialRequestHelper.getRequestedResolution(), request.getOverviewPolicy(),
                    request.getDecimationPolicy(), readParameters, request.rasterManager, overviewsController);
        } else {
            imageIndex = index;
            readParameters = imageReadParameters;
        }

        //get selected level and base level dimensions
        final GranuleOverviewLevelDescriptor selectedlevel = getLevel(imageIndex, reader);

        // now create the crop grid to world which can be used to decide
        // which source area we need to crop in the selected level taking
        // into account the scale factors imposed by the selection of this
        // level together with the base level grid to world transformation
        AffineTransform2D cropWorldToGrid = new AffineTransform2D(selectedlevel.gridToWorldTransformCorner);
        cropWorldToGrid = (AffineTransform2D) cropWorldToGrid.inverse();
        // computing the crop source area which lives into the
        // selected level raster space, NOTICE that at the end we need to
        // take into account the fact that we might also decimate therefore
        // we cannot just use the crop grid to world but we need to correct
        // it.
        final Rectangle sourceArea = CRS.transform(cropWorldToGrid, intersection).toRectangle2D().getBounds();
        //gutter
        if (selectedlevel.baseToLevelTransform.isIdentity()) {
            sourceArea.grow(2, 2);
        }
        XRectangle2D.intersect(sourceArea, selectedlevel.rasterDimensions, sourceArea);//make sure roundings don't bother us
        // is it empty??
        if (sourceArea.isEmpty()) {
            if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
                LOGGER.fine("Got empty area for granuleDescriptor " + this.toString() + " with request "
                        + request.toString() + " Resulting in no granule loaded: Empty result");

            }
            return null;

        } else if (LOGGER.isLoggable(java.util.logging.Level.FINER)) {
            LOGGER.finer("Loading level " + imageIndex + " with source region: " + sourceArea + " subsampling: "
                    + readParameters.getSourceXSubsampling() + "," + readParameters.getSourceYSubsampling()
                    + " for granule:" + granuleUrl);
        }

        // Setting subsampling 
        int newSubSamplingFactor = 0;
        final String pluginName = cachedReaderSPI.getPluginClassName();
        if (pluginName != null && pluginName.equals(ImageUtilities.DIRECT_KAKADU_PLUGIN)) {
            final int ssx = readParameters.getSourceXSubsampling();
            final int ssy = readParameters.getSourceYSubsampling();
            newSubSamplingFactor = ImageIOUtilities.getSubSamplingFactor2(ssx, ssy);
            if (newSubSamplingFactor != 0) {
                if (newSubSamplingFactor > maxDecimationFactor && maxDecimationFactor != -1) {
                    newSubSamplingFactor = maxDecimationFactor;
                }
                readParameters.setSourceSubsampling(newSubSamplingFactor, newSubSamplingFactor, 0, 0);
            }
        }

        // set the source region
        readParameters.setSourceRegion(sourceArea);
        RenderedImage raster;
        try {
            // read
            raster = request.getReadType().read(readParameters, imageIndex, granuleUrl,
                    selectedlevel.rasterDimensions, reader, hints, false);

        } catch (Throwable e) {
            if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
                LOGGER.log(java.util.logging.Level.FINE,
                        "Unable to load raster for granuleDescriptor " + this.toString() + " with request "
                                + request.toString() + " Resulting in no granule loaded: Empty result",
                        e);
            }
            return null;
        }

        // use fixed source area
        sourceArea.setRect(readParameters.getSourceRegion());

        //
        // setting new coefficients to define a new affineTransformation
        // to be applied to the grid to world transformation
        // -----------------------------------------------------------------------------------
        //
        // With respect to the original envelope, the obtained planarImage
        // needs to be rescaled. The scaling factors are computed as the
        // ratio between the cropped source region sizes and the read
        // image sizes.
        //
        // place it in the mosaic using the coords created above;
        double decimationScaleX = ((1.0 * sourceArea.width) / raster.getWidth());
        double decimationScaleY = ((1.0 * sourceArea.height) / raster.getHeight());
        final AffineTransform decimationScaleTranform = XAffineTransform.getScaleInstance(decimationScaleX,
                decimationScaleY);

        // keep into account translation  to work into the selected level raster space
        final AffineTransform afterDecimationTranslateTranform = XAffineTransform
                .getTranslateInstance(sourceArea.x, sourceArea.y);

        // now we need to go back to the base level raster space
        final AffineTransform backToBaseLevelScaleTransform = selectedlevel.baseToLevelTransform;

        // now create the overall transform
        final AffineTransform finalRaster2Model = new AffineTransform(baseGridToWorld);
        finalRaster2Model.concatenate(CoverageUtilities.CENTER_TO_CORNER);

        if (!XAffineTransform.isIdentity(backToBaseLevelScaleTransform, Utils.AFFINE_IDENTITY_EPS))
            finalRaster2Model.concatenate(backToBaseLevelScaleTransform);
        if (!XAffineTransform.isIdentity(afterDecimationTranslateTranform, Utils.AFFINE_IDENTITY_EPS))
            finalRaster2Model.concatenate(afterDecimationTranslateTranform);
        if (!XAffineTransform.isIdentity(decimationScaleTranform, Utils.AFFINE_IDENTITY_EPS))
            finalRaster2Model.concatenate(decimationScaleTranform);

        // adjust roi
        if (useFootprint) {

            ROIGeometry transformed;
            try {
                transformed = roiProvider.getTransformedROI(finalRaster2Model.createInverse());
                if (transformed.getAsGeometry().isEmpty()) {
                    // inset might have killed the geometry fully
                    return null;
                }

                PlanarImage pi = PlanarImage.wrapRenderedImage(raster);
                if (!transformed.intersects(pi.getBounds())) {
                    return null;
                }
                pi.setProperty("ROI", transformed);
                raster = pi;

            } catch (NoninvertibleTransformException e) {
                if (LOGGER.isLoggable(java.util.logging.Level.INFO))
                    LOGGER.info("Unable to create a granuleDescriptor " + this.toString()
                            + " due to a problem when managing the ROI");
                return null;
            }

        }
        // keep into account translation factors to place this tile
        finalRaster2Model.preConcatenate((AffineTransform) mosaicWorldToGrid);
        final Interpolation interpolation = request.getInterpolation();

        //paranoiac check to avoid that JAI freaks out when computing its internal layouT on images that are too small
        Rectangle2D finalLayout = ImageUtilities.layoutHelper(raster, (float) finalRaster2Model.getScaleX(),
                (float) finalRaster2Model.getScaleY(), (float) finalRaster2Model.getTranslateX(),
                (float) finalRaster2Model.getTranslateY(), interpolation);
        if (finalLayout.isEmpty()) {
            if (LOGGER.isLoggable(java.util.logging.Level.INFO))
                LOGGER.info("Unable to create a granuleDescriptor " + this.toString()
                        + " due to jai scale bug creating a null source area");
            return null;
        }

        // apply the affine transform  conserving indexed color model
        final RenderingHints localHints = new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL,
                interpolation instanceof InterpolationNearest ? Boolean.FALSE : Boolean.TRUE);
        if (XAffineTransform.isIdentity(finalRaster2Model, Utils.AFFINE_IDENTITY_EPS)) {
            return new GranuleLoadingResult(raster, null, granuleUrl, doFiltering, pamDataset);
        } else {
            //
            // In case we are asked to use certain tile dimensions we tile
            // also at this stage in case the read type is Direct since
            // buffered images comes up untiled and this can affect the
            // performances of the subsequent affine operation.
            //
            final Dimension tileDimensions = request.getTileDimensions();
            if (tileDimensions != null && request.getReadType().equals(ReadType.DIRECT_READ)) {
                final ImageLayout layout = new ImageLayout();
                layout.setTileHeight(tileDimensions.width).setTileWidth(tileDimensions.height);
                localHints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));
            } else {
                if (hints != null && hints.containsKey(JAI.KEY_IMAGE_LAYOUT)) {
                    final Object layout = hints.get(JAI.KEY_IMAGE_LAYOUT);
                    if (layout != null && layout instanceof ImageLayout) {
                        localHints
                                .add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, ((ImageLayout) layout).clone()));
                    }
                }
            }
            if (hints != null && hints.containsKey(JAI.KEY_TILE_CACHE)) {
                final Object cache = hints.get(JAI.KEY_TILE_CACHE);
                if (cache != null && cache instanceof TileCache)
                    localHints.add(new RenderingHints(JAI.KEY_TILE_CACHE, (TileCache) cache));
            }
            if (hints != null && hints.containsKey(JAI.KEY_TILE_SCHEDULER)) {
                final Object scheduler = hints.get(JAI.KEY_TILE_SCHEDULER);
                if (scheduler != null && scheduler instanceof TileScheduler)
                    localHints.add(new RenderingHints(JAI.KEY_TILE_SCHEDULER, (TileScheduler) scheduler));
            }
            boolean addBorderExtender = true;
            if (hints != null && hints.containsKey(JAI.KEY_BORDER_EXTENDER)) {
                final Object extender = hints.get(JAI.KEY_BORDER_EXTENDER);
                if (extender != null && extender instanceof BorderExtender) {
                    localHints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, (BorderExtender) extender));
                    addBorderExtender = false;
                }
            }
            // BORDER extender
            if (addBorderExtender) {
                localHints.add(ImageUtilities.BORDER_EXTENDER_HINTS);
            }

            ImageWorker iw = new ImageWorker(raster);
            iw.setRenderingHints(localHints);
            iw.affine(finalRaster2Model, interpolation, request.getBackgroundValues());
            return new GranuleLoadingResult(iw.getRenderedImage(), null, granuleUrl, doFiltering, pamDataset);
        }

    } catch (IllegalStateException e) {
        if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
            LOGGER.log(java.util.logging.Level.WARNING,
                    new StringBuilder("Unable to load raster for granuleDescriptor ").append(this.toString())
                            .append(" with request ").append(request.toString())
                            .append(" Resulting in no granule loaded: Empty result").toString(),
                    e);
        }
        return null;
    } catch (org.opengis.referencing.operation.NoninvertibleTransformException e) {
        if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
            LOGGER.log(java.util.logging.Level.WARNING,
                    new StringBuilder("Unable to load raster for granuleDescriptor ").append(this.toString())
                            .append(" with request ").append(request.toString())
                            .append(" Resulting in no granule loaded: Empty result").toString(),
                    e);
        }
        return null;
    } catch (TransformException e) {
        if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
            LOGGER.log(java.util.logging.Level.WARNING,
                    new StringBuilder("Unable to load raster for granuleDescriptor ").append(this.toString())
                            .append(" with request ").append(request.toString())
                            .append(" Resulting in no granule loaded: Empty result").toString(),
                    e);
        }
        return null;

    } finally {
        try {
            if (request.getReadType() != ReadType.JAI_IMAGEREAD && inStream != null) {
                inStream.close();
            }
        } finally {
            if (request.getReadType() != ReadType.JAI_IMAGEREAD && reader != null) {
                reader.dispose();
            }
        }
    }
}

From source file:org.jimcat.services.imagemanager.ImageUtil.java

/**
 * this methode will load given image using a subsample rate
 * //  www  .  j  av a  2s .  c  o m
 * this strategie is just reading a subset of images lines ans rowes to
 * reduce memory usage and cpu time.
 * 
 * @param reader -
 *            the reader to load image from
 * @param size -
 *            the resulting image size
 * @param quality -
 *            the quality used for necessary rendering
 * @return the image as buffered image
 * @throws IOException
 */
private static BufferedImage loadImageWithSubSampling(ImageReader reader, Dimension size, ImageQuality quality)
        throws IOException {

    // prepaire image reader parameter
    ImageReadParam param = reader.getDefaultReadParam();

    // calculate subsampling values
    int width = reader.getWidth(0);
    int height = reader.getHeight(0);
    int rateX = width / size.width;
    int rateY = height / size.height;
    param.setSourceSubsampling(rateX, rateY, 0, 0);

    // load image with subsamples
    BufferedImage img = reader.read(0, param);

    // scale to final size
    BufferedImage result = getScaledInstance(img, size, quality);

    // return result
    return result;
}

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth,
        int maxHeight, boolean fill) {
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {/*w  w  w.  j  a  v  a2 s.  c om*/
            reader.setInput(stream, true, true);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            Size size = new Size(width, height, false);
            Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
            SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);

            int readerMinIndex = reader.getMinIndex();
            ImageReadParam param = reader.getDefaultReadParam();
            Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
            while (imageTypes.hasNext()) {
                try {
                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                        param.setDestinationType(imageTypeSpecifier);
                    }

                    double memoryKB = (width * height * 4) / 1024d;
                    if (memoryKB > 2000) {// check limit at 20MB
                        double free = Runtime.getRuntime().freeMemory() / 1024d;
                        if (free > memoryKB) {
                            all.setImage(reader.read(readerMinIndex, param));
                        } else {
                            // make sub sampling to save memory
                            int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
                            param.setSourceSubsampling(ratio, ratio, 0, 0);
                            all.setImage(reader.read(readerMinIndex, param));
                        }
                    } else {
                        all.setImage(reader.read(readerMinIndex, param));
                    }
                    return all;
                } catch (IllegalArgumentException e) {
                    log.warn(e.getMessage(), e);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            reader.dispose();
        }
    } else {
        log.error("No reader found for given format: " + suffix, null);
    }
    return null;
}

From source file:org.photovault.image.ImageIOImage.java

/**
 Load the image and/or metadata/*from   w ww  .  ja  v  a 2 s. c  om*/
 @param loadImage Load the image pixel data if <CODE>true</CODE>
 @param loadMetadata Load image metadata if <CODE>true</CODE>.
 @param minWidth Minimum width of the loaded image
 @param minHeight Minimum height of the loaded image
 @param isLowQualityAllowed If <code>true</code>, use larger subsampling 
 to speed up loading.
 */
private void load(boolean loadImage, boolean loadMetadata, int minWidth, int minHeight,
        boolean isLowQualityAllowed) {
    if (f != null && f.canRead()) {
        ImageReader reader = getImageReader(f);
        if (reader != null) {
            log.debug("Creating stream");
            ImageInputStream iis = null;
            try {
                iis = ImageIO.createImageInputStream(f);
                reader.setInput(iis, false, false);
                width = reader.getWidth(0);
                height = reader.getHeight(0);
                if (loadImage) {
                    RenderedImage ri = null;
                    if (isLowQualityAllowed) {
                        ri = readExifThumbnail(f);
                        if (ri == null || !isOkForThumbCreation(ri.getWidth(), ri.getHeight(), minWidth,
                                minHeight, reader.getAspectRatio(0), 0.01)) {
                            /*
                             EXIF thumbnail either did not exist or was unusable,
                             try to read subsampled version of original
                             */
                            ri = readSubsampled(reader, minWidth, minHeight);
                        }
                    } else {
                        /*
                         High quality image is requested.
                                 
                         If the image is very large, use subsampling anyway
                         to decrease memory consumption & speed up interactive 
                         operations. Anyway, most often user just views image 
                         at screen resolution
                         */
                        ImageReadParam param = reader.getDefaultReadParam();

                        if (minWidth * 2 < width && minHeight * 2 < height) {
                            param.setSourceSubsampling(2, 2, 0, 0);
                        }
                        ri = reader.read(0, param);

                    }
                    if (ri != null) {
                        /*
                         TODO: JAI seems to have problems in doing convolutions
                         for large image tiles. Split image to reasonably sized
                         tiles as a workaround for this.
                         */
                        ri = new TiledImage(ri, 256, 256);
                        image = new RenderedImageAdapter(ri);
                        originalSampleModel = image.getSampleModel();
                        originalColorModel = image.getColorModel();
                        final float[] DEFAULT_KERNEL_1D = { 0.25f, 0.5f, 0.25f };
                        ParameterBlock pb = new ParameterBlock();
                        KernelJAI kernel = new KernelJAI(DEFAULT_KERNEL_1D.length, DEFAULT_KERNEL_1D.length,
                                DEFAULT_KERNEL_1D.length / 2, DEFAULT_KERNEL_1D.length / 2, DEFAULT_KERNEL_1D,
                                DEFAULT_KERNEL_1D);
                        pb.add(kernel);
                        BorderExtender extender = BorderExtender.createInstance(BorderExtender.BORDER_COPY);
                        RenderingHints hints = JAI.getDefaultInstance().getRenderingHints();
                        if (hints == null) {
                            hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
                        } else {
                            hints.put(JAI.KEY_BORDER_EXTENDER, extender);
                        }

                        RenderedOp filter = new RenderedOp("convolve", pb, hints);
                        // javax.media.jai.operator.BoxFilterDescriptor.create( null, new Integer(2), new Integer(2), new Integer(0), new Integer(0), null );

                        // Add the subsampling operation.
                        pb = new ParameterBlock();
                        pb.addSource(filter);
                        pb.add(new Float(0.5F)).add(new Float(0.5F));
                        pb.add(new Float(0.0F)).add(new Float(0.0F));
                        pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
                        RenderedOp downSampler = new RenderedOp("scale", pb, null);

                        renderableImage = RenderableDescriptor.createRenderable(image, downSampler, null, null,
                                null, null, null);
                    } else {
                        image = null;
                        renderableImage = null;
                    }
                    imageIsLowQuality = isLowQualityAllowed;
                }
                if (loadMetadata) {
                    readImageMetadata(reader);
                }
            } catch (Exception ex) {
                log.warn(ex.getMessage());
                ex.printStackTrace();
                return;
            }
        }
    }
}

From source file:org.photovault.image.ImageIOImage.java

/**
 Read the image (either original or proper thumbnail in the same file and subsample 
 it to save memory & time. The image is subsampled so that its reasolution is the
 smallest possible that is bigger than given limits. 
         //from  ww  w . j a v a 2s.  co m
 @param reader The image reader that is used for reading the image
 @param minWidth Minimum width of the subsampled image
 @param minHeight Minimum height of the subsampled iamge
         
 @return Subsampled image.
 */

private RenderedImage readSubsampled(ImageReader reader, int minWidth, int minHeight) throws IOException {
    /*
     We try to ensure that the thumbnail is actually from the original image
     by comparing aspect ratio of it to original. This is not a perfect check
     but it will usually catch the most typical errors (like having a the original
     rotated by RAW conversion SW but still the original EXIF thumbnail.
     */
    double origAspect = reader.getAspectRatio(0);
    double aspectAccuracy = 0.01;
    int minInstanceSide = Math.max(minWidth, minHeight);

    int numThumbs = 0;
    RenderedImage image = null;
    try {
        int numImages = reader.getNumImages(true);
        if (numImages > 0) {
            numThumbs = reader.getNumThumbnails(0);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (numThumbs > 0 && isOkForThumbCreation(reader.getThumbnailWidth(0, 0), reader.getThumbnailHeight(0, 0),
            minWidth, minHeight, origAspect, aspectAccuracy)) {
        // There is a thumbanil that is big enough - use it

        log.debug("Original has thumbnail, size " + reader.getThumbnailWidth(0, 0) + " x "
                + reader.getThumbnailHeight(0, 0));
        image = reader.readThumbnail(0, 0);
        log.debug("Read thumbnail");
    } else {
        log.debug("No thumbnail in original");
        ImageReadParam param = reader.getDefaultReadParam();

        // Find the maximum subsampling rate we can still use for creating
        // a quality thumbnail. Some image format readers seem to have
        // problems with subsampling values (e.g. PNG sometimes crashed
        // the whole virtual machine, to for now let's do this only
        // with JPG.
        int subsampling = 1;
        if (reader.getFormatName().equals("JPEG")) {
            int minDim = Math.min(reader.getWidth(0), reader.getHeight(0));
            while (2 * minInstanceSide * subsampling < minDim) {
                subsampling *= 2;
            }
        }
        param.setSourceSubsampling(subsampling, subsampling, 0, 0);
        image = reader.read(0, param);
    }
    return image;
}