Example usage for java.awt.geom AffineTransform AffineTransform

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

Introduction

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

Prototype

public AffineTransform(double[] flatmatrix) 

Source Link

Document

Constructs a new AffineTransform from an array of double precision values representing either the 4 non-translation entries or the 6 specifiable entries of the 3x3 transformation matrix.

Usage

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

/**
 * Retrieves the original grid to world transformation for this {@link AbstractGridCoverage2DReader}.
 * //from ww w. ja v a2 s  .  co m
 * @param pixInCell specifies the datum of the transformation we want.
 * @return the original grid to world transformation
 */
public static MathTransform getOriginalGridToWorld(MathTransform raster2Model, final PixelInCell pixInCell) {
    // we do not have to change the pixel datum
    if (pixInCell == PixelInCell.CELL_CENTER)
        return raster2Model;

    // we do have to change the pixel datum
    if (raster2Model instanceof AffineTransform) {
        final AffineTransform tr = new AffineTransform((AffineTransform) raster2Model);
        tr.concatenate(AffineTransform.getTranslateInstance(-0.5, -0.5));
        return ProjectiveTransform.create(tr);
    }
    if (raster2Model instanceof IdentityTransform) {
        final AffineTransform tr = new AffineTransform(1, 0, 0, 1, 0, 0);
        tr.concatenate(AffineTransform.getTranslateInstance(-0.5, -0.5));
        return ProjectiveTransform.create(tr);
    }
    throw new IllegalStateException("This grid to world transform is invalud!");
}

From source file:org.micromanager.plugins.magellan.surfacesandregions.SurfaceInterpolator.java

private void fitXYPositionsToConvexHull(double overlap) throws InterruptedException {
    int fullTileWidth = JavaLayerImageConstructor.getInstance().getImageWidth();
    int fullTileHeight = JavaLayerImageConstructor.getInstance().getImageHeight();
    int overlapX = (int) (JavaLayerImageConstructor.getInstance().getImageWidth() * overlap / 100);
    int overlapY = (int) (JavaLayerImageConstructor.getInstance().getImageHeight() * overlap / 100);
    int tileWidthMinusOverlap = fullTileWidth - overlapX;
    int tileHeightMinusOverlap = fullTileHeight - overlapY;
    int pixelPadding = (int) (xyPadding_um_ / Magellan.getCore().getPixelSizeUm());
    numRows_ = (int) Math
            .ceil((boundYPixelMax_ - boundYPixelMin_ + pixelPadding) / (double) tileHeightMinusOverlap);
    numCols_ = (int) Math
            .ceil((boundXPixelMax_ - boundXPixelMin_ + pixelPadding) / (double) tileWidthMinusOverlap);

    //take center of bounding box and create grid
    int pixelCenterX = boundXPixelMin_ + (boundXPixelMax_ - boundXPixelMin_) / 2;
    int pixelCenterY = boundYPixelMin_ + (boundYPixelMax_ - boundYPixelMin_) / 2;

    AffineTransform transform = AffineUtils.getAffineTransform(getCurrentPixelSizeConfig(), 0, 0);
    ArrayList<XYStagePosition> positions = new ArrayList<XYStagePosition>();
    Point2D.Double gridCenterStageCoords = new Point2D.Double();
    transform.transform(new Point2D.Double(pixelCenterX, pixelCenterY), gridCenterStageCoords);
    gridCenterStageCoords.x += convexHullVertices_[0].getX();
    gridCenterStageCoords.y += convexHullVertices_[0].getY();
    //set affine transform translation relative to grid center
    double[] transformMaxtrix = new double[6];
    transform.getMatrix(transformMaxtrix);
    transformMaxtrix[4] = gridCenterStageCoords.x;
    transformMaxtrix[5] = gridCenterStageCoords.y;
    //create new transform with translation applied
    transform = new AffineTransform(transformMaxtrix);
    //add all positions of rectangle around convex hull
    for (int col = 0; col < numCols_; col++) {
        double xPixelOffset = (col - (numCols_ - 1) / 2.0) * (tileWidthMinusOverlap);
        for (int row = 0; row < numRows_; row++) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }//from  w w  w . j  av  a  2  s .  co m
            double yPixelOffset = (row - (numRows_ - 1) / 2.0) * (tileHeightMinusOverlap);
            Point2D.Double pixelPos = new Point2D.Double(xPixelOffset, yPixelOffset);
            Point2D.Double stagePos = new Point2D.Double();
            transform.transform(pixelPos, stagePos);
            AffineTransform posTransform = AffineUtils.getAffineTransform(getCurrentPixelSizeConfig(),
                    stagePos.x, stagePos.y);
            positions.add(new XYStagePosition(stagePos, tileWidthMinusOverlap, tileHeightMinusOverlap,
                    fullTileWidth, fullTileHeight, row, col, posTransform));
        }
    }
    //delete positions squares (+padding) that do not overlap convex hull
    for (int i = positions.size() - 1; i >= 0; i--) {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        XYStagePosition pos = positions.get(i);
        //create square region correpsonding to stage pos
        Region<Euclidean2D> square = getStagePositionRegion(pos);
        //if convex hull and position have no intersection, delete
        Region<Euclidean2D> intersection = regionFacotry_.intersection(square, convexHullRegion_);
        if (intersection.isEmpty()) {
            positions.remove(i);
        }
        square.getBoundarySize();
    }
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }
    synchronized (xyPositionLock_) {
        xyPositions_ = positions;
        xyPositionLock_.notifyAll();
    }

    //let manger know new parmas caluclated
    manager_.updateSurfaceTableAndCombos();
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

