Objectives: Students should be able to —
- 1 Demonstrate how data storage is measured.
- 2 Calculate the file size of an image and sound file.
- 3 Identify and describe the purpose and need for data compression.
- 4 Describe how "Lossy compression" reduces the file size.
- 5 Describe how "Lossless compression" reduces the file size.
- 6 Give advantages and disadvantages of lossy and lossless compression.
Measurement of Data Storage
(i) Bit:
Bit stands for binary digit — the basic unit of data storage in computer memory.
It could be either 1 or 0, that represents the ON or OFF state of an electrical signal, which is the only thing a computer can understand.
(ii) Byte:
A group of 8 bits is called a Byte.
Byte is the smallest unit of memory in a computer.
Processors are all built to work with a set length of bits, which is usually a multiple of byte, like 8, 16, 32, 64, etc.
(iii) Nibble:
A group of 4 bits is called a Nibble, which is equal to half a byte.
A Nibble could represent 2⁴ = 16 possible values. Hence, each hexadecimal digit is represented by a nibble.
| Unit of Memory | Bits / Bytes | Bytes | General Name |
|---|---|---|---|
| 1 Nibble | 2² = 4 bits | — — — | Nibble |
| 1 Byte | 2³ = 8 bits | 1 Byte | Byte |
| 1 KiB (Kibibyte) | 2¹⁰ Bytes | 1024 Bytes | Kilo Byte |
| 1 MiB (Mebibyte) | 2²⁰ Bytes | 1024 KiB | Mega Byte |
| 1 GiB (Gibibyte) | 2³⁰ Bytes | 1024 MiB | Giga Byte |
| 1 TiB (Tebibyte) | 2⁴⁰ Bytes | 1024 GiB | Tera Byte |
| 1 PiB (Pebibyte) | 2⁵⁰ Bytes | 1024 TiB | Peta Byte |
| 1 EiB (Exbibyte) | 2⁶⁰ Bytes | 1024 PiB | Exa Byte |
| 1 ZiB (Zebibyte) | 2⁷⁰ Bytes | 1024 EiB | Zetta Byte |
| 1 YiB (Yobibyte) | 2⁸⁰ Bytes | 1024 ZiB | Yotta Byte |
In SI units, 1 Kilobyte (KB) = 1000 Bytes, 1 Megabyte (MB) = 1000 KB, 1 Gigabyte (GB) = 1000 MB, 1 Terabyte (TB) = 1000 GB, which is technically inaccurate because computer memory size is actually measured in terms of powers of 2.
Calculation of File Size
To calculate the file size of a bitmap image:
Size of image file (in bits) = Image resolution (in pixels) × Colour depth (in bits)
= Width (in pixels) × Height (in pixels) × Colour depth (in bits)
To calculate the file size of a Mono-sound:
Size of sound file (in bits) = Sample rate (in Hz) × Sample resolution (in bits) × Length of sound (in seconds)
To calculate the file size of a Stereo-sound (2 channels of audio):
Stereo is the sound recorded with two microphones placed in strategically chosen locations relative to the sound source, and played back through two channels (speakers). The two simultaneously recorded channels will be similar, but each will have distinct time-of-arrival and sound-pressure-level information.
Size of stereo-sound file (in bits) = Sample rate (in Hz) × Sample resolution (in bits) × Length of sound (in seconds) × 2 (number of channels)
To convert the size of file from bits to bytes, kilobytes, megabytes, etc.:
Size of file (in bytes) = File size in bits ÷ 8 bytes (since 1 byte = 8 bits)
Size of file (in Kibibytes) = File size in bits ÷ 8 × 1024 KiB (since 1 KiB = 1024 bytes)
Size of file (in Mebibytes) = File size in bits ÷ 8 × 1024 × 1024 MiB (since 1 MiB = 1024 KB)
(a) Image with 8 colours at 512 × 300 pixels:
8 colours = 2³ colours.
Hence, Colour depth = 3 bits (i.e. number of bits needed to represent 8 colours).
Working:
Size of image file (in bits) = Image resolution × colour depth = 512 × 300 × 3 bits
Size of image file (in bytes) = (512 × 300 × 3) ÷ 8 = 57 600 bytes (divide by 8, since 1 byte = 8 bits)
Size of image file (in KiB) = (512 × 300 × 3) ÷ (8 × 1024) = 56.25 KiB (or 57.6 KB approximately)
(b) Image converted into Black and White:
Black and White means 2 colours = 2¹ colours.
Hence, Colour depth = 1 bit (i.e. number of bits needed to represent 2 colours).
Working:
Size of image file (in bits) = Image resolution × colour depth = 512 × 300 × 1 bits
Size of image file (in bytes) = (512 × 300 × 1) ÷ 8 = 19 200 bytes
Size of image file (in KiB) = (512 × 300 × 1) ÷ (8 × 1024) = 18.75 KiB (or 19.2 KB approximately)
Working:
Size of image file (in bits) = Image resolution × colour depth = 1920 × 1536 × 16 bits
Size of image file (in bytes) = (1920 × 1536 × 16) ÷ 8 = 5 898 240 bytes (divide by 8, since 1 byte = 8 bits)
Size of image file (in MiB) = (1920 × 1536 × 16) ÷ (8 × 1024 × 1024) = 5.625 MiB (since 1 MiB = 1024 × 1024 bytes)
Working:
Size of each photo (in bits) = Image resolution × colour depth = 1024 × 1536 × 24 bits
Size of each photo (in bytes) = (1024 × 1536 × 24) ÷ 8 bytes (divide by 8, since 1 byte = 8 bits)
16 GiB (in bytes) = 16 × 1024 × 1024 × 1024 bytes
Number of photos on 16 GiB = (16 GiB memory in bytes) ÷ (Size of one photo in bytes)
= (16 × 1024 × 1024 × 1024 × 8) ÷ (1024 × 1536 × 24) = 3640.89
Working:
Bit rate (in bps) = Sample rate (Hz) × Sample resolution
Bit rate (in kbps) = (44 100 × 16) ÷ 1000 = 705.6 kbps (since 1 kbps = 1000 bits/sec)
File size (in bits) = Sample rate (Hz) × Sample resolution × Length of sound (in sec)
File size (in MiB) = (44 100 × 16 × 5 × 60) ÷ (8 × 1024 × 1024) = 25.23 MiB
(a) Size of one sample, in bits:
Size of one sample (in bits) = Sample rate (Hz) × Sample resolution × Length of audio (in sec)
= 44 100 × 8 × 30 = 10 584 000 bits
(b) Size of the audio recording in MiB (stereo = 2 channels):
File size of stereo recording (in bits) = Size of one sample (in bits) × 2 Channels
= 10 584 000 × 2 = 21 168 000 bits
File size of stereo recording (in MiB) = 21 168 000 ÷ (8 × 1024 × 1024) = 2.52 MiB
Working:
Size of one sample (in bits) = Sample rate (Hz) × Sample resolution × Length of audio (in sec)
= 44 100 × 16 × (3 × 60 + 30) = 44 100 × 16 × 210 = 148 176 000 bits
File size of stereo (in bits) = Size of one sample (in bits) × 2 Channels
= 148 176 000 × 2 = 296 352 000 bits
File size of stereo (in bytes) = 296 352 000 ÷ 8 = 37 044 000 bytes
Number of songs on 740 MiB = (740 MiB in bytes) ÷ (Size of a stereo in bytes)
= (740 × 1024 × 1024) ÷ 37 044 000 = 20.95
Lossless Compression
(a) Data / File Compression:
File Compression is the process of encoding data more efficiently to achieve a reduction in file size.
It helps to reduce resource usage, such as data storage space or transmission capacity.
Compressed data has to be decompressed while using.
(b) Why is it necessary to compress files?
- It helps to store more data in less storage space.
- It helps to transmit large amounts of data with fewer bits.
- It minimizes the download and upload time.
(a) How Lossless compression reduces the file size:
- Lossless compression reduces file size by identifying and replacing repeated data bits or words with shorter codes.
- No information is lost in lossless compression.
- The original data is completely recovered when decompressed.
- It is used to compress files which cannot afford any loss of data, like documents, databases, program codes, etc.
(b) Compressing program file, text or document files:
- Program files use lossless compression to reduce their size, by storing repeated words in a table and replacing them with its index or numerical value.
- It identifies and replaces the repeated phrases with shorter codes generated by a complex algorithm.
- The original data is completely recovered when the file is uncompressed before using.
"COMPARE TEXT FILES IN A COMPUTER AFTER FILE COMPRESSION"
Method:
- Store the repeated words (combination of characters) in a table with index number, like:
| Index | Word |
|---|---|
| 1 | COMP |
| 2 | FILE |
| 3 | TER |
Replace the words with its index number to transform the sentence into:
"1ARE TEXT 2S IN A 1U3 AF3 21RESSION"
(a) Run Length Encoding (RLE) algorithm:
- Run Length Encoding is a lossless data compression algorithm.
- It reduces the size of a string with consecutive identical data (e.g. repeated characters of text or pixels of an image).
- A repeating character in the string is encoded with two values:
- → the first value represents the number of repetitions or identical data items in the run.
- → the second value represents the code of the repeated data item (ASCII code for characters, pixel detail for an image).
- RLE is only effective where there is a long run of repeated units of data.
(b) RLE on 'a a a a a b b b b c c d d d d':
This string contains 16 characters. If each character requires 1 byte of memory, then this string needs 16 bytes.
Using ASCII code, this string can be coded as follows:
5 97 4 98 2 99 5 100
Where, in each pair, the first values 5, 4, 2 and 5 are number of runs (repetition) and 97, 98, 99 and 100 are the ASCII code for each repeating character.
Assuming each number requires 1-byte of memory, the RLE code will need 8 bytes (4 + 4). This is half the original file size.
(c) RLE on 32-character string:
This string contains 32 characters. If each character requires 1 byte of memory, then this string needs 32 bytes.
A flag value of 255 is used to indicate the repetition of characters, followed by a pair of values — the first value is the number of runs and the second value is the ASCII code of the character.
When the flag is not used, the characters are given only their ASCII code.
Using this algorithm, this string can be coded as follows:
255 8 97 255 10 98 99 100 99 100 99 100 255 8 99
Assuming each number requires 1-byte of memory, the RLE code now requires 15 bytes.
(a) Black and White image (letter 'F' on 8 × 8 grid):
Image: 8 × 8 = 64 pixels showing the letter 'F'.
Resolution: 8 × 8 = 64 pixels
File Size (Uncompressed): 64 bytes (1 byte per pixel)
Method — RLE code (W=White, B=Black):
9W 6B 2W 1B 7W 1B 7W 5B 3W 1B 7W 1B 7W 1B 6W
Binary Representation (W=1, B=0):
91 60 21 10 71 10 71 50 31 10 71 10 71 10 61
(b) Coloured image (8 × 8 with 4 colours — black, white, red, green):
Each pixel is made up of RGB colours (a mixture of three colours red, green and blue), hence each pixel would require 3 bytes of storage.
Original File Size: 8 × 8 × 3 = 192 bytes.
Colour Coding Table (RGB values):
| Pixel Colour | Red | Green | Blue |
|---|---|---|---|
| ■ (Black) | 0 | 0 | 0 |
| □ (White) | 255 | 255 | 255 |
| 🟩 (Green) | 0 | 255 | 0 |
| 🟥 (Red) | 255 | 0 | 0 |
Method — Using RLE, the image is coded row by row:
2 0 0 0 4 0 255 0 3 0 0 6 255 255 255 1 0 0 0 2 0 255 0 4 255 0 0 4 0 255 0
1 255 255 255 2 255 0 0 1 255 255 255 4 0 255 0 4 255 0 0 4 0 255 0 4 255 255 255
2 0 255 0 1 0 0 0 2 255 255 255 2 255 0 0 2 255 255 255 3 0 0 0 4 0 255 0 2 0 0 0
The compressed RLE code has 92 values and therefore needs only 92 bytes to store the image.
Lossy Compression
(a) How Lossy compression reduces the file size:
- Reduces the file size by permanently deleting the duplicate data without which the file could solve its purpose.
- It permanently deletes the data which human cannot interpret.
- It decides which parts of file need to be retained and which parts can be discarded.
- It is not possible to regain the original file after compression.
- It is generally used to compress audio, video and image files.
(b) Lossy compression for Image files:
- Reduces the file size by reducing the image resolution, permanently deleting the duplicate pixels without which the file could solve its purpose (or by reducing the dimension of the image or number of pixels along length and width of image).
- By reducing the colour depth that reduces the number of bits needed to represent each pixel.
- As the compression increases, the quality of the image decreases.
- It is not possible to regain the original file after compression.
(c) Lossy compression for Sound files:
- Reduces the file size by reducing the sound resolution (or bit-depth), the number of bits needed to represent each sample.
- By reducing the sample rate, the number of samples recorded per second.
- As the compression increases, the quality of the sound decreases.
- It is not possible to regain the original file after compression.
(d) Lossy compression for Video files:
- Reduces the file size by reducing the sound resolution and sample rate of the audio.
- Reduces the pixel dimension and colour depth of all the frames.
- Reduces the frame rate of the video.
- Once compressed, the file cannot be regained to its original form.
(e) Reason to choose lossy over lossless compression:
- It produces a much smaller compressed file than lossless method, that still meets the purpose of the file even by permanently deleting some data.
- MP3 file uses lossy compression that reduces its size by about 90%, by permanently removing the sounds that human ear cannot hear.
- It deletes the background noise, retaining only the loud clear sound called perceptual music shaping.
- MP3 algorithm further compresses the file using lossless compression by replacing repeated bits with shorter codes maintaining its quality.
- MP4 file uses lossy compression that reduces its size, by removing the pixel information like colour shades and brightness variations which human eyes cannot interpret from its video frames.
- It deletes the background noise and the sound which human ear cannot hear.
- Only stores the data that have changed from one frame to the next.
- Hence, the removed data will not affect the quality of the video.
- JPEG file uses lossy compression that reduces its size, by removing the pixel information like colour shades and brightness variations which human eyes cannot interpret.
- It is done by separating pixel colour from its brightness, which then allows certain information to be discarded from the image without losing any noticeable image quality.
- JPEG file cannot be reversed to regain its original bitmap image raw data.
(a) Uncompressed Image file formats:
RAW images are images that are unprocessed and uncompressed that have been created by a camera or scanner. There are a lot of different raw formats (like .raw, .cr2, .nef, .orf, .sr2, and more), each camera company often has its own proprietary format.
(b) Lossless compression Image file formats:
- BMP (file types ending in .bmp): BMP stands for Bitmap Image. They are uncompressed, designed to store raw device-independent bitmap images.
- TIFF (file types ending in .tif): TIFF stands for Tagged Image File Format. They are uncompressed and thus contain a lot of detailed image data, commonly used in medical imaging and in photo software.
- PNG (file types ending in .png): PNG stands for Portable Network Graphics. They are lossless compressed, commonly used to store web graphics, digital photographs, and images with transparent backgrounds.
- GIF (file types ending in .gif): GIF stands for Graphic Interchange Format. It has limited 256 colour range, supports transparent background, used for animations. It uses lossless compression suitable for the web.
(c) Lossy compression Image file formats:
- JPEG (file types ending in .jpg): JPEG stands for Joint Photographic Experts Group. They are compressed files using lossy algorithm, mostly used by digital cameras to store more photos in its memory cards and used for photographs on web pages.
- AVIF (file types ending in .avif): AVIF stands for AV1 Image File Format. It is the latest image file format that offers excellent lossy compression algorithm to achieve 50% smaller image size than JPEG images.
| Lossless compression | Lossy compression |
|---|---|
| Reduces the file size by replacing repeated data with shorter codes. | Reduces the file size by permanently deleting the data without which the file could solve its purpose. |
| File can be decompressed to its original state without losing any data. | File cannot be regained to its original form once it is compressed. |
Revision: Statements and Key Computing Terms
| Statement / Definition | Term |
|---|---|
| Reduction of the size of a file by removing repeated or redundant pieces of data; this can be lossy or lossless. | Compression |
| The maximum rate of transfer of data across a network, measured in kilobits per second (Kbps) or megabits (Mbps). | Bandwidth |
| A file compression method that allows the original file to be fully restored during the decompression process, for example, run length encoding (RLE). | Lossless file compression |
| A method used to reduce the size of a sound file using perceptual music shaping. | Audio compression |
| A lossy file compression method used for music files. | MP3 |
| A lossy file compression method used for multimedia files. | MP4 |
| From Joint Photographic Expert Group; a form of lossy file compression used with image files which relies on the inability of the human eye to distinguish certain colour changes and hues. | JPEG |
| A lossless file compression technique used to reduce the size of text and photo files in particular. | Run length encoding (RLE) |