0% found this document useful (0 votes)
6 views8 pages

Jessica

The document compares the file sizes and quality of BMP and JPEG image formats, highlighting that BMP files are large and uncompressed, preserving image quality, while JPEG files are smaller due to lossy compression, which may result in quality loss. It also discusses WAV and MP3 audio formats, noting that WAV files are uncompressed and large, ideal for high fidelity, whereas MP3 files are compressed, smaller, and suitable for casual listening despite some quality loss. Additionally, the document includes code for Run-Length Encoding and Huffman Tree construction for file compression.

Uploaded by

kripashrestha180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Jessica

The document compares the file sizes and quality of BMP and JPEG image formats, highlighting that BMP files are large and uncompressed, preserving image quality, while JPEG files are smaller due to lossy compression, which may result in quality loss. It also discusses WAV and MP3 audio formats, noting that WAV files are uncompressed and large, ideal for high fidelity, whereas MP3 files are compressed, smaller, and suitable for casual listening despite some quality loss. Additionally, the document includes code for Run-Length Encoding and Huffman Tree construction for file compression.

Uploaded by

kripashrestha180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 3:

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.

Definitions of BMP & JPEG

1. BMP (Bitmap Image File Format)


o BMP is an uncompressed raster image format developed by Microsoft.
o It stores image data pixel by pixel, preserving the exact colors and details of an
image without any compression.
o Due to the lack of compression, BMP files are large in size but maintain high
quality.
o Commonly used for image editing, printing, and archival purposes.

2. JPEG (Joint Photographic Experts Group)


o JPEG is a compressed image format that uses lossy compression to reduce file
size.
o It applies Discrete Cosine Transform (DCT) to remove unnecessary details and
optimize storage.
o JPEG images have smaller file sizes but may suffer from quality loss due to
compression artifacts.
o Widely used for photographs, web images, and digital sharing due to its
efficiency.

Why Do BMP and JPEG Have Different File Sizes?

 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.

Comparison of File Sizes and Quality


Feature BMP (Uncompressed) JPEG (Compressed)

File Size Very large (~6 MB for Much smaller (~50 KB to 2 MB


1920×1080) for same size)

Compression Type None (Uncompressed) Lossy Compression

Image Quality No loss (Perfect quality) Lossy (Depends on compression


level)

Best For Image editing, archiving, Web, photography, and sharing


professional use

Compression None Yes (e.g., blockiness, blurring,


Artifacts noise)

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.

WAV Format (Uncompressed)


 File Size: 229 MB (for a larger, high-quality recording, such as 1 hour of audio at standard CD
quality: 44.1 kHz, 16-bit stereo).
 Audio Quality:
o WAV is uncompressed, preserving full audio data with no loss in quality. This makes it
ideal for situations that require maximum fidelity, such as professional music
production, sound editing, or archiving.
o There are no compression artifacts in WAV files, making them an exact replica of the
original recording.
 Trade-Off:
o The primary downside is the large file size, which can make storing and sharing long
recordings cumbersome, especially when you have limited storage space or bandwidth.
o Best Use: Professional audio work, editing, and archiving, where the focus is on
preserving audio quality, not file size.

MP3 Format (Compressed)


 File Size: 20.7 MB (for the same 1-hour audio at a standard bitrate of 128 kbps).
 Audio Quality:
o MP3 uses lossy compression, meaning it removes some audio data to reduce file size.
o The degree of quality loss is affected by the bitrate:
 128 kbps: Noticeable quality reduction, particularly in high frequencies and
complex audio.
 256 kbps or 320 kbps: Higher quality, but still not as perfect as WAV. The quality
loss becomes less noticeable at these bitrates.
 Trade-Off:
o Advantage: MP3 files are much smaller in size, making them perfect for storage,
distribution, and streaming over the internet. They are ideal for general listening where
file size is more important than the absolute best sound quality.
o Disadvantage: The lossy nature means some audio details are lost, and this can become
apparent when listening on high-end audio systems or in environments that demand
clarity.
o Best Use: Casual listening, sharing, and streaming where smaller file sizes are crucial,
and the loss of quality is acceptable.
LAB 8
WAP for File Compression using Run-Length Encoding.

#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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy