zoukankan      html  css  js  c++  java
  • 转:JMir——Java版热血传奇2之资源文件与地图

      我虽然是90后,但是也很喜欢热血传奇2(以下简称“传奇”)这款游戏。

      进入程序员行业后自己也对传奇客户端实现有所研究,现在将我的一些研究结果展示出来,如果大家有兴趣的话不妨与我交流。

      

      项目我托管到codeplex上了,使用GPLv2开源协议。大家可以checkout代码出来看。

      我现在将地图加载出来了,算是达到了里程碑1吧。

      

      如果要将传奇的地图和资源文件详细解析可能我得写上几万字,不过我现在越来越懒了,就只将读取wix、wil、map文件的方法和它们的解析贴出来吧。

      准备工作:

        热血传奇十周年客户端

        JDK7

        Eclipse

      

      注意:

        阅读此篇文章后您将不需要再到网络上搜索传奇资源文件和地图文件解析,因为我的随笔绝对是最全最完整最详细的!但这可能需要您花费一些耐心。

      

      第一部分——地图:

        第一节——描述:

          Q: Tile是什么?

          A: Tile在中文是“瓷砖”、“块”的意思,具体到传奇地图中就是48*32屏幕像素大小的矩形区域。单个传奇地图就是由多个Tile构成的。

          Q: map格式文件究竟存放了哪些信息?

          A: map格式文件保存了一个完成地图的所有信息,但是对于当前Tile的图片只是保存了一个索引而不是把图片色彩数据保存下来。

          Q: map格式文件怎样读取?

          A: 对于文件读取以及对应到Java语言中的数据类型和数据结构我们要从两方面考虑。

            一是map的数据内容:

              map文件分为两部分。一个文件头标识了当前地图的高度、宽度等重要信息;剩余部分则是多个Tile的详细信息

            二是map格式文件是由Object-Pascal(以下简称Delphi)语言序列化而成的,我们首先需要了解从Delphi序列化的数据到Java反序列化需要进行的操作。

          以上内容表明了地图的信息,热血传奇中地图由Tile构成,每个Tile对应48*32屏幕像素大小。

          .map文件则保存了地图的宽度、高度以及每个Tile的详细信息。

        第二节——对应:

          .map文件如果对应到编程语言中数据结构的话在Delphi中如下(文件头):

    1 TMapHeader = packed record 
    2  wWidth: Word; 
    3  wHeight :Word; 
    4  sTitle :String[16]; 
    5  UpdateDate :TDateTime; 
    6  Reserved :array[0..22] of Char; 

          (Tile,两种都可以):

     1 type 
     2  TMapInfo = packed record 
     3  wBkImg :Word; 
     4  wMidImg :Word; 
     5  wFrImg :Word; 
     6  btDoorIndex :Byte; 
     7  btDoorOffset :Byte; 
     8  btAniFrame :Byte; 
     9  btAniTick :Byte; 
    10  btArea :Byte; 
    11  btLight :Byte; 
    12 
    13 type 
    14  TMapInfo = packed record 
    15  wBigTileImg :Word; 
    16  wSmTileImg :Word; 
    17  wObjImg :Word; 
    18  btDoorIndex :Byte; 
    19  btDoorOffset :Byte; 
    20  btAniFrame :Byte; 
    21  btAniTick :Byte; 
    22  btObjFile :Byte; 
    23  btLight :Byte; 
    MapTile

          每个.map文件如果在Delphi中就成了一个TMapHeader加wWidth*wHeight个MapTile。

          (对于每个字段占用的字节数请查看下面Java代码中注释)

      

          由于我们是使用Java语言描述热血传奇地图,所以我针对上述两个数据结构使用Java语言进行了描述:

      1 package org.coderecord.jmir.entt.internal;
      2 
      3 import java.util.Date;
      4 
      5 /**
      6  * 热血传奇2地图文件头
      7  * <p>
      8  * 针对*.map文件的数据结构使用Java语言描述
      9  * <br>
     10  * 地图文件头为52字节,在Pascal中定义为
     11  * <br>
     12  * &nbsp;TMapHeader = packed record
     13  * <br>
     14  * &emsp;wWidth:&emsp;Word;
     15  * <br>
     16  * &emsp;wHeight&emsp;:Word;
     17  * <br>
     18  * &emsp;sTitle&emsp;:String[16];
     19  * <br>
     20  * &emsp;UpdateDate&emsp;:TDateTime;
     21  * <br>
     22  * &emsp;Reserved&emsp;:array[0..22] of Char;
     23  * </p>
     24  * <p>
     25  * <b>wWidth</b> 表示地图宽度(占用两个字节,相当于Java语言short;一般不超过1000)
     26  * <br>
     27  * <b>wHeight</b> 表示地图高度(占用两个字节,相当于Java于洋short;一般不超过1000)
     28  * <br>
     29  * <b>sTitle</b> 标题,静态单字符串(占用17个字节,首字节为字符串已使用的长度即已存放的字符数,一般为“Legend of mir”)
     30  * <br>
     31  * <b>UpdateDate</b> 地图最后更新时间(占用8个字节,为TDateTime类型,可使用{@link org.coderecord.jmir.kits.Pascal#readDate(byte[], int, boolean) readDate} 转换为java.util.Date)
     32  * <br>
     33  * <b>Reserved</b> 保留字符,固定为23字节
     34  * </p>
     35  * 
     36  * @author ShawRyan
     37  *
     38  */
     39 public class MapHeader {
     40 
     41     /** 地图宽度(横向长度) */
     42     private short width;
     43     /** 地图高度(纵向长度) */
     44     private short height;
     45     /** 标题 */
     46     private String title;
     47     /** 更新日期 */
     48     private Date updateDate;
     49     /** 保留字符 */
     50     private char[] reserved;
     51     
     52     /** 默认构造函数 */
     53     public MapHeader() {}
     54     /** 带全部参数的构造函数 */
     55     public MapHeader(short width, short height, String title, Date updateDate, char[] reserved) {
     56         this.width = width;
     57         this.height = height;
     58         this.title = title;
     59         this.updateDate = updateDate;
     60         this.reserved = reserved;
     61     }
     62     /** 使用已有对象构造实例 */
     63     public MapHeader(MapHeader mapHeader) {
     64         this.width = mapHeader.getWidth();
     65         this.height = mapHeader.getHeight();
     66         this.title = mapHeader.getTitle();
     67         this.updateDate = mapHeader.getUpdateDate();
     68         this.reserved = mapHeader.getReserved();
     69     }
     70     
     71     /** 获取地图宽度(横向长度) */
     72     public short getWidth() {
     73         return width;
     74     }
     75     /** 设置地图宽度(横向长度) */
     76     public void setWidth(short width) {
     77         this.width = width;
     78     }
     79     /** 获取地图高度(纵向长度) */
     80     public short getHeight() {
     81         return height;
     82     }
     83     /** 设置地图高度(纵向长度) */
     84     public void setHeight(short height) {
     85         this.height = height;
     86     }
     87     /** 获取标题 */
     88     public String getTitle() {
     89         return title;
     90     }
     91     /** 设置标题 */
     92     public void setTitle(String title) {
     93         this.title = title;
     94     }
     95     /** 获取更新时间 */
     96     public Date getUpdateDate() {
     97         return updateDate;
     98     }
     99     /** 设置更新时间 */
    100     public void setUpdateDate(Date updateDate) {
    101         this.updateDate = updateDate;
    102     }
    103     /** 获取保留字符 */
    104     public char[] getReserved() {
    105         return reserved;
    106     }
    107     /** 设置保留字符 */
    108     public void setReserved(char[] reserved) {
    109         this.reserved = reserved;
    110     }
    111 }
    MapHeader

          (Tile我使用了两种描述方式,后一种用于生产环境更加优秀):

      1 package org.coderecord.jmir.entt.internal;
      2 
      3 /**
      4  * 热血传奇2地图“块”
      5  * <br>
      6  * 即 “<b>逻辑坐标</b>”点(人物/NPC等放置需要占用一个逻辑坐标点)
      7  * <br>
      8  * 需要注意的是逻辑坐标和屏幕坐标是不一样的,屏幕坐标一般为像素值,根据显示器分辨率设置而有所不同
      9  * <br>
     10  * 热血传奇2中一个逻辑坐标点(地图块)需要占用 48 * 32 屏幕坐标大小
     11  * <br>
     12  * 每个地图块为2层结构,包括‘地’和‘空’
     13  * 例如树叶投影下的地图块就是2层,包括地表及物体(如有突起石头的地面或有水流的地面)和树叶
     14  * <p>
     15  * 在Pascal语言中使用以下数据结构对地图块进行描述和存储(两种)
     16  * <br>
     17  * type
     18  * <br>
     19  * &nbsp;TMapInfo = packed record
     20  * <br>
     21  * &emsp;wBkImg&emsp;:Word;
     22  * <br>
     23  * &emsp;wMidImg&emsp;:Word;
     24  * <br>
     25  * &emsp;wFrImg&emsp;:Word;
     26  * <br>
     27  * &emsp;btDoorIndex&emsp;:Byte;
     28  * <br>
     29  * &emsp;btDoorOffset&emsp;:Byte;
     30  * <br>
     31  * &emsp;btAniFrame&emsp;:Byte;
     32  * <br>
     33  * &emsp;btAniTick&emsp;:Byte;
     34  * <br>
     35  * &emsp;btArea&emsp;:Byte;
     36  * <br>
     37  * &emsp;btLight&emsp;:Byte;
     38  * </p>
     39  * <p>
     40  * type
     41  * <br>
     42  * &nbsp;TMapInfo = packed record
     43  * <br>
     44  * &emsp;wBigTileImg&emsp;:Word;
     45  * <br>
     46  * &emsp;wSmTileImg&emsp;:Word;
     47  * <br>
     48  * &emsp;wObjImg&emsp;:Word;
     49  * <br>
     50  * &emsp;btDoorIndex&emsp;:Byte;
     51  * <br>
     52  * &emsp;btDoorOffset&emsp;:Byte;
     53  * <br>
     54  * &emsp;btAniFrame&emsp;:Byte;
     55  * <br>
     56  * &emsp;btAniTick&emsp;:Byte;
     57  * <br>
     58  * &emsp;btObjFile&emsp;:Byte;
     59  * <br>
     60  * &emsp;btLight&emsp;:Byte;
     61  * </p>
     62  * <p>
     63  * <b>wBkImg</b>或<b>wBigTileImg</b> 表示地图地表图片,如果最高位为1则表示不能通过(或站立),如河水型地表等。在判断是否可以飞过(从空中通过)时则不需要考虑
     64  * <br>
     65  * <b>wMidImg</b>或<b>wSmTileImg</b> 表示地图可视物体图片(有时被称为可视数据/中间层/小地图块/地图补充背景等等),如果wBkImg(或wBigTileImg)没有铺满则使用此地图块进行铺垫。最高位不作为判断依据,不过图片索引一般小于0x8000,即最高位一般为0。例如在某地图中第一个地图块的wBkImg(或wBigTileImg)大小为96 * 64,则代表该地图左上角4个块儿的地表都不为空,此时紧邻的三个地图块都可以不用设置wBkImg(或wBigTileImg)和wMidImg(或wSmTileImg);如果某个地图块的没有被其他块儿的wBkImg(或wBigTileImg)铺满,自己也没有wBkImg(或wBigTileImg),那么它就需要一个wMidImg(或wSmTileImg)进行铺垫。值得一提的是并不一定在有了wMidImg(或wBigTileImg)后就不需要绘制此层图片了
     66  * <br>
     67  * <b>wFrImg</b>或<b>wObjImg</b> 表示表层图片(对象),即空中遮挡物,如植物或建筑物,如果最高位为1则表示不能通过(或站立)。在判断是否可飞过(从空中通过)时需要作为唯一条件判断,在判断是否可以徒步通过或站立时需要联合wBkImg进行判断
     68  * <br>
     69  * <b>总的来说,地图一般为两层(只是针对上面的三个属性,下方的也属于地图部分,不过先不纳入考虑),包括背景层与对象层,背景层为wBkImg(或wBigTileImg)和wMidImg(或wSmTileImg)的集合,一般来说wBkImg就能搞定,也有时候需要两者都有;Spirit(人物/怪物/NPC/掉落物品等)在两层中间;索引从1开始,所以在从资源中真正取图片时应该减1(适用于所有资源索引);索引一般最高位为0,为1一般表示特殊情况(在Java语言中可以理解为大于0,因为首位为1表示负数)</b>
     70  * <br>
     71  * <b>btDoorIndex</b> 门索引,最高位为1表示有门,为0表示没有门。
     72  * <br>
     73  * <b>btDoorOffset</b> 门偏移,最高位为1表示门打开了,为0表示门为关闭状态
     74  * <br>
     75  * <b>btAniFrame</b> 帧数,指示当前地图块动态内容由多少张静态图片轮询播放,需要和btAniTick一起起作用;如果最高位为1(即值大于0x80,或者在Java中为小于0的数值)则表示有动态内容
     76  * <br>
     77  * <b>btAniTick</b> 跳帧数,指示当前地图块动态内容应该每隔多少帧变换当前显示的静态图片,需要和btAniFrame一起作用
     78  * <br>
     79  * <b>btAniFrame和btAniTick作用时表达式如下index = (gAniCount % (btAniFrame * (1 + btAniTick))) / (1 + btAniTick)
     80  * <br>
     81  * &emsp;其中gAniCount是当前画面帧是第几帧,它会在每次绘制游戏界面时累加,它可以有最大值,超过可以置0;index是相对当前objImgIdx的偏移量,比如当前对象层图片索引为1,而AniFrame为10,则表示从1到11这10副图片应该作为一动态内容播放(有待考证)
     82  * </b>
     83  * <br>
     84  * <b>btArea</b>或<b>btObjFile</b> 表示当前wFrImg(或wObjImg)和动态内容构成图片来自哪个Object资源文件,具体为Object{btArea}.wil中,如果btArea为0则是Objects.wil
     85  * <br>
     86  * <b>btLight</b> 亮度,一般为0/1/4
     87  * </p>
     88  * @author ShawRyan
     89  *
     90  */
     91 public class MapTile {
     92     
     93     /** 背景图索引 */
     94     private short bngImgIdx;
     95     /** 补充背景图索引 */
     96     private short midImgIdx;
     97     /** 对象图索引 */
     98     private short objImgIdx;
     99     /** 门索引 */
    100     private byte doorIdx;
    101     /** 门偏移 */
    102     private byte doorOffset;
    103     /** 动画帧数 */
    104     private byte aniFrame;
    105     /** 动画跳帧数 */
    106     private byte aniTick;
    107     /** 资源文件索引 */
    108     private byte objFileIdx;
    109     /** 亮度 */
    110     private byte light;
    111 
    112     /** 默认构造函数 */
    113     public MapTile() { }
    114     /** 使用已有对象构造实例 */
    115     public MapTile(MapTile mapTile) {
    116         this.bngImgIdx = mapTile.bngImgIdx;
    117         this.midImgIdx = mapTile.midImgIdx;
    118         this.objImgIdx = mapTile.objImgIdx;
    119         this.doorIdx = mapTile.doorIdx;
    120         this.doorOffset = mapTile.doorOffset;
    121         this.aniFrame = mapTile.aniFrame;
    122         this.aniTick = mapTile.aniTick;
    123         this.objFileIdx = mapTile.objFileIdx;
    124         this.light = mapTile.light;
    125     }
    126     /** 带全部参数的构造函数 */
    127     public MapTile(short bngImgIdx, short midImgIdx, short objImgIdx, byte doorIdx, byte doorOffset, byte aniFrame, byte aniTick, byte objFileIdx, byte light) {
    128         this.bngImgIdx = bngImgIdx;
    129         this.midImgIdx = midImgIdx;
    130         this.objImgIdx = objImgIdx;
    131         this.doorIdx = doorIdx;
    132         this.doorOffset = doorOffset;
    133         this.aniFrame = aniFrame;
    134         this.aniTick = aniTick;
    135         this.objFileIdx = objFileIdx;
    136         this.light = light;
    137     }
    138     
    139     /** 获取背景图索引 */
    140     public short getBngImgIdx() {
    141         return bngImgIdx;
    142     }
    143     /** 设置背景图索引 */
    144     public void setBngImgIdx(short bngImgIdx) {
    145         this.bngImgIdx = bngImgIdx;
    146     }
    147     /** 获取补充图索引 */
    148     public short getMidImgIdx() {
    149         return midImgIdx;
    150     }
    151     /** 设置补充图索引 */
    152     public void setMidImgIdx(short midImgIdx) {
    153         this.midImgIdx = midImgIdx;
    154     }
    155     /** 获取对象图索引 */
    156     public short getObjImgIdx() {
    157         return objImgIdx;
    158     }
    159     /** 设置对象图索引 */
    160     public void setObjImgIdx(short objImgIdx) {
    161         this.objImgIdx = objImgIdx;
    162     }
    163     /** 获取门索引 */
    164     public byte getDoorIdx() {
    165         return doorIdx;
    166     }
    167     /** 设置门索引 */
    168     public void setDoorIdx(byte doorIdx) {
    169         this.doorIdx = doorIdx;
    170     }
    171     /** 获取门偏移 */
    172     public byte getDoorOffset() {
    173         return doorOffset;
    174     }
    175     /** 设置门偏移 */
    176     public void setDoorOffset(byte doorOffset) {
    177         this.doorOffset = doorOffset;
    178     }
    179     /** 获取动画帧数 */
    180     public byte getAniFrame() {
    181         return aniFrame;
    182     }
    183     /** 设置动画帧数 */
    184     public void setAniFrame(byte aniFrame) {
    185         this.aniFrame = aniFrame;
    186     }
    187     /** 获取动画跳帧数 */
    188     public byte getAniTick() {
    189         return aniTick;
    190     }
    191     /** 设置动画跳帧数 */
    192     public void setAniTick(byte aniTick) {
    193         this.aniTick = aniTick;
    194     }
    195     /** 获取资源文件索引 */
    196     public byte getObjFileIdx() {
    197         return objFileIdx;
    198     }
    199     /** 设置资源文件索引 */
    200     public void setObjFileIdx(byte objFileIdx) {
    201         this.objFileIdx = objFileIdx;
    202     }
    203     /** 获取亮度 */
    204     public byte getLight() {
    205         return light;
    206     }
    207     /** 设置亮度 */
    208     public void setLight(byte light) {
    209         this.light = light;
    210     }
    211 }
    MapTile
      1 package org.coderecord.jmir.entt.internal;
      2 
      3 import org.coderecord.jmir.scn.DrawSupport;
      4 
      5 /**
      6  * MapTile方便程序逻辑的另类解读方式
      7  * 
      8  * @author ShawRyan
      9  *
     10  */
     11 public class MapTileInfo {
     12     /** 背景图索引 */
     13     private short bngImgIdx;
     14     /** 是否有背景图(在热血传奇2地图中,背景图大小为4个地图块,具体到绘制地图时则表现在只有横纵坐标都为双数时才绘制),见{@link DrawSupport#drawMap(int, int, org.coderecord.jmir.entt.Map, int, int) drawMap} */
     15     private boolean hasBng;
     16     /** 是否可行走(站立) */
     17     private boolean canWalk;
     18     /** 补充背景图索引 */
     19     private short midImgIdx;
     20     /** 是否有补充图 */
     21     private boolean hasMid;
     22     /** 对象图索引 */
     23     private short objImgIdx;
     24     /** 是否有对象图 */
     25     private boolean hasObj;
     26     /** 是否可以飞越 */
     27     private boolean canFly;
     28     /** 门索引 */
     29     private byte doorIdx;
     30     /** 是否有门 */
     31     private boolean hasDoor;
     32     /** 门偏移 */
     33     private byte doorOffset;
     34     /** 门是否开启 */
     35     private boolean doorOpen;
     36     /** 动画帧数 */
     37     private byte aniFrame;
     38     /** 是否有动画 */
     39     private boolean hasAni;
     40     /** 动画跳帧数 */
     41     private byte aniTick;
     42     /** 资源文件索引 */
     43     private byte objFileIdx;
     44     /** 亮度 */
     45     private byte light;
     46     
     47     /** 无参构造函数 */
     48     public MapTileInfo() { }
     49     /** 带全部参数的构造函数 */
     50     public MapTileInfo(short bngImgIdx, boolean hasBng, boolean canWalk, short midImgIdx, boolean hasMid, short objImgIdx, boolean hasObj, boolean canFly, byte doorIdx, boolean hasDoor, byte doorOffset, boolean doorOpen, byte aniFrame, boolean hasAni, byte aniTick, byte objFileIdx, byte light) {
     51         this.bngImgIdx = bngImgIdx;
     52         this.hasBng = hasBng;
     53         this.canWalk = canWalk;
     54         this.midImgIdx = midImgIdx;
     55         this.hasMid = hasMid;
     56         this.objImgIdx = objImgIdx;
     57         this.hasObj = hasObj;
     58         this.canFly = canFly;
     59         this.doorIdx = doorIdx;
     60         this.hasDoor = hasDoor;
     61         this.doorOffset = doorOffset;
     62         this.doorOpen = doorOpen;
     63         this.aniFrame = aniFrame;
     64         this.hasAni = hasAni;
     65         this.aniTick = aniTick;
     66         this.objFileIdx = objFileIdx;
     67         this.light = light;
     68     }
     69     /** 基于已有实体构造对象 */
     70     public MapTileInfo(MapTileInfo mapTileInfo) {
     71         this.bngImgIdx = mapTileInfo.bngImgIdx;
     72         this.hasBng = mapTileInfo.hasBng;
     73         this.canWalk = mapTileInfo.canWalk;
     74         this.midImgIdx = mapTileInfo.midImgIdx;
     75         this.hasMid = mapTileInfo.hasMid;
     76         this.objImgIdx = mapTileInfo.objImgIdx;
     77         this.hasObj = mapTileInfo.hasObj;
     78         this.canFly = mapTileInfo.canFly;
     79         this.doorIdx = mapTileInfo.doorIdx;
     80         this.hasDoor = mapTileInfo.hasDoor;
     81         this.doorOffset = mapTileInfo.doorOffset;
     82         this.doorOpen = mapTileInfo.doorOpen;
     83         this.aniFrame = mapTileInfo.aniFrame;
     84         this.hasAni = mapTileInfo.hasAni;
     85         this.aniTick = mapTileInfo.aniTick;
     86         this.objFileIdx = mapTileInfo.objFileIdx;
     87         this.light = mapTileInfo.light;
     88     }
     89 
     90     /** 获取背景图索引 */
     91     public short getBngImgIdx() {
     92         return bngImgIdx;
     93     }
     94     /** 设置背景图索引 */
     95     public void setBngImgIdx(short bngImgIdx) {
     96         this.bngImgIdx = bngImgIdx;
     97     }
     98     /** 获取该地图块是否有背景图 */
     99     public boolean isHasBng() {
    100         return hasBng;
    101     }
    102     /** 设置该地图块是否有背景图 */
    103     public void setHasBng(boolean hasBng) {
    104         this.hasBng = hasBng;
    105     }
    106     /** 获取该地图块是否可以站立或走过 */
    107     public boolean isCanWalk() {
    108         return canWalk;
    109     }
    110     /** 设置该地图块是否可以站立或走过 */
    111     public void setCanWalk(boolean canWalk) {
    112         this.canWalk = canWalk;
    113     }
    114     /** 获取补充图索引 */
    115     public short getMidImgIdx() {
    116         return midImgIdx;
    117     }
    118     /** 设置补充图索引 */
    119     public void setMidImgIdx(short midImgIdx) {
    120         this.midImgIdx = midImgIdx;
    121     }
    122     /** 获取该地图块是否有补充图 */
    123     public boolean isHasMid() {
    124         return hasMid;
    125     }
    126     /** 设置该地图块是否有补充图 */
    127     public void setHasMid(boolean hasMid) {
    128         this.hasMid = hasMid;
    129     }
    130     /** 获取对象图索引 */
    131     public short getObjImgIdx() {
    132         return objImgIdx;
    133     }
    134     /** 设置对象图索引 */
    135     public void setObjImgIdx(short objImgIdx) {
    136         this.objImgIdx = objImgIdx;
    137     }
    138     /** 获取该地图块是否有对象图 */
    139     public boolean isHasObj() {
    140         return hasObj;
    141     }
    142     /** 设置该地图块是否有对象图 */
    143     public void setHasObj(boolean hasObj) {
    144         this.hasObj = hasObj;
    145     }
    146     /** 获取该地图块是否可以飞越 */
    147     public boolean isCanFly() {
    148         return canFly;
    149     }
    150     /** 设置该地图块是否可以飞越 */
    151     public void setCanFly(boolean canFly) {
    152         this.canFly = canFly;
    153     }
    154     /** 获取门索引 */
    155     public byte getDoorIdx() {
    156         return doorIdx;
    157     }
    158     /** 设置门索引 */
    159     public void setDoorIdx(byte doorIdx) {
    160         this.doorIdx = doorIdx;
    161     }
    162     /** 获取该地图块是否有门 */
    163     public boolean isHasDoor() {
    164         return hasDoor;
    165     }
    166     /** 设置该地图块是否有门 */
    167     public void setHasDoor(boolean hasDoor) {
    168         this.hasDoor = hasDoor;
    169     }
    170     /** 获取门偏移 */
    171     public byte getDoorOffset() {
    172         return doorOffset;
    173     }
    174     /** 设置门偏移 */
    175     public void setDoorOffset(byte doorOffset) {
    176         this.doorOffset = doorOffset;
    177     }
    178     /** 获取该地图块门是否打开 */
    179     public boolean isDoorOpen() {
    180         return doorOpen;
    181     }
    182     /** 设置该地图块门是否打开 */
    183     public void setDoorOpen(boolean doorOpen) {
    184         this.doorOpen = doorOpen;
    185     }
    186     /** 获取动画帧数 */
    187     public byte getAniFrame() {
    188         return aniFrame;
    189     }
    190     /** 设置动画帧数 */
    191     public void setAniFrame(byte aniFrame) {
    192         this.aniFrame = aniFrame;
    193     }
    194     /** 获取该地图块是否有动画 */
    195     public boolean isHasAni() {
    196         return hasAni;
    197     }
    198     /** 设置该地图块是否有动画 */
    199     public void setHasAni(boolean hasAni) {
    200         this.hasAni = hasAni;
    201     }
    202     /** 获取动画跳帧数 */
    203     public byte getAniTick() {
    204         return aniTick;
    205     }
    206     /** 设置动画跳帧数 */
    207     public void setAniTick(byte aniTick) {
    208         this.aniTick = aniTick;
    209     }
    210     /** 获取资源文件索引 */
    211     public byte getObjFileIdx() {
    212         return objFileIdx;
    213     }
    214     /** 设置资源文件索引 */
    215     public void setObjFileIdx(byte objFileIdx) {
    216         this.objFileIdx = objFileIdx;
    217     }
    218     /** 获取亮度 */
    219     public byte getLight() {
    220         return light;
    221     }
    222     /** 设置亮度 */
    223     public void setLight(byte light) {
    224         this.light = light;
    225     }
    226 }
    MapTileInfo

          对于读取物理文件到产生对象,我使用一些工具方法,这里主要是高低位的问题,还有就是Delphi中字符串和时间日期到Java的不同处理。

        第三节——读取:

          我对.map文件读取数据生成对象的过程如下(工具方法请查阅源码,就不在此贴出了):

     1 /**
     2      * 通过字节数组反序列化地图文件头数据
     3      * 
     4      * @param bytes
     5      *     数据(文件中直接读取,未经过任何处理的字节数组)
     6      * @return
     7      *     地图文件头信息
     8      */
     9     public static MapHeader readMapHeader(byte[] bytes) {
    10         MapHeader res = new MapHeader();
    11         res.setWidth(Common.readShort(bytes, 0, true));
    12         res.setHeight(Common.readShort(bytes, 2, true));
    13         res.setTitle(readStaticSingleString(bytes, 4));
    14         res.setUpdateDate(readDate(bytes, 21, true));
    15         res.setReserved(readChars(bytes, 29, 23));
    16         return res;
    17     }
     1 /**
     2      * 通过字节数组反序列化地图逻辑坐标块儿数据
     3      * 
     4      * @param bytes
     5      *     数据(文件中直接读取,未经过任何处理的字节数组)
     6      * @return
     7      *     地图逻辑坐标块儿信息
     8      */
     9     public static MapTile readMapTile(byte[] bytes) {
    10         MapTile res = new MapTile();
    11         res.setBngImgIdx(Common.readShort(bytes, 0, true));
    12         res.setMidImgIdx(Common.readShort(bytes, 2, true));
    13         res.setObjImgIdx(Common.readShort(bytes, 4, true));
    14         res.setDoorIdx(bytes[6]);
    15         res.setDoorOffset(bytes[7]);
    16         res.setAniFrame(bytes[8]);
    17         res.setAniTick(bytes[9]);
    18         res.setObjFileIdx(bytes[10]);
    19         res.setLight(bytes[11]);
    20         return res;
    21     }
    22     
    23     /**
    24      * 通过字节数组反序列化地图逻辑坐标块信息
    25      * 
    26      * @param bytes
    27      *     数据(文件中直接读取,未经过任何处理的字节数组)
    28      * @return
    29      *     地图逻辑坐标块儿信息
    30      */
    31     public static MapTileInfo readMapTileInfo(byte[] bytes) {
    32         MapTileInfo res = new MapTileInfo();
    33         // 读取背景
    34         short bng = Common.readShort(bytes, 0, true);
    35         // 读取中间层
    36         short mid = Common.readShort(bytes, 2, true);
    37         // 读取对象层
    38         short obj = Common.readShort(bytes, 4, true);
    39         // 设置背景
    40         if((bng & 0b0111_1111_1111_1111) > 0) {
    41             res.setBngImgIdx((short) (bng & 0b0111_1111_1111_1111));
    42             res.setHasBng(true);
    43         }
    44         // 设置中间层
    45         if((mid & 0b0111_1111_1111_1111) > 0) {
    46             res.setMidImgIdx((short) (mid & 0b0111_1111_1111_1111));
    47             res.setHasMid(true);
    48         }
    49         // 设置对象层
    50         if((obj & 0b0111_1111_1111_1111) > 0) {
    51             res.setObjImgIdx((short) (obj & 0b0111_1111_1111_1111));
    52             res.setHasObj(true);
    53         }
    54         // 设置是否可站立
    55         res.setCanWalk(!Common.is1AtTopDigit(bng) && !Common.is1AtTopDigit(obj));
    56         // 设置是否可飞行
    57         res.setCanFly(!Common.is1AtTopDigit(obj));
    58                 
    59         res.setDoorOffset(bytes[7]);
    60         if(Common.is1AtTopDigit(bytes[7])) res.setDoorOpen(true);
    61         res.setAniFrame(bytes[8]);
    62         if(Common.is1AtTopDigit(bytes[8])) {
    63             res.setAniFrame((byte) (bytes[8] & 0x7F));
    64             res.setHasAni(true);
    65         }
    66         res.setAniTick(bytes[9]);
    67         res.setObjFileIdx(bytes[10]);
    68         res.setLight(bytes[11]);
    69         return res;
    70     }

      第二部分——资源文件:

        第一节——描述:

          Q: wix和wil文件分别是什么?

          A: wix全名为“Wemade Image Index”,顾名思义是图片库索引;wil全名为“Wemade Image Lib”,意为图片库。wix文件中存储了对应图片库的基本信息,包括图片数量、每个图片色彩数据起始位置;wil文件则存储了包括图片调色板、图片色彩数据在内的所有图片信息。

          Q: wix和wil需要对应起来用吗?

          A: 其实在生产环境中只需要使用wil就可以了,它包含了程序所需所有信息,网络上有人说必须使用wix来寻找每个图片色彩数据起始位置的说法是错误的,不过结合起来使用能最大限度避免错误,如果不服,请联系我!

          Q: wix文件结构和wil文件结构?

          A: wix文件由标题、图片数和图片色彩数据起始位置(对应wil中索引)的数组构成;wil文件由文件头和多个图片数据构成,文件头内容相对固定,每个图片色彩数据长度都不尽相同。

        第二节——对应:

          wix文件:

      

            Delphi语言描述(起始位置数组没有加上):

    1 type 
    2  TIndexHeader = record 
    3  sTitle :String[40]; 
    4  iIndexCount :Integer; 

            Java语言描述:

     1 package org.coderecord.jmir.entt;
     2 
     3 /**
     4  * WIX即“WEMADE IMAGE INDEX”
     5  * <br>
     6  * 意味图片索引
     7  * <br>
     8  * 在热血传奇2中,图片资源存储方式一般为一个wix文件和一个wil文件形成一组,负责保存一个内容或功能的图片资源
     9  * <br>
    10  * wix文件为索引文件,通过它从wil文件中解析出图片数据
    11  * <br>
    12  * 对于wix文件(头)可使用如下数据结构进行描述
    13  * <br>
    14  * type
    15  * <br>
    16  * &nbsp;TIndexHeader&nbsp;=&nbsp;record
    17  * <br>
    18  * &emsp;sTitle&emsp;:String[40];
    19  * <br>
    20  * &emsp;iIndexCount&emsp;:Integer;
    21  * <br>
    22  * <b>sTitle一般为“INDX v1.0-WEMADE Entertainment inc.”,表示标题;iIndexCount表示对应wil(lib库文件)中存储的图片数量;其实重点是此文件中前44个字节用处不大,从45字节开始才是我们关心的。</b>
    23  * <p>
    24  *     <b>注意:</b>为什么sTitle是String[40]而非String[43],如果是后者,则sTitle会占用44字节,刚好是我们所说的前44字节?其实Pascal中对record的packed修饰符有特殊处理,<b>不带</b>packed表示“对齐”(对齐意味着能被4整除),现在sTitle为String[40],加上其头部修饰的一个字节占共用41字节,并不能被4整除,所以编译器会在后面加上3个字节来<b>“对齐”</b>,所以在这里sTitle一共占用44字节
    25  * </p>
    26  * 从第49字节开始则是iIndexCount个Integer类型数据,标识在对应wil文件中各个图片数据对应位置(array of Integer),这里的位置索引从0开始,指示在文件二进制数据的索引
    27  * 
    28  * @author ShawRyan
    29  *
    30  */
    31 public class WIX {
    32     /** 标题 */
    33     private String title;
    34     /** 资源数量 */
    35     private int indexCount;
    36     /** 资源数据起始位置 */
    37     private int[] indexArray;
    38     
    39     /** 默认构造函数 */
    40     public WIX() {}
    41     /** 基于已有对象构造实例 */
    42     public WIX(WIX wix) {
    43         this.title = wix.title;
    44         this.indexCount = wix.indexCount;
    45         this.indexArray = wix.indexArray;
    46     }
    47     /** 使用全部参数构造实例 */
    48     public WIX(String title, int indexCount, int[] indexArray) {
    49         this.title = title;
    50         this.indexCount = indexCount;
    51         this.indexArray = indexArray;
    52     }
    53     
    54     /** 获取标题 */
    55     public String getTitle() {
    56         return title;
    57     }
    58     /** 设置标题 */
    59     public void setTitle(String title) {
    60         this.title = title;
    61     }
    62     /** 获取资源数量 */
    63     public int getIndexCount() {
    64         return indexCount;
    65     }
    66     /** 设置资源数量 */
    67     public void setIndexCount(int indexCount) {
    68         this.indexCount = indexCount;
    69     }
    70     /** 获取资源索引 */
    71     public int[] getIndexArray() {
    72         return indexArray;
    73     }
    74     /** 设置资源索引 */
    75     public void setIndexArray(int[] indexArray) {
    76         this.indexArray = indexArray;
    77     }
    78 }
    Wix

          wil文件:

      

            Delphi语言描述(调色板未加上,调色板说起来比较麻烦):

    1 type 
    2  TLibHeader = record 
    3  sTitle :String[40]; 
    4  iImageCount :Integer; 
    5  iColorCount :Integer; 
    6  iPaletteSize :Integer; 
    1 type 
    2  TImageInfo = record 
    3  siWidth :SmallInt; 
    4  siHeight :SmallInt; 
    5  siPx :SmallInt; 
    6  siPy :SmallInt; 
    7  Bits :PByte; 

          Java语言描述:

     1 package org.coderecord.jmir.entt;
     2 
     3 import org.coderecord.jmir.entt.internal.ImageInfo;
     4 import org.coderecord.jmir.entt.internal.LibHeader;
     5 
     6 /**
     7  * WIL即“WEMADE IMAGE LIBRARY”
     8  * <br>
     9  * 意为图片库
    10  * <br>
    11  * 存放多个图片数据(包括像素点色彩)
    12  * <br>
    13  * WIL文件由头部和多个图片信息组成
    14  * <br>
    15  * 头部可以使用如下类型进行描述
    16  * <br>
    17  * type
    18  * <br>
    19  * &nbsp;TLibHeader&nbsp;=&nbsp;record
    20  * <br>
    21  * &emsp;sTitle&emsp;:String[40];
    22  * <br>
    23  * &emsp;iImageCount&emsp;:Integer;
    24  * <br>
    25  * &emsp;iColorCount&emsp;:Integer;
    26  * <br>
    27  * &emsp;iPaletteSize&emsp;:Integer;
    28  * <br>
    29  * <b>头部共占用56字节,sTitle占用44字节,原因参见{@link WIX}。其中sTitle为标题,一般为“ILIB v1.0-WEMADE Entertainment inc.”;iImageCount为图片数量;iColorCount表示色彩位深度,如256表示8bit位图,65536表示16bit位图(2的幂数);iPaletteSize表示调色板字节数</b>
    30  * <br>
    31  * 头部后则是调色板(调色板请参照{@link org.coderecord.jmir.entt.internal.LibHeader#pallette LibHeader})和多个图片数据,包括图片宽高/坐标和像素色彩,可以使用下面的结构进行描述(不包括调色板,调色板其实也可放入header中,类型为 array of Byte)
    32  * <br>
    33  * type
    34  * <br>
    35  * &nbsp;TImageInfo&nbsp;=&nbsp;record
    36  * <br>
    37  * &emsp;siWidth&emsp;:SmallInt;
    38  * <br>
    39  * &emsp;siHeight&emsp;:SmallInt;
    40  * <br>
    41  * &emsp;siPx&emsp;:SmallInt;
    42  * <br>
    43  * &emsp;siPy&emsp;:SmallInt;
    44  * <br>
    45  * &emsp;Bits&emsp;:PByte;
    46  * <br>
    47  * <b>siWidth</b> 图片宽度
    48  * <br>
    49  * <b>siHeight</b> 图片高度
    50  * <br>
    51  * <b>siPx</b> 图片横向偏移量
    52  * <br>
    53  * <b>siPy</b> 图片纵向偏移量
    54  * <br>
    55  * <b>Bits</b> 图片像素色彩值数组
    56  * 
    57  * @author ShawRyan
    58  *
    59  */
    60 public class WIL {
    61     
    62     /** 文件头 */
    63     private LibHeader header;
    64     /** 图片数组 */
    65     private ImageInfo[] images;
    66     
    67     /** 无参构造函数 */
    68     public WIL() {}
    69     /** 基于已有实例构造对象 */
    70     public WIL(WIL wil) {
    71         this.header = wil.header;
    72         this.images = wil.images;
    73     }
    74     /** 全参构造函数 */
    75     public WIL(LibHeader header, ImageInfo[] images) {
    76         this.header = header;
    77         this.images = images;
    78     }
    79     
    80     /** 获取头部 */
    81     public LibHeader getHeader() {
    82         return header;
    83     }
    84     /** 设置头部 */
    85     public void setHeader(LibHeader header) {
    86         this.header = header;
    87     }
    88     /** 获取图片 */
    89     public ImageInfo[] getImages() {
    90         return images;
    91     }
    92     /** 设置图片 */
    93     public void setImages(ImageInfo[] images) {
    94         this.images = images;
    95     }
    96 }
    Wil
      1 package org.coderecord.jmir.entt.internal;
      2 
      3 /** WIL文件头 */
      4 public class LibHeader {
      5     /** 长度(字节数),不包括调色板大小 */
      6     public static final int BIT_LENGTH_WITHOUT_PATTELE = 56;
      7     
      8     /** 标题 */
      9     private String title;
     10     /** 图片数量 */
     11     private int imageCount;
     12     /** 色深度 */
     13     private int colorCount;
     14     /** 调色板字节数 */
     15     private int paletteSize;
     16     /**
     17      * 调色板
     18      * <br>
     19      * 调色板使用是一组字节数组即二维字节数组,第二维总为4字节,依次存储蓝、绿、红、Alpha色彩值
     20      * <br>
     21      * <br>
     22      * 对于任意色彩而言,都应该使用24位来存储,比如FF0000表示红色
     23      * <br>
     24      * 24位色彩值也被称为真彩
     25      * <br>
     26      * Windows 中位图有两种格式:
     27      * <br>
     28      * &emsp;设备相关位图 Device Depend Bitmap DDB
     29      * <br>
     30      * &emsp;设备无关位图 Device Independ Bitmap DIB
     31      * <br>
     32      * 热血传奇使用DIB对图片进行存储
     33      * <br>
     34      * 色彩深度有时少于24位,有时是因为精度要求并不会那么高,例如使用5或6个字节存储的R/G/B值就已经够用,此时可使用16位颜色值存储一个像素点的颜色
     35      * <br>
     36      * &emsp;有时甚至一幅图的色彩不超过256种,此时就可以将这256中颜色提取出来作为一个调色板,然后像素点色彩值则存储色彩值在这个调色板中的索引,这就是8位颜色值
     37      * <br>
     38      * &emsp;对于单色或16色(即使用1bit或4bit存储色彩值的情况不与考虑)
     39      */
     40     private int[] palette;
     41     
     42     /** 无参构造函数 */
     43     public LibHeader() {}
     44     /** 基于已有对象构造实例 */
     45     public LibHeader(LibHeader header) {
     46         this.title = header.title;
     47         this.imageCount = header.imageCount;
     48         this.colorCount = header.colorCount;
     49         this.paletteSize = header.paletteSize;
     50         this.palette = header.palette;
     51     }
     52     /** 带全部参数的构造函数 */
     53     public LibHeader(String title, int imageCount, int colorCount, int paletteSize, int[] pallette) {
     54         this.title = title;
     55         this.imageCount = imageCount;
     56         this.colorCount = colorCount;
     57         this.paletteSize = paletteSize;
     58         this.palette = pallette;
     59     }
     60     
     61     /** 获取标题 */
     62     public String getTitle() {
     63         return title;
     64     }
     65     /** 设置标题 */
     66     public void setTitle(String title) {
     67         this.title = title;
     68     }
     69     /** 获取图片数量 */
     70     public int getImageCount() {
     71         return imageCount;
     72     }
     73     /** 设置图片数量 */
     74     public void setImageCount(int imageCount) {
     75         this.imageCount = imageCount;
     76     }
     77     /** 获取色深 */
     78     public int getColorCount() {
     79         return colorCount;
     80     }
     81     /** 设置色深 */
     82     public void setColorCount(int colorCount) {
     83         this.colorCount = colorCount;
     84     }
     85     /** 获取调色板大小 */
     86     public int getPaletteSize() {
     87         return paletteSize;
     88     }
     89     /** 设置调色板大小 */
     90     public void setPaletteSize(int paletteSize) {
     91         this.paletteSize = paletteSize;
     92     }
     93     /** 获取调色板 */
     94     public int[] getPalette() {
     95         return palette;
     96     }
     97     /** 设置调色板 */
     98     public void setPalette(int[] pallette) {
     99         this.palette = pallette;
    100     }
    101 }
    LibHeader
      1 package org.coderecord.jmir.entt.internal;
      2 
      3 /** 图片信息 */
      4 public class ImageInfo {
      5     /** 图片宽度 */
      6     private short width;
      7     /** 图片高度 */
      8     private short height;
      9     /** 图片横向偏移量 */
     10     private short offsetX;
     11     /** 图片纵向偏移量 */
     12     private short offsetY;
     13     /**
     14      * 图片数据
     15      * <br>
     16      * 逐列存储像素色彩值,对于4 * 4的图片而言,其色彩数据如下(以字节为单位)
     17      * <br>
     18      * <b>注意:</b>如果图片宽度字节数不是4的倍数会有填充字节数,如果是读取真正的图片数据可以不用考虑,但如果需要一次性读取多张图片则需要跳过填充的字节,参见{@link org.coderecord.jmir.kits.Pascal#fillPByteLineWidth(int, int) fillPByteLineWidth}
     19      * <br>
     20      * 8位
     21      * <br>
     22      * 12&emsp;8&emsp;4&emsp;0&emsp;
     23      * <br>
     24      * 13&emsp;9&emsp;5&emsp;1&emsp;
     25      * <br>
     26      * 14&emsp;10&emsp;6&emsp;2&emsp;
     27      * <br>
     28      * 15&emsp;11&emsp;7&emsp;3&emsp;
     29      * <br>
     30      * 16位
     31      * <br>
     32      * 24&25&emsp;16&17&emsp;8&9&emsp;0&1&emsp;
     33      * <br>
     34      * 26&27&emsp;18&19&emsp;10&11&emsp;2&3&emsp;
     35      * <br>
     36      * 28&29&emsp;20&21&emsp;12&13&emsp;4&5&emsp;
     37      * <br>
     38      * 30&31&emsp;22&23&emsp;14&15&emsp;6&7&emsp;
     39      */
     40     private byte[] pixels;
     41     
     42     /** 无参构造函数 */
     43     public ImageInfo() {}
     44     /** 基于已有对象构造实例 */
     45     public ImageInfo(ImageInfo imageInfo) {
     46         this.height = imageInfo.height;
     47         this.offsetX = imageInfo.offsetX;
     48         this.offsetY = imageInfo.offsetY;
     49         this.pixels = imageInfo.pixels;
     50         this.width = imageInfo.width;
     51     }
     52     /** 带全部参数的构造函数 */
     53     public ImageInfo(short width, short height, short offsetX, short offsetY, byte[] pixels) {
     54         this.width = width;
     55         this.height = height;
     56         this.offsetX = offsetX;
     57         this.offsetY = offsetY;
     58         this.pixels = pixels;
     59     }
     60     
     61     /** 获取图片宽度 */
     62     public short getWidth() {
     63         return width;
     64     }
     65     /** 设置图片高度 */
     66     public void setWidth(short width) {
     67         this.width = width;
     68     }
     69     /** 获取图片高度 */
     70     public short getHeight() {
     71         return height;
     72     }
     73     /** 设置图片高度 */
     74     public void setHeight(short height) {
     75         this.height = height;
     76     }
     77     /** 获取图片横线偏移量 */
     78     public short getOffsetX() {
     79         return offsetX;
     80     }
     81     /** 设置图片横向偏移量 */
     82     public void setOffsetX(short offsetX) {
     83         this.offsetX = offsetX;
     84     }
     85     /** 获取图片纵向偏移量 */
     86     public short getOffsetY() {
     87         return offsetY;
     88     }
     89     /** 设置图片纵向偏移量 */
     90     public void setOffsetY(short offsetY) {
     91         this.offsetY = offsetY;
     92     }
     93     /** 获取图片二进制数据 */
     94     public byte[] getPixels() {
     95         return pixels;
     96     }
     97     /** 设置图片二进制数据 */
     98     public void setPixels(byte[] pixels) {
     99         this.pixels = pixels;
    100     }
    101 }
    ImageInfo

        第三节——读取:

          我们的目的在于使用wil中的图片,在上图其实可以看到我们只差一个每个图片色彩数据大小(??处),这个大小可以自己计算得到(涉及到Delphi位图处理,信息量较大,不在此赘述),但我们也可以从wix中拿到,这样比较方便,而且不会出错。

     1 /**
     2      * 根据库文件路径和索引文件路径读取图片库
     3      * 
     4      * @param wilPath
     5      *     图片库文件路径
     6      * @param wixPath
     7      *     图片索引文件路径
     8      * @return
     9      *     得到的图片库文件路径
    10      */
    11     public static WIL readWILFromFile(String wilPath, String wixPath) {
    12         WIX wix = new WIX();
    13         try(FileInputStream fis = new FileInputStream(wixPath)) {
    14             wix.setTitle(Pascal.readStaticSingleString(fis, 0, 44));
    15             wix.setIndexCount(Common.readInt(fis, 0, true));
    16             int[] imageIndexs = new int[wix.getIndexCount()];
    17             for(int i = 0; i < imageIndexs.length; ++i)
    18                 imageIndexs[i] = Common.readInt(fis, 0, true);
    19             wix.setIndexArray(imageIndexs);
    20         } catch (FileNotFoundException e) {
    21             throw new RuntimeException(e);
    22         } catch (IOException e) {
    23             throw new RuntimeException(e);
    24         }
    25         WIL res = new WIL();
    26         try(FileInputStream fis = new FileInputStream(wilPath)) {
    27             res.setHeader(Pascal.readLibHeader(fis));
    28         } catch (FileNotFoundException e) {
    29             throw new RuntimeException(e);
    30         } catch (IOException e) {
    31             throw new RuntimeException(e);
    32         }
    33         try(RandomAccessFile raf = new RandomAccessFile(wilPath, "r")){
    34             ImageInfo[] images = new ImageInfo[res.getHeader().getImageCount()];
    35             for(int i = 0; i < images.length; ++i)
    36                 images[i] = Pascal.readImageInfo(raf, wix.getIndexArray()[i], Pascal.colorCountToBitCount(res.getHeader().getColorCount()));
    37             res.setImages(images);
    38         } catch (FileNotFoundException e) {
    39             throw new RuntimeException(e);
    40         } catch (IOException e) {
    41             throw new RuntimeException(e);
    42         }
    43         return res;
    44     }
    readWil

        第四节——图片处理:

          图片数据需要经过转换才能在界面上展示(针对8位的图片):

          首先在读取LibHeader时就需要做透明色处理:

     1 /**
     2      * 从流中读取图片库文件头
     3      * 
     4      * @param is
     5      *     数据流
     6      * @return
     7      *     文件头
     8      * @throws IOException
     9      *     可能的流异常
    10      */
    11     public static LibHeader readLibHeader(InputStream is) throws IOException {
    12         LibHeader res = new LibHeader();
    13         res.setTitle(Pascal.readStaticSingleString(is, 0, 44));
    14         res.setImageCount(Common.readInt(is, 0, true));
    15         res.setColorCount(Common.readInt(is, 0, true));
    16         res.setPaletteSize(Common.readInt(is, 0, true));
    17         int[] palette = new int[res.getPaletteSize() / 4];
    18         for(int i = 0; i < palette.length; ++i) {
    19             byte[] byteArgb = new byte[4];
    20             is.read(byteArgb);
    21             // 最重要的一步,重设Alpha值
    22             // 调色板的Alpha值都为0,而实际上只有0x00000000(一般在调色板第一个色彩)才是透明色,即热血传奇2图片库中8位色彩没有不透明的纯黑色
    23             if(byteArgb[2] == 0 && byteArgb[1] == 0 && byteArgb[0] == 0)
    24                 byteArgb[3] = 0;
    25             else
    26                 byteArgb[3] = (byte) 255;
    27             
    28             palette[i] = Common.readInt(byteArgb, 0, true);
    29         }
    30         
    31         res.setPalette(palette);
    32         return res;
    33     }
    readLibHeader

          在显示时要记住图片颜色数据的存储是从右向左,从上往下的:

     1 /**
     2      * 从ImageInfo对象及调色板和色彩深度读取图片数据
     3      * 
     4      * @param imageInfo
     5      *     图片原数据信息
     6      * @param palette
     7      *     调色板
     8      * @param bitCount
     9      *     色彩深度
    10      * @return
    11      */
    12     public static BufferedImage readImage(ImageInfo imageInfo, int[] palette, int bitCount) {
    13         BufferedImage res = null;        
    14         if(bitCount == 8) {
    15             res = new BufferedImage(imageInfo.getWidth(), imageInfo.getHeight(), BufferedImage.TYPE_INT_ARGB);
    16 
    17             int index = 0;
    18             for(int h = 0; h < imageInfo.getHeight(); ++h)
    19                 for(int w = 0; w < imageInfo.getWidth(); ++w)
    20                     res.setRGB(w, res.getHeight() - 1 - h, palette[imageInfo.getPixels()[index++] & 0xff]);
    21         } else if(bitCount == 16) {
    22             // FIXME
    23             res = new BufferedImage(imageInfo.getWidth(), imageInfo.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
    24             int index = 0;
    25             for(int h = 0; h < imageInfo.getHeight(); ++h)
    26                 for(int w = 0; w < imageInfo.getWidth(); ++w, index += 2)
    27                     res.setRGB(w, res.getHeight() - 1 - h, Common.readShort(imageInfo.getPixels(), index, true));
    28         } else if(bitCount == 24) {
    29             // FIXME
    30             //res = new BufferedImage(imageInfo.getWidth(), imageInfo.getHeight(), BufferedImage.TYPE_INT_RGB);
    31             //int index = 0;
    32             //for(int h = 0; h < imageInfo.getHeight(); ++h)
    33             //    for(int w = 0; w < imageInfo.getWidth(); ++w)
    34             //        res.setRGB(w, h, Common.readInt(imageInfo.getPixels(), index++, true));
    35         } else {
    36             // FIXME
    37             //res = new BufferedImage(imageInfo.getWidth(), imageInfo.getHeight(), BufferedImage.TYPE_INT_RGB);
    38             //int index = 0;
    39             //for(int h = 0; h < imageInfo.getHeight(); ++h)
    40             //    for(int w = 0; w < imageInfo.getWidth(); ++w)
    41             //        res.setRGB(w, h, Common.readInt(imageInfo.getPixels(), index++, true));
    42         }
    43         return res;
    44     }
    readImage

      说了这么久,我自己都糊涂了。大家如果不清楚请下载源码或基于Eclipse和JDK的项目进行查看。

     欢迎您移步我们的交流群,无聊的时候大家一起打发时间:Programmer Union

     或者通过QQ与我联系:点击这里给我发消息

     (最后编辑时间2014-03-16 15:43:23)

  • 相关阅读:
    JDom写入XML例子
    hdu 2549
    hdu 1328
    hdu 1334
    hdu 2547
    hdu 2374
    hdu 2550
    hdu 1335
    hdu 2548
    hdu 1722
  • 原文地址:https://www.cnblogs.com/faeriesoft/p/3989728.html
Copyright © 2011-2022 走看看