public void AddTile(Rectangle bounds, FreeImageAlgorithmsBitmap fib)
        {
            if (fib == null)
            {
                return;
            }

            try
            {
                Rectangle imageBounds = TranslateVirtualRectangleToImageRectangle(bounds);

                if (this.xScaleFactor < 1.0f || this.yScaleFactor < 1.0f)
                {
                    fib.Rescale(imageBounds.Size, FREE_IMAGE_FILTER.FILTER_BILINEAR);
                }

                fib.ConvertToStandardType(true);

                if (this.forceGreyscale && !fib.IsGreyScale)
                {
                    fib.ConvertToGreyscale();
                }

                this.image.PasteFromTopLeft(fib, imageBounds.Location, this.blending);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Example #2
0
        public void TileImage(Tile tile)
        {
            lock (tile)
            {
                if (tile.Thumbnail == null)
                {
                    return;
                }

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

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

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

                Rectangle dstRect = new Rectangle(scaledPosition, scaledSize);

                FreeImageAlgorithmsBitmap thumb = null;

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

                thumb.ConvertToStandardType(true);

                if (thumb.IsGreyScale)
                {
                    thumb.LinearScaleToStandardType(MosaicWindow.MosaicInfo.ThumbnailScaleMinIntensity,
                                                    MosaicWindow.MosaicInfo.ThumbnailScaleMaxIntensity);

                    thumb.SetGreyLevelPalette();
                }

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

                thumb.Rescale(dstRect.Size, FREE_IMAGE_FILTER.FILTER_BOX);

                thumb.ConvertTo24Bits();

                this.bitmap.PasteFromTopLeft(thumb, dstRect.Location, this.imageViewer.BlendingEnabled);

                thumb.Dispose();
            }

            this.Invalidate();
        }
Example #3
0
        private void  OnTileCorrelationBegin(CorrelationTile tile, FreeImageAlgorithmsBitmap fib)
        {
            this.useCorrelationCheckBox.Checked = false;
            this.currentTile = tile;

            #if DEBUG
            FreeImageAlgorithmsBitmap fg = new FreeImageAlgorithmsBitmap(fib);

            fg.ConvertToStandardType(true);
            fg.ConvertTo24Bits();

            this.debugForm.TileImageView.Image = fg.ToBitmap();

            this.debugForm.BackgroundImageView.Refresh();
            this.debugForm.TileImageView.Refresh();

            fg.Dispose();
            #endif
        }
Example #4
0
        private FreeImageAlgorithmsBitmap TileImage(Tile tile)
        {
            FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap();

            fib.ConvertToStandardType(true);
            if (!fib.IsGreyScale)
            {
                if (this.channel == FREE_IMAGE_COLOR_CHANNEL.FICC_RGB)
                {
                    fib.ConvertToGreyscale();
                }
                else
                {
                    fib.GetChannel(this.channel);
                }
            }

            return(fib);
        }
Example #5
0
        public FreeImageAlgorithmsWPFImage(string filepath) : base()
        {
            this.FilePath = filepath;

            try
            {
                this.Fib = Tile.LoadFreeImageBitmapFromFile(filepath);
            }
            catch (Exception e)
            {
                throw;
            }

            ThumbnailFib = new FreeImageAlgorithmsBitmap(this.Fib);
            Tile.ResizeToThumbnail(ThumbnailFib);

            if (ThumbnailFib.IsGreyScale)
            {
                ThumbnailFib.LinearScaleToStandardType(0, 0);
            }
            else
            {
                ThumbnailFib.ConvertToStandardType(true);
            }

            Bitmap bitmap = ThumbnailFib.ToBitmap();

            System.Windows.Int32Rect rect = new System.Windows.Int32Rect();

            rect.X      = 0;
            rect.Y      = 0;
            rect.Width  = bitmap.Width;
            rect.Height = bitmap.Height;

            this.Source = ConvertGdiToWPF(bitmap, rect);
        }
Example #6
0
        private void SaveStitchedImage()
        {
            if (System.IO.Path.GetExtension(this.filePath) != ".ics")
            {
                MessageBox.Show("Saving in formats other than ics may result " +
                                "in a loss of infomation.", "Warning");
            }

            if (this.threadController != null)
            {
                this.threadController.ReportThreadStarted(this, "Started Image Export");
            }

            FreeImageAlgorithmsBitmap stitchedImage = this.Stitch(this.saveDialog.ExportWidth,
                                                                  this.saveDialog.ExportHeight);

            if (stitchedImage == null)
            {
                this.FileToLargeError();
                return;
            }

            try
            {
                if (System.IO.Path.GetExtension(this.filePath) != ".ics")
                {
                    if (stitchedImage.ImageType != FREE_IMAGE_TYPE.FIT_BITMAP)
                    {
                        stitchedImage.ConvertToStandardType(true);  // PRB convert takes double the memory, only do it when necessary
                    }
                    if (this.saveInfoPanel)
                    {
                        stitchedImage.Paste(this.infoBitmap, new Point(20, 20), 256);
                    }

                    //stitchedImage.SaveToFile(this.filePath);  // uses many converts and clones in FIA
                    stitchedImage.Save(this.filePath);  // PRB use a simpler native free_image save
                }
                else
                {
                    // infoBitmap is 8bit for greyscale or 24bit colour
                    // If we are saving a greyscale image with > 8 bits
                    // we need to scale the infoBitmap to the min and max
                    // possible values of the stitchedImage.
                    if (this.saveInfoPanel)
                    {
                        if (MosaicWindow.MosaicInfo.IsGreyScale && MosaicWindow.MosaicInfo.FreeImageType != this.infoBitmap.ImageType)
                        {
                            double min, max;
                            stitchedImage.FindMinMaxIntensity(out min, out max);
                            this.infoBitmap.StretchImageToType(MosaicWindow.MosaicInfo.FreeImageType, max);
                        }

                        stitchedImage.Paste(this.infoBitmap, new Point(20, 20));
                    }

                    IcsFile.SaveToFile(stitchedImage, this.filePath, true);

                    if (MosaicWindow.MosaicInfo.MetaData != null)
                    {
                        IcsFile icsFile = new IcsFile(this.filePath);

                        // We have to add the metadata to reflect the
                        // exported image.
                        //                       if(MosaicWindow.MosaicInfo.MetaData.ContainsKey("extents"))
                        {
                            string extentString = String.Format("{0:#.###e+000} {1:#.###e+000}",
                                                                this.saveDialog.NativeRegionWidth * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel,
                                                                this.saveDialog.NativeRegionHeight * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel);

                            MosaicWindow.MosaicInfo.MetaData["extents"] = extentString;
                            MosaicWindow.MosaicInfo.MetaData["units"]   = "um um";
                        }

//                        if (MosaicWindow.MosaicInfo.MetaData.ContainsKey("image physical_sizex"))
                        {
                            string extentString = String.Format("{0:#.###e+000}",
                                                                this.saveDialog.NativeRegionWidth * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel);

                            MosaicWindow.MosaicInfo.MetaData["image physical_sizex"] = extentString;
                        }

                        //                       if (MosaicWindow.MosaicInfo.MetaData.ContainsKey("image physical_sizey"))
                        {
                            string extentString = String.Format("{0:#.###e+000}",
                                                                this.saveDialog.NativeRegionHeight * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel);

                            MosaicWindow.MosaicInfo.MetaData["image physical_sizey"] = extentString;
                        }

//                        if (MosaicWindow.MosaicInfo.MetaData.ContainsKey("image sizex"))
                        {
                            MosaicWindow.MosaicInfo.MetaData["image sizex"] = stitchedImage.Width.ToString();
                        }

//                        if (MosaicWindow.MosaicInfo.MetaData.ContainsKey("image sizey"))
                        {
                            MosaicWindow.MosaicInfo.MetaData["image sizey"] = stitchedImage.Height.ToString();
                        }

                        //                       if (MosaicWindow.MosaicInfo.MetaData.ContainsKey("dimensions"))
                        {
                            string extentString = String.Format("{0} {1}",
                                                                stitchedImage.Width,
                                                                stitchedImage.Height);

                            MosaicWindow.MosaicInfo.MetaData["dimensions"] = extentString;
                        }

                        // Add some metadata
                        MosaicWindow.MosaicInfo.MetaData["processed by"] = String.Format("{0} {1}",
                                                                                         Application.ProductName, Application.ProductVersion);

                        icsFile.AppendHistory(MosaicWindow.MosaicInfo.MetaData);

                        // calculate the um per pixel scale of the saved image
                        double xscale = this.saveDialog.NativeRegionWidth * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel / stitchedImage.Width;
                        double yscale = this.saveDialog.NativeRegionHeight * MosaicWindow.MosaicInfo.OriginalMicronsPerPixel / stitchedImage.Height;

                        icsFile.SetNativeScale(0, 0.0, xscale, "microns");
                        icsFile.SetNativeScale(1, 0.0, yscale, "microns");

                        icsFile.Close();
                    }
                }

                stitchedImage.Dispose();

                if (this.infoBitmap != null)
                {
                    this.infoBitmap.Dispose();
                }

                if (this.threadController != null)
                {
                    this.threadController.ReportThreadCompleted(this, "Exported file", false);
                }

                this.Window.TileView.AllowMouseWheelZoom = true;
                this.Window.ToolStrip.Enabled            = true;
            }
            catch (FreeImageException)
            {
                this.FileToLargeError();

                stitchedImage.Dispose();

                //          this.Export();
            }
        }
Example #7
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();
        }