}
return mipMapCount;
}
private void extractDDS() throws IOException {
DDSHeader header = new DDSHeader();
header.dwWidth = tex.width;
header.dwHeight = tex.height;
switch (tex.textureFormat) {
case Alpha8:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_ALPHA;
header.ddspf.dwABitMask = 0xff;
header.ddspf.dwRGBBitCount = 8;
break;
case RGB24:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGB;
header.ddspf.dwRBitMask = 0xff0000;
header.ddspf.dwGBitMask = 0x00ff00;
header.ddspf.dwBBitMask = 0x0000ff;
header.ddspf.dwRGBBitCount = 24;
break;
case RGBA32:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGBA;
header.ddspf.dwRBitMask = 0x000000ff;
header.ddspf.dwGBitMask = 0x0000ff00;
header.ddspf.dwBBitMask = 0x00ff0000;
header.ddspf.dwABitMask = 0xff000000;
header.ddspf.dwRGBBitCount = 32;
break;
case BGRA32:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGBA;
header.ddspf.dwRBitMask = 0x00ff0000;
header.ddspf.dwGBitMask = 0x0000ff00;
header.ddspf.dwBBitMask = 0x000000ff;
header.ddspf.dwABitMask = 0xff000000;
header.ddspf.dwRGBBitCount = 32;
break;
case ARGB32:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGBA;
header.ddspf.dwRBitMask = 0x0000ff00;
header.ddspf.dwGBitMask = 0x00ff0000;
header.ddspf.dwBBitMask = 0xff000000;
header.ddspf.dwABitMask = 0x000000ff;
header.ddspf.dwRGBBitCount = 32;
break;
case ARGB4444:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGBA;
header.ddspf.dwRBitMask = 0x0f00;
header.ddspf.dwGBitMask = 0x00f0;
header.ddspf.dwBBitMask = 0x000f;
header.ddspf.dwABitMask = 0xf000;
header.ddspf.dwRGBBitCount = 16;
break;
case RGBA4444:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGBA;
header.ddspf.dwRBitMask = 0xf000;
header.ddspf.dwGBitMask = 0x0f00;
header.ddspf.dwBBitMask = 0x00f0;
header.ddspf.dwABitMask = 0x000f;
header.ddspf.dwRGBBitCount = 16;
break;
case RGB565:
header.ddspf.dwFlags = DDSPixelFormat.DDPF_RGB;
header.ddspf.dwRBitMask = 0xf800;
header.ddspf.dwGBitMask = 0x07e0;
header.ddspf.dwBBitMask = 0x001f;
header.ddspf.dwRGBBitCount = 16;
break;
case DXT1:
header.ddspf.dwFourCC = DDSPixelFormat.PF_DXT1;
break;
case DXT5:
header.ddspf.dwFourCC = DDSPixelFormat.PF_DXT5;
break;
default:
throw new IllegalStateException("Invalid texture format for DDS: " + tex.textureFormat);
}
// set mip map flags if required
if (tex.mipMap) {
header.dwFlags |= DDSHeader.DDS_HEADER_FLAGS_MIPMAP;
header.dwCaps |= DDSHeader.DDS_SURFACE_FLAGS_MIPMAP;
header.dwMipMapCount = getMipMapCount(header.dwWidth, header.dwHeight);
}
// set and calculate linear size
header.dwFlags |= DDSHeader.DDS_HEADER_FLAGS_LINEARSIZE;
if (header.ddspf.dwFourCC != 0) {
header.dwPitchOrLinearSize = header.dwWidth * header.dwHeight;
if (tex.textureFormat == TextureFormat.DXT1) {
header.dwPitchOrLinearSize /= 2;
}
header.ddspf.dwFlags |= DDSPixelFormat.DDPF_FOURCC;
} else {
header.dwPitchOrLinearSize = (tex.width * tex.height * header.ddspf.dwRGBBitCount) / 8;
}
ByteBuffer bbTex = ByteBuffer.allocateDirect(DDSHeader.SIZE + tex.imageBuffer.capacity());
bbTex.order(ByteOrder.LITTLE_ENDIAN);
// write header
DataOutputWriter out = DataOutputWriter.newWriter(bbTex);
header.write(out);
// write data
bbTex.put(tex.imageBuffer);
bbTex.rewind();