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

📄 imageutil.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 15-May-2005 at 21:28:30.
 * 
 * Copyright (c) 2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
// package de.enough.polish.util;





/**
 * <p>Helps to transform images</p>
 *
 * <p>Copyright (c) 2005, 2006 Enough Software</p>
 * <pre>
 * history
 *        15-May-2005 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public final class ImageUtil {

	/**
	 * No instantiation is allowed
	 */
	private ImageUtil() {
		super();
	}
	
	/**
	 * Scales the rgb data and stores it into the given scaledRgbData array.
	 * 
	 * @param scaleFactor the factor by which the rgb data should be magnified in percent, e.g. 130
	 * @param width the width of the rgbData and the scaledRgbData (scanline width)
	 * @param height the height of the rgbData and the scaledRgbData (scanline width)
	 * @param rgbData the source rgbData
	 * @param scaledRgbData the target rgbData array, must have the same dimensions like the given rgbData
	 */
	public static void scale(int scaleFactor, int width, int height, int[] rgbData, int[] scaledRgbData) {
		int yStart = ((height - height * 100 / scaleFactor ) / 2) * width;
		int xStart = (width - width * 100 / scaleFactor ) / 2;
		for (int y = 0; y < height; y++) {
			int c1 = y * width;
			int c2 = yStart + (y * 100  / scaleFactor) * width;
			for (int x = 0; x < width; x++) {
				scaledRgbData[c1 + x] = rgbData[ c2 + xStart + x * 100/scaleFactor ];
			}
		}
	}

	
	/**
	 * Scales the rgb data and stores it into the given scaledRgbData array and adds an alpha semi transparency value at the same time.
	 * 
	 * @param opacity the alpha value, 255 (0xFF) means fully opaque, 0 fully transparent
	 * @param scaleFactor the factor by which the rgb data should be magnified in percent, e.g. 130
	 * @param width the width of the rgbData and the scaledRgbData (scanline width)
	 * @param height the height of the rgbData and the scaledRgbData (scanline width)
	 * @param rgbData the source rgbData
	 * @param scaledRgbData the target rgbData array, must have the same dimensions like the given rgbData
	 */
	public static void scale( int opacity, int scaleFactor, int width, int height, int[] rgbData, int[] scaledRgbData) {
		opacity = (opacity << 24) | 0xFFFFFF;
		
		int yStart = ((height - height * 100 / scaleFactor ) / 2) * width;
		int xStart = (width - width * 100 / scaleFactor ) / 2;
		for (int y = 0; y < height; y++) {
			int c1 = y * width;
			int c2 = yStart + (y * 100  / scaleFactor) * width;
			for (int x = 0; x < width; x++) {
				scaledRgbData[c1 + x] = rgbData[ c2 + xStart + x * 100/scaleFactor ] & opacity;
			}
		}
	}

	
	/**
	 * Scales the rgb data and stores it into the given scaledRgbData array and adds an alpha semi transparency value at the same time.
	 * 
	 * @param scaledWidth the width of the scaled rgbData
	 * @param scaledHeight the height of the scaled rgbData
	 * @param scanlength the length of the given RGB data, usual the same as sourceWidth
	 * @param sourceWidth the width of the rgbData
	 * @param sourceHeight the height of the rgbData
	 * @param rgbData the source rgbData
	 * @return a new rgbData array that contains the scaled version
	 */
	public static int[] scale(int scaledWidth, int scaledHeight, int scanlength, int sourceWidth, int sourceHeight, int[] rgbData) {
		int scaledRgbData[] = new int[scaledWidth * scaledHeight];
		for (int y = 0; y < scaledHeight; y++) {
			int c1 = y * scaledWidth;
			int c2 = (y * sourceHeight / scaledHeight) * scanlength;
			for (int x = 0; x < scaledWidth; x++) {
				scaledRgbData[c1 + x] = rgbData[c2 + x * sourceWidth / scaledWidth];
			}
		}
		return scaledRgbData;
	}

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the given RGB data image and uses the center as the reference point for the rotation.
	 //# * 
	 //# * @param image the RGB data image that is going to be rotated. Warning: the RGB data might get replaced
	 //# * @param angle the angle for the rotation in degrees (-360..0..360)
	 //# */
	//# public static void rotate( RgbImage image, int angle ) {
		//# rotate( image, angle, image.getWidth()/2, image.getHeight()/2 );
	//# }
	//#endif

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the given RGB data image.
	 //# * 
	 //# * @param image the RGB data image that is going to be rotated. Warning: the RGB data might get replaced
	 //# * @param angle the angle for the rotation in degrees (-360..0..360)
	 //# * @param referenceX the horizontal reference point for the rotation
	 //# * @param referenceY the vertical reference point for the rotation 
	 //# */
	//# public static void rotate(RgbImage image, int angle, int referenceX, int referenceY ) {
		//# int[] rgbData = image.getRgbData();
		//# int width = image.getWidth();
		//# int height = image.getHeight();
		//# double degreeCos = Math.cos(Math.PI*angle/180);
		//# double degreeSin = Math.sin(Math.PI*angle/180);
		//# int rotatedWidth = getRotatedWidth(angle, width, height, degreeCos, degreeSin);
		//# int rotatedHeight = getRotatedHeight(angle, width, height, degreeCos, degreeSin);
		//# int[] rotatedRgbData = new int[ rotatedWidth * rotatedHeight ];
		//# ImageUtil.rotate(rgbData, width, height, 
				//# referenceX, referenceY, 0x00FFFFFF,
				//# degreeCos, degreeSin, rotatedRgbData, rotatedWidth, rotatedHeight );
		//# image.setRgbData(rotatedRgbData, rotatedWidth);
		//# image.setWidth(rotatedWidth);
		//# image.setHeight(rotatedHeight);
	//# }
	//#endif

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the given rgb data and returns the rotated rgb data array.
	 //# * 
	 //# * @param argbArray the rgb data to be rotated.
	 //# * @param width the width of the rgb data.
	 //# * @param height the heigth of the rgb data.
	 //# * @param degree the degree value of the rotation.
	 //# * @param backgroundColor the ARGB color used for the background
	 //# * @return the rotated rgb data.
	 //# */
	//# public static final int[] rotate(int[]argbArray, int width, int height, int degree, int backgroundColor) {
		//# return rotate(argbArray, width, height, degree, width/2, height/2, backgroundColor);
	//# }
	//#endif

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the given rgb data and returns the rotated rgb data array.
	 //# * 
	 //# * @param sourceRgbData the rgb data to be rotated.
	 //# * @param width the width of the rgb data.
	 //# * @param height the heigth of the rgb data.
	 //# * @param degree the degree value of the rotation.
	 //# * @param backgroundColor the ARGB color used for the background
	 //# * @return the rotated rgb data.
	 //# */
	//# public static final int[] rotate(int[] sourceRgbData, int width, int height, int targetRgbData, int degree, double degreeCos,double degreeSin, int backgroundColor) {
		//# return rotate(sourceRgbData, width, height, degree, width/2, height/2, backgroundColor);
	//# }
	//#endif

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the given rgb data and returns the rotated rgb data array.
	 //# * 
	 //# * @param sourceRgbData the rgb data to be rotated.
	 //# * @param width the width of the source rgb data.
	 //# * @param height the heigth of the source rgb data.
	 //# * @param degree the angle of the rotation.
	 //# * @param referenceX the x position for the center of rotation.
	 //# * @param referenceY the y position for the center of rotation.
	 //# * @param backgroundColor the ARGB color used for the background
	 //# * @return the rotated rgb data.
	 //# * @author Tim Muders
	 //# */
	//# public static final int[] rotate(int[] sourceRgbData,int width, int height,int degree,int referenceX,int referenceY,int backgroundColor){
		//# double degreeCos = Math.cos(Math.PI*degree/180);
		//# double degreeSin = Math.sin(Math.PI*degree/180);		
		//# int rotatedWidth = getRotatedWidth(degree,width,height,degreeCos,degreeSin);
		//# int rotatedHeight = getRotatedHeight(degree,width,height,degreeCos,degreeSin);
		//# int[] rotatedRGB = new int [rotatedHeight * rotatedWidth];
		//# rotate(sourceRgbData, width, height, referenceX, referenceY, backgroundColor, degreeCos, degreeSin, rotatedRGB, rotatedWidth, rotatedHeight);
		//# return rotatedRGB;
	//# }
	//#endif

	//#if polish.hasFloatingPoint
	//# /**
	 //# * Rotates the source RGB data and stores it within the target RGB data array.
	 //# * 
	 //# * @param sourceRgbData the rgb data to be rotated.
	 //# * @param width the width of the source rgb data.
	 //# * @param height the heigth of the source rgb data.
	 //# * @param referenceX the x position for the center of rotation.
	 //# * @param referenceY the y position for the center of rotation.
	 //# * @param backgroundColor the ARGB color used for the background
	 //# * @param degreeCos the cosine of the degree value: Math.cos(Math.PI*degree/180)
	 //# * @param degreeSin the sine of the degree value: Math.sin(Math.PI*degree/180)
	 //# * @param rotatedRGB the RGB data array for storing the rotated pixel data
	 //# * @param rotatedWidth the width of the rotated rgb data
	 //# * @param rotatedHeight the height of the rotated rgb data
	 //# */
	//# public static void rotate(int[] sourceRgbData, int width, int height, int referenceX, int referenceY, int backgroundColor, double degreeCos, double degreeSin, int[] rotatedRGB, int rotatedWidth, int rotatedHeight) {
		//# int halfOfWidth = width/2;
		//# int halfOfHeigth = height/2;
		//# int refX,refY,newX,newY,sumXY;
		//# for(int x = 0; x < rotatedWidth; x++){
			//# for(int y = 0; y < rotatedHeight; y++){	
				//# refX = x - referenceX;
				//# refY = y - referenceY;
				//# newX = (int)(refX  * degreeCos + refY * degreeSin);
				//# newY = (int)(refY * degreeCos - refX  * degreeSin);
				//# newX += halfOfWidth;
				//# newY += halfOfHeigth;
				//# if( newX >= 0 && newX < width && newY >= 0 && newY < height ){
					//# sumXY  = newX + newY * width;
					//# rotatedRGB [x+y*rotatedWidth] = sourceRgbData[sumXY];
				//# } else {
					//# rotatedRGB [x+y*rotatedWidth] = backgroundColor;
				//# }
			//# }
		//# }
	//# }
	//#endif
	
	//#if polish.hasFloatingPoint
	//# /**
	 //# * Returns the new height for the given degree. The new heigth symbols the heigth for an rotated rgb data array with the same degree rotation.
	 //# * 
	 //# * @param degree the degree value of the rotation.
	 //# * @param width the width of the rgb source.
	 //# * @param heigth the heigth of the rgb source
	 //# * @param degreeCos the cosine of the degree value: Math.cos(Math.PI*degree/180)
	 //# * @param degreeSin the sine of the degree value: Math.sin(Math.PI*degree/180)
	 //# * @return the new height of the rgb data.
	 //# * @author Tim Muders
	 //# */
	//# public static final int getRotatedHeight(int degree, int width, int heigth, double degreeCos, double degreeSin)
	//# {
		//# if(degree == -90 || degree == 90 || degree == 270 || degree == -270){
			//# return width;
		//# }
		//# else if(degree == 360 || degree == 180 || degree == 0){
			//# return heigth;
		//# }
		//# long pointY1 = MathUtil.round(0 * degreeSin + 0 * degreeCos);
		//# long pointY2 = MathUtil.round(width *degreeSin + 0 *degreeCos);
		//# long pointY3 = MathUtil.round(0 *degreeSin + heigth *degreeCos);
		//# long pointY4 = MathUtil.round(width *degreeSin + heigth *degreeCos);
		//# long minY = pointY1;
		//# if(pointY2 < minY){
			//# minY = pointY2;
		//# }
		//# if(pointY3 < minY){
			//# minY = pointY3;
		//# }
		//# if(pointY4 < minY){
			//# minY = pointY4;
		//# }
		//# long maxY = pointY1;
		//# if(pointY2 > maxY){
			//# maxY = pointY2;
		//# }
		//# if(pointY3 > maxY){
			//# maxY = pointY3;
		//# }

⌨️ 快捷键说明

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