Exemple #1
0
        protected void getStrips(int fid, out uint[] stripPos, out uint[] stripCount, out uint rowsCount)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];

            try
            {
                Array temp = dataStruct.searchData(TiffInfoCollection.StripOffsets);
                if (temp is uint[])
                {
                    stripPos = temp as uint[];
                }
                else
                {
                    stripPos = new uint[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                    {
                        stripPos[i] = Convert.ToUInt32(temp.GetValue(i));
                    }
                }

                temp = dataStruct.searchData(TiffInfoCollection.StripByteCounts);
                if (temp is uint[])
                {
                    stripCount = temp as uint[];
                }
                else
                {
                    stripCount = new uint[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                    {
                        stripCount[i] = Convert.ToUInt32(temp.GetValue(i));
                    }
                }
            }
            catch
            {
                throw new ReadFileException("Unable to read image info: ");
            }

            try {
                rowsCount = Convert.ToUInt32(dataStruct.searchData(TiffInfoCollection.RowsPerStrip).GetValue(0));
            } catch {
                int height, width;
                getImageSize(fid, out width, out height);
                rowsCount = Convert.ToUInt32(height / stripPos.Length);
            }
        }
Exemple #2
0
        internal virtual void finalizeImageWriting(TiffInfoCollection allInfo, TiffStruct fileData)
        {
            System.IO.Stream stream = writer.BaseStream;
            uint             tagPos = toWordBoundary();

            stream.Seek(nextIFDpos, System.IO.SeekOrigin.Begin);
            writer.Write(tagPos);

            stream.Seek(tagPos, System.IO.SeekOrigin.Begin);
            fileData.setFromInfoCollection(allInfo);
            fileData.NextIFD = 0;
            fileData.write(writer, out nextIFDpos);
        }
Exemple #3
0
        /// <summary>
        /// Get compression type.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Compression type. Read TIFF documentation for details.</returns>
        public MyTiffCompression.CompressionMethod getCompressionTagNumber(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            Array      ca         = dataStruct.searchData(TiffInfoCollection.Compression);

            if (ca == null || ca.Length == 0)
            {
                return(MyTiffCompression.CompressionMethod.UNCOMPRESSED);
            }
            return((MyTiffCompression.CompressionMethod)Convert.ToInt32(ca.GetValue(0)));
        }
Exemple #4
0
        /// <summary>
        /// Get planar configuration.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Planar configuration. Read TIFF documents</returns>
        public int getPlanarConfiguration(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            int        pc         = 0;

            try {
                pc = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.PlanarConfiguration).GetValue(0));
            } catch {
                return(1);
            }

            return(pc);
        }
Exemple #5
0
        /// <summary>
        /// Get sample per pixel.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Number of samples per pixel. 1 for grayscale, 3 for RGB</returns>
        public int getSamplePerPixel(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            int        spp        = 0;

            try {
                spp = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.SamplesPerPixel).GetValue(0));
            } catch {
                throw new ReadFileException("Unable to read image info: ");
            }

            return(spp);
        }
Exemple #6
0
        /// <summary>
        /// Determine if image data represent float point numbers.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>True if image data represent float-point numbers.</returns>
        public bool isDataFloatPoint(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            ushort     nbits      = 0;

            try {
                nbits = Convert.ToUInt16(dataStruct.searchData(TiffInfoCollection.SampleFormat).GetValue(0));
            } catch {
                return(false);
            }

            return(nbits == 3);
        }
Exemple #7
0
        /// <summary>
        /// Determine if differencing was used for compression
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Predictor. Read TIFF documentation for details.</returns>
        public bool getDifferencePredictor(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            Array      ca         = dataStruct.searchData(TiffInfoCollection.DifferencingPredictor);

            if (ca == null || ca.Length == 0)
            {
                return(false);
            }
            else
            {
                return((MyTiffCompression.DifferencePredictor)Convert.ToInt32(ca.GetValue(0)) == MyTiffCompression.DifferencePredictor.Horizontal);
            }
        }
Exemple #8
0
        /// <summary>
        /// Read a piece of info.
        /// </summary>
        /// <param name="tag">Tag of the info to be read.</param>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>An instance of TIFFinfo that contains the info.</returns>
        public virtual TiffData getInfo(int tag, int fid)
        {
            if (fid < 0 || fid >= IFDs.Length)
            {
                return(null);
            }
            TiffStruct fileData = IFDs[fid];

            TiffDirData dir = fileData.search(tag);

            if (dir == null)
            {
                return(null);
            }
            if (dir.Offset > 0)
            {
                reader.BaseStream.Seek(dir.Offset, System.IO.SeekOrigin.Begin);
                dir.readData(reader);
            }
            return(dir.Data);
        }
