Example usage for java.awt.geom AffineTransform createInverse

List of usage examples for java.awt.geom AffineTransform createInverse

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform createInverse.

Prototype

public AffineTransform createInverse() throws NoninvertibleTransformException 

Source Link

Document

Returns an AffineTransform object representing the inverse transformation.

Usage

From source file:Main.java

public static Point2D.Double toSheetPoint(Point2D pScreen, AffineTransform at) {
    Point2D.Double pWorld = new Point2D.Double();
    AffineTransform at1;// www. jav  a2  s.c o m

    try {
        at1 = at.createInverse();
        at1.transform(pScreen, pWorld);
    } catch (NoninvertibleTransformException e) {
    }

    return pWorld;
}

From source file:edu.valelab.gaussianfit.datasettransformations.CoordinateMapper.java

public static void logAffineTransform(AffineTransform af) {
    AffineTransform tmp = new AffineTransform(af);
    try {//from  www.j  ava2 s. c  o  m
        AffineTransform inv = tmp.createInverse();
        ij.IJ.log(inv.toString());
    } catch (NoninvertibleTransformException ex) {
        ReportingUtils.logError(ex, "Problem while printing affine transform");
    }
}

From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java

@Override
protected void processOperator(PDFOperator operator, List<COSBase> arguments) throws IOException {
    String operation = operator.getOperation();
    if (operation.equals("Do")) {
        COSName objectName = (COSName) arguments.get(0);
        Map<?, ?> xobjects = getResources().getXObjects();
        PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
        if (xobject instanceof PDXObjectImage) {
            try {
                PDXObjectImage image = (PDXObjectImage) xobject;
                SignaturePlaceholderData data = checkImage(image);
                if (data != null) {
                    PDPage page = getCurrentPage();
                    Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
                    int pageRotation = page.findRotation();
                    pageRotation = pageRotation % 360;
                    double rotationInRadians = Math.toRadians(pageRotation);//(page.findRotation() * Math.PI) / 180;

                    AffineTransform rotation = new AffineTransform();
                    rotation.setToRotation(rotationInRadians);
                    AffineTransform rotationInverse = rotation.createInverse();
                    Matrix rotationInverseMatrix = new Matrix();
                    rotationInverseMatrix.setFromAffineTransform(rotationInverse);
                    Matrix rotationMatrix = new Matrix();
                    rotationMatrix.setFromAffineTransform(rotation);

                    Matrix unrotatedCTM = ctm.multiply(rotationInverseMatrix);

                    float x = unrotatedCTM.getXPosition();
                    float yPos = unrotatedCTM.getYPosition();
                    float yScale = unrotatedCTM.getYScale();
                    float y = yPos + yScale;
                    float w = unrotatedCTM.getXScale();

                    logger.debug("Page height: {}", page.findCropBox().getHeight());
                    logger.debug("Page width: {}", page.findCropBox().getWidth());

                    if (pageRotation == 90) {
                        y = page.findCropBox().getWidth() - (y * (-1));
                    } else if (pageRotation == 180) {
                        x = page.findCropBox().getWidth() + x;
                        y = page.findCropBox().getHeight() - (y * (-1));
                    } else if (pageRotation == 270) {
                        x = page.findCropBox().getHeight() + x;
                    }//  ww  w. ja v a 2  s  .c  o m

                    String posString = "p:" + currentPage + ";x:" + x + ";y:" + y + ";w:" + w;

                    logger.debug("Found Placeholder at: {}", posString);
                    try {
                        data.setTablePos(new TablePos(posString));
                        data.setPlaceholderName(objectName.getName());
                        placeholders.add(data);
                    } catch (PdfAsException e) {
                        throw new WrappedIOException(e);
                    }
                }
            } catch (NoninvertibleTransformException e) {
                throw new WrappedIOException(e);
            }
        }
    } else {
        super.processOperator(operator, arguments);
    }
}

From source file:DefaultGraphics2D.java

