c 16*16图片解码

发布时间:2024年01月11日

解码的是16×16图片,从比特流到Z排序都和验证数据相同。但生成的彩条太离谱。

Jpeg编码已完成,解码不想再完普了,想得出来问题出在Y亮度分量的排序上面。此前学jpeg是想搞一个摄像头压缩程序,可现在搞出来2秒才能编码一帧图片,没有实用价值。查网络主要是余弦转换的浮点运算太多,数学不好不想再伤脑了。转而去学ffmpeg,借用它的库函数来完成信号压缩。


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <math.h>
#define file1 "/home/wjs/Pictures/4.jpeg"
#define PI 3.1415926

#define  pic_width 16
#define  pic_heigth  16     //yuv420p   y:pic_width*pic_heigth    u:v=y/4
                            //8*8块     y:  pic_width*pic_heigth/64   u:v=pic_width*pic_heigth/(4*64)
char ali(char len, char i);
char y_dc(unsigned char len, int bit );
char  y_ac(unsigned char cd, unsigned int i, unsigned char out[2]);
char uv_ac(unsigned char cd, unsigned int i, unsigned char out[2]);
char uv_dc(unsigned char len, int bit );
int zh(int (*i)[2],int t,unsigned char (*out)[64],unsigned char lh[64]);
int jlh(int i[64], unsigned char lhb[64], double o[64]);
int IDCT(double i[64],unsigned char out[64]);

