⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geotransform.java

📁 图象编辑器
💻 JAVA
字号:
package imageapp;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class GeoTransform {
	/** 双线性插值*/
	protected static float biLinear(int d00, int d01, int d10, int d11, 
							 float p, float q) {
		return (1-q)*( (1-p)*d00+p*d01 ) + q*( (1-p)*d10+p*d11 );
	}
	
	public static BufferedImage horMirror(BufferedImage srcImage) {
		int width = srcImage.getWidth();
		int height = srcImage.getHeight();
		int srcRGBs[] = srcImage.getRGB(0, 0, width, height, null, 0, width);
		BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		for(int j=0; j<height; j++) {
			destImage.setRGB(0, height-j-1, width, 1, srcRGBs, j*width, width);
		}	
		return destImage;
	}
	
	public static BufferedImage verMirror(BufferedImage srcImage) {
		int width = srcImage.getWidth();
		int height = srcImage.getHeight();
		int srcRGBs[] = srcImage.getRGB(0, 0, width, height, null, 0, width);
		BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				destImage.setRGB(i, j, srcRGBs[j*width+width-i-1]);
			}
		}	
		return destImage;
	}
	
	public static BufferedImage scale(BufferedImage srcImage, float sx, float sy) {
		int srcWidth = srcImage.getWidth();
		int srcHeight = srcImage.getHeight();
		int srcRGBs[] = srcImage.getRGB(0, 0, srcWidth, srcHeight, null, 0, srcWidth);
		int destWidth = Math.round(srcWidth*sx);
		int destHeight = Math.round(srcHeight*sy);
		BufferedImage destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
		
		int m, m1, n, n1;
		float x, y, p, q;
		int r, g, b;
		int p00[] = new int[3];
		int p01[] = new int[3];
		int p10[] = new int[3];
		int p11[] = new int[3];
		int tmp[] = new int[3];
		int rgb[] = new int[3];
	
		for(int j=0; j<destHeight; j++) {
			y = j/sy;	m = (int)y;		q = y-m;
			m1 = m+1 >= srcHeight ? m : m+1;
			for(int i=0; i<destWidth; i++) {
				x = i/sx;	n = (int)x;		p = x-n;
				n1 = n+1 >= srcWidth ? n : n+1;
				
				ImageUtil.decodeColor(srcRGBs[m*srcWidth+n], p00);
				ImageUtil.decodeColor(srcRGBs[m*srcWidth+n1], p01);
				ImageUtil.decodeColor(srcRGBs[m1*srcWidth+n], p10);
				ImageUtil.decodeColor(srcRGBs[m1*srcWidth+n1], p11);
				
				for(int k=0; k<3; k++) {
					rgb[k] = Math.round(biLinear(p00[k], p01[k], p10[k], p11[k], p, q));
				}
				destImage.setRGB(i, j, ImageUtil.encodeColor(rgb));
			}
		}
	
		return destImage;
	}
	
	public static BufferedImage rotate(BufferedImage srcImage, float af, boolean isResize) {
		int i, j;
		int m, m1, n, n1;
		float x, y, p, q;
		int r, g, b;
		int p00[] = new int[3];
		int p01[] = new int[3];
		int p10[] = new int[3];
		int p11[] = new int[3];
		int tmp[] = new int[3];
		int rgb[] = new int[3];
		
		int srcWidth = srcImage.getWidth();
		int srcHeight = srcImage.getHeight();
		int srcRGBs[] = srcImage.getRGB(0, 0, srcWidth, srcHeight, null, 0, srcWidth);
	
		float sinAF = (float)Math.sin(af);
		float cosAF = (float)Math.cos(af);
		
		// (1)求旋转后图像的尺寸
		float x0, y0;
		float ptX[] = new float[3], ptY[] = new float[3];//存放角点坐标(0,1) (1,0) (1,1)
		float minX, maxX, minY, maxY;
		if(isResize) {
			minX = 0; 		maxX = 0; 
			minY = 0; 		maxY = 0;
			ptX[0] = srcWidth-1;	ptY[0] = 0;
			ptX[1] = 0;				ptY[1] = srcHeight-1;
			ptX[2] = srcWidth-1;	ptY[2] = srcHeight-1;
			for(i=0; i<3; i++) {
				x = ptY[i]*sinAF+ptX[i]*cosAF;
				if(x < minX) minX = x;
				if(x > maxX) maxX = x;
				y = ptY[i]*cosAF-ptX[i]*sinAF;
				if(y < minY) minY = y;
				if(y > maxY) maxY = y;
			}	
			x0 = minX;		y0 = minY;
		}
		else {
			minX = 0; 		maxX = srcWidth-1; 
			minY = 0; 		maxY = srcHeight-1;
			ptX[0] = srcWidth/2;		ptY[0] = srcHeight/2;
			x = ptY[0]*sinAF+ptX[0]*cosAF;
			y = ptY[0]*cosAF-ptX[0]*sinAF;	
			x0 = x-srcWidth/2;
			y0 = y-srcHeight/2;
		}
		int destWidth = Math.round(maxX-minX+1);
		int destHeight = Math.round(maxY-minY+1);
		BufferedImage destImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
		
		// (2)旋转图像
		for(j=0; j<destHeight; j++) {
			for(i=0; i<destWidth; i++) {
				y = (i+x0)*sinAF + (j+y0)*cosAF;
				x = (i+x0)*cosAF - (j+y0)*sinAF; 
				m = (int)y;		n = (int)x;
				p = x-n;		q = y-m;		
	
				if(m >= 0 && m < srcHeight && n >= 0 && n < srcWidth) {
					m1 = m+1 >= srcHeight ? m : m+1;
					n1 = n+1 >= srcWidth ? n : n+1;
					ImageUtil.decodeColor(srcRGBs[m*srcWidth+n], p00);
					ImageUtil.decodeColor(srcRGBs[m*srcWidth+n1], p01);
					ImageUtil.decodeColor(srcRGBs[m1*srcWidth+n], p10);
					ImageUtil.decodeColor(srcRGBs[m1*srcWidth+n1], p11);
	
					for(int k=0; k<3; k++) {
						rgb[k] = Math.round(biLinear(p00[k], p01[k], p10[k], p11[k], p, q));
					}
					
				}
				else { for(int k=0; k<3; k++) rgb[k] = 255; }
				
				destImage.setRGB(i, j, ImageUtil.encodeColor(rgb));
			}
		}
		
		return destImage;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -