/// <summary> /// given a location of a pixel (x,y), returns the info about that pixel. /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <param name="imageDataArray"></param> /// <param name="injectedContext"></param> /// <returns></returns> public pixel getPixelInfo(int i, int j, byte[] imageDataArray, SeamCarvingContext injectedContext) { byte[] red = { 0, 0, 0, this.getPixel(imageDataArray, i, j, 0, injectedContext) }; byte[] green = { 0, 0, 0, this.getPixel(imageDataArray, i, j, 1, injectedContext) }; byte[] blue = { 0, 0, 0, this.getPixel(imageDataArray, i, j, 2, injectedContext) }; byte[] alpha = { 0, 0, 0, this.getPixel(imageDataArray, i, j, 3, injectedContext) }; if (BitConverter.IsLittleEndian) { Array.Reverse(red); Array.Reverse(green); Array.Reverse(blue); Array.Reverse(alpha); } int redInt = BitConverter.ToInt32(red, 0); int greenInt = BitConverter.ToInt32(green, 0); int blueInt = BitConverter.ToInt32(blue, 0); int alphaInt = BitConverter.ToInt32(alpha, 0); pixel toReturn = new pixel(); toReturn.red = redInt; toReturn.green = greenInt; toReturn.blue = blueInt; toReturn.alpha = alphaInt; return toReturn; }
/// <summary> /// given a seam carving context, calculates the gradient of every pixel. /// </summary> /// <param name="injectedContext"></param> public void calculateGradient(SeamCarvingContext injectedContext) { injectedContext.gradientArray = new byte[injectedContext.imageDataArray.Length]; for (int i = 1; i < injectedContext.Height - 1; i++) { for (int j = 1; j < injectedContext.Width - 1; j++) { pixel last = seamUtilities.getPixelInfo(i, j - 1, injectedContext.imageDataArray, injectedContext); pixel current = seamUtilities.getPixelInfo(i, j, injectedContext.imageDataArray, injectedContext); pixel next = seamUtilities.getPixelInfo(i, j + 1, injectedContext.imageDataArray, injectedContext); byte gradient = calculateGradientOfPixel(last, current, next); seamUtilities.setPixel(injectedContext.gradientArray, i, j, 0, gradient, injectedContext); seamUtilities.setPixel(injectedContext.gradientArray, i, j, 1, gradient, injectedContext); seamUtilities.setPixel(injectedContext.gradientArray, i, j, 2, gradient, injectedContext); seamUtilities.setPixel(injectedContext.gradientArray, i, j, 3, 0xff, injectedContext); } } }
/// <summary> /// takes three pixels and calculates the current pixels difference from the pixels next to it. /// </summary> /// <param name="last"></param> /// <param name="current"></param> /// <param name="next"></param> /// <returns></returns> public byte calculateGradientOfPixel(pixel last, pixel current, pixel next) { return((byte)((((current.red - next.red + 1) * (current.blue - next.blue + 1) * (current.green - next.green + 1)) + last.red) / 128)); }