Exemple #9
0
        /// <summary>
        /// Get photo metric interpretation.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Photo metric interpretation. Read TIFF documentation for details.</returns>
        public int getPhotometricInterpretation(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            int        pmi        = 0;

            try
            {
                pmi = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.PhotometricInterpretation).GetValue(0));
            }
            catch
            {
                return(1);
            }

            return(pmi);
        }
Exemple #10
0
        /// <summary>
        /// Get the image bit depth.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Image depth in number of bits.</returns>
        public int getNumBits(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];
            int        nbits      = 0;

            try
            {
                nbits = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.BitsPerSample).GetValue(0));
            }
            catch
            {
                throw new ReadFileException("Unable to read image info: ");
            }

            return(nbits);
        }
Exemple #11
0
        /// <summary>
        /// Get image size.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <param name="height">Height.</param>
        /// <param name="width">Width.</param>
        public void getImageSize(int fid, out int width, out int height)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];

            try
            {
                width  = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.ImageWidth).GetValue(0));
                height = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.ImageLength).GetValue(0));
            }
            catch
            {
                throw new ReadFileException("Unable to read image info: ");
            }

            if (width <= 0 || height <= 0)
            {
                throw new ReadFileException("Invalid image info.");
            }
        }
Exemple #12
0
        protected bool checkTIFF()
        {
            System.IO.Stream stream = reader.BaseStream;
            if (stream == null)
            {
                return(false);
            }

            byte[] fileType = new byte[2];

            List <TiffStruct> IFDlist = new List <TiffStruct>();

            try
            {
                stream.Seek(0, System.IO.SeekOrigin.Begin);
                stream.Read(fileType, 0, 2);
                if (fileType[1] == 'M' && fileType[0] == 'M')
                {
                    reader.IsLittleEndian = false;
                }
                else if (fileType[1] == 'I' && fileType[0] == 'I')
                {
                    reader.IsLittleEndian = true;
                }
                else
                {
                    return(false);
                }
                ushort num = reader.ReadUInt16();
                if (num != 42)
                {
                    return(false);
                }
                long firstIFD = reader.ReadUInt32();
                if (firstIFD <= 0)
                {
                    return(false);
                }

                while (firstIFD > 0)
                {
                    stream.Seek(firstIFD, System.IO.SeekOrigin.Begin);
                    TiffStruct ifd = createNewStruct();
                    ifd.read(reader);
                    IFDlist.Add(ifd);
                    firstIFD = ifd.NextIFD;
                }
            }
            catch (Exception ex)
            {
                if (IFDlist.Count == 0)
                {
                    if (ex is ReadFileException)
                    {
                        throw ex;
                    }
                    throw new ReadFileException("Unexpected error, " + ex.Message);
                }
            }
            finally
            {
                IFDs = IFDlist.ToArray();
            }
            return(true);
        }
Exemple #13
0
        /// <summary>
        /// Get colormap.
        /// </summary>
        /// <param name="fid">Zero-based index of images.</param>
        /// <returns>Colormap for palette-color images</returns>
        public byte[][] getColormap(int fid)
        {
            if (fid >= IFDs.Length)
            {
                throw new ReadFileException("Image # " + fid + " doesn't exist.");
            }

            TiffStruct dataStruct = IFDs[fid];

            ushort[] temp;
            byte[][] colormap;
            int      nbits;

            try {
                Array t = dataStruct.searchData(TiffInfoCollection.BitsPerSample);
                if (t == null || t.Length == 0)
                {
                    return(null);
                }
                nbits = Convert.ToInt32(t.GetValue(0));
                nbits = 1 << nbits;
                temp  = dataStruct.searchData(TiffInfoCollection.ColorMap) as ushort[];
                if (temp.Length != 3 * nbits)
                {
                    nbits = temp.Length / 3;
                    if (nbits != 256)
                    {
                        throw new ReadFileException("Colormap bit number error.");
                    }
                }

                colormap = new byte[3][];
                for (int k = 0; k < 3; k++)
                {
                    colormap[k] = new byte[nbits];
                }

                int highBits = 0;
                int lowBits  = 0;
                for (int k = 1; k <= 3; k++)
                {
                    highBits += temp[nbits - k] >> 8;
                    lowBits  += temp[nbits - k] & 0xff;
                }

                if (highBits > lowBits)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        for (int i = 0; i < nbits; i++)
                        {
                            colormap[k][i] = Convert.ToByte(temp[k * nbits + i] >> 8);
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < 3; k++)
                    {
                        for (int i = 0; i < nbits; i++)
                        {
                            colormap[k][i] = Convert.ToByte(temp[k * nbits + i] & 0xff);
                        }
                    }
                }
            } catch (Exception ex) {
                throw new ReadFileException("Unable to read image info: ", ex);
            }

            return(colormap);
        }