Repository navigation
Speed up PNG decode and encode - #3285
Merged
Merged
Conversation
PNG decode: - One generic filter decoder for each filter. It uses pixel operators for pixels of 1, 2, 3, 4, 6 and 8 bytes. The separate filter classes are gone. - A scanline processor for each frame. Palette rows and gray rows of fewer than 8 bits use a pixel table and the shared PaletteExpander. - 8-bit and 16-bit rows convert in bulk through PixelOperations. - Adam7 pass 7 converts straight into the image row. - The CRC check of IDAT and fdAT chunks reads the data in blocks, with no buffer for each chunk. PNG encode: - 16-bit rows convert into the scanline buffer, then change to big-endian in place. - The adaptive filter computes the sums of all four filters in one pass, and writes only the chosen filter. Pixel operations: - Rgba32CompatiblePixelOperations<TPixel, TOrder> converts luminance and 16-bit layouts to each 8-bit RGB and RGBA layout with SIMD kernels. - Rgba64CompatiblePixelOperations<TPixel> converts each layout to Rgba64 and Rgb48. - Each source type's To method calls the destination's From method, so each conversion has one implementation. BMP and TIFF: - No decoder checks the pixel type. BMP bit fields write Rgba32 rows, and 5-6-5 masks use the bulk Bgr565 conversion. The TIFF 32-bit integer decoders convert through PixelOperations<Rgb96> and PixelOperations<Rgba128>. SimdUtils: - Remove FastRound, PseudoRound and HasVector8. Vector.Round replaces FastRound. - Generic Pad3 and Slice3 with the Pad3x8Layout and Pad3x16Layout masks. - Remove the obsolete Block8x8F benchmarks.
- DecodePng decodes one photo in each PNG color type and bit depth, and EncodePng encodes it to each 8-bit and 16-bit layout. Both use no filter, so the System.Drawing comparison stays fair. - PngFilterDecode covers each pixel width from 1 to 8 bytes. - Each PNG benchmark has its results as a comment. - The Sub decode uses byte lanes, because its predictor does not read the row above and needs no 16-bit arithmetic.
- PngFilterEncoder.Encode uses a left offset of 0 when the operator does not read the left pixel. The Up filter then starts its vector loops at the first byte, as on main. - The PngFilterEncode benchmark passes the real byte count for Up, as the encoder does. - The PNG filter decoder advances a pixel reference. - Unsafe.Add offsets added on this branch are unsigned. - DecodeFilteredPng decodes one photo stored with each filter, in 8-bit RGB and RGBA.
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.
Prerequisites
Description
This pull request makes PNG decode and encode faster.
PixelOperations.PixelOperations.Benchmarks
Summary:
mainallocates up to 34 KB.main.mainallocates up to 346 KB.maintakes 53.75 ms.main.mainand this branch ran in the same session on the same machine, with the benchmarkConfig.Shortsettings (3 iterations).DecodePngandEncodePnguse no filter.DecodePng
EncodePng
DecodeFilteredPng
The photo is encoded with one filter for all rows, as 8-bit RGB and 8-bit RGBA.