Read the IFDs contained in the Tiff file.
548882
private void readIFDs() {
    int offset0 = 0;
    try {
      // The pointer to the first IFD is located in bytes 4-7
      offset0 = data.readLong(4).toInt();
      tiffModel.setFirstIFDOffset(offset0);
      if (offset0 == 0)
        validation.addErrorLoc("There is no first IFD", "Header");
      else if (offset0 > data.size())
        validation.addErrorLoc("Incorrect offset", "Header");
    } catch (Exception ex) {
      validation.addErrorLoc("IO exception", "Header");
    }
      
    if (validation.isCorrect()) {
      int nifd = 1;
      try {
        IfdReader ifd0 = readIFD(offset0, true, 0);
        HashSet<Integer> usedOffsets = new HashSet<Integer>();
        usedOffsets.add(offset0);
        if (ifd0.getIfd() == null) {
          validation.addErrorLoc("Parsing error in first IFD", "IFD" + 0);
        } else {
          IFD iifd = ifd0.getIfd();
          iifd.setNextOffset(ifd0.getNextIfdOffset());
          tiffModel.addIfd0(iifd);

          IfdReader current_ifd = ifd0;

          // Read next IFDs
          boolean stop = false;
          while (current_ifd.getNextIfdOffset() > 0 && !stop) {
            if (usedOffsets.contains(current_ifd.getNextIfdOffset())) {
              // Circular reference
              validation.addErrorLoc("IFD offset already used", "IFD" + nifd);
              stop = true;
            } else if (current_ifd.getNextIfdOffset() > data.size()) {
              validation.addErrorLoc("Incorrect offset", "IFD" + nifd);
              stop = true;
            } else {
              usedOffsets.add(current_ifd.getNextIfdOffset());
              IfdReader next_ifd = readIFD(current_ifd.getNextIfdOffset(), true, nifd);
              if (next_ifd == null) {
                validation.addErrorLoc("Parsing error in IFD " + nifd, "IFD" + nifd);
                stop = true;
              } else {
                iifd = next_ifd.getIfd();
                iifd.setNextOffset(next_ifd.getNextIfdOffset());
                current_ifd.getIfd().setNextIFD(iifd);
                current_ifd = next_ifd;
              }
              nifd++;
            }
          }
        }
      } catch (Exception ex) {
        validation.addErrorLoc("IFD parsing error", "IFD" + nifd);
      }

      try {
        tiffModel.createMetadataDictionary();
      } catch (Exception ex) {
      }
    }
  }
684078
def idfreader(fname, iddfile, conv=True):
    """"""
    data, commdct, idd_index = readidf.readdatacommdct(fname, iddfile=iddfile)
    if conv:
        convertallfields(data, commdct)
    # fill gaps in idd
    ddtt, dtls = data.dt, data.dtls
    # skiplist = ["TABLE:MULTIVARIABLELOOKUP"]
    nofirstfields = iddgaps.missingkeys_standard(
        commdct, dtls,
        skiplist=["TABLE:MULTIVARIABLELOOKUP"])
    iddgaps.missingkeys_nonstandard(None, commdct, dtls, nofirstfields)
    bunchdt = makebunches(data, commdct)
    return bunchdt, data, commdct, idd_index
387006
private void readTiles() {
    ImageTiles imageTiles = new ImageTiles();
    List<Tile> tiles = new ArrayList<Tile>();
    if (ifd.containsTagId(TiffTags.getTagId("TileOffsets"))
        && ifd.containsTagId(TiffTags.getTagId("TileBYTECounts"))
        && ifd.containsTagId(TiffTags.getTagId("TileLength"))
        && ifd.containsTagId(TiffTags.getTagId("TileWidth"))) {
      long totalWidth = ifd.getTag("ImageWidth").getFirstNumericValue();
      long totalLength = ifd.getTag("ImageLength").getFirstNumericValue();
      long tilesWidth = ifd.getTag("TileWidth").getFirstNumericValue();
      long tilesHeight = ifd.getTag("TileLength").getFirstNumericValue();
      int actualWidth = 0;
      long actualHeight = tilesHeight;
      for (int i = 0; i < ifd.getTag("TileOffsets").getValue().size(); i++) {
        try {
          int to = ifd.getTag("TileOffsets").getValue().get(i).toInt();
          Tile tile = new Tile();
          tile.setOffset(to);
          tile.setWidth((int) tilesWidth);
          tile.setHeight((int) tilesHeight);
          long padX = 0;
          long padY = 0;
          boolean newLine = false;
          actualWidth += tilesWidth;
          if (actualWidth > totalWidth) {
            padX = tilesWidth - actualWidth % totalWidth;
            newLine = true;
          }
          if (actualHeight > totalLength) {
            padY = tilesHeight - actualHeight % totalLength;
          }
          tile.setPadding((int) padX, (int) padY);
          if (newLine) {
            actualHeight += tilesHeight;
            actualWidth = 0;
          }
          tiles.add(tile);
        } catch (Exception ex) {

        }
      }
      imageTiles.setTiles(tiles);

      imageTiles.setTileHeight((int) tilesHeight);
      imageTiles.setTileWidth((int) tilesWidth);
    }
    ifd.setImageTiles(imageTiles);
  }