/**
 * Renders an image, applying a transform from image space into user space
 * before drawing. The transformation from user space into device space is
 * done with the current <code>Transform</code> in the
 * <code>Graphics2D</code>. The specified transformation is applied to the
 * image before the transform attribute in the <code>Graphics2D</code>
 * context is applied. The rendering attributes applied include the
 * <code>Clip</code>, <code>Transform</code>, and <code>Composite</code>
 * attributes. Note that no rendering is done if the specified transform is
 * noninvertible.//from  w w  w. j a  va2 s  .  c o  m
 * 
 * @param img
 *          the <code>Image</code> to be rendered
 * @param xform
 *          the transformation from image space into user space
 * @param obs
 *          the {@link ImageObserver} to be notified as more of the
 *          <code>Image</code> is converted
 * @return <code>true</code> if the <code>Image</code> is fully loaded and
 *         completely rendered; <code>false</code> if the <code>Image</code>
 *         is still being loaded.
 * @see #transform
 * @see #setTransform
 * @see #setComposite
 * @see #clip
 * @see #setClip(Shape)
 */
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
    boolean retVal = true;

    if (xform.getDeterminant() != 0) {
        AffineTransform inverseTransform = null;
        try {
            inverseTransform = xform.createInverse();
        } catch (NoninvertibleTransformException e) {
            // Should never happen since we checked the
            // matrix determinant
            throw new Error(e.getMessage());
        }

        gc.transform(xform);
        retVal = drawImage(img, 0, 0, null);
        gc.transform(inverseTransform);
    } else {
        AffineTransform savTransform = new AffineTransform(gc.getTransform());
        gc.transform(xform);
        retVal = drawImage(img, 0, 0, null);
        gc.setTransform(savTransform);
    }

    return retVal;

}

From source file:org.apache.pdfbox.pdmodel.font.PDSimpleFont.java

/**
 * This will draw a string on a canvas using the font.
 *
 * @param g2d The graphics to draw onto.
 * @param at The transformation matrix with all information for scaling and shearing of the font.
 * @param x The x coordinate to draw at.
 * @param y The y coordinate to draw at.
 * @param glyphs The GlyphVector containing the glyphs to be drawn.
 *
 *///from   w ww.  ja  v a 2s  .  c  o m