int main(void) {
	
	//-------JPEG通用量化表--------------------------------
	unsigned char lhb0[0x45] = {0xff, 0xdb, 0, 0x43, 0,
		     16, 11, 10, 16, 24, 40, 51, 61,
		12, 12, 14, 19, 26, 58, 60, 55,
		14, 13, 16, 24, 40, 57, 69, 56,
		14, 17, 22, 29, 51, 87, 80, 62,
		18, 22, 37, 56, 68, 109, 103, 77,
		24, 35, 55, 64, 81, 104, 113, 92,
		49, 64, 78, 87, 103, 121, 120, 101,
		72, 92, 95, 98, 112, 100, 103, 99
		
	//	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
		
	};
	
	unsigned char lhb1[0x45] = {0xff, 0xdb, 0, 0x43, 1,
	   17, 18, 24, 47, 99, 99, 99, 99,     //17,18,24,47
		18, 21, 26, 66, 99, 99, 99, 99,     //18,21,26,66,
		24, 26, 56, 99, 99, 99, 99, 99,      //24,26,56
		47, 66, 99, 99, 99, 99, 99, 99,      //47,66,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99
		
	//	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	};

	unsigned char data[] = {
		131 ,251 ,51 ,254 ,154 ,255 ,0 ,227 ,180 ,127 ,102 ,127 ,211 ,95 ,252 ,118 ,173 ,255 ,0 ,194 ,
		11 ,255 ,0 ,81 ,31 ,252 ,131 ,255 ,0 ,215 ,163 ,254 ,16 ,95 ,250 ,136 ,255 ,0 ,228 ,31 ,254 ,
		189 ,31 ,217 ,244 ,191 ,231 ,255 ,0 ,254 ,74 ,31 ,91 ,204 ,127 ,231 ,239 ,224 ,143,0xff,0xd9};

	
	typedef struct {
		unsigned char b1: 1;
		unsigned char b2: 1;
		unsigned char b3: 1;
		unsigned char b4: 1;
		unsigned char b5: 1;
		unsigned char b6: 1;
		unsigned char b7: 1;
		unsigned char b8: 1;
	} BIT;
	BIT bit;

	unsigned char z[5000000] = {};


	int t = 0;
	int tz=0;
	 while(1){
		if ((data[t] == 0xff) && (data[t + 1] == 0xd9)) {
			break;
		}
		memset(&bit, 0, 1);
		memcpy(&bit, &data[t], 1);

		z[tz + 0] = bit.b8;
		z[tz + 1] = bit.b7;
		z[tz + 2] = bit.b6;
		z[tz + 3] = bit.b5;
		z[tz + 4] = bit.b4;
		z[tz + 5] = bit.b3;
		z[tz + 6] = bit.b2;
		 z[tz + 7] = bit.b1;
		if ((data[t] == 0xff) && (data[t + 1] == 0)) {
			t = t + 2;
		} else {
			t++;
		}
		 tz=tz+8;
		 
	}

	int a=0;
//-------	-------------------------
    int o_y[pic_heigth*pic_width][2]={};   //[1]=0的个数  [0]=系数
	int o_u[pic_heigth*pic_width][2]={};
	int o_v[pic_heigth*pic_width][2]={};

	int jb = 0;  //比特流指针
	
//-----------提取Y------------------------------------------
	int ny=0;   //0个数+系数对的个数
	int ycs=0;
	for(int xh=0;xh<4;xh++){            //MCU  中4个Y 1个U  1个V
		
    //---------0Y_DC------------------
	    int ybc=0;
		for ( a = 2; a < 18; a++) {
		
			int ls = 0;
			int b = 0;
			for (b = 0; b < a; b++) {
				ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
			}
			int ws = y_dc(a, ls);
			if (ws >= 0) {
				jb = jb + a;
				int ls = 0;
				for (int b = 0; b < ws; b++) {
					ls = ls + z[jb + b] * (pow(2, (ws - b - 1)));
				}
			//	o_y[ny][0] = ali(ws, ls);
				ybc=ali(ws,ls)+ycs;
				o_y[ny][0]=ybc;
				ycs=ybc;
				o_y[ny][1] = 88;          //代表是DC
				jb = jb + ws;
				ny++;
				break;
			}

			if (a == 17) {
				puts("hfm error");
				exit(-1);
			}
		}
	//-----0Y_AC------------------------------

		while (1) {

			for ( a = 2; a <18; a++) {
                if (a>17) {
					puts("hfm error");
					exit(-1);
				}
				
				int ls = 0;
				int b = 0;
				unsigned char o1[2] = {};
				for (b = 0; b < a; b++) {
					ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
				}
				int ws = y_ac(a, ls, o1);

				if (ws >= 0) {
					jb = jb + a;
					int ls = 0;
					for (int b = 0; b < o1[1]; b++) {
						ls = ls + z[jb + b] * (pow(2, (o1[1] - b - 1)));
					}
					jb = jb + o1[1];
					o_y[ny][1] = o1[0];
					o_y[ny][0] = ali(o1[1], ls);
			
					ny++;
					break;
				}
			
			}
			if ((o_y[ny - 1][0] == 0) && (o_y[ny - 1][1] == 0)) {
				break;
			}
		}

	}
		
	
//++++++++U_DC+++++++++++++++++++++++++++++++++++++++
	int ucs=0;
	int nu=0;	
	int ubc=0;
			for ( a = 2; a < 18; a++) {
				
				int ls = 0;
				int b = 0;
				for (b = 0; b < a; b++) {
					ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
				}
				char ws = uv_dc(a, ls);
				if (ws >= 0) {
					
					jb = jb + a;
					
					int ls = 0;
					
					for (int b = 0; b < ws; b++) {
						ls = ls + z[jb + b] * (pow(2, (ws - b - 1)));
					}
					
					jb=jb+ws;
				//	o_u[nu][0] = ali(ws, ls);
					ubc=ali(ws,ls)+ucs;
					o_u[nu][0]=ubc;
					ucs=ubc;
					o_u[nu][1] = 88;
					nu++;
					
					break;
				}
				
				if (a == 17) {
					puts("hfm error");
					exit(-1);
				}
			}
				//--------U_AC------------------------

				while (1) {
                      
					for ( a = 2; a < 18; a++) {

						unsigned int ls = 0;
					
						unsigned char o1[2] = {};

						for (int b = 0; b < a; b++) {
							ls = ls + z[jb + b] * (pow(2, (a - b-1 )));   //ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
						}

						char ws = uv_ac(a,ls,o1);
						char n0=o1[0];
						char ws1=o1[1];

						if (ws>=0) {
					
				
							jb = jb + a;
							unsigned int ls = 0;

							for (int b = 0; b <ws1; b++) {
								ls = ls + z[jb + b] * (pow(2, (ws1- b - 1)));
							}

							jb = jb + ws1;
							o_u[nu][1] =n0;//ac 中0 的个数
							o_u[nu][0] = ali(ws1, ls);  //ac 系数
				     

							nu++;
							break;
						}
                 //---------------------------------------------------
						if (a == 17) {
							puts("hfm error");
							exit(-1);
						}
					}
                  //------------------------------------------------
					if ((o_u[nu - 1][0] == 0) && (o_u[nu - 1][1] == 0)) {

						break;
					}
				}
   //----------------------V----------------------------------------------
    //--------V_DC--------------------------------------------					
	   	int nv=0;
	    int vcs=0;
	  int vbc=0;
		for ( a = 2; a < 18; a++) {
			
			int ls = 0;
			int b = 0;
			for (b = 0; b < a; b++) {
				ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
			}
			char ws = uv_dc(a, ls);
			if (ws >= 0) {
				
				jb = jb + a;
				
				int ls = 0;
				
				for (int b = 0; b < ws; b++) {
					ls = ls + z[jb + b] * (pow(2, (ws - b - 1)));
				}
				
				jb=jb+ws;
			//	o_v[nv][0] = ali(ws, ls);
				vbc=ali(ws,ls)+vcs;
				o_v[nv][0]=vbc;
				vcs=vbc;
				o_v[nv][1] = 88;
				nv++;
				
				break;
			}
			
			if (a == 17) {
				puts("hfm error");
				exit(-1);
			}
		}
	//--------V_AC------------------------
					
					while (1) {
						
						for ( a = 2; a < 18; a++) {
							
							unsigned int ls = 0;
							
							unsigned char o1[2] = {};
							
							for (int b = 0; b < a; b++) {
								ls = ls + z[jb + b] * (pow(2, (a - b-1 )));   //ls = ls + z[jb + b] * (pow(2, (a - b - 1)));
							}
							
							char ws = uv_ac(a,ls,o1);
							char n0=o1[0];
							char ws1=o1[1];
							
							if (ws>=0) {
								
								
								jb = jb + a;
								unsigned int ls = 0;
								
								for (int b = 0; b <ws1; b++) {
									ls = ls + z[jb + b] * (pow(2, (ws1- b - 1)));
								}
								
								jb = jb + ws1;
								o_v[nv][1] =n0;//ac 中0 的个数
								o_v[nv][0] = ali(ws1, ls);  //ac 系数
								
								
								nv++;
								break;
							}
							//---------------------------------------------------
							if (a == 17) {
								puts("hfm error");
								exit(-1);
							}
						}
						//------------------------------------------------
						if ((o_v[nv - 1][0] == 0) && (o_v[nv - 1][1] == 0)) {
							
							break;
						}		

            	}
   


	//--------------------------
	
	unsigned char y64[pic_heigth*pic_width/64][64]={};
	unsigned char u64[pic_heigth*pic_width/256][64]={};
	unsigned char  v64[pic_heigth*pic_width/256][64]={};

	zh(o_y,ny,y64,lhb0);
	zh(o_u,nu,u64,lhb1);
	zh(o_v,nv,v64,lhb1);
//-+++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	unsigned char yw[pic_heigth * pic_width] = {};
	
	int yt=0;
	for (int y = 0; y < pic_heigth; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width; x = x + 8) { //提取左上角点的水平数据
			
			for (int a = 0; a < 8; a++) {
				for (int b = 0; b < 8; b++) {
					yw[pic_width* (a + y) + (b + x)]=y64[yt][a*8+b];
					
				}
			}
			yt++;
			
		}
		
	}
	unsigned char uw[pic_heigth * pic_width] = {};
	
	int ut=0;
	for (int y = 0; y < pic_heigth/2; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width/2; x = x + 8) { //提取左上角点的水平数据
			
			for (int a = 0; a < 8; a++) {
				for (int b = 0; b < 8; b++) {
					uw[pic_width/2 * (a + y) + (b + x)]=u64[ut][a*8+b];
					
				}
			}
			ut++;
			
		}
		
	}
	unsigned char vw[pic_heigth * pic_width] = {};
	
	int vt=0;
	for (int y = 0; y < pic_heigth/2; y = y + 8) { //提取左上角点的垂直数据
		for (int x = 0; x < pic_width/2; x = x + 8) { //提取左上角点的水平数据
			
			for (int a = 0; a < 8; a++) {
				for (int b = 0; b < 8; b++) {
					vw[pic_width/2 * (a + y) + (b + x)]=v64[vt][a*8+b];
					
				}
			}
			vt++;
			
		}
		
	}
	
	
	FILE *w=fopen("/home/wjs/ok.yuv","w+b");
	fwrite(yw,pic_heigth*pic_width,1,w);
	fwrite(uw,pic_heigth*pic_width/4,1,w);
	fwrite(vw,pic_heigth*pic_width/4,1,w);
	fclose(w);
   

	return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
