`
futrueboy
  • 浏览: 83705 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

图像压缩(行程编码)

阅读更多

图像压缩(行程编码)

利用行程编码对图像进行压缩

//进行压缩 对图像进行编码
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	FILE *imageFile,*outFile;
	int i, data, nextData; 
	int cnt = 0;

	if ((imageFile = fopen("Lena.bmp","rb")) == NULL) {
		printf("can't open Lena.bmp\n");
		exit(1);
	}

	if ((outFile = fopen("Lena.cod","wb")) == NULL) {
		printf("can't open Lena.cod\n");
		exit(1);
	}

	while (!feof(imageFile))
	{
		data = fgetc(imageFile);
		cnt++;
		
		if (cnt >= 1078) {
			cnt = 1;
			//first identifer---white pixel or black pixel
			fputc(data, outFile);
			while (!feof(imageFile)) {
				nextData = fgetc(imageFile);
				if (data != nextData) {
					//output two bytes
					//the first one is Low byte
					fputc(cnt % 256, outFile);
					//the second one is High byte
					cnt /= 256;
					fputc(cnt, outFile);
					cnt = 0;
					//exchange
					data = nextData;
				}
				cnt++;
			}
			goto closeFile;
		}

		fputc((char)data,outFile);
	}
closeFile:
	if (fclose(imageFile)) exit(1);
	if (fclose(outFile)) exit(1);

	system("pause");
	return 0;
}

 压缩后文件只有24K

//反编码 恢复图像
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	FILE *imageZipFile,*outFile;
	int i, data, nextData, identifer; 
	int cnt = 0;

	if ((imageZipFile = fopen("Lena.cod", "rb")) == NULL) {
		printf("can't open Lena.cod\n");
		exit(1);
	}

	if ((outFile = fopen("RecoveryLena.bmp", "wb")) == NULL) {
		printf("can't open RecoveryLena.bmp\n");
		exit(1);
	}

	while (!feof(imageZipFile))
	{
		data = fgetc(imageZipFile);
		cnt++;
		
		if (cnt >= 1078) {
			identifer = data;
			while (!feof(imageZipFile)) {
				data = fgetc(imageZipFile);
				nextData = fgetc(imageZipFile);
				cnt = nextData * 256 + data;
				for (i = 0; i < cnt; i++) {
					fputc(identifer, outFile);
				}
				identifer ^= 0x00ff;
			}
			goto closeFile;
		}

		fputc((char)data,outFile);
	}
closeFile:
	if (fclose(imageZipFile)) exit(1);
	if (fclose(outFile)) exit(1);

	system("pause");
	return 0;
}

 恢复后的图像

 

  • 描述: 进行压缩的图像258K
  • 大小: 10.6 KB
  • 描述: 恢复图像 258K
  • 大小: 10.6 KB
  • Lena.rar (19.9 KB)
  • 描述: 相关附件
  • 下载次数: 13
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics