protected void createInstance(uint numPlane, TiffInfoCollection tifInfo) { allInfo = tifInfo.InfoCollection; this.numPlane = Convert.ToInt32(numPlane); StkInfoCollection stkInfo = null; if (tifInfo is StkInfoCollection) { stkInfo = tifInfo as StkInfoCollection; } if (stkInfo != null && stkInfo.NumberOfPlanes == numPlane) { for (uint i = 0; i < this.numPlane; i++) { zDistance.Add(stkInfo.ZDistance[i].Clone() as TiffInfo); creationTime.Add(stkInfo.CreationTime[i].Clone() as TiffInfo); modifiedTime.Add(stkInfo.ModifiedTime[i].Clone() as TiffInfo); wavelength.Add(stkInfo.Wavelength[i].Clone() as TiffInfo); } UIC1 = stkInfo.UIC1DataDeepCopy; UIC4 = stkInfo.UIC4DataDeepCopy; uint[] num, denom; if (!UIC4.ContainsKey(AbsoluteZ)) { num = new uint[numPlane]; denom = new uint[numPlane]; for (int i = 0; i < numPlane; i++) { denom[i] = 1; } setAbsoluteZUIC4(num, denom); setAbsoluteZUIC1(num, denom); } else { try { getAbsoluteZUIC4(out num, out denom); setAbsoluteZUIC1(num, denom); } catch { num = new uint[numPlane]; denom = new uint[numPlane]; for (int i = 0; i < numPlane; i++) { denom[i] = 1; } setAbsoluteZUIC4(num, denom); setAbsoluteZUIC1(num, denom); } } } else { createUICtagInfo(); } }
/// <summary> /// Write RGB image data to a stack image. Also test the availability of required info. May throw WriteFileException. /// </summary> /// <param name="data">Image data in a 3D jagged array, with dimesion number of planes * 3 * (height * width).</param> /// <param name="allInfo">Info collection to be written in the stack file.</param> public void writeRGBstack(byte[][][] data, int width, int height, StkInfoCollection allInfo) { if (data == null) { throw new WriteFileException("Invalid image data"); } if (allInfo == null) { throw new WriteFileException("Info collection is NULL"); } System.IO.Stream stream = writer.BaseStream; int planeNum = data.Length; if (planeNum != allInfo.NumberOfPlanes) { throw new WriteFileException("Incompatible image data and image info collection: Inconsistent plane number"); } int size = width * height; for (int i = 0; i < planeNum; i++) { if (data[i] == null || data[i].Length != 3) { throw new WriteFileException("Invalid image data"); } for (int k = 0; k < 3; k++) { if (data[i][k].Length != size) { throw new WriteFileException("Invalid image data"); } } } prepareWriteRGBstack(allInfo, width, height, planeNum); if (numberOfBits != 8) { throw new WriteFileException("Can only write 8bit RGB image."); } if (!isPrepared) { throw new WriteFileException("Failed to pre-write data"); } if (BufferSize < MIN_BUFFER_SIZE) { BufferSize = MIN_BUFFER_SIZE; } writeRGBImageDataStrips(data, byteCountsPerPlane, (uint)width, (uint)height, allInfo); finalizeImageWriting(allInfo, new StkStruct()); }
/// <summary> /// Write grayscale image data to a stack image. Also test the availability of required info. May throw WriteFileException. /// </summary> /// <param name="data">Image data in a 2D jagged array, with dimesion number of planes * (height * width).</param> /// <param name="allInfo">Info collection to be written in the stack file.</param> public void writeGrayscaleStack(Array[] data, int width, int height, StkInfoCollection allInfo) { if (data == null || data.Length == 0) { throw new WriteFileException("Empty image data"); } if (allInfo == null) { throw new WriteFileException("Info collection is NULL"); } System.IO.Stream stream = writer.BaseStream; int planeNum = data.Length; if (planeNum != allInfo.NumberOfPlanes) { throw new WriteFileException("Incompatible image data and image info collection: Inconsistent plane number"); } int size = width * height; for (int i = 0; i < planeNum; i++) { if (data[i] == null || data[i].Length != size) { throw new WriteFileException("Invalid image data"); } } prepareWriteGrayscaleStack(allInfo, width, height, TiffWriter.getArrayDataType(data[0]), planeNum); if (!isPrepared) { throw new WriteFileException("Failed to pre-write data"); } if (BufferSize < MIN_BUFFER_SIZE) { BufferSize = MIN_BUFFER_SIZE; } BufferSize = BufferSize / 4 * 4; writeGrayScaleImageDataStrips(data, byteCountsPerPlane, (uint)width, (uint)height, allInfo); finalizeImageWriting(allInfo, new StkStruct()); }
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; }
public void prepareWriteRGBstack(StkInfoCollection allInfo, int width, int height) { prepareWriteStack(allInfo, width, height, ImageDataType.Byte, (ushort)2); }
public void prepareWritePaletteColorStack(StkInfoCollection allInfo, int width, int height, ImageDataType dataType, byte[][] colormap) { addColormapToTiffInfo(allInfo, colormap); prepareWriteStack(allInfo, width, height, dataType, (ushort)3); }
public void prepareWriteGrayscaleStack(StkInfoCollection allInfo, int width, int height, ImageDataType dataType) { prepareWriteStack(allInfo, width, height, dataType, (ushort)1); }
internal override TiffInfoCollection toInfoCollection() { StkInfoCollection infoC = new StkInfoCollection(Convert.ToUInt32(numPlane), base.toInfoCollection()); TiffDirData uic2test = search(StkInfoCollection.UIC2Tag); if (uic2test == null || UIC2data == null) { return(infoC); } uint[] uicContent = (uint[])UIC2data.getContent(); if (uicContent.Length < 6 * numPlane) { return(infoC); } for (int i = 0; i < numPlane; i++) { infoC.setZdistance(uicContent[6 * i], uicContent[6 * i + 1], i); infoC.setCreationDatetime(uicContent[6 * i + 2], uicContent[6 * i + 3], i); infoC.setModifiedDatetime(uicContent[6 * i + 4], uicContent[6 * i + 5], i); } TiffDirData uic3test = search(StkInfoCollection.UIC3Tag); if (uic3test != null && UIC3data != null) { uicContent = (uint[])UIC3data.getContent(); if (uicContent.Length < 2 * numPlane) { return(infoC); } for (int i = 0; i < numPlane; i++) { infoC.setWavelength(uicContent[2 * i], uicContent[2 * i + 1], i); } } TiffDirData uic4test = search(StkInfoCollection.UIC4Tag); if (uic4test != null && UIC4data != null && UIC4data.Count > 0) { foreach (KeyValuePair <ushort, TiffData[]> pair in UIC4data) { if (pair.Value.Length != numPlane) { continue; } TiffInfo[] temp = new TiffInfo[numPlane]; for (int j = 0; j < numPlane; j++) { temp[j] = new TiffInfo(StkInfoCollection.UIC4Tag, "UIC4", (pair.Value)[j]); } infoC.forceAddUIC4Data(pair.Key, temp); } } TiffDirData uic1test = search(StkInfoCollection.UIC1Tag); if (uic1test != null && UIC1data != null && UIC1data.Count > 0) { foreach (KeyValuePair <uint, TiffData[]> pair in UIC1data) { TiffInfo[] uic1info = new TiffInfo[pair.Value.Length]; for (int i = 0; i < pair.Value.Length; i++) { uic1info[i] = new TiffInfo(StkInfoCollection.UIC1Tag, "UIC1", pair.Value[i]); } infoC.forceAddUIC1Data(pair.Key, uic1info); } } return(infoC); }
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); }