-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTester.java
More file actions
573 lines (427 loc) · 18.5 KB
/
ImageTester.java
File metadata and controls
573 lines (427 loc) · 18.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
*ImageEditor Project starter file
*This file is intended to be used for investigation
*Please make a new file for your ImageEditor Project
*/
// https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.lang.Math.*;
import java.util.*;
public class ImageTester
{
/**
* A method that turns the image red
* @param image and newTitle
*/
public static void makeRed(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int red = 0x00ff0000;
int og = image.getRGB(c,r);
red = red & og;
newImage.setRGB(c,r, red);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that turn the image blue
* @param image and newTitle
*/
public static void makeBlue(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int blue = 0x000000ff;
int og = image.getRGB(c,r);
blue = blue & og;
newImage.setRGB(c,r, blue);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that turns image green
* @param image and newTitle
*/
public static void makeGreen(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int green = 0x0000ff00;
int og = image.getRGB(c,r);
green = green & og;
newImage.setRGB(c,r, green);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that inverts the image
* @param image and newTitle
*/
public static void Invert(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int og = image.getRGB(c,r);
newImage.setRGB(c,r, ~og); //tilda = invert
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that removes the red pixels
* @param image and newTitle
*/
public static void deleteRed(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int red = 0x0000ffff;
int og = image.getRGB(c,r);
red = red & og;
newImage.setRGB(c,r, red);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that takes away green pixels
* @param image and newTitle
*/
public static void deleteGreen(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int green = 0x00ff00ff;
int og = image.getRGB(c,r);
green = green & og;
newImage.setRGB(c,r, green);
}
}
// writes a new file with the new pixel details with the given parameter of the newTitle
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that makes takes away blue pixels
* @param image and newTitle
*/
public static void deleteBlue(BufferedImage image, String newTitle) throws IOException
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int blue = 0x00ffff00;
int og = image.getRGB(c,r);
blue = blue & og;
newImage.setRGB(c,r, blue);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that makes the image gray
* @param image, newTitle
*/
public static void makeGray(BufferedImage image, String newTitle) throws IOException
{
// makes a new image of same size
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
// finds average of the color value and replaces the pixels
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int og = image.getRGB(c,r);
int redPix = og & 0x00ff0000;
redPix = redPix >> 16;
int greenPix = og & 0x0000ff00;
greenPix = greenPix >> 8;
int bluePix = og & 0x000000ff;
int avg = (redPix + greenPix + bluePix) / 3;
redPix = avg;
redPix = redPix << 8;
greenPix = avg;
greenPix = greenPix << 16;
bluePix = avg;
int newPix = 0x00ffffff;
newPix = redPix + greenPix + bluePix;
newImage.setRGB(c,r, newPix);
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that creates four images but alters each one creating an andy warhol style image
* @param image and newTitle
*/
public static void makeWarhol(BufferedImage image, String newTitle) throws IOException
{
// makes a new image of two times the original image's height and width
int height = ( (image.getHeight()) * 2);
int width = ( (image.getWidth()) * 2);
BufferedImage warhol = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// four for loops iterate through the OG image and then change the pictures
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int og = image.getRGB(c,r);
warhol.setRGB(c,r, og);
}
}
//tells where to start putting pixels
int newHeight = image.getHeight();
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int col = 0x00f00f00;
int og = image.getRGB(c,r);
col = col & og;
warhol.setRGB( c, newHeight, col);
}
newHeight++;
}
//reset after each for loop
int newWidth = image.getWidth();
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int col = 0x0f0000ff;
int og = image.getRGB(c,r);
col = col & og;
warhol.setRGB( newWidth, r, col);
newWidth++;
}
newWidth = image.getWidth();
}
newHeight = image.getHeight();
newWidth = image.getWidth();
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int col = 0x0f000ff;
int og = image.getRGB(c,r);
col = col & og;
warhol.setRGB( newWidth, newHeight, col);
newWidth++;
}
newHeight++;
newWidth = image.getWidth();
}
ImageIO.write(warhol, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that mirrors the image's left and right sides with the original image in the middle
* @param image and newTitle
*/
public static void makeMirrored(BufferedImage image, String newTitle) throws IOException
{
// makes a new image
int ogHeight = image.getHeight();
int ogWidth = ( (image.getWidth()) * 3);
int secWidthFirst = image.getWidth();
int thirdWidthFirst = image.getWidth() * 2;
BufferedImage mirrored = new BufferedImage(ogWidth, ogHeight, BufferedImage.TYPE_INT_RGB);
// goes through left half from left to right
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c <= image.getWidth() / 2; c++)
{
int og = image.getRGB(c,r);
mirrored.setRGB(c,r, og);
mirrored.setRGB( ( image.getWidth() - c),r, og);
}
}
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int og = image.getRGB(c,r);
mirrored.setRGB(secWidthFirst,r, og);
secWidthFirst++;
}
secWidthFirst = image.getWidth();
}
// iterates through the original image and places same pixels on right and left sides
int count = 0;
int totalWidth = mirrored.getWidth() - 1;
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = image.getWidth() - 1; c >= image.getWidth() / 2; c--)
{
int og = image.getRGB(c,r);
mirrored.setRGB( (totalWidth - (image.getWidth() - c) ),r, og);
mirrored.setRGB( thirdWidthFirst,r, og);
thirdWidthFirst++;
count++;
}
count = 0;
thirdWidthFirst = image.getWidth() * 2;
}
ImageIO.write(mirrored, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that rotates the original image clockwise
* @param image and newTitle
*/
public static void rotateClockWise(BufferedImage image, String newTitle) throws IOException
{
// new image w opposite dimensions
BufferedImage clockwise = new BufferedImage(image.getHeight(), image.getWidth(), BufferedImage.TYPE_INT_RGB); //they all have to be named newImage to be stores properly for some reason. I tried changing the names but got errors.
// switches rows and columns
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
int og = image.getRGB(c,r);
clockwise.setRGB( image.getHeight() - r - 1, c, og);
}
}
ImageIO.write(clockwise, "jpg",new File(newTitle + ".jpg"));
}
/*
*@param image and newTitle
* the purpose of this method is to create line art of the original image by finding each edge and shifting the pixels.
*/
public static void findEdges(BufferedImage image, String newTitle) throws IOException //best lineart ever for real
{
//same size as the og image
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int r = 0; r < image.getHeight(); r++)
{
for (int c = 0; c < image.getWidth(); c++)
{
if (c < image.getWidth() - 1)
{
int thisPix = image.getRGB(c, r);
int rightPix = image.getRGB(c + 1, r);
int redDiff = ( (rightPix >> 16) - (thisPix >> 16) ) * ( (rightPix >> 16) - (thisPix >> 16) );
//System.out.print(redDiff);
int greenDiff = ( (rightPix >> 8) - (thisPix >> 8) ) * ( (rightPix >> 8) - (thisPix >> 8) );
//System.out.print(greenDiff);
int blueDiff = (( rightPix) - (thisPix)) * ( (rightPix) - (thisPix) );
//System.out.print(blueDiff);
double diff = Math.sqrt( Math.abs(redDiff + greenDiff + blueDiff) );
//System.out.print(doubleDiff);
if (diff < 40000)
{
newImage.setRGB(c, r, 0x00000000); //white
newImage.setRGB(c + 1, r, 0x00ffffff); //black
}
else
{
newImage.setRGB(c, r, 0x00ffffff);
newImage.setRGB(c + 1, r, 0x00000000);
}
}
}
}
ImageIO.write(newImage, "jpg",new File(newTitle + ".jpg"));
}
/**
* A method that contains every method in the program. the userinterface is designed to give the user easy access and simplify the code.
* @ param none
*/
public static void runUI() throws IOException
{
Scanner scan = new Scanner(System.in); //scanner
// asks the user file they want to alter
System.out.println("Please enter the name of the file you would like to alter: ");
String title = scan.nextLine();
File file = new File(title);
BufferedImage image = ImageIO.read(file);
// new file name
System.out.println("Please enter the name you would like to call your altered file: ");
String newTitle = scan.nextLine();
// googled this switch statement like a pro
System.out.println("Pick a number to alter your image: ?\n1: Make Red\n2: Make Green\n3: Make Blue\n4: Make Inverted\n5: Delete Red\n6: Delete Green\n7: Delete Blue\n8: Make Gray\n9: Make Warhol\n10: Make Mirrored\n11: Rotate Clock Wise\n12: Find Edges");
int choice = scan.nextInt();
switch (choice)
{
case 1:
System.out.println("The image will be red");
makeRed(image, newTitle);
break;
case 2:
System.out.println("The image will be green");
makeGreen(image, newTitle);
break;
case 3:
System.out.println("The image will be blue");
makeBlue(image, newTitle);
break;
case 4:
System.out.println("The image will be inverted");
Invert(image, newTitle);
break;
case 5:
System.out.println("The image will delete red");
deleteRed(image, newTitle);
break;
case 6:
System.out.println("The image will delete green");
deleteGreen(image, newTitle);
break;
case 7:
System.out.println("The image will delete blue");
deleteBlue(image, newTitle);
break;
case 8:
System.out.println("The image will be gray");
makeGray(image, newTitle);
break;
case 9:
System.out.println("The image will be displayed warhol-style.");
makeWarhol(image, newTitle);
break;
case 10:
System.out.println("The image will be mirrored. Prepare for #beauty!!"); //choose anokyai.jpg
makeMirrored(image, newTitle);
break;
case 11:
System.out.println("The image will be rotated clockwise");
rotateClockWise(image, newTitle);
break;
case 12:
System.out.println("The image will be displayed as black and white lineart. ");
findEdges(image, newTitle);
break;
default:
System.out.println("You did not pick a suitable number.");
}
}
public static void main(String[] args) throws IOException
{
runUI();
//everybody was kung fu fighting for realz
}
}