135723
private void validateIfd(IFD ifd, int n) {
    boolean hasSubIfd = ifd.hasSubIFD();
    boolean thumbnail = hasSubIfd && ifd.getsubIFD().getImageSize() > ifd.getImageSize();
    boolean structureOk = false;

    IfdTags metadata;
    if (!hasSubIfd) {
      validation.addErrorLoc("Missing Sub IFD", "IFD" + n);
      metadata = ifd.getMetadata();
    } else if (!thumbnail) {
      validation.addErrorLoc("Sub IFD image is not bigger than the main image", "IFD" + n);
      metadata = ifd.getMetadata();
    } else {
      metadata = ifd.getsubIFD().getMetadata();
      structureOk = true;
    }

    checkRequiredTag(metadata, "ImageLength", 1, "IFD" + n);
    checkRequiredTag(metadata, "ImageWidth", 1, "IFD" + n);
    int spp = -1;
    if (checkRequiredTag(metadata, "SamplesPerPixel", 1, "IFD" + n)) {
      spp = (int) metadata.get("SamplesPerPixel").getFirstNumericValue();
      checkRequiredTag(metadata, "BitsPerSample", spp, "IFD" + n);
    }
    if (n == 0)
      checkRequiredTag(metadata, "ImageDescription", -1, "IFD" + n);
    if (checkRequiredTag(metadata, "Compression", 1, "IFD" + n)) {
      int comp = (int) metadata.get("Compression").getFirstNumericValue();
      if (comp == 1)
        checkForbiddenTag(metadata, "CompressedBitsPerPixel", "IFD" + n);
    }
    checkRequiredTag(metadata, "XResolution", 1, "IFD" + n);
    checkRequiredTag(metadata, "YResolution", 1, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "Make", -1, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "Model", -1, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "Software", -1, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "Copyright", -1, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "DateTimeOriginal", 20, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "DateTime", 20, "IFD" + n);
    if (n == 0)
      checkRequiredTag(metadata, "TIFFEPStandardID", 4, "IFD" + n);
    if (checkRequiredTag(metadata, "NewSubfileType", 1, new long[] {0, 1}, "IFD" + n)) {
      if (structureOk) {
        checkRequiredTag(ifd.getMetadata(), "NewSubfileType", 1, new long[] {1}, "IFD" + n);
        checkRequiredTag(metadata, "NewSubfileType", 1, new long[] {0}, "IFD" + n);
      }
      int nst = (int) metadata.get("NewSubfileType").getFirstNumericValue();
      if (nst != 0) {
        if (n == 0)
          checkRequiredTag(metadata, "SubIFDs", -1, "IFD" + n);
      }
    }
    if (checkRequiredTag(metadata, "PhotometricInterpretation", 1, new long[]{1, 2, 6, 32803,
        32767}, "IFD" + n)) {
      int photo = (int) metadata.get("PhotometricInterpretation").getFirstNumericValue();
      if (photo != 6) {
        checkForbiddenTag(metadata, "YCbCrCoefficients", "IFD" + n);
        checkForbiddenTag(metadata, "YCbCrSubSampling", "IFD" + n);
        checkForbiddenTag(metadata, "YCbCrPositioning", "IFD" + n);
        checkForbiddenTag(metadata, "ReferenceBlackWhite", "IFD" + n);
      }
      if (photo == 2 || photo == 3) {
        if (spp != 3) {
          validation.addError("Invalid SampesPerPixel value fo TiffEP", "IFD" + n, spp);
        }
      } else if (photo == 1 || photo == 32803) {
        if (spp != 1) {
          validation.addError("Invalid SampesPerPixel value fo TiffEP", "IFD" + n, spp);
        }
        if (photo == 32803) {
          checkRequiredTag(metadata, "CFARepeatPatternDim", 2, "IFD" + n);
          checkRequiredTag(metadata, "CFAPattern", -1, "IFD" + n);
        }
      }
    }
    checkRequiredTag(metadata, "PlanarConfiguration", 1, new long[] {1, 2}, "IFD" + n);
    checkRequiredTag(metadata, "ResolutionUnit", 1, new long[] {1, 2, 3}, "IFD" + n);
    if (metadata.containsTagId(TiffTags.getTagId("Orientation")))
      checkRequiredTag(metadata, "Orientation", 1, new long[] {1, 3, 6, 8, 9}, "IFD" + n);

    if (!thumbnail) {
      if (ifd.hasStrips()) {
        checkRequiredTag(metadata, "StripBYTECount", -1, "IFD" + n);
        checkRequiredTag(metadata, "StripOffsets", -1, "IFD" + n);
        checkRequiredTag(metadata, "RowsPerStrip", 1, "IFD" + n);
        if (ifd.hasTiles()) {
          validation.addErrorLoc("Image in both strips and tiles", "IFD" + n);
        }
      } else if (ifd.hasTiles()) {
        checkRequiredTag(metadata, "TileLength", 1, "IFD" + n);
        checkRequiredTag(metadata, "TileOffsets", 1, "IFD" + n);
        checkRequiredTag(metadata, "TileWidth", -1, "IFD" + n);
      }
    } else {
      checkRequiredTag(metadata, "StripBYTECount", -1, "IFD" + n);
      checkRequiredTag(metadata, "StripOffsets", -1, "IFD" + n);
      checkRequiredTag(metadata, "RowsPerStrip", 1, "IFD" + n);
      checkForbiddenTag(metadata, "TileLength", "IFD" + n);
      checkForbiddenTag(metadata, "TileOffsets", "IFD" + n);
      checkForbiddenTag(metadata, "TileWidth", "IFD" + n);
    }

    int nycbcr = 0;
    if (metadata.containsTagId(TiffTags.getTagId("YCbCrCoefficients")))
      nycbcr++;
    if (metadata.containsTagId(TiffTags.getTagId("YCbCrSubSampling")))
      nycbcr++;
    if (metadata.containsTagId(TiffTags.getTagId("YCbCrPositioning")))
      nycbcr++;
    if (metadata.containsTagId(TiffTags.getTagId("ReferenceBlackWhite")))
      nycbcr++;
    if (nycbcr > 0 && nycbcr != 4) {
      checkRequiredTag(metadata, "YCbCrCoefficients", 3, "IFD" + n);
      checkRequiredTag(metadata, "YCbCrSubSampling", 2, "IFD" + n);
      checkRequiredTag(metadata, "YCbCrPositioning", 1, "IFD" + n);
      checkRequiredTag(metadata, "ReferenceBlackWhite", 6, "IFD" + n);
    }
    if (thumbnail) {
      checkForbiddenTag(metadata, "YCbCrCoefficients", "IFD" + n);
      checkForbiddenTag(metadata, "YCbCrSubSampling", "IFD" + n);
      checkForbiddenTag(metadata, "YCbCrPositioning", "IFD" + n);
      checkForbiddenTag(metadata, "ReferenceBlackWhite", "IFD" + n);
    }

    checkForbiddenTag(metadata, "PrimaryChromaticities", "IFD" + n);
    checkForbiddenTag(metadata, "WhitePoint", "IFD" + n);
    checkForbiddenTag(metadata, "TransferFunction", "IFD" + n);

    if (metadata.containsTagId(TiffTags.getTagId("FocalPlaneResolutionUnit"))) {
      int focal = (int) metadata.get("FocalPlaneResolutionUnit").getFirstNumericValue();
      if (focal < 1 || focal > 5)
        validation.addErrorLoc("Invalid value fot TiffEP tag FocalPlaneResolutionUnit", "IFD" + n);
    }

    if (metadata.containsTagId(TiffTags.getTagId("SensingMethod"))) {
      int sensing = (int) metadata.get("SensingMethod").getFirstNumericValue();
      if (sensing < 0 || sensing > 8)
        validation.addErrorLoc("Invalid value fot TiffEP tag SensingMethod", "IFD" + n);
    }
  }
1211889
def __read(self, i: int) -> bytes:
        """"""
        b = self.data[self.idx: self.idx + i]
        self.idx += i
        if len(b) != i:
            raise bencodepy.DecodingError(
                "Incorrect byte length returned between indexes of {0} and {1}. Possible unexpected End of File."
                    .format(str(self.idx), str(self.idx - i)))
        return b
468639
def _ensure_ifaces_tuple(ifaces):
    """"""
    try:

        ifaces = tuple(ifaces)

    except TypeError:

        ifaces = (ifaces,)

    for iface in ifaces:

        if not _issubclass(iface, ibc.Iface):

            raise TypeError('Can only compare against interfaces.')

    return ifaces
1050198
private void CheckPalleteImage(IfdTags metadata, int nifd) {
    // Color Map
    if (!metadata.containsTagId(TiffTags.getTagId("ColorMap"))) {
      validation.addErrorLoc("Missing Color Map", "IFD" + nifd);
    } else {
      int n = metadata.get(TiffTags.getTagId("ColorMap")).getCardinality();
      if (n != 3 * (int) Math.pow(2, metadata.get(TiffTags.getTagId("BitsPerSample"))
          .getFirstNumericValue()))
        validation.addError("Incorrect Color Map Cardinality", "IFD" + nifd, metadata.get(320)
            .getCardinality());
    }

    // Bits per Sample
    long bps = metadata.get(TiffTags.getTagId("BitsPerSample")).getFirstNumericValue();
    if (bps != 4 && bps != 8)
      validation.addError("Invalid Bits per Sample", "IFD" + nifd, bps);

    // Compression
    long comp = metadata.get(TiffTags.getTagId("Compression")).getFirstNumericValue();
    // if (comp != 1 && comp != 32773)
    if (comp < 1)
      validation.addError("Invalid Compression", "IFD" + nifd, comp);
  }
