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 m00, double m10, double m01, double m11, double m02, double m12) 

Source Link

Document

Constructs a new AffineTransform from 6 double precision values representing the 6 specifiable entries of the 3x3 transformation matrix.

Usage

From source file:MathFunctions.java

public static AffineTransform generateAffineTransformFromPointPairs(
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    RealMatrix u = new Array2DRowRealMatrix(pointPairs.size(), 3);
    RealMatrix v = new Array2DRowRealMatrix(pointPairs.size(), 3);

    // Create u (source) and v (dest) matrices whose row vectors
    // are [x,y,1] for each Point2D.Double:

    int i = 0;//from www.j a  va  2  s  . c  o  m
    for (Map.Entry pair : pointPairs.entrySet()) {
        Point2D.Double uPt = (Point2D.Double) pair.getKey();
        Point2D.Double vPt = (Point2D.Double) pair.getValue();

        insertPoint2DInMatrix(u, uPt, i);
        insertPoint2DInMatrix(v, vPt, i);

        i++;
    }
    // Find the 3x3 linear least squares solution to u*m'=v
    // (the last row should be [0,0,1]):
    DecompositionSolver solver = (new QRDecompositionImpl(u)).getSolver();
    double[][] m = solver.solve(v).transpose().getData();

    // Create an AffineTransform object from the elements of m
    // (the last row is omitted as specified in AffineTransform class):
    return new AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][2], m[1][2]);
}

From source file:Matrix.java

/**
 * Create an affine transform from this matrix's values.
 * //  www.  j ava  2 s  . c  o m
 * @return An affine transform with this matrix's values.
 */
public AffineTransform createAffineTransform() {
    AffineTransform retval = new AffineTransform(single[0], single[1], single[3], single[4], single[6],
            single[7]);
    return retval;
}

From source file:org.apache.pdfbox.util.operator.pagedrawer.BeginInlineImage.java

/**
 * process : BI : begin inline image./*from   w ww .  java2s.  c  om*/
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws IOException If there is an error displaying the inline image.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    PageDrawer drawer = (PageDrawer) context;
    PDPage page = drawer.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        log.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = drawer.getPageSize().getHeight();

    Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    drawer.drawImage(awtImage, at);
}

From source file:edu.umass.cs.iesl.pdf2meta.cli.extract.pdfbox.pagedrawer.BeginInlineImage.java

/**
 * process : BI : begin inline image./*w w w .  ja v a 2 s .c o m*/
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws java.io.IOException If there is an error displaying the inline image.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context;

    PDPage page = drawer.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        log.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = drawer.getPageSize().getHeight();

    Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    drawer.drawImage(awtImage, at);
}

From source file:com.github.dougkelly88.FLIMPlateReaderGUI.InstrumentInterfaceClasses.XYZMotionInterface.java

public static AffineTransform deriveAffineTransform(Point2D.Double[] xplt, Point2D.Double[] stage) {
    // GENERAL CASE:    
    // if S is stage space and P is XPLT plate space, 
    // and T is the matrix to transform between the two:
    // S = TP/*w w w .  ja v a2  s.c  o  m*/
    // SP^-1 = TPP^-1
    // T = SP^-1;
    // where S is a 2x3 matrix with 3 points, T is a 2x3 matrix and P is a 
    // 3x3 matrix with 3 points and the bottom row occupied by ones...

    double[][] Parr = { { xplt[0].getX(), xplt[1].getX(), xplt[2].getX() },
            { xplt[0].getY(), xplt[1].getY(), xplt[2].getY() }, { 1, 1, 1 } };
    RealMatrix P = MatrixUtils.createRealMatrix(Parr);

    double[][] Sarr = { { stage[0].getX(), stage[1].getX(), stage[2].getX() },
            { stage[0].getY(), stage[1].getY(), stage[2].getY() } };
    RealMatrix S = MatrixUtils.createRealMatrix(Sarr);

    RealMatrix Pinv = (new LUDecomposition(P)).getSolver().getInverse();
    RealMatrix transformationMatrix = S.multiply(Pinv);

    double m00 = transformationMatrix.getEntry(0, 0);
    double m01 = transformationMatrix.getEntry(0, 1);
    double m02 = transformationMatrix.getEntry(0, 2);
    double m10 = transformationMatrix.getEntry(1, 0);
    double m11 = transformationMatrix.getEntry(1, 1);
    double m12 = transformationMatrix.getEntry(1, 2);

    return new AffineTransform(m00, m10, m01, m11, m02, m12);
}

From source file:org.apache.fop.afp.svg.AFPGraphicsConfiguration.java

/**
 * The normalizing transform (1:1) (since we currently
 * render images at 72dpi, which we might want to change
 * in the future)./*w  w w  .  j  a  va2  s  .  c o  m*/
 *
 * @return the normalizing transform for the configuration
 */
public AffineTransform getNormalizingTransform() {
    LOG.debug("getNormalizingTransform()");
    if (normalizingTransform == null) {
        normalizingTransform = new AffineTransform(2, 0, 0, 2, 0, 0);
    }
    return normalizingTransform;
}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.BeginInlineImage.java

/**
 * process : BI : begin inline image./*from   w w w  .  j av a2s .  c o  m*/
 *
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws IOException If there is an error displaying the inline image.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context;
    PDPage page = extractor.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    if (params.isStencil()) {
        //TODO implement inline image stencil masks 
        LOG.warn("Stencil masks are not implemented, background may be incorrect");
    }
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        LOG.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = extractor.getPageSize().getHeight();

    Matrix ctm = extractor.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    extractor.drawImage(awtImage, at);
}

