exportSprite之三(AImage.java)

本文介绍了一个名为AImage的Java类,该类用于处理多种类型的图像文件,包括PNG、BMP和其他格式。AImage支持透明度设置、调色板加载及索引颜色图像处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AImage.java
文件如下:
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;

public classAImage
{

int id = 0;
String fileName = "";
int transp_color = 0;
String palFileName[];
int width = 0;
int height = 0;
int pals[][];
int palCount = 0;
int colorCount = 0;
byte data[];
int rgbData[];
private boolean blIndexColor = false;

AImage(String fileName, int transpColor, int palMax)
{
this.fileName = fileName;
transp_color = transpColor;
palFileName = new String[palMax];
pals = new int[palMax][];
if (isPngImage())
{
loadPngImage();
} else if (isBmpImage())
{
loadBmpImage();
}
else
{
blIndexColor=false;
loadOtherImage();
}
}

void appendPal(String palFile)
{
int i = 0;
for (i = 0; i < palFileName.length; i++)
{
if (palFileName[i] == null)
{
palFileName[i] = palFile;
break;
}
}
DataInputStream in = null;
try
{
File file = new File(palFile);
if (!file.exists())
{
System.out.println("Erro!the file :" + palFile + " do not exist");
return;
}
if (file.isDirectory())
{
System.out.println("Erro!the file : " + palFile + " is Directory");
return;
}
in = new DataInputStream(new FileInputStream(palFile));
int pal[] = new int[colorCount];
byte rgb[] = new byte[3];
for (i = 0; i < colorCount; i++)
{
in.read(rgb);
pal[i] = ((rgb[0] & 0xFF) << 16) | ((rgb[1] & 0xFF) << 16) | (rgb[2] & 0xFF);
}
if(pal[0]!=transp_color)
for(i=1;i<colorCount;i++)
{
if(pal[i]==transp_color)
{
pal[i]=pal[0];
pal[0]=transp_color;
break;
}
}
in.close();
pals[palCount++] = pal;
;
} catch (Exception e)
{
e.printStackTrace();
Util.closeInputStream(in);
in = null;
}
return;
}
boolean loadOtherImage()
{
DataInputStream in = null;
int i = 0;
int j = 0;
int k = 0;
try
{
File file = new File(fileName);

if (!file.exists())
{
System.out.println("Erro!the file :" + fileName + " do not exist");
return false;
}
if (file.isDirectory())
{
System.out.println("Erro!the file : " + fileName + " is Directory");
return false;
}
BufferedImage bufferImage = ImageIO.read(file);
width = bufferImage.getWidth();
height = bufferImage.getHeight();
rgbData = new int[width * height];
int sRGBData[] = bufferImage.getRGB(0, 0, width, height, rgbData, 0, width);

} catch (Exception e)
{
e.printStackTrace();
Util.closeInputStream(in);
in = null;
}
return true;
}
boolean loadPngImage()
{
DataInputStream in = null;
int i = 0;
int j = 0;
int k = 0;
try
{
File file = new File(fileName);

if (!file.exists())
{
System.out.println("Erro!the file :" + fileName + " do not exist");
return false;
}
if (file.isDirectory())
{
System.out.println("Erro!the file : " + fileName + " is Directory");
return false;
}
in = new DataInputStream(new FileInputStream(fileName));
byte magic[] = new byte[8];
in.read(magic);
int len = in.readInt();
byte ihdr[] = new byte[4];
in.read(ihdr);
width = in.readInt();
height = in.readInt();
byte info8[] = new byte[5];
in.read(info8);
byte CRC[] = new byte[4];
in.read(CRC);
Chunk chunk = null;
k = 6 + 1;
while (true && k > 0)
{
chunk = readChunk(in);
if (chunk.type[0] == 'P' && chunk.type[1] == 'L' && chunk.type[2] == 'T' && chunk.type[3] == 'E')
{
blIndexColor = true;
palCount=1;
break;
}
k--;
}
in.close();
BufferedImage bufferImage = ImageIO.read(file);
rgbData = new int[width * height];
data = new byte[width * height];
int sRGBData[] = bufferImage.getRGB(0, 0, width, height, rgbData, 0, width);
if (!blIndexColor)
{
return true;
}
colorCount = chunk.len / 3;
int pal[] = new int[colorCount];
for (i = 0; i < colorCount; i++)
{
k = i * 3;
pal[i] = ((chunk.data[k] & 0xFF) << 16) | ((chunk.data[k + 1] & 0xFF) << 8) | (chunk.data[k + 2] & 0xFF);
}
if(pal[0]!=transp_color)
for(i=1;i<colorCount;i++)
{
if(pal[i]==transp_color)
{
pal[i]=pal[0];
pal[0]=transp_color;
break;
}
}
pals[0] = pal;
for (i = 0; i < rgbData.length; i++)
{
k = rgbData[i] & 0xFFFFFF;

for (j = 0; j < colorCount; j++)
{
if (k == pal[j])
{
data[i] = (byte) j;
break;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
Util.closeInputStream(in);
in = null;
}
return true;

}

Chunk readChunk(DataInputStream in)
{
Chunk chunk = new Chunk(in);
return chunk;
}

boolean loadBmpImage()
{
int i = 0;
int j = 0;
int k = 0;
DataInputStream in = null;
try
{
File file = new File(fileName);
if (!file.exists())
{
System.out.println("Erro!the file :" + fileName + " do not exist");
return false;
}
if (file.isDirectory())
{
System.out.println("Erro!the file : " + fileName + " is Directory");
return false;
}
in = new DataInputStream(new FileInputStream(fileName));
int type0 = in.readByte();//位图文件的类型,必须为BM(0-1字节)
int type1 = in.readByte();
int temp = Util.readInt2(in);//位图文件的大小,以字节为单位(2-5字节)
Util.readShort2(in);//位图文件保留字,必须为0(6-7字节)
Util.readShort2(in);//位图文件保留字,必须为0(8-9字节)
int bfOffBits = Util.readInt2(in);// 位图数据的起始位置,以相对于位图(10-13字节)

temp = Util.readInt2(in);//本结构所占用字节数(14-17字节)
width = Util.readInt2(in);// 位图的宽度,以像素为单位(18-21字节)
height = Util.readInt2(in);//位图的高度,以像素为单位(22-25字节)
temp = Util.readShort2(in);//目标设备的级别,必须为1(26-27字节
short biBitCount = Util.readShort2(in);//每个像素所需的位数,必须是1(双色),(28-29字节) 4(16色),8(256色)或24(真彩色)之一
int biCompression = Util.readInt2(in);// 位图压缩类型,必须是 0(不压缩),(30-33字节) 1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
int biSizeImage = Util.readInt2(in);//位图的大小,以字节为单位(34-37字节)
Util.readInt2(in);//位图水平分辨率,每米像素数(38-41字节)
Util.readInt2(in);//位图垂直分辨率,每米像素数(42-45字节)
int colorCntUsed = Util.readInt2(in);//位图实际使用的颜色表中的颜色数(46-49字节)
int colorDisplay = Util.readInt2(in);//位图显示过程中重要的颜色数(50-53字节)
if (biBitCount <= 8)
{
colorCount = 1 << biBitCount;
} else
{
System.out.println("ERRO:" + fileName + "is not be index color!");
return false;
}
int pal[] = new int[colorCount];
colorCount = colorCntUsed;
int red;
int blue;
int green;
for (i = 0; i < colorCount; i++)
{
blue = in.readByte() & 0xFF;
green = in.readByte() & 0xFF;
red = in.readByte() & 0xFF;
in.readByte();//// 保留,必须为0
pal[i] = (red << 16) | (green << 8) | blue;
}
if(pal[0]!=transp_color)
for(i=1;i<colorCount;i++)
{
if(pal[i]==transp_color)
{
pal[i]=pal[0];
pal[0]=transp_color;
break;
}
}
pals[0] = pal;
in.close();

BufferedImage bufferImage = ImageIO.read(file);
rgbData = new int[width * height];
data = new byte[width * height];
int sRGBData[] = bufferImage.getRGB(0, 0, width, height, rgbData, 0, width);
blIndexColor=true;
palCount=1;
if (!blIndexColor)
{
return true;
}
for (i = 0; i < rgbData.length; i++)
{
k = rgbData[i] & 0xFFFFFF;

for (j = 0; j < colorCount; j++)
{
if (k == pal[j])
{
data[i] = (byte) j;
break;
}
}
}
/*
int size = width * height * biBitCount / 8;
data = new byte[size];
int DataSizePerLine = (width * biBitCount + 31) / 8;
// 一个扫描行所占的字节数
DataSizePerLine = DataSizePerLine / 4 * 4; // 字节数必须是4的倍数
j = 0;
for (i = height - 1; i >= 0; i--)
{
for (j = 0; j < width; j++)
{
k = i * width + j;
data[k] = in.readByte();
}
while (j < DataSizePerLine)
{
in.readByte();
}
}*/

} catch (Exception e)
{
e.printStackTrace();
Util.closeInputStream(in);
in = null;
}
return true;

}

byte[] getImageData(int x, int y, int w, int h)
{
if (x < 0 || x > width)
{
x = 0;
}
if (y < 0 || y > height)
{
y = 0;
}
if (x + w > width)
{
w = width - x;
}
if (y + h > height)
{
h = height - y;
}
byte moduleData[] = new byte[w * h];
int k = 0;
int index = 0;
for (int i = 0; i < h; i++)
{
k = (y + i) * width + x;
for (int j = 0; j < w; j++)
{
moduleData[index++] = data[k];
k++;
}
}
return moduleData;
}

int[] getRGB()
{
int k = 0;
int i = 0;
try
{
if (rgbData == null)
{
int pal[] = pals[0];
rgbData = new int[width * height];
for (i = 0; i < rgbData.length; i++)
{
k = data[i] & 0xFF;
rgbData[i] = pal[data[i] & 0xFF];
}
}
} catch (Exception e)
{
e.printStackTrace();
System.out.println(k);
}
return rgbData;
}
int[] getRGB(int x,int y,int w,int h)
{
if (x < 0 || x > width)
{
x = 0;
}
if (y < 0 || y > height)
{
y = 0;
}
if (x + w > width)
{
w = width - x;
}
if (y + h > height)
{
h = height - y;
}
int k = 0;
int i = 0;
try
{
if (rgbData == null&&blIndexColor)
{
int pal[] = pals[0];
rgbData = new int[width * height];
for (i = 0; i < rgbData.length; i++)
{
k = data[i] & 0xFF;
rgbData[i] = pal[data[i] & 0xFF];
}
}
} catch (Exception e)
{
e.printStackTrace();
System.out.println(k);
}
int moduleData[] = new int[w * h];
k = 0;
int index = 0;
for (i = 0; i < h; i++)
{
k = (y + i) * width + x;
for (int j = 0; j < w; j++)
{
moduleData[index++] = rgbData[k];
k++;
}
}
return moduleData;
}

boolean isPngImage()
{
if (fileName == null)
{
return false;
}
return fileName.endsWith(".png");
}

boolean isBmpImage()
{
if (fileName == null)
{
return false;
}
return fileName.endsWith(".bmp");
}
boolean isIndexedColor()
{
return blIndexColor;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值