コード例 #1
0
ファイル: TileReader.cs プロジェクト: MobileScientists/Mosaic
        private bool ReadMetaData(TileLoadInfo info, string filepath)
        {
            if (!File.Exists(filepath))
            {
                return(false);
            }

            using (StreamReader file = System.IO.File.OpenText(filepath))
            {
                // Read file
                while (!file.EndOfStream)
                {
                    String line = file.ReadLine();

                    // Ignore empty lines
                    if (line.Length > 0)
                    {
                        string[] lineData = line.Split('\t');

                        info.AddMetaData(lineData[0], lineData[1]);
                    }
                }

                // Remove any silly values - Why do we export these from the scope ?
                info.MetaData.Remove("stage pos");
                info.MetaData.Remove("stage positionx");
                info.MetaData.Remove("stage positiony");
                info.MetaData.Remove("stage positionz");
            }

            return(true);
        }
コード例 #2
0
ファイル: MosaicFileReader.cs プロジェクト: atdgroup/Mosaic
        /*
        public override void SetPrefix(TileLoadInfo info)
        {
            this.s = new ZipInputStream(File.OpenRead(this.FilePath));
            this.sr = new StreamReader(s);
            this.s.GetNextEntry(); // Get the first entry ie the info file.

            string text = sr.ReadToEnd();

            this.tileData = Regex.Split(text, "\r\n");
            string[] line = this.tileData[0].Split('\u0000');

            // Get the prefix of the tiles
            info.Prefix = this.tileData[1].Split('\u0000')[0];

            this.s.Close();
            this.sr.Close();
        }
        */
        protected override void ReadHeader(TileLoadInfo info)
        {
            try
            {
                this.s = new ZipInputStream(File.OpenRead(this.FilePath));
                this.sr = new StreamReader(s);
                this.s.GetNextEntry(); // Get the first entry ie the info file.

                string text = sr.ReadToEnd();
                double version = 1.0;

                this.tileData = Regex.Split(text, "\r\n");
                string[] line = this.tileData[0].Split('\u0000');
                int j = 0;  // version number at start
                version = Convert.ToDouble(line[j]); j++;
                info.WidthInTiles = Convert.ToInt32(line[j]); j++;
                info.HeightInTiles = Convert.ToInt32(line[j]); j++;
                info.TotalWidth = Convert.ToInt32(line[j]); j++;
                info.TotalHeight = Convert.ToInt32(line[j]); j++;
                info.ColorDepth = Convert.ToInt32(line[j]); j++;
                info.OriginalPixelsPerMicron = Convert.ToDouble(line[j]); j++;
                info.OverLapPercentageX = Convert.ToDouble(line[j]); j++;
                info.OverLapPercentageY = Convert.ToDouble(line[j]); j++;
                double dummyOverlap = Convert.ToDouble(line[j]); j++;
                info.TotalMinIntensity = Convert.ToDouble(line[j]); j++;
                info.TotalMaxIntensity = Convert.ToDouble(line[j]); j++;
                info.ScaleMinIntensity = info.TotalMinIntensity;
                info.ScaleMaxIntensity = info.TotalMaxIntensity;
                info.FreeImageType = (FREE_IMAGE_TYPE)Convert.ToInt32(line[j]); j++;
                this.numberOfTiles = Convert.ToInt32(line[j]); j++;
                this.thumbnailWidth = Convert.ToInt32(line[j]); j++;
                this.thumbnailHeight = Convert.ToInt32(line[j]); j++;
                info.IsCorrelated = Convert.ToBoolean(line[j]); j++;

                info.Prefix = this.tileData[1].Split('\u0000')[0];

                string filepath, filename, filename2="", filename3="";

                int i = 1;

                // Read the thumbnails
                while (i <= this.numberOfTiles)
                {
                    if (this.ThreadController.ThreadAborted)
                        return;

                    line = this.tileData[i + 1].Split('\u0000');
                    j = 0;
                    filename = line[j].ToString(); j++;
                    if (version >= 1.5)  // composite images
                    {
                        filename2 = line[j].ToString(); j++;
                        filename3 = line[j].ToString(); j++;
                    }
                    filepath = this.DirectoryPath + Path.DirectorySeparatorChar + filename;

                    int x = Convert.ToInt32(line[j]); j++;
                    int y = Convert.ToInt32(line[j]); j++;
                    int width = Convert.ToInt32(line[j]); j++;
                    int height = Convert.ToInt32(line[j]); j++;
                    int bpp = Convert.ToInt32(line[j]); j++;
                    int minIntensity = Convert.ToInt32(line[j]); j++;
                    int maxIntensity = Convert.ToInt32(line[j]); j++;
                    int type = Convert.ToInt32(line[j]); j++;
                    bool isDummy = Convert.ToBoolean(line[j]); j++;
                    int correlatedx = Convert.ToInt32(line[j]); j++;
                    int correlatedy = Convert.ToInt32(line[j]); j++;
                    bool isCorrelated = Convert.ToBoolean(line[j]); j++;

                    Point position = new Point(x, y);
                    Point correlatedPosition = new Point(correlatedx, correlatedy);

                    Tile tile = new Tile(filepath, position, width, height);

                    tile.ColorDepth = bpp;
                    tile.FreeImageType = (FREE_IMAGE_TYPE)type;
                    tile.MinIntensity = minIntensity;
                    tile.MaxIntensity = maxIntensity;
                    tile.IsDummy = isDummy;
                    tile.AdjustedPosition = correlatedPosition;
                    tile.IsAdjusted = isCorrelated;
                    if (version >= 1.5)  // composite images
                    {
                        tile.setFileName(1, filename2);
                        tile.setFileName(2, filename3);
                    }

                    info.Items.Add(tile);

                    i++;
                }

                if (version >= 1.5) // composite images
                {
                    line = this.tileData[i + 1].Split('\u0000'); i++;
                    j = 0;
                    Tile.IsCompositeRGB = Convert.ToBoolean(line[j]); j++;
                    info.IsCompositeRGB = Tile.IsCompositeRGB;
                    Tile.nCompositeImages = Convert.ToInt32(line[j]); j++;
                    Tile.channel[0] = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.channel[1] = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.channel[2] = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.scaleMin[0] = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMin[1] = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMin[2] = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[0] = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[1] = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[2] = Convert.ToDouble(line[j]); j++;
                    if (version >= 1.6)  // composite rgb channel shifts
                    {
                        Tile.channelShift[0].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[0].Y = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[1].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[1].Y = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[2].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[2].Y = Convert.ToInt32(line[j]); j++;
                    }
                    Tile.channelPrefix[0] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                    Tile.channelPrefix[1] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                    Tile.channelPrefix[2] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                }
                else
                {
                    Tile.IsCompositeRGB = false;
                    info.IsCompositeRGB = false;
                }

                this.s.Close();
                this.sr.Close();
            }
            catch (System.FormatException)
            {
                // probably an old mos file
                // just continue to 'finally' section
                MessageBox.Show("Old mos file imported.");
            }
            catch (Exception e)
            {
                throw new MosaicCacheReaderException("Failed to read cache file format: " + e.Message);
            }
            finally
            {
                this.s.Close();
                this.sr.Close();
            }
        }
