Beispiel #1
0
  /** Reads an SRATIONAL value and returns it as a Rational. */
  protected Rational readSignedRational(long count, long value) throws IOException {
    _raf.seek(value);

    long numer = ModuleBase.readSignedInt(_raf, _bigEndian);
    long denom = ModuleBase.readSignedInt(_raf, _bigEndian);
    return new Rational(numer, denom);
  }
Beispiel #2
0
  /**
   * Reads an array of strings from the TIFF file.
   *
   * @param count Number of strings to read
   * @param value Offset from which to read
   */
  protected String[] readASCIIArray(long count, long value) throws IOException {
    _raf.seek(value);

    int nstrs = 0;
    List list = new LinkedList();
    byte[] buf = new byte[(int) count];
    _raf.read(buf);
    StringBuffer strbuf = new StringBuffer();
    for (int i = 0; i < count; i++) {
      int b = buf[i];
      if (b == 0) {
        list.add(strbuf.toString());
        strbuf.setLength(0);
      } else {
        strbuf.append((char) b);
      }
    }
    /* We can't use ArrayList.toArray because that returns an
    Object[], not a String[] ... sigh. */
    String[] strs = new String[nstrs];
    ListIterator iter = list.listIterator();
    for (int i = 0; i < nstrs; i++) {
      strs[i] = (String) iter.next();
    }
    return strs;
  }
Beispiel #3
0
  /** Reads a TIFF array of signed 16-bit values and returns it as an int array. */
  protected int[] readSShortArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    int[] iarray = new int[(int) count];
    for (int i = 0; i < count; i++) {
      iarray[i] = ModuleBase.readSignedShort(_raf, _bigEndian);
    }
    return iarray;
  }
Beispiel #4
0
  /** Reads a TIFF array of unsigned 16-bit values and returns it as an int array. */
  protected int[] readShortArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    int[] iarray = new int[(int) count];
    for (int i = 0; i < count; i++) {
      iarray[i] = (int) readUnsigned(type);
    }
    return iarray;
  }
Beispiel #5
0
  /**
   * Reads a TIFF array of signed 32-bit integer values and returns it as a long array.
   *
   * @param type TIFF type to read; must be a 32-bit type
   * @param count Number of values to read
   * @param value Offset from which to read
   */
  protected long[] readLongArray(int type, long count, long value) throws IOException {
    _raf.seek(value);

    long[] array = new long[(int) count];
    for (int i = 0; i < count; i++) {
      array[i] = readUnsigned(type);
    }
    return array;
  }
Beispiel #6
0
  /**
   * Reads a TIFF array of DOUBLE 64-bit values and returns it as a double array.
   *
   * @param count Number of values to read
   * @param value Offset from which to read
   */
  protected double[] readDoubleArray(long count, long value) throws IOException {
    _raf.seek(value);

    double[] darray = new double[(int) count];
    for (int i = 0; i < count; i++) {
      darray[i] = ModuleBase.readDouble(_raf, _bigEndian);
    }

    return darray;
  }
Beispiel #7
0
  /**
   * Reads a string value from the TIFF file.
   *
   * @param count Length of string
   * @param value Offset of string
   */
  protected String readASCII(long count, long value) throws IOException {
    _raf.seek(value);

    byte[] buffer = new byte[(int) count];
    _raf.read(buffer);

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < count; i++) {
      if (buffer[i] == 0) {
        break;
      }
      sb.append((char) buffer[i]);
    }
    return sb.toString();
  }
Beispiel #8
0
  /** Reads an array of SRATIONAL values and returns it as an array of Rational. */
  protected Rational[] readSignedRationalArray(long count, long value) throws IOException {
    _raf.seek(value);

    byte[] buffer = new byte[(int) calcValueSize(SRATIONAL, count)];
    _raf.read(buffer);
    DataInputStream stream = new DataInputStream(new ByteArrayInputStream(buffer));

    Rational[] rarray = new Rational[(int) count];
    for (int i = 0; i < count; i++) {
      long numer = ModuleBase.readSignedInt(stream, _bigEndian, null);
      long denom = ModuleBase.readSignedInt(stream, _bigEndian, null);
      rarray[i] = new Rational(numer, denom);
    }

    return rarray;
  }