From source file:org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder.java

@Override
public void createAffineTransform(byte[] params) {
    AffineTransform transform = new AffineTransform(params[0], params[1], params[2], params[3], params[4],
            params[5]);/*from www.  j  a  va2  s.c om*/
    pdfStructure.setAffineTransform(transform);
    log.info("Matrix has been added");
}

From source file:net.sf.maltcms.chromaui.annotations.XYSelectableShapeAnnotation.java

/**
 *
 * @param arg0/*from   www  .ja  v a2s  .c o  m*/
 * @param arg1
 * @param arg2
 * @param arg3
 * @param arg4
 * @param arg5
 * @param arg6
 */
@Override
public void draw(Graphics2D arg0, XYPlot arg1, Rectangle2D arg2, ValueAxis arg3, ValueAxis arg4, int arg5,
        PlotRenderingInfo arg6) {
    //System.out.println("Annotation "+toString()+" is active: "+isActive());
    PlotOrientation orientation = arg1.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(arg1.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(arg1.getRangeAxisLocation(), orientation);

    // compute transform matrix elements via sample points. Assume no
    // rotation or shear.
    Rectangle2D bounds = this.s.getBounds2D();
    double x0 = bounds.getMinX();
    double x1 = bounds.getMaxX();
    double xx0 = arg3.valueToJava2D(x0, arg2, domainEdge);
    double xx1 = arg3.valueToJava2D(x1, arg2, domainEdge);
    double m00 = (xx1 - xx0) / (x1 - x0);
    double m02 = xx0 - x0 * m00;

    double y0 = bounds.getMaxY();
    double y1 = bounds.getMinY();
    double yy0 = arg4.valueToJava2D(y0, arg2, rangeEdge);
    double yy1 = arg4.valueToJava2D(y1, arg2, rangeEdge);
    double m11 = (yy1 - yy0) / (y1 - y0);
    double m12 = yy0 - m11 * y0;

    //  create transform & transform shape
    Shape s = null, ch = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        AffineTransform t1 = new AffineTransform(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
        AffineTransform t2 = new AffineTransform(m11, 0.0f, 0.0f, m00, m12, m02);
        s = t1.createTransformedShape(this.s);
        s = t2.createTransformedShape(s);
        //            ch = t1.createTransformedShape(this.ch);
        //            ch = t2.createTransformedShape(ch);
    } else if (orientation == PlotOrientation.VERTICAL) {
        AffineTransform t = new AffineTransform(m00, 0, 0, m11, m02, m12);
        s = t.createTransformedShape(this.s);
        //            ch = t.createTransformedShape(this.ch);
    }

    if (this.active) {
        arg0.setPaint(this.highlight);
        //            double x = s.getBounds2D().getX();
        //            double y = s.getBounds2D().getY();
        //            double w = s.getBounds2D().getWidth();
        //            double h = s.getBounds2D().getHeight();
        //            Shape e = new Ellipse2D.Double(x, y, w, h);
        arg0.fill(s);
        arg0.setPaint(this.outline);
        arg0.draw(s);
        //            arg0.setStroke(this.stroke);
        //            arg0.draw(ch);
    } else {
        arg0.setPaint(this.fill);
        arg0.fill(s);
        arg0.setPaint(this.outline);
        arg0.draw(s);
    }
    addEntity(arg6, s, arg5, getToolTipText(), getURL());
    this.xyta.draw(arg0, arg1, arg2, arg3, arg4, arg5, arg6);
}

From source file:org.apache.fop.render.pdf.PDFDocumentHandler.java

/** {@inheritDoc} */
public void startPage(int index, String name, String pageMasterName, Dimension size) throws IFException {
    this.pdfResources = this.pdfDoc.getResources();

    PageBoundaries boundaries = new PageBoundaries(size, getContext().getForeignAttributes());

    Rectangle trimBox = boundaries.getTrimBox();
    Rectangle bleedBox = boundaries.getBleedBox();
    Rectangle mediaBox = boundaries.getMediaBox();
    Rectangle cropBox = boundaries.getCropBox();

    // set scale attributes
    double scaleX = 1;
    double scaleY = 1;
    String scale = (String) getContext().getForeignAttribute(PageScale.EXT_PAGE_SCALE);
    Point2D scales = PageScale.getScale(scale);
    if (scales != null) {
        scaleX = scales.getX();/*from   w  ww .j  a v a 2 s . c om*/
        scaleY = scales.getY();
    }

    //PDF uses the lower left as origin, need to transform from FOP's internal coord system
    AffineTransform boxTransform = new AffineTransform(scaleX / 1000, 0, 0, -scaleY / 1000, 0,
            scaleY * size.getHeight() / 1000);

    this.currentPage = this.pdfDoc.getFactory().makePage(this.pdfResources, index,
            toPDFCoordSystem(mediaBox, boxTransform), toPDFCoordSystem(cropBox, boxTransform),
            toPDFCoordSystem(bleedBox, boxTransform), toPDFCoordSystem(trimBox, boxTransform));
    if (accessEnabled) {
        logicalStructureHandler.startPage(currentPage);
    }

    pdfUtil.generatePageLabel(index, name);

    currentPageRef = new PageReference(currentPage, size);
    this.pageReferences.put(Integer.valueOf(index), currentPageRef);

    this.generator = new PDFContentGenerator(this.pdfDoc, this.outputStream, this.currentPage);
    // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFPainter's
    AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0, (scaleY * size.height) / 1000f);
    basicPageTransform.scale(scaleX, scaleY);
    generator.saveGraphicsState();
    generator.concatenate(basicPageTransform);
}