Jessica
Jessica
Compare the file sizes of an uncompressed BMP image and a compressed JPEG image of the
same resolution. Explain why the sizes differ and the impact on quality.
BMP Files: Store raw pixel data without any form of compression. Each pixel is
represented by a fixed number of bytes (usually 3 for 24-bit color). Therefore, the file
size increases directly with the resolution and color depth.
o Result: BMP files are large, especially for high-resolution images.
JPEG Files: Use lossy compression to remove details that are less noticeable to the
human eye, such as subtle color changes or fine texture details.
o JPEG reduces the file size by discarding data, which lowers the image quality to
some extent. However, it significantly reduces storage requirements.
o Result: JPEG files are much smaller than BMP files.
Impact on Image Quality
BMP: Since BMP files store the exact pixel data, there is no loss of quality. Every
detail is preserved.
o Use case: Ideal for applications requiring the highest quality like image editing
or printing.
JPEG: JPEG’s lossy compression sacrifices image quality to achieve smaller file sizes.
The level of quality loss depends on the compression rate.
o At high compression, the quality loss is noticeable in the form of artifacts like
blurring and pixelation.
o At low compression (high quality), JPEG images maintain a good balance
between file size and quality, often undetectable to the human eye.
Storage Efficiency Poor (large file size) Excellent (small file size)
Editing Flexibility Excellent (since no loss occurs) Poor (quality degrades with each
edit)
LAB 4:
Record an audio clip in WAV format (uncompressed) and in MP3 format (compressed). Analyze
the file size and discuss the trade-off between file size and audio quality.
#include <stdio.h>
#include <string.h>
#define MAX 1000
void rle_compress(char *input, char *output) {
int count, j = 0;
for (int i = 0; input[i] != '\0'; i++) {
count = 1;
while (input[i] == input[i + 1]) {
count++;
i++;
}
output[j++] = input[i];
j += sprintf(output + j, "%d", count);
}
output[j] = '\0';
}
void rle_decompress(char *input, char *output) {
int j = 0, count;
for (int i = 0; input[i] != '\0'; i++) {
char ch = input[i];
count = 0;
while (input[i + 1] >= '0' && input[i + 1] <= '9') {
count = count * 10 + (input[i + 1] - '0');
i++;
}
for (int k = 0; k < count; k++) {
output[j++] = ch;
}
}
output[j] = '\0';
}
int main() {
char input[MAX], compressed[MAX], decompressed[MAX];
printf("Enter a string: ");
scanf("%s", input);
rle_compress(input, compressed);
printf("Compressed: %s\n", compressed);
rle_decompress(compressed, decompressed);
printf("Decompressed: %s\n", decompressed);
return 0;
}
Output:
LAB 9:
WAP to build a Huffman Tree and print the codes.
#include <stdio.h>
#include <stdlib.h>
struct HuffmanNode {
char data;
int freq;
struct HuffmanNode *left, *right;
};
struct MinHeap {
int size;
struct HuffmanNode **array;
};
struct HuffmanNode* createNode(char data, int freq) {
struct HuffmanNode* node = (struct HuffmanNode*)malloc(sizeof(struct HuffmanNode));
node->data = data;
node->freq = freq;
node->left = node->right = NULL;
return node;
}
void swapNodes(struct HuffmanNode** a, struct HuffmanNode** b) {
struct HuffmanNode* temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(struct MinHeap* minHeap, int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != index) {
swapNodes(&minHeap->array[smallest], &minHeap->array[index]);
minHeapify(minHeap, smallest);
}
}
struct HuffmanNode* extractMin(struct MinHeap* minHeap) {
struct HuffmanNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
minHeap->size--;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap* minHeap, struct HuffmanNode* node) {
minHeap->size++;
int i = minHeap->size - 1;
while (i && node->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = node;
}
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) {
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = size;
minHeap->array = (struct HuffmanNode**)malloc(size * sizeof(struct HuffmanNode*));
for (int i = 0; i < size; i++)
minHeap->array[i] = createNode(data[i], freq[i]);
for (int i = (size - 1) / 2; i >= 0; i--)
minHeapify(minHeap, i);
return minHeap;
}
struct HuffmanNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
while (minHeap->size > 1) {
struct HuffmanNode* left = extractMin(minHeap);
struct HuffmanNode* right = extractMin(minHeap);
struct HuffmanNode* top = createNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct HuffmanNode* root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (!root->left && !root->right) {
printf("%c: ", root->data);
for (int i = 0; i < top; i++)
printf("%d", arr[i]);
printf("\n");
}
}
int main() {
int n;
printf("Enter number of characters: ");
scanf("%d", &n);
char data[n];
int freq[n];
printf("Enter characters: ");
for (int i = 0; i < n; i++)
scanf(" %c", &data[i]);
printf("Enter their frequencies: ");
for (int i = 0; i < n; i++)
scanf("%d", &freq[i]);
struct HuffmanNode* root = buildHuffmanTree(data, freq, n);
int arr[100], top = 0;
printf("Huffman Codes:\n");
printCodes(root, arr, top);
return 0;
}
Output: