站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题古代帝国中的图片资源节省算法

古代帝国中的图片资源节省算法

摘要: 古代帝国中的图片资源节省算法
<tr><td>
http:///tech/article1308.html
[转载于minisoyo]

作者:bb


代码:

/**
* 图片转换
* @param content 原始图片信息
* @param type 类型,1 :中立角色 2 : 敌方 0 :自己
[] */
public static void imgConvert(byte content[], int type){
try{
int pos = 33;
[] int relpos = 0;
relpos = ((content[pos] & 0xff) << 24 | (content[pos + 1] & 0xff) << 16 | (content[pos + 2] & 0xff) << 8 | content[pos + 3] & 0xff) & -1;
pos += 4;
int newcolor = -1;
for(int idx = 0; idx < 4; idx++)
newcolor = pixelConvert(content[pos + idx], newcolor);

[] pos += 4;
boolean flag = true;
boolean flag1 = false;
boolean flag2 = false;
for(int idx = pos; idx < pos + relpos; idx += 3){
int b = content[idx] & 0xff;
int g = content[idx + 1] & 0xff;
int r = content[idx + 2] & 0xff;
if(r > b && r > g){
//绿色和蓝色互换,中立角色
if(type == 1){
int tmp = b;
b = g;
r = b;
g /= 2;
}
//转换为红色,敌人角色
else if (type == 2){
b = r;
g = r;
}
content[idx] = (byte)b;
content[idx + 1] = (byte)g;
content[idx + 2] = (byte)r;
}
[] newcolor = pixelConvert(content[idx], newcolor);
newcolor = pixelConvert(content[idx + 1], newcolor);
newcolor = pixelConvert(content[idx + 2], newcolor);
}

newcolor = ~newcolor;
int actpos = 41 + relpos;
content[actpos] = (byte)(newcolor >> 24);
content[actpos + 1] = (byte)(newcolor >> 16);
content[actpos + 2] = (byte)(newcolor >> 8);
content[actpos + 3] = (byte)newcolor;
}
[] catch(Exception e){}
}

/**
* 像素转换
* @param pixel 像素
* @param color 颜色值
* @return
*/
public static int pixelConvert(byte pixel, int color){
int tmp = pixel & 0xff;
color ^= tmp;
[] for(int idx = 0; idx < 8; idx++)
if((color & 1) != 0)
//土黄色
color = color >>> 1 ^ 0xedb88320;
else
color >>>= 1;
return color;
}

[]

效果图如下
原始图
540) this.width = 540'>

用代码进行转换的效果图
540) this.width = 540'>

注意 :为了能让你的程序实现转换更简单,也就是说你怎么知道要变换那些颜色值。例如上面说的转换条件为:
r > b && r > g

所以一张图片的设计,像素的取舍需要跟美工配合好,需要画什么样的图片,以及需要转换的像素点。也可见MacroSpace中美工和程序是紧密地结合在一起的,做到了代码实现更方便以及用代码来节省图片资源。
http:///tech/article1308.html
</td></tr></table></td> </tr> <tr> <td background="/pic/split.gif" height=1></td> </tr> <tr> <td class="tdMargin1">
↑返回目录
前一篇: 安全高效的使用PNG图
后一篇: 开发易于移植的J2ME游戏