package org.zsword.util;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author JemiZhuu(周士淳, zsword)
* @version 1.1
* @since 2009-09-13
* DDS(DIRECTDRAWSURFACE) File Reader
*/
public class PluginDDS {
private DDSHEADER header;
private int bpp;
private int rowBlockSize;
private BufferedImage image;
private static final Logger logger = Logger.getLogger(PluginDDS.class.getName());
static {
logger.setLevel(Level.SEVERE);
}
public DDSHEADER getHeader() {
return header;
}
private static final int DDPF_ALPHAPIXELS = 0x00000001;
private static final int DDPF_ALPHA = 0x00000002;
private static final int DDPF_FOURCC = 0x00000004;
private static final int DDPF_RGB = 0x00000040;
private static final String FOURCC_DXT1 = "DXT1";
private static final String FOURCC_DXT2 = "DXT2";
private static final String FOURCC_DXT3 = "DXT3";
private static final String FOURCC_DXT4 = "DXT4";
private static final String FOURCC_DXT5 = "DXT5";
private static final int FI_RGBA_RED = 2;
private static final int FI_RGBA_GREEN = 1;
private static final int FI_RGBA_BLUE = 0;
private static final int FI_RGBA_ALPHA = 3;
private static final int FI_RGBA_RED_MASK = 0x00FF0000;
private static final int FI_RGBA_GREEN_MASK = 0x0000FF00;
private static final int FI_RGBA_BLUE_MASK = 0x000000FF;
private static final int FI_RGBA_ALPHA_MASK = 0xFF000000;
private static final int FI_RGBA_RED_SHIFT = 16;
private static final int FI_RGBA_GREEN_SHIFT = 8;
private static final int FI_RGBA_BLUE_SHIFT = 0;
private static final int FI_RGBA_ALPHA_SHIFT = 24;
private static final int FIT_BITMAP = 1;
/**
* read a DDS Texture File
* @param io
* @return BufferedImage
* @throws IOException
*/
public BufferedImage readDDSFile(ByteFileReader io) throws IOException {
this.readHeader(io);
this.readPixeData(io);
return image;
}
/**
* read DDS header
* @param io
* @throws IOException
*/
protected void readHeader(ByteFileReader io) throws IOException {
if (io == null || io.length() <= 0) {
return;
}
header = new DDSHEADER();
byte[] buf = new byte[4];
io.read(buf);
header.dwMagic = new String(buf);
header.dwSize = io.read4BytesAsInt();
header.dwFlags = io.read4BytesAsInt();
header.dwHeight = io.read4BytesAsInt();
header.dwWidth = io.read4BytesAsInt();
header.dwPitchOrLinearSize = io.read4BytesAsInt();
header.dwDepth = io.read4BytesAsInt();
header.dwMipMapCount = io.read4BytesAsInt();
for (int i = 0; i < header.dwReserved1.length; i++) {
header.dwReserved1[i] = io.read4BytesAsInt();
}
header.ddpfPixelFormat.dwSize = io.read4BytesAsInt();
header.ddpfPixelFormat.dwFlags = io.read4BytesAsInt();
io.read(buf);
header.ddpfPixelFormat.dwFourCC = new String(buf);
header.ddpfPixelFormat.dwRGBBitCount = io.read4BytesAsInt();
header.ddpfPixelFormat.dwRBitMask = io.read4BytesAsInt();
header.ddpfPixelFormat.dwGBitMask = io.read4BytesAsInt();
header.ddpfPixelFormat.dwBBitMask = io.read4BytesAsInt();
header.ddpfPixelFormat.dwRGBAlphaBitMask = io.read4BytesAsInt();
header.ddsCaps.dwCaps1 = io.read4BytesAsInt();
header.ddsCaps.dwCaps2 = io.read4BytesAsInt();
for (int i = 0; i < header.ddsCaps.dwReserved.length; i++) {
header.ddsCaps.dwReserved[i] = io.read4BytesAsInt();
}
header.dwReserved2 = io.read4BytesAsInt();
}
/**
* read DDS pixel datas
* @param io
* @throws IOException
*/
protected void readPixeData(ByteFileReader io) throws IOException {
if (header == null) {
throw new IOException("DDS Header is NULL");
}
image = new BufferedImage(header.dwWidth, header.dwHeight, BufferedImage.TYPE_INT_ARGB_PRE);
if (0 != (header.ddpfPixelFormat.dwFlags & DDPF_RGB)) {
LoadRGB(io);
} else if (0 != (header.ddpfPixelFormat.dwFlags & DDPF_FOURCC)) {
this.LoadDXT(io);
} else {
throw new IOException("Unkown DDS Format");
}
}
/**
* load DXT data by buffer
* @param io
* @throws IOException
*/
protected void LoadDXT(ByteFileReader io) throws IOException {
if (header == null) {
throw new IOException("DDS Header is NULL");
}
rowBlockSize = header.dwWidth * 4;
int width = header.dwWidth & ~3;
int height = header.dwHeight & ~3;
int bytesPerBlock = 8;
if (FOURCC_DXT3.equals(header.ddpfPixelFormat.dwFourCC)) {
bytesPerBlock = 16;
} else if (FOURCC_DXT5.equals(header.ddpfPixelFormat.dwFourCC)) {
bytesPerBlock = 16;
}
//allocate a 32-bit dib
bpp = 32;
int line = ((width * bpp) + 7) / 8;
int widthRest = width & 3;
int heightRest = height & 3;
int inputLine = (width + 3) / 4;
ByteBuffer buff = ByteBuffer.allocate(inputLine * bytesPerBlock);
if (height >= 4) {
for (int yheight = 0; yheight < height; yheight += 4) {
buff.clear();
io.read(buff.array());
if (width >= 4) {
for (int xwidth = 0; xwidth < width; xwidth += 4) {
this.DecodeDXTBlock(image, buff, xwidth, yheight, 4, 4);
}
}
if (widthRest != 0) {
this.DecodeDXTBlock(image, buff, widthRest, yheight, widthRest, 4);
}
}
}
if (heightRest != 0) {
buff.clear();
io.read(buff.array());
if (width >= 4) {
for (int xwidth = 0; xwidth < width; xwidth += 4) {
this.DecodeDXTBlock(image, buff, xwidth, heightRest, 4, heightRest);
}
}
if (widthRest != 0) {
this.DecodeDXTBlock(image, buff, widthRest, heightRest, widthRest, heightRest);
}
}
}
/**
* dacode DXT pixel data
* @param img
* @param io
* @param width
* @param height
* @param bw
* @param bh
*/
protected void DecodeDXTBlock(BufferedImage img, ByteBuffer io, int width, int height, int bw, int bh) {
int alpha[] = null;
int aphrow[] = null;
int data[] = null;
//read alpha data
if (FOURCC_DXT3.equals(header.ddpfPixelFormat.dwFourCC)) {
aphrow = new int[4];
for (int i = 0; i < 4; i++) {
aphrow[i] = BufferUtil.readBytesAsInt(io, 2);
}
} else if (FOURCC_DXT5.equals(header.ddpfPixelFormat.dwFourCC)) {
alpha = new int[8];
for (int i = 0; i < 2; i++) {
alpha[i] = BufferUtil.readBytesAsInt(io, 1);
}
if (alpha[0] > alpha[1]) {
//8 alpha block
for (int i = 0; i < 6; i++) {
alpha[i + 2] = (((6 - i) * alpha[0] + (1 + i) * alpha[1] + 3) / 7);
}
} else {
//6 alpha block
for (int i = 0; i < 4; i++) {
alpha[i + 2] = (((4 - i) * alpha[0] + (1 + i) * alpha[1] + 2) / 5);
}
alpha[6] = 0x00;
alpha[7] = 0xFF;
}
data = new int[6];
for (int i = 0; i < 6; i++) {
data[i] = BufferUtil.readBytesAsInt(io, 1);
}
}
//read color data
Color8888[] colors = new Color8888[4];
for (int i = 0; i < 4; i++) {
colors[i] = new Color8888();
}
int colval[] = new int[2];
for (int i = 0; i < 2; i++) {
colval[i] = BufferUtil.readBytesAsInt(io, 2);
}
GetBlockColors(colval, colors, true);
int row[] = new int[4];
for (int i = 0; i < 4; i++) {
row[i] = BufferUtil.readBytesAsInt(io, 1);
}
//decode color data
for (int y = 0; y < bh; y++) {
int m_colorRow = row[y];
int m_alphaRow = 0;
int m_alphaBits = 0;
int m_offset = 0;
if (FOURCC_DXT3.equals(header.ddpfPixelFormat.dwFourCC)) {
m_alphaRow = (short) aphrow[y];
} else if (FOURCC_DXT5.equals(header.ddpfPixelFormat.dwFourCC)) {
int ti = y / 2;
m_alphaBits = (data[0 + ti * 3] | (data[1 + ti * 3] << 8) | (data[2 + ti * 3] << 16));
m_offset = (y & 1) * 12;
}
for (int x = 0; x < bw; x++) {
Color8888 getCol = new Color8888();
int bits = ((m_colorRow >> (x * 2)) & 3);
getCol.Copy(colors[bits]);
if (FOURCC_DXT3.equals(header.ddpfPixelFormat.dwFourCC)) {
bits = ((m_alphaRow >> (x *
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
支持将DDS图像数据转换为BufferedImage,以便在GUI程序中显示. dds是DirectDraw Surface的缩写,实际上,它是DirectX纹理压缩(DirectX Texture Compression,简称DXTC)的产物。DXTC减少了纹理内存消耗的50%甚至更多,有3种DXTC的格式可供使用,它们分别是DXT1,DXT3和DXT5。 在OrigoEngine的材质系统中,可以看到很多的dds文件。可以简单地认为这些dds文件同bmp、tga等常见的图片格式一样,记录了一张图片的信息,如果我们在photoshop中使用dds的插件,就可以在photoshop中打开这些文件。
资源详情
资源评论
资源推荐
收起资源包目录















































共 34 条
- 1










格式:zip 资源大小:201.6KB






格式:pdf 资源大小:420.8KB 页数:8















JemiChow
- 粉丝: 19
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 利用MATHLAB研究火箭升空问题-软件.docx
- 某网站建设招标书.doc
- 卷积神经网络的对抗性攻击与防御实验研究
- DNS解析的探究.docx
- 某某国家森林公园旅游区建设项目管理.doc
- 2009年9月全国计算机等级考试四级网络工程师试题.doc
- C--面向对象程序设计-(陈维新-林小茶-著).doc
- 单片机火灾自动报警系统方案设计书.doc
- (源码)基于C++和Qt框架的Nitrokey应用程序.zip
- 单片机控制八音盒的方案设计大学课程方案设计.doc
- C语言课程方案设计书-学生综合测评系统.doc
- 信息化工作管理标准.doc
- 基于Hadoop的市政设施监控大数据分析.docx
- 单片机全自动洗衣机控制系统软硬件设计方案.doc
- 基于大数据理论的企业档案管理提升策略.docx
- 110千伏及以上电力项目管理投资建设资金管理.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论5