Example #1
0
        private void GetFrames(DMIState state, FreeImageBitmap img)
        {
            for (int i = 0; i < state.Frames; i++)
            {
                int[] dirs  = { Directions.SOUTH, Directions.NORTH, Directions.EAST, Directions.WEST, Directions.SOUTHEAST, Directions.SOUTHWEST, Directions.NORTHEAST, Directions.NORTHWEST };
                var   frame = new DMIFrame(state.GetDelay(i));
                for (int j = 0; j < state.Dir; j++)
                {
                    int dir = dirs[j];

                    if (_pixelX >= img.Width)
                    {
                        _pixelX  = 0;
                        _pixelY += StateHeight;
                    }
                    Bitmap frameBitmap;
#if (ASEPRITE_LOAD)
                    //                       string getString = "./in/FromAse/NabFinalParts/r_nabber" + state.colourStr + " (" + state.Name + ") " + Directions.DirToAse(dir) + ".png";
                    string          getString = "./in/FromAse/NabFinalParts/r_nabber" + state.colourStr + " (" + state.Name + ") " + Directions.DirToAse(dir) + ".png";
                    FreeImageBitmap inImage   = new FreeImageBitmap(getString);
                    frameBitmap = inImage.ToBitmap();
#else
                    frameBitmap = img.Copy(new Rectangle(_pixelX, _pixelY, StateWidth, StateHeight)).ToBitmap();
#endif
                    frame.Add(new DMIImageData(frameBitmap, dir));
                    _pixelX += StateWidth;
                }
                state.Add(frame);
            }
        }
