use of org.apache.pdfbox.pdmodel.graphics.color.PDICCBased in project pdfbox-graphics2d by rototor.
the class PdfBoxGraphics2DLosslessImageEncoder method encodeImage.
@Override
public PDImageXObject encodeImage(PDDocument document, PDPageContentStream contentStream, Image image) {
final BufferedImage bi;
if (image instanceof BufferedImage) {
bi = (BufferedImage) image;
} else {
int width = image.getWidth(null);
int height = image.getHeight(null);
bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics graphics = bi.getGraphics();
if (!graphics.drawImage(image, 0, 0, null, null))
throw new IllegalStateException("Not fully loaded images are not supported.");
graphics.dispose();
}
try {
if (doc == null || doc.get() != document) {
imageMap = new HashMap<ImageSoftReference, SoftReference<PDImageXObject>>();
profileMap = new HashMap<ProfileSoftReference, SoftReference<PDICCBased>>();
doc = new SoftReference<PDDocument>(document);
}
SoftReference<PDImageXObject> pdImageXObjectSoftReference = imageMap.get(new ImageSoftReference(image));
PDImageXObject imageXObject = pdImageXObjectSoftReference == null ? null : pdImageXObjectSoftReference.get();
if (imageXObject == null) {
imageXObject = LosslessFactory.createFromImage(document, bi);
/*
* Do we have a color profile we need to embed?
*/
if (bi.getColorModel().getColorSpace() instanceof ICC_ColorSpace) {
ICC_Profile profile = ((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile();
/*
* Only tag a profile if it is not the default sRGB profile.
*/
if (((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile() != ICC_Profile.getInstance(ColorSpace.CS_sRGB)) {
SoftReference<PDICCBased> pdProfileRef = profileMap.get(new ProfileSoftReference(profile));
PDICCBased pdProfile = pdProfileRef == null ? null : pdProfileRef.get();
if (pdProfile == null) {
pdProfile = new PDICCBased(document);
OutputStream outputStream = pdProfile.getPDStream().createOutputStream(COSName.FLATE_DECODE);
outputStream.write(profile.getData());
outputStream.close();
pdProfile.getPDStream().getCOSObject().setInt(COSName.N, profile.getNumComponents());
profileMap.put(new ProfileSoftReference(profile), new SoftReference<PDICCBased>(pdProfile));
}
imageXObject.setColorSpace(pdProfile);
}
}
imageMap.put(new ImageSoftReference(image), new SoftReference<PDImageXObject>(imageXObject));
}
return imageXObject;
} catch (IOException e) {
throw new RuntimeException("Could not encode Image", e);
}
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDICCBased in project pdfbox by apache.
the class StandardColorSpaceHelper method processICCBasedColorSpace.
/**
* Method called by the processAllColorSpace if the ColorSpace to check is a ICCBased color space. Because this kind
* of ColorSpace can have alternate color space, the processAllColorSpace is called to check this alternate color
* space. (Pattern is forbidden as Alternate Color Space)
*
* @param colorSpace
* the color space object to check.
*/
protected void processICCBasedColorSpace(PDColorSpace colorSpace) {
PDICCBased iccBased = (PDICCBased) colorSpace;
try {
ICC_Profile.getInstance(iccBased.getPDStream().createInputStream());
PDColorSpace altpdcs = iccBased.getAlternateColorSpace();
if (altpdcs != null) {
ColorSpaces altCsId = ColorSpaces.valueOf(altpdcs.getName());
if (altCsId == ColorSpaces.Pattern) {
context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN, "Pattern is forbidden as AlternateColorSpace of a ICCBased"));
}
/*
* According to the ISO-19005-1:2005
*
* A conforming reader shall render ICCBased colour spaces as specified by the ICC specification,
* and shall not use the Alternate colour space specified in an ICC profile stream dictionary
*
* We don't check the alternate ColorSpaces
*/
}
} catch (IllegalArgumentException e) {
// this is not a ICC_Profile
context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_ICCBASED, "ICCBased color space is invalid: " + e.getMessage(), e));
} catch (IOException e) {
context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read ICCBase color space: " + e.getMessage(), e));
}
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDICCBased in project pdfbox by apache.
the class CSArrayBased method initUI.
private void initUI() {
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setPreferredSize(new Dimension(300, 500));
if (colorSpace == null) {
JLabel error = new JLabel(errmsg);
error.setAlignmentX(Component.CENTER_ALIGNMENT);
error.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
panel.add(error);
return;
}
JLabel colorSpaceLabel = new JLabel(colorSpace.getName() + " colorspace");
colorSpaceLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
colorSpaceLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 30));
panel.add(colorSpaceLabel);
if (numberOfComponents > 0) {
JLabel colorCountLabel = new JLabel("Component Count: " + numberOfComponents);
colorCountLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
colorCountLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
panel.add(colorCountLabel);
}
if (colorSpace instanceof PDICCBased) {
PDICCBased icc = (PDICCBased) colorSpace;
int colorSpaceType = icc.getColorSpaceType();
String cs;
switch(colorSpaceType) {
case ColorSpace.CS_LINEAR_RGB:
cs = "linear RGB";
break;
case ColorSpace.CS_CIEXYZ:
cs = "CIEXYZ";
break;
case ColorSpace.CS_GRAY:
cs = "linear gray";
break;
case ColorSpace.CS_sRGB:
cs = "sRGB";
break;
case ColorSpace.TYPE_RGB:
cs = "RGB";
break;
case ColorSpace.TYPE_GRAY:
cs = "gray";
break;
case ColorSpace.TYPE_CMYK:
cs = "CMYK";
break;
default:
cs = "type " + colorSpaceType;
break;
}
JLabel otherLabel = new JLabel("Colorspace type: " + cs);
otherLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
otherLabel.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
panel.add(otherLabel);
}
}
Aggregations