Skip to content

Fix indexed image detection to prevent PDF size inflation#1479

Merged
andreasrosdal merged 5 commits intomasterfrom
copilot/fix-issue-1289
Feb 16, 2026
Merged

Fix indexed image detection to prevent PDF size inflation#1479
andreasrosdal merged 5 commits intomasterfrom
copilot/fix-issue-1289

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

Fix indexed image detection to prevent PDF size inflation

Fixes #1289

Problem

Indexed images (GIFs and PNGs with indexed colors) were incorrectly detected as RGB images, causing significant PDF size inflation (100%+ in some cases as reported in the issue).

Root Cause

When loading GIF/PNG via ImageIO, BufferedImage with IndexColorModel was converted to RGB using PixelGrabber, which:

  • Lost the palette information
  • Expanded 1-byte indexed pixels to 3-byte RGB pixels
  • Significantly inflated the resulting PDF file size

Solution Implemented

Modified Image.getInstance(java.awt.Image, java.awt.Color, boolean) to:

  1. Detect BufferedImage with IndexColorModel before the PixelGrabber conversion
  2. Extract palette data (RGB values) directly from the IndexColorModel
  3. Extract pixel indices directly from the raster without conversion
  4. Create an indexed image with the original palette stored in PDF format: [/Indexed /DeviceRGB maxIndex palette]
  5. Normalize bitsPerPixel to valid PDF values (1, 2, 4, or 8)

Changes Made

  • Image.java: Added IndexColorModel detection and handling before the existing PixelGrabber code path
  • ImageTest.java: Added 2 comprehensive tests:
    • shouldDetectIndexedColorGif: Tests loading indexed GIF from file
    • shouldDetectIndexedColorFromBufferedImage: Tests programmatically created indexed BufferedImage

Testing Results

  • ✅ All 11 ImageTest tests pass
  • ✅ All 2039 existing openpdf-core tests pass with no failures or errors
  • ✅ Verified indexed GIF files are correctly detected with colorspace=1
  • ✅ Verified additional dictionary with indexed colorspace array is set
  • ✅ No security vulnerabilities detected by CodeQL

Verification

Tested with H.gif (indexed GIF from test resources):

  • Image correctly detected as indexed (colorspace=1)
  • Additional dictionary properly set with indexed colorspace
  • Raw data size minimal (1459 bytes for 171x250 image)
  • All existing tests continue to pass

This fix ensures that indexed images maintain their compact representation in PDFs, preventing the size inflation reported in issue #1289.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@andreasrosdal andreasrosdal changed the title [WIP] Fix issue with PDF image rendering Fix indexed image detection to prevent PDF size inflation Feb 16, 2026
Copilot AI and others added 4 commits February 16, 2026 11:14
Co-authored-by: andreasrosdal <259156774+andreasrosdal@users.noreply.github.com>
Co-authored-by: andreasrosdal <259156774+andreasrosdal@users.noreply.github.com>
Co-authored-by: andreasrosdal <259156774+andreasrosdal@users.noreply.github.com>
@sonarqubecloud
Copy link

@andreasrosdal andreasrosdal marked this pull request as ready for review February 16, 2026 11:22
@andreasrosdal andreasrosdal merged commit 7c04f66 into master Feb 16, 2026
12 of 13 checks passed
@andreasrosdal andreasrosdal deleted the copilot/fix-issue-1289 branch February 16, 2026 11:22
Copilot AI requested a review from andreasrosdal February 16, 2026 11:23
Copilot stopped work on behalf of andreasrosdal due to an error February 16, 2026 11:23
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.

regression since itext 2.1.7: indexed images not detected as indexed, sometimes inflating size

2 participants