栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java TGA加载器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java TGA加载器

我们使用从某些开源项目复制的此类读取TGA文件。真的很旧 它只能处理具有最基本编码的Targa文件。试试看。

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 depre(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 depre(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;        }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号