private void drawBufferedImage(BufferedImage image, AffineTransform at) throws IOException {
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    setClip();/*from w  w  w  .  jav a  2 s  .  c o m*/
    PDSoftMask softMask = getGraphicsState().getSoftMask();
    if (softMask != null) {
        AffineTransform imageTransform = new AffineTransform(at);
        imageTransform.scale(1, -1);
        imageTransform.translate(0, -1);
        Paint awtPaint = new TexturePaint(image, new Rectangle2D.Double(imageTransform.getTranslateX(),
                imageTransform.getTranslateY(), imageTransform.getScaleX(), imageTransform.getScaleY()));
        awtPaint = applySoftMaskToPaint(awtPaint, softMask);
        graphics.setPaint(awtPaint);
        Rectangle2D unitRect = new Rectangle2D.Float(0, 0, 1, 1);
        graphics.fill(at.createTransformedShape(unitRect));
    } else {
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        AffineTransform imageTransform = new AffineTransform(at);
        imageTransform.scale(1.0 / width, -1.0 / height);
        imageTransform.translate(0, -height);
        graphics.drawImage(image, imageTransform, null);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#setTransform(AffineTransform)
 */// ww  w.  j  a  va  2s  . c o m
@Override
public void setTransform(final AffineTransform t) {
    transform = new AffineTransform(t);
    this.stroke = transformStroke(originalStroke);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#getTransform()
 */
@Override
public AffineTransform getTransform() {
    return new AffineTransform(transform);
}

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);

    }/* w  w w  .j  av  a 2  s.  c o 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.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics#create()/*from  w ww .  jav  a2s .  c om*/
 */
@Override
public Graphics create() {
    final PdfGraphics2D g2 = new PdfGraphics2D();
    g2.transform = new AffineTransform(this.transform);
    g2.metaData = this.metaData;
    g2.paint = this.paint;
    g2.fillGState = this.fillGState;
    g2.strokeGState = this.strokeGState;
    g2.background = this.background;
    g2.mediaTracker = this.mediaTracker;
    g2.setFont(this.font);
    g2.cb = this.cb.getDuplicate();
    g2.cb.saveState();
    g2.width = this.width;
    g2.height = this.height;
    g2.followPath(new Area(new Rectangle2D.Float(0, 0, width, height)), PdfGraphics2D.CLIP);
    if (this.clip != null) {
        g2.clip = new Area(this.clip);
    }
    g2.stroke = stroke;
    g2.originalStroke = originalStroke;
    g2.strokeOne = (BasicStroke) g2.transformStroke(g2.strokeOne);
    g2.oldStroke = g2.strokeOne;
    g2.setStrokeDiff(g2.oldStroke, null);
    g2.cb.saveState();
    if (g2.clip != null) {
        g2.followPath(g2.clip, PdfGraphics2D.CLIP);
    }
    g2.parent = this;
    if (this.kids == null) {
        this.kids = new ArrayList();
    }
    // This is disgusting. You really cant override the buffer on the fixed position. The way the dispose() code
    // handles the cleanup, this will create a huge mess ...
    // this.kids.add(new Integer(cb.getInternalBuffer().size()));
    this.kids.add(g2);
    return g2;
}

From source file:org.csstudio.utility.batik.SVGHandler.java

private Shape calculateShape() {
    double width = originalDimension.getWidth();
    double height = originalDimension.getHeight();

    double[] flatmatrix = new double[] { matrix[0][0], matrix[1][0], matrix[0][1], matrix[1][1] };
    AffineTransform at = new AffineTransform(flatmatrix);
    Shape curAOI = new Rectangle2D.Double(0, 0, width, height);
    return at.createTransformedShape(curAOI);
}

From source file:org.apache.fop.render.AbstractRenderer.java

/**
 * Converts a millipoint-based transformation matrix to points.
 * @param at a millipoint-based transformation matrix
 * @return a point-based transformation matrix
 *///  w ww.j  a  v a2  s. c om
protected AffineTransform mptToPt(AffineTransform at) {
    double[] matrix = new double[6];
    at.getMatrix(matrix);
    //Convert to points
    matrix[4] = matrix[4] / 1000;
    matrix[5] = matrix[5] / 1000;
    return new AffineTransform(matrix);
}

From source file:org.apache.fop.render.AbstractRenderer.java

/**
 * Converts a point-based transformation matrix to millipoints.
 * @param at a point-based transformation matrix
 * @return a millipoint-based transformation matrix
 *///from  w  w w . ja v a 2 s.c  o  m
protected AffineTransform ptToMpt(AffineTransform at) {
    double[] matrix = new double[6];
    at.getMatrix(matrix);
    //Convert to millipoints
    //Math.round() because things like this can happen: 65.6 * 1000 = 65.599999999999999
    //which is bad for testing
    matrix[4] = Math.round(matrix[4] * 1000);
    matrix[5] = Math.round(matrix[5] * 1000);
    return new AffineTransform(matrix);
}