//--------Z 正向排序-------------------------------

int jzz( int i[64],int out[64]) {
	int zb[64] = {0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6,
		7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47,
		55, 62, 63
	};

	for (int a = 0; a < 64; a++) {
		out[zb[a]]=i[a];
	}

	return 0;
}	
	//---反量化---------------
	
	int jlh(int i[64], unsigned char lhb[64], double o[64]) {
	
		for (int a = 0; a < 64; a++) {
			
			o[a] = round((i[a]) * (lhb[a]));
			
		}
	
		return 0;
	}


//-------z中间格式转8×8块------------------------------------------------
int zh(int (*i)[2],int t,unsigned char (*out)[64],unsigned char lh[64]){
	int o[64]={};
	int lhs[64]={};
	double flh[64]={};
	int n_z=0;   //y(0个数+系数)的数量指针
	int n88=0 ;   //y8*8的总块数
	while(n_z<t){
		int c=0;  //8×8 下标
		//	if((i[n_z][1]!=0)&&(i[n_z][0]!=0)){
		o[c]=i[n_z][0];  //DC
		n_z++;
		c++;
		//    }
		while(1){
			if((i[n_z][1]==0)&&(i[n_z][0]==0)){
				for(int a=c;a<64;a++){
					o[c]=0;
					
				}
				n_z++;
				
				jzz(o,lhs);   //jzz
				jlh(lhs,lh,flh);
				IDCT(flh,out[n88]);
		
				n88++;
				break;
			}
			if((i[n_z][1]==0)&&(i[n_z][0]!=0)){
				o[c]=i[n_z][0];
				c++;
				n_z++;
			}
			if(i[n_z][1]!=0){
				for(int a=0;a<i[n_z][1];a++){
					o[c]=0;
					c++;
				}	
				o[c]=i[n_z][0];
				c++;
				n_z++;
				
			}
		}
	}
	return 0;
}

 //--------IDCT------------------------------	

