예제 #1
0
  // Microsoft doesn't follow their own specifications and the
  // simplest conversion using the DxTex tool to e.g. a DXT3 texture
  // results in an illegal .dds file without either DDSD_PITCH or
  // DDSD_LINEARSIZE set in the header's flags. This code, adapted
  // from the DevIL library, fixes up the header in these situations.
  private void fixupHeader() {
    if (isCompressed() && !isSurfaceDescFlagSet(DDSD_LINEARSIZE)) {
      // Figure out how big the linear size should be
      int depth = header.backBufferCountOrDepth;
      if (depth == 0) {
        depth = 1;
      }

      header.pitchOrLinearSize =
          computeCompressedBlockSize(getWidth(), getHeight(), depth, getCompressionFormat());
      header.flags |= DDSD_LINEARSIZE;
    }
  }
예제 #2
0
 public void write(FileOutputStream fos) throws IOException {
   FileChannel chan = fos.getChannel();
   // Create ByteBuffer for header in case the start of our
   // ByteBuffer isn't actually memory-mapped
   ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize());
   hdr.order(ByteOrder.LITTLE_ENDIAN);
   header.write(hdr);
   hdr.rewind();
   chan.write(hdr);
   buf.position(Header.writtenSize());
   chan.write(buf);
   chan.force(true);
   chan.close();
 }
예제 #3
0
 private void readFromBuffer(ByteBuffer buf) throws IOException {
   this.buf = buf;
   buf.order(ByteOrder.LITTLE_ENDIAN);
   header = new Header();
   header.read(buf);
   fixupHeader();
 }
예제 #4
0
  /**
   * Gets the <i>i</i>th mipmap data (0..getNumMipMaps() - 1)
   *
   * @param side Cubemap side or 0 for 2D texture
   * @param map Mipmap index
   * @return Image object
   */
  public ImageInfo getMipMap(int side, int map) {
    if (!isCubemap() && (side != 0)) {
      throw new RuntimeException("Illegal side for 2D texture: " + side);
    }
    if (isCubemap() && !isCubemapSidePresent(side)) {
      throw new RuntimeException("Illegal side, side not present: " + side);
    }
    if (getNumMipMaps() > 0 && ((map < 0) || (map >= getNumMipMaps()))) {
      throw new RuntimeException(
          "Illegal mipmap number " + map + " (0.." + (getNumMipMaps() - 1) + ")");
    }

    // Figure out how far to seek
    int seek = Header.writtenSize();
    if (isCubemap()) {
      seek += sideShiftInBytes(side);
    }
    for (int i = 0; i < map; i++) {
      seek += mipMapSizeInBytes(i);
    }
    buf.limit(seek + mipMapSizeInBytes(map));
    buf.position(seek);
    ByteBuffer next = buf.slice();
    buf.position(0);
    buf.limit(buf.capacity());
    return new ImageInfo(
        next, mipMapWidth(map), mipMapHeight(map), isCompressed(), getCompressionFormat());
  }
