/**
   * This method writes the Contiguous codestream box
   *
   * @param cs The contiguous codestream
   * @exception java.io.IOException If an I/O error ocurred.
   */
  public void writeContiguousCodeStreamBox() throws IOException {

    // when write a jp2 file
    if (metadata != null) {
      // Write box length (LBox)
      // This value is set to 0 since in this implementation, this box is
      // always last
      stream.writeInt(clength + 8);

      // Write contiguous codestream box name (TBox)
      stream.writeInt(CONTIGUOUS_CODESTREAM_BOX);
    }
    // Read and buffer the codestream
    BEBufferedRandomAccessFile fi = new BEBufferedRandomAccessFile(file, "rw+");
    int remainder = clength;
    byte[] codestream = new byte[1024];

    while (remainder > 0) {
      int len = remainder > 1024 ? 1024 : remainder;
      fi.readFully(codestream, 0, len);

      // Write codestream
      stream.write(codestream, 0, len);
      remainder -= len;
    }

    // Close the file.
    fi.close();
  }
 private void writeBox(IIOMetadataNode node) throws IOException {
   int type = Box.getTypeInt((String) Box.getAttribute(node, "Type"));
   int length = new Integer((String) Box.getAttribute(node, "Length")).intValue();
   Box box = Box.createBox(type, node);
   otherLength += length;
   stream.writeInt(length);
   stream.writeInt(type);
   byte[] data = box.getContent();
   stream.write(data, 0, data.length);
 }
  private void writeSuperBox(IIOMetadataNode node) throws IOException {
    NodeList list = node.getChildNodes();

    String name = node.getNodeName();
    if (name.startsWith("JPEG2000")) {
      stream.writeInt(computeLength(node));
      stream.writeInt(Box.getTypeInt((String) Box.getTypeByName(name)));
      otherLength += 8;
    }

    for (int i = 0; i < list.getLength(); i++) {
      IIOMetadataNode child = (IIOMetadataNode) list.item(i);

      name = child.getNodeName();
      if (name.startsWith("JPEG2000") && format.isLeaf(name)) writeBox(child);
      else writeSuperBox(child);
    }
  }
예제 #4
0
 public void prepareWriteSequence(IIOMetadata streamMetadata) throws IOException {
   ImageOutputStream out = (ImageOutputStream) getOutput();
   out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
   out.writeShort(0x00004949); // 0: II = intel = little endian
   out.writeShort(42); // 2: version, magic value
   ifdptr = out.getStreamPosition(); // save position (4): write here later the offset to first ifd
   // (ifd linked list)
   out.writeInt(0); // 4: offset first Image File Directory
   // 8: header size
 }
