Exemplo n.º 1
0
        private void CreateCurveForGreylevelImageLineData(Point start, Point end)
        {
            TileView viewer = this.mosaicWindow.TileView;

            List <Tile> tiles = viewer.GetVisibleTiles();

            if (tiles == null)
            {
                return;
            }

            if (MosaicWindow.MosaicInfo.ColorDepth >= 24)
            {
                return;
            }

            int max_pixels = Math.Abs((end.X - start.X)) + Math.Abs((end.Y - start.Y));

            double[] values = new double[max_pixels];

            int len = 0;

            foreach (Tile tile in tiles)
            {
                if (tile.IsDummy)
                {
                    continue;
                }

                FreeImageAlgorithmsBitmap dib = tile.LoadFreeImageBitmap();

                len = dib.GetGreyScalePixelValuesAsDoublesForLine(start, end, out values);

                dib.Dispose();
            }

            PointPairList list = new PointPairList();

            double x, y;

            for (int i = 0; i < len; i++)
            {
                x = (double)i;
                y = (double)values[i];
                list.Add(x, y);
            }

            if (this.curve != null)
            {
                this.zedGraphControl.GraphPane.CurveList.Remove(this.curve);
            }

            this.curve = new LineItem("Profile", list, Color.Black, SymbolType.None);

            this.zedGraphControl.GraphPane.CurveList.Insert(0, curve);

            this.zedGraphControl.AxisChange();
        }
Exemplo n.º 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();
        }
Exemplo n.º 3
0
        private void DrawThumbnailImage(Graphics graphics, Tile tile)
        {
            if (tile.Thumbnail != null)
            {
                lock (tile.ThumbnailLock)
                {
                    //tile.Thumbnail.ConvertToStandardType();
                    FreeImageAlgorithmsBitmap thumb = new FreeImageAlgorithmsBitmap(tile.Thumbnail);

                    if (thumb.IsGreyScale)
                    {
                        thumb.LinearScaleToStandardType(
                            mosaicInfo.ThumbnailScaleMinIntensity, mosaicInfo.ThumbnailScaleMaxIntensity);
                        thumb.SetGreyLevelPalette();
                    }

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

                    graphics.DrawImage(thumb.ToBitmap(), tile.Bounds);

                    thumb.Dispose();
                }
            }

            /*
             * if (this.ShowFileNames)
             * {
             *  // Create font and brush.
             *  Font drawFont = new Font("Arial", 16);
             *  SolidBrush drawBrush = new SolidBrush(Color.Black);
             *
             *  Point location = new Point(tile.Bounds.Location.X + 100, tile.Bounds.Location.Y + 100);
             *
             *  SizeF size = graphics.MeasureString(tile.FileName, drawFont);
             *
             *  Brush brush = new SolidBrush(Color.FromArgb(50, Color.Gray));
             *
             *  graphics.FillRectangle(brush, location.X - 2, location.Y - 2,
             *      size.Width + 4, size.Height + 4);
             *
             *  graphics.DrawString(tile.FileName, drawFont, drawBrush,
             *      new PointF(location.X, location.Y));
             * }
             */

            if (this.ShowJoins)
            {
                Pen pen = new Pen(Color.Red, 2.0f);
                graphics.DrawRectangle(pen, tile.Bounds);
            }
        }
Exemplo n.º 4
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
        }
Exemplo n.º 5
0
        private void OnTileCorrelated(CorrelationTile tile, Rectangle bounds, FreeImageAlgorithmsBitmap fib, bool success)
        {
            this.tiledImageViewer.AddTile(bounds, fib);

            this.tiledImageViewer.Refresh();

            #if DEBUG
            if (success == false)
            {
                FreeImageAlgorithmsBitmap bg = new FreeImageAlgorithmsBitmap(this.correlator.BackgroundImage);
                bg.ConvertTo24Bits();
                bg.DrawColourRect(tile.OriginalBoundsRelativeToOrigin, Color.Red, 2);
                this.debugForm.BackgroundImageView.Image = bg.ToBitmap();
                bg.Dispose();
            }

            this.debugForm.Refresh();
            #endif
        }
Exemplo n.º 6
0
        private void DrawPreviouslyPlacedTiles(CorrelationTile tile)
        {
            this.BackgroundImage.Clear();

            // Paste the previous placed images (correlated) onto the temporary (bg) image
            foreach (CorrelationTile t in this.CorrelationTiles)
            {
                if (t.PerformedCorrelation == false)
                {
                    continue;
                }

                if (!t.BoundsRelativeToMosaic.IntersectsWith(tile.BoundsRelativeToMosaic))
                {
                    continue;
                }

                //               FreeImageAlgorithmsBitmap fib = Tile.LoadFreeImageBitmapFromFile(t.Tile.FilePath);
                //               fib.ConvertToStandardType(true);
                FreeImageAlgorithmsBitmap fib = TileImage(t.Tile);

                // Paste first image. We blend as it may improve the correlation.
                try
                {
                    this.BackgroundImage.PasteFromTopLeft(fib, t.CorrelatedBoundsRelativeToOrigin.Location, true);
                }
                catch (FreeImageException)
                {
                    this.SendFeedback("Tile positions do not intersect for pasting together" + Environment.NewLine
                                      , Color.Red);
                }
                finally
                {
                    fib.Dispose();
                }
            }
        }