コード例 #3
0
ファイル: MosaicFileReader.cs プロジェクト: atdgroup/Mosaic
        internal override void ReadThumbnails(TileLoadInfo info)
        {
            this.ThreadController.ReportThreadStarted(this, "Loading files");

            ZipEntry zipEntry;

            int totalCount = 0;
            int count = 0;

            ThumbnailMapCollection mapCollection =
                new ThumbnailMapCollection(this.thumbnailWidth, this.thumbnailHeight);

            this.s.Close();
            this.s = new ZipInputStream(File.OpenRead(this.FilePath));

            totalCount = 0;

            try
            {

                // First go through the zip file and get the image maps
                while ((zipEntry = this.s.GetNextEntry()) != null)
                {
                    if (zipEntry.Name.StartsWith("Thumbnail"))
                    {
                        if (this.ThreadController.ThreadAborted)
                            return;

                        count = 0;

                        ThumbnailMap map = new ThumbnailMap(this.thumbnailWidth, this.thumbnailHeight, this.s);

                        Tile tile = null;

                        for (int i = 0; i < map.MapSizeInTiles && totalCount < MosaicWindow.MosaicInfo.Items.Count; i++)
                        {
                            tile = MosaicWindow.MosaicInfo.Items[totalCount];

                            FreeImageAlgorithmsBitmap fib = map.GetImage(count);
                            tile.Thumbnail = fib;

                            if (tile.Thumbnail == null)
                            {
                                throw new Exception("Failed to retrive tumbnail from map");
                            }

                            this.ThreadController.ReportThreadPercentage(this, "Creating Thumbnails",
                                totalCount++, MosaicWindow.MosaicInfo.Items.Count);

                            count++;
                        }

                        map.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                FailedToReadFormat = true;
                this.ThreadController.ReportThreadCompleted(this, "Loaded file", false);
                return;
            }

            this.s.Close();
            this.sr.Close();

            info.FreeImageType = MosaicWindow.MosaicInfo.Items[0].FreeImageType;
            info.ColorDepth = MosaicWindow.MosaicInfo.Items[0].ColorDepth;

            this.ThreadController.ReportThreadCompleted(this, "Loaded file", false);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: TileReader.cs プロジェクト: MobileScientists/Mosaic
        internal void RegenerateThumbnails(TileLoadInfo info)
        {   // this is never overidden by a TileReader
            double min = Double.MaxValue, max = Double.MinValue;
            double tmp_min = 0.0, tmp_max = 0.0;
            int    count = 0;

            this.threadController.ReportThreadStarted(this, "Creating Thumbnails");

            foreach (Tile tile in info.Items)
            {
                if (this.threadController.ThreadAborted)
                {
                    return;
                }

                using (FreeImageAlgorithmsBitmap fib = tile.LoadFreeImageBitmap())
                {
                    Tile.ResizeToThumbnail(fib);

                    if (info.TotalMaxIntensity == 0.0)
                    {
                        // We have to find the max intensity of all the images then linear scale later
                        fib.FindMinMaxIntensity(out tmp_min, out tmp_max);

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

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

                        info.TotalMinIntensity = min;
                        info.TotalMaxIntensity = max;
                        info.ScaleMinIntensity = min;
                        info.ScaleMaxIntensity = max;
                    }

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

                        if (tile.Thumbnail == null)
                        {
                            MessageBox.Show("Error creating thumbnail.");
                        }

                        if (fib.IsGreyScale)
                        {
                            tile.Thumbnail.LinearScaleToStandardType(info.ScaleMinIntensity, info.ScaleMaxIntensity);
                            tile.Thumbnail.SetGreyLevelPalette();
                        }
                    }

                    this.threadController.ReportThreadPercentage(this, "Creating Thumbnails", count++, info.Items.Count);
                }
            }

            // Have to scale the bitmaps for Ros's formats
            if (info.TotalMaxIntensity != 0.0)
            {
                info.FreeImageType = info.Items[0].FreeImageType;
                info.ColorDepth    = info.Items[0].ColorDepth;
            }
            else
            {
                info.ScaleMinIntensity = min;
                info.ScaleMaxIntensity = max;

                count = 0;

                info.FreeImageType = info.Items[0].FreeImageType;
                info.ColorDepth    = info.Items[0].ColorDepth;
            }

            this.threadController.ReportThreadCompleted(this, "Loaded Thumbnails", false);
        }
コード例 #6
0
ファイル: TileReader.cs プロジェクト: MobileScientists/Mosaic
 internal virtual void ReadThumbnails(TileLoadInfo info)
 {  // this may be overridden by a TileReader
     RegenerateThumbnails(info);
 }
コード例 #7
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);
            }
        }
