2024-12-24 18:40:18 +00:00
|
|
|
|
#include "compressor.h"
|
2024-12-24 15:08:56 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// --- Алгоритм Run-Length Encoding (RLE) ---
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
int compress_2(const char* input_file_name, const char* output_file_name) {
|
|
|
|
|
FILE *infile = fopen(input_file_name, "rb");
|
|
|
|
|
if (!infile) {
|
|
|
|
|
perror("Error opening input file");
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
FILE *outfile = fopen(output_file_name, "wb");
|
|
|
|
|
if (!outfile) {
|
|
|
|
|
perror("Error opening output file");
|
|
|
|
|
fclose(infile);
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
unsigned char current_byte, previous_byte;
|
|
|
|
|
size_t count = 0;
|
2024-12-24 18:41:40 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Читаємо перший байт
|
|
|
|
|
if (fread(&previous_byte, 1, 1, infile) != 1) {
|
|
|
|
|
fclose(infile);
|
|
|
|
|
fclose(outfile);
|
|
|
|
|
return 0; // Порожній файл
|
|
|
|
|
}
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
count = 1; // Ініціалізуємо лічильник
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Читаємо залишок файлу
|
|
|
|
|
while (fread(¤t_byte, 1, 1, infile) == 1) {
|
|
|
|
|
if (current_byte == previous_byte && count < 255) {
|
|
|
|
|
count++; // Збільшуємо лічильник
|
|
|
|
|
} else {
|
|
|
|
|
// Записуємо попередній символ і його кількість
|
|
|
|
|
fwrite(&previous_byte, 1, 1, outfile);
|
|
|
|
|
fwrite(&count, 1, 1, outfile);
|
|
|
|
|
previous_byte = current_byte;
|
|
|
|
|
count = 1;
|
|
|
|
|
}
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Записуємо останній символ
|
|
|
|
|
fwrite(&previous_byte, 1, 1, outfile);
|
|
|
|
|
fwrite(&count, 1, 1, outfile);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
fclose(infile);
|
|
|
|
|
fclose(outfile);
|
|
|
|
|
|
|
|
|
|
return 1; // Повертаємо успішний результат
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
int decompress_2(const char* input_file_name, const char* output_file_name) {
|
|
|
|
|
FILE *infile = fopen(input_file_name, "rb");
|
|
|
|
|
if (!infile) {
|
|
|
|
|
perror("Error opening input file");
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
FILE *outfile = fopen(output_file_name, "wb");
|
|
|
|
|
if (!outfile) {
|
|
|
|
|
perror("Error opening output file");
|
|
|
|
|
fclose(infile);
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
unsigned char current_byte;
|
|
|
|
|
size_t count;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Декомпресія файлу
|
|
|
|
|
while (fread(¤t_byte, 1, 1, infile) == 1) {
|
|
|
|
|
fread(&count, 1, 1, infile);
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
|
fwrite(¤t_byte, 1, 1, outfile);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
fclose(infile);
|
|
|
|
|
fclose(outfile);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
return 1; // Повертаємо успішний результат
|
|
|
|
|
}
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// --- Алгоритм Хаффмана (Huffman Coding) ---
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
typedef struct Node {
|
2024-12-24 18:40:18 +00:00
|
|
|
|
unsigned char symbol;
|
|
|
|
|
size_t frequency;
|
|
|
|
|
struct Node *left, *right;
|
|
|
|
|
} Node;
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Структура для кодів Хаффмана
|
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned char symbol;
|
|
|
|
|
char *code;
|
|
|
|
|
} HuffmanCode;
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Функція для порівняння вузлів для використання в черзі
|
|
|
|
|
int compare_nodes(const void *a, const void *b) {
|
2024-12-24 18:41:40 +00:00
|
|
|
|
return ((Node*)a)->frequency - ((Node*)b)->frequency;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
// Створення дерева Хаффмана
|
2024-12-24 18:41:40 +00:00
|
|
|
|
Node* create_huffman_tree(unsigned char *data, size_t size) {
|
|
|
|
|
size_t freq[256] = {0};
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
|
freq[data[i]]++;
|
|
|
|
|
}
|
2024-12-24 18:40:18 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Створюємо список вузлів
|
|
|
|
|
Node *nodes[256];
|
|
|
|
|
size_t node_count = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
|
if (freq[i] > 0) {
|
|
|
|
|
nodes[node_count] = malloc(sizeof(Node));
|
|
|
|
|
nodes[node_count]->symbol = (unsigned char)i;
|
|
|
|
|
nodes[node_count]->frequency = freq[i];
|
|
|
|
|
nodes[node_count]->left = nodes[node_count]->right = NULL;
|
|
|
|
|
node_count++;
|
|
|
|
|
}
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Сортуємо вузли по частоті
|
|
|
|
|
qsort(nodes, node_count, sizeof(Node*), compare_nodes);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Побудова дерева
|
|
|
|
|
while (node_count > 1) {
|
|
|
|
|
Node* left = nodes[0];
|
|
|
|
|
Node* right = nodes[1];
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
Node* parent = malloc(sizeof(Node));
|
2024-12-24 18:41:40 +00:00
|
|
|
|
parent->symbol = 0;
|
|
|
|
|
parent->frequency = left->frequency + right->frequency;
|
2024-12-24 18:40:18 +00:00
|
|
|
|
parent->left = left;
|
|
|
|
|
parent->right = right;
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Зміщуємо вузли в черзі
|
|
|
|
|
memmove(nodes, nodes + 2, (node_count - 2) * sizeof(Node*));
|
|
|
|
|
nodes[node_count - 2] = parent;
|
|
|
|
|
node_count--;
|
2024-12-24 18:40:18 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
qsort(nodes, node_count, sizeof(Node*), compare_nodes);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
return nodes[0]; // Повертаємо корінь дерева
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Функція для рекурсивного запису бітових кодів з дерева Хаффмана
|
|
|
|
|
void generate_huffman_codes(Node* root, HuffmanCode* codes, char* current_code, int depth) {
|
|
|
|
|
if (!root) return;
|
2024-12-24 18:40:18 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
if (root->left == NULL && root->right == NULL) {
|
|
|
|
|
current_code[depth] = '\0';
|
|
|
|
|
codes[root->symbol].symbol = root->symbol;
|
|
|
|
|
codes[root->symbol].code = strdup(current_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_code[depth] = '0';
|
|
|
|
|
generate_huffman_codes(root->left, codes, current_code, depth + 1);
|
|
|
|
|
|
|
|
|
|
current_code[depth] = '1';
|
|
|
|
|
generate_huffman_codes(root->right, codes, current_code, depth + 1);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Функція для стиснення за допомогою Хаффмана
|
2024-12-24 18:40:18 +00:00
|
|
|
|
int compress_1(const char* input_file_name, const char* output_file_name) {
|
|
|
|
|
FILE *infile = fopen(input_file_name, "rb");
|
|
|
|
|
if (!infile) {
|
|
|
|
|
perror("Error opening input file");
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:40:18 +00:00
|
|
|
|
fseek(infile, 0, SEEK_END);
|
|
|
|
|
size_t file_size = ftell(infile);
|
|
|
|
|
fseek(infile, 0, SEEK_SET);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
unsigned char* data = malloc(file_size);
|
|
|
|
|
if (!data) {
|
2024-12-24 18:40:18 +00:00
|
|
|
|
fclose(infile);
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
fread(data, 1, file_size, infile);
|
2024-12-24 18:40:18 +00:00
|
|
|
|
fclose(infile);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Створюємо дерево Хаффмана
|
|
|
|
|
Node* root = create_huffman_tree(data, file_size);
|
|
|
|
|
|
|
|
|
|
// Генерація кодів Хаффмана
|
|
|
|
|
HuffmanCode codes[256];
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
|
codes[i].code = NULL;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
char current_code[256];
|
|
|
|
|
generate_huffman_codes(root, codes, current_code, 0);
|
2024-12-24 15:08:56 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Стиснення даних
|
|
|
|
|
FILE *outfile = fopen(output_file_name, "wb");
|
|
|
|
|
if (!outfile) {
|
|
|
|
|
free(data);
|
|
|
|
|
return -1;
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
for (size_t i = 0; i < file_size; i++) {
|
|
|
|
|
unsigned char symbol = data[i];
|
|
|
|
|
const char* code = codes[symbol].code;
|
|
|
|
|
for (size_t j = 0; code[j] != '\0'; j++) {
|
|
|
|
|
// Записуємо біт в файл
|
|
|
|
|
fputc(code[j] == '1' ? 1 : 0, outfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-24 18:40:18 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
fclose(outfile);
|
|
|
|
|
free(data);
|
2024-12-24 18:40:18 +00:00
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
return 1; // Повертаємо успішний результат
|
2024-12-24 18:40:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Функція для декомпресії за допомогою Хаффмана
|
2024-12-24 18:40:18 +00:00
|
|
|
|
int decompress_1(const char* input_file_name, const char* output_file_name) {
|
2024-12-24 18:41:40 +00:00
|
|
|
|
// Декомпресія за допомогою Хаффмана потребує відновлення дерева та розшифровки бітових кодів
|
|
|
|
|
return 0; // Не реалізовано повністю
|
2024-12-24 15:08:56 +00:00
|
|
|
|
}
|