Beispiel #1
0
        private void allItemsInColorPaletteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var         mats  = Materials.List.Where(x => x.IsEnabled);
            var         map   = new int[10, 1 + (mats.Count() / 10)];
            BlueprintPA print = new BlueprintPA()
            {
                MaxDepth = 1,
            };

            SchemFormatter.writeBlueprint("./io.schem", print);
        }
Beispiel #2
0
        public async Task RenderImageAndShowIt()
        {
            await TaskManager.Get.StartAsync((token) =>
            {
                TaskManager.SafeReport(0, "Optimizing...");

                PreRenderImage(false, token);

                if (this.PreRenderedImage != null)
                {
                    if (this.PreRenderedImage.Height > 256 && Options.Get.IsSideView)
                    {
                        MessageBox.Show("Minecraft cannot support images larger than 256 blocks in height.");
                        return;
                    }
                }
                TaskManager.SafeReport(0, "Getting Blueprint...");
                BlueprintPA blueprint = BlueprintPA.GetBluePrintAsync(token, this.PreRenderedImage).GetAwaiter().GetResult();
                TaskManager.SafeReport(0, "Rendering Blueprint...");
                Bitmap renderedImage = RenderedImagePanel.RenderBitmapFromBlueprint(token, blueprint, out int?textureSize);
                if (textureSize == null)
                {
                    return;
                }
                if (renderedImage == null)
                {
                    return;
                }

                try
                {
                    MainForm.Self.InvokeEx(x =>
                    {
                        x.LoadedBlueprint = blueprint;
                        x.ShowRenderedImagePanel();
                        x.renderedImagePanel.SetBluePrint(blueprint, renderedImage, textureSize);
                        TaskManager.SafeReport(0, "Finished.");
                    });
                }
                catch (OperationCanceledException) { }
            });
        }
        public void SetBluePrint(BlueprintPA src, Bitmap renderedImage, int?textureSize)
        {
            this.image = src;

            #region Calculate size
            this.CalculatedTextureSize = textureSize ?? Constants.TextureSize;
            this.renderedImage.DisposeSafely();
            this.renderedImage = renderedImage;
            #endregion

            bool preserveZoom = (MainForm.PanZoomSettings != null);
            if (!preserveZoom)
            {
                var settings = new PanZoomSettings()
                {
                    initialImageX = 0,
                    initialImageY = 0,
                    imageX        = 0,
                    imageY        = 0,
                    zoomLevel     = 0,
                    maxZoomLevel  = 100.0D,
                    minZoomLevel  = 0.0D,
                };

                bool isv     = true;
                int  mWidth  = src.Mapper.GetXLength(isv);
                int  mHeight = isv ? src.Mapper.GetYLength(isv) : src.Mapper.GetZLength(isv);

                double wRatio = (double)Width / mWidth;
                double hRatio = (double)Height / mHeight;
                if (hRatio < wRatio)
                {
                    settings.zoomLevel = hRatio;
                    settings.imageX    = (Width - (int)(mWidth * hRatio)) / 2;
                }
                else
                {
                    settings.zoomLevel = wRatio;
                    settings.imageY    = (Height - (int)(mHeight * wRatio)) / 2;
                }

                int numICareAbout = Math.Max(mWidth, mHeight);
                settings.minZoomLevel = (100.0D / numICareAbout);
                if (settings.minZoomLevel > 1.0D)
                {
                    settings.minZoomLevel = 1.0D;
                }

                MainForm.PanZoomSettings = settings;
            }

            //renderedImage.DisposeSafely();
            //renderedImage = null;
            Refresh();

            //var rndrWorker = BackgroundWorkerHelper.CreateWorker("RenderedImagePanel_RenderBitmap",
            //    (object sender, DoWorkEventArgs workArgs) => {
            //        var worker = sender as BackgroundWorker;
            //        RenderBitmap(worker);
            //    }, (object sender, RunWorkerCompletedEventArgs args) => {
            //        Refresh();
            //    });
        }
        public static Bitmap RenderBitmapFromBlueprint(CancellationToken?worker, BlueprintPA blueprint, out int?textureSize)
        {
            // TODO: Make sure this value is saved to the render panel instance somehow or else there will be horrible issues
            textureSize = CalculateTextureSize(blueprint);
            if (textureSize == null)
            {
                return(null);
            }

            if (blueprint != null)
            {
                bool isSelectiveLayerViewEnabled = Options.Get.IsEnabled(Constants.RenderedZIndexFilter, false);

                bool   isSide = Options.Get.IsSideView;
                double origW  = blueprint.Width;
                double origH  = blueprint.Height;
                int    w      = (int)(origW * MainForm.PanZoomSettings.zoomLevel);
                int    h      = (int)(origH * MainForm.PanZoomSettings.zoomLevel);
                int    zoom   = (int)(MainForm.PanZoomSettings.zoomLevel);


                SolidBrush brush = new SolidBrush(Color.Black);
                Pen        pen   = new Pen(brush);

                bool   isv     = true;
                int    mWidth  = blueprint.Mapper.GetXLength(isv);
                int    mHeight = isv ? blueprint.Mapper.GetYLength(isv) : blueprint.Mapper.GetZLength(isv);
                int    mDepth  = isv ? blueprint.Mapper.GetZLength(isv) : blueprint.Mapper.GetYLength(isv);
                int    calcW   = mWidth * textureSize.Value;
                int    calcH   = mHeight * textureSize.Value;
                Bitmap bm      = new Bitmap(
                    width: calcW,
                    height: calcH,
                    format: PixelFormat.Format32bppArgb);

                bool _IsSolidColors        = Options.Get.Rendered_IsSolidColors;
                bool _IsColorPalette       = Options.Get.Rendered_IsColorPalette;
                bool _IsMultiLayer         = Options.Get.IsMultiLayer;
                int  _RenderedZIndexToShow = Options.Get.Rendered_RenderedZIndexToShow;

                using (Graphics gImg = Graphics.FromImage(bm))
                {
                    gImg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    gImg.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
                    gImg.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.Half;

                    TaskManager.SafeReport(0, "Rendering to materials display...");
                    for (int x = 0; x < mWidth; x++)
                    {
                        TaskManager.SafeReport(100 * x / mWidth);
                        worker?.SafeThrowIfCancellationRequested();
                        for (int y = 0; y < mHeight; y++)
                        {
                            for (int z = 0; z < mDepth; z++)
                            {
                                if (isSelectiveLayerViewEnabled)
                                {
                                    if (z != _RenderedZIndexToShow)
                                    {
                                        continue;
                                    }
                                }

                                int xi = x * textureSize.Value;
                                int yi = y * textureSize.Value;
                                if (xi + MainForm.PanZoomSettings.zoomLevel >= 0 && yi + MainForm.PanZoomSettings.zoomLevel >= 0)
                                {
                                    Material m = blueprint.Mapper.GetMaterialAt(isv, x, y, z);

                                    if (m.BlockID != 0)
                                    {
                                        if (_IsSolidColors)
                                        {
                                            brush.Color = blueprint.GetColor(x, y);
                                            gImg.FillRectangle(brush, xi, yi, textureSize.Value, textureSize.Value);
                                        }
                                        else if (_IsColorPalette)
                                        {
                                            brush.Color = blueprint.GetColor(x, y);
                                            gImg.DrawImage(m.getImage(isSide), xi, yi, textureSize.Value, textureSize.Value);
                                            gImg.FillRectangle(brush, xi, yi, textureSize.Value / 2, textureSize.Value / 2);
                                            brush.Color = Color.Black;
                                            gImg.DrawRectangle(pen, xi, yi, textureSize.Value / 2, textureSize.Value / 2);
                                        }
                                        else
                                        {
                                            gImg.DrawImage(m.getImage(isSide), xi, yi, textureSize.Value, textureSize.Value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return(bm);
            }

            return(null);
        }
        private static int?CalculateTextureSize(BlueprintPA image)
        {
            int CalculatedTextureSize = Constants.TextureSize;

            // Calculate texture size so we can handle large images.
            if (image == null)
            {
                return(CalculatedTextureSize);
            }
            int mbSize  = (image.Width * image.Height * 32 / 8); // Still need to multiply by texture size
            int maxSize = 400 * 1024 * 1024;                     // max size in bytes we'll want to allow.
            int tSize   = 16;

            if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;                                      // 16
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;                                           // 14
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;                                           // 10
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;                                           // 6
            }
            else if (mbSize * tSize * tSize-- <= maxSize)
            {
                CalculatedTextureSize = tSize + 1;
            }
            else
            {
                CalculatedTextureSize = 4;
            }

            while (CalculatedTextureSize * image.Width > 22000 || CalculatedTextureSize * image.Height > 22000)
            {
                if (CalculatedTextureSize <= 1)
                {
                    MessageBox.Show(Constants.ERR_DownsizeYourImage);
                    MainForm.Self.PreRenderedImage.DisposeSafely();
                    MainForm.Self.PreRenderedImage = null;
                    return(null);
                }

                CalculatedTextureSize -= 2;
            }
            bool isSuccess = false;

            do
            {
                try
                {
                    int numMegaBytes = 32 / 8 * CalculatedTextureSize * CalculatedTextureSize * image.Height * image.Width / 1024 / 1024;
                    if (numMegaBytes > 0)
                    {
                        using (var memoryCheck = new System.Runtime.MemoryFailPoint(numMegaBytes))
                        {
                        }
                    }

                    isSuccess = true;
                }
                catch (InsufficientMemoryException)
                {
                    CalculatedTextureSize = Math.Max(1, CalculatedTextureSize - 2);
                }
            } while (isSuccess == false && CalculatedTextureSize > 1);

            if (isSuccess == false)
            {
                MessageBox.Show(Constants.ERR_DownsizeYourImage);
                MainForm.Self.PreRenderedImage.DisposeSafely();
                MainForm.Self.PreRenderedImage = null;
                return(null);
            }

            return(CalculatedTextureSize);
        }