Skip to content

Map preview image offsets through page file offsets and wrap bare bitmaps - #45

Merged
mahach666 merged 4 commits into
mainfrom
fix/10-preview-offsets
Nov 16, 2026
Merged

mahach666 merged 4 commits into
mainfrom
fix/10-preview-offsets

Conversation

@mahach666

Copy link
Copy Markdown
Contributor

Fixes #10

Problem

The image directory in AcDb:Preview stores absolute file offsets, but PreviewParser read them as section-relative offsets. AC1024 previews therefore resolved to nothing, AC1027/AC1032 PNGs were only recovered by the byte-pattern fallback, and a BMP slot holding arbitrary bytes was still labelled Bmp.

Change

  • R2004Container.ReadSection can now report each page's section offset, decompressed length and payload file offset. Only uncompressed pages are eligible for absolute-offset mapping.
  • PreviewParser converts each directory start to a section position through the page layout and range-checks the result before reading. Section-relative offsets remain a fallback for writers that stored them that way.
  • A bare DIB (bitmap info header plus pixels, no BM) is wrapped in a 14-byte BITMAPFILEHEADER. bfOffBits is computed from the DIB header size, bit depth, compression and palette size.
  • ImageKind is now derived from the bytes actually extracted (PNG, BMP or WMF); bytes that match no format produce ImageKind.None and HasImage == false.
  • The comment in Preview_HasImageWhenSectionPresent now describes the real reason a preview section can lack an image.

Tests

  • Synthetic preview with an absolute directory offset mapped through a page layout.
  • Bare 8-bpp and 24-bpp DIBs get a BMP file header with the expected bfOffBits.
  • Invalid image bytes yield ImageKind.None and no bytes.
  • The existing section-relative PNG test still passes.

Verification

  • dotnet build DwgIO.slnx -c Release: succeeded.
  • dotnet test DwgIO.slnx -c Release --no-build: 73 passed, 7 fixture tests skipped, 0 failed on net8.0 and net10.0.
  • DWGIO_TEST_FILES=<AC1024 sample folder> on net10.0: 80 passed, 0 skipped, 0 failed.
  • AC1024 sample: preview is now Bmp with 86,070 bytes; the file starts with BM, bfSize 86,070, bfOffBits 1,078, 329x256 at 8 bpp.
  • AC1027 sample: Png 2,022 bytes, SHA256 identical to the pre-change export (F547...C515).
  • AC1032 sample: the declared directory entry is a 1,448-byte PNG ending exactly at IEND; the old fallback returned 1,720 bytes because it scanned to the last sentinel and included a second, undeclared preview block after the first end sentinel. The new result is the 1,448-byte image described by the directory.

…maps

AcDb:Preview directory entries store absolute file offsets. ReadSection now reports each page's section position and payload file offset, and PreviewParser converts directory offsets to section positions through that layout, range-checking every candidate. Bitmap previews are stored as a bare DIB, so a 14-byte BMP file header with a computed pixel offset is added. ImageKind is derived from the bytes actually found instead of the directory slot, and invalid bytes no longer produce a Bmp preview.
Keep the image kind found in its directory slot instead of
re-detecting (which dropped WMF), include WMF in DetectKind,
account BI_BITFIELDS masks in bfOffBits, and extract images
whose bytes span consecutive uncompressed pages.
BI_ALPHABITFIELDS carries red, green, blue and alpha masks after
the 40-byte header; BI_BITFIELDS carries three. Add a 32bpp
regression test expecting bfOffBits 70.
@mahach666
mahach666 merged commit 89b9c07 into main Nov 16, 2026
4 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.

Thumbnail previews are not found in AC1024 drawings because image offsets are read as section-relative

1 participant