Пример #1
0
        /// <summary>
        /// Write a single RGB image with default TIFF info. May throw WriteFileException.
        /// </summary>
        /// <param name="data">Row-major image data</param>
        /// <param name="width">Image width</param>
        /// <param name="height">Image height</param>
        public virtual void writeSingleRGBImage(byte[][] data, int width, int height)
        {
            TiffInfoCollection info = new TiffInfoCollection();

            info.setImageSize((uint)width, (uint)height);
            writeSingleRGBImage(data, info);
        }
Пример #2
0
        /// <summary>
        /// Write a single grayscle image with default TIFF info. May throw WriteFileException.
        /// </summary>
        /// <param name="data">Row-major image data</param>
        /// <param name="width">Image width</param>
        /// <param name="height">Image height</param>
        public virtual void writeSingleGrayscaleImage(Array data, int width, int height)
        {
            TiffInfoCollection info = new TiffInfoCollection();

            info.setImageSize((uint)width, (uint)height);
            writeSingleGrayscaleImage(data, info);
        }
Пример #3
0
        /// <summary>
        /// Write a single palette-color image with default TIFF info. May throw WriteFileException.
        /// </summary>
        /// <param name="data">Row-major image data</param>
        /// <param name="colormap">Colormap</param>
        /// <param name="width">Image width</param>
        /// <param name="height">Image height</param>
        public virtual void writeSinglePaletteColorImage(Array data, byte[][] colormap, int width, int height)
        {
            TiffInfoCollection info = new TiffInfoCollection();

            info.setImageSize((uint)width, (uint)height);
            writeSinglePaletteColorImage(data, colormap, info);
        }
Пример #4
0
 /// <summary>
 /// Write a single RGB color image. May throw WriteFileException.
 /// </summary>
 /// <param name="data">Image data in row-first order.</param>
 /// <param name="allInfo">Collection of all info.</param>
 public virtual void writeSingleRGBImage(byte[][] data, TiffInfoCollection allInfo)
 {
     if (allInfo == null)
     {
         throw new WriteFileException("Info collection is NULL");
     }
     prepareWriteFile();
     addRGBplane(data, allInfo);
 }
Пример #5
0
 /// <summary>
 /// Write a single palette color image. May throw WriteFileException.
 /// </summary>
 /// <param name="data">Image data in row-first order.</param>
 /// <param name="allInfo">Collection of all info.</param>
 public virtual void writeSinglePaletteColorImage(Array data, TiffInfoCollection allInfo)
 {
     if (allInfo == null)
     {
         throw new WriteFileException("Info collection is NULL");
     }
     prepareWriteFile();
     addPaletteColorPlane(data, allInfo);
 }
Пример #6
0
        /// <summary>
        ///Return a deep copy of this object
        /// </summary>
        /// <returns>A deep copy</returns>
        public virtual object Clone()
        {
            TiffInfoCollection copy = new TiffInfoCollection();

            foreach (KeyValuePair <ushort, TiffInfo> pair in allInfo)
            {
                copy.allInfo.Add(pair.Key, pair.Value.Clone() as TiffInfo);
            }
            return(copy);
        }
Пример #7
0
        internal virtual TiffInfoCollection toInfoCollection()
        {
            TiffInfoCollection col = new TiffInfoCollection();

            for (int i = 0; i < dirArray.Length; i++)
            {
                col.add(new TiffInfo(dirArray[i].Tag, "", dirArray[i].Data));
            }

            return(col);
        }
Пример #8
0
 public static void setCompressionTag(TiffIO.TiffInfoCollection info, CompressionMethod compID, CompressionLevel compressLevel)
 {
     if (compressLevel == CompressionLevel.NoCompression)
     {
         info.forceAdd(new TiffInfo(TiffInfoCollection.Compression, "Compression", (ushort)CompressionMethod.UNCOMPRESSED));
     }
     else
     {
         info.forceAdd(new TiffInfo(TiffInfoCollection.Compression, "Compression", Convert.ToUInt16(compID)));
     }
 }
Пример #9
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);
        }
