5
5
import java .util .Random ;
6
6
7
7
public class RgbRandomWalk {
8
- private static Random prng = new Random ();
9
-
10
- private static int step (int pos , int upperBound ) {
11
- if (pos <= 0 ) {
12
- return prng .nextInt (2 );
13
- } else if (pos >= upperBound - 1 ) {
14
- return upperBound - 1 - prng .nextInt (2 );
15
- } else {
16
- return pos - 1 + prng .nextInt (3 );
17
- }
18
- }
19
-
20
- public static void main (String [] args ) {
21
- long start = System .currentTimeMillis ();
22
- String fileName = "" ;
23
- int width = 1920 , height = 1080 ;
24
- int increment = 8 ;
25
- int steps = 6000000 ;
26
- String imgFormat = "png" ;
27
-
28
- if (args .length != 4 ) {
29
- System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
30
- System .exit (0 );
31
- } else {
32
- try {
33
- fileName = args [0 ];
34
- width = Integer .parseInt (args [1 ], 10 );
35
- height = Integer .parseInt (args [2 ], 10 );
36
- steps = Integer .parseInt (args [3 ], 10 );
37
-
38
- } catch (NumberFormatException e ) {
39
- System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
40
- System .out .println ("Error: Invalid number." );
41
- System .exit (0 );
42
- }
43
-
44
- if (width <= 0 || height <= 0 || steps <= 0 ) {
45
- System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
46
- System .out .println ("Error: Negative number." );
47
- System .exit (0 );
48
- }
49
- }
50
-
51
- BufferedImage img ;
52
- Color c ;
53
- // Good parameters are 1920 x 1080, intensity increment 8, and steps = 6000000.
54
-
55
- int rX = width / 2 , rY = height / 2 ;
56
- int gX = width / 2 , gY = height / 2 ;
57
- int bX = width / 2 , bY = height / 2 ;
58
- int r = 0 , g = 0 , b = 0 ;
59
- // int sRGB, other;
60
-
61
-
62
- img = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
63
-
64
- rX = width / 2 ;
65
- rY = height / 2 ;
66
- gX = width / 2 ;
67
- gY = height / 2 ;
68
- bX = width / 2 ;
69
- bY = height / 2 ;
70
-
71
- for (int j = 0 ; j < steps ; j ++) {
72
-
73
-
74
- rX = step (rX , width );
75
- rY = step (rY , height );
76
- gX = step (gX , width );
77
- gY = step (gY , height );
78
- bX = step (bX , width );
79
- bY = step (bY , height );
80
-
81
- c = new Color (img .getRGB (rX , rY ));
82
- img .setRGB (rX , rY , (c .getRGB () & 0xff00ffff ) | (Math .min (c .getRed () + increment , 255 ) << 16 ));
83
-
84
- c = new Color (img .getRGB (gX , gY ));
85
- img .setRGB (gX , gY , (c .getRGB () & 0xffff00ff ) | (Math .min (c .getGreen () + increment , 255 ) << 8 ));
86
-
87
- c = new Color (img .getRGB (bX , bY ));
88
- img .setRGB (bX , bY , (c .getRGB () & 0xffffff00 ) | (Math .min (c .getBlue () + increment , 255 )));
89
- }
90
-
91
- try {
92
- File outputfile = new File (fileName );
93
- ImageIO .write (img , imgFormat , outputfile );
94
- } catch (Exception e ) {
95
- System .out .println ("Error: cannot open the file." );
96
- System .exit (0 );
97
- }
98
-
99
-
100
- long time = System .currentTimeMillis ()-start ;
101
- System .out .printf ("Time used: %dm, %ds, %dms.%n" , time / 60000 , (time / 1000 ) % 60 , time % 1000 );
102
- }
103
-
104
- }
8
+ private static Random prng = new Random ();
9
+
10
+ private static int step (int pos , int upperBound ) {
11
+ if (pos <= 0 ) {
12
+ return prng .nextInt (2 );
13
+ } else if (pos >= upperBound - 1 ) {
14
+ return upperBound - 1 - prng .nextInt (2 );
15
+ } else {
16
+ return pos - 1 + prng .nextInt (3 );
17
+ }
18
+ }
19
+
20
+ public static void main (String [] args ) {
21
+ long start = System .currentTimeMillis ();
22
+ String fileName = "" ;
23
+ int width = 1920 , height = 1080 ;
24
+ int increment = 8 ;
25
+ int steps = 6000000 ;
26
+ String imgFormat = "png" ;
27
+
28
+ if (args .length != 4 ) {
29
+ System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
30
+ System .exit (0 );
31
+ } else {
32
+ try {
33
+ fileName = args [0 ];
34
+ width = Integer .parseInt (args [1 ], 10 );
35
+ height = Integer .parseInt (args [2 ], 10 );
36
+ steps = Integer .parseInt (args [3 ], 10 );
37
+
38
+ } catch (NumberFormatException e ) {
39
+ System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
40
+ System .out .println ("Error: Invalid number." );
41
+ System .exit (0 );
42
+ }
43
+
44
+ if (width <= 0 || height <= 0 || steps <= 0 ) {
45
+ System .out .println ("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>" );
46
+ System .out .println ("Error: Negative number." );
47
+ System .exit (0 );
48
+ }
49
+ }
50
+
51
+ BufferedImage img ;
52
+ Color c ;
53
+ // Good parameters are 1920 x 1080, intensity increment 8, and steps = 6000000.
54
+
55
+ int rX = width / 2 , rY = height / 2 ;
56
+ int gX = width / 2 , gY = height / 2 ;
57
+ int bX = width / 2 , bY = height / 2 ;
58
+ int r = 0 , g = 0 , b = 0 ;
59
+ // int sRGB, other;
60
+
61
+
62
+ img = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
63
+
64
+ rX = width / 2 ;
65
+ rY = height / 2 ;
66
+ gX = width / 2 ;
67
+ gY = height / 2 ;
68
+ bX = width / 2 ;
69
+ bY = height / 2 ;
70
+
71
+ for (int j = 0 ; j < steps ; j ++) {
72
+ rX = step (rX , width );
73
+ rY = step (rY , height );
74
+ gX = step (gX , width );
75
+ gY = step (gY , height );
76
+ bX = step (bX , width );
77
+ bY = step (bY , height );
78
+
79
+ c = new Color (img .getRGB (rX , rY ));
80
+ img .setRGB (rX , rY , (c .getRGB () & 0xff00ffff ) | (Math .min (c .getRed () + increment , 255 ) << 16 ));
81
+
82
+ c = new Color (img .getRGB (gX , gY ));
83
+ img .setRGB (gX , gY , (c .getRGB () & 0xffff00ff ) | (Math .min (c .getGreen () + increment , 255 ) << 8 ));
84
+
85
+ c = new Color (img .getRGB (bX , bY ));
86
+ img .setRGB (bX , bY , (c .getRGB () & 0xffffff00 ) | (Math .min (c .getBlue () + increment , 255 )));
87
+ }
88
+
89
+ try {
90
+ File outputfile = new File (fileName );
91
+ ImageIO .write (img , imgFormat , outputfile );
92
+ } catch (Exception e ) {
93
+ System .out .println ("Error: cannot open the file." );
94
+ System .exit (0 );
95
+ }
96
+
97
+
98
+ long time = System .currentTimeMillis ()-start ;
99
+ System .out .printf ("Time used: %dm, %ds, %dms.%n" , time / 60000 , (time / 1000 ) % 60 , time % 1000 );
100
+ }
101
+ }
0 commit comments