internal override void read(System.IO.BinaryReader reader) { preRead(reader); if (dirArray == null) { throw new ReadFileException("Not a TIFF file."); } TiffDirData uic2 = search(StkInfoCollection.UIC2Tag); if (uic2 == null) { throw new ReadFileException("Not a stack file."); } TiffDirData uic3 = search(StkInfoCollection.UIC3Tag); if (uic3 == null) { throw new ReadFileException("Not a stack file."); } numPlane = uic3.Data.Count; if (numPlane <= 0) { throw new ReadFileException("Not a stack file."); } postRead(reader); }
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); }
// Not all STK file info can be read : The STK documentation I used might be out of date? internal override void postRead(System.IO.BinaryReader reader) { System.IO.Stream stream = reader.BaseStream; List <TiffDirData> dirTemp = new List <TiffDirData>(); TiffDirData dir = search(StkInfoCollection.UIC2Tag); if (dir != null) { if (dir.Offset < 0) { throw new ReadFileException("Corrupted stack file."); } try { stream.Seek(dir.Offset, System.IO.SeekOrigin.Begin); UIC2data = new TiffData(TiffData.TIFFdataType.Rational, 3 * numPlane); UIC2data.read(reader); dirTemp.Add(dir); } catch { throw new ReadFileException("Corrupted stack file."); } } dir = search(StkInfoCollection.UIC3Tag); if (dir != null) { try { stream.Seek(dir.Offset, System.IO.SeekOrigin.Begin); UIC3data = new TiffData(TiffData.TIFFdataType.Rational, numPlane); UIC3data.read(reader); dirTemp.Add(dir); } catch { UIC3data = null; } } dir = search(StkInfoCollection.UIC4Tag); if (dir != null) { try { stream.Seek(dir.Offset, System.IO.SeekOrigin.Begin); readUIC4tagData(reader); dirTemp.Add(dir); } catch { UIC4data = null; } } dir = search(StkInfoCollection.UIC1Tag); if (dir != null) { try { stream.Seek(dir.Offset, System.IO.SeekOrigin.Begin); readUIC1tagData(reader, dir.Data.Count); dirTemp.Add(dir); } catch { UIC1data = null; } } for (int i = 0; i < dirArray.Length; i++) { if (dirArray[i].Tag == StkInfoCollection.UIC1Tag) { continue; } else if (dirArray[i].Tag == StkInfoCollection.UIC2Tag) { continue; } else if (dirArray[i].Tag == StkInfoCollection.UIC3Tag) { continue; } else if (dirArray[i].Tag == StkInfoCollection.UIC4Tag) { continue; } else if (dirArray[i].Offset > 0) { try { stream.Seek(dirArray[i].Offset, System.IO.SeekOrigin.Begin); dirArray[i].readData(reader); } catch { continue; } } dirTemp.Add(dirArray[i]); } dirArray = dirTemp.ToArray(); this.sort(); }
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); }