⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decode.cpp

📁 一种无损图象压缩算法,可用于珍贵图象的保存.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if (temp > UPPER)  temp = UPPER; 
	else if (temp < 0) temp = 0;
	k = nh (aerr + 2*ABS (ww - w) + 2*ABS (w - nw));
	E0[width] = err = nreremap (decode_symbol (k), temp, 0);
	aerr      = ABS (err);
	curr = temp + err;
	//NEARLY LOSSLESS COMPRESSION
	#ifdef OPTION_NEARLY_LOSSLESS
	{
		if (curr < 0)     curr = 0;
		if (curr > UPPER) curr = UPPER;
	}
	#endif
	//NEARLY LOSSLESS COMPRESSION
	u1[width] = curr;
	u1[width+1] = u1[width];    /* Duplicate the last  pixel */
	Dh[width] = ABS (curr - w);
	Dv[width] = ABS (curr - n);

	//From 3rd row process each row
	for (j = 2; j < height; ++j)
	{
		//First pixel of each row
		nn   = u2[1];
		n    = u1[1];
		ne   = u1[2];
		temp = n + (ne - nn)/4;
		if (temp > UPPER)  temp = UPPER; 
		else if (temp < 0) temp = 0;
		k = nh (2*ABS (n - ne) + 2*ABS (n - nn));
		
		err  = nreremap (decode_symbol (k), temp, 0);
		aerr = ABS (err);
		curr = temp + err;
		//NEARLY LOSSLESS COMPRESSION
		#ifdef OPTION_NEARLY_LOSSLESS
		{
			if (curr < 0)     curr = 0;
			if (curr > UPPER) curr = UPPER;
		}
		#endif
		//NEARLY LOSSLESS COMPRESSION
		u0[1] = curr;
		u0[0] = u0[1];        /* Duplicate the first pixel */
		//Second pixel of each row
		nn   = u2[2];
		nne  = u2[3];
		nw   = u1[1];
		n    = u1[2];
		ne   = u1[3];
		w    = u0[1];
		temp = (w + n + 1)/2 + (ne - nw)/4;
		if (temp > UPPER)  temp = UPPER; 
		else if (temp < 0) temp = 0;
		k = nh (aerr + ABS (n - ne) + ABS (n - nw) + ABS (n - nn) + ABS (ne - nne) + ABS (w - nw));
		E0[1] = err;
		
		err   = nreremap (decode_symbol (k), temp, 0);
		aerr  = ABS (err);
		curr = temp + err;
		//NEARLY LOSSLESS COMPRESSION
		#ifdef OPTION_NEARLY_LOSSLESS
		{
			if (curr < 0)     curr = 0;
			if (curr > UPPER) curr = UPPER;
		}
		#endif
		//NEARLY LOSSLESS COMPRESSION
		u0[2] = curr;
		//Rest of the row
		for (x = 3; x <= width - 1; ++x)
		{
			nw   = u1[x-1];
			n    = u1[x];
			ne   = u1[x+1];
			ww   = u0[x-2];
			w    = u0[x-1];
			symb1 = symb2 = w;
			Dv[x-1] = ABS (symb1 - nw);
			Dh[x-1] = ABS (symb1 - ww);
			if (ne != symb1) symb2 = ne;
			else if (n  != symb1) symb2 = n;
			else if (nw != symb1) symb2 = nw;
			else if (ww != symb1) symb2 = ww;
			bflag = 0;

			if (symb1 == symb2) /* only one symbol */
			{
				bflag = 1;
				//ZN:
				tolerance=QT[0];
				mask=QS[0];
				//	
				berr = bdecode_symbol (0);
				if (berr == 0)
				{ 
					E0[x-1] = err;
					aerr    = err = 0;
					curr = symb1;
					u0[x] = curr;
				}
				else 
				{
					/* only TWO symbols: symb1 and esc */
					temp = symb1;
					E0[x-1] = err;
					err     = nreremap (decode_symbol (N_SEQ-2), temp, 0);
					aerr    = ABS (err);
					curr = temp + err;
					//NEARLY LOSSLESS COMPRESSION 
					#ifdef OPTION_NEARLY_LOSSLESS
					{
						if (curr < 0)     curr = 0;
						if (curr > UPPER) curr = UPPER;
					}
					#endif
					//NEARLY LOSSLESS COMPRESSION
					u0[x] = curr;
				}
			}
			else
			{
				nn  = u2[x];
				nne = u2[x+1];
				if ((nne != symb1) && (nne != symb2)) bflag = 0;
				else if ((nn != symb1) && (nn != symb2)) bflag = 0;
				else if ((ne != symb1) && (ne != symb2)) bflag = 0;
				else if ((n  != symb1) && (n  != symb2)) bflag = 0;
				else if ((nw != symb1) && (nw != symb2)) bflag = 0;
				else if ((ww != symb1) && (ww != symb2)) bflag = 0;
				else bflag = 2; /* two symbols */
			}
			if (bflag == 2)
			{
				//ZN:
				tolerance=QT[0];
				mask=QS[0];
				//	
				if (ww == symb1) ptn = 1; else ptn = 0;
				if (nw == symb1) ptn |= 02;
				if (n  == symb1) ptn |= 04;
				if (ne == symb1) ptn |= 010;
				if (nn == symb1) ptn |= 020;
				if (symb1 < symb2) ptn |= 040;
				/* pass out symb1, symb2, esc */
				berr = bdecode_symbol (ptn);
				if (berr == 0)
				{ 
					E0[x-1] = err;
					aerr    = err = 0;
					curr = symb1;
					u0[x] = curr;
				}
				else if (berr == 1)
				{ 
					E0[x-1] = err;
					aerr    = err = 0;
					curr = symb2;
					u0[x] = curr;
				}
				else
				{
					temp = symb1;
					E0[x-1] = err;
					err     = nreremap (decode_symbol (N_SEQ-1), temp, 0);
					aerr    = ABS (err);
					curr = temp + err;
					//NEARLY LOSSLESS COMPRESSION
					#ifdef OPTION_NEARLY_LOSSLESS
					{
						if (curr < 0)     curr = 0;
						if (curr > UPPER) curr = UPPER;
					}
					#endif
					//NEARLY LOSSLESS COMPRESSION
					u0[x] = curr;
				}
			}
			if (bflag == 0)
			{
				nnww = u2[x-2];
				nnw  = u2[x-1];
				nww  = u1[x-2];
				nee  = u1[x+2];
				www  = u0[x-3];
				dv = Dv[x-1] + Dv[x] + Dv[x+1]/2;
				dh = Dh[x-1] + Dh[x] + Dh[x+1];
				k = nh (dv + dh + aerr + ABS (E0[x]) + (ABS (E0[x-1]) + ABS (E0[x+1]))/2);
				/* This computation can be made incremental to reduce running time */
				min = max = w;
				if (n  < min)      min = n;
				else if (n  > max) max = n;
				if (nw < min)      min = nw;
				else if (nw > max) max = nw;
				if (ne < min)      min = ne;
				else if (ne > max) max = ne;
				
				int tempmed,tempgap;
					symb1 = dv - dh;
					temp = (w + n)/2 + (ne - nw)/4; 
					if (symb1 > 32)       temp = (temp + w)/2;
					else if (symb1 > 8)   temp = (temp + (temp << 1) + w)/4;
					else if (symb1 < -31) temp = (temp + n)/2;
					else if (symb1 < -8) temp = (temp + (temp << 1) + n)/4;
				tempgap = temp;
					if (w < n) { symb1 = w; symb2 = n; }
					else       { symb1 = n; symb2 = w; }
					if (nw >= symb2)      temp = symb1;
					else if (nw <= symb1) temp = symb2;
					else temp = w + n - nw;
				tempmed = temp;
				if ( max-min<100 || Pr[x-1]+Pr[x]+Pr[x+1]<2 )
					 temp = tempgap;
				else temp = tempmed;

				if (w  < temp) ptn0  = 01; else ptn0 = 0;
				if (n  < temp) ptn0 |= 02;
				if (nw < temp) ptn0 |= 04;
				if (ne < temp) ptn0 |= 010;
				if (nn < temp) ptn0 |= 020;
				if (ww < temp) ptn0 |= 040;
				if (2*w-ww < temp) ptn0 |= 0100;
				if (2*n-nn < temp) ptn0 |= 0200;
				ptn0 |= ((k/2) << 8);
				if (nww < temp) ptn1 = ptn0 | 02000; else ptn1 = ptn0;
				if (nnw < temp) ptn1 |= 04000; 
				if (nee < temp) ptn2 = ptn0 | 02000; else ptn2 = ptn0;
				if (nne < temp) ptn2 |= 04000;

				//lw
				symb1 = symb2 = 0;
				if (N1[ptn1] > 3)
				{
					symb1 += N1[ptn1];
					symb2 += S1[ptn1];
				}
				adjust = (symb2/(symb1 + 1));
				temp += adjust;
				if (temp > max)      temp = max;
				else if (temp < min) temp = min;
				
				symb1   = S1[ptn1] + S2[ptn2];
				symb1  += err + E0[x] + E0[x-1] + E0[x+1];
				E0[x-1] = err;
					
				//ZN:
				tolerance=QT[k];
				mask=QS[k];
				if (symb1 < 0)
				{
					err  = -nreremap (decode_symbol (k), temp, 1);
					aerr = ABS (err);
				}
				else
				{
					err  = nreremap (decode_symbol (k), temp, 0);
					aerr = ABS (err);
				}
				curr = temp + err;

				//NEARLY LOSSLESS COMPRESSION
				#ifdef OPTION_NEARLY_LOSSLESS
				{
					if (curr < 0)     curr = 0;
					if (curr > UPPER) curr = UPPER;
				}
				#endif
				//NEARLY LOSSLESS COMPRESSION
				
				u0[x] = curr;
				if (ABS (err) < (16*((k >> 1) + 1)))
				{
					if (N1[ptn1] == 63)
					{
						if (ABS (S1[ptn1]) > 128)
						{
							S1[ptn1] = ((S1[ptn1] + err)/4);
							N1[ptn1] = 16;
						}
						else 
						{
							S1[ptn1] = ((S1[ptn1] + err)/2);
							N1[ptn1] = 32;
						}
					}
					else
					{
						S1[ptn1] += err;
						N1[ptn1]++;
					}
					if (N2[ptn2] == 63) 
					{
						if (ABS (S2[ptn2]) > 128)
						{
							S2[ptn2] = ((S2[ptn2] + err)/4);
							N2[ptn2] = 16;
						}
						else
						{
							S2[ptn2] = ((S2[ptn2] + err)/2);
							N2[ptn2] = 32;
						}
					}
					else
					{
						S2[ptn2] += err;
						N2[ptn2]++;
					}
				}
			}//FLAG
		}//X

		//Last pixel
		nn   = u2[width];
		nw   = u1[width-1];
		n    = u1[width];
		ww   = u0[width-2];
		w    = u0[width-1];
		Dh[width-1] = ABS (w - ww);
		Dv[width-1] = ABS (w - nw);
		temp = (w + n + 1)/2;
		if (temp > UPPER)  temp = UPPER; 
		else if (temp < 0) temp = 0;
		k = nh (aerr + ABS (n - nw) + ABS (n - nn) + ABS (w - ww) + ABS (w - nw));
		E0[width-1] = err;
		err         = nreremap (decode_symbol (k), temp, 0);
		aerr        = ABS (err);
		curr = temp + err;
		//NEARLY LOSSLESS COMPRESSION 
		#ifdef OPTION_NEARLY_LOSSLESS
		{
			if (curr < 0)     curr = 0;
			if (curr > UPPER) curr = UPPER;
		}
		#endif
		//NEARLY LOSSLESS COMPRESSION 
		
		u0[width] = curr;
		u0[width+1] = u0[width];    /* Duplicate the last  pixel */
		Dh[width] = ABS (curr - w);
		Dv[width] = ABS (curr - n);
		E0[width] = err;
		
		//Update input buffer here
		if (j < (height - 1)) update_buffer (&u0, &u1, &u2, width);
	}
	flush_buffer (u0, u1, u2, width);
	//Free the memory
	free (N1);  free (S1);
	free (N2);  free (S2);
	free (Dh);  free (Dv);
	free (Pr);
	free (E0);
}

