public ScaleImage(SColor[] _colors, int _width) { m_originalColors = _colors; m_width = _width; m_height = m_originalColors.Length / _width; }
public SColor[] ScaleLinear(int _targetWidth, int _targetHeight) { m_scaleX = m_width / (float)_targetWidth; m_scaleY = m_height / (float)_targetHeight; // 500 / 250 = 2 m_scaleSizeX = (int)System.Math.Round(m_scaleX / 2.0f); m_scaleSizeY = (int)System.Math.Round(m_scaleY / 2.0f); m_outArray = new SColor[_targetWidth * _targetHeight]; for (int y = 0; y < _targetHeight; y++) { for (int x = 0; x < _targetWidth; x++) { m_buffer = GetPixelsForTargetPixel(x, y); SColor result = MergePixels(); m_outArray [y * _targetWidth + x] = result; } } return(m_outArray); }
public void Add(SColor _c) { r += _c.r; g += _c.g; b += _c.b; a += _c.a; }
SColor MergePixels() { SColor outColor = new SColor(0, 0, 0, 0); for (int i = 0; i < m_buffer.Length; i++) { outColor.Add(m_buffer[i]); } outColor.Divide(m_buffer.Length); return(outColor); }
public static Scaling.SColor[] GetColors(Color32[] _bytes) { Scaling.SColor[] outArray = new Scaling.SColor[_bytes.Length]; for (int i = 0; i < _bytes.Length; i++) { outArray[i] = new Scaling.SColor( _bytes[i].r / 255.0f, _bytes[i].g / 255.0f, _bytes[i].b / 255.0f, _bytes[i].a / 255.0f ); } return(outArray); }
public static Scaling.SColor[] GetColors(Color32[] _bytes) { Scaling.SColor[] outArray = new Scaling.SColor[_bytes.Length]; for(int i = 0;i< _bytes.Length;i++) { outArray[i] = new Scaling.SColor( _bytes[i].r / 255.0f, _bytes[i].g / 255.0f, _bytes[i].b / 255.0f, _bytes[i].a / 255.0f ); } return outArray; }
public SColor[] Filter(int _targetWidth, int _targetHeight) { double xfactor = _targetWidth / (double)m_width; double yfactor = _targetHeight / (double)m_height; minsupport = 5; // reset kernel cache m_kernelsCacheX = new Kernel1D[100]; m_kernelsCacheY = new Kernel1D[100]; // create empty output image m_targetWidth = (int)(0.5 + m_width * xfactor); m_targetHeight = (int)(0.5 + m_height * yfactor); m_output = new SColor[m_targetWidth * m_targetHeight]; // for each pixel in output image for (int y = 0; y < m_targetHeight; y++) { for (int x = 0; x < m_targetWidth; x++) { // ideal sample point in the source image double xo = x / xfactor; double yo = y / yfactor; // separate integer part and fractionnal part int x_int = (int)xo; double x_frac = xo - x_int; int y_int = (int)yo; double y_frac = yo - y_int; // get/compute resampling Kernels Kernel1D kx = getKernelX(xfactor, x_frac); Kernel1D ky = getKernelY(yfactor, y_frac); // compute resampled value SColor rgb = fastconvolve(x_int, y_int, kx, ky); // set to output image m_output[x + y * m_targetWidth] = rgb; } } return(m_output); }
/** * convolve an image with a kernel for one pixel * * @param c input image * @param x,y coords of the pixel * @param kernelx,kernely kernels to use * @return new value of the pixel */ private SColor fastconvolve(int x, int y, Kernel1D kernelx, Kernel1D kernely) { int halfwindowy = kernely.size / 2; // assume a odd size int halfwindowx = kernelx.size / 2; // assume a odd size // empty tmpbuffer ArrayFill(tmpbuffer_r, 0); ArrayFill(tmpbuffer_g, 0); ArrayFill(tmpbuffer_b, 0); ArrayFill(tmpbuffer_a, 0); // pass 1 : horizontal convolution of image lines aree stored in tmpbuffer for (int dy = -halfwindowy; dy <= halfwindowy; dy++) { if (y + dy < 0 || y + dy >= m_height) { continue; } for (int dx = -halfwindowx; dx <= halfwindowx; dx++) { if (x + dx < 0 || x + dx >= m_width) { continue; } SColor rgb = m_input[x + dx + (y + dy) * m_width]; tmpbuffer_r[halfwindowy + dy] += kernelx.coefs[halfwindowx - dx] * rgb.r; tmpbuffer_g[halfwindowy + dy] += kernelx.coefs[halfwindowx - dx] * rgb.g; tmpbuffer_b[halfwindowy + dy] += kernelx.coefs[halfwindowx - dx] * rgb.b; tmpbuffer_a[halfwindowy + dy] += kernelx.coefs[halfwindowx - dx] * rgb.a; } } // pass 2 : vertical convolution of values stored in tmpbuffer double rc = 0, gc = 0, bc = 0, ac = 0; for (int dy = -halfwindowy; dy <= halfwindowy; dy++) { rc += kernely.coefs[halfwindowy - dy] * tmpbuffer_r[halfwindowy + dy]; gc += kernely.coefs[halfwindowy - dy] * tmpbuffer_g[halfwindowy + dy]; bc += kernely.coefs[halfwindowy - dy] * tmpbuffer_b[halfwindowy + dy]; ac += kernely.coefs[halfwindowy - dy] * tmpbuffer_a[halfwindowy + dy]; } // normalization double norm = kernelx.normalizer * kernely.normalizer; rc /= norm; gc /= norm; bc /= norm; ac /= norm; // return in argb format float r = (float)Math.Min(1.0, Math.Max(0, rc)); float g = (float)Math.Min(1, Math.Max(0, gc)); float b = (float)Math.Min(1, Math.Max(0, bc)); float a = (float)Math.Min(1, Math.Max(0, ac)); return(new SColor(r, g, b, a)); }
SColor MergePixels() { SColor outColor = new SColor(0,0,0,0); for(int i = 0;i<m_buffer.Length;i++) { outColor.Add(m_buffer[i]); } outColor.Divide(m_buffer.Length); return outColor; }