Example #2
0
        public void makeSampleImage(FreeImageBitmap bitmap)
        {
            if (LoadLogo != null)
            {
                using (FreeImageBitmap logo = LoadLogo())
                {
                    float sizeRatio       = (float)bitmap.Width / logo.Width;
                    int   finalLogoWidth  = (int)(logo.Width * sizeRatio);
                    int   finalLogoHeight = (int)(logo.Height * sizeRatio);
                    int   currentY        = 0;
                    int   imageHeight     = bitmap.Height;
                    bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);

                    logo.Rescale(finalLogoWidth, finalLogoHeight, FREE_IMAGE_FILTER.FILTER_BILINEAR);
                    while (currentY < imageHeight)
                    {
                        int sectionHeight = logo.Height;
                        if (currentY + sectionHeight > imageHeight)
                        {
                            sectionHeight = imageHeight - currentY;
                        }
                        using (FreeImageBitmap section = bitmap.Copy(0, currentY, logo.Width, currentY + sectionHeight))
                        {
                            using (FreeImageBitmap logoComposite = logo.Copy(0, 0, logo.Width, sectionHeight))
                            {
                                logoComposite.Composite(false, null, section);
                                bitmap.Paste(logoComposite, 0, currentY, int.MaxValue);
                                currentY += finalLogoHeight;
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        public void writeText(FreeImageBitmap bitmap, string p, int fontSize)
        {
            p = p.Replace("\n", "<br/>");

            int width  = Math.Min(bitmap.Width, 2048);
            int height = Math.Min(bitmap.Height, 256);

            ImageBox     imageBox     = (ImageBox)Gui.Instance.createWidgetT("ImageBox", "ImageBox", 0, 0, width, height, Align.Default, "Overlapped", "TempImageBox");
            RocketWidget rocketWidget = null;

            try
            {
                imageBox.Visible = false;
                rocketWidget     = new RocketWidget(imageBox, true);

                var cornerColor = Engine.Color.FromARGB(bitmap.GetPixel(0, 0).ToArgb());
                rocketWidget.resized();
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(DocumentStart, cornerColor.ToHexString(), fontSize);
                sb.AppendFormat("<p>{0}</p>", p);
                sb.Append(DocumentEnd);

                var resourceLoader = new RocketAssemblyResourceLoader(this.GetType().Assembly);
                RocketInterface.Instance.FileInterface.addExtension(resourceLoader);

                var document = rocketWidget.Context.LoadDocumentFromMemory(sb.ToString());
                if (document != null)
                {
                    document.Show(ElementDocument.FocusFlags.NONE);
                }

                RocketInterface.Instance.FileInterface.removeExtension(resourceLoader);

                using (FreeImageBitmap copyrightBitmap = new FreeImageBitmap(imageBox.Width, imageBox.Height, PixelFormat.Format32bppArgb))
                {
                    rocketWidget.writeToGraphics(copyrightBitmap, new Rectangle(0, 0, copyrightBitmap.Width, copyrightBitmap.Height));
                    using (FreeImageBitmap background = bitmap.Copy(0, 0, Math.Min(copyrightBitmap.Width, bitmap.Width), Math.Min(copyrightBitmap.Height, bitmap.Height)))
                    {
                        background.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                        copyrightBitmap.Composite(false, null, background);
                        bitmap.Paste(copyrightBitmap, 0, 0, int.MaxValue);
                    }
                }
            }
            finally
            {
                //Using block does not work (wtf?), dispose manually
                if (rocketWidget != null)
                {
                    rocketWidget.Dispose();
                }
                Gui.Instance.destroyWidget(imageBox);
            }
        }
Example #4
0
        private void GetFrames(DMIState state, FreeImageBitmap img)
        {
            for (int i = 0; i < state.Frames; i++)
            {
                int[] dirs  = { Directions.SOUTH, Directions.NORTH, Directions.EAST, Directions.WEST, Directions.SOUTHEAST, Directions.SOUTHWEST, Directions.NORTHEAST, Directions.NORTHWEST };
                var   frame = new DMIFrame(state.GetDelay(i));
                for (int j = 0; j < state.Dir; j++)
                {
                    int dir = dirs[j];

                    if (_pixelX >= img.Width)
                    {
                        _pixelX  = 0;
                        _pixelY += StateHeight;
                    }

                    Bitmap frameBitmap;
                    frameBitmap = img.Copy(new Rectangle(_pixelX, _pixelY, StateWidth, StateHeight)).ToBitmap();
                    frame.Add(new DMIImageData(frameBitmap, dir));
                    _pixelX += StateWidth;
                }
                state.Add(frame);
            }
        }
Example #5
0
        private void GetFrames(DMIState state, FreeImageBitmap img)
        {
            for (int i = 0; i < state.Frames; i++)
            {
                int[] dirs = { Directions.SOUTH, Directions.NORTH, Directions.EAST, Directions.WEST, Directions.SOUTHEAST, Directions.SOUTHWEST, Directions.NORTHEAST, Directions.NORTHWEST };
                var frame = new DMIFrame(state.GetDelay(i));
                for (int j = 0; j < state.Dir; j++)
                {
                    int dir = dirs[j];

                    if (_pixelX >= img.Width)
                    {
                        _pixelX = 0;
                        _pixelY += StateHeight;
                    }

                    Bitmap frameBitmap;
                    frameBitmap = img.Copy(new Rectangle(_pixelX, _pixelY, StateWidth, StateHeight)).ToBitmap();
                    frame.Add(new DMIImageData(frameBitmap, dir));
                    _pixelX += StateWidth;
                }
                state.Add(frame);
            }
        }
Example #6
0
        private IEnumerable <IdleStatus> createRender(int finalWidth, int finalHeight, int aaMode, bool showWatermark, bool transparentBG, Engine.Color backColor, Camera cloneCamera, Vector3 position, Vector3 lookAt, float minNearDistance, float nearPlaneWorld, float nearFarLength, ImageRendererProperties properties, Action <FreeImageBitmap> renderingCompletedCallback)
        {
            FreeImageBitmap  bitmap       = null;
            OgreSceneManager sceneManager = controller.CurrentScene.getDefaultSubScene().getSimElementManager <OgreSceneManager>();

            if (sceneManager != null)
            {
                bool doGridRender;
                int  backBufferWidth;
                int  backBufferHeight;

                using (TexturePtr texture = createOgreTexture(finalWidth, finalHeight, aaMode, out doGridRender, out backBufferWidth, out backBufferHeight))
                {
                    if (texture != null)
                    {
                        using (HardwarePixelBufferSharedPtr pixelBuffer = texture.Value.getBuffer())
                        {
                            RenderTexture renderTexture = pixelBuffer.Value.getRenderTarget();
                            Camera        camera        = sceneManager.SceneManager.createCamera("__PictureCamera");
                            camera.setLodBias(cloneCamera.getLodBias());
                            camera.setUseRenderingDistance(cloneCamera.getUseRenderingDistance());
                            camera.setNearClipDistance(cloneCamera.getNearClipDistance());
                            camera.setFarClipDistance(cloneCamera.getFarClipDistance());
                            camera.setPolygonMode(cloneCamera.getPolygonMode());
                            camera.setRenderingDistance(cloneCamera.getRenderingDistance());
                            camera.setProjectionType(cloneCamera.getProjectionType());
                            camera.setFOVy(cloneCamera.getFOVy());

                            camera.setAutoAspectRatio(false);
                            camera.setAspectRatio((float)finalWidth / finalHeight);

                            SceneNode node = sceneManager.SceneManager.createSceneNode("__PictureCameraNode");
                            node.attachObject(camera);
                            node.setPosition(position);
                            sceneManager.SceneManager.getRootSceneNode().addChild(node);
                            camera.lookAt(lookAt);
                            Viewport viewport = renderTexture.addViewport(camera, 1, 0.0f, 0.0f, 1.0f, 1.0f);

                            if (properties.UseIncludePoint)
                            {
                                Matrix4x4 viewMatrix       = camera.getViewMatrix();
                                Matrix4x4 projectionMatrix = camera.getProjectionMatrix();
                                float     aspect           = camera.getAspectRatio();
                                float     fovy             = camera.getFOVy() * 0.5f;

                                float   distance  = SceneViewWindow.computeOffsetToIncludePoint(viewMatrix, projectionMatrix, properties.IncludePoint, aspect, fovy);
                                Vector3 direction = (position - lookAt).normalized();
                                node.setPosition(position - (direction * distance));
                                camera.lookAt(lookAt);
                            }

                            if (transparentBG)
                            {
                                backColor.a = 0.0f;
                            }

                            ViewportBackground bgViewport = null;
                            if (background != null)
                            {
                                bgViewport = new ViewportBackground("ImageRenderer", 0, background, renderTexture, false);
                                bgViewport.BackgroundColor = backColor;
                                bgViewport.Camera.setAutoAspectRatio(false);
                                bgViewport.Camera.setAspectRatio((float)finalWidth / finalHeight);
                            }
                            viewport.setBackgroundColor(backColor);
                            viewport.setOverlaysEnabled(false);
                            viewport.setClearEveryFrame(false);

                            if (properties.CustomizeCameraPosition != null)
                            {
                                properties.CustomizeCameraPosition(camera, viewport);
                            }

                            float near = CameraPositioner.computeNearClipDistance(camera.getDerivedPosition().length(), minNearDistance, nearPlaneWorld);
                            camera.setNearClipDistance(near);
                            camera.setFarClipDistance(near + nearFarLength);

                            if (doGridRender)
                            {
                                IEnumerable <IdleStatus> process = gridRender(finalWidth * aaMode, finalHeight * aaMode, backBufferWidth, backBufferHeight, aaMode, renderTexture, camera, bgViewport != null ? bgViewport.Camera : null, transparentBG, backColor,
                                                                              (product) =>
                                {
                                    bitmap = product;
                                });
                                foreach (IdleStatus idleStatus in process)
                                {
                                    yield return(idleStatus);
                                }
                            }
                            else
                            {
                                bitmap = simpleRender(backBufferWidth, backBufferHeight, aaMode, transparentBG, backColor, renderTexture);
                            }

                            if (showWatermark && LoadLogo != null)
                            {
                                using (FreeImageBitmap logo = LoadLogo())
                                {
                                    float imageFinalHeight = bitmap.Height * 0.0447f;
                                    float scale            = imageFinalHeight / logo.Height;
                                    float imageFinalWidth  = logo.Width * scale;
                                    if (imageFinalWidth > bitmap.Width)
                                    {
                                        imageFinalWidth  = bitmap.Width;
                                        scale            = imageFinalWidth / logo.Width;
                                        imageFinalHeight = logo.Height * scale;
                                    }

                                    logo.Rescale((int)imageFinalWidth, (int)imageFinalHeight, FREE_IMAGE_FILTER.FILTER_BILINEAR);
                                    //Have to composite the logo image first.
                                    using (FreeImageBitmap fullImageCorner = bitmap.Copy(0, bitmap.Height - (int)imageFinalHeight, (int)imageFinalWidth, bitmap.Height))
                                    {
                                        fullImageCorner.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                                        logo.Composite(false, null, fullImageCorner);
                                    }
                                    bitmap.Paste(logo, 0, bitmap.Height - (int)imageFinalHeight, int.MaxValue);
                                }
                            }

                            renderTexture.destroyViewport(viewport);
                            if (bgViewport != null)
                            {
                                bgViewport.Dispose();
                            }
                            sceneManager.SceneManager.getRootSceneNode().removeChild(node);
                            sceneManager.SceneManager.destroySceneNode(node);
                            sceneManager.SceneManager.destroyCamera(camera);

                            TextureManager.getInstance().remove(texture);
                        }
                    }
                    else
                    {
                        //An error making the render texture. Log it and return the error image.
                        Log.Error("Could not render image. Returning placeholder image. Reason: Could not create valid render to texture target.");
                        bitmap = new FreeImageBitmap(finalWidth, finalHeight);
                        bitmap.FillBackground(new RGBQUAD()
                        {
                            rgbRed = 255
                        });
                    }
                }
            }
            renderingCompletedCallback(bitmap);
            yield break;
        }
Example #7
0
        /// <summary>
        /// Write to the given graphics, if the widget texture is not large enough it will be
        /// resized temporarily and then sized back. So be careful with large destrects.
        /// </summary>
        /// <param name="g"></param>
        /// <param name="destRect"></param>
        public void writeToGraphics(FreeImageBitmap g, Rectangle destRect)
        {
            bool      changedSize  = false;
            IntSize2  originalSize = new IntSize2(imageBox.Width, imageBox.Height);
            float     cropRatio    = (float)imageBox.Width / destRect.Width;
            Rectangle srcRect      = new Rectangle(0, 0, imageBox.Width, (int)(destRect.Height * cropRatio));

            //Make sure the source image is large enough, if not resize.
            if (originalSize.Width < srcRect.Width || originalSize.Height < srcRect.Height)
            {
                imageBox.setSize(srcRect.Width, srcRect.Height);
                changedSize = true;
                resized();
                renderTexture.update();
            }
            else if (renderOneFrame)
            {
                renderTexture.update();
            }

            using (FreeImageBitmap fullBitmap = new FreeImageBitmap(currentTextureWidth, currentTextureHeight, BitmapFormat))
            {
                fullBitmap.copyFromRenderTarget(renderTexture, ogreTextureFormat);
                //Remove alpha
                //BitmapDataExtensions.SetAlpha(bmpData, 255);

                if (srcRect.Height > fullBitmap.Height)
                {
                    srcRect.Height = fullBitmap.Height;
                }

                using (FreeImageBitmap cropped = fullBitmap.Copy(srcRect))
                {
                    if (cropped != null)
                    {
                        cropped.Rescale(destRect.Width, destRect.Height, FREE_IMAGE_FILTER.FILTER_BILINEAR);
                        if (ogreTextureFormat == OgrePlugin.PixelFormat.PF_X8R8G8B8)
                        {
                            //Use the api to set the alpha channel to 255, this makes sure we dont carry over the x8 channel from ogre
                            using (var alpha = cropped.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA))
                            {
                                alpha.FillBackground(new RGBQUAD(new FreeImageAPI.Color()
                                {
                                    R = 255,
                                    G = 255,
                                    B = 255,
                                    A = 255
                                }));
                                cropped.SetChannel(alpha, FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA);
                            }
                        }
                        g.Paste(cropped, destRect.X, destRect.Y, int.MaxValue);
                    }
                }
            }

            if (changedSize)
            {
                imageBox.setSize(originalSize.Width, originalSize.Height);
                resized();
                renderTexture.update();
            }
        }