protected Header readHeaderFromBuffer(ByteBuffer buffer) throws WWRuntimeException {
    // Read file code - first byte
    int fileCode = buffer.get();
    if (fileCode > 5) {
      String message = Logging.getMessage("SHP.NotADBaseFile", file.getPath());
      Logging.logger().log(java.util.logging.Level.SEVERE, message);
      throw new WWRuntimeException(message);
    }

    // Last update date
    int yy = 0xFF & buffer.get(); // unsigned
    int mm = buffer.get();
    int dd = buffer.get();

    // Number of records
    int numRecords = buffer.getInt();

    // Header struct length
    int headerLength = buffer.getShort();

    // Record length
    int recordLength = buffer.getShort();

    // Assemble header
    Header header = new Header();
    header.fileCode = fileCode;
    Calendar cal = Calendar.getInstance();
    cal.set(1900 + yy, mm - 1, dd);
    header.lastModificationDate = cal.getTime();
    header.numberOfRecords = numRecords;
    header.headerLength = headerLength;
    header.recordLength = recordLength;

    return header;
  }
  protected Header readHeaderFromStream(InputStream is) throws IOException {
    ReadableByteChannel channel = Channels.newChannel(is);
    // Read header fixed portion
    ByteBuffer headerBuffer = ShapefileUtils.readByteChannelToBuffer(channel, FIXED_HEADER_LENGTH);
    Header header = this.readHeaderFromBuffer(headerBuffer);
    // Read fields description header
    int fieldsHeaderLength = header.headerLength - FIXED_HEADER_LENGTH;
    header.fieldsHeaderBuffer = ShapefileUtils.readByteChannelToBuffer(channel, fieldsHeaderLength);

    return header;
  }
Exemple #3
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;
    }
  }
Exemple #4
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();
 }
Exemple #5
0
 private void readFromBuffer(ByteBuffer buf) throws IOException {
   this.buf = buf;
   buf.order(ByteOrder.LITTLE_ENDIAN);
   header = new Header();
   header.read(buf);
   fixupHeader();
 }
Exemple #6
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());
  }
Exemple #7
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
  }