예제 #5
0
  public void writeToStream(ImageOutputStream stream, final boolean isBTIFF) throws IOException {

    long nextSpace;

    if (!isBTIFF) {
      int numFields = getNumTIFFFields();
      stream.writeShort(numFields);

      nextSpace = stream.getStreamPosition() + 12 * numFields + 4;

    } else {
      long numFields = getNumTIFFFields();
      stream.writeLong(numFields);

      nextSpace = stream.getStreamPosition() + 20 * numFields + 8;
    }

    Iterator iter = iterator();

    while (iter.hasNext()) {
      TIFFField f = (TIFFField) iter.next();

      TIFFTag tag = f.getTag();

      int type = f.getType();
      int count = f.getCount();

      // Hack to deal with unknown tags
      if (type == 0) {
        type = TIFFTag.TIFF_UNDEFINED;
      }
      int size = count * TIFFTag.getSizeOfType(type);

      if (type == TIFFTag.TIFF_ASCII) {
        int chars = 0;
        for (int i = 0; i < count; i++) {
          chars += f.getAsString(i).length() + 1;
        }
        count = chars;
        size = count;
      }

      int tagNumber = f.getTagNumber();
      stream.writeShort(tagNumber);
      stream.writeShort(type);

      if (isBTIFF) {
        stream.writeLong(count);
        stream.writeLong(0);
        stream.mark(); // Mark beginning of next field
        stream.skipBytes(-8);
      } else {
        stream.writeInt(count);
        stream.writeInt(0);
        stream.mark(); // Mark beginning of next field
        stream.skipBytes(-4);
      }

      long pos;

      if (!isBTIFF) {
        if (size > 4 || tag.isIFDPointer()) {
          // Ensure IFD or value is written on a word boundary
          nextSpace = (nextSpace + 3) & ~0x3;

          stream.writeInt((int) nextSpace);
          stream.seek(nextSpace);
          pos = nextSpace;

          if (tag.isIFDPointer()) {
            TIFFIFD subIFD = (TIFFIFD) f.getData();
            subIFD.writeToStream(stream, isBTIFF);
            nextSpace = subIFD.lastPosition;
          } else {
            writeTIFFFieldToStream(f, stream);
            nextSpace = stream.getStreamPosition();
          }
        } else {
          pos = stream.getStreamPosition();
          writeTIFFFieldToStream(f, stream);
        }
      } else {
        if (size > 8 || tag.isIFDPointer()) {
          // Ensure IFD or value is written on a Long boundary
          nextSpace = (nextSpace + 7) & ~0x7;

          stream.writeLong(nextSpace);
          stream.seek(nextSpace);
          pos = nextSpace;

          if (tag.isIFDPointer()) {
            TIFFIFD subIFD = (TIFFIFD) f.getData();
            subIFD.writeToStream(stream, isBTIFF);
            nextSpace = subIFD.lastPosition;
          } else {
            writeTIFFFieldToStream(f, stream);
            nextSpace = stream.getStreamPosition();
          }
        } else {
          pos = stream.getStreamPosition();
          writeTIFFFieldToStream(f, stream);
        }
      }
      // If we are writing the data for the
      // StripByteCounts, TileByteCounts, StripOffsets,
      // TileOffsets, JPEGInterchangeFormat, or
      // JPEGInterchangeFormatLength fields, record the current stream
      // position for backpatching
      if (tagNumber == BaselineTIFFTagSet.TAG_STRIP_BYTE_COUNTS
          || tagNumber == BaselineTIFFTagSet.TAG_TILE_BYTE_COUNTS
          || tagNumber == BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH) {
        this.stripOrTileByteCountsPosition = pos;
      } else if (tagNumber == BaselineTIFFTagSet.TAG_STRIP_OFFSETS
          || tagNumber == BaselineTIFFTagSet.TAG_TILE_OFFSETS
          || tagNumber == BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT) {
        this.stripOrTileOffsetsPosition = pos;
      }

      stream.reset(); // Go to marked position of next field
    }

    this.lastPosition = nextSpace;
  }
예제 #6
0
  private static void writeTIFFFieldToStream(TIFFField field, ImageOutputStream stream)
      throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
      case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
          String s = ((String[]) data)[i];
          int length = s.length();
          for (int j = 0; j < length; j++) {
            stream.writeByte(s.charAt(j) & 0xff);
          }
          stream.writeByte(0);
        }
        break;
      case TIFFTag.TIFF_UNDEFINED:
      case TIFFTag.TIFF_BYTE:
      case TIFFTag.TIFF_SBYTE:
        stream.write((byte[]) data);
        break;
      case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[]) data, 0, ((char[]) data).length);
        break;
      case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[]) data, 0, ((short[]) data).length);
        break;
      case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[]) data, 0, ((int[]) data).length);
        break;
      case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
          stream.writeInt((int) (((long[]) data)[i]));
        }
        break;
      case TIFFTag.TIFF_LONG8:
        stream.writeLongs((long[]) data, 0, ((long[]) data).length);
        break;
      case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
      case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[]) data, 0, ((float[]) data).length);
        break;
      case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[]) data, 0, ((double[]) data).length);
        break;
      case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
          stream.writeInt(((int[][]) data)[i][0]);
          stream.writeInt(((int[][]) data)[i][1]);
        }
        break;
      case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
          long num = ((long[][]) data)[i][0];
          long den = ((long[][]) data)[i][1];
          stream.writeInt((int) num);
          stream.writeInt((int) den);
        }
        break;
      default:
        // error
    }
  }