Exemplo n.º 7
0
        private void SaveScreenShot()
        {
            string filePath = Utilities.SaveDialog(true);

            if (filePath == null)
            {
                return;
            }

            Bitmap   bitmap = this.imageView.ScreenBitmap;
            Graphics g      = Graphics.FromImage(bitmap);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

            OnSavingScreenShot(g, new MosaicWindowEventArgs(MosaicWindow.MosaicInfo));

            FreeImageAlgorithmsBitmap fib = new FreeImageAlgorithmsBitmap(bitmap);

            fib.ConvertTo24Bits();

            g.Dispose();

            bitmap.Dispose();

            string extension = Path.GetExtension(filePath);

            if (extension != ".ics")
            {
                fib.SaveToFile(filePath);
            }
            else
            {
                IcsFile.SaveToFile(fib, filePath, true);
            }

            fib.Dispose();
        }
        protected override void ReadHeader(TileLoadInfo info)
        {
            // TODO check all seqs have the same format

            int count = 0;
            List <TilePosition> tilePositions = new List <TilePosition>();

            string prefix = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "file format", "");
            string prefix2 = "", prefix3 = "";  // may not have 3 files, but should have 2

            if (this.FilePaths.Length > 1)
            {
                prefix2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "file format", "");
                Tile.nCompositeImages = 2;
            }
            if (this.FilePaths.Length > 2)
            {
                prefix3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "file format", "");
                Tile.nCompositeImages = 3;
            }

            string iniValue, iniValue2, iniValue3;

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "roi left", "0.0");
            double roiLeft = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "roi top", "0.0");
            double roiTop = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Roi Height", "0.0");
            double roiHeight = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Horizontal Overlap", "0.0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Horizontal Overlap", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Horizontal Overlap.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Horizontal Overlap", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Horizontal Overlap.");
                }
            }
            // This is overlap in %
            decimal overlapX = Convert.ToDecimal(iniValue);

            info.OverLapPercentageX = (double)overlapX;

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Vertical Overlap", "0.0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Vertical Overlap", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Vertical Overlap.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Vertical Overlap", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Vertical Overlap.");
                }
            }
            // This is overlap in %
            decimal overlapY = Convert.ToDecimal(iniValue);

            info.OverLapPercentageY = (double)overlapY;

            iniValue           = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Horizontal Frames", "0.0");
            info.WidthInTiles  = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);
            iniValue           = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Vertical Frames", "0.0");
            info.HeightInTiles = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Max Intensity", "0.0");
            info.TotalMinIntensity = 0;
            info.TotalMaxIntensity = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);
            info.ScaleMinIntensity = info.TotalMinIntensity;
            info.ScaleMaxIntensity = info.TotalMaxIntensity;

