diff options
Diffstat (limited to 'src/misc')
-rwxr-xr-x | src/misc/CompressedImage.java | 129 | ||||
-rwxr-xr-x | src/misc/Stapel.java | 51 | ||||
-rwxr-xr-x | src/misc/Utils.java | 73 |
3 files changed, 253 insertions, 0 deletions
diff --git a/src/misc/CompressedImage.java b/src/misc/CompressedImage.java new file mode 100755 index 0000000..b37daf9 --- /dev/null +++ b/src/misc/CompressedImage.java @@ -0,0 +1,129 @@ +package misc;
+
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+import java.util.ArrayList;
+
+public class CompressedImage {
+
+ private BufferedImage image;
+ private int blockWidth, blockHeight, colors;
+ private byte[] data;
+
+ public CompressedImage(BufferedImage image, int blockWidth, int blockHeight, int colors) {
+ ArrayList<Byte[]> compData = new ArrayList<Byte[]>();
+ int imageWidth = image.getWidth(), imageHeight = image.getHeight();
+ this.image = new BufferedImage(imageWidth, imageHeight, image.getType());
+ this.blockWidth = blockWidth;
+ this.blockHeight = blockHeight;
+ compData.add(Utils.intToByteArray(imageWidth));
+ compData.add(Utils.intToByteArray(imageHeight));
+ compData.add(Utils.intToByteArray(image.getType()));
+ compData.add(Utils.intToByteArray(blockWidth));
+ compData.add(Utils.intToByteArray(blockHeight));
+ compData.add(Utils.intToByteArray(colors));
+ Color[][] block;
+ int[][][] table;
+ boolean inList;
+ ArrayList<Integer[]> list;
+ for(int xb = 0; xb<imageWidth; xb+=blockWidth) {
+ for(int yb = 0; yb<imageHeight; yb+=blockHeight) {
+ block = new Color[blockWidth][blockHeight];
+ list = new ArrayList<Integer[]>();
+ x: for(int x = 0; x<blockWidth; x++) {
+ y: for(int y = 0; y<blockHeight; y++) {
+ System.out.println("Lese pixel: (" + (xb+x) + "," + (yb+y) + ")");
+ if(xb+x>=imageWidth) break y;
+ if(yb+y>=imageHeight) continue x;
+ block[x][y] = new Color(image.getRGB(xb+x, yb+y));
+ inList = false;
+ for(int i = 0, size = list.size(); i<size; i++) {
+ if(list.get(i)[0] == block[x][y].getRed() && list.get(i)[1] == block[x][y].getGreen() && list.get(i)[2] == block[x][y].getBlue() && list.get(i)[3] == block[x][y].getAlpha()) {
+ list.set(i, new Integer[] { block[x][y].getRed(), block[x][y].getGreen(), block[x][y].getBlue(), block[x][y].getAlpha(), list.get(i)[4]+1 });
+ inList = true;
+ }
+ }
+ if(!inList)
+ list.add(new Integer[] { block[x][y].getRed(), block[x][y].getGreen(), block[x][y].getBlue(), block[x][y].getAlpha(), 1 });
+ }
+ }
+ int count = 0;
+ Stapel a = new Stapel(), b = new Stapel(), c = new Stapel();
+ for(int i = 0, size = list.size(); i<size; i++) {
+ System.out.println("Zwischenspeichern... (" + i + "/" + size + ")");
+ a.ablegen(list.get(i));
+ }
+ count = 0;
+ while(!a.istLeer()) {
+ System.out.println("Verarbeiten... (" + count + "/" + list.size() + ")");
+ Integer[] top = (Integer[])a.entnehmen();
+ while(!b.istLeer() && ((Integer[])b.inhaltGeben())[4] > top[4]) {
+ a.ablegen(b.entnehmen());
+ count--;
+ }
+ b.ablegen(top);
+ count++;
+ }
+ Color[] palette = new Color[colors];
+ Color previous = null;
+ Integer[] current = null;
+ for(int i = 0; i<palette.length; i++) {
+ if(b.istLeer()) {
+ if(previous == null) {
+ palette[i] = new Color(0,0,0);
+ } else {
+ palette[i] = previous;
+ }
+ } else {
+ current = (Integer[])b.entnehmen();
+ palette[i] = new Color(current[0], current[1], current[2], current[3]);
+ previous = new Color(current[0], current[1], current[2], current[3]);
+ }
+
+ }
+ for(int i = 0; i<palette.length; i++) {
+ compData.add(Utils.intToByteArray(palette[i].getRGB()));
+ }
+ int min, diff, smallest;
+ x: for(int x = 0; x<blockWidth; x++) {
+ y: for(int y = 0; y<blockHeight; y++) {
+ if(xb+x>=imageWidth) break y;
+ if(yb+y>=imageHeight) continue x;
+ Color col = new Color(image.getRGB(xb+x, yb+y));
+ min = 2000;
+ diff = 0;
+ smallest = 0;
+ for(int i = 0; i<palette.length; i++) {
+ if((diff = (int)Math.sqrt(Math.pow(((palette[i].getRed()+palette[i].getGreen()+palette[i].getBlue()+palette[i].getAlpha()) - (col.getRed()+col.getGreen()+col.getBlue()+col.getAlpha())), 2))) < min) {
+ min = diff;
+ smallest = i;
+ }
+ }
+ this.image.setRGB(xb+x, yb+y, palette[smallest].getRGB());
+ compData.add(new Byte[] {(byte)smallest});
+ }
+ }
+ }
+ }
+ int total = 0;
+ for(int i = 0, size = compData.size(); i<size; i++) {
+ total += compData.get(i).length;
+ }
+ this.data = new byte[total];
+ for(int i = 0, size = compData.size(); i<size; i++) {
+ Byte[] current = compData.get(i);
+ for(int j = 0; j<current.length; j++) {
+ this.data[i+j] = current[j];
+ }
+ }
+ }
+
+ public BufferedImage getImage() {
+ return image;
+ }
+
+ public byte[] getData() {
+ return data;
+ }
+
+}
diff --git a/src/misc/Stapel.java b/src/misc/Stapel.java new file mode 100755 index 0000000..92efc6d --- /dev/null +++ b/src/misc/Stapel.java @@ -0,0 +1,51 @@ +package misc;
+public class Stapel { //Die Klasse / Datei muss genau so heißen!
+
+ private Stapel nextObject;
+ private Object content;
+
+ private Stapel(Object o){
+ content = o;
+ nextObject = null;
+ }
+
+ public Stapel(){
+ nextObject = null;
+ }
+
+ public boolean istLeer(){
+ if (nextObject == null) return true;
+ else return false;
+ }
+
+ public Object inhaltGeben(){
+ if (!istLeer()) return nextObject.content;
+ else return null;
+ }
+
+ public void ablegen(Object o){
+ Stapel neu = new Stapel(o);
+ neu.nextObject = nextObject;
+ nextObject = neu;
+ }
+
+ public Object entnehmen(){
+ Object aus;
+ if (!istLeer()){
+ aus = inhaltGeben();
+ nextObject = nextObject.nextObject;
+ return aus;
+ }
+ else return null;
+ }
+
+ public String ausgeben(){
+ String aus = "";
+ if (!istLeer()){
+ aus = aus + nextObject.content + "|";
+ if (nextObject.nextObject != null) aus = aus + nextObject.ausgeben();
+ }
+ return aus;
+ }
+
+}
\ No newline at end of file diff --git a/src/misc/Utils.java b/src/misc/Utils.java new file mode 100755 index 0000000..a3d5b0a --- /dev/null +++ b/src/misc/Utils.java @@ -0,0 +1,73 @@ +package misc;
+
+
+import java.io.UnsupportedEncodingException;
+
+public class Utils {
+
+ public static int randomInt(int from, int to) {
+ return (int) (from + Math.round(Math.random() * to));
+ }
+
+ public static String randomStringAN(int length) {
+ String output = "";
+ char[] table = new char[] {
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
+ };
+ for(int i = 0; i<length; i++)
+ output += table[randomInt(0, table.length-1)];
+ return output;
+ }
+
+ public static String randomStringUTF8(int length) {
+ byte[] b = new byte[length];
+ while(true) {
+ for(int i = 0; i<length; i++)
+ b[i] = (byte) Utils.randomInt(0, 255);
+ try {
+ return new String(b, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public static int byteArrayToInt(Byte[] b)
+ {
+ return b[3] & 0xFF |
+ (b[2] & 0xFF) << 8 |
+ (b[1] & 0xFF) << 16 |
+ (b[0] & 0xFF) << 24;
+ }
+
+ public static Byte[] intToByteArray(int a)
+ {
+ return new Byte[] {
+ (byte) ((a >> 24) & 0xFF),
+ (byte) ((a >> 16) & 0xFF),
+ (byte) ((a >> 8) & 0xFF),
+ (byte) (a & 0xFF)
+ };
+ }
+
+ public static int byteArrayToInt(byte[] b)
+ {
+ return b[3] & 0xFF |
+ (b[2] & 0xFF) << 8 |
+ (b[1] & 0xFF) << 16 |
+ (b[0] & 0xFF) << 24;
+ }
+
+ public static byte[] intTobyteArray(int a)
+ {
+ return new byte[] {
+ (byte) ((a >> 24) & 0xFF),
+ (byte) ((a >> 16) & 0xFF),
+ (byte) ((a >> 8) & 0xFF),
+ (byte) (a & 0xFF)
+ };
+ }
+
+}
|