Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InRamImageData"/> class.
 /// Creates the bitmap from the raw image specified.  The bounds should be set on this later.
 /// </summary>
 /// <param name="rawImage">
 /// The raw image.
 /// </param>
 public InRamImageData(Image rawImage)
 {
     _myImage = new Bitmap(rawImage.Width, rawImage.Height);
     Width = rawImage.Width;
     Height = rawImage.Height;
     Graphics g = Graphics.FromImage(_myImage);
     g.DrawImageUnscaled(rawImage, 0, 0);
     g.Dispose();
     WorldFile = new WorldFile();
     double[] aff = new[] { .5, 1.0, 0, _myImage.Height - .5, 0, -1.0 };
     Bounds = new RasterBounds(_myImage.Height, _myImage.Width, aff);
     MemorySetup();
 }
Exemplo n.º 2
0
 public void TestMethod1()
 {
     double[] affine = new double[6];
     affine[0] = 10;
     affine[1] = 1;
     affine[2] = .2;
     affine[3] = 100;
     affine[4] = .2;
     affine[5] = -1.2;
     RasterBounds rb = new RasterBounds(100, 100, affine);
     int row = 40;
     int col = 30;
     Coordinate c = rb.CellCenter_ToProj(row, col);
     RcIndex rc = rb.ProjToCell(c);
     Assert.AreEqual(rc.Row, row);
     Assert.AreEqual(rc.Column, col);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InRamImageData"/> class.
 /// Constructs a new ImageData of the specified width and height.
 /// </summary>
 /// <param name="width">
 /// The integer width in pixels.
 /// </param>
 /// <param name="height">
 /// The integer height in pixels.
 /// </param>
 public InRamImageData(int width, int height)
 {
     WorldFile = new WorldFile();
     double[] aff = new[] { .5, 1.0, 0, _myImage.Height - .5, 0, -1.0 };
     Bounds = new RasterBounds(height, width, aff);
     _myImage = new Bitmap(width, height);
     Width = width;
     Height = height;
     MemorySetup();
 }
Exemplo n.º 4
0
        public bool RasterMath(IRaster input1, IRaster input2, IRaster output, ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (input1 == null || input2 == null || output == null)
            {
                return false;
            }

            Extent envelope = UnionEnvelope(input1, input2);

            // Figures out which raster has smaller cells
            IRaster smallestCellRaster = input1.CellWidth < input2.CellWidth ? input1 : input2;

            // Given the envelope of the two rasters we calculate the number of columns / rows
            int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / smallestCellRaster.CellWidth));
            int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / smallestCellRaster.CellHeight));

            // create output raster
            output = Raster.CreateRaster(
                output.Filename, string.Empty, noOfCol, noOfRow, 1, typeof(int), new[] { string.Empty });
            RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope);
            output.Bounds = bound;

            output.NoDataValue = input1.NoDataValue;

            RcIndex v1;
            RcIndex v2;

            int previous = 0;
            int max = output.Bounds.NumRows + 1;
            for (int i = 0; i < output.Bounds.NumRows; i++)
            {
                for (int j = 0; j < output.Bounds.NumColumns; j++)
                {
                    Coordinate cellCenter = output.CellToProj(i, j);
                    v1 = input1.ProjToCell(cellCenter);
                    double val1;
                    if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1)
                    {
                        val1 = input1.Value[v1.Row, v1.Column];
                    }
                    else
                    {
                        val1 = input1.NoDataValue;
                    }

                    v2 = input2.ProjToCell(cellCenter);
                    double val2;
                    if (v2.Row <= input2.EndRow && v2.Column <= input2.EndColumn && v2.Row > -1 && v2.Column > -1)
                    {
                        val2 = input2.Value[v2.Row, v2.Column];
                    }
                    else
                    {
                        val2 = input2.NoDataValue;
                    }

                    if (val1 == input1.NoDataValue && val2 == input2.NoDataValue)
                    {
                        output.Value[i, j] = output.NoDataValue;
                    }
                    else if (val1 != input1.NoDataValue && val2 == input2.NoDataValue)
                    {
                        output.Value[i, j] = output.NoDataValue;
                    }
                    else if (val1 == input1.NoDataValue && val2 != input2.NoDataValue)
                    {
                        output.Value[i, j] = output.NoDataValue;
                    }
                    else
                    {
                        output.Value[i, j] = Operation(val1, val2);
                    }

                    if (cancelProgressHandler.Cancel)
                    {
                        return false;
                    }
                }

                int current = Convert.ToInt32(Math.Round(i * 100D / max));

                // only update when increment in persentage
                if (current > previous)
                {
                    cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted);
                }

                previous = current;
            }

            // output = Temp;
            output.Save();
            return true;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the RasterFromLAS tool.
        /// </summary>
        /// <param name="filenameLAS">The string filename of the LAS file to convert.</param>
        /// <param name="outputExtent">The extent of the output raster.</param>
        /// <param name="numRows">The integer number of rows of the output raster.</param>
        /// <param name="numColumns">The integer number of columns.</param>
        /// <param name="output">The output raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>Boolean, true if the method was successful.</returns>
        public bool Execute(
            string filenameLAS,
            Extent outputExtent,
            int numRows,
            int numColumns,
            IRaster output,
            ICancelProgressHandler cancelProgressHandler)
        {
            // create output raster
            output = Raster.CreateRaster(
                output.Filename, string.Empty, numColumns, numRows, 1, typeof(int), new[] { string.Empty });
            RasterBounds bound = new RasterBounds(numRows, numColumns, outputExtent);
            output.Bounds = bound;

            output.NoDataValue = int.MinValue;

            ProgressMeter pm = new ProgressMeter(
                cancelProgressHandler,
                TextStrings.ConvertingLAS + filenameLAS + TextStrings.Progresstoraster + "...",
                numRows);

            for (int row = 0; row < numRows; row++)
            {
                for (int j = 0; j < output.Bounds.NumColumns; j++)
                {
                    // TO DO: PING CAN ADD LAS READING AND CELL ASSIGNMENT HERE
                    if (cancelProgressHandler.Cancel)
                    {
                        return false;
                    }
                }

                pm.CurrentValue = row;
            }

            // output = Temp;
            output.Save();
            return true;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Reads the header only from the specified mwi file.  The header is in xml format.
 /// This is a test.  We may have to jurry rig the thing to ensure it ignores the actual
 /// image content.
 /// </summary>
 /// <param name="fileName">Whether this is the mwi or mwh file, this reads the mwh file for the fileName.</param>
 public void ReadHeader(string fileName)
 {
     string header = Path.ChangeExtension(fileName, "mwh");
     XmlSerializer s = new XmlSerializer(typeof(PyramidHeader));
     TextReader r = new StreamReader(header);
     _header = (PyramidHeader)s.Deserialize(r);
     PyramidImageHeader ph = _header.ImageHeaders[0];
     Bounds = new RasterBounds(ph.NumRows, ph.NumColumns, ph.Affine);
     Width = _header.ImageHeaders[0].NumColumns;
     Height = _header.ImageHeaders[0].NumRows;
     r.Close();
 }
Exemplo n.º 7
0
        /// <summary>
        /// This will resample the cells.
        /// If the cell size is zero, this will default to the shorter of the width or height
        /// divided by 256.
        /// </summary>
        /// <param name="input1">the input raster.</param>
        /// <param name="cellHeight">The new cell height or null.</param>
        /// <param name="cellWidth">The new cell width or null.</param>
        /// <param name="outputFileName">The string name of the output raster.</param>
        /// <param name="progressHandler">An interface for handling the progress messages.</param>
        /// <returns>The resampled raster.</returns>
        public static IRaster Resample(IRaster input1, double cellHeight, double cellWidth, string outputFileName,
                                       IProgressHandler progressHandler)
        {
            if (input1 == null)
            {
                return null;
            }

            Extent envelope = input1.Bounds.Extent;

            if (cellHeight == 0)
            {
                cellHeight = envelope.Height / 256;
            }
            if (cellWidth == 0)
            {
                cellWidth = envelope.Width / 256;
            }

            //Calculate new number of columns and rows
            int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / cellWidth));
            int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / cellHeight));

            IRaster output = Raster.CreateRaster(outputFileName, string.Empty, noOfCol, noOfRow, 1, input1.DataType,
                                                 new[] { string.Empty });
            RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope);
            output.Bounds = bound;

            output.NoDataValue = input1.NoDataValue;

            RcIndex index1;
            int max = (output.Bounds.NumRows);
            ProgressMeter pm = new ProgressMeter(progressHandler, "ReSize Cells", max);

            //Loop through every cell for new value
            for (int i = 0; i < max; i++)
            {
                for (int j = 0; j < output.Bounds.NumColumns; j++)
                {
                    //Project the cell position to Map
                    Coordinate cellCenter = output.CellToProj(i, j);
                    index1 = input1.ProjToCell(cellCenter);

                    double val;
                    if (index1.Row <= input1.EndRow && index1.Column <= input1.EndColumn && index1.Row > -1 &&
                        index1.Column > -1)
                    {
                        val = input1.Value[index1.Row, index1.Column] == input1.NoDataValue
                                  ? output.NoDataValue
                                  : input1.Value[index1.Row, index1.Column];
                    }
                    else
                    {
                        val = output.NoDataValue;
                    }
                    output.Value[i, j] = val;
                }
                pm.CurrentValue = i;
            }
            output.Save();
            pm.Reset();
            return output;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of an InRamImage class.  This class supports a basic .Net Image
 /// plus a geographic extent as a RasterBounds.  This class does not feature any byte level
 /// data access or built in file access.  The expectation is that this will only be used in memory.
 /// </summary>
 /// <param name="image">THe Bitmap image to use to create this image.</param>
 public InRamImage(Bitmap image)
 {
     _myImage = image;
     Bounds = new RasterBounds(image.Height, image.Width, new double[] { 0, 1, 0, image.Height, 0, -1 });
 }