/*            Tile.TotalMaxIntensity[0] = info.TotalMaxIntensity;
 *          iniValue = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Max Intensity", "0.0");
 *          Tile.TotalMaxIntensity[1] = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);
 *          if (Tile.nCompositeImages == 3)
 *          {
 *              iniValue = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Max Intensity", "0.0");
 *              Tile.TotalMaxIntensity[2] = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);
 *          }
 */
            string extension  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Extension", ".ics");
            string extension2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Extension", ".ics");

            if (extension != extension2)
            {
                throw new MosaicReaderException("Seq files have different File types.");
            }
            if (Tile.nCompositeImages == 3)
            {
                string extension3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Extension", ".ics");
                if (extension != extension3)
                {
                    throw new MosaicReaderException("Seq files have different File types.");
                }
            }

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Width", "0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Tile Width", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Tile Width.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Tile Width", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Tile Width.");
                }
            }
            int tileWidth = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Height", "0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Tile Height", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Tile Height.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Tile Height", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Tile Height.");
                }
            }
            int tileHeight = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Bits Per Pixel", "0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Tile Bits Per Pixel", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Tile Bits Per Pixel.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Tile Bits Per Pixel", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Tile Bits Per Pixel.");
                }
            }
            info.ColorDepth = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Image Type", "0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Tile Image Type", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Tile Image Type.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Tile Image Type", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Tile Image Type.");
                }
            }
            int tileImageType = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue  = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Pixels Per Micron", "1.0");
            iniValue2 = IniParser.IniFile.GetIniFileString(this.FilePaths[1], info_ini_name, "Pixels Per Micron", "0.0");
            if (iniValue != iniValue2)
            {
                throw new MosaicReaderException("Seq files have different Pixels Per Micron.");
            }
            if (Tile.nCompositeImages == 3)
            {
                iniValue3 = IniParser.IniFile.GetIniFileString(this.FilePaths[2], info_ini_name, "Pixels Per Micron", "0.0");
                if (iniValue != iniValue3)
                {
                    throw new MosaicReaderException("Seq files have different Pixels Per Micron.");
                }
            }
            info.OriginalPixelsPerMicron = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            // This file format provides a list of tiles and their position relative
            // to the left, top of the first region of interest.

            // these prefix's are needed below, but let's ask for the prefix for the composite mosaic
            TextStringDialog nameDialog = new TextStringDialog();

            nameDialog.Text        = "Composite Mosaic Name";
            nameDialog.Description = "Please enter a name for this composite Mosaic.";
            nameDialog.TextString  = prefix + "_" + prefix2 + "_" + prefix3;
            //          if (Tile.nCompositeImages==3)
            //            nameDialog.TextString.
            nameDialog.ShowDialog();
            this.dataSetName      = nameDialog.TextString;
            Tile.channelPrefix[0] = prefix;
            Tile.channelPrefix[1] = prefix2;
            Tile.channelPrefix[2] = prefix3;
            info.Prefix           = this.dataSetName;

            DirectoryInfo dir = new DirectoryInfo(this.DirectoryPath);

            FileInfo[] filesInDir = dir.GetFiles(prefix + "*" + extension);

            count = info.WidthInTiles * info.HeightInTiles;

            Tile[] validTiles = new Tile[count];

            Tile.IsCompositeRGB = true;
            info.IsCompositeRGB = Tile.IsCompositeRGB;

            // Check whether all the filenames we have are valid
            for (int i = 1; i <= count; i++)
            {
                string filename = BuildExpectedFilename(prefix, i, extension);
                string fullpath = this.DirectoryPath + Path.DirectorySeparatorChar + filename;

                validTiles[i - 1] = new Tile(fullpath, i, tileWidth, tileHeight);
            }

            int             width  = 0;
            int             height = 0;
            int             bpp    = 24; // always 24 bit colour for this reader
            FREE_IMAGE_TYPE type   = FREE_IMAGE_TYPE.FIT_BITMAP;
            int             max_posible_intensity = 256;

            {                                                                                    // pick a tile from the middle of the stack to get some info, this is a single channel
                Tile tile = validTiles[validTiles.Length / 2];
                FreeImageAlgorithmsBitmap fib = Tile.LoadFreeImageBitmapFromFile(tile.FilePath); // use fn that loads fib directly from filename as tiles are not properly set up yet
                width  = fib.Width;
                height = fib.Height;
                // bpp = fib.ColorDepth;
                // type = fib.ImageType;
                max_posible_intensity = Utilities.guessFibMaxValue(fib);
                fib.Dispose();
            }

            foreach (Tile tile in validTiles)
            {
                if (this.ThreadController.ThreadAborted)
                {
                    return;
                }

                Point position = new Point();

                int row, col;

                row = ((tile.TileNumber - 1) / info.WidthInTiles) + 1;

                if ((row % 2) > 0)
                {
                    // Odd row reversed order
                    col = ((tile.TileNumber - 1) % info.WidthInTiles) + 1;
                }
                else
                {
                    // Even row nornal order
                    col = info.WidthInTiles - ((tile.TileNumber - 1) % info.WidthInTiles);
                }

                decimal overlapXInpixels = (overlapX / 100.0M) * (decimal)tileWidth;
                decimal overlapYInpixels = (overlapY / 100.0M) * (decimal)tileHeight;

                position.X = (int)Math.Round(((decimal)width - overlapXInpixels) * ((decimal)col - 1.0M));
                position.Y = (int)Math.Round(((decimal)height - overlapYInpixels) * ((decimal)row - 1.0M));

                tile.OriginalPosition = position;
                tile.Width            = width;
                tile.Height           = height;
                tile.ColorDepth       = bpp;
                tile.FreeImageType    = type;

                // multi seq specific stuff

                string filename = BuildExpectedFilename(prefix2, tile.TileNumber, extension);
                tile.setFileName(1, filename);

                if (Tile.nCompositeImages == 3)
                {
                    filename = BuildExpectedFilename(prefix3, tile.TileNumber, extension);
                    tile.setFileName(2, filename);
                }

                info.Items.Add(tile);
            }

            Tile.scaleMin[0] = 0.0;
            Tile.scaleMax[0] = (double)max_posible_intensity;
            Tile.scaleMin[1] = 0.0;
            Tile.scaleMax[1] = (double)max_posible_intensity;
            Tile.scaleMin[2] = 0.0;
            Tile.scaleMax[2] = (double)max_posible_intensity;

            Tile.channelShift[0] = new Point(0, 0);
            Tile.channelShift[1] = new Point(0, 0);
            Tile.channelShift[2] = new Point(0, 0);

            // check all files
            bool oneNotFound = false, atLeastOneFound = false;

            foreach (Tile tile in validTiles)
            {
                if (System.IO.File.Exists(tile.FilePath))
                {
                    if (System.IO.File.Exists(tile.getFilePath(1)))
                    {
                        if (Tile.nCompositeImages == 3)
                        {
                            if (System.IO.File.Exists(tile.getFilePath(2)))
                            {
                                atLeastOneFound = true;
                            }
                            else
                            {
                                oneNotFound = true;
                            }
                        }
                        else
                        {
                            atLeastOneFound = true;
                        }
                    }
                    else
                    {
                        oneNotFound = true;
                    }
                }
                else
                {
                    oneNotFound = true;
                }
            }

            if (!atLeastOneFound)  // No images at all!
            {
                throw (new MosaicReaderException("No tiles have all channels, images missing."));
            }

            if (oneNotFound)
            {
                MessageBox.Show("At least 1 image is missing from the mosaic.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemplo n.º 9
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);
        }
        protected override void ReadHeader(TileLoadInfo info)
        {
            int     count          = 0;
            decimal OverLapMicrons = 0M;

            FileInfo[]          filesInDir    = null;
            List <TilePosition> tilePositions = new List <TilePosition>();

            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(this.FilePath))
            {
                String   line;
                string[] fields;

                try
                {
                    // Read extents but don't use
                    line = sr.ReadLine();

                    // Get the overlap
                    line           = sr.ReadLine();
                    OverLapMicrons = Convert.ToDecimal(line);

                    // Get the number of frames in each direction
                    line   = sr.ReadLine();
                    fields = line.Split('\t');

                    info.WidthInTiles  = Convert.ToInt32(fields[0], CultureInfo.InvariantCulture);
                    info.HeightInTiles = Convert.ToInt32(fields[1], CultureInfo.InvariantCulture);

                    // Get the microns per pixel factor
                    line = sr.ReadLine();
                    info.OriginalPixelsPerMicron = Convert.ToDouble(line, CultureInfo.InvariantCulture);

                    // By definition Ros's TileLoadInfo files consist of correlated data.
                    info.IsCorrelated = true;

                    // Get the extension of the files
                    line = sr.ReadLine();

                    filesInDir = new FileInfo[info.NumberOfTiles];
                    count      = 0;

                    while ((line = sr.ReadLine()) != null)
                    {
                        fields = line.Split('\t');

                        tilePositions.Add(new TilePosition(fields[0],
                                                           Convert.ToInt32(fields[1], CultureInfo.InvariantCulture),
                                                           Convert.ToInt32(fields[2], CultureInfo.InvariantCulture)));

                        filesInDir[count++] = new FileInfo(this.DirectoryPath
                                                           + "\\" + fields[0]);
                    }
                }
                catch (IOException e)
                {
                    System.Windows.Forms.MessageBox.Show("Cannot parse file: " + e.Message);
                }
            }

            Tile.IsCompositeRGB = false;

            count = 0;
            int             width  = 0;
            int             height = 0;
            int             bpp    = 8;
            FREE_IMAGE_TYPE type   = FREE_IMAGE_TYPE.FIT_BITMAP;

            // Find the first tile that exists to get the sizes and color depth etc
            // but check all
            bool oneNotFound = false, atLeastOneFound = false;

            foreach (FileInfo file in filesInDir)
            {
                if (System.IO.File.Exists(file.FullName))
                {
                    if (!atLeastOneFound)
                    {
                        FreeImageAlgorithmsBitmap fib = Tile.LoadFreeImageBitmapFromFile(file.FullName);
                        width  = fib.Width;
                        height = fib.Height;
                        bpp    = fib.ColorDepth;
                        type   = fib.ImageType;
                        fib.Dispose();

                        // Set the overlap percentage
                        double widthInMicrions  = width / info.OriginalPixelsPerMicron;
                        double heightInMicrions = height / info.OriginalPixelsPerMicron;

                        info.OverLapPercentageX =
                            (double)((double)OverLapMicrons / widthInMicrions) * 100.0;

                        info.OverLapPercentageY =
                            (double)((double)OverLapMicrons / heightInMicrions) * 100.0;

                        atLeastOneFound = true;
                    }
                }
                else
                {
                    oneNotFound = true;
                }
            }

            if (!atLeastOneFound)  // No images at all!
            {
                throw (new MosaicReaderException("No images found."));
            }

            if (oneNotFound)
            {
                MessageBox.Show("At least 1 image is missing from the mosaic.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            foreach (FileInfo file in filesInDir)
            {
                if (this.ThreadController.ThreadAborted)
                {
                    return;
                }

                if (!this.AllowedExtensions.Contains(file.Extension))
                {
                    continue;
                }

                Point position = new Point();

                position.X = tilePositions[count].X;
                position.Y = tilePositions[count].Y;

                Tile tile = new Tile(file.FullName, position, width, height);

                tile.Width         = width;
                tile.Height        = height;
                tile.ColorDepth    = bpp;
                tile.FreeImageType = (FREE_IMAGE_TYPE)type;

                info.Items.Add(tile);

                count++;
            }
        }
Exemplo n.º 11
0
        public void UpdateGraphForLoadedImage()
        {
            TileView viewer = this.mosaicWindow.TileView;

//            CalculateGreyLevelHistogramShape();

            double min_in_images = Double.MaxValue, max_in_images = Double.MinValue;
            double max_posible_intensity = 0.0;

            if (MosaicWindow.MosaicInfo.IsColour)
            {
                return;
            }

            if (viewer.CacheImageAvailiable)
            {
                max_posible_intensity = viewer.CacheImage.MaximumPossibleIntensityValue;
                viewer.CacheImage.FindMinMaxIntensity(out min_in_images, out max_in_images);
            }
            else
            {
                List <Tile> tiles = viewer.GetVisibleTiles();

                if (tiles == null)
                {
                    return;
                }

                foreach (Tile tile in tiles)
                {
                    if (tile.IsDummy)
                    {
                        continue;
                    }

                    FreeImageAlgorithmsBitmap dib = tile.LoadFreeImageBitmap();

                    if (max_posible_intensity == 0.0)
                    {
                        max_posible_intensity = dib.MaximumPossibleIntensityValue;
                    }

                    double min;
                    double max;

                    dib.FindMinMaxIntensity(out min, out max);

                    if (min < min_in_images)
                    {
                        min_in_images = min;
                    }

                    if (max > max_in_images)
                    {
                        max_in_images = max;
                    }

                    dib.Dispose();
                }
            }

            this.min          = min_in_images;
            this.max          = max_in_images;
            this.max_possible = max_posible_intensity;

            this.minNumericUpDown.Minimum = Convert.ToDecimal(0);
            this.maxNumericUpDown.Minimum = Convert.ToDecimal(1);
            this.minNumericUpDown.Maximum = Convert.ToDecimal(this.max_possible - 1);
            this.maxNumericUpDown.Maximum = Convert.ToDecimal(this.max_possible);
            this.minNumericUpDown.Value   = Convert.ToDecimal(this.min);
            this.maxNumericUpDown.Value   = Convert.ToDecimal(this.max);

            this.zedGraphControl.GraphPane.XAxis.Scale.Min = 0;
            this.zedGraphControl.GraphPane.XAxis.Scale.Max = this.max;
            this.zedGraphControl.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphControl.GraphPane.YAxis.Scale.Max = 255;

            this.cursor1.Position = this.min;
            this.cursor2.Position = this.max;

            this.DisplayPlot();

            this.zedGraphControl.AxisChange();
        }
Exemplo n.º 12
0
        protected override void ReadHeader(TileLoadInfo info)
        {
            int     count          = 0;
            string  extension      = null;
            decimal OverLapMicrons = 0M;

            info.Prefix        = System.IO.Path.GetFileNameWithoutExtension(this.FilePath);
            info.DirectoryPath = System.IO.Path.GetDirectoryName(this.FilePath);

            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(this.FilePath))
            {
                String   line;
                string[] fields;

                try
                {
                    // Read extents but don't use
                    line = sr.ReadLine();

                    // Get the overlap in microns
                    line           = sr.ReadLine();
                    OverLapMicrons = Convert.ToDecimal(line);

                    // Get the number of frames in each direction
                    line   = sr.ReadLine();
                    fields = line.Split('\t');

                    info.WidthInTiles  = Convert.ToInt32(fields[0], CultureInfo.InvariantCulture);
                    info.HeightInTiles = Convert.ToInt32(fields[1], CultureInfo.InvariantCulture);

                    // Get the microns per pixel factor
                    line = sr.ReadLine();
                    info.OriginalPixelsPerMicron = Convert.ToDouble(line, CultureInfo.InvariantCulture);

                    // Get the extension of the files
                    extension = sr.ReadLine();
                }
                catch (IOException e)
                {
                    System.Windows.Forms.MessageBox.Show("Cannot parse file: " + e.Message);
                }
            }

            DirectoryInfo dir = new DirectoryInfo(this.DirectoryPath);

            FileInfo[] filesInDir = dir.GetFiles(info.Prefix + "*" + extension);

            count = info.WidthInTiles * info.HeightInTiles;

            Tile[] validTiles = new Tile[count];

            Tile.IsCompositeRGB = false;

            // Check whether all the filenames will have are valid
            for (int i = 1; i <= count; i++)
            {
                string filename = BuildExpectedFilename(info.Prefix, i, extension);
                string fullpath = this.DirectoryPath + System.IO.Path.DirectorySeparatorChar + filename;

                validTiles[i - 1] = new Tile(fullpath, i, 0, 0);
            }

            int             width  = 0;
            int             height = 0;
            int             bpp    = 8;
            FREE_IMAGE_TYPE type   = FREE_IMAGE_TYPE.FIT_BITMAP;


            // Find the first tile that exists to get the sizes and color depth etc
            // but check all
            bool oneNotFound = false, atLeastOneFound = false;

            foreach (Tile tile in validTiles)
            {
                if (System.IO.File.Exists(tile.FilePath))
                {
                    if (!atLeastOneFound)
                    {
                        FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap();
                        width  = fib.Width;
                        height = fib.Height;
                        bpp    = fib.ColorDepth;
                        type   = fib.ImageType;
                        fib.Dispose();

                        // Set the overlap percentage
                        double widthInMicrions  = width / info.OriginalPixelsPerMicron;
                        double heightInMicrions = height / info.OriginalPixelsPerMicron;

                        info.OverLapPercentageX =
                            (double)((double)OverLapMicrons / widthInMicrions) * 100.0;

                        info.OverLapPercentageY =
                            (double)((double)OverLapMicrons / heightInMicrions) * 100.0;
                        atLeastOneFound = true;
                    }
                }
                else
                {
                    oneNotFound = true;
                }
            }

            if (!atLeastOneFound)  // No images at all!
            {
                throw (new MosaicReaderException("No images found."));
            }

            if (oneNotFound)
            {
                MessageBox.Show("At least 1 image is missing from the mosaic.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            foreach (Tile tile in validTiles)
            {
                if (this.ThreadController.ThreadAborted)
                {
                    return;
                }

                Point position = new Point();

                int row, col;

                row = ((tile.TileNumber - 1) / info.WidthInTiles) + 1;

                if ((row % 2) > 0)
                {
                    // Odd row reversed order
                    col = ((tile.TileNumber - 1) % info.WidthInTiles) + 1;
                }
                else
                {
                    // Even row nornal order
                    col = info.WidthInTiles - ((tile.TileNumber - 1) % info.WidthInTiles);
                }

                // The original seq spec only allowed for one overlap in microns (not X and Y)
                decimal overlapXInpixels = ((decimal)info.OverLapPercentageX / 100.0M) * (decimal)width;
                decimal overlapYInpixels = ((decimal)info.OverLapPercentageY / 100.0M) * (decimal)height;

                position.X = (int)Math.Round(((decimal)width - overlapXInpixels) * ((decimal)col - 1.0M));
                position.Y = (int)Math.Round(((decimal)height - overlapYInpixels) * ((decimal)row - 1.0M));

                tile.OriginalPosition = position;
                tile.Width            = width;
                tile.Height           = height;
                tile.ColorDepth       = bpp;
                tile.FreeImageType    = type;

                info.Items.Add(tile);
            }
        }
Exemplo n.º 13
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();
            }
        }
