Is there some way to predict png size?

前端 未结 1 1232
臣服心动
臣服心动 2021-01-23 07:50

I am implementing clipboard, and I want to allocate memory for png image one time. Is there some way to predict maximum size of png file?

相关标签:
1条回答
  • 2021-01-23 08:39

    A PNG image includes several things:

    1. Signature and basic metadata (image size and type)
    2. Palette (only if the image is indexed)
    3. Raw pixel data
    4. Optional metadata (ancillary chunks)
    5. End of image chunk

    Size of item 1 is fixed: 8 + 12 + 11 = 31 bytes

    Size of item 2 (if required) is at most 12 + 3 * 256 = 780 bytes

    Size of item 5 is fixed: 12 bytes

    Item 3, raw pixels data, is usually the most important one. The filtered-uncompressed data amounts to

    FUD=(W*C*8/BPC+1)*H bytes

    Where W=width in pixels, H=height in pixels, C=channels (3 if RGB, 1 if palette or grayscale, 4 if RGBA, 2 if GA), BPC=bits per channel (normally 8)

    That is compressed with ZLIB. It's practically impossible to bound precisely the worst case compression rate. In practice, one might assume that in the worst case the compressed stream will have a few bytes more than the original. Then the item 3 size would be approximately bound by (again assuming a fairly small IDAT chunk size of 8192 bytes) by

     (FUD + 6)(1 + 12/8192) ~ FUD
    

    Item 4 (ancillary chunk data) is practically impossible to bound.

    0 讨论(0)
提交回复
热议问题