Ejemplo n.º 1
0
        protected void prepareWriteStack(StkInfoCollection allInfo, int width, int height, ImageDataType dataType, int planeNum, ushort photometricInterpretation)
        {
            if (allInfo == null)
            {
                return;
            }
            stkInfo = allInfo;
            TiffWriter.getDataTypeInfo(dataType, out numberOfBits, out isFloatPoint);

            isPrepared   = false;
            planeCounter = 0;
            if (width <= 0 || height <= 0 || planeNum <= 0)
            {
                throw new WriteFileException("Invalid image parameters");
            }
            this.width    = width;
            this.height   = height;
            this.planeNum = planeNum;

            System.IO.Stream stream = writer.BaseStream;
            prepareWriteFile();

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.PhotometricInterpretation, "", photometricInterpretation));
            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));

            if (isFloatPoint)
            {
                allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.FloatPoint);
            }
            else
            {
                allInfo.setSampleFormat(TiffInfoCollection.SampleFormatType.UnsignedInteger);
            }

            if (photometricInterpretation <= 1 || photometricInterpretation == 3)
            {
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", (ushort)numberOfBits));
                byteCountsPerPlane = (uint)(width * height * numberOfBits / 8);
            }
            else if (photometricInterpretation == 2)
            {
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.BitsPerSample, "Bits per Sample", new ushort[] { (ushort)8, (ushort)8, (ushort)8 }));
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.SamplesPerPixel, "Sample per Pixel", (ushort)3));
                allInfo.forceAdd(new TiffInfo(TiffInfoCollection.PlanarConfiguration, "", (ushort)1));
                byteCountsPerPlane = (uint)(width * height * 3);
            }
            else
            {
                throw new Exception("Only support grayscale, palette-color, or RGB images.");
            }

            stripOffset = (uint)stream.Position;

            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.ImageWidth, "width", (uint)width));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.ImageLength, "height", (uint)height));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripOffsets, "strip Offsets", stripOffset));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.StripByteCounts, "strip Byte Counts", byteCountsPerPlane));
            allInfo.forceAdd(new TiffInfo(TiffInfoCollection.RowsPerStrip, "Rows per strip", (uint)height));
            MyTiffCompression.setCompressionTag(allInfo, CompressMethod, CompressLevel);
            MyTiffCompression.setHorizontalDifferencing(allInfo, HorizontalDifferencing);

            int[] missing;
            if (photometricInterpretation <= 1)
            {
                missing = allInfo.missingInfoGrayscale();
            }
            else if (photometricInterpretation == 2)
            {
                missing = allInfo.missingInfoRGB();
            }
            else
            {
                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 (!allInfo.validUIC2tag())
            {
                throw new WriteFileException("Invalid UIC2 data");
            }

            isPrepared = true;
        }