Exemplo n.º 14
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();
        }
Exemplo n.º 15
0
        public void UpdateUIForLoadedImage()
        {
            TileView viewer = this.mosaicWindow.TileView;

/*
 *          CalculateGreyLevelHistogramShape(Tile.channel[0], hist_list1, Tile.TotalMaxIntensity[0]);
 *          if (this.histogramCurve1 != null)
 *              this.zedGraphControl1.GraphPane.CurveList.Remove(this.histogramCurve1);
 *          this.histogramCurve1 = this.zedGraphControl1.GraphPane.AddCurve("Histogram", hist_list1, Color.Black, SymbolType.None);
 *          this.histogramCurve1.Line.Fill = new Fill(Color.White, Color.LightSkyBlue, -45F);
 *
 *          CalculateGreyLevelHistogramShape(Tile.channel[1], hist_list2, Tile.TotalMaxIntensity[1]);
 *          if (this.histogramCurve2 != null)
 *              this.zedGraphControl2.GraphPane.CurveList.Remove(this.histogramCurve2);
 *          this.histogramCurve2 = this.zedGraphControl2.GraphPane.AddCurve("Histogram", hist_list2, Color.Black, SymbolType.None);
 *          this.histogramCurve2.Line.Fill = new Fill(Color.White, Color.LightSkyBlue, -45F);
 *
 *          CalculateGreyLevelHistogramShape(Tile.channel[2], hist_list3, Tile.TotalMaxIntensity[2]);
 *          if (this.histogramCurve3 != null)
 *              this.zedGraphControl3.GraphPane.CurveList.Remove(this.histogramCurve3);
 *          this.histogramCurve3 = this.zedGraphControl3.GraphPane.AddCurve("Histogram", hist_list3, Color.Black, SymbolType.None);
 *          this.histogramCurve3.Line.Fill = new Fill(Color.White, Color.LightSkyBlue, -45F);
 */

//            double min_in_images = Double.MaxValue, max_in_images = Double.MinValue;
            double max_posible_intensity = 0.0;

            if (!MosaicWindow.MosaicInfo.IsColour)
            {
                return;
            }

            // load 1 image to get the max possible grey value for the channels
            List <Tile> tiles = viewer.GetVisibleTiles();

            if (tiles == null)
            {
                return;
            }

            FreeImageAlgorithmsBitmap dib = Tile.LoadFreeImageBitmapFromFile(tiles[0].FilePath);  // get on e image from one of the channels

//            max_posible_intensity = dib.MaximumPossibleIntensityValue;
            max_posible_intensity = Utilities.guessFibMaxValue(dib);
            dib.Dispose();

            this.min          = 0;
            this.max          = max_posible_intensity;
            this.max_possible = max_posible_intensity;

            this.minNumericUpDown1.Minimum = Convert.ToDecimal(0);
            this.maxNumericUpDown1.Minimum = Convert.ToDecimal(1);
            this.minNumericUpDown1.Maximum = Convert.ToDecimal(this.max_possible - 1);
            this.maxNumericUpDown1.Maximum = Convert.ToDecimal(this.max_possible);
            this.minNumericUpDown2.Minimum = Convert.ToDecimal(0);
            this.maxNumericUpDown2.Minimum = Convert.ToDecimal(1);
            this.minNumericUpDown2.Maximum = Convert.ToDecimal(this.max_possible - 1);
            this.maxNumericUpDown2.Maximum = Convert.ToDecimal(this.max_possible);
            this.minNumericUpDown3.Minimum = Convert.ToDecimal(0);
            this.maxNumericUpDown3.Minimum = Convert.ToDecimal(1);
            this.minNumericUpDown3.Maximum = Convert.ToDecimal(this.max_possible - 1);
            this.maxNumericUpDown3.Maximum = Convert.ToDecimal(this.max_possible);

            this.minNumericUpDown1.Value = Convert.ToDecimal(Tile.scaleMin[0]);
            this.minNumericUpDown2.Value = Convert.ToDecimal(Tile.scaleMin[1]);
            this.minNumericUpDown3.Value = Convert.ToDecimal(Tile.scaleMin[2]);
            this.maxNumericUpDown1.Value = Convert.ToDecimal(Tile.scaleMax[0]);
            this.maxNumericUpDown2.Value = Convert.ToDecimal(Tile.scaleMax[1]);
            this.maxNumericUpDown3.Value = Convert.ToDecimal(Tile.scaleMax[2]);

            this.numericUpDown1X.Value = Convert.ToDecimal(Tile.channelShift[1].X);
            this.numericUpDown1Y.Value = Convert.ToDecimal(Tile.channelShift[1].Y);
            this.numericUpDown2X.Value = Convert.ToDecimal(Tile.channelShift[2].X);
            this.numericUpDown2Y.Value = Convert.ToDecimal(Tile.channelShift[2].Y);

            this.channelTitle1.Text = Tile.channelPrefix[0];
            this.channelTitle2.Text = Tile.channelPrefix[1];
            this.channelTitle3.Text = Tile.channelPrefix[2];

            setComboBoxForFICC(this.comboBox1, Tile.channel[0]);
            setComboBoxForFICC(this.comboBox2, Tile.channel[1]);
            setComboBoxForFICC(this.comboBox3, Tile.channel[2]);

            this.zedGraphControl1.GraphPane.XAxis.Scale.Min = 0;
            this.zedGraphControl1.GraphPane.XAxis.Scale.Max = this.max;
            this.zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphControl1.GraphPane.YAxis.Scale.Max = 255;
            this.zedGraphControl2.GraphPane.XAxis.Scale.Min = 0;
            this.zedGraphControl2.GraphPane.XAxis.Scale.Max = this.max;
            this.zedGraphControl2.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphControl2.GraphPane.YAxis.Scale.Max = 255;
            this.zedGraphControl3.GraphPane.XAxis.Scale.Min = 0;
            this.zedGraphControl3.GraphPane.XAxis.Scale.Max = this.max;
            this.zedGraphControl3.GraphPane.YAxis.Scale.Min = 0;
            this.zedGraphControl3.GraphPane.YAxis.Scale.Max = 255;

            this.cursor1_1.Position = Tile.scaleMin[0];
            this.cursor1_2.Position = Tile.scaleMax[0];
            this.cursor2_1.Position = Tile.scaleMin[1];
            this.cursor2_2.Position = Tile.scaleMax[1];
            this.cursor3_1.Position = Tile.scaleMin[2];
            this.cursor3_2.Position = Tile.scaleMax[2];

            this.DisplayPlot();

            this.zedGraphControl1.AxisChange();
            this.zedGraphControl2.AxisChange();
            this.zedGraphControl3.AxisChange();
        }
