예제 #1
0
 private void SetStretchers()
 {
     if (_selectedBandNos.Length == 1)
     {
         if (_stretcherProvider != null)
         {
             //by chennan 20130403 修改单波段文件密度分割、线性填色错误问题
             ColorMapTable <int> oldColorMapTable = _colorMapTable;
             //
             object obj = _stretcherProvider.GetStretcher(_dataProvider.fileName, _dataProvider.DataType, _colorTableName, out _colorMapTable);
             if (obj != null)
             {
                 _stretcher = obj as Func <T, byte>;
             }
             else
             {
                 //by chennan 20130403 修改单波段文件密度分割、线性填色错误问题
                 _colorMapTable = oldColorMapTable;
                 //
                 st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
             }
         }
         else
         {
             st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
         }
     }
     else if (_selectedBandNos.Length == 3)
     {
         st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
         st2 = _dataProvider.GetRasterBand(_selectedBandNos[1]).Stretcher as IRgbStretcher <T>;
         st3 = _dataProvider.GetRasterBand(_selectedBandNos[2]).Stretcher as IRgbStretcher <T>;
     }
 }
예제 #2
0
 private void SetStretchers()
 {
     if (_selectedBandNos.Length == 1)
     {
         if (_stretcherProvider != null)
         {
             object obj = _stretcherProvider.GetStretcher(_dataProvider.fileName, _colorTableName, out _colorMapTable);
             if (obj != null)
             {
                 _stretcher = obj as Func <T, byte>;
             }
             else
             {
                 st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
             }
         }
         else
         {
             st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
         }
     }
     else if (_selectedBandNos.Length == 3)
     {
         st1 = _dataProvider.GetRasterBand(_selectedBandNos[0]).Stretcher as IRgbStretcher <T>;
         st2 = _dataProvider.GetRasterBand(_selectedBandNos[1]).Stretcher as IRgbStretcher <T>;
         st3 = _dataProvider.GetRasterBand(_selectedBandNos[2]).Stretcher as IRgbStretcher <T>;
     }
 }
예제 #3
0
        public ColorPalette GetColorPalette(IRgbStretcher <T> stretcher, ColorMapTable <T> colorMapTable)
        {
            ColorPalette   palette = (new Bitmap(1, 1, PixelFormat.Format8bppIndexed)).Palette;
            byte           minByte = 0, maxByte = 0;
            Func <T, byte> tobyte = stretcher.Stretcher;

            foreach (ColorMapItem <T> it in colorMapTable.Items)
            {
                minByte = tobyte(it.MinValue);
                maxByte = tobyte(it.MaxValue);
                for (int i = minByte; i < maxByte; i++)
                {
                    palette.Entries[i] = it.Color;
                }
            }
            return(palette);
        }
