C语言编码实现文件压缩,C语言文件压缩 代码,该如何处理

C语言文件压缩 代码

求压缩文件的C源代码,谢谢了

------解决方案--------------------

lzss的源代码:

#include

#include

#include

#include

#define N 4096 /* size of ring buffer */

#define F 18 /* upper limit for match_length */

#define THRESHOLD 2 /* encode string into position and length

if match_length is greate

r thhan this */

#define NIL N /* index for root of binary search t

rees

*/

unsigned long int

textsize = 0, /* text size counter */

codesize = 0, /* code size counter */

printcount = 0; /* counter for reporting progress every 1K b

ytes

*/

unsigned char

text_buf[N + F - 1]; /* ring buffer of size N,

with extra F-1 bytes to facilitate string comparison

*/

int match_position, match_length, /* of longest match. These a

re

set by the InsertNode() procedure. */

lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right chi

ldre

&

parents -- These constitute binary search trees. */

FILE *infile, *outfile; /* input & output files */

void InitTree(void) /* initialize trees */

{

int i;

/* For i = 0 to N - 1, rson[i] and lson[i] will be the right and

left children of node i. These nodes need not be initialized.

Also, dad[i] is the parent of node i. These are initialized to

NIL (= N), which stands for 'not used. '

For i = 0 to 255, rson[N + i + 1] is the root of the tree

for strings that begin with character i. These are initialized

to NIL. Note there are 256 trees. */

for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;

for (i = 0; i < N; i++) dad[i] = NIL;

}

void InsertNode(int r)

/* Inserts string of length F, text_buf[r..r+F-1], into one of the

trees (text_buf[r] 'th tree) and returns the longest-match positio

n

and length via the global variables match_position and

match_length.

If match_length = F, then removes the old node in favor of the ne

w

one, because the old one will be deleted sooner.

Note r plays double role, as tree node and position in buffer. */

{

int i, p, cmp;

unsigned char *key;

cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];

rson[r] = lson[r] = NIL; match_length = 0;

for ( ; ; ) {

if (cmp > = 0) {

if (rson[p] != NIL) p = rson[p];

else { rson[p] = r; dad[r] = p; return; }

} else {

if (lson[p] != NIL) p = lson[p];

else { lson[p] = r; dad[r] = p; return; }

}

for (i = 1; i < F; i++)

if ((cmp = key[i] - text_buf[p + i]) != 0) break;

if (i > match_length) {

match_position = p;

if ((match_length = i) > = F) break;

}

}

dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];

dad[lson[p]] = r; dad[rson[p]] = r;

if (rson[dad[p]] == p) rson[dad[p]] = r;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的使用C语言实现bmp文件压缩的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define BMP_HEADER_SIZE 54 typedef struct { unsigned char r, g, b; } RGB; int main(int argc, char **argv) { if (argc != 3) { printf("Usage: %s input.bmp output.bmp\n", argv[0]); exit(1); } char *input_filename = argv[1]; char *output_filename = argv[2]; FILE *input_fp = fopen(input_filename, "rb"); if (input_fp == NULL) { printf("Failed to open input file: %s\n", input_filename); exit(1); } FILE *output_fp = fopen(output_filename, "wb"); if (output_fp == NULL) { printf("Failed to open output file: %s\n", output_filename); fclose(input_fp); exit(1); } // 读取bmp文件头 unsigned char bmp_header[BMP_HEADER_SIZE]; fread(bmp_header, 1, BMP_HEADER_SIZE, input_fp); // 获取图像宽度和高度 int width = *(int *)(bmp_header + 18); int height = *(int *)(bmp_header + 22); // 计算图像数据大小 int row_size = width * 3; int padding_size = 0; while ((row_size + padding_size) % 4 != 0) { padding_size++; } int data_size = (row_size + padding_size) * height; // 读取图像数据 unsigned char *data = (unsigned char *)malloc(data_size); fread(data, 1, data_size, input_fp); // 压缩图像数据 int compressed_size = data_size / 2; unsigned char *compressed_data = (unsigned char *)malloc(compressed_size); int i, j; for (i = 0, j = 0; i < data_size; i += 6, j += 3) { RGB p1 = { data[i], data[i+1], data[i+2] }; RGB p2 = { data[i+3], data[i+4], data[i+5] }; RGB avg = { (p1.r + p2.r) / 2, (p1.g + p2.g) / 2, (p1.b + p2.b) / 2 }; compressed_data[j] = avg.r; compressed_data[j+1] = avg.g; compressed_data[j+2] = avg.b; } // 写入压缩后的bmp文件头 unsigned char compressed_bmp_header[BMP_HEADER_SIZE]; memcpy(compressed_bmp_header, bmp_header, BMP_HEADER_SIZE); *(int *)(compressed_bmp_header + 2) = BMP_HEADER_SIZE + compressed_size; *(int *)(compressed_bmp_header + 18) = width / 2; *(int *)(compressed_bmp_header + 22) = height; *(int *)(compressed_bmp_header + 34) = compressed_size; fwrite(compressed_bmp_header, 1, BMP_HEADER_SIZE, output_fp); // 写入压缩后的图像数据 fwrite(compressed_data, 1, compressed_size, output_fp); fclose(input_fp); fclose(output_fp); free(data); free(compressed_data); printf("Compression completed.\n"); return 0; } ``` 这段代码实现了读取bmp文件压缩图像数据、写入压缩后的bmp文件。具体来说,它首先读取bmp文件头,获取图像的宽度和高度,然后计算图像数据大小,读取图像数据,对图像数据进行压缩,写入压缩后的bmp文件头和压缩后的图像数据,最后释放内存并结束程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值