protected void writeFont(final Graphics2D g2d, final AffineTransform at, final float x, final float y,
        final GlyphVector glyphs) {
    // check if we have a rotation
    if (!at.isIdentity()) {
        try {
            AffineTransform atInv = at.createInverse();
            // do only apply the size of the transform, rotation will be realized by rotating the graphics,
            // otherwise the hp printers will not render the font
            // apply the transformation to the graphics, which should be the same as applying the
            // transformation itself to the text
            g2d.transform(at);
            // translate the coordinates
            Point2D.Float newXy = new Point2D.Float(x, y);
            atInv.transform(new Point2D.Float(x, y), newXy);
            g2d.drawGlyphVector(glyphs, (float) newXy.getX(), (float) newXy.getY());
            // restore the original transformation
            g2d.transform(atInv);
        } catch (NoninvertibleTransformException e) {
            LOG.error("Error in " + getClass().getName() + ".writeFont", e);
        }
    } else {
        g2d.drawGlyphVector(glyphs, x, y);
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.Type1ShadingContext.java

/**
 * Constructor creates an instance to be used for fill operations.
 *
 * @param shading the shading type to be used
 * @param colorModel the color model to be used
 * @param xform transformation for user to device space
 * @param matrix the pattern matrix concatenated with that of the parent content stream
 */// w w  w. j a v  a  2 s.  c  o m
Type1ShadingContext(PDShadingType1 shading, ColorModel colorModel, AffineTransform xform, Matrix matrix)
        throws IOException {
    super(shading, colorModel, xform, matrix);
    this.type1ShadingType = shading;

    // (Optional) An array of four numbers [ xmin xmax ymin ymax ] 
    // specifying the rectangular domain of coordinates over which the 
    // color function(s) are defined. Default value: [ 0.0 1.0 0.0 1.0 ].
    if (shading.getDomain() != null) {
        domain = shading.getDomain().toFloatArray();
    } else {
        domain = new float[] { 0, 1, 0, 1 };
    }

    try {
        // get inverse transform to be independent of 
        // shading matrix and current user / device space 
        // when handling actual pixels in getRaster()
        rat = shading.getMatrix().createAffineTransform().createInverse();
        rat.concatenate(matrix.createAffineTransform().createInverse());
        rat.concatenate(xform.createInverse());
    } catch (NoninvertibleTransformException ex) {
        LOG.error(ex, ex);
    }
}

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.
*//*w w w .  j  a va 2  s  . c o m*/
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.geotools.gce.imagemosaic.RasterLayerResponse.java

private RenderedImage postProcessRaster(RenderedImage image) {
    // alpha on the final mosaic
    if (finalTransparentColor != null) {
        if (LOGGER.isLoggable(Level.FINE))
            LOGGER.fine("Support for alpha on final mosaic");
        return ImageUtilities.maskColor(finalTransparentColor, image);

    }/*  ww  w  .ja  v  a2  s .co  m*/
    if (!needsReprojection) {
        try {

            // creating source grid to world corrected to the pixel corner
            final AffineTransform sourceGridToWorld = new AffineTransform(
                    (AffineTransform) finalGridToWorldCorner);

            // target world to grid at the corner
            final AffineTransform targetGridToWorld = new AffineTransform(request.getRequestedGridToWorld());
            targetGridToWorld.concatenate(CoverageUtilities.CENTER_TO_CORNER);

            // target world to grid at the corner
            final AffineTransform targetWorldToGrid = targetGridToWorld.createInverse();
            // final complete transformation
            targetWorldToGrid.concatenate(sourceGridToWorld);

            //update final grid to world
            finalGridToWorldCorner = new AffineTransform2D(targetGridToWorld);
            //
            // Check and see if the affine transform is doing a copy.
            // If so call the copy operation.
            //
            // we are in raster space here, so 1E-3 is safe
            if (XAffineTransform.isIdentity(targetWorldToGrid, Utils.AFFINE_IDENTITY_EPS))
                return image;

            // create final image
            // TODO this one could be optimized further depending on how the affine is created
            //
            // 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 Hints localHints = new Hints(hints);
            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(ImageUtilities.EXTEND_BORDER_BY_COPYING);
                }
            }

            //                         image = AffineDescriptor.create(image, targetWorldToGrid , interpolation, backgroundValues, localHints);
            ImageWorker iw = new ImageWorker(image);
            iw.setRenderingHints(localHints);
            iw.affine(targetWorldToGrid, interpolation, backgroundValues);
            image = iw.getRenderedImage();
        } catch (NoninvertibleTransformException e) {
            if (LOGGER.isLoggable(Level.SEVERE)) {
                LOGGER.log(Level.SEVERE, "Unable to create the requested mosaic ", e);
            }
        }
    }
    return image;
}

From source file:org.kepler.gui.KeplerGraphFrame.java

/**
 * Return the size of the visible part of the canvas, in canvas coordinates.
 * //from ww  w . ja  v  a2s  . co m
 * @return Rectangle2D
 */
public Rectangle2D getVisibleSize() {
    AffineTransform current = _jgraph.getGraphPane().getCanvas().getCanvasPane().getTransformContext()
            .getTransform();
    AffineTransform inverse;
    try {
        inverse = current.createInverse();
    } catch (NoninvertibleTransformException e) {
        throw new RuntimeException(e.toString());
    }
    Dimension size = _jgraph.getGraphPane().getCanvas().getSize();
    Rectangle2D visibleRect = new Rectangle2D.Double(0, 0, size.getWidth(), size.getHeight());
    return ShapeUtilities.transformBounds(visibleRect, inverse);
}

From source file:org.uva.itast.blended.omr.pages.PageImage.java

/**
 * Convert from  pixel-space to paper-space.
 * Paper-space refers to logical area of the paper in the image.
 * Pixel-space refers to entire area of the image that contains the image of the paper. (Maybe with offset and rotation)
 * @param i/*from w  w w .  j  av  a  2  s  .  c  om*/
 * @param j
 * @return
 * @throws NoninvertibleTransformException 
 */
public Point2D toMilimeters(int i, int j) {

    try {
        AffineTransform tr = getAllignmentInfo();
        AffineTransform inv;
        inv = tr.createInverse();
        Point2D pixeles = new Point(i, j);
        Point2D dest = new Point2D.Double();
        return inv.transform(pixeles, dest);
    } catch (NoninvertibleTransformException e) {
        throw new RuntimeException("error page definition.", e);
    }

}