int IDCT(double i[64],unsigned char out[64]){
	
	double (*p)[8]=(double (*)[8])i;
	
	double s;
	double au;
	double av;
	unsigned char o[8][8]={};
	
	for(int y=0;y<8;y++){
		for(int x=0;x<8;x++){
			
			for(int u=0;u<8;u++){
				for(int v=0;v<8;v++){
					if(u==0){
						au=1.0/sqrt(2);
					}else{
						au=1.0;
					}
					if(v==0){
						av=1.0/sqrt(2);
					}else{
						av=1.0;
					}
					
					s=s+(1.0/4)*au*av*p[u][v]*cos((2*y+1)*u*PI/16)*cos((2*x+1)*v*PI/16);
				}
			}
			
			o[y][x]=s+128;
			s=0;
		}
	}
	memcpy(out,o,64);
	return 0;
}


//------------------------
char ali(char len, char i) {          //ALI
	char o;

	if ((len == 0) ) {
		o = 0;
	}
	if ((len == 1) && (i == 0)) {
		o = -1;
	}
	if ((len == 1) && (i == 1)) {
		o = 1;
	}
	//--------------------------
	if ((i >= pow(2, len - 1)) && (i <= pow(2, len))) {
		o = i;
	}
	if ((i >= 0) && (i < pow(2, len - 1))) {
		o = i - pow(2, len) + 1;
	}
	return o;
}


