use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class LosslessFactoryTest method testCreateLosslessFrom16BitPNG.
// doBitmaskTransparencyTest: Android does not have bitmask transparency
/**
* Test lossless encoding of CMYK images
*/
// public void testCreateLosslessFromImageCMYK() throws IOException TODO: PdfBox-Android
// public void testCreateLosslessFrom16Bit() throws IOException TODO: PdfBox-Android
// public void testCreateLosslessFromImageINT_BGR() throws IOException TODO: PdfBox-Android
// public void testCreateLosslessFromImageINT_RGB() throws IOException TODO: PdfBox-Android
// public void testCreateLosslessFromImageBYTE_3BGR() throws IOException TODO: PdfBox-Android
@FlakyTest(detail = "Behavior depends heavily on API level / device")
@Test
public void testCreateLosslessFrom16BitPNG() throws IOException {
// TODO: PdfBox-Android PNG is reduced to 8 bit, this causes changes in test values
PDDocument document = new PDDocument();
File TARGETDIR = new File(testContext.getCacheDir(), "imgs");
TARGETDIR.mkdirs();
File imgFile = TestResourceGenerator.downloadTestResource(TARGETDIR, "PDFBOX-4184-16bit.png", "https://issues.apache.org/jira/secure/attachment/12929821/16bit.png");
assumeTrue(imgFile.exists());
Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Bitmap compareImage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// TODO: PdfBox-Android This is a workaround for RGBA_16 failing the checkIdent calls
compareImage = image.copy(Bitmap.Config.ARGB_8888, false);
} else {
compareImage = image;
}
// assertEquals(64, image.getColorModel().getPixelSize());
// assertEquals(Transparency.TRANSLUCENT, image.getColorModel().getTransparency());
// assertEquals(4, image.getRaster().getNumDataElements());
// assertEquals(java.awt.image.DataBuffer.TYPE_USHORT, image.getRaster().getDataBuffer().getDataType());
PDImageXObject ximage = LosslessFactory.createFromImage(document, image);
int w = image.getWidth();
int h = image.getHeight();
validate(ximage, 8, w, h, "png", PDDeviceRGB.INSTANCE.getName());
checkIdent(compareImage, ximage.getImage());
checkIdentRGB(compareImage, ximage.getOpaqueImage());
assertNotNull(ximage.getSoftMask());
validate(ximage.getSoftMask(), 8, w, h, "png", PDDeviceGray.INSTANCE.getName());
// assertEquals(35, colorCount(ximage.getSoftMask().getImage())); TODO: PdfBox-Android
doWritePDF(document, ximage, testResultsDir, "png16bit.pdf");
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class LosslessFactoryTest method testCreateLosslessFromImageINT_ARGB.
/**
* Tests INT_ARGB LosslessFactoryTest#createFromImage(PDDocument document,
* BufferedImage image)
*
* @throws java.io.IOException
*/
@Test
public void testCreateLosslessFromImageINT_ARGB() throws IOException {
PDDocument document = new PDDocument();
Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/png.png"));
// create an ARGB image
int w = image.getWidth();
int h = image.getHeight();
Bitmap argbImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(argbImage);
Paint paint = new Paint();
canvas.drawBitmap(image, 0, 0, paint);
int[] argbPixels = new int[w * h];
argbImage.getPixels(argbPixels, 0, w, 0, 0, w, h);
for (int x = 0; x < argbImage.getWidth(); ++x) {
for (int y = 0; y < argbImage.getHeight(); ++y) {
argbPixels[x + w * y] = (argbPixels[x + w * y] & 0xFFFFFF) | ((y / 10 * 10) << 24);
}
}
argbImage.setPixels(argbPixels, 0, w, 0, 0, w, h);
PDImageXObject ximage = LosslessFactory.createFromImage(document, argbImage);
validate(ximage, 8, argbImage.getWidth(), argbImage.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
checkIdent(argbImage, ximage.getImage());
checkIdentRGB(argbImage, ximage.getOpaqueImage());
assertNotNull(ximage.getSoftMask());
validate(ximage.getSoftMask(), 8, argbImage.getWidth(), argbImage.getHeight(), "png", PDDeviceGray.INSTANCE.getName());
assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
doWritePDF(document, ximage, testResultsDir, "intargb.pdf");
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class LosslessFactoryTest method testCreateLosslessFromGovdocs032163.
// TODO: PdfBox-Android : testCreateLosslessFromTransparentGIF: GIF images not currently supported
// TODO: PdfBox-Android : testCreateLosslessFromTransparent1BitGIF: GIF images not currently supported
/**
* Test file that had a predictor encoding bug in PDFBOX-4184.
*
* @throws java.io.IOException
*/
@Test
public void testCreateLosslessFromGovdocs032163() throws IOException {
File inDir = new File(testContext.getCacheDir(), "imgs");
inDir.mkdirs();
File imageFile = TestResourceGenerator.downloadTestResource(inDir, "PDFBOX-4184-032163.jpg", "https://issues.apache.org/jira/secure/attachment/12949710/032163.jpg");
assumeTrue(imageFile.exists());
PDDocument document = new PDDocument();
Bitmap image = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
PDImageXObject ximage = LosslessFactory.createFromImage(document, image);
validate(ximage, 8, image.getWidth(), image.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
checkIdent(image, ximage.getImage());
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDInlineImageTest method testInlineImage.
/**
* Tests PDInlineImage#PDInlineImage(COSDictionary parameters, byte[] data,
* Map<String, PDColorSpace> colorSpaces)
*/
@Test
public void testInlineImage() throws IOException {
COSDictionary dict = new COSDictionary();
dict.setBoolean(COSName.IM, true);
int width = 31;
int height = 27;
dict.setInt(COSName.W, width);
dict.setInt(COSName.H, height);
dict.setInt(COSName.BPC, 1);
int rowbytes = width / 8;
if (rowbytes * 8 < width) {
// PDF spec:
// If the number of data bits per row is not a multiple of 8,
// the end of the row is padded with extra bits to fill out the last byte.
++rowbytes;
}
// draw a grid
int datalen = rowbytes * height;
byte[] data = new byte[datalen];
for (int i = 0; i < datalen; ++i) {
data[i] = (i / 4 % 2 == 0) ? (byte) Integer.parseInt("10101010", 2) : 0;
}
PDInlineImage inlineImage1 = new PDInlineImage(dict, data, null);
assertTrue(inlineImage1.isStencil());
assertEquals(width, inlineImage1.getWidth());
assertEquals(height, inlineImage1.getHeight());
assertEquals(1, inlineImage1.getBitsPerComponent());
COSDictionary dict2 = new COSDictionary();
dict2.addAll(dict);
// use decode array to revert in image2
COSArray decodeArray = new COSArray();
decodeArray.add(COSInteger.ONE);
decodeArray.add(COSInteger.ZERO);
dict2.setItem(COSName.DECODE, decodeArray);
PDInlineImage inlineImage2 = new PDInlineImage(dict2, data, null);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Bitmap stencilImage = inlineImage1.getStencilImage(paint);
assertEquals(width, stencilImage.getWidth());
assertEquals(height, stencilImage.getHeight());
Bitmap stencilImage2 = inlineImage2.getStencilImage(paint);
assertEquals(width, stencilImage2.getWidth());
assertEquals(height, stencilImage2.getHeight());
Bitmap image1 = inlineImage1.getImage();
assertEquals(width, image1.getWidth());
assertEquals(height, image1.getHeight());
Bitmap image2 = inlineImage2.getImage();
assertEquals(width, image2.getWidth());
assertEquals(height, image2.getHeight());
// write and read
boolean writeOk = image1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(testResultsDir.getPath() + "/inline-grid1.png"));
assertTrue(writeOk);
Bitmap bim1 = BitmapFactory.decodeFile(new File(testResultsDir + "/inline-grid1.png").getPath());
assertNotNull(bim1);
assertEquals(width, bim1.getWidth());
assertEquals(height, bim1.getHeight());
writeOk = image2.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(testResultsDir.getPath() + "/inline-grid2.png"));
assertTrue(writeOk);
Bitmap bim2 = BitmapFactory.decodeFile(new File(testResultsDir + "/inline-grid2.png").getPath());
assertNotNull(bim2);
assertEquals(width, bim2.getWidth());
assertEquals(height, bim2.getHeight());
// compare: pixels with even coordinates are white (FF), all others are black (0)
int[] bimPixels = new int[width * height];
bim1.getPixels(bimPixels, 0, width, 0, 0, width, height);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (x % 2 == 0 && y % 2 == 0) {
assertEquals(0xFFFFFF, bimPixels[x + width * y] & 0xFFFFFF);
} else {
assertEquals(0, bimPixels[x + width * y] & 0xFFFFFF);
}
}
}
// compare: pixels with odd coordinates are white (FF), all others are black (0)
bim2.getPixels(bimPixels, 0, width, 0, 0, width, height);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (x % 2 == 0 && y % 2 == 0) {
assertEquals(0, bimPixels[x + width * y] & 0xFFFFFF);
} else {
assertEquals(0xFFFFFF, bimPixels[x + width * y] & 0xFFFFFF);
}
}
}
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false);
contentStream.drawImage(inlineImage1, 150, 400);
contentStream.drawImage(inlineImage1, 150, 500, inlineImage1.getWidth() * 2, inlineImage1.getHeight() * 2);
contentStream.drawImage(inlineImage1, 150, 600, inlineImage1.getWidth() * 4, inlineImage1.getHeight() * 4);
contentStream.drawImage(inlineImage2, 350, 400);
contentStream.drawImage(inlineImage2, 350, 500, inlineImage2.getWidth() * 2, inlineImage2.getHeight() * 2);
contentStream.drawImage(inlineImage2, 350, 600, inlineImage2.getWidth() * 4, inlineImage2.getHeight() * 4);
contentStream.close();
File pdfFile = new File(testResultsDir, "inline.pdf");
document.save(pdfFile);
document.close();
document = PDDocument.load(pdfFile, (String) null);
new PDFRenderer(document).renderImage(0);
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroupsInstrumentationTest method testOCGGenerationSameNameCanHaveSameVisibilityOff.
/**
* PDFBOX-4496: setGroupEnabled(String, boolean) must catch all OCGs of a name even when several
* names are identical.
*
* @throws IOException
*/
@Test
public void testOCGGenerationSameNameCanHaveSameVisibilityOff() throws IOException {
Bitmap expectedImage;
Bitmap actualImage;
PDDocument doc = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
// Prepare OCG functionality
PDOptionalContentProperties ocprops = new PDOptionalContentProperties();
doc.getDocumentCatalog().setOCProperties(ocprops);
// ocprops.setBaseState(BaseState.ON); //ON=default
// Create OCG for background
PDOptionalContentGroup background = new PDOptionalContentGroup("background");
ocprops.addGroup(background);
assertTrue(ocprops.isGroupEnabled("background"));
// Create OCG for enabled
PDOptionalContentGroup enabled = new PDOptionalContentGroup("science");
ocprops.addGroup(enabled);
assertFalse(ocprops.setGroupEnabled("science", true));
assertTrue(ocprops.isGroupEnabled("science"));
// Create OCG for disabled1
PDOptionalContentGroup disabled1 = new PDOptionalContentGroup("alternative");
ocprops.addGroup(disabled1);
// Create OCG for disabled2 with same name as disabled1
PDOptionalContentGroup disabled2 = new PDOptionalContentGroup("alternative");
ocprops.addGroup(disabled2);
assertFalse(ocprops.setGroupEnabled("alternative", false));
assertFalse(ocprops.isGroupEnabled("alternative"));
// Setup page content stream and paint background/title
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginMarkedContent(COSName.OC, background);
contentStream.beginText();
contentStream.setFont(font, 14);
contentStream.newLineAtOffset(80, 700);
contentStream.showText("PDF 1.5: Optional Content Groups");
contentStream.endText();
contentStream.endMarkedContent();
font = PDType1Font.HELVETICA;
// Paint enabled layer
contentStream.beginMarkedContent(COSName.OC, enabled);
contentStream.setNonStrokingColor(AWTColor.GREEN);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 600);
contentStream.showText("The earth is a sphere");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer1
contentStream.beginMarkedContent(COSName.OC, disabled1);
contentStream.setNonStrokingColor(AWTColor.RED);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 500);
contentStream.showText("Alternative 1: The earth is a flat circle");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer2
contentStream.beginMarkedContent(COSName.OC, disabled2);
contentStream.setNonStrokingColor(AWTColor.BLUE);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 450);
contentStream.showText("Alternative 2: The earth is a flat parallelogram");
contentStream.endText();
contentStream.endMarkedContent();
contentStream.close();
doc.getDocumentCatalog().setPageMode(PageMode.USE_OPTIONAL_CONTENT);
File targetFile = new File(testResultsDir, "ocg-generation-same-name-off.pdf");
doc.save(targetFile.getAbsolutePath());
doc.close();
// render PDF with science disabled and alternatives with same name enabled
doc = PDDocument.load(new File(testResultsDir, "ocg-generation-same-name-off.pdf"));
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("background", false);
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("science", false);
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("alternative", true);
actualImage = new PDFRenderer(doc).renderImage(0, 2);
actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-actual.png")));
} finally {
doc.close();
}
// create PDF without OCGs to created expected rendering
PDDocument doc2 = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc2.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
PDPageContentStream contentStream = new PDPageContentStream(doc2, page, AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA;
contentStream.setNonStrokingColor(AWTColor.RED);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 500);
contentStream.showText("Alternative 1: The earth is a flat circle");
contentStream.endText();
contentStream.setNonStrokingColor(AWTColor.BLUE);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 450);
contentStream.showText("Alternative 2: The earth is a flat parallelogram");
contentStream.endText();
contentStream.close();
expectedImage = new PDFRenderer(doc2).renderImage(0, 2);
actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-expected.png")));
} finally {
doc2.close();
}
// compare images
int height = expectedImage.getHeight();
int width = expectedImage.getWidth();
int[] expectedImagePixels = new int[width * height];
expectedImage.getPixels(expectedImagePixels, 0, width, 0, 0, width, height);
int[] actualImagePixels = new int[width * height];
actualImage.getPixels(actualImagePixels, 0, width, 0, 0, width, height);
Assert.assertArrayEquals(expectedImagePixels, actualImagePixels);
}
Aggregations