Exemplo n.º 9
0
        /// <summary>
        /// Executes the Erase Opaeration tool programaticaly
        /// Ping Yang deleted static for external testing 01/2010
        /// </summary>
        /// <param name="input">The input raster</param>
        /// <param name="oldValue">The original double value representing no-data</param>
        /// <param name="newValue">The new double value representing no-data</param>
        /// <param name="output">The output raster</param>
        /// <param name="cancelProgressHandler">The progress handler</param>
        /// <returns></returns>
        public bool Execute(
            IRaster input,
            double oldValue,
            double newValue,
            IRaster output,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (input == null || newValue == 0 || output == null)
            {
                return false;
            }

            Extent envelope = input.Bounds.Extent;

            int noOfCol = input.NumColumns;
            int noOfRow = input.NumRows;
            int previous = 0;
            Type dataType = input.DataType;

            // create output raster
            output = Raster.CreateRaster(
                output.Filename, string.Empty, noOfCol, noOfRow, 1, dataType, new[] { string.Empty });
            RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope);
            output.Bounds = bound;

            output.NoDataValue = newValue;

            // Loop throug every cell
            int max = output.Bounds.NumRows + 1;
            for (int i = 0; i < output.Bounds.NumRows; i++)
            {
                for (int j = 0; j < output.Bounds.NumColumns; j++)
                {
                    if (input.Value[i, j] == oldValue)
                    {
                        output.Value[i, j] = newValue;
                    }
                    else
                    {
                        output.Value[i, j] = input.Value[i, j];
                    }

                    if (cancelProgressHandler.Cancel)
                    {
                        return false;
                    }
                }

                int current = Convert.ToInt32(Math.Round(i * 100D / max));

                // only update when increment in persentage
                if (current > previous)
                {
                    cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted);
                }

                previous = current;
            }

            // output = Temp;
            output.Save();
            return true;
        }                                           