char y_dc(unsigned char len, int bit ) { //
	if ((len == 2) && (bit == 0b00)) {
		return 0;
	}
	if ((len == 3) && (bit == 0b010)) {
		return 1;
	}
	if ((len == 3) && (bit == 0b011)) {
		return 2;
	}
	if ((len == 3) && (bit == 0b100)) {
		return 3;
	}
	if ((len == 3) && (bit == 0b101)) {
		return 4;
	}
	if ((len == 3) && (bit == 0b110)) {
		return 5;
	}
	if ((len == 4) && (bit == 0b1110)) {
		return 6;
	}
	if ((len == 5) && (bit == 0b11110)) {
		return 7;
	}
	if ((len == 6) && (bit == 0b111110)) {
		return 8;
	}
	if ((len == 7) && (bit == 0b1111110)) {
		return 9;
	}
	if ((len == 8) && (bit == 0b11111110)) {
		return 10;
	}
	if ((len == 9) && (bit == 0b111111110)) {
		return 11;
	} else return -1;
}
//----------------------------------

char  y_ac(unsigned char cd, unsigned int i, unsigned char out[2]) {

	char bb = 1;
	unsigned int i_bit = i;
	unsigned char i_len = cd;

	unsigned char len;
	unsigned char o;

	unsigned char  ws[16] = {0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d};
	unsigned char zh[162] = {0x1, 0x2, 0x3, 0x0, 0x4, 0x11, 0x5, 0x12, 0x21, 0x31, 0x41, 0x6, 0x13, 0x51, 0x61, 0x7,
	                         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
	                         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
	                         0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
	                         0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
	                         0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
	                         0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
	                         0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
	                         0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
	                         0xe3, 0xe4, 0xe5, 0xe6, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
	                         0xf9, 0xfa
	                        };
	unsigned char  cx_ws, cx_b;

	unsigned char hfm[17][0x7e] = {};
	unsigned char *zz = (unsigned char *)hfm;
	for (int a = 0; a < 17* 0x7e; a++) {
		zz[a] = 0xff;
	}

	int t = 0;
	for (int a = 0; a < 16; a++) {
		if (ws[a] == 0) {
			continue;
		}

		for (int b = 0; b < ws[a]; b++) {
			hfm[a + 1][b] = zh[t];
			t++;
		}
	}
//---------------------------------------
	cx_ws = i_len;

	int o_js = 0;
	if (cx_ws == 2) {      //   00-1  01-2
		o_js = 0b00;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 3) {       //100-3
		o_js = 0b100;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 4) {       //1010-0  1011-4  1100-11
		o_js = 0b1010;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 5) {         //11010-5   11011-12  11100 -21
		o_js = 0b11010;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 6) {
		o_js = 0b111010;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 7) {
		o_js = 0b1111000;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 8) {
		o_js = 0b11111000;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 9) {
		o_js = 0b111110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 10) {
		o_js = 0b1111110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 11) {
		o_js = 0b11111110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 12) {
		o_js = 0b111111110100;
		cx_b = i_bit - o_js;


	}
	if (cx_ws == 15) {
		o_js = 0b111111111000000;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 16) {
		o_js = 0b1111111110000010;
		cx_b = i_bit - o_js;

	}

//-----------------------------------------
    
	unsigned char o_zj = hfm[cx_ws][cx_b];
	if (o_zj == 0xff)  bb = -1;

	len = o_zj / 16;
	o = o_zj % 16;
	out[0] = len;
	out[1] = o;
	if ((cx_ws == 4) && ((i_bit - 0b1010) == 0)) {
		out[0] = 0;
		out[1] = 0;
		bb = 0;
	}
	if(cx_ws>16){
		bb=-1;
	}

	return bb;
}

char uv_dc(unsigned char len, int bit ) {
	if ((len == 2) && (bit == 0b00)) {
		return 0;
	}
	if ((len == 2) && (bit == 0b01)) {
		return 1;
	}
	if ((len == 2) && (bit == 0b10)) {
		return 2;
	}
	if ((len == 3) && (bit == 0b110)) {
		return 3;
	}
	if ((len == 4) && (bit == 0b1110)) {
		return 4;
	}
	if ((len == 5) && (bit == 0b11110)) {
		return 5;
	}
	if ((len == 6) && (bit == 0b111110)) {
		return 6;
	}
	if ((len == 7) && (bit == 0b1111110)) {
		return 7;
	}
	if ((len == 8) && (bit == 0b11111110)) {
		return 8;
	}
	if ((len == 9) && (bit == 0b111111110)) {
		return 9;
	}
	if ((len == 10) && (bit == 0b1111111110)) {
		return 10;
	}
	if ((len == 11) && (bit == 0b11111111110)) {
		return 11;
	} else return -1;
}

