Example usage for java.awt.image BufferedImage getWidth

List of usage examples for java.awt.image BufferedImage getWidth

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

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

/**
 * process : Do : Paint the specified XObject (section 4.7).
 * @param operator The operator that is being executed.
 * @param arguments List//from   w w  w.  ja  v a 2  s. co  m
 * @throws java.io.IOException If there is an error invoking the sub object.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context;
    PDPage page = drawer.getPage();
    COSName objectName = (COSName) arguments.get(0);
    Map xobjects = drawer.getResources().getXObjects();
    PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
    if (xobject == null) {
        log.warn("Can't find the XObject for '" + objectName.getName() + "'");
    } else if (xobject instanceof PDXObjectImage) {
        PDXObjectImage image = (PDXObjectImage) xobject;
        try {
            image.setGraphicsState(drawer.getGraphicsState());
            BufferedImage awtImage = image.getRGBImage();
            if (awtImage == null) {
                log.warn("getRGBImage returned NULL");
                return;//TODO PKOCH
            }
            int imageWidth = awtImage.getWidth();
            int imageHeight = awtImage.getHeight();
            double pageHeight = drawer.getPageSize().getHeight();

            log.debug("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);

            Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
            float yScaling = ctm.getYScale();
            float angle = (float) Math.acos(ctm.getValue(0, 0) / ctm.getXScale());
            if (ctm.getValue(0, 1) < 0 && ctm.getValue(1, 0) > 0)
                angle = (-1) * angle;
            ctm.setValue(2, 1, (float) (pageHeight - ctm.getYPosition() - Math.cos(angle) * yScaling));
            ctm.setValue(2, 0, (float) (ctm.getXPosition() - Math.sin(angle) * yScaling));
            // because of the moved 0,0-reference, we have to shear in the opposite direction
            ctm.setValue(0, 1, (-1) * ctm.getValue(0, 1));
            ctm.setValue(1, 0, (-1) * ctm.getValue(1, 0));
            AffineTransform ctmAT = ctm.createAffineTransform();
            ctmAT.scale(1f / imageWidth, 1f / imageHeight);
            drawer.drawImage(awtImage, ctmAT);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e, e);
        }
    } else if (xobject instanceof PDXObjectForm) {
        // save the graphics state
        context.getGraphicsStack().push((PDGraphicsState) context.getGraphicsState().clone());

        PDXObjectForm form = (PDXObjectForm) xobject;
        COSStream invoke = (COSStream) form.getCOSObject();
        PDResources pdResources = form.getResources();
        if (pdResources == null) {
            pdResources = page.findResources();
        }
        // if there is an optional form matrix, we have to
        // map the form space to the user space
        Matrix matrix = form.getMatrix();
        if (matrix != null) {
            Matrix xobjectCTM = matrix.multiply(context.getGraphicsState().getCurrentTransformationMatrix());
            context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
        }
        getContext().processSubStream(page, pdResources, invoke);

        // restore the graphics state
        context.setGraphicsState((PDGraphicsState) context.getGraphicsStack().pop());
    }
}