long calcCurve(long x, long y, long Ng, t_int32 *gaussKernel,
		char *phi, t_jit_matrix_info* phi_minfo)
{
	long i, j;
	long curveVelocity=0;
	long range;

	//e.g. for 5, go from -2 to + 2 (not taking into account even kernels)
	range = (long)jit_math_floor( ((float)Ng)/2.0f );

	//don't handle boundary conditions right now
	for(j=-range; j <= range; j++) {
		for(i=-range; i <= range; i++) {
			curveVelocity += (long)(*(phi + x+i + (y+j)*phi_minfo->dimstride[1])) * gaussKernel[ i+range + (j+range)*Ng ];
		}
	}

	return curveVelocity;
}
void xray_jit_colormap_calculate_ndim(t_xray_jit_colormap *x, long dimcount, long *dim, long planecount, 
		t_jit_matrix_info *in1_minfo, char *bip1, 
		t_jit_matrix_info *out_minfo, char *bop, 
		t_jit_matrix_info *count_minfo, char *bcountp)
{
	long i,j;
	uchar *ip;
	long height, width, incolspan, outcolspan, inrowspan, outrowspan;
	long luma;
	float *fcp, *fcountp, currentColor[3];
	float colorSlope[3], colorPos;
	long lastColorIndex;
		
	if (dimcount<1) return; //safety
	
	switch(dimcount)
	{
	default:
	case 1:
		dim[1]=1;
	case 2:
		width  = dim[0];
		height = dim[1];
		
		incolspan = in1_minfo->dimstride[0];
		outcolspan = out_minfo->dimstride[0];
		inrowspan = in1_minfo->dimstride[1];
		outrowspan = out_minfo->dimstride[1];
		
		if (in1_minfo->type==_jit_sym_char)
		{
			fcp = (float *)(bop);
			fcountp = (float *)(bcountp);
		
			if(x->mode)
			{
				//accumulate color values according to their luminance
				//and increment counter for each color accumulated
				for(i=0; i < height; i++)
				{
					ip = (uchar *)(bip1 + i*inrowspan);
					
					for(j=0; j < width; j++)
					{
						currentColor[0] = (float)ip[4*j+1];
						currentColor[1] = (float)ip[4*j+2];
						currentColor[2] = (float)ip[4*j+3];
						
						luma = (long)jit_math_floor(0.299*currentColor[0] + 0.587*currentColor[1] + 0.114*currentColor[2]);
						
						if(!fcountp[luma])
						{
							fcp[luma*3] += currentColor[0];
							fcp[luma*3+1] += currentColor[1];
							fcp[luma*3+2] += currentColor[2];
						
							fcountp[luma]++;
						}
					}
				}
				
				//set white value
				fcp[3*255] = 255;
				fcp[3*255+1] = 255;
				fcp[3*255+2] = 255;
				fcountp[255]++;
				
				lastColorIndex = 0;
				
				//interpolate missing colorvalues
				for(i=0; i < out_minfo->dim[0]; i++)
				{
					if(fcountp[i])
					{
						if(i-lastColorIndex > 1)
						{
							colorSlope[0] = (fcp[3*i]-fcp[3*lastColorIndex])/((float)(i-lastColorIndex));
							colorSlope[1] = (fcp[3*i+1]-fcp[3*lastColorIndex+1])/((float)(i-lastColorIndex));
							colorSlope[2] = (fcp[3*i+2]-fcp[3*lastColorIndex+2])/((float)(i-lastColorIndex));
							
							for(j=lastColorIndex+1; j < i; j++)
							{
								colorPos = (float)(j-lastColorIndex);
								
								fcp[3*j] = colorPos*colorSlope[0]+fcp[3*lastColorIndex];
								fcp[3*j+1] = colorPos*colorSlope[1]+fcp[3*lastColorIndex+1];
								fcp[3*j+2] = colorPos*colorSlope[2]+fcp[3*lastColorIndex+2];
							}
						}
						
						lastColorIndex = i;
					}
				}
			}
			else
			{
				//set white value
				fcp[3*255] = 255;
				fcp[3*255+1] = 255;
				fcp[3*255+2] = 255;
				fcountp[255]++;
				
				//accumulate color values according to their luminance
				//and increment counter for each color accumulated
				for(i=0; i < height; i++)
				{
					ip = (uchar *)(bip1 + i*inrowspan);
					
					for(j=0; j < width; j++)
					{
						currentColor[0] = (float)(ip[4*j+1]);
						currentColor[1] = (float)(ip[4*j+2]);
						currentColor[2] = (float)(ip[4*j+3]);
						
						luma = (long)jit_math_floor(0.299*currentColor[0] + 0.587*currentColor[1] + 0.114*currentColor[2]);

						fcp[luma*3] += currentColor[0];
						fcp[luma*3+1] += currentColor[1];
						fcp[luma*3+2] += currentColor[2];
						
						fcountp[luma]++;
					}
				}
				
				lastColorIndex = 0;
				
				//average accumulated colorvalues, then interpolate missing values
				for(i=0; i < out_minfo->dim[0]; i++)
				{				
					if(fcountp[i])
					{
						fcp[3*i] /= fcountp[i];
						fcp[3*i+1] /= fcountp[i];
						fcp[3*i+2] /= fcountp[i];
						
						if(i-lastColorIndex > 1)
						{
							colorSlope[0] = (fcp[3*i]-fcp[3*lastColorIndex])/((float)(i-lastColorIndex));
							colorSlope[1] = (fcp[3*i+1]-fcp[3*lastColorIndex+1])/((float)(i-lastColorIndex));
							colorSlope[2] = (fcp[3*i+2]-fcp[3*lastColorIndex+2])/((float)(i-lastColorIndex));
							
							for(j=lastColorIndex+1; j < i; j++)
							{
								colorPos = (float)(j-lastColorIndex);
								
								fcp[3*j] = colorPos*colorSlope[0]+fcp[3*lastColorIndex];
								fcp[3*j+1] = colorPos*colorSlope[1]+fcp[3*lastColorIndex+1];
								fcp[3*j+2] = colorPos*colorSlope[2]+fcp[3*lastColorIndex+2];
							}
						}
						
						lastColorIndex = i;
					}
				}
			}
		}
		break;
	}
}