Exemple #1
0
 public static bool ReadFromFile(string fileName, ref MemoryBlock memBlock, ref TiffImageInfo tiffInfo)
 {
     IntPtr tiff = Open(fileName, "r");
     if (tiff == IntPtr.Zero)
     {
         throw new Exception("Unable to open TIFF file: " + fileName);
     }
     try
     {
         UInt16 i16buf = 0;   // буфер для чтения параметров
         GetIntField(tiff, FieldName.PLANARCONFIG, ref i16buf);
         tiffInfo.config = (PlanarConfig)i16buf;
         GetIntField(tiff, FieldName.BITSPERSAMPLE, ref tiffInfo.bps);
         GetIntField(tiff, FieldName.SAMPLESPERPIXEL, ref tiffInfo.spp);
         if ((tiffInfo.bps != 1 && tiffInfo.bps != 8) ||
             tiffInfo.spp != 1 ||
             tiffInfo.config != PlanarConfig.CONTIG)
         {
             throw new Exception("TIFF parameters do not meet requirements: bps = " + tiffInfo.bps + ", spp = " + tiffInfo.spp + ", config = " + tiffInfo.config);
         }
         GetIntField(tiff, FieldName.IMAGEWIDTH, ref tiffInfo.width);
         GetIntField(tiff, FieldName.IMAGELENGTH, ref tiffInfo.height);
         GetIntField(tiff, FieldName.PHOTOMETRIC, ref i16buf);
         tiffInfo.photometric = (Photometric)i16buf;
         if (tiffInfo.photometric != Photometric.MINISWHITE &&
             tiffInfo.photometric != Photometric.MINISBLACK)
         {
             tiffInfo.photometric = Photometric.MINISBLACK;
         }
         if (memBlock == null)
         {
             memBlock = new MemoryBlock();
         }
         int nSize = (int)((tiffInfo.width * tiffInfo.height * tiffInfo.bps) / 8);
         if (memBlock.SizeOf < nSize)
         {
             memBlock.Free();
             memBlock.Alloc(nSize);
         }
         IntPtr pMemory = memBlock.ToPointer();
         int scanSize = GetScanlineSize(tiff);
         IntPtr bufPtr = Marshal.AllocHGlobal(scanSize);
         byte[] oTemp = new byte[nSize];
         try
         {
             for (int row = 0; row < tiffInfo.height; row++)
             {
                 ReadScanline(tiff, bufPtr, row, 0);
                 int nDelta = (int)((tiffInfo.width * tiffInfo.bps) / 8 * row);
                 Marshal.Copy(bufPtr, oTemp, nDelta, scanSize);
             }
             Marshal.Copy(oTemp, 0, pMemory, nSize);
             return true;
         }
         finally
         {
             Marshal.FreeHGlobal(bufPtr);
         }
     }
     finally
     {
         Close(tiff);
     }
 }
Exemple #2
0
        public static bool ReadFromFile(string fileName, ref MemoryBlock memBlock, ref TiffImageInfo tiffInfo)
        {
            IntPtr tiff = Open(fileName, "r");

            if (tiff == IntPtr.Zero)
            {
                throw new Exception("Unable to open TIFF file: " + fileName);
            }
            try
            {
                UInt16 i16buf = 0;   // буфер для чтения параметров
                GetIntField(tiff, FieldName.PLANARCONFIG, ref i16buf);
                tiffInfo.config = (PlanarConfig)i16buf;
                GetIntField(tiff, FieldName.BITSPERSAMPLE, ref tiffInfo.bps);
                GetIntField(tiff, FieldName.SAMPLESPERPIXEL, ref tiffInfo.spp);
                if ((tiffInfo.bps != 1 && tiffInfo.bps != 8) ||
                    tiffInfo.spp != 1 ||
                    tiffInfo.config != PlanarConfig.CONTIG)
                {
                    throw new Exception("TIFF parameters do not meet requirements: bps = " + tiffInfo.bps + ", spp = " + tiffInfo.spp + ", config = " + tiffInfo.config);
                }
                GetIntField(tiff, FieldName.IMAGEWIDTH, ref tiffInfo.width);
                GetIntField(tiff, FieldName.IMAGELENGTH, ref tiffInfo.height);
                GetIntField(tiff, FieldName.PHOTOMETRIC, ref i16buf);
                tiffInfo.photometric = (Photometric)i16buf;
                if (tiffInfo.photometric != Photometric.MINISWHITE &&
                    tiffInfo.photometric != Photometric.MINISBLACK)
                {
                    tiffInfo.photometric = Photometric.MINISBLACK;
                }
                if (memBlock == null)
                {
                    memBlock = new MemoryBlock();
                }
                int nSize = (int)((tiffInfo.width * tiffInfo.height * tiffInfo.bps) / 8);
                if (memBlock.SizeOf < nSize)
                {
                    memBlock.Free();
                    memBlock.Alloc(nSize);
                }
                IntPtr pMemory  = memBlock.ToPointer();
                int    scanSize = GetScanlineSize(tiff);
                IntPtr bufPtr   = Marshal.AllocHGlobal(scanSize);
                byte[] oTemp    = new byte[nSize];
                try
                {
                    for (int row = 0; row < tiffInfo.height; row++)
                    {
                        ReadScanline(tiff, bufPtr, row, 0);
                        int nDelta = (int)((tiffInfo.width * tiffInfo.bps) / 8 * row);
                        Marshal.Copy(bufPtr, oTemp, nDelta, scanSize);
                    }
                    Marshal.Copy(oTemp, 0, pMemory, nSize);
                    return(true);
                }
                finally
                {
                    Marshal.FreeHGlobal(bufPtr);
                }
            }
            finally
            {
                Close(tiff);
            }
        }