-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageProcessing.java
99 lines (95 loc) · 2.3 KB
/
ImageProcessing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class ImageProcessing{
private int w,h;
private BufferedImage read,write;
/* Color */
public int a(int c){
return c>>>24;
}
public int r(int c){
return c>>16&0xff;
}
public int g(int c){
return c>>8&0xff;
}
public int b(int c){
return c&0xff;
}
public int gray(int c){
return (int)(0.2989*r(c))+(int)(0.5870*g(c))+(int)(0.1140*b(c));
}
public int rgb(int r,int g,int b){
if(r<0) r=0;
else if(r>255) r=255;
if(g<0) g=0;
else if(g>255) g=255;
if(b<0) b=0;
else if(b>255) b=255;
return 0xff000000 | r <<16 | g <<8 | b;
}
public int argb(int a,int r,int g,int b){
return a<<24 | r <<16 | g <<8 | b;
}
/* Image */
private void setWidth(int w){
this.w=w;
}
private void setHeight(int h){
this.h=h;
}
public int getWidth(){
return w;
}
public int getHeight(){
return h;
}
private void setRead(BufferedImage read){
this.read=read;
}
private void setWrite(BufferedImage write){
this.write=write;
}
public BufferedImage getRead(){
return read;
}
public BufferedImage getWrite(){
return write;
}
protected void setRGB(int x,int y,int rgb){
write.setRGB(x,y,rgb);
}
public int getRGB(int x,int y){
return read.getRGB(x,y);
}
public BufferedImage copyImage(BufferedImage source){
BufferedImage copy=new BufferedImage(source.getWidth(),
source.getHeight(),
source.getType());
copy.setData(source.getData());
return copy;
}
/* File */
public void readImage(String name){
try{
File before=new File(name);
setRead(ImageIO.read(before));
setWidth(read.getWidth()); setHeight(read.getHeight());
setWrite(copyImage(read));
} catch(IOException e){
System.out.println(e);
}
}
public void writeImage(String name){
try{
File after=new File(name);
ImageIO.write(write,extension(name),after);
} catch(IOException e){
System.out.println(e);
}
}
public String extension(String filename){
return filename.substring(filename.indexOf('.')+1,filename.length());
}
}