List of usage examples for com.itextpdf.text.pdf.parser ImageRenderInfo getImageCTM
public Matrix getImageCTM()
From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java
License:Open Source License
/** * Calculates intersection of the image and the render filter region in the coordinate system relative to the image. * * @return <code>null</code> if the image is not allowed, {@link java.util.List} of * {@link com.itextpdf.text.Rectangle} objects otherwise. *///from w w w . j ava2s . c o m protected List<Rectangle> getCoveredAreas(ImageRenderInfo renderInfo) { Rectangle imageRect = calcImageRect(renderInfo); List<Rectangle> coveredAreas = new ArrayList<Rectangle>(); if (imageRect == null) { return null; } for (Rectangle rectangle : rectangles) { Rectangle intersectionRect = intersection(imageRect, rectangle); if (intersectionRect != null) { // True if the image is completely covered if (imageRect.equals(intersectionRect)) { return null; } coveredAreas.add(transformIntersection(renderInfo.getImageCTM(), intersectionRect)); } } return coveredAreas; }
From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java
License:Open Source License
/** * @return Image boundary rectangle in device space. *//*from w ww .j a v a2 s . c o m*/ private Rectangle calcImageRect(ImageRenderInfo renderInfo) { Matrix ctm = renderInfo.getImageCTM(); if (ctm == null) { return null; } Point2D[] points = transformPoints(ctm, false, new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1)); return getRectangle(points[0], points[1], points[2], points[3]); }
From source file:org.gmdev.pdftrick.engine.ImageListenerShowThumb.java
License:Open Source License
/** * extracts the images contained in selected page and convert it in a BufferedImage to add and show to the center panel. * @param renderInfo//from w w w.ja v a 2 s . co m */ private void render(ImageRenderInfo renderInfo) { final HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages(); boolean isInline = false; PdfImageObject image = null; try { BufferedImage buffImg = null; if (renderInfo.getRef() == null) { isInline = true; inlineImageCounter += 1; } try { image = renderInfo.getImage(); } catch (UnsupportedPdfException updfe) { try { if (isInline) { buffImg = null; } else { buffImg = CustomExtraImgReader.readIndexedPNG(renderInfo.getRef().getNumber(), factory.getResultFile()); } } catch (Exception e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); unsupportedImage++; return; } } if (image != null) { BufferedImage buffPic = null; try { // if image is JBIG2 type i need a custom way and using jbig2 ImageIO plugin if (image.getFileType().equalsIgnoreCase("JBIG2")) { buffPic = CustomExtraImgReader.readJBIG2(image); } else { buffPic = image.getBufferedImage(); } } catch (IIOException iioex) { byte[] imageByteArray = image.getImageAsBytes(); try { buffPic = CustomExtraImgReader.readCMYK_JPG(imageByteArray); } catch (Exception e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); unsupportedImage++; return; } } catch (Exception e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); unsupportedImage++; return; } // check if image contains a mask image BufferedImage buffMask = null; PdfDictionary imageDictionary = image.getDictionary(); PRStream maskStream = (PRStream) imageDictionary.getAsStream(PdfName.SMASK); if (maskStream != null) { // if i have an smak object i check that is not a jpeg format, because this may cause some problem on offscreen rendering // usually all imges with mask are png ... and there aren't problems, if image is jpg i discard the mask :) if (!image.getFileType().equalsIgnoreCase("jpg") && buffPic != null) { PdfImageObject maskImage = new PdfImageObject(maskStream); buffMask = maskImage.getBufferedImage(); Image img = PdfTrickUtils.TransformGrayToTransparency(buffMask); buffImg = PdfTrickUtils.ApplyTransparency(buffPic, img); } else { buffImg = buffPic; } } else { buffImg = buffPic; } } String flip = ""; String rotate = ""; Matrix matrix = renderInfo.getImageCTM(); String angle = "" + rotationFromPages.get(numPage); // experimental float i11 = matrix.get(Matrix.I11); // if negative -> horizontal flip float i12 = matrix.get(Matrix.I12); // if negative -> 90 degree rotation float i21 = matrix.get(Matrix.I21); // if negative -> 270 degree rotation float i22 = matrix.get(Matrix.I22); // if negative -> vertical flip // flip and rotation ... from matrix if i11 or i22 is negative i have to flip image if (("" + i11).charAt(0) == '-') { flip = "fh"; } else if (("" + i22).charAt(0) == '-') { flip = "fv"; } if (angle.equalsIgnoreCase("270") || ("" + i21).charAt(0) == '-') { rotate = "270"; } else if (angle.equalsIgnoreCase("180")) { rotate = "180"; } else if (angle.equalsIgnoreCase("90") || ("" + i12).charAt(0) == '-') { rotate = "90"; } if (buffImg != null) { buffImg = PdfTrickUtils.adjustImage(buffImg, flip, rotate); RenderedImageAttributes imageAttrs = null; if (isInline) { // set up inline image object attributes and store it in a hashmap InlineImage inImg = new InlineImage(buffImg, image != null ? image.getFileType() : "png"); imageAttrs = new RenderedImageInline(inlineImageCounter, inImg, numPage, flip, rotate); } else { // set up image object for normal images imageAttrs = new RenderedImageNormal(numPage, renderInfo.getRef().getNumber(), flip, rotate); } // scaling image with original aspect ratio (if image exceded pic box) int w = buffImg.getWidth(); int h = buffImg.getHeight(); if (w > 170 || h > 170) { double faktor; if (w > h) { faktor = 160 / (double) w; int scaledW = (int) Math.round(faktor * w); int scaledH = (int) Math.round(faktor * h); buffImg = PdfTrickUtils.getScaledImagWithScalr(buffImg, scaledW, scaledH); } else { faktor = 160 / (double) h; int scaledW = (int) Math.round(faktor * w); int scaledH = (int) Math.round(faktor * h); buffImg = PdfTrickUtils.getScaledImagWithScalr(buffImg, scaledW, scaledH); } } numImg++; // dynamic update on the center panel (under EDT thread). UpdatePanelCenter upPcenter = this.upPcenter; upPcenter.setInlineImg(isInline); upPcenter.setBuffImg(buffImg); upPcenter.setImageAttrs(imageAttrs); try { SwingUtilities.invokeAndWait(upPcenter); } catch (InterruptedException e) { logger.error("Exception", e); } catch (InvocationTargetException e) { logger.error("Exception", e); } buffImg.flush(); } else { unsupportedImage++; } } catch (IOException e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); unsupportedImage++; } catch (Exception e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); unsupportedImage++; } }