Skip to content

DW-9: Load image using original BPP and support changing color depth#149

Open
Sawraz-IS wants to merge 1 commit into
developfrom
DW-9-load-image-using-original-bpp
Open

DW-9: Load image using original BPP and support changing color depth#149
Sawraz-IS wants to merge 1 commit into
developfrom
DW-9-load-image-using-original-bpp

Conversation

@Sawraz-IS
Copy link
Copy Markdown
Collaborator

@Sawraz-IS Sawraz-IS commented Jun 1, 2026

Description

AnyBitmap.BitsPerPixel previously reported 32 bpp for every TIFF, including 1‑bpp black & white scans, because TIFFs are decoded into a 32‑bpp Rgba32 image (via LibTiff / ImageSharp) and the property simply returned the in‑memory pixel depth. SixLabors.ImageSharp has no pixel format below 8 bpp, so the original color depth was lost.

This PR makes BitsPerPixel report the original color depth of the source image when it is loaded preserving its original format (the FromFile default), matching what System.Drawing.Bitmap reports (e.g. Format1bppIndexed → 1).

What changed (AnyBitmap.cs):

  • Added an _originalBitsPerPixel field, populated during LoadImage for TIFFs (when preserveOriginalFormat == true) by reading BitsPerSample × SamplesPerPixel straight from the TIFF metadata via a new lightweight GetTiffBitsPerPixelFast() helper (modeled on the existing GetTiffFrameCountFast() — no full decode required).
  • BitsPerPixel now returns _originalBitsPerPixel ?? InMemoryBitsPerPixel. Non‑TIFF formats and preserveOriginalFormat == false are unchanged (still report the decoded depth, e.g. 32).
  • GetStride() now uses the in‑memory pixel depth rather than the reported BitsPerPixel, so stride stays consistent with the 32‑bpp pixel data exposed by GetFirstPixelData().
  • Added ChangeBitsPerPixel(int) — the System.Drawing ChangeBpp equivalent. Returns a new AnyBitmap converted to 8 (grayscale), 24 (RGB) or 32 (RGBA); throws NotSupportedException for unsupported depths.

Type of change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Added unit tests in AnyBitmapFunctionality.cs using the attached sample TIFFs (added to the test Data folder):

  • DW_9_LoadImage_ShouldReturnOriginalBitsPerPixelScanDev_BW.tif→1, ScanDev_Gray.tif→8, ScanDev_Color.tif→24
  • DW_9_LoadBlackAndWhiteTiff_ShouldReturnOriginalBitsPerPixel_AndAllowChangingBpp — reproduces the exact reported scenario with tifimg.tif: BitsPerPixel == 1, cross‑checked against new System.Drawing.Bitmap(path).PixelFormat == Format1bppIndexed, plus ChangeBitsPerPixel(24) → 24
  • DW_9_LoadImage_NotPreservingOriginalFormat_ShouldReturn32BitsPerPixelpreserveOriginalFormat: false → 32
  • DW_9_ChangeBitsPerPixel_ShouldReturnRequestedColorDepth — 8 / 24 / 32
  • DW_9_ChangeBitsPerPixel_WithUnsupportedDepth_ShouldThrowNotSupportedException

Local results: full AnyBitmapFunctionality suite passes — 103/103 (net8.0) and 101/101 (net48), no regressions.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have successfully run all unit tests on Windows
  • I have successfully run all unit tests on Linux

Additional Context

Behavior change for BitsPerPixel on TIFFs loaded with preserveOriginalFormat = true (the FromFile default):

File Before After
tifimg.tif (B&W) 32 1
ScanDev_BW.tif 32 1
ScanDev_Gray.tif 32 8
ScanDev_Color.tif 32 24

@Sawraz-IS Sawraz-IS marked this pull request as ready for review June 1, 2026 08:29
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.

1 participant