public BufferedImage convert( final DecoderResults resultsFromDecode, final int displayRotation, final PdfResources res, final ExternalHandlers externalHandlers, final int renderMode, final PdfPageData pageData, final AcroRenderer formRenderer, final float scaling, final PdfObjectReader currentPdfFile, final int pageIndex, final boolean imageIsTransparent, final String currentPageOffset) throws PdfException { final ObjectStore localStore = new ObjectStore(); /** read page or next pages */ final PdfObject pdfObject = new PageObject(currentPageOffset); currentPdfFile.readObject(pdfObject); currentPdfFile.checkParentForResources(pdfObject); // ensure set (needed for XFA) pdfObject.setPageNumber(pageIndex); final PdfObject Resources = pdfObject.getDictionary(PdfDictionary.Resources); imageDisplay = getDisplay(pageIndex, localStore, imageIsTransparent); if (!imageDisplay.isHTMLorSVG()) { if (options.getPageColor() != null) { imageDisplay.setValue( DynamicVectorRenderer.ALT_BACKGROUND_COLOR, options.getPageColor().getRGB()); } if (options.getTextColor() != null) { imageDisplay.setValue( DynamicVectorRenderer.ALT_FOREGROUND_COLOR, options.getTextColor().getRGB()); if (options.getChangeTextAndLine()) { imageDisplay.setValue(DynamicVectorRenderer.FOREGROUND_INCLUDE_LINEART, 1); } else { imageDisplay.setValue(DynamicVectorRenderer.FOREGROUND_INCLUDE_LINEART, 0); } imageDisplay.setValue( DynamicVectorRenderer.COLOR_REPLACEMENT_THRESHOLD, options.getReplacementColorThreshold()); } } final PdfStreamDecoder currentImageDecoder = formRenderer.getStreamDecoder(currentPdfFile, true, null, true); currentImageDecoder.setParameters( true, true, renderMode, PdfDecoderInt.TEXT, false, externalHandlers.getMode().equals(GUIModes.JAVAFX)); externalHandlers.addHandlers(currentImageDecoder); // currentImageDecoder.setObjectValue(ValueTypes.Name, filename); currentImageDecoder.setObjectValue(ValueTypes.ObjectStore, localStore); currentImageDecoder.setMultiplyer(multiplyer); currentImageDecoder.setObjectValue(ValueTypes.PDFPageData, pageData); currentImageDecoder.setIntValue(ValueTypes.PageNum, pageIndex); currentImageDecoder.setRenderer(imageDisplay); externalHandlers.addHandlers(currentImageDecoder); res.setupResources(currentImageDecoder, true, Resources, pageIndex, currentPdfFile); // can for max if (multiplyer == -2) { multiplyer = -1; currentImageDecoder.setMultiplyer(multiplyer); final PdfStreamDecoderForSampling currentImageDecoder2 = new PdfStreamDecoderForSampling(currentPdfFile); currentImageDecoder2.setParameters( true, true, renderMode, 0, false, externalHandlers.getMode().equals(GUIModes.JAVAFX)); // currentImageDecoder2.setObjectValue(ValueTypes.Name, filename); currentImageDecoder2.setObjectValue(ValueTypes.ObjectStore, localStore); currentImageDecoder2.setMultiplyer(multiplyer); currentImageDecoder2.setObjectValue(ValueTypes.PDFPageData, pageData); currentImageDecoder2.setIntValue(ValueTypes.PageNum, pageIndex); currentImageDecoder2.setRenderer(imageDisplay); res.setupResources(currentImageDecoder2, true, Resources, pageIndex, currentPdfFile); externalHandlers.addHandlers(currentImageDecoder2); /** bare minimum to get value */ multiplyer = currentImageDecoder2.decodePageContentForImageSampling(pdfObject); int bestQualityMaxScalingToUse = 0; if (instance_bestQualityMaxScaling != null) { bestQualityMaxScalingToUse = instance_bestQualityMaxScaling; } else if (bestQualityMaxScaling != null) { bestQualityMaxScalingToUse = bestQualityMaxScaling; } if (bestQualityMaxScalingToUse > 0 && multiplyer > bestQualityMaxScalingToUse) { multiplyer = bestQualityMaxScalingToUse; } currentImageDecoder2.setMultiplyer(multiplyer); currentImageDecoder.setMultiplyer(multiplyer); } if (!allowPagesSmallerThanPageSize && !instance_allowPagesSmallerThanPageSize && multiplyer < 1 && multiplyer > 0) { multiplyer = 1; } // allow for value not set if (multiplyer == -1) { multiplyer = 1; } /** setup positions,transformations and image */ imageScaling = PDFtoImageConvertorSwing.setPageParametersForImage( scaling * multiplyer, pageIndex, pageData); setParams(scaling, pageData, pageIndex); final BufferedImage image = pageToImage(imageIsTransparent, currentImageDecoder, scaling, pdfObject, formRenderer); resultsFromDecode.update(currentImageDecoder, false); /** draw acroform data onto Panel */ if (formRenderer != null && formRenderer.hasFormsOnPage(pageIndex) && !formRenderer.ignoreForms()) { resultsFromDecode.resetColorSpaces(); if (!formRenderer.getCompData().hasformsOnPageDecoded(pageIndex)) { formRenderer.createDisplayComponentsForPage(pageIndex, currentImageDecoder); } if (isFX) { // done in fx image code } else if (!formRenderer.getCompData().formsRasterizedForDisplay()) { if (!formRenderer.useXFA()) { java.util.List[] formsOrdered = formRenderer.getCompData().getFormList(true); // get unsorted components and iterate over forms for (Object nextVal : formsOrdered[pageIndex]) { if (nextVal != null) { formRenderer .getFormFlattener() .drawFlattenedForm( currentImageDecoder, (org.jpedal.objects.raw.FormObject) nextVal, false, (PdfObject) formRenderer.getFormResources()[0]); } } } else { formRenderer .getCompData() .renderFormsOntoG2( image.getGraphics(), pageIndex, 0, displayRotation, null, null, pageData.getMediaBoxHeight(pageIndex)); } } else { final java.util.List[] formsOrdered = formRenderer.getCompData().getFormList(true); // get unsorted components and iterate over forms for (final Object nextVal : formsOrdered[pageIndex]) { if (nextVal != null) { formRenderer .getFormFlattener() .drawFlattenedForm( currentImageDecoder, (org.jpedal.objects.raw.FormObject) nextVal, false, (PdfObject) formRenderer.getFormResources()[0]); } } } } if (currentImageDecoder != null) { currentImageDecoder.dispose(); } localStore.flush(); return image; }
public static boolean convert( byte[] objectData, ImageData imageData, GenericColorSpace decodeColorData, PdfObject newSMask, PdfObjectReader currentPdfFile) throws PdfException { newSMask.setFloatArray(PdfDictionary.Decode, new float[] {1, 0}); final PdfArrayIterator Filters = newSMask.getMixedArray(PdfDictionary.Filter); boolean isMaskJPX = false; // check not handled elsewhere int firstValue; if (Filters != null && Filters.hasMoreTokens()) { while (Filters.hasMoreTokens()) { firstValue = Filters.getNextValueAsConstant(true); // isDCT=firstValue==PdfFilteredReader.DCTDecode; isMaskJPX = firstValue == PdfFilteredReader.JPXDecode; } } byte[] objData = currentPdfFile.readStream(newSMask, true, true, false, false, false, null); imageData.setObjectData(objData); imageData.setDepth(1); imageData.setWidth(newSMask.getInt(PdfDictionary.Width)); imageData.setHeight(newSMask.getInt(PdfDictionary.Height)); int newDepth = newSMask.getInt(PdfDictionary.BitsPerComponent); // JPEG2000 causes us special difficulties as we not actually decoding objectData because // it is JPEG2000 and we decode as part of image handling because Java does byte[] to image // directly // // This code fixes specific example by getting the actual data and ignoring empty mask but will // probably need developing further if we find other examples if (isMaskJPX) { // see case 17665 for example BufferedImage img = decodeColorData.JPEG2000ToRGBImage( imageData.getObjectData(), imageData.getWidth(), imageData.getHeight(), null, -1, -1, 8); img = ColorSpaceConvertor.convertColorspace(img, 10); objData = ((DataBufferByte) img.getRaster().getDataBuffer()).getData(); imageData.setObjectData(objData); boolean isEmptyMask = true; for (final byte b : objectData) { if (b != 0) { isEmptyMask = false; break; } } if (isEmptyMask) { imageData.setObjectData(null); } // we need to unset this as e have handled the JPX filter newSMask.setMixedArray(PdfDictionary.Filter, null); } if (newDepth != PdfDictionary.Unknown) { imageData.setDepth(newDepth); } return objData == null; }