public override void Perform(Pixmap pixmap, PreviewImageQuery query) { int desiredWidth = query.DesiredWidth; int desiredHeight = query.DesiredHeight; Pixmap.Layer layer = pixmap.MainLayer; if (layer == null) { query.Result = new Bitmap(1, 1); return; } float widthRatio = (float)layer.Width / (float)layer.Height; if (pixmap.Width * pixmap.Height > 4096 * 4096) { layer = layer.CloneSubImage( pixmap.Width / 2 - Math.Min(desiredWidth, pixmap.Width) / 2, pixmap.Height / 2 - Math.Min(desiredHeight, pixmap.Height) / 2, Math.Min(desiredWidth, pixmap.Width), Math.Min(desiredHeight, pixmap.Height)); if (layer.Width != desiredWidth || layer.Height != desiredHeight) { layer = layer.CloneRescale(desiredWidth, desiredHeight, Pixmap.FilterMethod.Linear); } } else if (query.SizeMode == PreviewSizeMode.FixedBoth) { layer = layer.CloneRescale(desiredWidth, desiredHeight, Pixmap.FilterMethod.Linear); } else if (query.SizeMode == PreviewSizeMode.FixedWidth) { layer = layer.CloneRescale(desiredWidth, MathF.RoundToInt(desiredWidth / widthRatio), Pixmap.FilterMethod.Linear); } else if (query.SizeMode == PreviewSizeMode.FixedHeight) { layer = layer.CloneRescale(MathF.RoundToInt(widthRatio * desiredHeight), desiredHeight, Pixmap.FilterMethod.Linear); } else { layer = layer.Clone(); } query.Result = layer.ToBitmap(); }
/// <summary> /// Loads the specified <see cref="Duality.Resources.Pixmap">Pixmaps</see> pixel data. /// </summary> /// <param name="basePixmap">The <see cref="Duality.Resources.Pixmap"/> that is used as pixel data source.</param> /// <param name="sizeMode">Specifies behaviour in case the source data has non-power-of-two dimensions.</param> public void LoadData(ContentRef <Pixmap> basePixmap, SizeMode sizeMode) { DualityApp.GuardSingleThreadState(); if (this.glTexId == 0) { this.glTexId = GL.GenTexture(); } this.needsReload = false; this.basePixmap = basePixmap; this.texSizeMode = sizeMode; int lastTexId; GL.GetInteger(GetPName.TextureBinding2D, out lastTexId); GL.BindTexture(TextureTarget.Texture2D, this.glTexId); if (!this.basePixmap.IsExplicitNull) { Pixmap.Layer pixelData = null; Pixmap basePixmapRes = this.basePixmap.IsAvailable ? this.basePixmap.Res : null; if (basePixmapRes != null) { pixelData = basePixmapRes.MainLayer; this.atlas = basePixmapRes.Atlas != null?basePixmapRes.Atlas.ToArray() : null; } if (pixelData == null) { pixelData = Pixmap.Checkerboard.Res.MainLayer; } this.AdjustSize(pixelData.Width, pixelData.Height); this.SetupOpenGLRes(); if (this.texSizeMode != SizeMode.NonPowerOfTwo && (this.pxWidth != this.texWidth || this.pxHeight != this.texHeight)) { if (this.texSizeMode == SizeMode.Enlarge) { Pixmap.Layer oldData = pixelData; pixelData = oldData.CloneResize(this.texWidth, this.texHeight); // Fill border pixels manually - that's cheaper than ColorTransparentPixels here. oldData.DrawOnto(pixelData, BlendMode.Solid, this.pxWidth, 0, 1, this.pxHeight, this.pxWidth - 1, 0); oldData.DrawOnto(pixelData, BlendMode.Solid, 0, this.pxHeight, this.pxWidth, 1, 0, this.pxHeight - 1); } else { pixelData = pixelData.CloneRescale(this.texWidth, this.texHeight, Pixmap.FilterMethod.Linear); } } // Load pixel data to video memory GL.TexImage2D(TextureTarget.Texture2D, 0, this.pixelformat, pixelData.Width, pixelData.Height, 0, GLPixelFormat.Rgba, PixelType.UnsignedByte, pixelData.Data); // Adjust atlas to represent UV coordinates if (this.atlas != null) { Vector2 scale; scale.X = this.uvRatio.X / this.pxWidth; scale.Y = this.uvRatio.Y / this.pxHeight; for (int i = 0; i < this.atlas.Length; i++) { this.atlas[i].X *= scale.X; this.atlas[i].W *= scale.X; this.atlas[i].Y *= scale.Y; this.atlas[i].H *= scale.Y; } } } else { this.atlas = null; this.AdjustSize(this.size.X, this.size.Y); this.SetupOpenGLRes(); } GL.BindTexture(TextureTarget.Texture2D, lastTexId); }