Exemplo n.º 10
0
        /// <summary>
        /// For big images the scale that is just one step larger than the specified window will be used.
        /// </summary>
        /// <param name="envelope">The envelope containing the geographic extent.</param>
        /// <param name="window">The rectangle containing the window extent.</param>
        /// <returns>The bitmap.</returns>
        public override Bitmap GetBitmap(Extent envelope, Rectangle window)
        {
            if (window.Width == 0 || window.Height == 0)
            {
                return(null);
            }
            if (Header?.ImageHeaders?[0] == null)
            {
                return(null);
            }

            Rectangle expWindow   = window.ExpandBy(1);
            Envelope  expEnvelope = envelope.ToEnvelope().Reproportion(window, expWindow);

            Envelope env = expEnvelope.Intersection(Bounds.Extent.ToEnvelope());

            if (env == null || env.IsNull || env.Height == 0 || env.Width == 0)
            {
                return(null);
            }

            PyramidImageHeader he = Header.ImageHeaders[0];
            int scale;

            double cwa = expWindow.Width / expEnvelope.Width;
            double cha = expWindow.Height / expEnvelope.Height;

            for (scale = 0; scale < Header.ImageHeaders.Length; scale++)
            {
                PyramidImageHeader ph = Header.ImageHeaders[scale];

                if (cwa > ph.NumColumns / Bounds.Width || cha > ph.NumRows / Bounds.Height)
                {
                    if (scale > 0)
                    {
                        scale -= 1;
                    }
                    break;
                }

                he = ph;
            }

            RasterBounds overviewBounds = new RasterBounds(he.NumRows, he.NumColumns, he.Affine);
            Rectangle    r = overviewBounds.CellsContainingExtent(envelope);

            if (r.Width == 0 || r.Height == 0)
            {
                return(null);
            }

            byte[]     vals  = ReadWindow(r.Y, r.X, r.Height, r.Width, scale);
            Bitmap     bmp   = new Bitmap(r.Width, r.Height);
            BitmapData bData = bmp.LockBits(new Rectangle(0, 0, r.Width, r.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            bmp.UnlockBits(bData);

            // Use the cell coordinates to determine the affine coefficients for the cells retrieved.
            double[] affine = new double[6];
            Array.Copy(he.Affine, affine, 6);
            affine[0] = affine[0] + (r.X * affine[1]) + (r.Y * affine[2]);
            affine[3] = affine[3] + (r.X * affine[4]) + (r.Y * affine[5]);

            if (window.Width == 0 || window.Height == 0)
            {
                return(null);
            }

            Bitmap   result = new Bitmap(window.Width, window.Height);
            Graphics g      = Graphics.FromImage(result);

            // Gets the scaling factor for converting from geographic to pixel coordinates
            double dx = window.Width / envelope.Width;
            double dy = window.Height / envelope.Height;

            double[] a = affine;

            // gets the affine scaling factors.
            float  m11    = Convert.ToSingle(a[1] * dx);
            float  m22    = Convert.ToSingle(a[5] * -dy);
            float  m21    = Convert.ToSingle(a[2] * dx);
            float  m12    = Convert.ToSingle(a[4] * -dy);
            double l      = a[0] - (.5 * (a[1] + a[2])); // Left of top left pixel
            double t      = a[3] - (.5 * (a[4] + a[5])); // top of top left pixel
            float  xShift = (float)((l - envelope.MinX) * dx);
            float  yShift = (float)((envelope.MaxY - t) * dy);

            g.Transform       = new Matrix(m11, m12, m21, m22, xShift, yShift);
            g.PixelOffsetMode = PixelOffsetMode.Half;
            if (m11 > 1 || m22 > 1)
            {
                g.InterpolationMode = InterpolationMode.NearestNeighbor;
            }

            g.DrawImage(bmp, new PointF(0, 0));
            bmp.Dispose();
            g.Dispose();
            return(result);
        }
Exemplo n.º 11
0
 /// <summary>
 /// This takes an original image and calculates the header content for all the lower resolution tiles.
 /// This does not actually write the bytes for those images.
 /// </summary>
 /// <param name="originalImage">The raster bounds for the original image.</param>
 public void CreateHeaders(RasterBounds originalImage)
 {
     Bounds = originalImage;
     CreateHeaders(originalImage.NumRows, originalImage.NumColumns, originalImage.AffineCoefficients);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InRamImage"/> class. This class supports a basic .Net Image
 /// plus a geographic extent as a RasterBounds. This class does not feature any byte level
 /// data access or built in file access. The expectation is that this will only be used in memory.
 /// </summary>
 /// <param name="image">THe Bitmap image to use to create this image.</param>
 public InRamImage(Bitmap image)
 {
     _myImage = image;
     Bounds   = new RasterBounds(image.Height, image.Width, new double[] { 0, 1, 0, image.Height, 0, -1 });
 }
Exemplo n.º 13
0
        /// <summary>
        /// Creates a new image and world file, placing the default bounds at the origin, with one pixel per unit.
        /// </summary>
        /// <param name="fileName">The string fileName.</param>
        /// <param name="width">The integer width.</param>
        /// <param name="height">The integer height.</param>
        /// <param name="bandType">The ImageBandType that clarifies how the separate bands are layered in the image.</param>
        /// <returns>The create image.</returns>
        public override IImageData Create(string fileName, int width, int height, ImageBandType bandType)
        {
            Filename  = fileName;
            WorldFile = new WorldFile();
            double[] aff = { 1.0, 0, 0, -1.0, 0, height };
            Bounds             = new RasterBounds(height, width, aff);
            WorldFile.Filename = WorldFile.GenerateFilename(fileName);
            Width    = width;
            Height   = height;
            _myImage = new Bitmap(width, height);
            string      ext = Path.GetExtension(fileName);
            ImageFormat imageFormat;

            switch (ext)
            {
            case ".bmp":
                imageFormat = ImageFormat.Bmp;
                break;

            case ".emf":
                imageFormat = ImageFormat.Emf;
                break;

            case ".exf":
                imageFormat = ImageFormat.Exif;
                break;

            case ".gif":
                imageFormat = ImageFormat.Gif;
                break;

            case ".ico":
                imageFormat = ImageFormat.Icon;
                break;

            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                break;

            case ".mbp":
                imageFormat = ImageFormat.MemoryBmp;
                break;

            case ".png":
                imageFormat = ImageFormat.Png;
                break;

            case ".tif":
                imageFormat = ImageFormat.Tiff;
                break;

            case ".wmf":
                imageFormat = ImageFormat.Wmf;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(fileName), DataStrings.FileTypeNotSupported);
            }

            _myImage.Save(fileName, imageFormat);

            NumBands      = 4;
            BytesPerPixel = 4;
            CopyBitmapToValues();

            return(this);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Executes the ReSample Opaeration tool programaticaly
        /// Ping deleted the static property for external testing.
        /// </summary>
        /// <param name="input1">The input raster.</param>
        /// <param name="newCellHeight">The size of the cell's hight.</param>
        /// <param name="newCellWidth">The size of the cell's width.</param>
        /// <param name="output">The output raster.</param>
        /// <param name="cancelProgressHandler">The progress handler.</param>
        /// <returns>Boolean, true if the method was successful.</returns>
        public bool Execute(
            IRaster input1,
            double newCellHeight,
            double newCellWidth,
            IRaster output,
            ICancelProgressHandler cancelProgressHandler)
        {
            // Validates the input and output data
            if (input1 == null || newCellWidth == 0 || output == null)
            {
                return false;
            }

            Extent envelope = input1.Bounds.Extent;

            // Calculate new number of columns and rows
            int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / newCellWidth));
            int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / newCellHeight));

            int previous = 0;

            // ************OLD Method
            ////Create the new raster with the appropriate dimensions
            // Raster Temp = new Raster();
            ////Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType);
            // Temp.CreateNew(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { "" });
            // Temp.CellWidth = newCellSize;
            // Temp.CellHeight = oldCellSize;
            // Temp.Xllcenter = input1.Bounds.Envelope.Minimum.X + (Temp.CellWidth / 2);
            // Temp.Yllcenter = input1.Bounds.Envelope.Minimum.Y + (Temp.CellHeight / 2);
            // ***************

            // create output raster
            output = Raster.CreateRaster(
                output.Filename, string.Empty, noOfCol, noOfRow, 1, input1.DataType, new[] { string.Empty });
            RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope);
            output.Bounds = bound;

            output.NoDataValue = input1.NoDataValue;

            RcIndex index1;

            // Loop throug every cell for new value
            int max = output.Bounds.NumRows + 1;
            for (int i = 0; i < output.Bounds.NumRows; i++)
            {
                for (int j = 0; j < output.Bounds.NumColumns; j++)
                {
                    // Projet the cell position to Map
                    Coordinate cellCenter = output.CellToProj(i, j);
                    index1 = input1.ProjToCell(cellCenter);

                    double val;
                    if (index1.Row <= input1.EndRow && index1.Column <= input1.EndColumn && index1.Row > -1
                        && index1.Column > -1)
                    {
                        val = input1.Value[index1.Row, index1.Column] == input1.NoDataValue
                                  ? output.NoDataValue
                                  : input1.Value[index1.Row, index1.Column];
                    }
                    else
                    {
                        val = output.NoDataValue;
                    }

                    output.Value[i, j] = val;

                    if (cancelProgressHandler.Cancel)
                    {
                        return false;
                    }
                }

                int current = Convert.ToInt32(Math.Round(i * 100D / max));

                // only update when increment in persentage
                if (current > previous)
                {
                    cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted);
                }

                previous = current;
            }

            // output = Temp;
            output.Save();
            return true;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Creates a new image and world file, placing the default bounds at the origin, with one pixel per unit.
        /// </summary>
        /// <param name="fileName">
        /// The string fileName
        /// </param>
        /// <param name="width">
        /// The integer width
        /// </param>
        /// <param name="height">
        /// The integer height
        /// </param>
        /// <param name="bandType">The ImageBandType that clarifies how the separate bands are layered in the image.</param>
        public override IImageData Create(string fileName, int width, int height, ImageBandType bandType)
        {
            Filename = fileName;
            WorldFile = new WorldFile();
            double[] aff = new[] { 1.0, 0, 0, -1.0, 0, height };
            Bounds = new RasterBounds(height, width, aff);
            WorldFile.Filename = WorldFile.GenerateFilename(fileName);
            Width = width;
            Height = height;
            _myImage = new Bitmap(width, height);
            string ext = Path.GetExtension(fileName);
            switch (ext)
            {
                case ".bmp":
                    _myImage.Save(fileName, ImageFormat.Bmp);
                    break;
                case ".emf":
                    _myImage.Save(fileName, ImageFormat.Emf);
                    break;
                case ".exf":
                    _myImage.Save(fileName, ImageFormat.Exif);
                    break;
                case ".gif":
                    _myImage.Save(fileName, ImageFormat.Gif);
                    break;
                case ".ico":
                    _myImage.Save(fileName, ImageFormat.Icon);
                    break;
                case ".jpg":
                    _myImage.Save(fileName, ImageFormat.Jpeg);
                    break;
                case ".mbp":
                    _myImage.Save(fileName, ImageFormat.MemoryBmp);
                    break;
                case ".png":
                    _myImage.Save(fileName, ImageFormat.Png);
                    break;
                case ".tif":
                    _myImage.Save(fileName, ImageFormat.Tiff);
                    break;
                case ".wmf":
                    _myImage.Save(fileName, ImageFormat.Wmf);
                    break;
            }

            NumBands = 4;
            BytesPerPixel = 4;
            CopyBitmapToValues();

            return this;
        }
