Compression
- Appropriate use of compression can reduce file sizes.
- Small file sizes can be transmitted and processed more quickly.
For example, a whole webpage can be compressed, reducing the amount of data which has to be sent to the client computer and therefore reducing the load time. The browser will decompress the page before displaying it.
Individual media elements audio, graphics and video within the webpage could also be compressed in order to reduce the size further.
- Lossless compression reduces file sizes without removing any data.
- Lossy compression reduces file sizes by removing some data.
Audio Formats
MP3 (.mp3) Lossy compression
- Removes the very high or low frequencies that humans cannot hear.
- When two similar sounds (but different volume) occur at the same time, removing the quieter one.
- User can choose the level of compression.
- A full compression MP3 compresses data to around 10% of the original size, with relatively little loss of quality.
Audio File Size Calculations
To calculate the file size (in bits) of an uncompressed audio file the basic formula is:
sample frequency (Hz) x sample depth (bits) x length (seconds) x number of channels
- Sample frequency is the number of times per second that a sample of the original sound was taken.
1 Hz = once every second. 1 KHz = 1000 times every second. - Sample depth is the number of bits that were used to store each sample.
- Length of sound should always be converted into seconds if necessary.
- Number of channels is typically 1 (mono) or 2 (stereo), although it could be more for a surround sound file.
Calculate the file size of a mono voice recording captured at 24KHz with a sample depth of 8 bits, lasting for 15 seconds.
24000 x 8 x 15 x 1 = 2880000 bits
2880000 / 8 = 360000 bytes
360000 / 1024 = 351.56 KB
Graphic Formats
JPEG (.jpg) Lossy compression
- 24 bit colour.
- Allows the user to select an appropriate level of compression.
- JPEG is a good compression format for high-quality images and images where quality is unimportant (e.g. thumbnails).
PNG (.png) Lossless compression
- Intended to improve upon GIF format.
- 24 bit colour.
- Allows user to set alpha level for improved transparency.
GIF (.gif) Lossless compression • Restricted to 8 bit colour.
- Uses LZW algorithm to compress data.
- Several images can be stored in one GIF file, allowing for animation.
- Allows a specific colour in an image to be made transparent.
- GIF is a good compression format for images with areas of flat colour, images that have few colours, animated images or images that require transparency.
RLE (Run Length Encoding) is a lossless compression algorithm.
- It works by taking runs of the same colour and storing them as two numbers: how many pixels and colour code of this run of pixels.
- RLE works best for images with large blocks of the same colour.
- If an image has many different colours (e.g. a photo) RLE could potentially end up increasing the file size!
LZW (Lempel Ziv Welch) is a lossless compression method which is more effective than RLE.
- It works by looking for repeated patterns of bits within data, stores them in a dictionary, then replaces them with references to the dictionary.
Indexed colour involves storing the actual RGB codes used by the image in a separate colour palette, in order to reduce file size. This can be used as a compression technique where the bit depth of the graphic is less than 24.
Graphic file size calculations
To calculate the file size (in bits) of a bitmap graphic file the basic formula is:
number of pixels in image x bit depth
- Bit depth or colour depth is the number of bits that were used to store the colour of each pixel.
Example 1
Calculate the file size of a 400 x 400 pixel graphic with a colour depth of 24.
400 x 400 = 160000 pixels in the image
160000 x 24 = 3840000 bits
3840000 / 8 = 480000 bytes
480000 / 1024 = 468.75 KB
Example 2
Calculate the file size of a 300 x 200 graphic with 256 colours.
256 colours means a colour depth of 8 (because 28 = 256)
300 x 200 = 60000 pixels in the image
60000 x 8 = 480000 bits
480000 / 8 = 60000 bytes
60000 / 1024 = 58.6 KB
Example 3
Calculate the file size of a 6’ x 5’ image, scanned at 200dpi, with 16 bit colour.
6 x 5 x 200 x 200 = 1200000 pixels in image
1200000 x 16 = 19200000 bits
19200000 / 8 = 2400000 bytes
2400000 / 1024 = 2343.75 KB
2343.75 / 1024 = 2.3 MB
Video Formats
MPEG (.mp4) Lossy compression
- The level of compression can be determined by the user.
- Key frames (individual still images) are compressed and stored as JPEGs – generally only one full frame in every 8 to 24 frames needs to be stored.
- Any data that stays the same beyond the key frame does not need to be stored. Changes to the image in each subsequent frame are then stored.
AVI (.avi) No compression built-in
- Limited to 320 x 240 resolution
- Limited to 30 frames per second
- Can take up a large amount of storage space and can be quite RAM intensive.
Video file size calculations
To calculate the file size (in bits) of an uncompressed video file the basic formula is:
number of pixels in frame x bit depth x frame rate (fps) x length of video (in seconds)
A video is simply a series of bitmap images, so this rule simply multiplies the size of one image by the number of images in the video.
Calculate the file size of a 300 x 250 pixel video, using 16 bit colour, 30 frames per second and lasting 30 seconds.
300 x 250 = 75000 pixels
75000 x 16 = 1200000 bits for 1 image
1200000 x 30 = 36000000 bits for 1 second of video
36000000 x 30 = 1080000000 bits for entire video
/8 = 135000000 bytes
/1024 = 131835.94 KB
/1024 = 128.75 MB
Thank you to C O’Toole & A Madill from Braidhurst High School for allowing me to edit and publish this here.