Max
Max

Reputation: 101

Image size increases after loading

I have an RGB JPEG image which weighs about 11 MB and its resolution 7680 x 4320. I use an array of uchar4 to store it in the RAM. sizeof(uchar4) is 4 bytes, which is logical. It is not hard to calculate that the size of the array I use will be 4 x 7680 x 4320 = 132710400 bytes = [approx.] 126 MB. So how comes that the image weighs only 11 MB when it's stored on the hard drive and weighs 126 MB after being loaded into the RAM.

Upvotes: 0

Views: 67

Answers (1)

Frank Puffer
Frank Puffer

Reputation: 8215

So actually your question is why is the image size smaller when it is stored on disk because the size in memory is actually what you expected, right?

Unfortunately you did not tell us which file format is used to store the image, but basically all common image formats don't store the pixel values as-is. They apply a compression algorithm first. Some formats like PNG or GIF use lossless compression, others like JPEG use lossy compression which means that image quality gets slightly worse whenever the image is stored. However these formats allow a better compression.

All compression algorithms depend on the fact that image pixels are not (statistically) independent from each other. Nearby pixels are usually correlated. This correlation is used to reduce the amount of data. Because different images typically have different correlations, the image file size can vary even if the number of pixels is the same.

Upvotes: 4

Related Questions