Exemplo n.º 16
0
        /// <summary>
        /// For big images the scale that is just one step larger than the specified window will be used.
        /// </summary>
        /// <param name="envelope"></param>
        /// <param name="window"></param>
        /// <returns></returns>
        public override Bitmap GetBitmap(Extent envelope, Rectangle window)
        {
            if (window.Width == 0 || window.Height == 0) return null;
            if (_header == null) return null;
            if (_header.ImageHeaders == null) return null;
            if (_header.ImageHeaders[0] == null) return null;

            Rectangle expWindow = window.ExpandBy(1);
            IEnvelope expEnvelope = envelope.ToEnvelope().Reproportion(window, expWindow);

            IEnvelope env = expEnvelope.Intersection(Bounds.Extent.ToEnvelope());
            if (env == null || env.IsNull || env.Height == 0 || env.Width == 0) return null;

            PyramidImageHeader he = _header.ImageHeaders[0];
            int scale;

            double cwa = expWindow.Width / expEnvelope.Width;
            double cha = expWindow.Height / expEnvelope.Height;

            for (scale = 0; scale < _header.ImageHeaders.Length; scale++)
            {
                PyramidImageHeader ph = _header.ImageHeaders[scale];

                if (cwa > ph.NumColumns / Bounds.Width || cha > ph.NumRows / Bounds.Height)
                {
                    if (scale > 0) scale -= 1;
                    break;
                }
                he = ph;
            }

            RasterBounds overviewBounds = new RasterBounds(he.NumRows, he.NumColumns, he.Affine);
            Rectangle r = overviewBounds.CellsContainingExtent(envelope);
            if (r.Width == 0 || r.Height == 0) return null;
            byte[] vals = ReadWindow(r.Y, r.X, r.Height, r.Width, scale);
            Bitmap bmp = new Bitmap(r.Width, r.Height);
            BitmapData bData = bmp.LockBits(new Rectangle(0, 0, r.Width, r.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            bmp.UnlockBits(bData);

            // Use the cell coordinates to determine the affine coefficients for the cells retrieved.
            double[] affine = new double[6];
            Array.Copy(he.Affine, affine, 6);
            affine[0] = affine[0] + r.X * affine[1] + r.Y * affine[2];
            affine[3] = affine[3] + r.X * affine[4] + r.Y * affine[5];

            if (window.Width == 0 || window.Height == 0)
            {
                return null;
            }

            Bitmap result = new Bitmap(window.Width, window.Height);
            Graphics g = Graphics.FromImage(result);

            //

            // Gets the scaling factor for converting from geographic to pixel coordinates
            double dx = (window.Width / envelope.Width);
            double dy = (window.Height / envelope.Height);

            double[] a = affine;

            // gets the affine scaling factors.
            float m11 = Convert.ToSingle(a[1] * dx);
            float m22 = Convert.ToSingle(a[5] * -dy);
            float m21 = Convert.ToSingle(a[2] * dx);
            float m12 = Convert.ToSingle(a[4] * -dy);
            float l = (float)(a[0] - .5 * (a[1] + a[2])); // Left of top left pixel
            float t = (float)(a[3] - .5 * (a[4] + a[5])); // top of top left pixel
            float xShift = (float)((l - envelope.MinX) * dx);
            float yShift = (float)((envelope.MaxY - t) * dy);
            g.Transform = new Matrix(m11, m12, m21, m22, xShift, yShift);
            g.PixelOffsetMode = PixelOffsetMode.Half;
            if (m11 > 1 || m22 > 1)
            {
                g.InterpolationMode = InterpolationMode.NearestNeighbor;
            }

            g.DrawImage(bmp, new PointF(0, 0));
            bmp.Dispose();
            g.Dispose();
            return result;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Opens the file, assuming that the fileName has already been specified using a Dot Net Image object
        /// </summary>
        public override void Open()
        {
            if (_myImage != null)
            {
                _myImage.Dispose();
            }

            using (FileStream stream = new FileStream(Filename, FileMode.Open))
            {
                using (Image temp = Image.FromStream(stream))
                {
                    _myImage = new Bitmap(temp.Width, temp.Height, PixelFormat.Format32bppArgb);
                    Width = temp.Width;
                    Height = temp.Height;
                    using (Graphics g = Graphics.FromImage(_myImage))
                    {
                        g.DrawImage(temp, new Rectangle(0, 0, temp.Width, temp.Height));
                    }
                }
            }
            WorldFile = new WorldFile(Filename);
            if (WorldFile.Affine == null)
                WorldFile.Affine = new[] { .5, 1.0, 0, _myImage.Height - .5, 0, -1.0 };
            Bounds = new RasterBounds(_myImage.Height, _myImage.Width, WorldFile.Affine);

            NumBands = 4;
            BytesPerPixel = 4;
            CopyBitmapToValues();
        }
Exemplo n.º 18
0
 /// <summary>
 /// This takes an original image and calculates the header content for all the lower resolution tiles.
 /// This does not actually write the bytes for those images.
 /// </summary>
 /// <param name="originalImage">The raster bounds for the original image.</param>
 public void CreateHeaders(RasterBounds originalImage)
 {
     Bounds = originalImage;
     CreateHeaders(originalImage.NumRows, originalImage.NumColumns, originalImage.AffineCoefficients);
 }
Exemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InRamImageData"/> class.
 /// Uses a bitmap and a geographic envelope in order to define a new imageData object.
 /// </summary>
 /// <param name="rawImage">
 /// The raw image.
 /// </param>
 /// <param name="bounds">
 /// The envelope bounds.
 /// </param>
 public InRamImageData(Bitmap rawImage, Extent bounds)
 {
     _myImage = rawImage;
     Width = _myImage.Width;
     Height = _myImage.Height;
     Bounds = new RasterBounds(_myImage.Height, _myImage.Width, bounds);
     MemorySetup();
 }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a new image and world file, placing the default bounds at the origin, with one pixel per unit.
        /// </summary>
        /// <param name="fileName">
        /// The string fileName
        /// </param>
        /// <param name="width">
        /// The integer width
        /// </param>
        /// <param name="height">
        /// The integer height
        /// </param>
        /// <param name="bandType">The ImageBandType that clarifies how the separate bands are layered in the image.</param>
        public override IImageData Create(string fileName, int width, int height, ImageBandType bandType)
        {
            Filename  = fileName;
            WorldFile = new WorldFile();
            double[] aff = new[] { 1.0, 0, 0, -1.0, 0, height };
            Bounds             = new RasterBounds(height, width, aff);
            WorldFile.Filename = WorldFile.GenerateFilename(fileName);
            Width    = width;
            Height   = height;
            _myImage = new Bitmap(width, height);
            string ext = Path.GetExtension(fileName);

            switch (ext)
            {
            case ".bmp":
                _myImage.Save(fileName, ImageFormat.Bmp);
                break;

            case ".emf":
                _myImage.Save(fileName, ImageFormat.Emf);
                break;

            case ".exf":
                _myImage.Save(fileName, ImageFormat.Exif);
                break;

            case ".gif":
                _myImage.Save(fileName, ImageFormat.Gif);
                break;

            case ".ico":
                _myImage.Save(fileName, ImageFormat.Icon);
                break;

            case ".jpg":
                _myImage.Save(fileName, ImageFormat.Jpeg);
                break;

            case ".mbp":
                _myImage.Save(fileName, ImageFormat.MemoryBmp);
                break;

            case ".png":
                _myImage.Save(fileName, ImageFormat.Png);
                break;

            case ".tif":
                _myImage.Save(fileName, ImageFormat.Tiff);
                break;

            case ".wmf":
                _myImage.Save(fileName, ImageFormat.Wmf);
                break;
            }

            NumBands      = 4;
            BytesPerPixel = 4;
            CopyBitmapToValues();

            return(this);
        }