Ejemplo n.º 2
0
        internal override void setFromInfoCollection(TiffInfoCollection info)
        {
            base.setFromInfoCollection(info);

            StkInfoCollection allInfo = info as StkInfoCollection;

            if (allInfo == null)
            {
                return;
            }

            List <TiffDirData> dirTemp = new List <TiffDirData>();

            numPlane = allInfo.NumberOfPlanes;

            uint[] uic2 = new uint[6 * numPlane];

            if (allInfo.validUIC2tag())
            {
                for (int i = 0; i < Math.Min(numPlane, allInfo.ZDistance.Length); i++)
                {
                    if (allInfo.ZDistance[i] != null)
                    {
                        Array content = allInfo.ZDistance[i].Data.getContent();
                        if (content == null || content.Length <= 0)
                        {
                            continue;
                        }
                        uint[] temp = (uint[])content;
                        uic2[6 * i]     = temp[0];
                        uic2[6 * i + 1] = temp[1];
                    }
                    else
                    {
                        uic2[6 * i]     = 0;
                        uic2[6 * i + 1] = 0;
                    }
                }
                for (int i = 0; i < Math.Min(numPlane, allInfo.CreationTime.Length); i++)
                {
                    if (allInfo.CreationTime[i] != null)
                    {
                        Array content = allInfo.CreationTime[i].Data.getContent();
                        if (content == null || content.Length <= 0)
                        {
                            continue;
                        }
                        uint[] temp = (uint[])content;
                        uic2[6 * i + 2] = temp[0];
                        uic2[6 * i + 3] = temp[1];
                    }
                    else
                    {
                        uic2[6 * i + 2] = 0;
                        uic2[6 * i + 3] = 0;
                    }
                }
                for (int i = 0; i < Math.Min(numPlane, allInfo.ModifiedTime.Length); i++)
                {
                    if (allInfo.ModifiedTime[i] != null)
                    {
                        Array content = allInfo.ModifiedTime[i].Data.getContent();
                        if (content == null || content.Length <= 0)
                        {
                            continue;
                        }
                        uint[] temp = (uint[])(content);
                        uic2[6 * i + 4] = temp[0];
                        uic2[6 * i + 5] = temp[1];
                    }
                    else
                    {
                        uic2[6 * i + 4] = 0;
                        uic2[6 * i + 5] = 0;
                    }
                }
            }

            UIC2data = new TiffData(TiffData.TIFFdataType.Rational);
            UIC2data.setContent(uic2);
            TiffDirData ddTemp = new TiffDirData();

            ddTemp.Tag  = StkInfoCollection.UIC2Tag;
            ddTemp.Data = new TiffData(TiffData.TIFFdataType.Rational, numPlane);
            dirTemp.Add(ddTemp);


            if (allInfo.validUIC3tag())
            {
                uint[] uic3 = new uint[2 * numPlane];

                for (int i = 0; i < numPlane; i++)
                {
                    if (allInfo.Wavelength[i] != null)
                    {
                        uint[] temp = (uint[])(allInfo.Wavelength[i].Data.getContent());
                        uic3[2 * i]     = temp[0];
                        uic3[2 * i + 1] = temp[1];
                    }
                    else
                    {
                        uic3[2 * i]     = 0;
                        uic3[2 * i + 1] = 0;
                    }
                }
                UIC3data = new TiffData(TiffData.TIFFdataType.Rational);
                UIC3data.setContent(uic3);
                ddTemp      = new TiffDirData();
                ddTemp.Tag  = StkInfoCollection.UIC3Tag;
                ddTemp.Data = new TiffData(TiffData.TIFFdataType.Rational, numPlane);
                dirTemp.Add(ddTemp);
            }

            UIC4data = new SortedList <ushort, TiffData[]>();
            if (allInfo.validUIC4tag())
            {
                ddTemp      = new TiffDirData();
                ddTemp.Tag  = StkInfoCollection.UIC4Tag;
                ddTemp.Data = new TiffData(TiffData.TIFFdataType.Byte, numPlane);
                dirTemp.Add(ddTemp);

                foreach (KeyValuePair <ushort, List <TiffInfo> > pair in allInfo.UIC4DataDeepCopy)
                {
                    if (pair.Value.Count != numPlane)
                    {
                        continue;
                    }
                    TiffData[] temp = new TiffData[numPlane];
                    for (int j = 0; j < numPlane; j++)
                    {
                        temp[j] = (pair.Value)[j].Data;
                    }
                    UIC4data.Add(pair.Key, temp);
                }
            }

            UIC1data = new SortedList <uint, TiffData[]>();
            if (allInfo.validUIC1tag())
            {
                ddTemp     = new TiffDirData();
                ddTemp.Tag = StkInfoCollection.UIC1Tag;
                SortedList <uint, List <TiffInfo> > uic1Copy = allInfo.UIC1DataDeepCopy;
                ddTemp.Data = new TiffData(TiffData.TIFFdataType.Byte, uic1Copy.Count);
                dirTemp.Add(ddTemp);

                foreach (KeyValuePair <uint, List <TiffInfo> > pair in uic1Copy)
                {
                    TiffData[] temp = new TiffData[pair.Value.Count];
                    for (int i = 0; i < pair.Value.Count; i++)
                    {
                        temp[i] = pair.Value[i].Data;
                    }
                    UIC1data.Add(pair.Key, temp);
                }
            }

            TiffDirData[] newDir = new TiffDirData[dirArray.Length + dirTemp.Count];
            for (int i = 0; i < dirArray.Length; i++)
            {
                newDir[i] = dirArray[i];
            }
            for (int i = dirArray.Length; i < newDir.Length; i++)
            {
                newDir[i] = dirTemp[i - dirArray.Length];
            }
            dirArray = newDir;
            Array.Sort(dirArray);
        }