处理16位TIFF(增加亮度)

时间:2018-10-31 08:47:30

标签: c# console-application tiff

我收到错误消息'System.IndexOutOfRangeException:'索引超出了数组的范围。'在行输出处.WriteScanline(scanline,i);

我要从一个文件夹中提取文件,然后处理后保存到新文件夹。告诉我如何做。 这是libtiff.net.Link https://bitmiracle.github.io/libtiff.net/html/91d736ff-8648-4c2f-a87a-9d8d215416b8.htm

中提供的示例代码。
using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using BitMiracle.LibTiff.Classic;


namespace TiffConverterApp
{
   internal class Program   {

            public static void Main()
            {

            using (Tiff tiff = Tiff.Open(@"SampleData\abc.tiff", "r"))
                {
                    int width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
                    int height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
                    double dpiX = tiff.GetField(TiffTag.XRESOLUTION)[0].ToDouble();
                    double dpiY = tiff.GetField(TiffTag.YRESOLUTION)[0].ToDouble();

                    byte[] scanline = new byte[tiff.ScanlineSize()];
                    ushort[] scanline16Bit = new ushort[tiff.ScanlineSize() / 2];

                    using (Tiff output = Tiff.Open("processed.tif", "w"))
                    {
                        if (output == null)
                            return;

                        output.SetField(TiffTag.IMAGEWIDTH, width);
                        output.SetField(TiffTag.IMAGELENGTH, height);
                        output.SetField(TiffTag.BITSPERSAMPLE, 16);
                        output.SetField(TiffTag.SAMPLESPERPIXEL, 1);
                        output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
                        output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
                        output.SetField(TiffTag.ROWSPERSTRIP, 1);
                        output.SetField(TiffTag.COMPRESSION, Compression.LZW);

                        for (int i = 0; i < height; i++)
                        {
                            tiff.ReadScanline(scanline, i);
                            MultiplyScanLineAs16BitSamples(scanline, scanline16Bit, 16);
                            output.WriteScanline(scanline, i);

                        }
                    }

                    Process.Start("processed.tif");
                }
            }

            private static void MultiplyScanLineAs16BitSamples(byte[] scanline, ushort[] temp, ushort factor)
            {
                if (scanline.Length % 2 != 0)
                {
                    // each two bytes define one sample so there should be even number of bytes
                    throw new ArgumentException();
                }

                // pack all bytes to ushorts
                Buffer.BlockCopy(scanline, 0, temp, 0, scanline.Length);

                for (int i = 0; i < temp.Length; i++)
                    temp[i] *= factor;

                // unpack all ushorts to bytes
                Buffer.BlockCopy(temp, 0, scanline, 0, scanline.Length);
            }

    public static void AddToLog(string Msg)
        {
            try
            {
                string path = ConfigurationSettings.AppSettings["LogFilePath"];
                string TodayDate = DateTime.Now.ToString("ddMMyyyy");
                string LogPath = path + "\\" + TodayDate;
                if (!Directory.Exists(LogPath))
                    Directory.CreateDirectory(LogPath);
                FileStream fileStream = new FileStream(LogPath + "\\Log.log", FileMode.Append);
                StreamWriter streamWriter = new StreamWriter((Stream)fileStream);
                streamWriter.WriteLine(DateTime.Now.ToString() + ": " + Msg);
                streamWriter.Close();
                fileStream.Close();
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
    }
}

以下是堆栈跟踪

at BitMiracle.LibTiff.Classic.Internal.LZWCodec.LZWEncode(Byte[] buffer, Int32 offset, Int32 count, Int16 plane)
at BitMiracle.LibTiff.Classic.Internal.LZWCodec.predictor_encoderow(Byte[] buffer, Int32 offset, Int32 count, Int16 plane)
at BitMiracle.LibTiff.Classic.Internal.CodecWithPredictor.EncodeRow(Byte[] buffer, Int32 offset, Int32 count, Int16 plane)
at BitMiracle.LibTiff.Classic.Tiff.WriteScanline(Byte[] buffer, Int32 offset, Int32 row, Int16 plane)
at BitMiracle.LibTiff.Classic.Tiff.WriteScanline(Byte[] buffer, Int32 row)
at TiffConverterApp.Program.Main() in D:\TiffProject\TiffConverterApp\TiffConverterApp\Program.cs:line 52

0 个答案:

没有答案