for(j=0; j<filterSize; j++)
for(j=0; j<*outFilterSize; j++)
static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,			      int srcW, int dstW, int filterAlign, int one, int flags,			      SwsVector *srcFilter, SwsVector *dstFilter){	int i;	int filterSize;	int filter2Size;	int minFilterSize;	double *filter=NULL;	double *filter2=NULL;#ifdef ARCH_X86	if(gCpuCaps.hasMMX)		asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)#endif	// Note the +1 is for the MMXscaler which reads over the end	*filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));	if(ABS(xInc - 0x10000) <10) // unscaled	{		int i;		filterSize= 1;		filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);		for(i=0; i<dstW*filterSize; i++) filter[i]=0;		for(i=0; i<dstW; i++)		{			filter[i*filterSize]=1;			(*filterPos)[i]=i;		}	}	else if(flags&SWS_POINT) // lame looking point sampling mode	{		int i;		int xDstInSrc;		filterSize= 1;		filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);				xDstInSrc= xInc/2 - 0x8000;		for(i=0; i<dstW; i++)		{			int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;			(*filterPos)[i]= xx;			filter[i]= 1.0;			xDstInSrc+= xInc;		}	}	else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale	{		int i;		int xDstInSrc;		if     (flags&SWS_BICUBIC) filterSize= 4;		else if(flags&SWS_X      ) filterSize= 4;		else			   filterSize= 2; // SWS_BILINEAR / SWS_AREA 		filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);		xDstInSrc= xInc/2 - 0x8000;		for(i=0; i<dstW; i++)		{			int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;			int j;			(*filterPos)[i]= xx;				//Bilinear upscale / linear interpolate / Area averaging				for(j=0; j<filterSize; j++)				{					double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);					double coeff= 1.0 - d;					if(coeff<0) coeff=0;					filter[i*filterSize + j]= coeff;					xx++;				}			xDstInSrc+= xInc;		}	}	else	{		double xDstInSrc;		double sizeFactor, filterSizeInSrc;		const double xInc1= (double)xInc / (double)(1<<16);		int param= (flags&SWS_PARAM_MASK)>>SWS_PARAM_SHIFT;		if     (flags&SWS_BICUBIC)	sizeFactor= 4.0;		else if(flags&SWS_X)		sizeFactor= 8.0;		else if(flags&SWS_AREA)		sizeFactor= 1.0; //downscale only, for upscale it is bilinear		else if(flags&SWS_GAUSS)	sizeFactor= 8.0;   // infinite ;)		else if(flags&SWS_LANCZOS)	sizeFactor= param ? 2.0*param : 6.0;		else if(flags&SWS_SINC)		sizeFactor= 100.0; // infinite ;)		else if(flags&SWS_SPLINE)	sizeFactor= 20.0;  // infinite ;)		else if(flags&SWS_BILINEAR)	sizeFactor= 2.0;		else ASSERT(0)				if(xInc1 <= 1.0)	filterSizeInSrc= sizeFactor; // upscale		else			filterSizeInSrc= sizeFactor*srcW / (double)dstW;		filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible		if(filterSize > srcW-2) filterSize=srcW-2;		filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);		xDstInSrc= xInc1 / 2.0 - 0.5;		for(i=0; i<dstW; i++)		{			int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);			int j;			(*filterPos)[i]= xx;			for(j=0; j<filterSize; j++)			{				double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;				double coeff;				if(flags & SWS_BICUBIC)				{					double A= param ? -param*0.01 : -0.60;										// Equation is from VirtualDub					if(d<1.0)						coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);					else if(d<2.0)						coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);					else						coeff=0.0;				}/*				else if(flags & SWS_X)				{					double p= param ? param*0.01 : 0.3;					coeff = d ? sin(d*PI)/(d*PI) : 1.0;					coeff*= pow(2.0, - p*d*d);				}*/				else if(flags & SWS_X)				{					double A= param ? param*0.1 : 1.0;										if(d<1.0)						coeff = cos(d*PI);					else						coeff=-1.0;					if(coeff<0.0) 	coeff= -pow(-coeff, A);					else		coeff=  pow( coeff, A);					coeff= coeff*0.5 + 0.5;				}				else if(flags & SWS_AREA)				{					double srcPixelSize= 1.0/xInc1;					if(d + srcPixelSize/2 < 0.5) coeff= 1.0;					else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;					else coeff=0.0;				}				else if(flags & SWS_GAUSS)				{					double p= param ? param*0.1 : 3.0;					coeff = pow(2.0, - p*d*d);				}				else if(flags & SWS_SINC)				{					coeff = d ? sin(d*PI)/(d*PI) : 1.0;				}				else if(flags & SWS_LANCZOS)				{					double p= param ? param : 3.0; 					coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;					if(d>p) coeff=0;				}				else if(flags & SWS_BILINEAR)				{					coeff= 1.0 - d;					if(coeff<0) coeff=0;				}				else if(flags & SWS_SPLINE)				{					double p=-2.196152422706632;					coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);				}				else ASSERT(0)				filter[i*filterSize + j]= coeff;				xx++;			}			xDstInSrc+= xInc1;		}	}	/* apply src & dst Filter to filter -> filter2	   free(filter);	*/	ASSERT(filterSize>0)	filter2Size= filterSize;	if(srcFilter) filter2Size+= srcFilter->length - 1;	if(dstFilter) filter2Size+= dstFilter->length - 1;	ASSERT(filter2Size>0)	filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));	for(i=0; i<dstW; i++)	{		int j;		SwsVector scaleFilter;		SwsVector *outVec;		scaleFilter.coeff= filter + i*filterSize;		scaleFilter.length= filterSize;		if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);		else	      outVec= &scaleFilter;		ASSERT(outVec->length == filter2Size)		//FIXME dstFilter		for(j=0; j<outVec->length; j++)		{			filter2[i*filter2Size + j]= outVec->coeff[j];		}		(*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;		if(outVec != &scaleFilter) freeVec(outVec);	}	free(filter); filter=NULL;	/* try to reduce the filter-size (step1 find size and shift left) */	// Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)	minFilterSize= 0;	for(i=dstW-1; i>=0; i--)	{		int min= filter2Size;		int j;		double cutOff=0.0;		/* get rid off near zero elements on the left by shifting left */		for(j=0; j<filter2Size; j++)		{			int k;			cutOff += ABS(filter2[i*filter2Size]);			if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;			/* preserve Monotonicity because the core cant handle the filter otherwise */			if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;			// Move filter coeffs left			for(k=1; k<filter2Size; k++)				filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];			filter2[i*filter2Size + k - 1]= 0.0;			(*filterPos)[i]++;		}		cutOff=0.0;		/* count near zeros on the right */		for(j=filter2Size-1; j>0; j--)		{			cutOff += ABS(filter2[i*filter2Size + j]);			if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;			min--;		}		if(min>minFilterSize) minFilterSize= min;	}	ASSERT(minFilterSize > 0)	filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));	ASSERT(filterSize > 0)	filter= (double*)memalign(8, filterSize*dstW*sizeof(double));	*outFilterSize= filterSize;	if(flags&SWS_PRINT_INFO)		MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);	/* try to reduce the filter-size (step2 reduce it) */	for(i=0; i<dstW; i++)	{		int j;		for(j=0; j<filterSize; j++)		{			if(j>=filter2Size) filter[i*filterSize + j]= 0.0;			else		   filter[i*filterSize + j]= filter2[i*filter2Size + j];		}	}	free(filter2); filter2=NULL;		//FIXME try to align filterpos if possible	//fix borders	for(i=0; i<dstW; i++)	{		int j;		if((*filterPos)[i] < 0)		{			// Move filter coeffs left to compensate for filterPos			for(j=1; j<filterSize; j++)			{				int left= MAX(j + (*filterPos)[i], 0);				filter[i*filterSize + left] += filter[i*filterSize + j];				filter[i*filterSize + j]=0;			}			(*filterPos)[i]= 0;		}		if((*filterPos)[i] + filterSize > srcW)		{			int shift= (*filterPos)[i] + filterSize - srcW;			// Move filter coeffs right to compensate for filterPos			for(j=filterSize-2; j>=0; j--)			{				int right= MIN(j + shift, filterSize-1);				filter[i*filterSize +right] += filter[i*filterSize +j];				filter[i*filterSize +j]=0;			}			(*filterPos)[i]= srcW - filterSize;		}	}	// Note the +1 is for the MMXscaler which reads over the end	*outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));	memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));	/* Normalize & Store in outFilter */	for(i=0; i<dstW; i++)	{		int j;		double sum=0;		double scale= one;		for(j=0; j<filterSize; j++)		{			sum+= filter[i*filterSize + j];		}		scale/= sum;		for(j=0; j<filterSize; j++)		{			(*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);		}	}		(*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end	for(i=0; i<*outFilterSize; i++)	{		int j= dstW*(*outFilterSize);		(*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];	}	free(filter);}