Пример #10
0
        /// <summary>
        /// Add the next RGB image
        /// </summary>
        /// <param name="data">Image data</param>
        /// <param name="allInfo">TIFF info</param>
        public void addRGBplane(byte[][] data, TiffInfoCollection allInfo)
        {
            System.IO.Stream stream = writer.BaseStream;

            allInfo.add(new TiffInfo(TiffInfoCollection.PhotometricInterpretation, "", (ushort)2));
            allInfo.add(new TiffInfo(TiffInfoCollection.XResolution, "", (uint)1, (uint)1));
            allInfo.add(new TiffInfo(TiffInfoCollection.YResolution, "", (uint)1, (uint)1));
            allInfo.add(new TiffInfo(TiffInfoCollection.ResolutionUnit, "", (ushort)1));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.SamplesPerPixel, "", (ushort)3));

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.PlanarConfiguration, "", (ushort)1));
            MyTiffCompression.setCompressionTag(allInfo, CompressMethod, CompressLevel);
            MyTiffCompression.setHorizontalDifferencing(allInfo, HorizontalDifferencing);

            ushort[] nbits = new ushort[] { (ushort)8, (ushort)8, (ushort)8 };
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", nbits));

            uint width, height;

            allInfo.getImageSize(out width, out height);
            uint colorBytes        = width * height;
            uint totOrigByteCounts = colorBytes * 3;

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripOffsets, "strip Offsets", toWordBoundary()));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripByteCounts, "strip Byte Counts", totOrigByteCounts));

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.RowsPerStrip, "Rows per strip", height));

            int[] missing = allInfo.missingInfoRGB();
            if (missing.Length > 0)
            {
                String msg = "Missing tags: ";
                for (int i = 0; i < missing.Length; i++)
                {
                    msg += missing[i] + " ";
                }
                throw new WriteFileException(msg);
            }

            if (BufferSize < MIN_BUFFER_SIZE)
            {
                BufferSize = MIN_BUFFER_SIZE;
            }
            writeRGBImageDataStrips(new byte[1][][] { data }, totOrigByteCounts, width, height, allInfo);

            finalizeImageWriting(allInfo, new TiffStruct());
        }
Пример #11
0
 protected void dealWithBits(Array data, TiffInfoCollection allInfo, out uint nBits, out ushort[][] data16bit, out byte[][] data8bit, out uint[][] data32bit, out float[][] dataFloat)
 {
     data16bit = null;
     data8bit  = null;
     data32bit = null;
     dataFloat = null;
     if (data is ushort[])
     {
         nBits     = 16;
         data16bit = new ushort[1][] { data as ushort[] };
         allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", (ushort)16));
         allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.UnsignedInteger);
     }
     else if (data is byte[])
     {
         nBits    = 8;
         data8bit = new byte[1][] { data as byte[] };
         allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", (ushort)8));
         allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.UnsignedInteger);
     }
     else if (data is float[])
     {
         nBits     = 32;
         dataFloat = new float[1][] { data as float[] };
         allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", (ushort)32));
         allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.FloatPoint);
     }
     else if (data is uint[])
     {
         nBits     = 32;
         data32bit = new uint[1][] { data as uint[] };
         allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", (ushort)32));
         allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.UnsignedInteger);
     }
     else
     {
         throw new WriteFileException("Can only write 8/16/32 bit data, unsigned integer or single-precision float-point.");
     }
 }
Пример #12
0
        internal virtual void setFromInfoCollection(TiffInfoCollection allInfo)
        {
            List <TiffDirData> dirList = new List <TiffDirData>();

            foreach (KeyValuePair <ushort, TiffInfo> pair in allInfo.InfoCollection)
            {
                if (pair.Value.Data == null)
                {
                    continue;
                }
                if (pair.Value.Data.Count == 0)
                {
                    continue;
                }
                TiffDirData temp = new TiffDirData();
                temp.Tag  = pair.Value.Tag;
                temp.Data = pair.Value.Data.Clone() as TiffData;
                dirList.Add(temp);
            }
            dirArray = dirList.ToArray();
            sort();
        }