コード例 #8
0
        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++;
            }
        }
コード例 #9
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);
            }
        }
コード例 #10
0
        /*
         * public override void SetPrefix(TileLoadInfo info)
         * {
         *  this.s = new ZipInputStream(File.OpenRead(this.FilePath));
         *  this.sr = new StreamReader(s);
         *  this.s.GetNextEntry(); // Get the first entry ie the info file.
         *
         *  string text = sr.ReadToEnd();
         *
         *  this.tileData = Regex.Split(text, "\r\n");
         *  string[] line = this.tileData[0].Split('\u0000');
         *
         *  // Get the prefix of the tiles
         *  info.Prefix = this.tileData[1].Split('\u0000')[0];
         *
         *  this.s.Close();
         *  this.sr.Close();
         * }
         */

        protected override void ReadHeader(TileLoadInfo info)
        {
            try
            {
                this.s  = new ZipInputStream(File.OpenRead(this.FilePath));
                this.sr = new StreamReader(s);
                this.s.GetNextEntry(); // Get the first entry ie the info file.

                string text    = sr.ReadToEnd();
                double version = 1.0;

                this.tileData = Regex.Split(text, "\r\n");
                string[] line = this.tileData[0].Split('\u0000');
                int      j    = 0; // version number at start
                version                      = Convert.ToDouble(line[j]); j++;
                info.WidthInTiles            = Convert.ToInt32(line[j]); j++;
                info.HeightInTiles           = Convert.ToInt32(line[j]); j++;
                info.TotalWidth              = Convert.ToInt32(line[j]); j++;
                info.TotalHeight             = Convert.ToInt32(line[j]); j++;
                info.ColorDepth              = Convert.ToInt32(line[j]); j++;
                info.OriginalPixelsPerMicron = Convert.ToDouble(line[j]); j++;
                info.OverLapPercentageX      = Convert.ToDouble(line[j]); j++;
                info.OverLapPercentageY      = Convert.ToDouble(line[j]); j++;
                double dummyOverlap = Convert.ToDouble(line[j]); j++;
                info.TotalMinIntensity = Convert.ToDouble(line[j]); j++;
                info.TotalMaxIntensity = Convert.ToDouble(line[j]); j++;
                info.ScaleMinIntensity = info.TotalMinIntensity;
                info.ScaleMaxIntensity = info.TotalMaxIntensity;
                info.FreeImageType     = (FREE_IMAGE_TYPE)Convert.ToInt32(line[j]); j++;
                this.numberOfTiles     = Convert.ToInt32(line[j]); j++;
                this.thumbnailWidth    = Convert.ToInt32(line[j]); j++;
                this.thumbnailHeight   = Convert.ToInt32(line[j]); j++;
                info.IsCorrelated      = Convert.ToBoolean(line[j]); j++;

                info.Prefix = this.tileData[1].Split('\u0000')[0];

                string filepath, filename, filename2 = "", filename3 = "";

                int i = 1;

                // Read the thumbnails
                while (i <= this.numberOfTiles)
                {
                    if (this.ThreadController.ThreadAborted)
                    {
                        return;
                    }

                    line     = this.tileData[i + 1].Split('\u0000');
                    j        = 0;
                    filename = line[j].ToString(); j++;
                    if (version >= 1.5)  // composite images
                    {
                        filename2 = line[j].ToString(); j++;
                        filename3 = line[j].ToString(); j++;
                    }
                    filepath = this.DirectoryPath + Path.DirectorySeparatorChar + filename;

                    int  x            = Convert.ToInt32(line[j]); j++;
                    int  y            = Convert.ToInt32(line[j]); j++;
                    int  width        = Convert.ToInt32(line[j]); j++;
                    int  height       = Convert.ToInt32(line[j]); j++;
                    int  bpp          = Convert.ToInt32(line[j]); j++;
                    int  minIntensity = Convert.ToInt32(line[j]); j++;
                    int  maxIntensity = Convert.ToInt32(line[j]); j++;
                    int  type         = Convert.ToInt32(line[j]); j++;
                    bool isDummy      = Convert.ToBoolean(line[j]); j++;
                    int  correlatedx  = Convert.ToInt32(line[j]); j++;
                    int  correlatedy  = Convert.ToInt32(line[j]); j++;
                    bool isCorrelated = Convert.ToBoolean(line[j]); j++;

                    Point position           = new Point(x, y);
                    Point correlatedPosition = new Point(correlatedx, correlatedy);

                    Tile tile = new Tile(filepath, position, width, height);

                    tile.ColorDepth       = bpp;
                    tile.FreeImageType    = (FREE_IMAGE_TYPE)type;
                    tile.MinIntensity     = minIntensity;
                    tile.MaxIntensity     = maxIntensity;
                    tile.IsDummy          = isDummy;
                    tile.AdjustedPosition = correlatedPosition;
                    tile.IsAdjusted       = isCorrelated;
                    if (version >= 1.5)  // composite images
                    {
                        tile.setFileName(1, filename2);
                        tile.setFileName(2, filename3);
                    }

                    info.Items.Add(tile);

                    i++;
                }

                if (version >= 1.5) // composite images
                {
                    line = this.tileData[i + 1].Split('\u0000'); i++;
                    j    = 0;
                    Tile.IsCompositeRGB   = Convert.ToBoolean(line[j]); j++;
                    info.IsCompositeRGB   = Tile.IsCompositeRGB;
                    Tile.nCompositeImages = Convert.ToInt32(line[j]); j++;
                    Tile.channel[0]       = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.channel[1]       = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.channel[2]       = (FREE_IMAGE_COLOR_CHANNEL)Convert.ToInt32(line[j]); j++;
                    Tile.scaleMin[0]      = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMin[1]      = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMin[2]      = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[0]      = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[1]      = Convert.ToDouble(line[j]); j++;
                    Tile.scaleMax[2]      = Convert.ToDouble(line[j]); j++;
                    if (version >= 1.6)  // composite rgb channel shifts
                    {
                        Tile.channelShift[0].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[0].Y = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[1].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[1].Y = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[2].X = Convert.ToInt32(line[j]); j++;
                        Tile.channelShift[2].Y = Convert.ToInt32(line[j]); j++;
                    }
                    Tile.channelPrefix[0] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                    Tile.channelPrefix[1] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                    Tile.channelPrefix[2] = this.tileData[i + 1].Split('\u0000')[0]; i++;
                }
                else
                {
                    Tile.IsCompositeRGB = false;
                    info.IsCompositeRGB = false;
                }

                this.s.Close();
                this.sr.Close();
            }
            catch (System.FormatException)
            {
                // probably an old mos file
                // just continue to 'finally' section
                MessageBox.Show("Old mos file imported.");
            }
            catch (Exception e)
            {
                throw new MosaicCacheReaderException("Failed to read cache file format: " + e.Message);
            }
            finally
            {
                this.s.Close();
                this.sr.Close();
            }
        }
