用行程编码压缩图片后,海明码检错、纠错(随机生成一位出错),再用行程编码解压图片? 求海明码代码~
RT..#include <stdio.h>
#include <stdlib.h>
int compress(int argc, char *argv[])
{
FILE *imageFile,*outFile;
int i, data, nextData;
int n = 0;
if ((imageFile = fopen("123.bmp","rb")) == NULL) {
printf("can't open 123.bmp\n");
exit(1);
}
if ((outFile = fopen("123.cod","wb")) == NULL) {
printf("can't open 123.cod\n");
exit(1);
}
while (!feof(imageFile))
{
data = fgetc(imageFile);
n++;
if (n >= 1078) {
n = 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(n % 256, outFile);
//the second one is High byte
n /= 256;
fputc(n, outFile);
n = 0;
//exchange
data = nextData;
}
n++;
}
goto closeFile;
}
fputc((char)data,outFile);
}
closeFile:
if (fclose(imageFile)) exit(1);
if (fclose(outFile)) exit(1);
system("pause");
return 0;
}
int haiming(int argc, char *argv[])
{
}
int decompress(int argc, char *argv[])
{
FILE *imageZipFile,*outFile;
int i, data, nextData, identifer;
int m = 0;
if ((imageZipFile = fopen("123.cod", "rb")) == NULL) {
printf("can't open 123.cod\n");
exit(1);
}
if ((outFile = fopen("Recovery123.bmp", "wb")) == NULL) {
printf("can't open Recovery123.bmp\n");
exit(1);
}
while (!feof(imageZipFile))
{
data = fgetc(imageZipFile);
m++;
if (m >= 1078) {
identifer = data;
while (!feof(imageZipFile)) {
data = fgetc(imageZipFile);
nextData = fgetc(imageZipFile);
m = nextData * 256 + data;
for (i = 0; i < m; 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;
}
int main(){
int compress();
int haiming();
int decompress();
}
[ 本帖最后由 你是我的菜 于 2010-6-6 16:52 编辑 ]