From 24fd48bee4dc7c1a3d43bcedaf0f7e97247b325e Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 28 Feb 2016 15:34:54 -0500 Subject: Created AutoArtAndroid --- .../org/neocities/autoart/autoart/AutoArt.java | 331 +++++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 src/java/org/neocities/autoart/autoart/AutoArt.java (limited to 'src/java') diff --git a/src/java/org/neocities/autoart/autoart/AutoArt.java b/src/java/org/neocities/autoart/autoart/AutoArt.java new file mode 100644 index 0000000..cab52f0 --- /dev/null +++ b/src/java/org/neocities/autoart/autoart/AutoArt.java @@ -0,0 +1,331 @@ +package org.neocities.autoart.autoart; + +import android.graphics.Bitmap; +import android.graphics.Color; +import android.os.Environment; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.widget.Button; +import android.widget.EditText; +import android.view.View; +import android.widget.ImageView; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Stack; + +public class AutoArt extends AppCompatActivity +{ + final int FUNCTION_LENGTH = 40; + int[][] matrix_add(int[][] a, int[][] b) + { + for (int i = 0; i < a.length; i++) + { + for (int j = 0; j < a[i].length; j++) + a[i][j] += b[i][j]; + } + return a; + } + + int[][] matrix_sub(int[][] a, int[][] b) + { + for (int i = 0; i < a.length; i++) + { + for (int j = 0; j < a[i].length; j++) + a[i][j] -= b[i][j]; + } + return a; + } + + int[][] matrix_mul(int[][] a, int[][] b) + { + for (int i = 0; i < a.length; i++) + { + for (int j = 0; j < a[i].length; j++) + a[i][j] *= b[i][j]; + } + return a; + } + + int[][] matrix_add_constant(int[][] m, int c) + { + for (int i = 0; i < m.length; i++) + { + for (int j = 0; j < m[i].length; j++) + m[i][j] += c; + } + return m; + } + + int[][] matrix_scale(int[][] m, int c) + { + for (int i = 0; i < m.length; i++) + { + for (int j = 0; j < m[i].length; j++) + m[i][j] *= c; + } + return m; + } + + int[][] sin_matrix(int[][] m) + { + for (int i = 0; i < m.length; i++) + for (int j = 0; j < m[i].length; j++) + m[i][j] = (int)(255*Math.sin((double)m[i][j])); + return m; + } + + int[][] cos_matrix(int[][] m) + { + for (int i = 0; i < m.length; i++) + for (int j = 0; j < m[i].length; j++) + m[i][j] = (int)(255*Math.cos((double) m[i][j])); + return m; + } + + int[][] sqrt_matrix(int[][] m) + { + for (int i = 0; i < m.length; i++) + for (int j = 0; j < m[i].length; j++) + m[i][j] = (int)(255*Math.sqrt(Math.abs((double) m[i][j]))); + return m; + } + + int[][] matrix_mod256(int[][] m) + { + for (int i = 0; i < m.length; i++) + { + for (int j = 0; j < m[i].length; j++) + m[i][j] %= 256; + } + return m; + } + + int randrange(int start, int end) + { + return (int)(Math.random()*(end-start)+start); + } + + String randFunction() + { + String function = null; + boolean hasX = false; + boolean hasY = false; + int operation; + int numbersOnStack; + int i; + while (!(hasX && hasY)) + { + function = "x "; + hasX = false; + hasY = false; + numbersOnStack = 1; + for (i = 0; i < FUNCTION_LENGTH; i++) + { + if (numbersOnStack > 1) + operation = randrange(0, 9); + else + operation = randrange(0, 6); + switch (operation) + { + case 0: + function += "x "; + hasX = true; + numbersOnStack++; + break; + case 1: + function += "y "; + hasY = true; + numbersOnStack++; + break; + case 2: + function += randrange(0, 200) + " "; + numbersOnStack++; + break; + case 3: + function += "sqrt "; + break; + case 4: + function += "cos "; + break; + case 5: + function += "sin "; + break; + case 6: + function += "+ "; + numbersOnStack--; + break; + case 7: + function += "- "; + numbersOnStack--; + break; + case 8: + function += "* "; + numbersOnStack--; + break; + + } + } + while (numbersOnStack-- > 1) + { + operation = randrange(0, 3); + switch(operation) + { + case 0: + function += "+ "; + break; + case 1: + function += "- "; + break; + case 2: + function += "* "; + break; + } + } + + } + return function; + + } + + protected int[][] evalFunction(String function, int width, int height) + { + Stack stack = new Stack<>(); + int[][] x = new int[width][height]; + int[][] y = new int[width][height]; + + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + x[i][j] = i; + y[i][j] = j; + } + } + + + String[] tokens = function.split(" "); + + for (String token: tokens) { + if (token.length() == 0) + continue; + + if (token.equals("x")) + stack.push(x); + else if (token.equals("y")) + stack.push(y); + else if (token.equals("sin")) + stack.push(sin_matrix(stack.pop())); + else if (token.equals("cos")) + stack.push(cos_matrix(stack.pop())); + else if (token.equals("sqrt")) + stack.push(sqrt_matrix(stack.pop())); + else if (token.equals("+")) + { + int[][] a = stack.pop(); + int[][] b = stack.pop(); + if (a.length == 1 && a[0].length == 1 && !(b.length == 1 && b[0].length == 1)) + stack.push(matrix_add_constant(b, a[0][0])); + else if (!(a.length == 1 && a[0].length == 1) && b.length == 1 && b[0].length == 1) + stack.push(matrix_add_constant(a, b[0][0])); + else + stack.push(matrix_add(a, b)); + + } + else if (token.equals("-")) + { + int[][] a = stack.pop(); + int[][] b = stack.pop(); + if (a.length == 1 && a[0].length == 1 && !(b.length == 1 && b[0].length == 1)) + stack.push(matrix_add_constant(b, -a[0][0])); + + else if (!(a.length == 1 && a[0].length == 1) && b.length == 1 && b[0].length == 1) + stack.push(matrix_add_constant(a, -b[0][0])); + + else + stack.push(matrix_sub(a, b)); + } + else if (token.equals("*")) + { + int[][] a = stack.pop(); + int[][] b = stack.pop(); + if (a.length == 1 && a[0].length == 1 && !(b.length == 1 && b[0].length == 1)) + stack.push(matrix_scale(b, a[0][0])); + + else if (!(a.length == 1 && a[0].length == 1) && b.length == 1 && b[0].length == 1) + stack.push(matrix_scale(a, b[0][0])); + + else + stack.push(matrix_mul(a, b)); + + } + else + { + int[][] m = new int[1][1]; + m[0][0] = Integer.parseInt(token); + stack.push(m); + } + + } + + return stack.pop(); + } + + protected void on_button_press() + { + EditText widthEdit = (EditText)findViewById(R.id.image_width_edit); + EditText heightEdit = (EditText)findViewById(R.id.image_height_edit); + int width = Integer.parseInt(widthEdit.getText().toString()); + int height = Integer.parseInt(heightEdit.getText().toString()); + int[][] r = matrix_mod256(evalFunction(randFunction(), width, height)); + int[][] g = matrix_mod256(evalFunction(randFunction(), width, height)); + int[][] b = matrix_mod256(evalFunction(randFunction(), width, height)); + Bitmap img = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + int al, gr, bl, rd; + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + al = 255; + rd = r[i][j]; + gr = g[i][j]; + bl = b[i][j]; + img.setPixel(i, j, Color.argb(al, rd, gr, bl)); + + } + + } + ImageView imgv = (ImageView) findViewById(R.id.image_view); + imgv.setImageBitmap(img); + File root = Environment.getExternalStorageDirectory(); + File imgFile = new File(root.getAbsolutePath() + "/DCIM/Camera/" + Math.random() + ".png"); + try + { + imgFile.createNewFile(); + FileOutputStream ostream = new FileOutputStream(imgFile); + img.compress(Bitmap.CompressFormat.PNG, 100, ostream); + ostream.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + + } + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_auto_art); + + final Button create_button = (Button)findViewById(R.id.create_button); + create_button.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) + { + on_button_press(); + } + }); + } + +} -- cgit v1.2.3