75771
private void CheckBilevelImage(IfdTags metadata, int n) {
    // Compression
    long comp = metadata.get(TiffTags.getTagId("Compression")).getFirstNumericValue();
    // if (comp != 1 && comp != 2 && comp != 32773)
    if (comp < 1)
      validation.addError("Invalid Compression", "IFD" + n, comp);
  }
1064284
private ArrayList<IIOMetadata> readExif( IIOMetadataNode app1EXIFNode ) {
        // Set up input skipping EXIF ID 6-byte sequence.
        byte[] app1Params = (byte[]) app1EXIFNode.getUserObject();

        MemoryCacheImageInputStream app1EXIFInput = new MemoryCacheImageInputStream(new ByteArrayInputStream(app1Params, 6,
                app1Params.length - 6));

        // only the tiff reader knows how to interpret the exif metadata
        ImageReader tiffReader = null;
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("tiff");

        while( readers.hasNext() ) {
            tiffReader = (ImageReader) readers.next();
            if (tiffReader.getClass().getName().startsWith("com.sun.media")) {
                // Break on finding the core provider.
                break;
            }
        }
        if (tiffReader == null) {
            throw new RuntimeException("Cannot find core TIFF reader!");
        }

        ArrayList<IIOMetadata> out = new ArrayList<IIOMetadata>(1);

        tiffReader.setInput(app1EXIFInput);

        IIOMetadata tiffMetadata = null;

        try {
            tiffMetadata = tiffReader.getImageMetadata(0);
            // IIOMetadata meta = tiffReader.getImageMetadata(0);
            TIFFImageReadParam rParam = (TIFFImageReadParam) tiffReader.getDefaultReadParam();
            rParam.setTIFFDecompressor(null);
        } catch (IOException e) {
            e.printStackTrace();
        };

        tiffReader.dispose();

        out.add(0, tiffMetadata);

        return out;
    }
