private boolean drawImage( Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) { if (xform == null) xform = new AffineTransform(); else xform = new AffineTransform(xform); xform.translate(0, img.getHeight(obs)); xform.scale(img.getWidth(obs), img.getHeight(obs)); AffineTransform inverse = this.normalizeMatrix(); AffineTransform flipper = AffineTransform.getScaleInstance(1, -1); inverse.concatenate(xform); inverse.concatenate(flipper); double[] mx = new double[6]; inverse.getMatrix(mx); if (currentFillGState != 255) { PdfGState gs = fillGState[255]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(1); fillGState[255] = gs; } cb.setGState(gs); } try { com.lowagie.text.Image image = null; if (!convertImagesToJPEG) { image = com.lowagie.text.Image.getInstance(img, bgColor); } else { BufferedImage scaled = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g3 = scaled.createGraphics(); g3.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null); g3.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault()); iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwparam.setCompressionQuality(jpegQuality); // Set here your compression rate ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("jpg").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); iw.setOutput(ios); iw.write(null, new IIOImage(scaled, null, null), iwparam); iw.dispose(); ios.close(); scaled.flush(); scaled = null; image = com.lowagie.text.Image.getInstance(baos.toByteArray()); } if (mask != null) { com.lowagie.text.Image msk = com.lowagie.text.Image.getInstance(mask, null, true); msk.makeMask(); msk.setInverted(true); image.setImageMask(msk); } cb.addImage( image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); Object url = getRenderingHint(HyperLinkKey.KEY_INSTANCE); if (url != null && !url.equals(HyperLinkKey.VALUE_HYPERLINKKEY_OFF)) { PdfAction action = new PdfAction(url.toString()); cb.setAction( action, (float) mx[4], (float) mx[5], (float) (mx[0] + mx[4]), (float) (mx[3] + mx[5])); } } catch (Exception ex) { throw new IllegalArgumentException(); } if (currentFillGState != 255) { PdfGState gs = fillGState[currentFillGState]; cb.setGState(gs); } return true; }
Image getImage() throws IOException { readPng(); try { int pal0 = 0; int palIdx = 0; palShades = false; if (trans != null) { for (int k = 0; k < trans.length; ++k) { int n = trans[k] & 0xff; if (n == 0) { ++pal0; palIdx = k; } if (n != 0 && n != 255) { palShades = true; break; } } } if ((colorType & 4) != 0) palShades = true; genBWMask = (!palShades && (pal0 > 1 || transRedGray >= 0)); if (!palShades && !genBWMask && pal0 == 1) { additional.put(PdfName.MASK, new PdfLiteral("[" + palIdx + " " + palIdx + "]")); } boolean needDecode = (interlaceMethod == 1) || (bitDepth == 16) || ((colorType & 4) != 0) || palShades || genBWMask; switch (colorType) { case 0: inputBands = 1; break; case 2: inputBands = 3; break; case 3: inputBands = 1; break; case 4: inputBands = 2; break; case 6: inputBands = 4; break; } if (needDecode) decodeIdat(); int components = inputBands; if ((colorType & 4) != 0) --components; int bpc = bitDepth; if (bpc == 16) bpc = 8; Image img; if (image != null) img = Image.getInstance(width, height, components, bpc, image); else { img = new ImgRaw(width, height, components, bpc, idat.toByteArray()); img.setDeflated(true); PdfDictionary decodeparms = new PdfDictionary(); decodeparms.put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth)); decodeparms.put(PdfName.PREDICTOR, new PdfNumber(15)); decodeparms.put(PdfName.COLUMNS, new PdfNumber(width)); decodeparms.put( PdfName.COLORS, new PdfNumber((colorType == 3 || (colorType & 2) == 0) ? 1 : 3)); additional.put(PdfName.DECODEPARMS, decodeparms); } if (additional.get(PdfName.COLORSPACE) == null) additional.put(PdfName.COLORSPACE, getColorspace()); if (intent != null) additional.put(PdfName.INTENT, intent); if (additional.size() > 0) img.setAdditional(additional); if (icc_profile != null) img.tagICC(icc_profile); if (palShades) { Image im2 = Image.getInstance(width, height, 1, 8, smask); im2.makeMask(); img.setImageMask(im2); } if (genBWMask) { Image im2 = Image.getInstance(width, height, 1, 1, smask); im2.makeMask(); img.setImageMask(im2); } img.setDpi(dpiX, dpiY); img.setXYRatio(XYRatio); img.setOriginalType(Image.ORIGINAL_PNG); return img; } catch (Exception e) { throw new ExceptionConverter(e); } }
/** * Applying masks to images. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("masked images"); Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("maskedImages.pdf")); document.open(); Paragraph p = new Paragraph("Some text behind a masked image."); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); document.add(p); PdfContentByte cb = writer.getDirectContent(); byte maskr[] = { (byte) 0x3c, (byte) 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3, (byte) 0xe7, (byte) 0x7e, (byte) 0x3c }; Image mask = Image.getInstance(8, 8, 1, 1, maskr); mask.makeMask(); mask.setInverted(true); Image image = Image.getInstance("otsoe.jpg"); image.setImageMask(mask); image.setAbsolutePosition(60, 550); // explicit masking cb.addImage(image); // stencil masking cb.setRGBColorFill(255, 0, 0); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 450); cb.setRGBColorFill(0, 255, 0); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 400); cb.setRGBColorFill(0, 0, 255); cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 350); document.close(); } catch (Exception de) { de.printStackTrace(); } }
private boolean drawImage( Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) { if (xform == null) return true; xform.translate(0, img.getHeight(obs)); xform.scale(img.getWidth(obs), img.getHeight(obs)); AffineTransform inverse = this.normalizeMatrix(); AffineTransform flipper = AffineTransform.getScaleInstance(1, -1); inverse.concatenate(xform); inverse.concatenate(flipper); double[] mx = new double[6]; inverse.getMatrix(mx); if (currentFillGState != 255) { PdfGState gs = fillGState[255]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(1); fillGState[255] = gs; } cb.setGState(gs); } try { com.lowagie.text.Image image = null; // ssteward: cut out com.sun.image.codec.jpeg code if (true || !convertImagesToJPEG) { image = com.lowagie.text.Image.getInstance(img, bgColor); convertImagesToJPEG = false; // ssteward } else { /* ssteward BufferedImage scaled = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g3 = scaled.createGraphics(); g3.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null); g3.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(baos); com.sun.image.codec.jpeg.JPEGEncodeParam param = com.sun.image.codec.jpeg.JPEGCodec.getDefaultJPEGEncodeParam(scaled); param.setQuality(jpegQuality, true); encoder.encode(scaled, param); scaled.flush(); scaled = null; image = com.lowagie.text.Image.getInstance(baos.toByteArray()); */ } if (mask != null) { com.lowagie.text.Image msk = com.lowagie.text.Image.getInstance(mask, null, true); msk.makeMask(); msk.setInvertMask(true); image.setImageMask(msk); } cb.addImage( image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); } catch (Exception ex) { throw new IllegalArgumentException(); } if (currentFillGState != 255) { PdfGState gs = fillGState[currentFillGState]; cb.setGState(gs); } return true; }