public TextureProfile ReadTextureProfile(string fileName, IPixelsSource pixelsSource) { TextureProfile textureProfile = JsonConvert.DeserializeObject <TextureProfile>(File.ReadAllText(fileName)); textureProfile.Blobs.ForEach ( blob => { blob.PixelsSource = pixelsSource; //Reconnect the pixels to their color, since we removed them while writing to save space. blob.Pixels.ForEach(pixel => pixel.PixelColor = blob.BlobColor); //Doing this cut the file size down to 1/5 of the size. } ); return(textureProfile); }
private TextureProfile LoadTextureProfile(IPixelsSource pixelsSource) { return(new TextureProfileReader().ReadTextureProfile(this.TextureProfilePath, pixelsSource)); }
public PixelBlob(IPixelsSource pixelsSource) { this.pixelsSource = pixelsSource; }
private PixelColor[,] DrawRandomLine(IAlgorithmTarget target, PixelColor[,] source, IPixelsSource pixelsSource) { var random = new Random(); var direction = (SiblingDirection)random.Next(0, 7); var length = random.Next(20, 100); var currentCount = 0; var randomColor = System.Windows.Media.Color.FromArgb(255, (byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)).ToPixelColor(); var currentPixel = target.AlgorithmPixels[random.Next(0, target.AlgorithmPixels.Count - 1)]; do { source[(int)currentPixel.Position.Y, (int)currentPixel.Position.X] = randomColor; currentPixel = pixelsSource.GetSibling(currentPixel, direction, true); ++currentCount; }while (currentPixel != null && currentCount < length); return(source); }
private List <PixelBlob> ProcessPixelGroup(IGrouping <PixelColor, Pixel> pixelGroup, IPixelsSource pixelsSource, ITaskContext taskContext) { var pixelGroupList = pixelGroup.ToList(); var keyDisplayName = pixelGroup.Key.ToColor().ToHexString(); var groupBlobs = new List <PixelBlob>(); try { double originalCount = (double)pixelGroupList.Count; Func <int> calculatePercentage = () => { return(Math.Min((int)(100 - (((double)pixelGroupList.Count / originalCount) * 100)), 99)); }; int percentageDone = 0; Action updatePercentage = () => { taskContext.UpdateMessage($"{keyDisplayName}: Processing pixel group. {pixelGroupList.Count} left out of {originalCount}"); percentageDone = calculatePercentage(); taskContext.UpdateProgress(percentageDone); //Only go up to 99 so the task doesn't end. }; while (pixelGroupList.Count > 0) { updatePercentage(); var nextPixel = pixelGroupList.First(); var blobPixels = new List <Pixel>(); var currentList = new List <Pixel>(); currentList.Add(nextPixel); var siblingTransferList = new List <Pixel>(); do { blobPixels.AddRange(currentList); currentList.ForEach ( x => { pixelGroupList.Remove(x); foreach (var sibling in pixelsSource.GetSiblings(x, true)) { if (sibling == null || blobPixels.Contains(sibling) || siblingTransferList.Contains(sibling)) { continue; } siblingTransferList.Add(sibling); } } ); currentList.Clear(); currentList.AddRange(siblingTransferList); siblingTransferList.Clear(); updatePercentage(); }while (currentList.Any()); var blob = new PixelBlob(pixelsSource) { BlobColor = pixelGroup.Key, Pixels = blobPixels }; groupBlobs.Add(blob); pixelGroupList = pixelGroupList.Where(pixel => !blobPixels.Contains(pixel)).ToList(); } taskContext.UpdateMessage($"{keyDisplayName}: {groupBlobs.Count} blobs."); taskContext.UpdateProgress(100); } catch (Exception ex) { string stopHere = ex.Message; } return(groupBlobs); }