예제 #4
0
        private bool Write(string inputFileName, string outputFileName, string bandNos, string imageHanceFile, Action <int, string> progressCallback)
        {
            if (string.IsNullOrEmpty(inputFileName) || !File.Exists(inputFileName))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(outputFileName))
            {
                outputFileName = Path.Combine(Path.GetDirectoryName(inputFileName), Path.GetFileNameWithoutExtension(inputFileName) + ".tif");
            }
            //解析图像增强方案
            if (!string.IsNullOrEmpty(imageHanceFile) && File.Exists(imageHanceFile))
            {
                _grbProcessors = TryGetRgbProcess(imageHanceFile);
            }
            using (IRasterDataProvider inRaster = GeoDataDriver.Open(inputFileName) as IRasterDataProvider)
            {
                RasterIdentify identify = new RasterIdentify(inputFileName);
                //设置波段选择及拉伸器
                int[] bandNums = TryGetBandSet(bandNos);
                if (bandNums == null || (bandNums.Length != 1 && bandNums.Length != 3))
                {
                    Console.WriteLine("输入波段号参数错误!");
                    return(false);
                }
                foreach (int band in bandNums)
                {
                    if (band > inRaster.BandCount)
                    {
                        return(false);
                    }
                }
                IRgbStretcher <UInt16>[] stretchers = new IRgbStretcher <UInt16> [bandNums.Length];
                for (int i = 0; i < stretchers.Length; i++)
                {
                    stretchers[i] = new LinearRgbStretcherUInt16(0, 1000, 0, 255);
                }
                IRasterDataProvider outRaster = null;
                try
                {
                    if (progressCallback != null)
                    {
                        progressCallback(0, "开始进行文件格式转换...");
                    }
                    outRaster = CreatOutputRaster(outputFileName, inRaster, bandNums.Length, progressCallback);
                    int rowStep   = ComputeRowStep(inRaster);
                    int stepCount = (int)((inRaster.Height) / rowStep + 0.5f);
                    int curStep   = -1;
                    for (int i = 0; i < inRaster.Height; i += rowStep)
                    {
                        if (i + rowStep > inRaster.Height)
                        {
                            rowStep = inRaster.Height - i;
                        }
                        long bufferSize = inRaster.Width * rowStep;
                        switch (inRaster.DataType)
                        {
                        case enumDataType.UInt16:
                        {
                            curStep++;
                            int curp = (int)((curStep * 1.0f / stepCount) * 100);
                            if (progressCallback != null)
                            {
                                progressCallback(curp, "转换完成" + curp + "%");
                            }
                            UInt16[] dataBuffer = new UInt16[bufferSize];
                            if (bandNums.Length == 3)
                            {
                                Byte[] outRDatabuffer = new Byte[bufferSize];
                                Byte[] outGDatabuffer = new Byte[bufferSize];
                                Byte[] outBDatabuffer = new Byte[bufferSize];
                                unsafe
                                {
                                    //读取RGB三个波段的值并做拉伸处理存入相应buffer数组
                                    fixed(UInt16 *ptr = dataBuffer)
                                    {
                                        fixed(Byte *outRPtr = outRDatabuffer)
                                        {
                                            fixed(Byte *outGPtr = outGDatabuffer)
                                            {
                                                fixed(Byte *outBPtr = outBDatabuffer)
                                                {
                                                    IntPtr buffer = new IntPtr(ptr);

                                                    inRaster.GetRasterBand(bandNums[0]).Read(0, i, inRaster.Width, rowStep, buffer, inRaster.DataType, inRaster.Width, rowStep);
                                                    //R拉伸转换
                                                    IntPtr outRBuffer = new IntPtr(outRPtr);
                                                    byte * rptr0      = (byte *)outRBuffer;

                                                    for (int j = 0; j < dataBuffer.Length; j++)
                                                    {
                                                        stretchers[0].Stretch(dataBuffer[j], rptr0);
                                                        rptr0++;
                                                    }
                                                    inRaster.GetRasterBand(bandNums[1]).Read(0, i, inRaster.Width, rowStep, buffer, inRaster.DataType, inRaster.Width, rowStep);
                                                    //G拉伸转换
                                                    IntPtr outGBuffer = new IntPtr(outGPtr);
                                                    byte * gptr0      = (byte *)outGBuffer;

                                                    for (int j = 0; j < dataBuffer.Length; j++)
                                                    {
                                                        stretchers[0].Stretch(dataBuffer[j], gptr0);
                                                        gptr0++;
                                                    }
                                                    inRaster.GetRasterBand(bandNums[2]).Read(0, i, inRaster.Width, rowStep, buffer, inRaster.DataType, inRaster.Width, rowStep);
                                                    //B拉伸转换
                                                    IntPtr outBBuffer = new IntPtr(outBPtr);
                                                    byte * bptr0      = (byte *)outBBuffer;

                                                    for (int j = 0; j < dataBuffer.Length; j++)
                                                    {
                                                        stretchers[0].Stretch(dataBuffer[j], bptr0);
                                                        bptr0++;
                                                    }
                                                    //图像增强处理
                                                    RgbProcess(outRBuffer, outGBuffer, outBBuffer, outRDatabuffer.Length);
                                                    outRaster.GetRasterBand(1).Write(0, i, inRaster.Width, rowStep, outRBuffer, enumDataType.Byte, inRaster.Width, rowStep);
                                                    outRaster.GetRasterBand(2).Write(0, i, inRaster.Width, rowStep, outGBuffer, enumDataType.Byte, inRaster.Width, rowStep);
                                                    outRaster.GetRasterBand(3).Write(0, i, inRaster.Width, rowStep, outBBuffer, enumDataType.Byte, inRaster.Width, rowStep);
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                            else if (bandNums.Length == 1)
                            {
                                Byte[] outDatabuffer = new Byte[bufferSize];
                                unsafe
                                {
                                    //读取1个波段的值并做拉伸处理存入相应buffer数组
                                    fixed(UInt16 *ptr = dataBuffer)
                                    {
                                        fixed(Byte *outRPtr = outDatabuffer)
                                        {
                                            IntPtr buffer = new IntPtr(ptr);

                                            inRaster.GetRasterBand(bandNums[0]).Read(0, i, inRaster.Width, rowStep, buffer, inRaster.DataType, inRaster.Width, rowStep);
                                            //R拉伸转换
                                            IntPtr outRBuffer = new IntPtr(outRPtr);
                                            byte * rptr0      = (byte *)outRBuffer;

                                            for (int j = 0; j < dataBuffer.Length; j++)
                                            {
                                                stretchers[0].Stretch(dataBuffer[j], rptr0);
                                                rptr0++;
                                            }
                                            outRaster.GetRasterBand(1).Write(0, i, inRaster.Width, rowStep, outRBuffer, enumDataType.Byte, inRaster.Width, rowStep);
                                        }
                                    }
                                }
                            }
                            break;
                        }

                        default: break;
                        }
                    }
                    if (progressCallback != null)
                    {
                        progressCallback(100, "文件" + inputFileName + "转换完成");
                    }
                    return(true);
                }
                finally
                {
                    if (outRaster != null)
                    {
                        outRaster.Dispose();
                    }
                }
            }
        }
예제 #5
0
        private Func <T, byte> GetStretcherFromDataProvider(int bandNo)
        {
            IRgbStretcher <T> st = _dataProvider.GetRasterBand(bandNo).Stretcher as IRgbStretcher <T>;

            return(st.Stretcher);
        }