Skip to content

Add CmykColorConverter for print production (RGB to CMYK with ICC profiles) - #1811

Merged
andreasrosdalw merged 1 commit into
LibrePDF:masterfrom
andreasrosdal:cmyk-colour-conversion
Dec 2, 2026
Merged

andreasrosdalw merged 1 commit into
LibrePDF:masterfrom
andreasrosdal:cmyk-colour-conversion

Conversation

@andreasrosdal

@andreasrosdal andreasrosdal commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description of the new Feature/Bugfix

Adds CmykColorConverter, which converts RGB colours and images to the CMYK colour space of a printing condition using its ICC profile, for example ISOnewspaper26v4 (newspapers, WAN-IFRA) or Coated FOGRA39 (offset on coated paper).

Print production expects CMYK content that matches the output intent: PDF/X-4 (#1809), PDF/X-1a and PDF/A with a CMYK output intent, and the Ghent Workgroup specifications that newspapers use to check ads. Until now OpenPDF could only write RGB images and colours as RGB, so users had to convert them with another tool first.

Usage

CmykColorConverter converter = new CmykColorConverter(ICC_Profile.getInstance(newspaperProfileBytes));

writer.setPDFXConformance(PdfWriter.PDFX4);
document.open();
converter.setOutputIntent(writer, "ISOnewspaper26v4", "ISO 12647-3:2013, Newspaper");

document.add(converter.toCmykImage(photoJpegBytes));                         // DeviceCMYK image
Font red = new Font(font, 12, Font.NORMAL, converter.toCmyk(new Color(200, 30, 30)));
cb.setColorFill(converter.toCmyk(Color.ORANGE));                             // CMYKColor
  • CmykColorConverter(ICC_Profile) rejects a profile that is not CMYK.
  • toCmyk(Color) returns a CMYKColor and keeps the alpha value. A CMYKColor is returned unchanged.
  • toCmykImage(java.awt.Image) and toCmykImage(byte[] imageFile) (PNG, JPEG, GIF, BMP through ImageIO) return a DeviceCMYK Image with 8 bits per component. An alpha channel becomes a soft mask, which PDF/X-4 and PDF/A-2/3 allow. The colours are first converted to sRGB, so images with an embedded profile such as Adobe RGB are converted correctly.
  • setOutputIntent(PdfWriter, String, String) sets the same profile as the output intent (GTS_PDFX, or GTS_PDFA1 for PDF/A).

The conversion uses the colour management of the JDK (Little CMS, through ColorConvertOp) with the default rendering intent of the profile, usually perceptual. The new class adds no dependency.

Not in this PR

  • Converting images that are already in a PDF, for example while stamping. This PR converts content before it is added.
  • A check of the total ink limit (for example 240% for newsprint). A profile for the printing condition already limits the total ink of converted colours.
  • Converting JPEGs without decoding them: the converted image is written with Flate compression, not as a CMYK JPEG.

Unit-Tests for the new Feature/Bugfix

CmykColorConverterTest (10 tests). The tests use TestCmykProfile, which creates a small valid CMYK ICC profile (v2, Lab lookup tables) in code, so the repository needs no press profile with unclear distribution terms.

  • An RGB profile is rejected. White uses no ink, black uses black ink, red becomes magenta and yellow. Alpha is kept.
  • Image conversion: pixel values, 4 components, no mask for opaque images, a soft mask with the alpha values for transparent images, a PNG file, an unsupported file.
  • A PDF/A-2b document with the profile as CMYK output intent, CMYK text and converted JPEG and PNG images passes veraPDF. veraPDF checks that DeviceCMYK content matches a CMYK output intent.

I also checked the converter locally with a real press profile, the Microsoft SWOP profile that comes with Windows. Gray 128 became C25 M21 Y20 K36 and red became M100 Y100. That profile is not part of the PR.

./mvnw -B install passes locally on Java 25 for all modules, and checkstyle:check reports nothing on the new files. The changelog for 4.0.1 is updated.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 70 complexity · 1 duplication

Metric Results
Complexity 70
Duplication 1

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Print production formats such as PDF/X-4 with a CMYK output intent,
for example for newspapers (ISOnewspaper26v4) or offset printing
(FOGRA39), expect the content in the CMYK colour space of the printing
condition. OpenPDF could only write RGB colours and images as RGB.

CmykColorConverter converts with the ICC profile of the printing
condition and the colour management of the JDK:

- toCmyk(Color) returns a CMYKColor for text, lines and fills.
- toCmykImage(java.awt.Image) and toCmykImage(byte[]) return a
  DeviceCMYK image. Transparency becomes a soft mask.
- setOutputIntent(PdfWriter, String, String) sets the profile as the
  output intent of the document.

The tests create a small CMYK ICC profile in code, so they need no
profile file with license restrictions. A PDF/A-2b document with that
profile as output intent and converted text and images passes veraPDF.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andreasrosdal
andreasrosdal force-pushed the cmyk-colour-conversion branch from 14bb73f to e058ae6 Compare December 2, 2026 21:57
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@andreasrosdalw
andreasrosdalw merged commit 053363d into LibrePDF:master Dec 2, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants