-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagechangeclu.java
More file actions
executable file
·180 lines (147 loc) · 5.23 KB
/
Copy pathimagechangeclu.java
File metadata and controls
executable file
·180 lines (147 loc) · 5.23 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* The Cluster Image Processing program
* This program performs the image transformations like negative,
* sepia, flip, black and white image
*
* The input image is sliced among the processors and then reduced after
* the manipulation is done
*
* @author Divin Visariya
* @author Sreeprasad Govindankutty
* @author Varun Goyal
*/
import java.awt.Image;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import edu.rit.mp.IntegerBuf;
import edu.rit.pj.Comm;
import edu.rit.pj.reduction.IntegerOp;
import edu.rit.util.Range;
public class imagechangeclu
{
static long time1,time2;
//Ranges for cluster processors
static Comm world;
static int size;
static int rank;
static Range[] ranges;
static Range myrange;
static int mylb;
static int myub;
public static void main(String args[]) throws Exception
{
Comm.init(args);
world = Comm.world();
size = world.size();
rank = world.rank();
Image image = null;
int height = 0, width = 0;
try
{
// Read from a file
File file = new File(args[0]);
image = ImageIO.read(file);
height = image.getHeight(null);
width = image.getWidth(null);
}
catch (IOException e)
{
}
//Upper and lower bound calculations for processors
ranges = new Range (0, height-1).subranges (size);
myrange = ranges[rank];
mylb = myrange.lb();
myub = myrange.ub();
Image img=image;
int x=0,y=0;
final int w=width;
final int h=height;
final int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try
{
pg.grabPixels();
}
catch (InterruptedException e)
{
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0)
{
System.err.println("image fetch aborted or errored");
return;
}
time1=System.currentTimeMillis();
final int[] negpixels = new int[w * h];
final int [] blackpixels = new int[w * h];
final int [] flippixels = new int[w * h];
final int [] sepiapixels = new int[w * h];
// loop local variable declaration
for (int j = mylb; j <= myub; j++)
{
for (int i = 0; i < w; i++)
{
flippixels[j * w + i] = pixels[j * w + (w - i - 1)];
int pixel = pixels[j * w + i];
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
int blackwhite = (int) (red*0.3 + green*0.59+ blue*0.11);//red + green + blue;
blackpixels[j * w + i] = (blackwhite << 16) | (blackwhite << 8) | (blackwhite);
negpixels[j * w + i] = ((255-red) << 16) | ((255-green) << 8) | (255-blue);
int sepia = 20;
int gray = (red + green + blue) / 3;
red = green = blue = gray;
red = red + (sepia * 2);
green = green + sepia;
if (red > 255) red=255;
if (green > 255) green=255;
if (blue > 255) blue=255;
int sepiaIntensity = 20;
// Darken blue color to increase sepia effect
blue-= sepiaIntensity;
// normalize if out of bounds
if (blue < 0) blue=0;
if (blue > 255) blue=255;
sepiapixels[j * w + i] = ((red) << 16) | ((green) << 8) | (blue);
}
}
//Buffer for the image to send
IntegerBuf buf = IntegerBuf.buffer(negpixels);
IntegerBuf buf1 = IntegerBuf.buffer(flippixels);
IntegerBuf buf2 = IntegerBuf.buffer(blackpixels);
IntegerBuf buf3 = IntegerBuf.buffer(sepiapixels);
//Reduce the results to the final matrix for image
world.reduce(0,buf,IntegerOp.SUM);
world.reduce(0,buf1,IntegerOp.SUM);
world.reduce(0,buf2,IntegerOp.SUM);
world.reduce(0,buf3,IntegerOp.SUM);
if(rank == 0)
{
BufferedImage pixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
pixelImage.setRGB(0, 0, w, h, blackpixels, 0, w);
File outputfile = new File("blackwhite.png");
ImageIO.write(pixelImage, "png", outputfile);
BufferedImage negpixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
negpixelImage.setRGB(0, 0, w, h, negpixels, 0, w);
// To write the Canny Edges to the image buffer...
File outputfile1 = new File("negative.png");
ImageIO.write(negpixelImage, "png", outputfile1);
BufferedImage flippixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
flippixelImage.setRGB(0, 0, w, h, flippixels, 0, w);
// retrieve image
File outputfile11 = new File("flip.png");
ImageIO.write(flippixelImage, "png", outputfile11);
BufferedImage sepiapixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
sepiapixelImage.setRGB(0, 0, w, h, sepiapixels, 0, w);
// retrieve image
File outputfile111 = new File("sepia.png");
ImageIO.write(sepiapixelImage, "png", outputfile111);
time2=System.currentTimeMillis();
System.out.println((time2-time1)+" msec");
}
}
}