// Disposer public virtual void Dispose() { // Not already disposed? if (!isdisposed) { lock (this) { // Clean up if (bitmap != null) { bitmap.Dispose(); } if (texture != null) { texture.Dispose(); } bitmap = null; texture = null; // Done usedinmap = false; imagestate = ImageLoadState.None; previewstate = ImageLoadState.None; isdisposed = true; } } }
protected Task OnPhotoLoadingStateChange(ImageLoadState imageLoadState) { _isImageLoaded = (imageLoadState == ImageLoadState.Loaded); _isImageError = (imageLoadState == ImageLoadState.Error); return(Task.CompletedTask); }
public void LoadImageNow() { if (imagestate != ImageLoadState.Ready) { imagestate = ImageLoadState.Loading; LoadImage(true); } }
protected async Task OnImageLoaded(EventArgs eventArgs) { await ComputeCoverStyleAsync(); if (Src != null) { imageLoadState = ImageLoadState.Loaded; await OnLoadingStateChange.InvokeAsync(imageLoadState); } StateHasChanged(); }
// This unloads the image public virtual void UnloadImage() { lock (this) { if (bitmap != null) { bitmap.Dispose(); } bitmap = null; imagestate = ImageLoadState.None; } }
public override async Task SetParametersAsync(ParameterView parameters) { string src; parameters.TryGetValue("Src", out src); if (this.Src != src) { imageLoadState = ImageLoadState.NotLoaded; } await base.SetParametersAsync(parameters); }
public override async Task SetParametersAsync(ParameterView parameters) { string src; parameters.TryGetValue("Src", out src); if (this.Src != src) { imageLoadState = ImageLoadState.NotLoaded; } //else if (hasRenderedOnce) // await ComputeCoverStyleAsync(); await base.SetParametersAsync(parameters); }
// Disposer public virtual void Dispose() { // Not already disposed? if (!isdisposed) { // Clean up loadedbitmap?.Dispose(); previewbitmap?.Dispose(); spritepreviewbitmap?.Dispose(); texture?.Dispose(); loadedbitmap = null; previewbitmap = null; spritepreviewbitmap = null; texture = null; // Done imagestate = ImageLoadState.None; previewstate = ImageLoadState.None; isdisposed = true; } }
// This requests loading the image protected virtual void LocalLoadImage() { BitmapData bmpdata = null; lock (this) { // Bitmap loaded successfully? if (bitmap != null) { // Bitmap has incorrect format? if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { if (dynamictexture) { throw new Exception("Dynamic images must be in 32 bits ARGB format."); } //General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed."); Bitmap oldbitmap = bitmap; try { // Convert to desired pixel format bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.PageUnit = GraphicsUnit.Pixel; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; g.PixelOffsetMode = PixelOffsetMode.None; g.Clear(Color.Transparent); g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height); g.Dispose(); oldbitmap.Dispose(); } catch (Exception e) { bitmap = oldbitmap; General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } } // This applies brightness correction on the image if (usecolorcorrection) { try { // Try locking the bitmap bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception e) { General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } // Bitmap locked? if (bmpdata != null) { // Apply color correction PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer()); General.Colors.ApplColorCorrection(pixels, bmpdata.Width * bmpdata.Height); bitmap.UnlockBits(bmpdata); } } } else { // Loading failed // We still mark the image as ready so that it will // not try loading again until Reload Resources is used loadfailed = true; bitmap = new Bitmap(Properties.Resources.Failed); } if (bitmap != null) { width = bitmap.Size.Width; height = bitmap.Size.Height; if (dynamictexture) { if ((width != General.NextPowerOf2(width)) || (height != General.NextPowerOf2(height))) { throw new Exception("Dynamic images must have a size in powers of 2."); } } // Do we still have to set a scale? if ((scale.x == 0.0f) && (scale.y == 0.0f)) { if ((General.Map != null) && (General.Map.Config != null)) { scale.x = General.Map.Config.DefaultTextureScale; scale.y = General.Map.Config.DefaultTextureScale; } else { scale.x = 1.0f; scale.y = 1.0f; } } } // Image is ready imagestate = ImageLoadState.Ready; } }
// This requests loading the image protected virtual void LocalLoadImage() { lock (this) { // Bitmap loaded successfully? if (bitmap != null) { // Bitmap has incorrect format? if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { if (dynamictexture) { throw new Exception("Dynamic images must be in 32 bits ARGB format."); } //General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed."); Bitmap oldbitmap = bitmap; try { // Convert to desired pixel format bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.PageUnit = GraphicsUnit.Pixel; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; g.PixelOffsetMode = PixelOffsetMode.None; g.Clear(Color.Transparent); g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height); g.Dispose(); oldbitmap.Dispose(); } catch (Exception e) { bitmap = oldbitmap; General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image \"" + name + "\" for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } } // This applies brightness correction on the image if (usecolorcorrection) { BitmapData bmpdata = null; try { // Try locking the bitmap bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception e) { General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image \"" + name + "\" for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } // Bitmap locked? if (bmpdata != null) { // Apply color correction PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer()); General.Colors.ApplyColorCorrection(pixels, bmpdata.Width * bmpdata.Height); bitmap.UnlockBits(bmpdata); } } } else { // Loading failed // We still mark the image as ready so that it will // not try loading again until Reload Resources is used loadfailed = true; bitmap = new Bitmap(Properties.Resources.Failed); } if (bitmap != null) { width = bitmap.Size.Width; height = bitmap.Size.Height; if (dynamictexture) { if ((width != General.NextPowerOf2(width)) || (height != General.NextPowerOf2(height))) { throw new Exception("Dynamic images must have a size in powers of 2."); } } // Do we still have to set a scale? if ((scale.x == 0.0f) && (scale.y == 0.0f)) { if ((General.Map != null) && (General.Map.Config != null)) { scale.x = General.Map.Config.DefaultTextureScale; scale.y = General.Map.Config.DefaultTextureScale; } else { scale.x = 1.0f; scale.y = 1.0f; } } //mxd. Calculate average color? if (General.Map != null && General.Map.Data != null && General.Map.Data.GlowingFlats != null && General.Map.Data.GlowingFlats.ContainsKey(longname) && General.Map.Data.GlowingFlats[longname].CalculateTextureColor) { BitmapData bmpdata = null; try { bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception e) { General.ErrorLogger.Add(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for glow color calculation. " + e.GetType().Name + ": " + e.Message); } if (bmpdata != null) { PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer()); int numpixels = bmpdata.Width * bmpdata.Height; uint r = 0; uint g = 0; uint b = 0; for (PixelColor *cp = pixels + numpixels - 1; cp >= pixels; cp--) { r += cp->r; g += cp->g; b += cp->b; // Also check alpha if (cp->a > 0 && cp->a < 255) { istranslucent = true; } else if (cp->a == 0) { ismasked = true; } } // Update glow data int br = (int)(r / numpixels); int bg = (int)(g / numpixels); int bb = (int)(b / numpixels); int max = Math.Max(br, Math.Max(bg, bb)); // Black can't glow... if (max == 0) { General.Map.Data.GlowingFlats.Remove(longname); } else { // That's how it's done in GZDoom (and I may be totally wrong about this) br = Math.Min(255, br * 153 / max); bg = Math.Min(255, bg * 153 / max); bb = Math.Min(255, bb * 153 / max); General.Map.Data.GlowingFlats[longname].Color = new PixelColor(255, (byte)br, (byte)bg, (byte)bb); General.Map.Data.GlowingFlats[longname].CalculateTextureColor = false; if (!General.Map.Data.GlowingFlats[longname].Fullbright) { General.Map.Data.GlowingFlats[longname].Brightness = (br + bg + bb) / 3; } } // Release the data bitmap.UnlockBits(bmpdata); } } //mxd. Check if the texture is translucent else if (!loadfailed) { BitmapData bmpdata = null; try { bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception e) { General.ErrorLogger.Add(ErrorType.Error, "Cannot lock image \"" + this.filepathname + "\" for translucency check. " + e.GetType().Name + ": " + e.Message); } if (bmpdata != null) { PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer()); int numpixels = bmpdata.Width * bmpdata.Height; for (PixelColor *cp = pixels + numpixels - 1; cp >= pixels; cp--) { // Check alpha if (cp->a > 0 && cp->a < 255) { istranslucent = true; } else if (cp->a == 0) { ismasked = true; } } // Release the data bitmap.UnlockBits(bmpdata); } } } // Image is ready imagestate = ImageLoadState.Ready; } }
protected Task OnImageError(EventArgs eventArgs) { imageLoadState = ImageLoadState.Error; return(OnLoadingStateChange.InvokeAsync(imageLoadState)); }
// This loads the image protected void LoadImage(bool notify) { if (imagestate == ImageLoadState.Ready && previewstate != ImageLoadState.Loading) { return; } // Do the loading LocalLoadResult loadResult = LocalLoadImage(); ConvertImageFormat(loadResult); MakeImagePreview(loadResult); MakeAlphaTestImage(loadResult); // Save memory by disposing the original image immediately if we only used it to load a preview image bool onlyPreview = false; if (imagestate != ImageLoadState.Loading) { loadResult.bitmap?.Dispose(); loadResult.bitmap = null; onlyPreview = true; } General.MainWindow.RunOnUIThread(() => { if (imagestate == ImageLoadState.Loading && !onlyPreview) { // Log errors and warnings foreach (LogMessage message in loadResult.messages) { General.ErrorLogger.Add(message.Type, message.Text); } if (loadResult.messages.Any(x => x.Type == ErrorType.Error)) { loadfailed = true; } loadedbitmap?.Dispose(); texture?.Dispose(); imagestate = ImageLoadState.Ready; loadedbitmap = loadResult.bitmap; alphatest = loadResult.alphatest; alphatestWidth = loadResult.alphatestWidth; alphatestHeight = loadResult.alphatestHeight; if (loadResult.uiThreadWork != null) { loadResult.uiThreadWork(); } } else { loadResult.bitmap?.Dispose(); } if (previewstate == ImageLoadState.Loading) { previewbitmap?.Dispose(); previewstate = ImageLoadState.Ready; previewbitmap = loadResult.preview; } else { loadResult.preview?.Dispose(); } }); // Notify the main thread about the change so that sectors can update their buffers if (notify) { if (this is SpriteImage || this is VoxelImage) { General.MainWindow.SpriteDataLoaded(this.Name); } else { General.MainWindow.ImageDataLoaded(this.name); } } }
// This requests loading the image protected virtual void LocalLoadImage() { BitmapData bmpdata = null; lock (this) { // Bitmap loaded successfully? if (bitmap != null) { // Bitmap has incorrect format? if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { // villsa if (General.Map.FormatInterface.InDoom64Mode) { ColorPalette ncp = bitmap.Palette; foreach (TextureIndexInfo tp in General.Map.Config.ThingPalettes) { // sprite contains alternate palette? if (General.Map.Data.ThingPalette.ContainsKey(tp.Title) && tp.Index == palindex) { Playpal pal = General.Map.Data.ThingPalette[tp.Title]; for (int i = 0; i < 255; i++) { // copy alternate palette over ncp.Entries[i] = pal[i].ToColor(); } } } } //General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed."); Bitmap oldbitmap = bitmap; try { // Convert to desired pixel format bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.PageUnit = GraphicsUnit.Pixel; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; g.PixelOffsetMode = PixelOffsetMode.None; g.Clear(Color.Transparent); g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height); g.Dispose(); oldbitmap.Dispose(); } catch (Exception e) { bitmap = oldbitmap; General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } } // This applies brightness correction on the image if (usecolorcorrection) { try { // Try locking the bitmap bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception e) { General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message); } // Bitmap locked? if (bmpdata != null) { // Apply color correction PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer()); General.Colors.ApplColorCorrection(pixels, bmpdata.Width * bmpdata.Height); bitmap.UnlockBits(bmpdata); } } } else { // Loading failed // We still mark the image as ready so that it will // not try loading again until Reload Resources is used loadfailed = true; bitmap = new Bitmap(Properties.Resources.Failed); } if (bitmap != null) { width = bitmap.Size.Width; height = bitmap.Size.Height; // Do we still have to set a scale? if ((scale.x == 0.0f) && (scale.y == 0.0f)) { if ((General.Map != null) && (General.Map.Config != null)) { scale.x = General.Map.Config.DefaultTextureScale; scale.y = General.Map.Config.DefaultTextureScale; } else { scale.x = 1.0f; scale.y = 1.0f; } } } // Image is ready imagestate = ImageLoadState.Ready; } }
private void LoadStateChanged(ImageLoadState imageLoadState) { ImageLoaded = imageLoadState != ImageLoadState.NotLoaded; }