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:org.apache.cocoon.reading.ImageReader.java

/**
 * Returns the affine transform that implements the scaling.
 * The behavior is the following: if both the new width and height values
 * are positive, the image is rescaled according to these new values and
 * the original aspect ratio is lost./*from   www .j ava2s.c  om*/
 * Otherwise, if one of the two parameters is zero or negative, the
 * aspect ratio is maintained and the positive parameter indicates the
 * scaling.
 * If both new values are zero or negative, no scaling takes place (a unit
 * transformation is applied).
 */
private AffineTransform getTransform(double ow, double oh, double nw, double nh) {
    double wm = 1.0d;
    double hm = 1.0d;

    if (fitUniform) {
        //
        // Compare aspect ratio of image vs. that of the "box"
        // defined by nw and nh
        //
        if (ow / oh > nw / nh) {
            nh = 0; // Original image is proportionately wider than the box,
                    // so scale to fit width
        } else {
            nw = 0; // Scale to fit height
        }
    }

    if (nw > 0) {
        wm = nw / ow;
        if (nh > 0) {
            hm = nh / oh;
        } else {
            hm = wm;
        }
    } else {
        if (nh > 0) {
            hm = nh / oh;
            wm = hm;
        }
    }

    if (!enlarge) {
        if ((nw > ow && nh <= 0) || (nh > oh && nw <= 0)) {
            wm = 1.0d;
            hm = 1.0d;
        } else if (nw > ow) {
            wm = 1.0d;
        } else if (nh > oh) {
            hm = 1.0d;
        }
    }
    return new AffineTransform(wm, 0.0d, 0.0d, hm, 0.0d, 0.0d);
}

From source file:ch.rasc.downloadchart.DownloadChartServlet.java

private static void handlePdf(HttpServletResponse response, byte[] imageData, Integer width, Integer height,
        String filename, PdfOptions options) throws IOException {

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".pdf\";");

    try (PDDocument document = new PDDocument()) {
        PDRectangle format = PDPage.PAGE_SIZE_A4;
        if (options != null) {
            if ("A3".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A3;
            } else if ("A5".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A5;
            } else if ("Letter".equals(options.format)) {
                format = PDPage.PAGE_SIZE_LETTER;
            } else if ("Legal".equals(options.format)) {
                format = new PDRectangle(215.9f * MM_TO_UNITS, 355.6f * MM_TO_UNITS);
            } else if ("Tabloid".equals(options.format)) {
                format = new PDRectangle(279 * MM_TO_UNITS, 432 * MM_TO_UNITS);
            } else if (options.width != null && options.height != null) {
                Float pdfWidth = convertToPixel(options.width);
                Float pdfHeight = convertToPixel(options.height);
                if (pdfWidth != null && pdfHeight != null) {
                    format = new PDRectangle(pdfWidth, pdfHeight);
                }//from  w w  w  . java 2s . co  m
            }
        }

        PDPage page = new PDPage(format);

        if (options != null && "landscape".equals(options.orientation)) {
            page.setRotation(90);
        }

        document.addPage(page);

        BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));

        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {

            PDPixelMap ximage = new PDPixelMap(document, originalImage);

            Dimension newDimension = calculateDimension(originalImage, width, height);
            int imgWidth = ximage.getWidth();
            int imgHeight = ximage.getHeight();
            if (newDimension != null) {
                imgWidth = newDimension.width;
                imgHeight = newDimension.height;
            }

            Float border = options != null ? convertToPixel(options.border) : null;
            if (border == null) {
                border = 0.0f;
            }

            AffineTransform transform;

            if (page.getRotation() == null) {
                float scale = (page.getMediaBox().getWidth() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight * scale);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight);
                }

            } else {
                float scale = (page.getMediaBox().getHeight() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight * scale + border,
                            border);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight + border, border);
                }

                transform.rotate(1.0 * Math.PI / 2.0);
            }

            contentStream.drawXObject(ximage, transform);

        }

        try {
            document.save(response.getOutputStream());
        } catch (COSVisitorException e) {
            throw new IOException(e);
        }
    }
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

/**
* Creates an AffineTransform object that maps a source planar coordinate system to
* a destination planar coordinate system. At least three point pairs are needed.
* 
* @pointPairs is a Map of points measured in the two coordinates systems (srcPt->destPt)
*//*from  w  w w.  j  a v  a2 s . c o  m*/
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;
    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:org.fao.geonet.services.region.GetMap.java