Beispiel #9
0
  /** Reads and returns a single unsigned 16-bit value. */
  protected int readShort(int type, long count, long value) throws IOException {
    _raf.seek(value);

    return (int) readUnsigned(type);
  }
Beispiel #10
0
  /**
   * Reads and returns a single unsigned 32-bit integer value.
   *
   * @param type TIFF type to read; must be a 32-bit type
   * @param count Unused
   * @param value Offset from which to read
   */
  protected long readLong(int type, long count, long value) throws IOException {
    _raf.seek(value);

    return readUnsigned(type);
  }
Beispiel #11
0
 /**
  * Reads an array of bytes and returns it as a byte array.
  *
  * @param type Unused
  * @param count Number of bytes to read
  * @param value Offset from which to read
  */
 protected byte[] readTrueByteArray(int type, long count, long value) throws IOException {
   _raf.seek(value);
   byte[] array = new byte[(int) count];
   _raf.read(array);
   return array;
 }
Beispiel #12
0
  /**
   * Parse the IFD. Errors are not suppressed.
   *
   * @param byteOffsetIsValid If true, allow offsets on odd byte boundaries
   * @return The offset of the next IFD
   */
  public long parse(boolean byteOffsetIsValid) throws TiffException {
    /* Start at the IFD offset, read the number of entries, then
     * read the entire IFD.
     */
    long offset = _offset;
    _next = 0L;
    byte[] buffer;
    int nFields = 0;
    try {
      _raf.seek(offset);
      nFields = ModuleBase.readUnsignedShort(_raf, _bigEndian);
      offset += 2;

      int len = 12 * nFields;
      buffer = new byte[len];
      _raf.read(buffer, 0, len);

      /* Read the offset of the next IFD (or 0 if none). */
      offset += len;
      _next = ModuleBase.readUnsignedInt(_raf, _bigEndian);
    } catch (Exception e) {
      throw new TiffException("Premature EOF", offset);
    }

    DataInputStream ifdStream = new DataInputStream(new ByteArrayInputStream(buffer));

    try {
      int prevTag = 0;
      for (int i = 0; i < nFields; i++) {
        int tag = ModuleBase.readUnsignedShort(ifdStream, _bigEndian, null);
        /* Tags must be in ascending numerical order. */
        if (!debug_allowoutofsequence && tag < prevTag) {
          _info.setMessage(
              new ErrorMessage("Tag " + tag + " out of sequence", _offset + 2 + 12 * i));
          _info.setWellFormed(false);
        }
        prevTag = tag;

        int type = ModuleBase.readUnsignedShort(ifdStream, _bigEndian, null);
        /* Skip over tags with unknown type. */
        if (type < BYTE || type > IFD) {
          _info.setMessage(
              new ErrorMessage(
                  "Unknown data type", "Type = " + type + ", Tag = " + tag, _offset + 4 + 12 * i));
        } else {
          /* Type gives indication of the TIFF version. */
          if (SBYTE <= type && type <= IFD) {
            _version = 6;
          }

          long count = ModuleBase.readUnsignedInt(ifdStream, _bigEndian, null);
          long value = ModuleBase.readUnsignedInt(ifdStream, _bigEndian, null);
          if (calcValueSize(type, count) > 4) {
            /* Value is the word-aligned offset of the actual
             * value. */
            if ((value & 1) != 0) {
              if (byteOffsetIsValid) {
                _info.setMessage(
                    new InfoMessage(
                        "Value offset not word-aligned: " + value, _offset + 10 + 12 * i));
              } else {
                throw new TiffException(
                    "Value offset not " + "word-aligned: " + value, _offset + 10 + 12 * i);
              }
            }
          } else {
            /* Value is the actual value; pass the offset of
             * the value. */
            value = _offset + 10 + 12 * i;
          }
          lookupTag(tag, type, count, value);
        }
      }
    } catch (IOException e) {
      throw new TiffException("Read error", _offset + 2);
    }
    postParseInitialization();

    return _next;
  }