private int CalculateDifference(IImageData a, IImageData b) { if (a.Width != b.Width || a.Height != b.Height) { throw new Exception(string.Format("Image sizes incompatible: {0} is {1}x{2}; {3} is {4}x{5}", a.ImagePath, a.X, a.Y, b.ImagePath, b.X, b.Y)); } var bitmapA = _imageLoader.LoadImage(a); var bitmapB = _imageLoader.LoadImage(b); bitmapA.Lock(); bitmapB.Lock(); int difference = 0; for (int xOffset = 0; xOffset < a.Width; xOffset++) { for (int yOffset = 0; yOffset < a.Height; yOffset++) { Color cA = bitmapA.GetPixel(a.X + xOffset, a.Y + yOffset); Color cB = bitmapB.GetPixel(b.X + xOffset, b.Y + yOffset); difference += CalculatePixelColorDifference(cA, cB); } } bitmapA.Unlock(); bitmapB.Unlock(); return(difference); }
public BitmapAndSegments[] RefineMatches(AdvancedStats origStats, ImageSegments[] basicMatches, IImageLoader loader, StatsGenerator statsGenerator) { return(basicMatches.Select(m => new { Image = m.Image, Bitmap = loader.LoadImage(m.Image.ImagePath), ManipulationInfos = m.ManipulationInfos }) .SelectMany(m => m.ManipulationInfos.Select(r => new { ImagePath = m.Image, Image = m.Bitmap, Stats = statsGenerator.GetAdvancedStats(m.Bitmap, r.Rectangle), ManipulationInfo = r, })) .OrderBy(m => m.Stats.MidResR.Difference(origStats.MidResR) + 1.1 * m.Stats.MidResG.Difference(origStats.MidResG) + m.Stats.MidResB.Difference(origStats.MidResB) + 1.5 * m.Stats.MidResIntensity.Difference(origStats.MidResIntensity) ) // .Take(10) .Take(1) .GroupBy(m => m.ImagePath.ImagePath) .Select(group => new BitmapAndSegments(group.First().Image, group.Select(i => i.ManipulationInfo))) .ToArray()); }
public async Task <ImageMeta> LoadImageAsync(string fileName) { _lastImageToLoad = fileName; var slowTask = Task.Run(() => _slowLoader.LoadImage(fileName)); var fastTask = Task.Run(() => _fastLoader.LoadImage(fileName)); var firstTask = await Task.WhenAny(slowTask, fastTask).ConfigureAwait(false); if (firstTask == fastTask) { Debug.WriteLine("fast was faster"); ThreadPool.QueueUserWorkItem(state => ReplaceImage(fileName, slowTask.Result)); } else { Debug.WriteLine("slow was faster"); } var img = await firstTask.ConfigureAwait(false); if (_lastImageToLoad == fileName) { _receiver.ReceiveImage(img); } return(img); }
public IEnumerable <ImageData> GetSourceDatas() { FastBitmap imageBitmap = _imageLoader.LoadImage(_imagePath); var imageRegion = new ImageRegion(Point.Origin, imageBitmap.Width, imageBitmap.Height); return(_regionCreationStrategy.GetRegions(imageRegion).Select(region => GetImageData(region, imageBitmap))); }
private int BuildImage(string uri, TextureInfo textureInfo) { int name = -1; // wrapped in this nasty try/catch because bad memory is touched when we're // rendering to a thumbnail (different glContext). driver bug? :_( try { string filename = uri.Replace(@"file:\\\", ""); string ext = Path.GetExtension(filename); IImageLoader loader = m_imageLoaderRegistry.GetLoader(ext); Image image; using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) image = loader.LoadImage(stream); name = TryCreateOpenGlTextureFromImage(image, textureInfo); } catch (System.IO.IOException e) { // excessive error messages adds minutes to a large level that is missing textures if (m_missingTextureReports++ < 10) { Outputs.WriteLine(OutputMessageType.Error, e.Message); } } catch (Exception e) { Outputs.WriteLine(OutputMessageType.Error, e.Message); Outputs.WriteLine(OutputMessageType.Info, e.StackTrace); } return(name); }
public async UniTask LoadImage(string url) { SetTexture(_loadingTexture); _currentUrl = url; try { var texture = await _imageLoader.LoadImage(url); // cancel if the scene has been unloaded if (!this || !_image || !_imageFitter) { return; } // skipped if (_currentUrl != url) { Debug.Log($"Skipped loading image: {url}"); return; } SetTexture(texture); } catch (CachedImageDownloaderException e) when(e.Code == 404) { Debug.Log($"Image not found (404): {e.Url}"); } catch (Exception e) { _errorReceiver.Receive(e, "Failed loading image", "Check the Internet connection"); SetTexture(_defaultTexture); } }
private ImageMeta PruneCacheAndLoadImage(string fileName) { if (_cache.Count >= _maxCacheSize) { RemoveLeastUsedItemFromCache(); } return(_loader.LoadImage(fileName)); }
private SKBitmap Decode(byte[] buffer, LoadRequest req) { req.Cancellation.ThrowIfCancellationRequested(); using (var input = new MemoryStream(buffer)) { return(_imageLoader.LoadImage(req.Entity, input)); } }
private void WriteSourceAndFill(IImageData hole, IImageData fill) { FastBitmap holeImage = _imageLoader.LoadImage(hole); FastBitmap fillImage = _imageLoader.LoadImage(fill); holeImage.Lock(); fillImage.Lock(); for (int xOffset = 0; xOffset < hole.Width; xOffset++) { for (int yOffset = 0; yOffset < hole.Height; yOffset++) { Color holeColor = holeImage.GetPixel(hole.X + xOffset, hole.Y + yOffset); Color fillColor = fillImage.GetPixel(fill.X + xOffset, fill.Y + yOffset); _newImage.SetPixel(hole.X + xOffset, hole.Y + yOffset, GetNewColor(holeColor, fillColor)); } } holeImage.Unlock(); fillImage.Unlock(); }
/* * public void DumpStats() * { * var img = _class1.GetSourceImage(); * * var randomSourceSegment = img.Segments.Random().First().ManipulationInfo; * SourceImage = _class1.GetBitmap(img.Image, randomSourceSegment).ToBitmapImage(); * OnPropertyChanged(nameof(SourceImage)); * * // StatsGenerator statsGenerator = new StatsGenerator(Loader); * // var convolutionImages = statsGenerator.GetMidResConvolutionAsBitmap(img.Image).Select(bm => bm.ToBitmapImage()); * // var convolutionReducedImages = statsGenerator.GetMidResConvolutionReducedAsBitmap(img.Image).Select(bm => bm.ToBitmapImage()); * * ConvolutionObservableCollection.Clear(); * ConvolutionReducedObservableCollection.Clear(); * * /* * foreach (var convolutionImage in convolutionImages) * { * ConvolutionObservableCollection.Add(convolutionImage); * } * * foreach (var convolutionImage in convolutionReducedImages) * { * ConvolutionReducedObservableCollection.Add(convolutionImage); * } #1# * * /* * var matchedImages = class1.CompareImageToAlphabet(img.Image, firstSegment) * .Select(i => i.ToBitmapImage()); * MatchesObservableCollection.Clear(); * foreach (var matchedImage in matchedImages) * { * MatchesObservableCollection.Add(matchedImage); * } #1# * * * var refinedMatchedImages = _class1.GetRefinedMatches(img.Image, randomSourceSegment) * .Select(i => i.ToBitmapImage()); * RefinedMatchesObservableCollection.Clear(); * foreach (var matchedImage in refinedMatchedImages) * { * RefinedMatchesObservableCollection.Add(matchedImage); * } * * var refinedMatchedImages2 = _class1.GetRefinedMatches2(img.Image, randomSourceSegment) * .Select(i => i.ToBitmapImage()); * RefinedMatches2ObservableCollection.Clear(); * foreach (var matchedImage in refinedMatchedImages2) * { * RefinedMatches2ObservableCollection.Add(matchedImage); * } * * OnPropertyChanged(nameof(MatchingImages)); * OnPropertyChanged(nameof(RefinedMatchingImages)); * OnPropertyChanged(nameof(RefinedMatchingImages2)); * } */ public void ProduceImage() { var openFileDialog1 = new OpenFileDialog(); var dialogResult = openFileDialog1.ShowDialog(); if (!dialogResult.HasValue || !dialogResult.Value) { return; } var sourceBmp = _class1.LoadSourceImage(openFileDialog1.FileName); SourceImage = sourceBmp.ToBitmapImage(); var img = _class1.GetSourceImage(); FastBitmap.FastBitmap originalImage = Loader.LoadImage(img.Image.ImagePath); ConvolutionObservableCollection.Clear(); ConvolutionReducedObservableCollection.Clear(); var matches = new List <SourceAndMatch>(); foreach (var segmentAndStatse in img.Segments) { // Get matches var refinedMatchedImages = _class1.GetRefinedMatches2(originalImage, segmentAndStatse.ManipulationInfo); // Select a match // TODO: Do better var bestMatch = refinedMatchedImages.SelectRandom(); // var bestMatch = refinedMatchedImages.First(); var match = new SourceAndMatch(segmentAndStatse.ManipulationInfo, bestMatch); matches.Add(match); } var newImageBuilder = new ReconstructedImageBuilder(originalImage, 0.75); newImageBuilder.ApplyMatches(matches); newImageBuilder.SaveAs(@"c:\src\MosaicMaker2\result.bmp"); ResultImage = newImageBuilder.NewImage.ToBitmapImage(); }
private void ProcessRequests() { while (true) { iEvent.WaitOne(); Monitor.Enter(iLockObject); if (iPendingRequests.Count > 0 && IsRunning) { ImageRequest request = iPendingRequests[0]; IImageLoader <ImageType> loader = iImageLoader; iPendingRequests.Remove(request); iExecutingRequests.Add(request); Monitor.Exit(iLockObject); try { IImage <ImageType> img = loader.LoadImage(new Uri(request.Uri), iDownscaleImageSize); img.IncrementReferenceCount(); lock (iLockObject) { Add(request.Uri, img); iExecutingRequests.Remove(request); } OnEventImageAdded(request, img); img.DecrementReferenceCount(); } catch (Exception ex) { UserLog.WriteLine("Error downloading image: " + request.Uri + ", " + ex.ToString()); lock (iLockObject) { iImageCacheFailures.Add(request.Uri); iExecutingRequests.Remove(request); } OnEventRequestFailed(request.Uri); } } else { iEvent.Reset(); Monitor.Exit(iLockObject); } } }
/// <summary> /// Add a new load request to the queue /// </summary> /// <param name="entity">Entity to load</param> /// <returns> /// Task finished when <paramref name="entity"/> is loaded or the loading failed. /// </returns> public Task <SKBitmap> EnqueueAsync(IEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } var version = _queueVersion; var promise = new TaskCompletionSource <SKBitmap>(); _queue = _queue.ContinueWith(_ => { // check if this request has been cancelled if (version != _queueVersion) { promise.SetResult(null); return; } try { var image = _imageLoader.LoadImage(entity); // if the request has been canceled, dispose the image if (version != _queueVersion) { image?.Dispose(); image = null; } promise.SetResult(image); } catch (OperationCanceledException) { promise.SetCanceled(); } catch (Exception e) { promise.SetException(e); } }); return(promise.Task); }
public override object LoadImage(string fileName) { using (var s = loader.LoadImage(fileName)) return(toolkit.ImageBackendHandler.LoadFromStream(s)); }
public IEnumerable <Bitmap> GetRefinedMatches(PhysicalImage image, ImageManipulationInfo manipulationInfo) { var bitmap = _loader.LoadImage(image.ImagePath); return(GetRefinedMatches(bitmap, manipulationInfo)); }
public BasicStats GetStats(PhysicalImage image, ImageManipulationInfo manipulationInfo) { global::FastBitmap.FastBitmap bitmap = _loader.LoadImage(image.ImagePath); return(GetStats(bitmap, manipulationInfo)); }
/// <summary> /// Generates scene graph updates for renderer. /// </summary> public async IAsyncEnumerable <IRenderCommand> Process(AssertionBatch assertionBatch) { var knownUpdateObjects = new List <ObjectIdentityBase>(); foreach (var assertion in assertionBatch.Assertions) { if (assertion is GetPickingInfo getPickingInfo) { yield return(new RenderCommands.RequestPickingInfo(getPickingInfo.ScreenX, getPickingInfo.ScreenY)); } else if (assertion is Core.Assertions.ClearColor clearColor) { _sceneGraph.RgbClearColor = new (clearColor.Red, clearColor.Green, clearColor.Blue, 1.0f); yield return(new RenderCommands.ClearColor(_sceneGraph.RgbClearColor)); } else if (assertion is CameraProjectionPerspective projectionPerspective) { // Projection matrix is set in uniforms yield return(new RenderCommands.SetProjectionMatrix(_sceneGraph, projectionPerspective.FieldOfViewVerticalDegrees, projectionPerspective.NearPlane, projectionPerspective.FarPlane)); } else if (assertion is CameraOrientation cameraOrientation) { // View matrix is set in uniforms _sceneGraph.ViewMatrix = cameraOrientation.ToMatrix(); } else if (assertion is Core.Assertions.Vertex assertionVertex) { if (!_sceneGraph.Vertices.TryGetValue(assertionVertex.VertexId, out Core.Scene.Vertex vertex)) { vertex = new Core.Scene.Vertex(_sceneGraph, assertionVertex.VertexId); _sceneGraph.Vertices[assertionVertex.VertexId] = vertex; } if (assertionVertex.Coordinates != default) { vertex.Coordinates = assertionVertex.Coordinates; knownUpdateObjects.Add(vertex); } } else if (assertion is Core.Assertions.Triangle triangle) { var newTriangle = new Core.Scene.Triangle(_sceneGraph, triangle.TriangleId) { VertexIds = triangle.VertexIds }; _partialObjectsWatchList.AddLast(newTriangle); _sceneGraph.Triangles[triangle.TriangleId] = newTriangle; } else if (assertion is Core.Assertions.GrabScreenshot) { yield return(new RenderCommands.GrabScreenshot(_renderWindow, _cancellationTokenManager)); } else if (assertion is Core.Assertions.Texture texture) { // Pre-load texture to avoid doing this in the function setting OpenGL state var loadedImage = await _imageLoader.LoadImage(new Uri(texture.Uri)); _sceneGraph.Textures[texture.TextureId] = new Core.Scene.Texture(_sceneGraph, texture.TextureId, loadedImage.BufferRgba, loadedImage.Width, loadedImage.Height); } else if (assertion is TexCoords texCoords) { _sceneGraph.SurfaceVertexTexCoords[texCoords.ObjectIdentifier] = texCoords; } else if (assertion is SurfaceColor surfaceColor) { _sceneGraph.SurfaceVertexColors[surfaceColor.ObjectIdentifier] = surfaceColor; } else { throw new ArgumentException($"Unknown assertion type: {assertion.GetType()}"); } } // Split objects into new and update lists var newRenderTriangles = new List <Core.Scene.Triangle>(); // Get known complete geometry LinkedListNode <Core.Scene.Triangle> currentNode = _partialObjectsWatchList.First; while (currentNode != null) { if (_bufferManager.HasExistingTriangleBuffer(currentNode.Value)) { knownUpdateObjects.Add(currentNode.Value); currentNode = currentNode.Next; continue; } if (!currentNode.Value.HasMinimumRenderInfo) { currentNode = currentNode.Next; continue; } newRenderTriangles.Add(currentNode.Value); var next = currentNode.Next; _partialObjectsWatchList.Remove(currentNode); currentNode = next; } if (newRenderTriangles.Any()) { yield return(_bufferManager.CreateRenderCommands(newRenderTriangles)); } if (knownUpdateObjects.Any()) { foreach (var reloadCommand in _bufferManager.CreateReloadCommands(knownUpdateObjects)) { yield return(reloadCommand); } } }
private Image <Gray, byte> GetInputImage() { return(_bitmapLoader.LoadImage(Configuration.InputImagePath)); }
private void PaintGraphicsElements(IEnumerable <IGraphicsElement> graphicsElements) { foreach (IGraphicsElement graphicsElement in graphicsElements) { ITransform?renderTransform = graphicsElement.RenderTransform; if (renderTransform != null) { skCanvas.Save(); ApplyTransform(renderTransform, graphicsElement); } if (graphicsElement is IPath path) { PaintPath(path); } else if (graphicsElement is ICanvas canvas) { // TODO: Handle canvas offset / transform PaintGraphicsElements(canvas.Children); } else if (graphicsElement is IShape shape) { SKPath skiaPath = NonPathShapeToSkiaPath(shape); FillSkiaPath(skiaPath, shape); StrokeSkiaPath(skiaPath, shape); } else if (graphicsElement is IImage image) { IImageSource imageSource = image.Source; if (imageSource is ILoadableImageSource loadableImageSource) { LoadingStatus loadingStatus = loadableImageSource.LoadingStatus; if (loadingStatus == LoadingStatus.NotStarted) { _imageLoader.LoadImage(loadableImageSource, null); } else if (loadingStatus == LoadingStatus.Succeeded) { LoadedImage loadedImage = loadableImageSource.LoadedImage !; if (loadedImage is BitmapLoadedImage bitmapLoadedImage) { SKBitmap bitmap = (SKBitmap)bitmapLoadedImage.ImageObject; // TODO: Render it } else if (loadedImage is VectorLoadedImage vectorLoadedImage) { XCanvas childCanvas = vectorLoadedImage.Canvas; PaintGraphicsElements(childCanvas.Children); } } } } else { throw new InvalidOperationException($"Unknown graphics element type {graphicsElement.GetType()}"); } if (renderTransform != null) { skCanvas.Restore(); } } }