예제 #5
0
  public void mainloop() {

    while (!client.isConnected()) {
      try {
        System.out.println("Connecting to " + hostname + ":" + port);
        client.connect(hostname, port);
      } catch (IOException e) {
      }
      if (!client.isConnected()) {
        System.out.println("Couldn't connect. waiting");
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          run = false;
          System.exit(-1);
        }
      }
    }

    // Load the header information in one go into a bytebuffer
    byte[] rawbytebuf = new byte[BUFFERSIZE];

    int n = 0;

    try {
      n = headerReader.read(rawbytebuf);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Byte-buffer used to parse the byte-stream. Force native ordering
    ByteBuffer hdrBuf = ByteBuffer.wrap(rawbytebuf, 0, n);
    hdrBuf.order(ByteOrder.nativeOrder());
    Header hdr = new Header(hdrBuf);
    if (VERB > 0) {
      System.out.println("Sending header: " + hdr.toString());
    }
    hdr.nSamples = 0; // reset number of samples to 0

    try {
      client.putHeader(hdr);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Interval between sending samples to the buffer
    int pktSamples = hdr.nChans * blockSize; // number data samples in each buffer packet
    int pktBytes = pktSamples * DataType.wordSize[hdr.dataType];
    int nsamp = 0; // sample counter
    int nblk = 0;
    int nevent = 0;
    byte[] samples = new byte[pktBytes];

    // Size of the event header: type,type_numel,val,val_numel,sample,offset,duration,bufsz
    int evtHdrSz = DataType.wordSize[DataType.INT32] * 8;
    byte[] evtRawBuf = new byte[BUFFERSIZE]; // buffer to hold complete event structure

    // Byte-buffer used to parse the byte-stream. Force native ordering
    ByteBuffer evtBuf = ByteBuffer.wrap(evtRawBuf);
    evtBuf.order(ByteOrder.nativeOrder());
    int payloadSz = 0;
    int evtSample = 0;
    int evtSz = 0;
    long sample_ms = 0;
    long starttime_ms = java.lang.System.currentTimeMillis();
    long elapsed_ms = 0;
    long print_ms = 0;

    // Now do the data forwarding
    boolean eof = false;

    while (!eof
        && run) { // The run switch allows control of stopping the thread and getting out of the
      // loop
      // Read one buffer packets worth of samples
      // increment the cursor position
      if (VERB > 0 && elapsed_ms > print_ms + 500) {
        print_ms = elapsed_ms;
        System.out.println(
            nblk
                + " "
                + nsamp
                + " "
                + nevent
                + " "
                + (elapsed_ms / 1000)
                + " (blk,samp,event,sec)\r");
      }

      // read and write the samples
      try {
        n = dataReader.read(samples);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (n <= 0) {
        eof = true;
        break;
      } // stop if run out of samples

      try {
        client.putRawData(blockSize, hdr.nChans, hdr.dataType, samples);
      } catch (IOException e) {
        e.printStackTrace();
      }

      // update the sample count
      nsamp += blockSize;
      while (evtSample <= nsamp) {
        if (evtSample > 0) { // send the current event
          try {
            client.putRawEvent(evtRawBuf, 0, evtSz);
          } catch (IOException e) {
            e.printStackTrace();
          }
          nevent++;
        }

        // read the next event
        try {
          n = eventReader.read(evtRawBuf, 0, evtHdrSz); // read the fixed size header
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (n <= 0) {
          eof = true;
          break;
        }
        evtSample = ((ByteBuffer) evtBuf.position(4 * 4)).getInt(); // sample index for this event
        payloadSz = ((ByteBuffer) evtBuf.position(4 * 7)).getInt(); // payload size for this event
        evtSz = evtHdrSz + payloadSz;

        // read the variable part
        try {
          n = eventReader.read(evtRawBuf, evtHdrSz, payloadSz);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (n <= 0) {
          eof = true;
          break;
        }

        // print the event we just read
        if (VERB > 1) {
          ByteBuffer tmpev = ByteBuffer.wrap(evtRawBuf, 0, evtSz);
          tmpev.order(evtBuf.order());
          BufferEvent evt = new BufferEvent(tmpev);
          System.out.println("Read Event: " + evt);
        }
      }

      // sleep until the next packet should be send OR EOF
      /*when to send the next sample */
      sample_ms = (long) ((float) (nsamp * 1000) / hdr.fSample / (float) speedup);
      elapsed_ms = java.lang.System.currentTimeMillis() - starttime_ms; // current time
      if (sample_ms > elapsed_ms)
        try {
          Thread.sleep(sample_ms - elapsed_ms);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      nblk++;
    }

    stop();
  }
예제 #6
0
  private void initFromData(int d3dFormat, int width, int height, ByteBuffer[] mipmapData)
      throws IllegalArgumentException {
    // Check size of mipmap data compared against format, width and
    // height
    int topmostMipmapSize = width * height;
    int pitchOrLinearSize = width;
    boolean isCompressed = false;
    switch (d3dFormat) {
      case D3DFMT_R8G8B8:
        topmostMipmapSize *= 3;
        pitchOrLinearSize *= 3;
        break;
      case D3DFMT_A8R8G8B8:
        topmostMipmapSize *= 4;
        pitchOrLinearSize *= 4;
        break;
      case D3DFMT_X8R8G8B8:
        topmostMipmapSize *= 4;
        pitchOrLinearSize *= 4;
        break;
      case D3DFMT_DXT1:
      case D3DFMT_DXT2:
      case D3DFMT_DXT3:
      case D3DFMT_DXT4:
      case D3DFMT_DXT5:
        topmostMipmapSize = computeCompressedBlockSize(width, height, 1, d3dFormat);
        pitchOrLinearSize = topmostMipmapSize;
        isCompressed = true;
        break;
      default:
        throw new IllegalArgumentException("d3dFormat must be one of the known formats");
    }

    // Now check the mipmaps against this size
    int curSize = topmostMipmapSize;
    int mipmapWidth = width;
    int mipmapHeight = height;
    int totalSize = 0;
    for (int i = 0; i < mipmapData.length; i++) {
      if (mipmapData[i].remaining() != curSize) {
        throw new IllegalArgumentException(
            "Mipmap level "
                + i
                + " didn't match expected data size (expected "
                + curSize
                + ", got "
                + mipmapData[i].remaining()
                + ")");
      }
      /* Change Daniel Senff
       * I got the problem, that MipMaps below the dimension of 8x8 blocks with DXT5
       * where assume smaller than they are created.
       * Assumed: < 16byte where 16byte where used by the compression. */
      if (isCompressed) {
        // size calculation for compressed mipmaps
        if (mipmapWidth > 1) mipmapWidth /= 2;
        if (mipmapHeight > 1) mipmapHeight /= 2;
        curSize = computeCompressedBlockSize(mipmapWidth, mipmapHeight, 1, d3dFormat);
      } else {
        curSize /= 4;
      }
      totalSize += mipmapData[i].remaining();
    }

    // OK, create one large ByteBuffer to hold all of the mipmap data
    totalSize += Header.writtenSize();
    ByteBuffer buf = ByteBuffer.allocate(totalSize);
    buf.position(Header.writtenSize());
    for (int i = 0; i < mipmapData.length; i++) {
      buf.put(mipmapData[i]);
    }
    this.buf = buf;

    // Allocate and initialize a Header
    header = new Header();
    header.size = Header.size();
    header.flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
    if (mipmapData.length > 1) {
      header.flags |= DDSD_MIPMAPCOUNT;
      header.mipMapCountOrAux = mipmapData.length;
    }
    header.width = width;
    header.height = height;
    if (isCompressed) {
      header.flags |= DDSD_LINEARSIZE;
      header.pfFlags |= DDPF_FOURCC;
      header.pfFourCC = d3dFormat;
    } else {
      header.flags |= DDSD_PITCH;
      // Figure out the various settings from the pixel format
      header.pfFlags |= DDPF_RGB;
      switch (d3dFormat) {
        case D3DFMT_R8G8B8:
          header.pfRGBBitCount = 24;
          break;
        case D3DFMT_A8R8G8B8:
          header.pfRGBBitCount = 32;
          header.pfFlags |= DDPF_ALPHAPIXELS;
          break;
        case D3DFMT_X8R8G8B8:
          header.pfRGBBitCount = 32;
          break;
      }
      header.pfRBitMask = 0x00FF0000;
      header.pfGBitMask = 0x0000FF00;
      header.pfBBitMask = 0x000000FF;
      if (d3dFormat == D3DFMT_A8R8G8B8) {
        header.pfABitMask = 0xFF000000;
      }
    }
    header.pitchOrLinearSize = pitchOrLinearSize;
    header.pfSize = Header.pfSize();
    // Not sure whether we can get away with leaving the rest of the
    // header blank
  }