Data storage and compression (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
An image file is stored in a format that uses 24 bits to represent each pixel. The image is 300 pixels wide and 200 pixels high. Calculate the approximate file size of the image in kilobytes (KB), assuming each colour component is stored using 1 byte.
Calculation:
- Total number of pixels: 300 pixels * 200 pixels = 60,000 pixels
- Number of colour components per pixel: 24 bits = 3 bytes
- Total number of bytes required: 60,000 pixels * 3 bytes/pixel = 180,000 bytes
- Convert to kilobytes: 180,000 bytes / 1024 bytes/KB ≈ 175.78 KB
Answer: The approximate file size of the image is 175.78 KB.
2.
Consider a file containing a series of numbers: 2, 4, 8, 16, 32, 64. Calculate the compression ratio if the file is compressed using a method that represents the numbers as follows: "2 4 8 16 32 64; 2; 2; 2; 2; 2". Explain why this compression method achieves a higher compression ratio than simply storing the original numbers.
Original file size: The original file contains 6 numbers, each requiring a certain amount of storage (e.g., 4 bytes if each number is a 32-bit integer). Let's assume each number takes 4 bytes, so the original file size is 6 * 4 = 24 bytes.
Compressed file size: The compressed file contains the string "2 4 8 16 32 64; 2; 2; 2; 2; 2". This consists of 12 characters. Assuming each character takes 1 byte, the compressed file size is 12 bytes.
Compression Ratio: Compression Ratio = (Original File Size / Compressed File Size) = 24 bytes / 12 bytes = 2. This means the compressed file is 1/2 the size of the original file.
Explanation: The compression method achieves a higher compression ratio because it exploits the redundancy in the data. Instead of storing each number individually, it stores the number of times the number repeats (the '2' in this case) and then the number itself. This is more efficient when there are many repetitions of the same value. The original method required storing each number separately, regardless of whether it was a repetition or not. RLE reduces the amount of data needed to represent the same information.
3.
Describe how a run-length encoding (RLE) algorithm works. Provide an example of a file type where RLE is commonly used and explain why it is effective for that type of data.
Run-length encoding (RLE) is a simple lossless data compression technique. It works by replacing consecutive occurrences of the same character with a single instance of the character and a count of how many times it repeats. For example, the string "WWWWWWWWWWWWB" can be compressed to "12W1B".
RLE is commonly used in bitmap images, particularly those with large areas of the same color. Bitmap images often have large sections of identical pixels. By representing these repeating sequences with a count and the value, the file size can be significantly reduced. It's effective because it exploits the redundancy in the data – the repetition of the same pixel values.