Пример #13
0
        public static void addColormapToTiffInfo(TiffInfoCollection allInfo, byte[][] colormap)
        {
            if (colormap.Length < 3)
            {
                throw new WriteFileException("Corrupted colormap.");
            }
            int mlen = colormap[0].Length;

            if (colormap[1].Length != mlen || colormap[2].Length != mlen)
            {
                throw new WriteFileException("Corrupted colormap.");
            }

            ushort[] maptemp = new ushort[3 * mlen];
            for (int k = 0; k < 3; k++)
            {
                for (int i = 0; i < mlen; i++)
                {
                    maptemp[k * mlen + i] = Convert.ToUInt16(colormap[k][i] * 256);
                }
            }

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.ColorMap, "Color Map", maptemp));
        }
Пример #14
0
        protected void writeRGBImageDataStrips(byte[][][] data, uint origByteCntPerPlane, uint width, uint height, TiffInfoCollection allInfo)
        {
            byte[]           buffer;
            System.IO.Stream stream = writer.BaseStream;

            if (CompressMethod == MyTiffCompression.CompressionMethod.UNCOMPRESSED)
            {
                buffer = new byte[BufferSize];
                for (int pn = 0; pn < data.Length; pn++)
                {
                    int  k         = 0;
                    int  curr      = 0;
                    uint leftSize  = origByteCntPerPlane;
                    uint writeSize = BufferSize;
                    while (leftSize > 0)
                    {
                        if (writeSize > leftSize)
                        {
                            writeSize = leftSize;
                        }
                        for (int i = 0; i < writeSize; i++)
                        {
                            buffer[i] = data[pn][k++][curr / 3];
                            curr++;
                            if (k == 3)
                            {
                                k = 0;
                            }
                        }
                        writer.Write(buffer, 0, Convert.ToInt32(writeSize));
                        leftSize -= writeSize;
                    }
                    if (PlaneWritten != null)
                    {
                        PlaneWritten(this, new ProgressChangedEventArgs(Convert.ToInt32(100.0 * pn / data.Length), null));
                    }
                }
            }
            else
            {
                uint nRows;
                int  nStrips;
                defineStripsGrayscale(origByteCntPerPlane, height, 3, out nRows, out nStrips);

                uint[] stripBytes, stripPos;
                stripBytes = new uint[nStrips * data.Length];
                stripPos   = new uint[nStrips * data.Length];
                int writeSize = Convert.ToInt32(origByteCntPerPlane / height * nRows);
                buffer = new byte[writeSize];
                int cnt = 0;

                for (int pn = 0; pn < data.Length; pn++)
                {
                    byte[][] dataTemp = new byte[3][];
                    for (int c = 0; c < 3; c++)
                    {
                        dataTemp[c] = data[pn][c];
                        if (HorizontalDifferencing)
                        {
                            dataTemp[c] = differencing(data[pn][c], (int)width, (int)height) as byte[];
                        }
                    }

                    int  curr     = 0;
                    int  k        = 0;
                    uint leftSize = origByteCntPerPlane;
                    while (leftSize > 0)
                    {
                        int currWriteSize = writeSize;
                        if (currWriteSize > leftSize)
                        {
                            currWriteSize = (int)leftSize;
                        }
                        for (int i = 0; i < currWriteSize; i++)
                        {
                            buffer[i] = dataTemp[k++][curr / 3];
                            curr++;
                            if (k == 3)
                            {
                                k = 0;
                            }
                        }
                        stripPos[cnt]     = (uint)stream.Position;
                        stripBytes[cnt++] = Convert.ToUInt32(MyTiffCompression.compress(buffer, 0, currWriteSize, writer, CompressMethod, CompressLevel));
                        leftSize         -= (uint)currWriteSize;
                    }
                    if (PlaneWritten != null)
                    {
                        PlaneWritten(this, new ProgressChangedEventArgs(Convert.ToInt32(100.0 * pn / data.Length), null));
                    }
                }

                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripOffsets, "strip Offsets", stripPos));
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripByteCounts, "strip Byte Counts", stripBytes));
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.RowsPerStrip, "Rows per strip", nRows));
            }
        }
