public void Load() { var image = this.LoadImage(); var pixelColors = image.CopyPixels(); var pixelsSource = PixelsSource.FromPixelColors(pixelColors); var dialogViewModel = new DialogViewModel(); Texture outputTexture = null; dialogViewModel.AddTask ( (taskContext) => { try { taskContext.UpdateMessage("Trying to load file."); var textureProfile = this.LoadTextureProfile(pixelsSource); outputTexture = new Texture(image, textureProfile); } catch (Exception ex) { taskContext.UpdateMessage($"Error loading file: {ex.Message}"); return; } taskContext.UpdateProgress(100); } ); this.windowManager.ShowDialog(dialogViewModel); this.OutputTexture = outputTexture; this.TryClose(this.OutputTexture != null); }
public async Task <TextureProfile> GenerateTextureProfileAsync(PixelColor[,] pixelColors, Color transparencyColor, IDialogContext dialogContext, ITaskContext ourTaskContext) { ourTaskContext.UpdateMessage("Starting processing of image"); PixelsSource pixelsSource = null; await dialogContext.AddTask ( (taskContext) => { taskContext.UpdateMessage("Converting pixels"); pixelsSource = PixelsSource.FromPixelColors(pixelColors); taskContext.UpdateProgress(100); } ); var pixelGroups = pixelsSource.PixelsList.GroupBy(pixel => pixel.PixelColor); var tasks = new List <Task>(); var modifiedPixelGroups = new List <List <PixelBlob> >(); object mutex = new object(); foreach (var pixelGroup in pixelGroups) { if (transparencyColor.IsEqualToPixelColor(pixelGroup.Key)) { continue; } tasks.Add ( dialogContext.AddTask ( (taskContext) => { var result = this.ProcessPixelGroup(pixelGroup, pixelsSource, taskContext); lock (mutex) { modifiedPixelGroups.Add(result); } } ) ); } Task.WaitAll(tasks.ToArray()); ourTaskContext.UpdateProgress(100); var texture = new TextureProfile() { TransparencyColor = transparencyColor.ToPixelColor(), Blobs = modifiedPixelGroups.SelectMany(x => x).ToList() }; return(texture); }