Skip to content

PDFBOX-6032: add configurable default image factory #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.apache.pdfbox.pdmodel.graphics.image;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;

@FunctionalInterface
public interface CustomFactory {
PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,27 @@ public static PDImageXObject createFromFileByContent(File file, PDDocument doc)
* @throws IllegalArgumentException if the image type is not supported.
*/
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name) throws IOException
{
return createFromByteArray(document, byteArray, name, null);
}

/**
* Create a PDImageXObject from an image byte array. This overloaded version allows providing
* a custom factory to handle specific image formats, such as BMP and GIF, or to act as a
* fallback strategy when the default converters (e.g., for PNG or TIFF) fail.
*
* @param document the document that shall use this PDImageXObject.
* @param byteArray bytes from an image file.
* @param name name of image file for exception messages, can be null.
* @param customFactory optional factory used to handle BMP, GIF, or fallback cases
* (e.g., for PNG or TIFF). If {@code null}, this method delegates to
* {@link #createFromByteArray(PDDocument, byte[], String)}.
* @return a PDImageXObject.
* @throws IOException if there is an error when reading the file or creating the
* PDImageXObject.
* @throws IllegalArgumentException if the image type is not supported.
*/
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name, CustomFactory customFactory) throws IOException
{
FileType fileType = FileTypeDetector.detectFileType(byteArray);
if (fileType == null)
Expand Down Expand Up @@ -376,9 +397,16 @@ public static PDImageXObject createFromByteArray(PDDocument document, byte[] byt
}
if (fileType == FileType.BMP || fileType == FileType.GIF || fileType == FileType.PNG)
{
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
BufferedImage bim = ImageIO.read(bais);
return LosslessFactory.createFromImage(document, bim);
if (customFactory != null)
{
return customFactory.createFromByteArray(document, byteArray);
}
else
{
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
BufferedImage bim = ImageIO.read(bais);
return LosslessFactory.createFromImage(document, bim);
}
}
throw new IllegalArgumentException("Image type " + fileType + " not supported: " + name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -125,6 +129,19 @@ void testCreateFromByteArray() throws IOException, URISyntaxException
testCompareCreatedFromByteArrayWithCreatedByLosslessFactory("lzw.tif");
}

/**
* Test of createFromByteArray method with CustomFactory parameter, of class PDImageXObject.
* @throws java.io.IOException
* @throws java.net.URISyntaxException
*/
@Test
void testCreateFromByteArrayWithCustomFactory() throws IOException, URISyntaxException
{
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("gif.gif");
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("gif-1bit-transparent.gif");
testCompareCreatedFromByteArrayWithCreatedByCustomFactory("lzw.tif");
}

private void testCompareCreatedFileByExtensionWithCreatedByLosslessFactory(String filename)
throws IOException, URISyntaxException
{
Expand Down Expand Up @@ -318,6 +335,26 @@ private void testCompareCreatedFromByteArrayWithCreatedByJPEGFactory(String file
}
}

private void testCompareCreatedFromByteArrayWithCreatedByCustomFactory(String filename)
throws IOException, URISyntaxException
{
try (PDDocument doc = new PDDocument())
{
File file = new File(PDImageXObjectTest.class.getResource(filename).toURI());
InputStream in = new FileInputStream(file);
byte[] byteArray = in.readAllBytes();

CustomFactory customFactory = this::alphaFlattenedJPEGFactory;

PDImageXObject image = PDImageXObject.createFromByteArray(doc, byteArray, filename, customFactory);

PDImageXObject expectedImage = alphaFlattenedJPEGFactory(doc, byteArray);

assertEquals(expectedImage.getSuffix(), image.getSuffix());
checkIdentARGB(image.getImage(), expectedImage.getImage());
}
}

private void checkIdentARGB(BufferedImage expectedImage, BufferedImage actualImage)
{
String errMsg = "";
Expand All @@ -337,5 +374,31 @@ private void checkIdentARGB(BufferedImage expectedImage, BufferedImage actualIma
assertEquals(expectedImage.getRGB(x, y), actualImage.getRGB(x, y), errMsg);
}
}
}
}

private PDImageXObject alphaFlattenedJPEGFactory(PDDocument document, byte[] byteArray) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
BufferedImage bim = ImageIO.read(bais);

if (bim.isAlphaPremultiplied()) {
ColorModel colorModel = bim.getColorModel();
WritableRaster raster = bim.copyData(null);
bim = new BufferedImage(colorModel, raster, false, null);
}

BufferedImage flattened = new BufferedImage(
bim.getWidth(),
bim.getHeight(),
BufferedImage.TYPE_INT_RGB
);

Graphics2D g = flattened.createGraphics();
g.setComposite(AlphaComposite.SrcOver);
g.setColor(Color.WHITE);
g.fillRect(0, 0, flattened.getWidth(), flattened.getHeight());
g.drawImage(bim, 0, 0, null);
g.dispose();

return JPEGFactory.createFromImage(document, flattened);
}
}