Exemplo n.º 16
0
        public FreeImageAlgorithmsBitmap LoadFreeImageBitmap(string filepath)
        {   // all overloaded functions come through this method
            FreeImageAlgorithmsBitmap fib = null;

            if (!System.IO.File.Exists(filepath))
            {
                this.dummyTile = true;
                return(new FreeImageAlgorithmsBitmap(this.Width, this.Height,
                                                     this.FreeImageType, this.colorDepth));
            }

            if (!(Tile.isCompositeRGB))
            {
                fib = LoadFreeImageBitmapFromFile(filepath);
            }
            else     //// Composite RGB: Insert 3 grey images into 1 24-bit colour image
            {
                FreeImageAlgorithmsBitmap fib8 = null;
                int n = Tile.nCompositeImages;

                // Crop images differently to apply the channel shift
                // shift for channel 0 will always be 0,0 but
                // crop for the channel with the min shift will be 0 to width-range and
                // crop for the channel with the max shift will be range to width.

                int   minX, minY, midX, midY, maxX, maxY, rX, rY;
                int[] orderX = new int[3], orderY = new int[3];
                int[] x      = new int[3], y = new int[3];
                x[0] = -Tile.channelShift[0].X;
                x[1] = -Tile.channelShift[1].X;
                x[2] = -Tile.channelShift[2].X;
                y[0] = -Tile.channelShift[0].Y;
                y[1] = -Tile.channelShift[1].Y;
                y[2] = -Tile.channelShift[2].Y;

                OrderShifts(x, orderX, out minX, out midX, out maxX);
                OrderShifts(y, orderY, out minY, out midY, out maxY);

                // ranges
                rX = maxX - minX;
                rY = maxY - minY;

                int[] shiftXl = new int[3], shiftYt = new int[3];
                int[] shiftXr = new int[3], shiftYb = new int[3];
                shiftXl[0] = 0;  // the shift for min shift channel
                shiftXr[0] = rX; // the shift for min shift channel
                shiftXl[1] = midX - minX;
                shiftXr[1] = maxX - midX;
                shiftXl[2] = rX; // the shift for max shift channel
                shiftXr[2] = 0;  // the shift for max shift channel
                shiftYt[0] = 0;  // the shift for min shift channel
                shiftYb[0] = rY; // the shift for min shift channel
                shiftYt[1] = midY - minY;
                shiftYb[1] = maxY - midY;
                shiftYt[2] = rY; // the shift for max shift channel
                shiftYb[2] = 0;  // the shift for max shift channel

                fib8 = LoadFreeImageBitmapFromFile(this.getFilePath(0));
                fib8.LinearScaleToStandardType(Tile.scaleMin[0], Tile.scaleMax[0]);
                //               fib8.Crop(0, 2, fib8.Width - 1 - 4, fib8.Height - 1);
                fib8.Crop(shiftXl[orderX[0]], shiftYt[orderY[0]], fib8.Width - 1 - shiftXr[orderX[0]], fib8.Height - 1 - shiftYb[orderY[0]]);
                fib = new FreeImageAlgorithmsBitmap(fib8.Width, fib8.Height, FREE_IMAGE_TYPE.FIT_BITMAP, 24);
                FreeImage.SetChannel(fib.Dib, fib8.Dib, Tile.channel[0]);
                fib8.Dispose();

                fib8 = LoadFreeImageBitmapFromFile(this.getFilePath(1));
                fib8.LinearScaleToStandardType(Tile.scaleMin[1], Tile.scaleMax[1]);
//                fib8.Crop(4, 0, fib8.Width - 1, fib8.Height - 1 - 2);
                fib8.Crop(shiftXl[orderX[1]], shiftYt[orderY[1]], fib8.Width - 1 - shiftXr[orderX[1]], fib8.Height - 1 - shiftYb[orderY[1]]);
                FreeImage.SetChannel(fib.Dib, fib8.Dib, Tile.channel[1]);
                fib8.Dispose();

                if (n == 3)  // may not have 3 files but should have at least 2
                {
                    fib8 = LoadFreeImageBitmapFromFile(this.getFilePath(2));
                    fib8.LinearScaleToStandardType(Tile.scaleMin[2], Tile.scaleMax[2]);
//                    fib8.Crop(2, 1, fib8.Width - 1 - 2, fib8.Height - 1 - 1);
                    fib8.Crop(shiftXl[orderX[2]], shiftYt[orderY[2]], fib8.Width - 1 - shiftXr[orderX[2]], fib8.Height - 1 - shiftYb[orderY[2]]);
                    FreeImage.SetChannel(fib.Dib, fib8.Dib, Tile.channel[2]);
                    fib8.Dispose();
                }
            }


            // Resample seems to only work for uint16 not int16 currently
            if (fib.ImageType == FREE_IMAGE_TYPE.FIT_INT16)
            {
                fib.ConvertInt16ToUInt16();
            }

            return(fib);
        }