char uv_ac(unsigned char cd, unsigned int i, unsigned char out[2]) {

	char bb = 1;
	unsigned int i_bit = i;
	unsigned char i_len = cd;

	unsigned char len;
	unsigned char o;

	unsigned char  ws[16] = {0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77};
	unsigned char zh[162 ] = {0x00, 0x1, 0x2, 0x3, 0x11, 0x4, 0x5, 0x21, 0x31, 0x6, 0x12, 0x41, 0x51, 0x7, 0x61, 0x71,
	                          0x13, 0x22, 0x32, 0x81, 0x8, 0x14,  0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x9, 0x23, 0x33, 0x52, 0xf0,
	                          0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
	                          0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
	                          0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
	                          0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
	                          0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
	                          0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
	                          0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
	                          0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	                          0xf9, 0xfa
	                         };

	unsigned char  cx_ws, cx_b;

	unsigned char hfm1[19][0x78] = {};
	unsigned char *zz = (unsigned char *)hfm1;
	for (int a = 0; a < 19 * 0x78; a++) {
		zz[a] = 0xff;
	}
	int t = 0;
	for (int a = 0; a < 16; a++) {

		if (ws[a] == 0) {
			continue;

		}

		for (int b = 0; b < ws[a]; b++) {
			hfm1[a+1][b] = zh[t];
			t++;
		}
	}
//---------------------------------------
	cx_ws = i_len;

	int o_js = 0;
	if (cx_ws == 2) {
		o_js = 0b00;       //00(EOF)      01(1)
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 3) {
		o_js = 0b100;      //100(2)
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 4) {
		o_js = 0b1010;     //1010(3)      1011(11)
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 5) {
		o_js = 0b11000;     //11000(4)  11001(5)  11010(21)  11011(31)
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 6) {     //111000(6)   111001(12)   111010(41)   111011(51)
		o_js = 0b111000;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 7) {     //1111000(7)   1111001(61)    1111010(71)
		o_js = 0b1111000;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 8) {      //11110110(13)   11110111(22)   11111000(32)  11111001 (81)
		o_js = 0b11110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 9) {      //111110100(8)  111110101(14)   111110110(42)    111110111(91)   111111000(a1)    111111001(b1)  111111010(c1)
		o_js = 0b111110100;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 10) {       //1111110110(9)  1111110111(23)   1111111000(33)   1111111001(52)   1111111010(f0)
		o_js = 0b1111110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 11) {       //11111110110(15)    11111110111(62)    11111111000(72)    11111111001(d1)
		o_js = 0b11111110110;
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 12) {
		o_js = 0b111111110100; //111111110100(0a)   111111110101(16)   111111110110 (24)     111111110111(34)
    	cx_b = i_bit - o_js;

	}
	if (cx_ws == 14) {
		o_js = 0b11111111100000;  //11111111100000(e1)
		cx_b = i_bit - o_js;
	}
	if (cx_ws == 15) {
		o_js = 0b111111111000010; //111111111000010 (25)   111111111000011(f1)
		cx_b = i_bit - o_js;

	}
	if (cx_ws == 16) {
		o_js = 0b1111111110001000;
		cx_b = i_bit - o_js;

	}

//-----------------------------------------

	unsigned char o_zj = hfm1[cx_ws][cx_b];

	if (o_zj == 0xff) {
		bb = -1;
	}

	if ((cx_ws == 2) && (cx_b == 0)) {
		out[0] = 0;
		out[1] = 0;
		bb = 0;
	}

	len = o_zj / 16;
	o = o_zj % 16;
	out[0] = len;
	out[1] = o;

	return bb;
}

?

?

?

?

文章来源:https://blog.csdn.net/m0_59802969/article/details/135485484
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。