您的位置:首页 > 其它

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

2005-11-30 11:20 344 查看
作者: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;
}

效果图如下
原始图



用代码进行转换的效果图



注意 :为了能让你的程序实现转换更简单,也就是说你怎么知道要变换那些颜色值。例如上面说的转换条件为:
r > b && r > g
所以一张图片的设计,像素的取舍需要跟美工配合好,需要画什么样的图片,以及需要转换的像素点。也可见MacroSpace中美工和程序是紧密地结合在一起的,做到了代码实现更方便以及用代码来节省图片资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: