You are on page 1of 2

20 September 2022

1.3 Compression
Lossy compression

• We remove parts of data in order to reduce the file size.


• Consider these examples:
◦ Photograph to be used on a school’s website
▪ The physical dimensions can be reduced or the image cropped to remove unwanted sections.
▪ Reduce the number of colours on the image. As the number of colours increases the number
of bits required per pixel also increases. So reducing the colours helps to reduce the file size.
▪ Many graphics software have features that help to remove certain shades to optimise the
image for use on a website. This is done without compromising on the quality of the image
to a great deal. Eg: PhotoShop
◦ An image to be used on an e-commerce app
▪ The app needs to quickly display the images of products.
▪ In order to do this, we either need to reduce the file size or have a high-speed connection.
▪ The better alternative is to reduce the file size.
◦ Images for use on printed media like brochures and magazines
▪ Printed images need to have a high quality.
▪ Therefore, reducing the number of colours would give a negative image about the product.
▪ So it is better not to use lossy compression on an image that needs to go a high-quality
printed magazine.
◦ Source code written in scripting language like JavaScript or an interpreter-based language.
▪ We cannot remove lines of code from a program as it would break the program or make it
incomplete.
▪ Programs often have embedded comments and white spaces – blanks, tabs, empty lines,
indents. The comments and white spaces can be removed without causing the program to
change. This also reduces the file size.

NOTE: Refer to the note on image file size calculation to see how reducing the number of colours works as
lossy compression.

Lossless compression

• Here we reduce the file size by encoding or representing the data in an alternative manner. We do
not remove any data.

B[8]
B[3]W[3]B[2]
W[2]K[2]B[4]
WK[5]B[2]
K[8]

• In the above example, the colours of the bitmap image are represented as:
◦ B = Blue
◦ W = White
◦ K = Black
• Instead of storing the actual pixels, we represent them as encoded values with the number of
iterations.
• For example, on line 4, it shows that we have 1 white, 5 blacks and 2 blues.
• This method of lossless compression is known as Run Length Encoding (RLE).
• Since nothing is removed during compression, we are able to recreate the original image.
• Thus lossless compression can be applied to any data such as documents, program code and images.
• In the past (around 35 years ago) lossless encoding was popular with facsimile (fax) transmissions.
This was extremely beneficial given that phone lines used to have very low bandwidth
(transmission speeds).

NOTE: RLE is one of the many different lossless compression methods. There are other, more efficient
algorithms for compression. Eg: Huffman encoding.

You might also like