Skip to content

BMP layouts, Huffman 1D and normalized segment integrity handling - #3284

Merged
JimBobSquarePants merged 17 commits into
mainfrom
js/bmp-layouts
Nov 24, 2026
Merged

JimBobSquarePants merged 17 commits into
mainfrom
js/bmp-layouts

Conversation

@JimBobSquarePants

@JimBobSquarePants JimBobSquarePants commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

BMP

  • Replace the 16-bit layout option with BMP color types. Add 5-6-5 encode with bit fields.
  • Reuse a decoded palette when the encoder has no quantizer and the color table fits the bit depth.
  • Share the palette index packing for 1, 2, 4 and 8 bits (EncodingUtilities.PackIndices) between BMP, PNG and TIFF. Write each BMP row with one stream write.
  • Add RLE8 and RLE4 encode.
  • Read V5 ICC profiles only when they are embedded.
  • Add OS/2 Huffman 1D read and write. The CCITT code moves from TIFF to Compression/Ccitt and is shared.
  • Validate the color table size of BMP, ICO and CUR entries before the palette is allocated.

ANI, ICO, CUR

  • Write ANI frames as ICO or CUR only, and store a reused frame once.
  • Validate icon metadata. ICO and CUR read the AND mask of 16-bit and 24-bit entries.

Segment integrity handling

  • New SegmentIntegrityHandlingExtensions owns the policy for all decoders: the mode checks, the ancillary and image-data reports with cold throws, the catch filters and the segment actions.
  • Segment code reports each error that it finds through the owner and then returns. Strict keeps the original exception.
  • In the default mode, decoders keep every pixel that they can decode from damaged or truncated data. This covers AV1/HEIF (OBU headers, layered items, extents, samples, tiles), JXL (container read before image data), GIF, JPEG, PNG, EXR, PBM, QOI, TIFF, WebP, BMP and ICO.
  • Fixes found by review include: GIF LZW reads past the frame end, PNG Adam7 passes without columns (decode and encode), PNG IDAT chunks after the zlib stream end, the TIFF LZW table bound, and TIFF strips that end early.

Tests

  • Tests compare whole images with ImageComparer against exact expectations from the file structure.
  • The Issue2859 GIF reference images are replaced. The old images held rows decoded after a bad LZW code.

Performance

The results comments in DecodeBmp, EncodeBmp and EncodeBmpMultiple are updated.

BMP
- The encoder writes 16-bit 5-6-5 pixels with BI_BITFIELDS. BmpBit16Layout
  selects the layout. BmpEncoder.Bit16Layout overrides BmpMetadata.Bit16Layout,
  and the decoder records the layout. A V3 header gets the 12-byte mask block.
- BmpEncoder.Quantizer defaults to null. Without a quantizer, an indexed
  output reuses the decoded color table when it fits the depth.
- The transparent color mode of the encoder applies to every quantizer.

Icons
- ICO, CUR and ANI metadata record the decoded 16-bit layout. The icon
  encoders still write 5-5-5, because the format permits only BI_RGB.
- ANI raw bitmap frames record their color table, and the encoder reuses the
  palette of each frame, as the icon encoder does.

Pixel formats
- Bgr565 is a common pixel type: IPixel.FromBgr565, generated bulk
  conversions, and integer UNORM conversions through ColorNumerics.ScaleUnorm.
- PixelConverter packs and unpacks Bgr565 and Bgra5551 with one SIMD kernel,
  through the IPacked16Layout and IChannelOrder operators.

Quantizers
- Decoded palettes are reused with exact color matching. The PNG, BMP, icon
  and ANI encoders use EncodingUtilities.CreateDecodedPaletteQuantizer.
- The first GIF frame uses the options of the decoded global palette.
- The 1-, 2- and 4-bit BMP paths keep the options of the quantizer.

Tests
- The GIF ICC test allows one 8-bit code per channel against the independent
  decoder, because a value near a rounding boundary can round either way.
- The Issue2866 reference frames are updated. The new output has less error
  against the source on each compared frame.
BmpEncoder now takes a BmpColorType and a BitsPerPixel, and resolves them
as the PNG encoder does: encoder options take precedence over metadata,
and a depth that does not fit the color type uses the default depth of
that color type. The color type selects the info header and the masks.
This replaces BitsPerPixel with Bit16Layout and SupportTransparency.

