// This makes a preview for the given image and updates the image settings private void MakeImagePreview(ImageData img) { lock (img) { // Load image if needed if (!img.IsImageLoaded) { img.LoadImage(); } int imagewidth, imageheight; Bitmap image = img.GetBitmap(); //mxd Bitmap preview; lock (image) { if (!img.LoadFailed) { imagewidth = img.Width; imageheight = img.Height; } else { Size size = image.Size; //mxd imagewidth = size.Width; imageheight = size.Height; } // Determine preview size float scalex = (img.Width > MAX_PREVIEW_SIZE) ? (MAX_PREVIEW_SIZE / (float)imagewidth) : 1.0f; float scaley = (img.Height > MAX_PREVIEW_SIZE) ? (MAX_PREVIEW_SIZE / (float)imageheight) : 1.0f; float scale = Math.Min(scalex, scaley); int previewwidth = (int)(imagewidth * scale); int previewheight = (int)(imageheight * scale); if (previewwidth < 1) { previewwidth = 1; } if (previewheight < 1) { previewheight = 1; } //mxd. Expected and actual image sizes and format match? if (previewwidth == imagewidth && previewheight == imageheight && image.PixelFormat == IMAGE_FORMAT) { preview = new Bitmap(image); } else { // Make new image preview = new Bitmap(previewwidth, previewheight, IMAGE_FORMAT); Graphics g = Graphics.FromImage(preview); g.PageUnit = GraphicsUnit.Pixel; //g.CompositingQuality = CompositingQuality.HighQuality; //mxd g.InterpolationMode = InterpolationMode.NearestNeighbor; //g.SmoothingMode = SmoothingMode.HighQuality; //mxd g.PixelOffsetMode = PixelOffsetMode.None; //g.Clear(Color.Transparent); //mxd // Draw image onto atlas Rectangle atlasrect = new Rectangle(0, 0, previewwidth, previewheight); RectangleF imgrect = General.MakeZoomedRect(new Size(imagewidth, imageheight), atlasrect); if (imgrect.Width < 1.0f) { imgrect.X -= 0.5f - imgrect.Width * 0.5f; imgrect.Width = 1.0f; } if (imgrect.Height < 1.0f) { imgrect.Y -= 0.5f - imgrect.Height * 0.5f; imgrect.Height = 1.0f; } g.DrawImage(image, imgrect); g.Dispose(); } } // Unload image if no longer needed if (!img.IsReferenced) { img.UnloadImage(); } lock (images) { // Set numbers img.PreviewIndex = images.Count; img.PreviewState = ImageLoadState.Ready; // Add to previews list images.Add(preview); } } }
// This loads the image protected override void LocalLoadImage() { // Checks if (this.IsImageLoaded || width == 0 || height == 0) { return; } Graphics g = null; lock (this) { // Create texture bitmap try { if (bitmap != null) { bitmap.Dispose(); } bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); PixelColor *pixels = (PixelColor *)bitmapdata.Scan0.ToPointer(); General.ZeroMemory(new IntPtr(pixels), width * height * sizeof(PixelColor)); bitmap.UnlockBits(bitmapdata); g = Graphics.FromImage(bitmap); } catch (Exception e) { // Unable to make bitmap General.ErrorLogger.Add(ErrorType.Error, "Unable to load texture image \"" + this.Name + "\". " + e.GetType().Name + ": " + e.Message); loadfailed = true; } int missingpatches = 0; //mxd if (patches.Count == 0) //mxd { //mxd. Empty image will suffice here, I suppose... if (nulltexture) { base.LocalLoadImage(); return; } // No patches! General.ErrorLogger.Add(ErrorType.Warning, "No patches are defined for texture \"" + this.Name + "\""); loadfailed = true; } else if (!loadfailed) { // Go for all patches foreach (TexturePatch p in patches) { //mxd. Some patches (like "TNT1A0") should be skipped if (p.Skip) { continue; } // Get the patch data stream string patchlocation = string.Empty; //mxd Stream patchdata = General.Map.Data.GetPatchData(p.LumpName, p.HasLongName, ref patchlocation); if (patchdata != null) { // Copy patch data to memory byte[] membytes = new byte[(int)patchdata.Length]; lock (patchdata) //mxd { patchdata.Seek(0, SeekOrigin.Begin); patchdata.Read(membytes, 0, (int)patchdata.Length); } MemoryStream mem = new MemoryStream(membytes); mem.Seek(0, SeekOrigin.Begin); // Get a reader for the data IImageReader reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMPICTURE, General.Map.Data.Palette); if (reader is UnknownImageReader) { //mxd. Probably that's a flat?.. if (General.Map.Config.MixTexturesFlats) { reader = ImageDataFormat.GetImageReader(mem, ImageDataFormat.DOOMFLAT, General.Map.Data.Palette); } if (reader is UnknownImageReader) { // Data is in an unknown format! if (!nulltexture) { General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Patch lump \"" + Path.Combine(patchlocation, p.LumpName) + "\" data format could not be read, while loading texture \"" + this.Name + "\""); } missingpatches++; //mxd } } if (!(reader is UnknownImageReader)) { // Get the patch mem.Seek(0, SeekOrigin.Begin); Bitmap patchbmp = null; try { patchbmp = reader.ReadAsBitmap(mem); } catch (InvalidDataException) { // Data cannot be read! if (!nulltexture) { General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Patch lump \"" + p.LumpName + "\" data format could not be read, while loading texture \"" + this.Name + "\""); } missingpatches++; //mxd } if (patchbmp != null) { //mxd. Apply transformations from TexturePatch patchbmp = TransformPatch(p, patchbmp); // Draw the patch on the texture image Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height); g.DrawImageUnscaledAndClipped(patchbmp, tgtrect); patchbmp.Dispose(); } } // Done mem.Dispose(); } else { //mxd. ZDoom can use any known graphic as patch if (General.Map.Config.MixTexturesFlats) { ImageData img = General.Map.Data.GetTextureImage(p.LumpName); if (!(img is UnknownImage) && img != this) { if (!img.IsImageLoaded) { img.LoadImage(); } //mxd. Apply transformations from TexturePatch. We don't want to modify the original bitmap here, so make a copy Bitmap bmp = img.GetBitmap(); Bitmap patchbmp; lock (bmp) { patchbmp = TransformPatch(p, new Bitmap(bmp)); } // Draw the patch on the texture image Rectangle tgtrect = new Rectangle(p.X, p.Y, patchbmp.Size.Width, patchbmp.Size.Height); g.DrawImageUnscaledAndClipped(patchbmp, tgtrect); patchbmp.Dispose(); continue; } } // Missing a patch lump! if (!nulltexture) { General.ErrorLogger.Add((optional ? ErrorType.Warning : ErrorType.Error), "Missing patch lump \"" + p.LumpName + "\" while loading texture \"" + this.Name + "\""); } missingpatches++; //mxd } } } // Dispose bitmap if load failed if (!nulltexture && (bitmap != null) && (loadfailed || missingpatches >= patches.Count)) //mxd. We can still display texture if at least one of the patches was loaded { bitmap.Dispose(); bitmap = null; loadfailed = true; } // Pass on to base base.LocalLoadImage(); } }
// This makes a preview for the given image and updates the image settings private void MakeImagePreview(ImageData img) { int previewwidth, previewheight; int imagewidth, imageheight; Bitmap preview; Graphics g; lock (img) { // Load image if needed if (!img.IsImageLoaded) { img.LoadImage(); } if (!img.LoadFailed) { imagewidth = img.Width; imageheight = img.Height; } else { imagewidth = img.GetBitmap().Size.Width; imageheight = img.GetBitmap().Size.Height; } // Determine preview size float scalex = (img.Width > maxpreviewwidth) ? ((float)maxpreviewwidth / (float)imagewidth) : 1.0f; float scaley = (img.Height > maxpreviewheight) ? ((float)maxpreviewheight / (float)imageheight) : 1.0f; float scale = Math.Min(scalex, scaley); previewwidth = (int)((float)imagewidth * scale); previewheight = (int)((float)imageheight * scale); if (previewwidth < 1) { previewwidth = 1; } if (previewheight < 1) { previewheight = 1; } // Make new image preview = new Bitmap(previewwidth, previewheight, IMAGE_FORMAT); g = Graphics.FromImage(preview); g.PageUnit = GraphicsUnit.Pixel; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.None; g.Clear(Color.Transparent); // Draw image onto atlas Rectangle atlasrect = new Rectangle(0, 0, previewwidth, previewheight); RectangleF imgrect = General.MakeZoomedRect(new Size(imagewidth, imageheight), atlasrect); if (imgrect.Width < 1.0f) { imgrect.X -= 0.5f - imgrect.Width * 0.5f; imgrect.Width = 1.0f; } if (imgrect.Height < 1.0f) { imgrect.Y -= 0.5f - imgrect.Height * 0.5f; imgrect.Height = 1.0f; } g.DrawImage(img.GetBitmap(), imgrect); g.Dispose(); // Unload image if no longer needed if (!img.IsReferenced) { img.UnloadImage(); } lock (images) { // Set numbers img.PreviewIndex = images.Count; img.PreviewState = ImageLoadState.Ready; // Add to previews list images.Add(preview); } } }