-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathImage.java
666 lines (532 loc) · 19.6 KB
/
Image.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package com.aviyehuda.easyimage;
import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* EasyImage lets you do all the basic image operations -
* converting, cropping, resizing, rotating, flipping…
* Plus it let’s you do some really cool affects.
* All is done super easily.
* Combining operations can produce some very cool results.
*
* Operations:
* Open image.
* Save image
* Convert image
* Re-size image
* Crop image
* Convert to black and white image
* Rotate image
* Flip image
* Add color to image
* Create image with multiple instance of the original
* Combining 2 images together
* Emphasize parts of the image
* Affine transform image
*
*
* @author Avi Yehuda
*
*/
public class Image {
private BufferedImage bufferedImage;
private String fileName;
/**
* Constructor - loads from an image file.
* @param imageFile
*/
public Image(File imageFile) {
try {
bufferedImage = ImageIO.read(imageFile);
fileName = imageFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
bufferedImage = null;
imageFile = null;
}
}
/**
* Constructor - loads from an image file.
* @param imageFilePath
*/
public Image(String imageFilePath) {
this(new File(imageFilePath));
}
/**
* Return image as java.awt.image.BufferedImage
* @return image as java.awt.image.BufferedImage
*/
public BufferedImage getAsBufferedImage(){
return bufferedImage;
}
/**
* Save the image as a new image file.
* Can also convert the image according to file type.
* @param fileName
*/
public void saveAs(String fileName){
saveImage(new File(fileName));
this.fileName = fileName;
}
/**
* Saves the image to the original file.
*/
public void save(){
saveImage(new File(fileName));
}
/**
* Resizing the image by percentage of the original.
* @param percentOfOriginal
*/
public void resize( int percentOfOriginal){
int newWidth = bufferedImage.getWidth() * percentOfOriginal / 100;
int newHeight = bufferedImage.getHeight() * percentOfOriginal / 100;
resize(newWidth, newHeight);
}
/**
* Resizing the image by width and height.
* @param newWidth
* @param newHeight
*/
public void resize( int newWidth, int newHeight){
int oldWidth = bufferedImage.getWidth();
int oldHeight = bufferedImage.getHeight();
if(newWidth == -1 || newHeight == -1){
if(newWidth == -1){
if(newHeight == -1){
return;
}
newWidth = newHeight * oldWidth/ oldHeight;
}
else {
newHeight = newWidth * oldHeight / oldWidth;
}
}
BufferedImage result =
new BufferedImage(newWidth , newHeight, BufferedImage.TYPE_INT_BGR);
double widthSkip = new Double(oldWidth-newWidth) / new Double(newWidth);
double heightSkip = new Double(oldHeight-newHeight) / new Double(newHeight);
double widthCounter = 0;
double heightCounter = 0;
int newY = 0;
boolean isNewImageWidthSmaller = widthSkip >0;
boolean isNewImageHeightSmaller = heightSkip >0;
for (int y = 0; y < oldHeight && newY < newHeight; y++) {
if(isNewImageHeightSmaller && heightCounter > 1){ //new image suppose to be smaller - skip row
heightCounter -= 1;
}
else if (heightCounter < -1){ //new image suppose to be bigger - duplicate row
heightCounter += 1;
if(y > 1)
y = y - 2;
else
y = y - 1;
}
else{
heightCounter += heightSkip;
int newX = 0;
for (int x = 0; x < oldWidth && newX < newWidth; x++) {
if(isNewImageWidthSmaller && widthCounter > 1){ //new image suppose to be smaller - skip column
widthCounter -= 1;
}
else if (widthCounter < -1){ //new image suppose to be bigger - duplicate pixel
widthCounter += 1;
if(x >1)
x = x - 2;
else
x = x - 1;
}
else{
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(newX, newY, rgb);
newX++;
widthCounter += widthSkip;
}
}
newY++;
}
}
bufferedImage = result;
}
/**
* Add color to the RGB of the pixel
* @param numToAdd
*/
public void addPixelColor(int numToAdd){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
bufferedImage.setRGB(x, y, rgb+numToAdd);
}
}
}
/**
* Covert image to black and white.
*/
public void convertToBlackAndWhite() {
ColorSpace gray_space = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp convert_to_gray_op = new ColorConvertOp(gray_space, null);
convert_to_gray_op.filter(bufferedImage, bufferedImage);
}
/**
* Rotates image 90 degrees to the left.
*/
public void rotateLeft(){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(height,
width, bufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(y, x, rgb);
}
}
bufferedImage = result;
}
/**
* Rotates image 90 degrees to the right.
*/
public void rotateRight(){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(height,
width, bufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(height-y-1, x, rgb);
}
}
bufferedImage = result;
}
/**
* Rotates image 180 degrees.
*/
public void rotate180(){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(width,
height, bufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(width-x-1, height-y-1, rgb);
}
}
bufferedImage = result;
}
/**
* Flips the image horizontally
*/
public void flipHorizontally(){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(width,
height, bufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(width-x-1, y, rgb);
}
}
bufferedImage = result;
}
/**
* Flips the image vertically.
*/
public void flipVertically(){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(width,
height, bufferedImage.TYPE_INT_BGR);
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(x, height-y-1, rgb);
}
}
bufferedImage = result;
}
/**
* Multiply the image.
* @param timesToMultiplyVertically
* @param timesToMultiplyHorizantelly
*/
public void multiply(int timesToMultiplyVertically,
int timesToMultiplyHorizantelly){
multiply(timesToMultiplyVertically,timesToMultiplyHorizantelly,0);
}
/**
* Multiply the image and also add color each of the multiplied images.
* @param timesToMultiplyVertically
* @param timesToMultiplyHorizantelly
*/
public void multiply(int timesToMultiplyVertically,
int timesToMultiplyHorizantelly, int colorToHenhancePerPixel){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage result = new BufferedImage(width*timesToMultiplyVertically,
height*timesToMultiplyHorizantelly, bufferedImage.TYPE_INT_BGR);
for (int xx = 0; xx < timesToMultiplyVertically; xx ++) {
for (int yy = 0; yy < timesToMultiplyHorizantelly; yy ++) {
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(width*xx+x, height*yy+y, rgb+colorToHenhancePerPixel*(yy+xx));
}
}
}
}
bufferedImage = result;
}
/**
* Combines the image with another image in an equal presence to both;
* @param newImagePath - image to combine with
*/
public void combineWithPicture(String newImagePath){
combineWithPicture(newImagePath, 2);
}
/**
* Combines the image with another image.
* jump = 2 means that every two pixels the new image is replaced.
* This makes the 2 images equal in presence. If jump=3 than every 3rd
* pixel is replaced by the new image.
* As the jump is higher this is how much the new image has less presence.
*
* @param newImagePath
* @param jump
*/
public void combineWithPicture(String newImagePath, int jump){
try {
BufferedImage bufferedImage2 = ImageIO.read(new File(newImagePath));
combineWithPicture(bufferedImage2, jump, null);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void combineWithPicture(Image image2){
combineWithPicture(image2.getAsBufferedImage(), 2, null);
}
public void combineWithPicture(Image image2, int jump){
combineWithPicture(image2.getAsBufferedImage(), jump, null);
}
public void combineWithPicture(Image image2, Color ignoreColor){
combineWithPicture(image2.getAsBufferedImage(), 2, ignoreColor);
}
public void combineWithPicture(Image image2, int jump, Color ignoreColor){
combineWithPicture(image2.getAsBufferedImage(), jump, ignoreColor);
}
/**
* Combines the image with another image.
* jump = 2 means that every two pixels the new image is replaced.
* This makes the 2 images equal in presence. If jump=3 than every 3rd
* pixel is replaced by the new image.
* As the jump is higher this is how much the new image has less presence.
*
* ignoreColor is a color in the new image that will not be copied -
* this is good where there is a background which you do not want to copy.
*
* @param bufferedImage2
* @param jump
* @param ignoreColor
*/
private void combineWithPicture(BufferedImage bufferedImage2,
int jump, Color ignoreColor){
checkJump(jump);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int width2 = bufferedImage2.getWidth();
int height2 = bufferedImage2.getHeight();
int ignoreColorRgb = -1;
if(ignoreColor != null){
ignoreColorRgb = ignoreColor.getRGB();
}
for (int y = 0; y < height; y ++) {
for (int x = y%jump; x < width; x +=jump) {
if(x >= width2 || y>= height2){
continue;
}
int rgb = bufferedImage2.getRGB(x, y);
if( rgb != ignoreColorRgb ){
bufferedImage.setRGB(x, y, rgb);
}
}
}
}
public void crop(int startX, int startY, int endX, int endY){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if(startX == -1){
startX = 0;
}
if(startY == -1){
startY = 0;
}
if(endX == -1){
endX = width-1;
}
if(endY == -1){
endY = height-1;
}
BufferedImage result = new BufferedImage(endX-startX+1,
endY-startY+1, bufferedImage.TYPE_INT_BGR);
for (int y = startY; y < endY; y ++) {
for (int x = startX; x < endX; x ++) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(x-startX, y-startY, rgb);
}
}
bufferedImage = result;
}
private void saveImage(File file) {
try {
ImageIO.write(bufferedImage, getFileType(file), file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void emphasize(int startX, int startY, int endX, int endY){
emphasize(startX, startY, endX, endY, Color.BLACK, 3 );
}
public void emphasize(int startX, int startY, int endX, int endY, Color backgroundColor){
emphasize(startX, startY, endX, endY, backgroundColor, 3 );
}
public void emphasize(int startX, int startY, int endX, int endY,int jump){
emphasize(startX, startY, endX, endY, Color.BLACK, jump );
}
public void emphasize(int startX, int startY, int endX, int endY, Color backgroundColor,int jump){
checkJump(jump);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if(startX == -1){
startX = 0;
}
if(startY == -1){
startY = 0;
}
if(endX == -1){
endX = width-1;
}
if(endY == -1){
endY = height-1;
}
for (int y = 0; y < height; y ++) {
for (int x = y%jump; x < width; x +=jump) {
if(y >= startY && y <= endY && x >= startX && x <= endX){
continue;
}
bufferedImage.setRGB(x, y, backgroundColor.getRGB());
}
}
}
private void checkJump(int jump) {
if(jump<1){
throw new RuntimeException("Error: jump can not be less than 1");
}
}
public void addColorToImage(Color color, int jump){
addColorToImage(color.getRGB(),jump);
}
public void addColorToImage(int rgb, int jump){
checkJump(jump);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
for (int y = 0; y < height; y ++) {
for (int x = y%jump; x < width; x +=jump) {
bufferedImage.setRGB(x, y, rgb);
}
}
}
public void affineTransform (double fShxFactor, double fShyFactor) {
try {
AffineTransform shearer =
AffineTransform.getShearInstance (fShxFactor, fShyFactor);
AffineTransformOp shear_op =
new AffineTransformOp (shearer, null);
bufferedImage = shear_op.filter (bufferedImage, null);
}
catch (Exception e) {
System.out.println("Shearing exception = " + e);
}
}
private String getFileType(File file) {
String fileName = file.getName();
int idx = fileName.lastIndexOf(".");
if(idx == -1){
throw new RuntimeException("Invalid file name");
}
return fileName.substring(idx+1);
}
public int getWidth(){
return bufferedImage.getWidth();
}
public int getHeight(){
return bufferedImage.getHeight();
}
public static void main(String[] args) {
/*
Image image = new Image("c:/pics/p1.jpg");
image.resize(10);
image.multiply(5, 5, 11111);
image.saveAs("c:/pics/multiply+color.jpg");
*/
/*
Image image = new Image("c:/pics/israel_flag.gif");
Image image2 = new Image("c:/pics/palestine_flag.gif");
//image.resize(50);
//image2.resize(50);
//image.affineTransform(0, 0.3);
//image2.affineTransform(0, -0.3);
image.combineWithPicture(image2);
image.saveAs("c:/pics/affineTransformAndCombine2.jpg");
*/
/*
Image image = new Image("c:/pics/p1.jpg");
image.resize(50);
image.affineTransform(0.0, 0.5);
image.saveAs("c:/pics/affineTransform.jpg");
*/
/*
Image image = new Image("c:/pics/heart.gif");
image.multiply(20, 20);
Image image2 = new Image("c:/pics/p6.jpg");
image2.crop(800, 0, -1, -1);
image2.resize(50);
image2.combineWithPicture(image,3,Color.white);
image2.saveAs("c:/pics/combineWithPictureWithoutBackground.jpg");
/*
image.resize(5);
image.multiply(20, 20);
image.combineWithPicture("c:/p2.jpg");
//image.addColorToImage(Color.yellow, 3);
//image.addColorToImage(Color.red, 5);
//image.combineWithPicture("c:/p2.jpg",3);
*/
Image image = new Image("c:/pics/p1.jpg");
int width = image.getWidth();
int height = image.getHeight();
for(int i=0,c=0;i<height;c++,i+=50){
int x = width/2 - i;
int y = height/2 - i;
image.emphasize(x, y, width-1-x, height-1-y, Color.BLACK, 12 - c/4);
}
image.saveAs("c:/pics/emphesizeTrick.jpg");
// */
// image.saveAs("c:/xxx.jpg");
/*
Image image = new Image("c:/pics/p1.jpg");
image.addColorToImage(Color.red, 5);
image.saveAs("c:/pics/addColorToImage.jpg");
*/
}
}