DW-9: Load image using original BPP and support changing color depth#149
Open
Sawraz-IS wants to merge 1 commit into
Open
DW-9: Load image using original BPP and support changing color depth#149Sawraz-IS wants to merge 1 commit into
Sawraz-IS wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
AnyBitmap.BitsPerPixelpreviously reported 32 bpp for every TIFF, including 1‑bpp black & white scans, because TIFFs are decoded into a 32‑bppRgba32image (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
BitsPerPixelreport the original color depth of the source image when it is loaded preserving its original format (theFromFiledefault), matching whatSystem.Drawing.Bitmapreports (e.g.Format1bppIndexed→ 1).What changed (
AnyBitmap.cs):_originalBitsPerPixelfield, populated duringLoadImagefor TIFFs (whenpreserveOriginalFormat == true) by readingBitsPerSample × SamplesPerPixelstraight from the TIFF metadata via a new lightweightGetTiffBitsPerPixelFast()helper (modeled on the existingGetTiffFrameCountFast()— no full decode required).BitsPerPixelnow returns_originalBitsPerPixel ?? InMemoryBitsPerPixel. Non‑TIFF formats andpreserveOriginalFormat == falseare unchanged (still report the decoded depth, e.g. 32).GetStride()now uses the in‑memory pixel depth rather than the reportedBitsPerPixel, so stride stays consistent with the 32‑bpp pixel data exposed byGetFirstPixelData().ChangeBitsPerPixel(int)— theSystem.DrawingChangeBppequivalent. Returns a newAnyBitmapconverted to 8 (grayscale), 24 (RGB) or 32 (RGBA); throwsNotSupportedExceptionfor unsupported depths.Type of change
How Has This Been Tested?
Added unit tests in
AnyBitmapFunctionality.csusing the attached sample TIFFs (added to the testDatafolder):DW_9_LoadImage_ShouldReturnOriginalBitsPerPixel—ScanDev_BW.tif→1,ScanDev_Gray.tif→8,ScanDev_Color.tif→24DW_9_LoadBlackAndWhiteTiff_ShouldReturnOriginalBitsPerPixel_AndAllowChangingBpp— reproduces the exact reported scenario withtifimg.tif:BitsPerPixel == 1, cross‑checked againstnew System.Drawing.Bitmap(path).PixelFormat == Format1bppIndexed, plusChangeBitsPerPixel(24)→ 24DW_9_LoadImage_NotPreservingOriginalFormat_ShouldReturn32BitsPerPixel—preserveOriginalFormat: false→ 32DW_9_ChangeBitsPerPixel_ShouldReturnRequestedColorDepth— 8 / 24 / 32DW_9_ChangeBitsPerPixel_WithUnsupportedDepth_ShouldThrow—NotSupportedExceptionLocal results: full
AnyBitmapFunctionalitysuite passes — 103/103 (net8.0) and 101/101 (net48), no regressions.Checklist:
Additional Context
Behavior change for
BitsPerPixelon TIFFs loaded withpreserveOriginalFormat = true(theFromFiledefault):tifimg.tif(B&W)ScanDev_BW.tifScanDev_Gray.tifScanDev_Color.tif