1、对一数值序列,如 5555444444466222223333377777/(8*8 图像的 64 个像素的灰度值,图像的灰度级为 8)采用游标编码,首先输出(gk,lk)的序列,然后输出相应的普通二进制码。#include#include#include#includeusing namespace std;int length,width,degree,point_k=0,l1,l2;int bmp5050,point1002; /bmp 是像素序列的存储结构/输入void input()int i,j;printf(“输入图像的行数:“);scanf(“%d“,printf(“输入图像的
2、列数:“);scanf(“%d“,printf(“输入图像的灰度级:“);scanf(“%d“,printf(“输入像素值:n“);for(i=1;i=length;i+)for(j=1;j=width;j+)scanf(“%d“,l1 = ceil(log(degree)/log(2);l2 = ceil(log(length)/log(2);/将十进制数译为 len 长的二进制数void transfer(int x,int len,char binary)int count = 0;while(x!=0)binarycount+ = x%2+48;x = x/2;while(countl
3、en) /译的时候如果不够码长在码的前面补 0binarycount+ = 0;binarycount = 0;/原来存的时候是倒着存的,码写完后再更正过来reverse(binary,binary+strlen(binary);/将二进制数译为十进制int ad_transfer(char binary)int len = strlen(binary),i;int outcome = 0;for(i=1;i=len;i+)outcome += pow(2,len-i)*(binaryi-1-48);return outcome;/根据图像获得数对序列void get_code()int i,
4、front=1,rear=1;for(i=1;i=length;i+)front = rear = 1;while(rear=width)while(bmpirear=bmpirear+1pointpoint_k0 = bmpirear; /灰度赋值给 Point0pointpoint_k1 = rear-front+1; /游程赋值给 Point1point_k+; /记录数对的个数rear+;front = rear; /通过 front 和 rear 的移动来记录游程/译码void decode(char binary)int i,j,k=0,record1,tag=0,flag=0;c
5、har temp100;for(i=0;istrlen(binary);i+)if(i%(l1+l2)=(l1-1)tempk = binaryi;tempk+1 = 0;record1 = ad_transfer(temp);k = 0;else if(i%(l1+l2)=(l1+l2-1)tempk = binaryi;tempk+1 = 0;flag = ad_transfer(temp);for(j=0;j=flag;j+)printf(“%d “,record1);tag += flag+1;if(!(tag%length)printf(“n“);k=0;else tempk+ =
6、binaryi;void main()int i=0;char receive1000=0;input();get_code();printf(“数对序列:n“);for(i;ipoint_k;i+)printf(“(%d,%d)“,pointi0,pointi1);printf(“n 游程编码结果:n“);for(i=0;ipoint_k;i+)char binary1100 = 0;transfer(pointi0,l1,binary1);strcat(receive,binary1);printf(“%s“,binary1);transfer(pointi1-1,l2,binary1);strcat(receive,binary1);printf(“%s“,binary1);printf(“n 其译码结果:n“);decode(receive);