古代帝国中的图片资源节省算法
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中美工和程序是紧密地结合在一起的,做到了代码实现更方便以及用代码来节省图片资源。
代码:
/**
* 图片转换
* @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中美工和程序是紧密地结合在一起的,做到了代码实现更方便以及用代码来节省图片资源。
相关文章推荐
- Android ImageSwitcher、TextSwitcher以及以比较节省资源的方式显示jpg图片小例子
- 深度学习为图片压缩算法赋能:节省55%带宽
- 即 为什么要优化图片Bitmap资源,具体如下图
- Android资源匹配算法
- 【资源】我的图片
- ZXing之二维码解析图片资源
- lightinthebox ZenCart里调节 设置产品图片大小的方法及其算法
- 小程序实现选择图片九宫格源码资源
- android 性能优化---(5)Bitmap图片资源优化
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- Ajax资源下载 - 活动提示的图片
- 自由开发者_免费可商用的图片资源推荐
- Android的图片,字符串,demin,color,以及Array,boolean,Integer资源的使用-android学习之旅(五十四)
- wpf图片资源路径问题及C#代码设置图片路径
- c语言、算法、数据结构资源汇总(2014年11月21日 更新)
- spring MVC 跳转js css图片等静态资源无法加载问题
- 用Bitmap加载图片资源时,报错java.lang.OutOfMemoryError: bitmap size exceeds VM budget的解决办法
- macs上ipa解压图片资源工具
- VC2010“添加资源->引入JPG图片”会改变图片大小
- Fresco加载资源图片是黑色问题