public static GafImageInfo TrimImageInfo(GafImageInfo imageInfo, Rect rect) { var newData = new byte[rect.Width * rect.Height]; for (var y = 0; y < rect.Height; ++y) { for (var x = 0; x < rect.Width; ++x) { var sourceY = rect.Y + y; var sourceX = rect.X + x; newData[(y * rect.Width) + x] = imageInfo.Data[(sourceY * imageInfo.Width) + sourceX]; } } return(new GafImageInfo { Data = newData, Width = rect.Width, Height = rect.Height, TransparencyIndex = imageInfo.TransparencyIndex }); }
public static Rect ComputeMinBoundingRect(GafImageInfo imageInfo) { // FIXME: this will blow up if the image is entirely blank var top = Enumerable.Range(0, imageInfo.Height) .First(y => Enumerable.Range(0, imageInfo.Width) .Any(x => imageInfo.Data[(y * imageInfo.Width) + x] != imageInfo.TransparencyIndex)); var left = Enumerable.Range(0, imageInfo.Width) .First(x => Enumerable.Range(0, imageInfo.Height) .Any(y => imageInfo.Data[(y * imageInfo.Width) + x] != imageInfo.TransparencyIndex)); var bottom = BackwardsRange(0, imageInfo.Height) .First(y => Enumerable.Range(0, imageInfo.Width) .Any(x => imageInfo.Data[(y * imageInfo.Width) + x] != imageInfo.TransparencyIndex)); var right = BackwardsRange(0, imageInfo.Width) .First(x => Enumerable.Range(0, imageInfo.Height) .Any(y => imageInfo.Data[(y * imageInfo.Width) + x] != imageInfo.TransparencyIndex)); return(new Rect(left, top, right - left + 1, bottom - top + 1)); }