//Function : main 
void main (int argc, char *argv[])
{
	unsigned char *line_curr;
	unsigned char *line_1_up;
	unsigned char *line_2_up;
	int width, height, depth;
	//Check if the number of arguments is correct
	if (argc != 7)
	{
		fprintf (stderr, "USAGE : %s InputFile OutputFile Width Height Depth\n", argv[0]);
		exit    (1);
	}
	//Determine the dimension of the input file
	width  = atoi (argv[3]);
	height = atoi (argv[4]);
	if ((depth = atoi (argv[5])) != 8)
	{
		fprintf (stderr, "ERROR : the input must be an 8-bits image!\n");
		exit    (1);
	}
	tolerance = atoi (argv[6]);
	mask = (tolerance<<1) + 1;
	infile=fopen(argv[1],"rb");
	outfile=fopen(argv[2],"wb");
	for(int i=0;i<=7;i++)
	{
		QT[i]=tolerance;
		QS[i]=mask;
	}
	/*	
	if(tolerance>3)
	{
		QT[0]=7;
		QS[0]=15;
		QT[1]=7;
		QS[1]=15;
	}*/
	//Process the input file
	init_bdecoder  ();
	init_sdecoder  ();
	init_mydecoder ();
	create_buffer  (&line_curr, &line_1_up, &line_2_up, width);
	decode_buffer  (line_curr, line_1_up, line_2_up, width, height);
	destory_buffer (&line_curr, &line_1_up, &line_2_up);
	stop_mydecoder ();
	fclose(infile);
	fclose(outfile);
	printf("Finish Decoding!\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -