Example #1
0
        public static void PasteTile(FreeImageAlgorithmsBitmap dst,
                                     FreeImageAlgorithmsBitmap src, Point location, bool blending)
        {
            if (blending)
            {
                if (!dst.GradientBlendPasteFromTopLeft(src, location))
                {
                    string errorStr = String.Format(
                        "Can not paste freeimage. Dst image bpp {0}, Src image bpp {1}",
                        dst.ColorDepth, src.ColorDepth);

                    throw new FormatException(errorStr);
                }
            }
            else
            {
                if (!dst.PasteFromTopLeft(src, location))
                {
                    string errorStr = String.Format(
                        "Can not paste freeimage. Dst image bpp {0}, Src image bpp {1}",
                        dst.ColorDepth, src.ColorDepth);

                    throw new FormatException(errorStr);
                }
            }
        }
Example #2
0
        private void OnIdle(object sender, EventArgs e)
        {
            // Nothing todo.
            if (this.preventIdleProcessing == true)
            {
                return;
            }

            if (this.highResBitmapReady == true)
            {
                return;
            }

            if (this.imageSize.IsEmpty)
            {
                return;
            }

            if (this.highResBitmap == null)
            {
                return;
            }

            if (this.intermediateBitmapDrawn == true && this.NumberOfTilesExceedCacheThreshold)
            {
                return;
            }

            this.Cursor = Cursors.WaitCursor;

            Rectangle viewRect = ViewedImageSectionRect;

            this.idleTiles = Tile.GetTilesIntersectingRectangle(MosaicWindow.MosaicInfo.Items, viewRect);

            // Go through the tiles and draw them at the highest resolution.
            // This is where gradient blending should go also.
            if (this.highResBitmapReady == false && this.currentTileIdleProcess < this.idleTiles.Count)
            {
                Tile tile = this.idleTiles[this.currentTileIdleProcess];

                FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap();

                if (fib.IsGreyScale)
                {
                    fib.LinearScaleToStandardType(mosaicInfo.ScaleMinIntensity, mosaicInfo.ScaleMaxIntensity);

                    fib.SetGreyLevelPalette();
                }

                fib.ConvertTo24Bits();

                if (this.zoom < 1.0)
                {
                    fib.Rescale(new Size((int)(fib.Width * this.zoom), (int)(fib.Height * this.zoom)), FREE_IMAGE_FILTER.FILTER_BILINEAR);
                }

                Point pos = new Point();

                // We may be using the thumbnails to display so
                // we need to work out the scaled coordinates if the zoom is < 1.0
                // For 1.0 and above we draw in native res just the area we can see on the screen.
                if (this.zoom < 1.0f)
                {
                    pos = tile.Position;

                    pos.X = (int)(tile.Position.X - viewRect.Location.X + 0.5);
                    pos.Y = (int)(tile.Position.Y - viewRect.Location.Y + 0.5);

                    pos.X = (int)(this.zoom * pos.X);
                    pos.Y = (int)(this.zoom * pos.Y);
                }
                else
                {
                    pos.X = tile.Position.X - viewRect.Location.X;
                    pos.Y = tile.Position.Y - viewRect.Location.Y;
                }

                lastIdleTileRectangles.Add(new Rectangle(pos, fib.Size));

                if (highResBitmap.Bounds.IntersectsWith(new Rectangle(pos, fib.Size)))
                {
                    highResBitmap.PasteFromTopLeft(fib, pos, this.blendingEnabled);
                }

                this.highResBitmapIsReset = false;

                fib.Dispose();

                // We have completed the last tile
                if (this.currentTileIdleProcess == this.idleTiles.Count - 1)
                {
                    // We are finished with the highres tiles
                    // Draw the joins if neccessary
                    if (this.ShowJoins)
                    {
                        int i = 0;
                        foreach (Tile t in this.idleTiles)
                        {
                            highResBitmap.DrawColourRect(lastIdleTileRectangles[i++], Color.Red, 2);
                        }
                    }

                    lastIdleTileSize.Width  = (int)(Tile.GetHorizontalRangeOfTiles(idleTiles) * this.zoom);
                    lastIdleTileSize.Height = (int)(Tile.GetVerticalRangeOfTiles(idleTiles) * this.zoom);

                    this.Cursor             = Cursors.Default;
                    this.highResBitmapReady = true;
                }

                this.currentTileIdleProcess++;

                this.Invalidate();
            }
        }
Example #3
0
        private FreeImageAlgorithmsBitmap Stitch(int stitchWidth, int stitchHeight)
        {
            float zoom   = 1.0f;
            Point origin = new Point(0, 0);

            List <Tile> tiles = null;

            RoiTool roiPlugin = this.Window.GetTool("Region") as RoiTool;

            Rectangle roi = Rectangle.Empty;

            if (roiPlugin.Active == true)
            {
                roi = roiPlugin.TransformedRegionOfInterest;
            }

            if (roi != null && roi != Rectangle.Empty)
            {
                tiles = new List <Tile>();

                foreach (Tile tile in MosaicWindow.MosaicInfo.Items)
                {
                    if (roi.IntersectsWith(tile.Bounds))
                    {
                        tiles.Add(tile);
                    }
                }

                zoom   = (float)stitchWidth / (float)(roi.Width);
                origin = roi.Location;
            }
            else
            {
                tiles = new List <Tile>(MosaicWindow.MosaicInfo.Items);
                zoom  = (float)stitchWidth / (float)(MosaicWindow.MosaicInfo.TotalWidth);
            }

            // origin = Tile.GetOriginOfTiles(tiles);

            //int width = Tile.GetHorizontalRangeOfTiles(tiles);
            //int height = Tile.GetVerticalRangeOfTiles(tiles);

            int width, height;

            if (roi == Rectangle.Empty)
            {
                // Whole mosaic Width / Height
                width  = MosaicWindow.MosaicInfo.TotalWidth;
                height = MosaicWindow.MosaicInfo.TotalHeight;
            }
            else
            {
                width  = roi.Width;
                height = roi.Height;
            }

            FreeImageAlgorithmsBitmap section = null;

            try
            {
                section = new FreeImageAlgorithmsBitmap((int)(width * zoom), (int)(height * zoom),
                                                        MosaicWindow.MosaicInfo.FreeImageType, MosaicWindow.MosaicInfo.ColorDepth);
            }
            catch (FreeImageException)
            {
                return(null);
            }

            FreeImageAlgorithmsBitmap tmpBitmap = null;

            int count = 1;

            foreach (Tile tile in tiles)
            {
                Point position = tile.GetTilePositionRelativeToPoint(origin);

                position.X = (int)(position.X * zoom);
                position.Y = (int)(position.Y * zoom);

                try
                {
                    tmpBitmap = tile.LoadFreeImageBitmap((int)(tile.Width * zoom), (int)(tile.Height * zoom));
                }
                catch (FreeImageException e)
                {
                    MessageBox.Show(e.Message);
                }

                section.PasteFromTopLeft(tmpBitmap, position, this.Window.BlendingEnabled);

                tmpBitmap.Dispose();

                this.threadController.ReportThreadPercentage(this, "Saving Tiles",
                                                             count, tiles.Count);

                count++;
            }

            return(section);
        }
Example #4
0
        private void DrawIntermediateBitmap()
        {
            if (!this.intermediateBitmapDrawn)
            {
                this.threadController.ReportThreadStarted(this, "Started Zoom");

                float aspectRatio = (float)(this.mosaicInfo.TotalWidth) / this.mosaicInfo.TotalHeight;

                int scaledWidth  = Screen.PrimaryScreen.Bounds.Size.Width;
                int scaledHeight = (int)(scaledWidth / aspectRatio + 0.5);

                if (this.mosaicInfo.IsGreyScale)
                {
                    this.intermediateBitmap = new FreeImageAlgorithmsBitmap(scaledWidth, scaledHeight, 8);
                }
                else
                {
                    this.intermediateBitmap = new FreeImageAlgorithmsBitmap(scaledWidth, scaledHeight, 24);
                }

                float xscaleFactor = (float)scaledWidth / this.mosaicInfo.TotalWidth;
                float yscaleFactor = (float)scaledHeight / this.mosaicInfo.TotalHeight;

                if (this.intermediateBitmap == null)
                {
                    MessageBox.Show("Failed to create intermediate bitmap");
                }

                float scale = (float)scaledWidth / this.mosaicInfo.TotalWidth;

                Point scaledPosition = new Point();

                int count = 0;

                foreach (Tile tile in this.mosaicInfo.Items)
                {
                    if (tile.Thumbnail == null)
                    {
                        MessageBox.Show("Error thumnail is null");
                    }

                    //FreeImageAlgorithmsBitmap thumb = null;

                    //lock (tile.ThumbnailLock)
                    //{
                    //    thumb = new FreeImageAlgorithmsBitmap(tile.Thumbnail);
                    //}

                    FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap();

                    fib.Rescale(new Size((int)(xscaleFactor * fib.Width), (int)(yscaleFactor * fib.Height)), FREE_IMAGE_FILTER.FILTER_BILINEAR);

                    if (fib.IsGreyScale)
                    {
                        fib.LinearScaleToStandardType(mosaicInfo.ScaleMinIntensity, mosaicInfo.ScaleMaxIntensity);

                        fib.SetGreyLevelPalette();
                    }
                    else
                    {
                        fib.ConvertToStandardType(true);
                    }

                    if (fib.ImageType != FREE_IMAGE_TYPE.FIT_BITMAP)
                    {
                        MessageBox.Show("Failed to convert tile thumbnail to a standard type ?");
                    }

                    Size scaledSize = new Size((int)(tile.Width * xscaleFactor + 0.5),
                                               (int)(tile.Height * yscaleFactor + 0.5));

                    scaledPosition.X = (int)(tile.Position.X * xscaleFactor + 0.5);
                    scaledPosition.Y = (int)(tile.Position.Y * yscaleFactor + 0.5);

                    Rectangle dstRect = new Rectangle(scaledPosition, scaledSize);

                    intermediateBitmap.PasteFromTopLeft(fib, scaledPosition, this.BlendingEnabled);

                    fib.Dispose();

                    this.threadController.ReportThreadPercentage(this,
                                                                 "Performing Zoom", count++, this.tiles.Count);
                }
            }

            //intermediateBitmap.ConvertTo24Bits();

            this.intermediateBitmapDrawn = true;
            this.preventIdleProcessing   = false;
            this.dontDraw = false;

            this.threadController.ReportThreadCompleted(this, "Zoom Completed", false);

            this.Redraw();
            this.Invalidate();
        }