コード例 #11
0
        internal override void ReadThumbnails(TileLoadInfo info)
        {
            this.ThreadController.ReportThreadStarted(this, "Loading files");

            ZipEntry zipEntry;

            int totalCount = 0;
            int count      = 0;

            ThumbnailMapCollection mapCollection =
                new ThumbnailMapCollection(this.thumbnailWidth, this.thumbnailHeight);

            this.s.Close();
            this.s = new ZipInputStream(File.OpenRead(this.FilePath));

            totalCount = 0;

            try
            {
                // First go through the zip file and get the image maps
                while ((zipEntry = this.s.GetNextEntry()) != null)
                {
                    if (zipEntry.Name.StartsWith("Thumbnail"))
                    {
                        if (this.ThreadController.ThreadAborted)
                        {
                            return;
                        }

                        count = 0;

                        ThumbnailMap map = new ThumbnailMap(this.thumbnailWidth, this.thumbnailHeight, this.s);

                        Tile tile = null;

                        for (int i = 0; i < map.MapSizeInTiles && totalCount < MosaicWindow.MosaicInfo.Items.Count; i++)
                        {
                            tile = MosaicWindow.MosaicInfo.Items[totalCount];

                            FreeImageAlgorithmsBitmap fib = map.GetImage(count);
                            tile.Thumbnail = fib;

                            if (tile.Thumbnail == null)
                            {
                                throw new Exception("Failed to retrive tumbnail from map");
                            }

                            this.ThreadController.ReportThreadPercentage(this, "Creating Thumbnails",
                                                                         totalCount++, MosaicWindow.MosaicInfo.Items.Count);

                            count++;
                        }

                        map.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                FailedToReadFormat = true;
                this.ThreadController.ReportThreadCompleted(this, "Loaded file", false);
                return;
            }

            this.s.Close();
            this.sr.Close();

            info.FreeImageType = MosaicWindow.MosaicInfo.Items[0].FreeImageType;
            info.ColorDepth    = MosaicWindow.MosaicInfo.Items[0].ColorDepth;

            this.ThreadController.ReportThreadCompleted(this, "Loaded file", false);
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
ファイル: SequenceFileReader.cs プロジェクト: atdgroup/Mosaic
        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);
            }
        }
コード例 #14
0
        protected override void ReadHeader(TileLoadInfo info)
        {
            info.OriginalPixelsPerMicron = 1.0;
            info.OverLapPercentageX = 0.0;
            info.OverLapPercentageY = 0.0;

            this.arranger = new ImageCollectionArrangerForm();

            TextStringDialog nameDialog = new TextStringDialog();

            nameDialog.Text = "Mosaic Name";
            nameDialog.Description = "Please enter a name for this Mosaic.";

            nameDialog.ShowDialog();

            this.dataSetName = nameDialog.TextString;
            info.Prefix = this.dataSetName;

            if (String.IsNullOrEmpty(this.dataSetName))
            {
                MessageBox.Show("Invalid Dataset name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Tile.IsCompositeRGB = false;
            Tile.ThumbnailHeight = 0;  // reset this so image aspect is correct

            //this.imageCollectionThreadController = new ThreadController(this.arranger);

            try
            {
                DisplayThumbnails();
            }
            catch (Exception e)
            {
                throw;
            }

            this.arranger.ShowDialog();

            List<DesignerItem> items = this.arranger.GetItemPositionsSortedFromMostTopLeft();

            float x, y, top, left;
            Point position;
            FreeImageAlgorithmsWPFImage wpfImage;

            wpfImage = items[0].Content as FreeImageAlgorithmsWPFImage;

            if (wpfImage == null)
            {
                throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
            }

            // Initialise left and top
            left = (float)DesignerCanvas.GetLeft(items[0]);
            top = (float)DesignerCanvas.GetTop(items[0]);

            foreach (DesignerItem item in items)
            {
                wpfImage = item.Content as FreeImageAlgorithmsWPFImage;

                if (wpfImage == null)
                {
                    throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
                }

                // Get position of item relative to the most top left
                x = (float)DesignerCanvas.GetLeft(item);
                y = (float)DesignerCanvas.GetTop(item);

                if (x < left)
                    left = x;

                if (y < top)
                    top = y;
            }

            // We now have the most top left position.

            foreach (DesignerItem item in items)
            {
                wpfImage = item.Content as FreeImageAlgorithmsWPFImage;

                if(wpfImage == null)
                {
                    throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
                }

                // Get position of item relative to the most top left
                x = (float)DesignerCanvas.GetLeft(item) - left;
                y = (float)DesignerCanvas.GetTop(item) - top;

                position = new Point((int)x, (int)y);

                Tile tile = new Tile(wpfImage.FilePath, position, wpfImage.Fib.Width, wpfImage.Fib.Height);

                x = (int)(x / tile.ThumbnailToFullWidthScaleFactor);
                y = (int)(y / tile.ThumbnailToFullHeightScaleFactor);

                position = new Point((int)x, (int)y);

                tile.OriginalPosition = position;
                tile.ColorDepth = wpfImage.Fib.ColorDepth;
                tile.FreeImageType = wpfImage.Fib.ImageType;

                info.Items.Add(tile);
            }
        }
コード例 #15
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);
            }
        }