934396
def _fromfile(self):
        """"""
        self._fd.seek(0)
        try:
            self.byte_order = {b'II': '<', b'MM': '>'}[self._fd.read(2)]
        except KeyError:
            raise ValueError("not a valid TIFF file")
        version = struct.unpack(self.byte_order+'H', self._fd.read(2))[0]
        if version == 43:  # BigTiff
            self.offset_size, zero = struct.unpack(self.byte_order+'HH',
                                                   self._fd.read(4))
            if zero or self.offset_size != 8:
                raise ValueError("not a valid BigTIFF file")
        elif version == 42:
            self.offset_size = 4
        else:
            raise ValueError("not a TIFF file")
        self.pages = []
        while True:
            try:
                page = TIFFpage(self)
                self.pages.append(page)
            except StopIteration:
                break
        if not self.pages:
            raise ValueError("empty TIFF file")
824338
function getNextIFDOffset(dataView, dirStart, bigEnd){
        //the first 2bytes means the number of directory entries contains in this IFD
        var entries = dataView.getUint16(dirStart, !bigEnd);

        // After last directory entry, there is a 4bytes of data,
        // it means an offset to next IFD.
        // If its value is '0x00000000', it means this is the last IFD and there is no linked IFD.

        return dataView.getUint32(dirStart + 2 + entries * 12, !bigEnd); // each entry is 12 bytes long
    }
210807
def p_ifdef(p):
    
    global ENABLED

    if ENABLED:
        p[0] = [p[2]] + p[3]
        p[0] += ['#line %i "%s"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]
    else:
        p[0] = ['#line %i "%s"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]

    ENABLED = IFDEFS[-1][0]
    IFDEFS.pop()
418239
def select_files_by_ifo_combination(ifocomb, insps):
    
    inspcomb = FileList()
    for ifo, ifile in zip(*insps.categorize_by_attr('ifo')):
        if ifo in ifocomb:
            inspcomb += ifile

    return inspcomb
235442
public void readImage() {
    if (ifd.containsTagId(TiffTags.getTagId("StripOffsets"))
        || ifd.containsTagId(TiffTags.getTagId("StripBYTECount"))) {
      readStrips();
    }
    if (ifd.containsTagId(TiffTags.getTagId("TileOffsets"))
        || ifd.containsTagId(TiffTags.getTagId("TileBYTECounts"))) {
      readTiles();
    }
  }
1274722
def read(*paths):
    ''''''
    with open(os.path.join(os.path.dirname(__file__), *paths)) as fp:
        return fp.read()
1155565
def supported_tifs(self):
        
        buf = ctypes.c_uint32()
        self._dll.JLINKARM_TIF_GetAvailable(ctypes.byref(buf))
        return buf.value
1140139
private void validateIfdBP(IFD ifd, int p) {
    IfdTags metadata = ifd.getMetadata();

    if (p == 1 || p == 2) {
      checkRequiredTag(metadata, "NewSubfileType", 1, new long[]{0});
    }
    checkRequiredTag(metadata, "ImageLength", 1);
    checkRequiredTag(metadata, "ImageWidth", 1);
    checkRequiredTag(metadata, "BitsPerSample", 1, new long[]{1});
    if (p == 0) {
      checkRequiredTag(metadata, "Compression", 1, new long[]{1,4,8});
    } else if (p == 1) {
      checkRequiredTag(metadata, "Compression", 1, new long[]{1});
    } else {
      checkRequiredTag(metadata, "Compression", 1, new long[]{1,4,8});
    }
    if (p == 0) {
      checkRequiredTag(metadata, "PhotometricInterpretation", 1);
    } else {
      checkRequiredTag(metadata, "PhotometricInterpretation", 1, new long[] {0});
    }
    checkRequiredTag(metadata, "StripOffsets", 1);
    if (p == 0) {
      checkRequiredTag(metadata, "Orientation", 1, new long[]{1,4,5,8});
    } else {
      checkRequiredTag(metadata, "Orientation", 1, new long[]{1});
    }
    checkRequiredTag(metadata, "SamplesPerPixel", 1, new long[]{1});
    checkRequiredTag(metadata, "StripBYTECount", 1);
    checkRequiredTag(metadata, "XResolution", 1);
    checkRequiredTag(metadata, "YResolution", 1);
    if (p == 1 || p == 2) {
      checkRequiredTag(metadata, "ResolutionUnit", 1, new long[]{2, 3});
      checkRequiredTag(metadata, "DotRange", 2, new long[]{0, 255});
    }
    checkRequiredTag(metadata, "ImageColorIndicator", 1, new long[]{0, 1, 2});
    checkRequiredTag(metadata, "BackgroundColorIndicator", 1, new long[]{0, 1, 2});
  }