Example #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;
        }