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

📄 colorconvert.java

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

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

class ColorConvert {
	public static BufferedImage grayScale(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);
		
		int rgb[] = new int[3];
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				rgb[0] = ImageUtil.getBrightness(srcRGBs[j*width+i]);
				rgb[1] = rgb[2] = rgb[0];
				destImage.setRGB(i, j, ImageUtil.encodeColor(rgb));
			}
		}	
		return destImage;
	}
	
	public static BufferedImage changeYHS(BufferedImage srcImage, 
										  float deltY, float deltH, float nS) {
		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);
		
		float yhs[] = new float[3];
		int rgb[] = new int[3];
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				ImageUtil.convertRGBToYHS(srcRGBs[j*width+i], yhs);
				yhs[0] += deltY;
				yhs[1] += deltH;
				yhs[2] *= nS;
				destImage.setRGB(i, j, ImageUtil.convertYHSToRGB(yhs));
			}
		}	
		return destImage;
	}
	
	public static BufferedImage changeContrast(BufferedImage srcImage, int deltY) {
		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);
		
		float yhs[] = new float[3];
		int rgb[] = new int[3];
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				ImageUtil.convertRGBToYHS(srcRGBs[j*width+i], yhs);
				yhs[0] = (255.0f+2*deltY)*(yhs[0])/(255.0f)-deltY;
				destImage.setRGB(i, j, ImageUtil.convertYHSToRGB(yhs));
			}
		}	
		return destImage;
	}
	
	public static int[] getHistInfo(BufferedImage srcImage, int histArray[]) {
		if(histArray == null) histArray = new int[256];
		for(int i=0; i<256; i++) histArray[i] = 0;
		
		int width = srcImage.getWidth();
		int height = srcImage.getHeight();
		int srcRGBs[] = srcImage.getRGB(0, 0, width, height, null, 0, width);
		
		float yhs[] = new float[3];
		int rgb[] = new int[3];
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				ImageUtil.convertRGBToYHS(srcRGBs[j*width+i], yhs);
				int hist = Math.round(yhs[0]);
				hist = hist < 0 ? 0 :hist;
				hist = hist > 255 ? 255 : hist;
				histArray[hist]++;
			}
		}		
		return histArray;
	}
	
	public static BufferedImage histPlane(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);
		
		class HistPlaneInnerStruct {	//灰度均衡内部结构
			int x;
			int y;
			float h;		//色相
			float s;		//饱和度
			
			public HistPlaneInnerStruct(int x, int y, float h, float s) {
				this.x = x;		this.y = y;
				this.h = h;		this.s = s;
			}
		}
		float yhs[] = new float[3];
		int rgb[] = new int[3];
		LinkedList histIndexs[] = new LinkedList[256]; 
		for(int i=0; i<256; i++) histIndexs[i] = new LinkedList();
		for(int i=0; i<width; i++) {
			for(int j=0; j<height; j++) {
				ImageUtil.convertRGBToYHS(srcRGBs[j*width+i], yhs);
				int hist = Math.round(yhs[0]);
				hist = hist < 0 ? 0 : hist;
				hist = hist > 255 ? 255 : hist;
				histIndexs[hist].addLast(new HistPlaneInnerStruct(i, j, yhs[1], yhs[2]));
			}
		}	
		
		int avCount = width*height/256;
		int index = 255;
		ListIterator it = histIndexs[index].listIterator();
		for(int i=255; i>=0; i--) {
			for(int j=avCount; j>0; j--) {
				while(!it.hasNext()) {
					index--;
					it = histIndexs[index].listIterator();
				}
				HistPlaneInnerStruct hpi = (HistPlaneInnerStruct)it.next();
				yhs[0] = i;
				yhs[1] = hpi.h;
				yhs[2] = hpi.s;
				destImage.setRGB(hpi.x, hpi.y, ImageUtil.convertYHSToRGB(yhs));
			}
		}
		
		return destImage;	
	}
}

⌨️ 快捷键说明

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