public static AffineTransform worldToScreenTransform(Envelope mapExtent, Dimension screenSize) {
    double scaleX = screenSize.getWidth() / mapExtent.getWidth();
    double scaleY = screenSize.getHeight() / mapExtent.getHeight();

    double tx = -mapExtent.getMinX() * scaleX;
    double ty = (mapExtent.getMinY() * scaleY) + screenSize.getHeight();

    AffineTransform at = new AffineTransform(scaleX, 0.0d, 0.0d, -scaleY, tx, ty);

    return at;// w  w w . j a v  a 2  s.c o m
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

/*** Rigid body transform (rotation and translation only)
        //from  ww w  . j a  v a  2 s .  c o  m
        
 /**
 * Creates an AffineTransform object that uses only rotation and translation
 * 
 * @pointPairs is a Map of points measured in the two coordinates systems (srcPt->destPt)
 */
public static AffineTransform generateRigidBodyTransform(Map<Point2D.Double, Point2D.Double> pointPairs) {
    int number = pointPairs.size();

    RealMatrix X = new Array2DRowRealMatrix(2 * number, 4);
    RealMatrix U = new Array2DRowRealMatrix(2 * number, 1);

    int i = 0;
    for (Map.Entry<Point2D.Double, Point2D.Double> pair : pointPairs.entrySet()) {
        double[] thisRow = { pair.getKey().x, pair.getKey().y, 1.0, 0.0 };
        X.setRow(i, thisRow);
        double[] otherRow = { pair.getKey().y, -pair.getKey().x, 0.0, 1.0 };
        X.setRow(i + number, otherRow);

        U.setEntry(i, 0, pair.getValue().x);
        U.setEntry(i + number, 0, pair.getValue().y);
        i++;
    }

    DecompositionSolver solver = (new QRDecompositionImpl(X)).getSolver();
    double[][] m = solver.solve(U).getData();

    return new AffineTransform(m[0][0], m[1][0], -m[1][0], m[0][0], m[2][0], m[3][0]);
}

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

/**
 * Draw an xobject(form or image) at the x,y coordinates and a certain width
 * and height./*from w w w  .j  av  a2 s  . com*/
 * 
 * @param xobject
 *            The xobject to draw.
 * @param x
 *            The x-coordinate to draw the image.
 * @param y
 *            The y-coordinate to draw the image.
 * @param width
 *            The width of the image to draw.
 * @param height
 *            The height of the image to draw.
 * 
 * @throws IOException
 *             If there is an error writing to the stream.
 */
public void drawXObject(PDXObject xobject, float x, float y, float width, float height) throws IOException {
    AffineTransform transform = new AffineTransform(width, 0, 0, height, x, y);
    drawXObject(xobject, transform);
}

From source file:org.apache.xmlgraphics.ps.PSGenerator.java

/**
 * Concats the transformation matrix.//w w  w . jav  a 2  s  .c om
 * @param a A part
 * @param b B part
 * @param c C part
 * @param d D part
 * @param e E part
 * @param f F part
 * @exception IOException In case of an I/O problem
 */
public void concatMatrix(double a, double b, double c, double d, double e, double f) throws IOException {
    AffineTransform at = new AffineTransform(a, b, c, d, e, f);
    concatMatrix(at);

}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

public void createAffineTransform(float[] params) {
    AffineTransform transform = new AffineTransform(params[0], params[1], params[2], params[3], params[4],
            params[5]);/* w  ww. jav a 2  s .  c o  m*/
    // transform.rotate(90);
    getStructure().setAffineTransform(transform);
    logger.debug("Matrix has been added");
}

From source file:org.apache.pdfbox.pdmodel.PDPageContentStream.java

/**
 * Draw an image at the x,y coordinates, with the given size.
 *
 * @param image The image to draw.// w  ww .  ja v  a 2  s . com
 * @param x The x-coordinate to draw the image.
 * @param y The y-coordinate to draw the image.
 * @param width The width to draw the image.
 * @param height The height to draw the image.
 *
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawImage(PDImageXObject image, float x, float y, float width, float height) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
    }

    saveGraphicsState();

    AffineTransform transform = new AffineTransform(width, 0, 0, height, x, y);
    transform(new Matrix(transform));

    writeOperand(resources.add(image));
    writeOperator("Do");

    restoreGraphicsState();
}

From source file:org.apache.cocoon.reading.RepoImageReader.java

/**
 * Returns the affine transform that implements the scaling.
 * The behavior is the following: if both the new width and height values
 * are positive, the image is rescaled according to these new values and
 * the original aspect ratio is lost.//w w w  . j a  va 2s .  c o  m
 * Otherwise, if one of the two parameters is zero or negative, the
 * aspect ratio is maintained and the positive parameter indicates the
 * scaling.
 * If both new values are zero or negative, no scaling takes place (a unit
 * transformation is applied).
 */
private AffineTransform getTransform(double ow, double oh, double nw, double nh) {
    double wm = 1.0d;
    double hm = 1.0d;

    if (fitUniform) {
        //
        // Compare aspect ratio of image vs. that of the "box"
        // defined by nw and nh
        //
        if (ow / oh > nw / nh) {
            nh = 0; // Original image is proportionately wider than the box,
            // so scale to fit width
        } else {
            nw = 0; // Scale to fit height
        }
    }

    if (nw > 0) {
        wm = nw / ow;
        if (nh > 0) {
            hm = nh / oh;
        } else {
            hm = wm;
        }
    } else {
        if (nh > 0) {
            hm = nh / oh;
            wm = hm;
        }
    }

    if (!enlarge) {
        if ((nw > ow && nh <= 0) || (nh > oh && nw <= 0)) {
            wm = 1.0d;
            hm = 1.0d;
        } else if (nw > ow) {
            wm = 1.0d;
        } else if (nh > oh) {
            hm = 1.0d;
        }
    }
    return new AffineTransform(wm, 0.0d, 0.0d, hm, 0.0d, 0.0d);
}