哈夫曼编码的图像压缩C语言
时间: 2023-07-19 20:14:50 浏览: 106
哈夫曼编码实现图像压缩
哈夫曼编码是一种无损的数据压缩算法,通常用于图像、音频、视频等大数据的压缩。下面是一个基于哈夫曼编码的图像压缩的C语言代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_TREE_HT 100
// 哈夫曼树节点结构体
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
// 哈夫曼树结构体
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
// 创建哈夫曼树节点
struct MinHeapNode* newNode(char data, unsigned freq) {
struct MinHeapNode* temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
// 创建哈夫曼树
struct MinHeap* createMinHeap(unsigned capacity) {
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// 交换两个节点
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b) {
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
// 使最小堆保持最小堆性质
void minHeapify(struct MinHeap* minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 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 != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// 判断堆是否只有一个元素
int isSizeOne(struct MinHeap* minHeap) {
return (minHeap->size == 1);
}
// 取出最小频率的节点
struct MinHeapNode* extractMin(struct MinHeap* minHeap) {
struct MinHeapNode* 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 MinHeapNode* minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
// 判断节点是否是叶子节点
int isLeaf(struct MinHeapNode* root) {
return !(root->left) && !(root->right);
}
// 创建并构建哈夫曼树
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i) {
insertMinHeap(minHeap, newNode(data[i], freq[i]));
}
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
// 构建哈夫曼编码表
void buildHuffmanCodes(struct MinHeapNode* root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
buildHuffmanCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
buildHuffmanCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
for (int i = 0; i < top; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
}
// 使用哈夫曼编码压缩图像
void compressImage(char* inputFileName, char* outputFileName) {
FILE* inputFile = fopen(inputFileName, "r");
FILE* outputFile = fopen(outputFileName, "w");
char c;
int freq[256] = {0};
while ((c = fgetc(inputFile)) != EOF) {
freq[(int)c]++;
}
struct MinHeapNode* root = buildHuffmanTree((char*)freq, freq, 256);
int arr[MAX_TREE_HT], top = 0;
buildHuffmanCodes(root, arr, top);
rewind(inputFile);
char buffer = 0;
int pos = 7;
while ((c = fgetc(inputFile)) != EOF) {
int* code = (int*)malloc(MAX_TREE_HT * sizeof(int));
buildHuffmanCodes(root, code, 0);
int i = 0;
while (code[i] != -1) {
buffer |= (code[i++] << pos);
pos--;
if (pos < 0) {
fputc(buffer, outputFile);
buffer = 0;
pos = 7;
}
}
free(code);
}
if (pos != 7) {
fputc(buffer, outputFile);
}
fclose(inputFile);
fclose(outputFile);
}
int main() {
compressImage("input.bmp", "output.bmp");
return 0;
}
```
以上代码实现了图像的哈夫曼编码压缩,可以尝试自己编写解压缩代码以还原图像。
阅读全文