protected void getStrips(int fid, out uint[] stripPos, out uint[] stripCount, out uint rowsCount) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; try { Array temp = dataStruct.searchData(TiffInfoCollection.StripOffsets); if (temp is uint[]) { stripPos = temp as uint[]; } else { stripPos = new uint[temp.Length]; for (int i = 0; i < temp.Length; i++) { stripPos[i] = Convert.ToUInt32(temp.GetValue(i)); } } temp = dataStruct.searchData(TiffInfoCollection.StripByteCounts); if (temp is uint[]) { stripCount = temp as uint[]; } else { stripCount = new uint[temp.Length]; for (int i = 0; i < temp.Length; i++) { stripCount[i] = Convert.ToUInt32(temp.GetValue(i)); } } } catch { throw new ReadFileException("Unable to read image info: "); } try { rowsCount = Convert.ToUInt32(dataStruct.searchData(TiffInfoCollection.RowsPerStrip).GetValue(0)); } catch { int height, width; getImageSize(fid, out width, out height); rowsCount = Convert.ToUInt32(height / stripPos.Length); } }
/// <summary> /// Get image size. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <param name="height">Height.</param> /// <param name="width">Width.</param> public void getImageSize(int fid, out int width, out int height) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; try { width = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.ImageWidth).GetValue(0)); height = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.ImageLength).GetValue(0)); } catch { throw new ReadFileException("Unable to read image info: "); } if (width <= 0 || height <= 0) { throw new ReadFileException("Invalid image info."); } }
/// <summary> /// Get compression type. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Compression type. Read TIFF documentation for details.</returns> public MyTiffCompression.CompressionMethod getCompressionTagNumber(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; Array ca = dataStruct.searchData(TiffInfoCollection.Compression); if (ca == null || ca.Length == 0) { return(MyTiffCompression.CompressionMethod.UNCOMPRESSED); } return((MyTiffCompression.CompressionMethod)Convert.ToInt32(ca.GetValue(0))); }
/// <summary> /// Get planar configuration. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Planar configuration. Read TIFF documents</returns> public int getPlanarConfiguration(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; int pc = 0; try { pc = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.PlanarConfiguration).GetValue(0)); } catch { return(1); } return(pc); }
/// <summary> /// Get sample per pixel. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Number of samples per pixel. 1 for grayscale, 3 for RGB</returns> public int getSamplePerPixel(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; int spp = 0; try { spp = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.SamplesPerPixel).GetValue(0)); } catch { throw new ReadFileException("Unable to read image info: "); } return(spp); }
/// <summary> /// Determine if image data represent float point numbers. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>True if image data represent float-point numbers.</returns> public bool isDataFloatPoint(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; ushort nbits = 0; try { nbits = Convert.ToUInt16(dataStruct.searchData(TiffInfoCollection.SampleFormat).GetValue(0)); } catch { return(false); } return(nbits == 3); }
/// <summary> /// Determine if differencing was used for compression /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Predictor. Read TIFF documentation for details.</returns> public bool getDifferencePredictor(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; Array ca = dataStruct.searchData(TiffInfoCollection.DifferencingPredictor); if (ca == null || ca.Length == 0) { return(false); } else { return((MyTiffCompression.DifferencePredictor)Convert.ToInt32(ca.GetValue(0)) == MyTiffCompression.DifferencePredictor.Horizontal); } }
/// <summary> /// Get photo metric interpretation. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Photo metric interpretation. Read TIFF documentation for details.</returns> public int getPhotometricInterpretation(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; int pmi = 0; try { pmi = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.PhotometricInterpretation).GetValue(0)); } catch { return(1); } return(pmi); }
/// <summary> /// Get the image bit depth. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Image depth in number of bits.</returns> public int getNumBits(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; int nbits = 0; try { nbits = Convert.ToInt32(dataStruct.searchData(TiffInfoCollection.BitsPerSample).GetValue(0)); } catch { throw new ReadFileException("Unable to read image info: "); } return(nbits); }
/// <summary> /// Get colormap. /// </summary> /// <param name="fid">Zero-based index of images.</param> /// <returns>Colormap for palette-color images</returns> public byte[][] getColormap(int fid) { if (fid >= IFDs.Length) { throw new ReadFileException("Image # " + fid + " doesn't exist."); } TiffStruct dataStruct = IFDs[fid]; ushort[] temp; byte[][] colormap; int nbits; try { Array t = dataStruct.searchData(TiffInfoCollection.BitsPerSample); if (t == null || t.Length == 0) { return(null); } nbits = Convert.ToInt32(t.GetValue(0)); nbits = 1 << nbits; temp = dataStruct.searchData(TiffInfoCollection.ColorMap) as ushort[]; if (temp.Length != 3 * nbits) { nbits = temp.Length / 3; if (nbits != 256) { throw new ReadFileException("Colormap bit number error."); } } colormap = new byte[3][]; for (int k = 0; k < 3; k++) { colormap[k] = new byte[nbits]; } int highBits = 0; int lowBits = 0; for (int k = 1; k <= 3; k++) { highBits += temp[nbits - k] >> 8; lowBits += temp[nbits - k] & 0xff; } if (highBits > lowBits) { for (int k = 0; k < 3; k++) { for (int i = 0; i < nbits; i++) { colormap[k][i] = Convert.ToByte(temp[k * nbits + i] >> 8); } } } else { for (int k = 0; k < 3; k++) { for (int i = 0; i < nbits; i++) { colormap[k][i] = Convert.ToByte(temp[k * nbits + i] & 0xff); } } } } catch (Exception ex) { throw new ReadFileException("Unable to read image info: ", ex); } return(colormap); }