Пример #15
0
        /// <summary>
        /// Add the next palette color image
        /// </summary>
        /// <param name="data">Image data</param>
        /// <param name="allInfo">Tiff info</param>
        public void addPaletteColorPlane(Array data, TiffInfoCollection allInfo)
        {
            System.IO.Stream stream = writer.BaseStream;

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.PhotometricInterpretation, "", (ushort)3));
            allInfo.add(new TiffInfo(TiffInfoCollection.XResolution, "", (uint)1, (uint)1));
            allInfo.add(new TiffInfo(TiffInfoCollection.YResolution, "", (uint)1, (uint)1));
            allInfo.add(new TiffInfo(TiffInfoCollection.ResolutionUnit, "", (ushort)1));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.SamplesPerPixel, "", (ushort)1));
            MyTiffCompression.setCompressionTag(allInfo, CompressMethod, CompressLevel);
            MyTiffCompression.setHorizontalDifferencing(allInfo, HorizontalDifferencing);

            uint nBits = 0;

            ushort[][] data16bit = null;
            byte[][]   data8bit  = null;
            float[][]  dataFloat = null;
            uint[][]   data32bit = null;
            dealWithBits(data, allInfo, out nBits, out data16bit, out data8bit, out data32bit, out dataFloat);

            Array info = allInfo.getOneInfoData(TiffInfoCollection.ColorMap);

            if (info == null)
            {
                throw new WriteFileException("Invalid colormap.");
            }

            uint width, height;

            allInfo.getImageSize(out width, out height);
            uint byteCounts = width * height * nBits / 8;

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripOffsets, "strip Offsets", toWordBoundary()));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripByteCounts, "strip Byte Counts", byteCounts));

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.RowsPerStrip, "Rows per strip", height));

            int[] missing = allInfo.missingInfoPaletteColor();
            if (missing.Length > 0)
            {
                String msg = "Missing tags: ";
                for (int i = 0; i < missing.Length; i++)
                {
                    msg += missing[i] + " ";
                }
                throw new WriteFileException(msg);
            }

            if (nBits == 8)
            {
                writeGrayScaleImageDataStrips(data8bit, byteCounts, width, height, allInfo);
            }
            else if (nBits == 16)
            {
                writeGrayScaleImageDataStrips(data16bit, byteCounts, width, height, allInfo);
            }
            else if (dataFloat != null)
            {
                writeGrayScaleImageDataStrips(dataFloat, byteCounts, width, height, allInfo);
            }
            else
            {
                writeGrayScaleImageDataStrips(data32bit, byteCounts, width, height, allInfo);
            }

            finalizeImageWriting(allInfo, new TiffStruct());
        }
Пример #16
0
 /// <summary>
 /// Write a single palette color image. May throw WriteFileException.
 /// </summary>
 /// <param name="data">Image data in row-first order.</param>
 /// <param name="allInfo">Collection of all info.</param>
 public virtual void writeSinglePaletteColorImage(Array data, byte[][] colormap, TiffInfoCollection allInfo)
 {
     if (allInfo == null)
     {
         throw new WriteFileException("Info collection is NULL");
     }
     addColormapToTiffInfo(allInfo, colormap);
     writeSinglePaletteColorImage(data, allInfo);
 }
Пример #17
0
 /// <summary>
 /// Write a single grayscle image with given TIFF info. May throw WriteFileException.
 /// </summary>
 /// <param name="data">Row-major image data</param>
 /// <param name="width">Image width</param>
 /// <param name="height">Image height</param>
 /// <param name="info">Tiff info collection</param>
 public virtual void writeSingleGrayscaleImage(Array data, int width, int height, TiffInfoCollection info)
 {
     if (info == null)
     {
         throw new WriteFileException("Info collection is NULL");
     }
     info.setImageSize((uint)width, (uint)height);
     writeSingleGrayscaleImage(data, info);
 }