Example #1
0
        public static Bitmap ConvertTiffInfoToBitmap(DoubleByteTiffInfo info)
        {
            var result = new Bitmap(info.Width, info.Height, PixelFormat.Format24bppRgb);

            byte[] buffer8Bit = null;

            for (int i = 0; i < info.Height; i++)
            {
                Rectangle  imgRect = new Rectangle(0, i, info.Width, 1);
                BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                if (buffer8Bit == null)
                {
                    buffer8Bit = new byte[imgData.Stride];
                }
                else
                {
                    Array.Clear(buffer8Bit, 0, buffer8Bit.Length);
                }

                for (int dst = 0, j = 0; j < info.Width; j++)
                {
                    ushort value16 = info.UshortBuffer[i][j];
                    buffer8Bit[dst++] = (byte)(value16 / 257.0 + 0.5);
                    buffer8Bit[dst++] = (byte)(value16 / 257.0 + 0.5);
                    buffer8Bit[dst++] = (byte)(value16 / 257.0 + 0.5);
                }

                Marshal.Copy(buffer8Bit, 0, imgData.Scan0, buffer8Bit.Length);
                result.UnlockBits(imgData);
            }
            return(result);
        }
Example #2
0
        private static void VerifyDoubleBitTiffInfo(DoubleByteTiffInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (info.UshortBuffer == null || info.UshortBuffer.Count <= 0)
            {
                throw new ArgumentNullException("info.UshortBuffer");
            }
            if (string.IsNullOrWhiteSpace(info.FilePath))
            {
                throw new ArgumentNullException("info.FilePath");
            }

            //如果不创建目录,图像不能生成
            var dir = Path.GetDirectoryName(info.FilePath);

            if (string.IsNullOrWhiteSpace(dir))
            {
                throw new NullReferenceException("dir");
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }
Example #3
0
        /// <summary>
        /// 读取一个16bit Tiff图像
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List <DoubleByteTiffInfo> ReadDoubleByteTiff(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException("path");
            }

            List <DoubleByteTiffInfo> tiffs = new List <DoubleByteTiffInfo>();

            using (var tiff = BitMiracle.LibTiff.Classic.Tiff.Open(path, ReadMode))
            {
                int width  = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
                int height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();

                byte[] scanline = new byte[tiff.ScanlineSize()];

                for (short i = 0; i < tiff.NumberOfDirectories(); i++)
                {
                    tiff.SetDirectory(i);
                    var doubleByteTiffInfo = new DoubleByteTiffInfo(width, height);

                    for (int j = 0; j < height; j++)
                    {
                        tiff.ReadScanline(scanline, j);
                        var scanline16Bit = new ushort[tiff.ScanlineSize() >> 1];
                        Buffer.BlockCopy(scanline, 0, scanline16Bit, 0, scanline.Length);
                        doubleByteTiffInfo.UshortBuffer.Add(scanline16Bit);
                    }
                    tiffs.Add(doubleByteTiffInfo);
                }
            }
            return(tiffs);
        }
Example #4
0
        public static void Create16BitGrayScaleTiff(DoubleByteTiffInfo info)
        {
            VerifyDoubleBitTiffInfo(info);

            using (var image = BitMiracle.LibTiff.Classic.Tiff.Open(info.FilePath, WriteMode))
            {
                SetTiffField(info, image);

                int    bytesPerRow = info.Width * sizeof(ushort);
                byte[] tempBuffer  = new byte[bytesPerRow];

                for (int i = 0; i < info.Height; i++)
                {
                    Buffer.BlockCopy(info.UshortBuffer[i], 0, tempBuffer, 0, bytesPerRow);
                    image.WriteScanline(tempBuffer, i);
                }
            }
        }
Example #5
0
        /// <summary>
        /// 纵向均匀切割Tiff图像,返回数据链表
        /// </summary>
        /// <param name="tiff"></param>
        /// <param name="count">切割的分块数</param>
        /// <returns></returns>
        public static List <DoubleByteTiffInfo> SliceVetically(DoubleByteTiffInfo tiff, int count)
        {
            var originalWidth = tiff.Width;
            var newWidth      = originalWidth / count;

            var newBytesWidth = newWidth * sizeof(ushort);

            var tiffLists = new List <DoubleByteTiffInfo>();


            for (int i = 0; i < count; i++)
            {
                DoubleByteTiffInfo info = new DoubleByteTiffInfo(newWidth, tiff.Height);

                for (int j = 0; j < tiff.Height; j++)
                {
                    ushort[] temp = new ushort[newWidth];
                    Buffer.BlockCopy(tiff.UshortBuffer[j], newBytesWidth * i, temp, 0, newBytesWidth);
                    info.UshortBuffer.Add(temp);
                }
                tiffLists.Add(info);
            }
            return(tiffLists);
        }