public class ImageUtil {
public static final Image zoomImage(Image src){
return zoomImage(src,src.getWidth(),src.getHeight(),true,false);
}
public static final Image zoomImage(Image src, int desW, int desH, boolean isBackgroundTrans, boolean isTrans) {
Image desImg = null;
int srcW = src.getWidth(); // source image width
int srcH = src.getHeight(); // source image height
int[] srcBuf = new int[srcW * srcH]; // source image pixel
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// compute interpolation table
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for ( int i = 0 ; i <= distance ; i++) { /* vertical direction */
tabY[db] = sb;
tems += srcH;
temd += desH;
if ( tems > distance) {
tems -= distance;
sb++;
}
if ( temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for ( int i = 0 ; i <= distance ; i++) { /* horizontal direction */
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if ( tems > distance) {
tems -= distance;
sb++;
}
if ( temd > distance) {
temd -= distance;
db++;
}
}
// set transparence
// if(isTrans){
// int a= 100;//set the transparence of pixel 100
// for(int i=0;i<srcBuf.length;i++){
// if(srcBuf[i]==0x00FFFFFF)continue;
// srcBuf[i]=(a<<24) | (srcBuf[i] & 0x00FFFFFF);// modify the highest 2 value
// }
// }
// formation enlarge and shorten buffer pixel
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for ( int i = 0 ; i < desH ; i++) {
if ( oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
}
else {
dx = 0;
for ( int j = 0 ; j < desW ; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
if(isTrans){
// int a= 100;//set the transparence of pixel 100
for(int i=0;i<desBuf.length;i++){
if(desBuf[i]==0x00FFFFFF)continue;
int alpha = ((desBuf[i] & 0xff000000) >>> 24)==0?0:100;
desBuf[i]=((alpha+1)<<24) | (desBuf[i] & 0x00FFFFFF);// modify the highest 2 value
}
}
if(isBackgroundTrans){
desImg = Image.createRGBImage(desBuf, desW, desH, true);
}else{
desImg = Image.createRGBImage(desBuf, desW, desH, false);
}
return desImg;
}
}
public static final Image zoomImage(Image src){
return zoomImage(src,src.getWidth(),src.getHeight(),true,false);
}
public static final Image zoomImage(Image src, int desW, int desH, boolean isBackgroundTrans, boolean isTrans) {
Image desImg = null;
int srcW = src.getWidth(); // source image width
int srcH = src.getHeight(); // source image height
int[] srcBuf = new int[srcW * srcH]; // source image pixel
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// compute interpolation table
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for ( int i = 0 ; i <= distance ; i++) { /* vertical direction */
tabY[db] = sb;
tems += srcH;
temd += desH;
if ( tems > distance) {
tems -= distance;
sb++;
}
if ( temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for ( int i = 0 ; i <= distance ; i++) { /* horizontal direction */
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if ( tems > distance) {
tems -= distance;
sb++;
}
if ( temd > distance) {
temd -= distance;
db++;
}
}
// set transparence
// if(isTrans){
// int a= 100;//set the transparence of pixel 100
// for(int i=0;i<srcBuf.length;i++){
// if(srcBuf[i]==0x00FFFFFF)continue;
// srcBuf[i]=(a<<24) | (srcBuf[i] & 0x00FFFFFF);// modify the highest 2 value
// }
// }
// formation enlarge and shorten buffer pixel
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for ( int i = 0 ; i < desH ; i++) {
if ( oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
}
else {
dx = 0;
for ( int j = 0 ; j < desW ; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
if(isTrans){
// int a= 100;//set the transparence of pixel 100
for(int i=0;i<desBuf.length;i++){
if(desBuf[i]==0x00FFFFFF)continue;
int alpha = ((desBuf[i] & 0xff000000) >>> 24)==0?0:100;
desBuf[i]=((alpha+1)<<24) | (desBuf[i] & 0x00FFFFFF);// modify the highest 2 value
}
}
if(isBackgroundTrans){
desImg = Image.createRGBImage(desBuf, desW, desH, true);
}else{
desImg = Image.createRGBImage(desBuf, desW, desH, false);
}
return desImg;
}
}