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

📄 lossy_d.c

📁 实现了JPEG-LS的标准算法
💻 C
📖 第 1 页 / 共 2 页
字号:
/* SPMG/JPEG-LS IMPLEMENTATION V.2.1   =====================================   These programs are Copyright (c) University of British Columbia. All rights reserved.   They may be freely redistributed in their entirety provided that this copyright   notice is not removed. THEY MAY NOT BE SOLD FOR PROFIT OR INCORPORATED IN   COMMERCIAL PROGRAMS WITHOUT THE WRITTEN PERMISSION OF THE COPYRIGHT HOLDER.   Each program is provided as is, without any express or implied warranty,   without even the warranty of fitness for a particular purpose.   =========================================================   THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:   =========================================================   LOCO-I/JPEG-LS IMPLEMENTATION V.0.90   -------------------------------------------------------------------------------   (c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999.        HEWLETT-PACKARD COMPANY ("HP") DOES NOT WARRANT THE ACCURACY OR   COMPLETENESS OF THE INFORMATION GIVEN HERE.  ANY USE MADE OF, OR   RELIANCE ON, SUCH INFORMATION IS ENTIRELY AT USER'S OWN RISK.        BY DOWNLOADING THE LOCO-I/JPEG-LS COMPRESSORS/DECOMPRESSORS   ("THE SOFTWARE") YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS   OF THIS LICENSING AGREEMENT.        YOU MAY DOWNLOAD AND USE THE SOFTWARE FOR NON-COMMERCIAL PURPOSES   FREE OF CHARGE OR FURTHER OBLIGATION.  YOU MAY NOT, DIRECTLY OR   INDIRECTLY, DISTRIBUTE THE SOFTWARE FOR A FEE, INCORPORATE THIS   SOFTWARE INTO ANY PRODUCT OFFERED FOR SALE, OR USE THE SOFTWARE   TO PROVIDE A SERVICE FOR WHICH A FEE IS CHARGED.        YOU MAY MAKE COPIES OF THE SOFTWARE AND DISTRIBUTE SUCH COPIES TO   OTHER PERSONS PROVIDED THAT SUCH COPIES ARE ACCOMPANIED BY   HEWLETT-PACKARD'S COPYRIGHT NOTICE AND THIS AGREEMENT AND THAT   SUCH OTHER PERSONS AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT.        THE SOFTWARE IS NOT OF PRODUCT QUALITY AND MAY HAVE ERRORS OR DEFECTS.   THE JPEG-LS STANDARD IS STILL UNDER DEVELOPMENT. THE SOFTWARE IS NOT A   FINAL OR FULL IMPLEMENTATION OF THE STANDARD.  HP GIVES NO EXPRESS OR   IMPLIED WARRANTY OF ANY KIND AND ANY IMPLIED WARRANTIES OF   MERCHANTABILITY AND FITNESS FOR PURPOSE ARE DISCLAIMED.        HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL,   OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE.   -------------------------------------------------------------------------------*//* lossy_d.c --- the main pipeline which processes a scanline by doing *                prediction, context computation, context quantization, *                and statistics gathering. (for LOSSY images) * * Initial code by Alex Jakulin,  Aug. 1995 * * Modified and optimized: Gadiel Seroussi, October 1995 * * Modified and added Restart marker and input tables by: * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999 */#include "global.h"#include "bitio.h"#include <stdio.h>#include <math.h>static int eor_limit;/* Do Golomb-Rice statistics and DECODING for LOSSY images*/inline int lossy_regular_mode_d(int Q, int SIGN, int Px){	int At, Bt, Nt, Errval, absErrval;	int current, k;	/* This function is called only for regular contexts. 	   End_of_run context is treated separately */	Nt = N[Q];	At = A[Q];	/* Estimate k */	{	    register nst = Nt;	    for(k=0; nst < At; nst *=2, k++);	}		/* Get the number of leading zeros */	absErrval = 0;	do {		int temp;		temp = zeroLUT[reg >> 24];		absErrval += temp;		if (temp != 8) {			FILLBUFFER(temp + 1);			break;		}		FILLBUFFER(8);	} while (1);	if ( absErrval < limit ) {		/* now add the binary part of the Rice code */		if (k) {			register unsigned long temp;			absErrval <<= k;			GETBITS(temp,k);			absErrval += temp;		}	}	else {		/* the original unary would have been too long:			(mapped value)-1 was sent verbatim */		GETBITS(absErrval, qbpp);		absErrval ++;	}	/* Do the Rice mapping */	if ( absErrval & 1 ) {        /* negative */		absErrval = (absErrval + 1) / 2;		Errval = -absErrval;	} else {		absErrval /= 2;		Errval = absErrval;	}	Bt = B[Q];	/* if ( k==0 && (2*Bt <= -qmul[Nt]) ) */	if ( k==0 && NEAR==0 && (2*Bt <= -Nt) ) 	{	/* special case: see encoder side */		Errval = -(Errval+1);		absErrval = (Errval<0)?(-Errval):Errval;	}	Errval = qmul[Errval];  /* dequantize prediction error */	/* center, clip if necessary, and mask final error */	if ( SIGN == -1 ) {	    Px -= C[Q];	    clip(Px, alpha);	    current = Px - Errval;	}	else {	    Px += C[Q];	    clip(Px,alpha);	    current = Px + Errval;	}	/* first, we reduce mod beta in the range -NEAR <= x <= alpha-1+NEAR,	   then we clip to [0,alpha] */	if (current < negNEAR)		current += beta;	else if (current > alpha1eps)		current -= beta;	clip(current,alpha);	/* update bias stats */	B[Q] = (Bt += Errval);	/* update Golomb-Rice stats */	A[Q] += absErrval;	/* check reset (joint for Rice-Golomb and bias cancelation) */	if(Nt == reset) {		N[Q] = (Nt >>= 1);		A[Q] >>= 1;		B[Q] = (Bt >>= 1);	}	/* Do bias estimation for NEXT pixel */	N[Q] = (++Nt);	if  ( Bt <= -Nt ) {	    if (C[Q] > MIN_C)			--C[Q];	    Bt = (B[Q] += Nt);	    if ( Bt <= -Nt ) 			B[Q] = -Nt+1;	} else if ( Bt > 0 ) {	    if (C[Q] < MAX_C)			++C[Q];	    Bt = (B[Q] -= Nt);	    if ( Bt > 0 )			B[Q] = 0;	}	return current;}/* Do end of run DECODING for LOSSY images */inline pixel lossy_end_of_run_d(pixel Ra, pixel Rb, int RItype){	int xpr,		Ix,		Errval,		absErrval,		MErrval,		k,		Q,		oldmap, 		Nt,		At;	Q = EOR_0 + RItype;	Nt = N[Q], 	At = A[Q];	if ( RItype )		At += Nt/2;	/* Estimate k */	for(k=0; Nt < At; Nt *=2, k++);	/* read and decode the Golomb code */	/* Get the number of leading zeros */	MErrval = 0;	do {		int temp;		temp = zeroLUT[reg >> 24];		MErrval += temp;		if (temp != 8) {			FILLBUFFER(temp + 1);			break;		}		FILLBUFFER(8);	} while (1);	eor_limit = limit - limit_reduce;	if ( MErrval < eor_limit ) {		/* now add the binary part of the Golomb code */		if (k) {			register unsigned long temp;			MErrval <<= k;			GETBITS(temp,k);			MErrval += temp;	    }	}	else {	    /* the original unary would have been too long:	       (mapped value)-1 was sent verbatim */	    GETBITS(MErrval, qbpp);	    MErrval ++;	}	oldmap = ( k==0 && (RItype||MErrval) && (2*B[Q]<Nt));	/* 	   Note: the Boolean variable 'oldmap' is not 	   identical to the variable 'map' in the	   JPEG-LS draft. We have       oldmap = (qdiff<0) ? (1-map) : map;	*/	MErrval += ( RItype + oldmap );	if ( MErrval & 1 ) { /* negative */	    Errval = oldmap - (MErrval+1)/2;	    absErrval = -Errval-RItype;	    B[Q]++;	}	else { /* nonnegative */	    Errval = MErrval/2;	    absErrval = Errval-RItype;	}	Errval = qmul[Errval];   /* de-quantize prediction error */	if ( RItype ) {	    Ix = Ra + Errval;	}	else  {	    if ( Rb < Ra )			Ix = Rb - Errval;	    else			Ix = Rb + Errval;	}	if ( Ix < negNEAR )		Ix += beta;	else if ( Ix > alpha1eps )		Ix -= beta;	clip(Ix,alpha);	/* update stats */	A[Q] += absErrval;	if (N[Q] == reset) {	    N[Q] >>= 1;	    A[Q] >>= 1;	    B[Q] >>= 1;	}	N[Q]++;  /* for next pixel */	return Ix;			}/* For DECODING line and plane interleaved mode for LOSSY images*/int lossy_undoscanline(	pixel *psl,			/* previous scanline */						pixel *sl,			/* current scanline */						int no, int color)	/* number of values in it *//*** watch it! actual pixels in the scan line are numbered 1 to no .     pixels with indices < 1 or > no are dummy "border" pixels  */{	int i, psfix;	pixel Ra, Rb, Rc, Rd;	int SIGN;	int cont;	int run_int_type;	psfix = 0;	/**********************************************/	/* Do for all pixels in the row in 8-bit mode */	/**********************************************/	if (bpp16==FALSE)	{		Rc = psl[0];		Rb = psl[1];		Ra = sl[0];		i = 1;		do {			pixel Px;			Rd = psl[i + 1];			/* Quantize the gradient */			cont =  vLUT[0][Rd - Rb + LUTMAX8] +					vLUT[1][Rb - Rc + LUTMAX8] +					vLUT[2][Rc - Ra + LUTMAX8];			if ( cont == 0 ) {				/********** RUN STATE *********/				register int n, m;				/* get length of the run */				/* arg is # of pixels left */				m = n= process_run_dec(no-i+1, color); 				if ( m > 0 )  {  /* run of nonzero length, otherwise									we go directly to the end-of-run 									state */					do {						sl[i++] = Ra;					} while(--n > 0);					if (i > no)						/* end of line */						return 0;					/* update context pixels */					Rb = psl[i];					Rd = psl[i + 1];				}				/* here we handle the "end-of-run" state */				run_int_type = ( (Rb-Ra) <= NEAR && (Rb-Ra) >= negNEAR);				Ra = lossy_end_of_run_d(Ra, Rb, run_int_type);			}			else {				/****** REGULAR CONTEXT ******/				predict(Rb, Ra, Rc);				/* map symmetric contexts */				cont = classmap[cont];				if (cont < 0) 				{					SIGN = -1;					cont = -cont;				}				else					SIGN = +1;				/* decode a Rice code of a given context */				Ra = lossy_regular_mode_d(cont, SIGN, Px);			}			sl[i] = Ra;			Rc = Rb;			Rb = Rd;			++i;		} while (i <= no);	} else	/***********************************************/	/* Do for all pixels in the row in 16-bit mode */	/***********************************************/	{		Rc = ENDIAN16(psl[0]);		Rb = ENDIAN16(psl[1]);

⌨️ 快捷键说明

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