java生成tga图片,Java TGA加载器

I am looking for a small and free TGA image loading class or library for java.

Ideally the result is a BufferedImage.

Yes, I have already googled, but most results are outdated, or are quite big libraries that contain a lot of other stuff i dont need. I am looking for something small and simple that reads just TGA images.

Thanks!

解决方案

We use this class copied from some open source project to read TGA files. It's really old. It can only handle Targa files with most basic encoding. Give it a try.

public class TargaReader

{

public static Image getImage(String fileName) throws IOException

{

File f = new File(fileName);

byte[] buf = new byte[(int)f.length()];

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));

bis.read(buf);

bis.close();

return decode(buf);

}

private static int offset;

private static int btoi(byte b)

{

int a = b;

return (a<0?256+a:a);

}

private static int read(byte[] buf)

{

return btoi(buf[offset++]);

}

public static Image decode(byte[] buf) throws IOException

{

offset = 0;

// Reading header

for (int i=0;i<12;i++)

read(buf);

int width = read(buf)+(read(buf)<<8);

int height = read(buf)+(read(buf)<<8);

read(buf);

read(buf);

// Reading data

int n = width*height;

int[] pixels = new int[n];

int idx=0;

while (n>0)

{

int nb = read(buf);

if ((nb&0x80)==0)

{

for (int i=0;i<=nb;i++)

{

int b = read(buf);

int g = read(buf);

int r = read(buf);

pixels[idx++] = 0xff000000 | (r<<16) | (g<<8) | b;

}

}

else

{

nb &= 0x7f;

int b = read(buf);

int g = read(buf);

int r = read(buf);

int v = 0xff000000 | (r<<16) | (g<<8) | b;

for (int i=0;i<=nb;i++)

pixels[idx++] = v;

}

n-=nb+1;

}

BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

bimg.setRGB(0,0,width,height,pixels,0,width);

return bimg;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值