コード例 #16
0
ファイル: TileReader.cs プロジェクト: MobileScientists/Mosaic
 protected abstract void ReadHeader(TileLoadInfo info);
コード例 #17
0
        protected override void ReadHeader(TileLoadInfo info)
        {
            info.OriginalPixelsPerMicron = 1.0;
            info.OverLapPercentageX      = 0.0;
            info.OverLapPercentageY      = 0.0;

            this.arranger = new ImageCollectionArrangerForm();

            TextStringDialog nameDialog = new TextStringDialog();

            nameDialog.Text        = "Mosaic Name";
            nameDialog.Description = "Please enter a name for this Mosaic.";

            nameDialog.ShowDialog();

            this.dataSetName = nameDialog.TextString;
            info.Prefix      = this.dataSetName;

            if (String.IsNullOrEmpty(this.dataSetName))
            {
                MessageBox.Show("Invalid Dataset name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Tile.IsCompositeRGB  = false;
            Tile.ThumbnailHeight = 0;  // reset this so image aspect is correct

            //this.imageCollectionThreadController = new ThreadController(this.arranger);

            try
            {
                DisplayThumbnails();
            }
            catch (Exception e)
            {
                throw;
            }

            this.arranger.ShowDialog();

            List <DesignerItem> items = this.arranger.GetItemPositionsSortedFromMostTopLeft();

            float x, y, top, left;
            Point position;
            FreeImageAlgorithmsWPFImage wpfImage;

            wpfImage = items[0].Content as FreeImageAlgorithmsWPFImage;

            if (wpfImage == null)
            {
                throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
            }

            // Initialise left and top
            left = (float)DesignerCanvas.GetLeft(items[0]);
            top  = (float)DesignerCanvas.GetTop(items[0]);

            foreach (DesignerItem item in items)
            {
                wpfImage = item.Content as FreeImageAlgorithmsWPFImage;

                if (wpfImage == null)
                {
                    throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
                }

                // Get position of item relative to the most top left
                x = (float)DesignerCanvas.GetLeft(item);
                y = (float)DesignerCanvas.GetTop(item);

                if (x < left)
                {
                    left = x;
                }

                if (y < top)
                {
                    top = y;
                }
            }

            // We now have the most top left position.

            foreach (DesignerItem item in items)
            {
                wpfImage = item.Content as FreeImageAlgorithmsWPFImage;

                if (wpfImage == null)
                {
                    throw new MosaicReaderException("UIElement is not a FreeImageAlgorithmsWPFImage type");
                }

                // Get position of item relative to the most top left
                x = (float)DesignerCanvas.GetLeft(item) - left;
                y = (float)DesignerCanvas.GetTop(item) - top;


                position = new Point((int)x, (int)y);

                Tile tile = new Tile(wpfImage.FilePath, position, wpfImage.Fib.Width, wpfImage.Fib.Height);

                x = (int)(x / tile.ThumbnailToFullWidthScaleFactor);
                y = (int)(y / tile.ThumbnailToFullHeightScaleFactor);

                position = new Point((int)x, (int)y);

                tile.OriginalPosition = position;
                tile.ColorDepth       = wpfImage.Fib.ColorDepth;
                tile.FreeImageType    = wpfImage.Fib.ImageType;

                info.Items.Add(tile);
            }
        }
コード例 #18
0
        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++;
            }
        }