Exemplo n.º 17
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();
            }
        }
Exemplo n.º 18
0
        protected override void ReadHeader(TileLoadInfo info)
        {
            int count = 0;
            List <TilePosition> tilePositions = new List <TilePosition>();

            string prefix = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "file format", "");

            info.Prefix = prefix;

            string iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "roi left", "0.0");
            double roiLeft  = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "roi top", "0.0");
            double roiTop = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Roi Height", "0.0");
            double roiHeight = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Horizontal Overlap", "0.0");
            // This is overlap in %
            decimal overlapX = Convert.ToDecimal(iniValue);

            info.OverLapPercentageX = (double)overlapX;

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Vertical Overlap", "0.0");
            // This is overlap in %
            decimal overlapY = Convert.ToDecimal(iniValue);

            info.OverLapPercentageY = (double)overlapY;

            iniValue           = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Horizontal Frames", "0.0");
            info.WidthInTiles  = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);
            iniValue           = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Vertical Frames", "0.0");
            info.HeightInTiles = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Max Intensity", "0.0");
            info.TotalMinIntensity = 0;
            info.TotalMaxIntensity = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);
            info.ScaleMinIntensity = info.TotalMinIntensity;
            info.ScaleMaxIntensity = info.TotalMaxIntensity;

            string extension = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Extension", ".ics");

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Width", "0");
            int tileWidth = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Height", "0");
            int tileHeight = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue        = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Bits Per Pixel", "0");
            info.ColorDepth = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Tile Image Type", "0");
            int tileImageType = Convert.ToInt32(iniValue, CultureInfo.InvariantCulture);

            iniValue = IniParser.IniFile.GetIniFileString(this.FilePath, info_ini_name, "Pixels Per Micron", "1.0");
            info.OriginalPixelsPerMicron = Convert.ToDouble(iniValue, CultureInfo.InvariantCulture);

            // This file format provides a list of tiles and their position relative
            // to the left, top of the first region of interest.

            DirectoryInfo dir = new DirectoryInfo(this.DirectoryPath);

            FileInfo[] filesInDir = dir.GetFiles(prefix + "*" + extension);

            count = info.WidthInTiles * info.HeightInTiles;

            Tile[] validTiles = new Tile[count];

            Tile.IsCompositeRGB = false;

            // Check whether all the filenames will have are valid
            for (int i = 1; i <= count; i++)
            {
                string filename = BuildExpectedFilename(prefix, i, extension);
                string fullpath = this.DirectoryPath + Path.DirectorySeparatorChar + filename;

                validTiles[i - 1] = new Tile(fullpath, i, tileWidth, tileHeight);
            }

            int             width  = 0;
            int             height = 0;
            int             bpp    = 8;
            FREE_IMAGE_TYPE type   = FREE_IMAGE_TYPE.FIT_BITMAP;

            // Find the first tile that exists to get the sizes and color depth etc
            // but check all
            bool oneNotFound = false, atLeastOneFound = false;

            foreach (Tile tile in validTiles)
            {
                if (System.IO.File.Exists(tile.FilePath))
                {
                    if (!atLeastOneFound)
                    {
                        FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap();
                        width  = fib.Width;
                        height = fib.Height;
                        bpp    = fib.ColorDepth;
                        type   = fib.ImageType;
                        fib.Dispose();
                        atLeastOneFound = true;
                    }
                }
                else
                {
                    oneNotFound = true;
                }
            }

            if (!atLeastOneFound)  // No images at all!
            {
                throw (new MosaicReaderException("No images found. Expecting prefix " + prefix + " as in seq file."));
            }

            if (oneNotFound)
            {
                MessageBox.Show("At least 1 image is missing from the mosaic.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            foreach (Tile tile in validTiles)
            {
                if (this.ThreadController.ThreadAborted)
                {
                    return;
                }

                Point position = new Point();

                int row, col;

                //   fileWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(filepath);

                // Ignore files in the directory that have shorter names than we expect.
                //   if (fileWithoutExtension.Length < mosaicInfo.Fileinfo.Prefix.Length)
                //       continue;

                //  fileWithoutPrefix = fileWithoutExtension.Substring(prefix.Length);
                //   fileNumber = Version2SequenceFileReader.ParseIntStringWithZeros(fileWithoutPrefix);

                row = ((tile.TileNumber - 1) / info.WidthInTiles) + 1;

                if ((row % 2) > 0)
                {
                    // Odd row reversed order
                    col = ((tile.TileNumber - 1) % info.WidthInTiles) + 1;
                }
                else
                {
                    // Even row nornal order
                    col = info.WidthInTiles - ((tile.TileNumber - 1) % info.WidthInTiles);
                }

                decimal overlapXInpixels = (overlapX / 100.0M) * (decimal)tileWidth;
                decimal overlapYInpixels = (overlapY / 100.0M) * (decimal)tileHeight;

                position.X = (int)Math.Round(((decimal)width - overlapXInpixels) * ((decimal)col - 1.0M));
                position.Y = (int)Math.Round(((decimal)height - overlapYInpixels) * ((decimal)row - 1.0M));

                tile.OriginalPosition = position;
                tile.Width            = width;
                tile.Height           = height;
                tile.ColorDepth       = bpp;
                tile.FreeImageType    = type;

                info.Items.Add(tile);
            }
        }
Exemplo n.º 19
0
        private void CreateGraph(ZedGraphControl zgc)
        {
            // Get a reference to the GraphPane
            GraphPane myPane = zgc.GraphPane;

            // Set the Titles
            myPane.Title.Text       = "Intensity Histogram";
            myPane.XAxis.Title.Text = "Intensity";
            myPane.YAxis.Title.Text = "Count";

            TileView viewer = this.mosaicWindow.TileView;

            List <Tile> tiles = viewer.GetVisibleTiles();

            if (tiles == null)
            {
                return;
            }

            int[]   total_red_hist   = new int[256];
            int[]   total_green_hist = new int[256];
            int[]   total_blue_hist  = new int[256];
            ulong[] total_hist       = new ulong[256];

            int[]   red_hist    = null;
            int[]   green_hist  = null;
            int[]   blue_hist   = null;
            ulong[] hist        = null;
            bool    colourImage = false;

            if (MosaicWindow.MosaicInfo.ColorDepth >= 24)
            {
                colourImage = true;
            }

            for (int i = 0; i < total_red_hist.Length; i++)
            {
                total_red_hist[i]   = 0;
                total_green_hist[i] = 0;
                total_blue_hist[i]  = 0;
            }

            double max_posible_intensity_in_images = 0.0;

            foreach (Tile tile in tiles)
            {
                if (tile.IsDummy)
                {
                    continue;
                }

                FreeImageAlgorithmsBitmap dib = tile.LoadFreeImageBitmap();

                if (dib.MaximumPossibleIntensityValue > max_posible_intensity_in_images)
                {
                    max_posible_intensity_in_images = dib.MaximumPossibleIntensityValue;
                }

                if (colourImage)
                {
                    dib.GetHistogram(FREE_IMAGE_COLOR_CHANNEL.FICC_RED, out red_hist);
                    dib.GetHistogram(FREE_IMAGE_COLOR_CHANNEL.FICC_GREEN, out green_hist);
                    dib.GetHistogram(FREE_IMAGE_COLOR_CHANNEL.FICC_BLUE, out blue_hist);
                }
                else
                {
                    dib.GetGreyLevelHistogram(256, out hist);
                }

                dib.Dispose();

                for (int i = 0; i < total_red_hist.Length; i++)
                {
                    if (colourImage)
                    {
                        total_red_hist[i]   += red_hist[i];
                        total_green_hist[i] += green_hist[i];
                        total_blue_hist[i]  += blue_hist[i];
                    }
                    else
                    {
                        total_hist[i] += hist[i];
                    }
                }
            }

            double range_per_bin = max_posible_intensity_in_images / 256;

            double        x, y;
            PointPairList red_list   = new PointPairList();
            PointPairList green_list = new PointPairList();
            PointPairList blue_list  = new PointPairList();

            if (colourImage)
            {
                for (int i = 0; i < total_red_hist.Length; i++)
                {
                    x = (double)i;
                    y = (double)total_red_hist[i];
                    red_list.Add(x, y);

                    y = (double)total_green_hist[i];
                    green_list.Add(x, y);

                    y = (double)total_blue_hist[i];
                    blue_list.Add(x, y);
                }

                // Generate a red curve with diamond
                LineItem red_curve = myPane.AddCurve("",
                                                     red_list, Color.Red, SymbolType.None);

                LineItem green_curve = myPane.AddCurve("",
                                                       green_list, Color.Green, SymbolType.None);

                LineItem blue_curve = myPane.AddCurve("",
                                                      blue_list, Color.Blue, SymbolType.None);
            }
            else
            {
                for (int i = 0; i < total_red_hist.Length; i++)
                {
                    x = (double)(i * range_per_bin);
                    y = (double)total_hist[i];
                    red_list.Add(x, y);
                }

                // Generate a red curve with diamond
                // symbols, and "Porsche" in the legend
                BarItem curve = myPane.AddBar("",
                                              red_list, Color.Black);
            }

            zgc.AxisChange();
        }