public override void Perform(Pixmap pixmap, PreviewImageQuery query)
        {
            Point2 desiredSize = new Point2(query.DesiredWidth, query.DesiredHeight);
            Point2 cropToSize  = new Point2(4096, 4096);

            PixelData layer = pixmap.MainLayer;

            if (layer == null)
            {
                query.Result = new Bitmap(1, 1);
                return;
            }

            // If the desired preview is way smaller than the source data, specify a crop size
            if (layer.Width > desiredSize.X * 8)
            {
                cropToSize.X = Math.Min(cropToSize.X, desiredSize.X * 8);
            }
            if (layer.Height > desiredSize.Y * 8)
            {
                cropToSize.Y = Math.Min(cropToSize.Y, desiredSize.Y * 8);
            }

            // If out image is too big, crop it
            if (layer.Width > cropToSize.X || layer.Height > cropToSize.Y)
            {
                layer = layer.CloneSubImage(
                    layer.Width / 2 - MathF.Min(layer.Width, cropToSize.X) / 2,
                    layer.Height / 2 - MathF.Min(layer.Height, cropToSize.Y) / 2,
                    MathF.Min(layer.Width, cropToSize.X),
                    MathF.Min(layer.Height, cropToSize.Y));
            }

            // Determine the target size for the preview based on desired and actual size
            Point2 targetSize;
            float  widthRatio = (float)layer.Width / (float)MathF.Max(layer.Height, 1);

            if (query.SizeMode == PreviewSizeMode.FixedWidth)
            {
                targetSize = new Point2(desiredSize.X, MathF.RoundToInt(desiredSize.X / widthRatio));
            }
            else if (query.SizeMode == PreviewSizeMode.FixedHeight)
            {
                targetSize = new Point2(MathF.RoundToInt(widthRatio * desiredSize.Y), desiredSize.Y);
            }
            else
            {
                targetSize = desiredSize;
            }

            // Create a properly resized version of the image data
            if (layer.Width != targetSize.X || layer.Height != targetSize.Y)
            {
                layer = layer.CloneRescale(targetSize.X, targetSize.Y, ImageScaleFilter.Linear);
            }

            query.Result = layer.ToBitmap();
        }
Пример #2
0
        public override void Perform(Pixmap pixmap, PreviewImageQuery query)
        {
            int desiredWidth  = query.DesiredWidth;
            int desiredHeight = query.DesiredHeight;

            PixelData 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, ImageScaleFilter.Linear);
                }
            }
            else if (query.SizeMode == PreviewSizeMode.FixedBoth)
            {
                layer = layer.CloneRescale(desiredWidth, desiredHeight, ImageScaleFilter.Linear);
            }
            else if (query.SizeMode == PreviewSizeMode.FixedWidth)
            {
                layer = layer.CloneRescale(desiredWidth, MathF.RoundToInt(desiredWidth / widthRatio), ImageScaleFilter.Linear);
            }
            else if (query.SizeMode == PreviewSizeMode.FixedHeight)
            {
                layer = layer.CloneRescale(MathF.RoundToInt(widthRatio * desiredHeight), desiredHeight, ImageScaleFilter.Linear);
            }
            else
            {
                layer = layer.Clone();
            }

            query.Result = layer.ToBitmap();
        }
        public override void Perform(Sprite sprite, PreviewImageQuery query)
        {
            int desiredWidth  = query.DesiredWidth;
            int desiredHeight = query.DesiredHeight;

            var pixMap = sprite.Material.Res?.MainTexture.Res?.BasePixmap.Res;

            PixelData data = pixMap?.MainLayer;

            if (data == null)
            {
                return;
            }
            float widthRatio = (float)data.Width / (float)data.Height;

            data = data.CloneSubImage((int)sprite.UVRect.X, (int)sprite.UVRect.Y, (int)sprite.UVRect.W, (int)sprite.UVRect.H);
            //data.Rescale(desiredWidth, (int)desiredHeight * widthRatio);
            query.Result = data.ToBitmap();
        }
Пример #4
0
        public override void Perform(Tileset tileset, PreviewImageQuery query)
        {
            int desiredWidth  = query.DesiredWidth;
            int desiredHeight = query.DesiredHeight;

            TilesetRenderInput input      = tileset.RenderConfig.FirstOrDefault(c => c.SourceData != null);
            Pixmap             mainPixmap = (input != null) ? input.SourceData.Res : null;
            PixelData          layer      = (mainPixmap != null) ? mainPixmap.MainLayer : null;

            if (layer == null)
            {
                query.Result = new Bitmap(1, 1);
                return;
            }
            float widthRatio = (float)layer.Width / (float)layer.Height;

            layer = layer.CloneSubImage(0, 0, desiredWidth * 2, desiredHeight * 2);
            layer.Rescale(desiredWidth, desiredHeight);

            query.Result = layer.ToBitmap();
        }