1 year ago
#378143
kozeljko
Find CMYK coverage in PDF with Java
I'm working on a tool where the user could upload a PDF with an image and then receive information about area and CMYK colors from the image.
Initially I thought that RGB values would be enough, so I used PdfBox library(PDFRenderer class) to convert the PDF to an RGB image and then counted the colors for each pixel. This works, but now I need CMYK values. I've been spending some time trying to convert RGB values to CMYK values, but can't get good results. It's not accurate at all. I've never worked with color spaces before, so it's a struggle.
Is there a reliable way of loading a PDF document and reading its CMYK values? Is there maybe a reliable way of converting RGB to CMYK, that I missed?
// Load CMYK ICC file
InputStream is = TestClass.class.getClassLoader().getResourceAsStream("USWebCoatedSWOP.icc")
// Load PDF Document
PDDocument doc = PDDPDDocument.load(TestClass.class.getClassLoader().getResourceAsStream("test.pdf"))
// Render image from document
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 600, ImageType.RGB);
// Read a pixel from image
float[] rgbF = new float[3];
bufferedImage.getData().getPixel(0,0,rgbF);
// Load color spaces
ColorSpace cmykCS = new ICC_ColorSpace(ICC_Profile.getInstance(is));
// Prepare RGB values for CMYK (I assume we want RGB values in [0.0, 1.0] ranger. I did try [0.0, 255.0] range as well. Was even worse).
for (int i = 0; i < 3; i++) {
rgbF[i] = rgbF[i] / 255.0f;
}
// Convert to CMYK
float[] cmyk = cmykCS.fromRGB(rgbF);
I also tried first converting to CIEXYZ, but same result:
ColorSpace rgbCS = ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB);
float[] cmyk = ascmykCS.fromCIEXYZ(rgbCS.toCIEXYZ(rgbF))
java
pdfbox
color-space
cmyk
0 Answers
Your Answer