Decode/read a large PNG stream in chunks/parts of bitmap data instead of committing the entire file to memory

Viewed 52

I am attempting to make a program that takes a PNG image from disk, modifies the RGB components of all pixels in the image (the transformation is just some basic math), and outputs/reencodes this to another output PNG image file on disk.

My problem is that the input images that I wish to run this program on are typically very large (8192x8192 up to 65535x65535). The naïve method of decoding the entire input PNG into a bitmap in memory (then transforming and reencoding) would far exceed the 8gb of memory my laptop provides me.

Is there any easy method that I can use to read and decode a large PNG stream in small buffers/chunks of bitmap data (thereby only ever allocating a small buffer instead of reading and decoding the entire file into memory) in .NET 6?. Or is this a situation where I must write my own decoder to allow for buffering and working on that buffer in this manner?

All images can be assumed to be 8-bit per channel RGB -- no alpha channel (Type 2), no interlacing (Type 0).

0 Answers
Related