summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <leonardomtenenbaum@gmail.com>2016-02-28 15:34:54 -0500
committerpommicket <leonardomtenenbaum@gmail.com>2016-02-28 15:34:54 -0500
commit24fd48bee4dc7c1a3d43bcedaf0f7e97247b325e (patch)
tree10e1310581f26bc8ce25687a6f992038a143c244
parent858177061afc14af133f8d699b8671ecda384904 (diff)
Created AutoArtAndroid
-rw-r--r--AutoArt.apkbin0 -> 1240131 bytes
-rw-r--r--src/AndroidManifest.xml20
-rw-r--r--src/java/org/neocities/autoart/autoart/AutoArt.java331
-rw-r--r--src/res/layout/activity_auto_art.xml58
-rw-r--r--src/res/mipmap-hdpi/ic_launcher.pngbin0 -> 3418 bytes
-rw-r--r--src/res/mipmap-mdpi/ic_launcher.pngbin0 -> 2206 bytes
-rw-r--r--src/res/mipmap-xhdpi/ic_launcher.pngbin0 -> 4842 bytes
-rw-r--r--src/res/mipmap-xxhdpi/ic_launcher.pngbin0 -> 7718 bytes
-rw-r--r--src/res/mipmap-xxxhdpi/ic_launcher.pngbin0 -> 10486 bytes
-rw-r--r--src/res/values-w820dp/dimens.xml6
-rw-r--r--src/res/values/colors.xml6
-rw-r--r--src/res/values/dimens.xml5
-rw-r--r--src/res/values/strings.xml6
-rw-r--r--src/res/values/styles.xml11
14 files changed, 443 insertions, 0 deletions
diff --git a/AutoArt.apk b/AutoArt.apk
new file mode 100644
index 0000000..d337513
--- /dev/null
+++ b/AutoArt.apk
Binary files differ
diff --git a/src/AndroidManifest.xml b/src/AndroidManifest.xml
new file mode 100644
index 0000000..d575f00
--- /dev/null
+++ b/src/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="org.neocities.autoart.autoart" >
+
+ <application
+ android:allowBackup="true"
+ android:icon="@mipmap/ic_launcher"
+ android:label="@string/app_name"
+ android:supportsRtl="true"
+ android:theme="@style/AppTheme" >
+ <activity android:name=".AutoArt" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
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<int[][]> 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();
+ }
+ });
+ }
+
+}
diff --git a/src/res/layout/activity_auto_art.xml b/src/res/layout/activity_auto_art.xml
new file mode 100644
index 0000000..760a1df
--- /dev/null
+++ b/src/res/layout/activity_auto_art.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:paddingLeft="@dimen/activity_horizontal_margin"
+ android:paddingRight="@dimen/activity_horizontal_margin"
+ android:paddingTop="@dimen/activity_vertical_margin"
+ android:paddingBottom="@dimen/activity_vertical_margin"
+ tools:context="org.neocities.autoart.autoart.AutoArt">
+
+ <TextView
+ android:text="@string/width_text"
+ android:textSize="20sp"
+ android:textColor="#000000"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+ <EditText
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:inputType="number"
+ android:id="@+id/image_width_edit"
+ android:layout_marginTop="20sp"
+ />
+ <TextView
+ android:text="@string/height_text"
+ android:textSize="20sp"
+ android:textColor="#000000"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_below="@id/image_width_edit"
+ android:id="@+id/image_height_text"
+ />
+ <EditText
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:inputType="number"
+ android:id="@+id/image_height_edit"
+ android:layout_below="@id/image_height_text"
+ />
+ <Button
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:id="@+id/create_button"
+ android:text="@string/create_button_text"
+ android:layout_below="@id/image_height_edit"
+ android:background="#beffbe"
+ />
+
+ <ImageView
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:layout_below="@id/create_button"
+ android:id="@+id/image_view"
+ />
+
+</RelativeLayout>
diff --git a/src/res/mipmap-hdpi/ic_launcher.png b/src/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..cde69bc
--- /dev/null
+++ b/src/res/mipmap-hdpi/ic_launcher.png
Binary files differ
diff --git a/src/res/mipmap-mdpi/ic_launcher.png b/src/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..c133a0c
--- /dev/null
+++ b/src/res/mipmap-mdpi/ic_launcher.png
Binary files differ
diff --git a/src/res/mipmap-xhdpi/ic_launcher.png b/src/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..bfa42f0
--- /dev/null
+++ b/src/res/mipmap-xhdpi/ic_launcher.png
Binary files differ
diff --git a/src/res/mipmap-xxhdpi/ic_launcher.png b/src/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..324e72c
--- /dev/null
+++ b/src/res/mipmap-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/src/res/mipmap-xxxhdpi/ic_launcher.png b/src/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..aee44e1
--- /dev/null
+++ b/src/res/mipmap-xxxhdpi/ic_launcher.png
Binary files differ
diff --git a/src/res/values-w820dp/dimens.xml b/src/res/values-w820dp/dimens.xml
new file mode 100644
index 0000000..63fc816
--- /dev/null
+++ b/src/res/values-w820dp/dimens.xml
@@ -0,0 +1,6 @@
+<resources>
+ <!-- Example customization of dimensions originally defined in res/values/dimens.xml
+ (such as screen margins) for screens with more than 820dp of available width. This
+ would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
+ <dimen name="activity_horizontal_margin">64dp</dimen>
+</resources>
diff --git a/src/res/values/colors.xml b/src/res/values/colors.xml
new file mode 100644
index 0000000..3ab3e9c
--- /dev/null
+++ b/src/res/values/colors.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <color name="colorPrimary">#3F51B5</color>
+ <color name="colorPrimaryDark">#303F9F</color>
+ <color name="colorAccent">#FF4081</color>
+</resources>
diff --git a/src/res/values/dimens.xml b/src/res/values/dimens.xml
new file mode 100644
index 0000000..47c8224
--- /dev/null
+++ b/src/res/values/dimens.xml
@@ -0,0 +1,5 @@
+<resources>
+ <!-- Default screen margins, per the Android Design guidelines. -->
+ <dimen name="activity_horizontal_margin">16dp</dimen>
+ <dimen name="activity_vertical_margin">16dp</dimen>
+</resources>
diff --git a/src/res/values/strings.xml b/src/res/values/strings.xml
new file mode 100644
index 0000000..f3e4529
--- /dev/null
+++ b/src/res/values/strings.xml
@@ -0,0 +1,6 @@
+<resources>
+ <string name="app_name">AutoArt</string>
+ <string name="width_text">Image width:</string>
+ <string name="height_text">Image height:</string>
+ <string name="create_button_text">Create image</string>
+</resources>
diff --git a/src/res/values/styles.xml b/src/res/values/styles.xml
new file mode 100644
index 0000000..5885930
--- /dev/null
+++ b/src/res/values/styles.xml
@@ -0,0 +1,11 @@
+<resources>
+
+ <!-- Base application theme. -->
+ <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
+ <!-- Customize your theme here. -->
+ <item name="colorPrimary">@color/colorPrimary</item>
+ <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
+ <item name="colorAccent">@color/colorAccent</item>
+ </style>
+
+</resources>