The color type names give the channels in storage order: Bgr, Bgra,
Bgr565 and Bgra5551. BMP and TGA metadata now report BGR, and 16-bit TGA
reports four components.
A V5 header with PROFILE_LINKED holds the file name of the profile at the
profile offset, not ICC data. The decoder now reads the profile only when
CsType is PROFILE_EMBEDDED. The profile-size test now declares an embedded
profile, so its profile fields apply.
# Conflicts:
#	src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
…nd TGA

- BmpRleEncoder writes RLE8 and RLE4 rows. A run splits an absolute run only
  when the split makes the row smaller.
- New public BmpCompression option on BmpEncoder and BmpMetadata. The header
  enum is now the internal BmpInfoCompression.
- TgaEncoder.Compression is nullable and falls back to TgaMetadata.Compression.
  Both decoders set the metadata value.
- ICO and ANI bitmaps always write BI_RGB, as the icon format requires.
- TgaImageType and its extensions are internal. Remove the unused public
  BmpFileMarkerType.
- The icon AND mask uses pooled buffers and PackIndices.
…lidate icon metadata

- ANI: the ACON specification defines each frame as an embedded .CUR file. The
  encoder now writes ICO and CUR frames only and always sets AF_ICON. The
  decoder reads raw-bitmap frames in the layout that ReactOS reads, and reports
  them as CUR frames. AniFrameFormat.Bmp and the reserved AniMetadata header
  properties are removed.
- ANI: SequenceNumber now identifies the stored frame. The encoder stores each
  stored frame once and writes a 'seq ' chunk when a step plays a stored frame
  again.
- Metadata: icon and ANI frame color tables hold at most 256 entries, TGA alpha
  channel bits at most 15, TGA color tables at most 65535 entries, and the ANI
  name and artist are ASCII.
- BMP encoder: the alpha mask loop honors cancellation.
- Move the CCITT T.4, T.6 and Modified Huffman code to Compression/Ccitt.
  TIFF and BMP use the same bit reader, bit writer, code tables and row
  coders.
- Make the CCITT row decoders and encoders faster with a 64-bit bit window,
  lookup tables and packed row writes.
- Add BmpCompression.Huffman1D. Read and write OS/2 BITMAPINFOHEADER2
  bitmaps with Huffman 1D data.
- Rename the BmpInfoCompression members to .NET casing and fix their docs.
- Map 16-bit BMP bit fields to exact layouts only. Write zero unused alpha
  for 32-bit Bgr. Ignore the fourth byte of V3 32-bit RGB bitmaps.
- Report the 16-bit icon layout correctly in ICO, CUR and ANI metadata.
- TIFF: fix BlackIsZero polarity for CCITT data, planar strip and tile
  counts, and damaged fax blocks.
- Fix docs, comments and spec citations.
…amaged data

- Add SegmentIntegrityHandlingExtensions as the single owner of the integrity
  policy: mode checks, ancillary and image-data reports with cold throws,
  catch filters, and segment actions. Remove the duplicate helpers from
  ImageDecoderCore and HeifDecoderCore and the WebP delegate plumbing.
- Report every error that segment code detects through the owner, and keep
  original exceptions in Strict.
- AV1/HEIF: keep decoded frames when data ends inside an OBU header, size
  field, layered item, extent or sample; stop appending extents after a
  truncated one.
- JXL: read the container structure before image data, as HEIF does; a
  truncated codestream box follows the image-data policy.
- GIF: make the LZW end of data sticky and stop the frame at an invalid code.
- JPEG: fill zero bits at end of stream; report truncated arithmetic scans;
  apply the ancillary policy to truncated APPn and COM segments.
- PNG: fix Adam7 passes without columns in the decoder and encoder; skip IDAT
  chunks after the zlib stream ends.
- EXR, PBM, QOI, TIFF, BMP, ICO, CCITT and WebP integrity fixes from review.
- Tests compare whole images with exact, structure-derived expectations.
- Replace the Issue2859 GIF reference images, which recorded rows decoded
  after a bad LZW code.
@JimBobSquarePants
JimBobSquarePants merged commit a7c67cd into main Nov 24, 2026
12 checks passed
@JimBobSquarePants
JimBobSquarePants deleted the js/bmp-layouts branch November 24, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant