summaryrefslogtreecommitdiff
path: root/src/aufgaben/imageio
diff options
context:
space:
mode:
Diffstat (limited to 'src/aufgaben/imageio')
-rwxr-xr-xsrc/aufgaben/imageio/Aufgabe1.java193
-rwxr-xr-xsrc/aufgaben/imageio/ImageType.java8
-rwxr-xr-xsrc/aufgaben/imageio/Komprimierung1.java225
3 files changed, 426 insertions, 0 deletions
diff --git a/src/aufgaben/imageio/Aufgabe1.java b/src/aufgaben/imageio/Aufgabe1.java
new file mode 100755
index 0000000..6394f80
--- /dev/null
+++ b/src/aufgaben/imageio/Aufgabe1.java
@@ -0,0 +1,193 @@
+package aufgaben.imageio;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.image.BufferedImage;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import javax.imageio.ImageIO;
+import javax.swing.JButton;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import aufgaben.Aufgabe;
+import misc.Utils;
+
+public class Aufgabe1 extends Aufgabe {
+
+ private final int width = 500, height = 500;
+ private boolean done = false;
+ private String name;
+ private JPanel panel;
+ private JTextField inPath, outPath;
+ private JButton readButton, writeButton;
+ private BufferedImage bild;
+
+ public Aufgabe1() {
+ name = "Lesen Speichern";
+ this.setSize(width, height);
+ this.setTitle(name);
+ this.addWindowListener(
+ new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ System.out.println("close");
+ done = true;
+ }
+ }
+ );
+ this.setLayout(null);
+ }
+
+ @Override
+ public boolean done() {
+ return done;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void init() {
+ inPath = new JTextField();
+ inPath.setBounds(50, 50, 200, 20);
+ outPath = new JTextField();
+ outPath.setBounds(50, 80, 200, 20);
+ readButton = new JButton("Lesen");
+ readButton.setBounds(260, 50, 100, 20);
+ readButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ bild = readImage(new File(inPath.getText()));
+ /*
+ try {
+ bild = ImageIO.read(new File("title.png"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ */
+ panel.repaint();
+ }
+ });
+ writeButton = new JButton("Schreiben");
+ writeButton.setBounds(260, 80, 100, 20);
+ writeButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ writeImage(new File(outPath.getText()), bild);
+ }
+ });
+ panel = new JPanel() {
+ public void paint(Graphics g) {
+ if(bild != null)
+ g.drawImage(bild, 0, 0, 500, 390, this);
+ }
+ };
+ panel.setBounds(0, 110, 500, 390);
+ this.add(inPath);
+ this.add(outPath);
+ this.add(readButton);
+ this.add(writeButton);
+ this.add(panel);
+ this.repaint();
+ }
+
+ private BufferedImage getGreyImage(BufferedImage i) {
+ BufferedImage image = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
+ for(int x = 0, width = i.getWidth(); x<width; x++) {
+ for(int y = 0, height = i.getHeight(); y<height; y++) {
+ Color c = new Color(i.getRGB(x, y));
+ int h = (int)(((double)(c.getRed()+c.getGreen()+c.getBlue())/765)*255);
+ image.setRGB(x, y, new Color(h, h, h).getRGB());
+ }
+ }
+ return image;
+ }
+
+ private BufferedImage readImage(File file) {
+ FileInputStream fis;
+ BufferedInputStream bis;
+ try {
+ fis = new FileInputStream(file);
+ bis = new BufferedInputStream(fis);
+ byte[] b = new byte[4];
+ bis.read(b);
+ int width = byteArrayToInt(b);
+ bis.read(b);
+ int height = byteArrayToInt(b);
+ bis.read(b);
+ int type = byteArrayToInt(b);
+ BufferedImage image = new BufferedImage(width, height, type);
+ for(int x = 0; x<width; x++) {
+ for(int y = 0; y<height; y++) {
+ bis.read(b);
+ image.setRGB(x, y, byteArrayToInt(b));
+ }
+ }
+ bis.close();
+ fis.close();
+ return image;
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private void writeImage(File file, BufferedImage image) {
+ FileOutputStream fos;
+ BufferedOutputStream bos;
+ try {
+ fos = new FileOutputStream(file);
+ bos = new BufferedOutputStream(fos);
+ bos.write(intToByteArray(image.getWidth()));
+ bos.write(intToByteArray(image.getHeight()));
+ bos.write(intToByteArray(image.getType()));
+ for(int x = 0, width = image.getWidth(); x<width; x++) {
+ for(int y = 0, height = image.getHeight(); y<height; y++) {
+ bos.write(intToByteArray(image.getRGB(x, y)));
+ }
+ }
+ bos.close();
+ fos.close();
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException 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)
+ };
+ }
+
+}
diff --git a/src/aufgaben/imageio/ImageType.java b/src/aufgaben/imageio/ImageType.java
new file mode 100755
index 0000000..cfecabe
--- /dev/null
+++ b/src/aufgaben/imageio/ImageType.java
@@ -0,0 +1,8 @@
+package aufgaben.imageio;
+
+public class ImageType {
+
+ public static final byte RAW = 0x00;
+ public static final byte COMPRESSED = 0x01;
+
+}
diff --git a/src/aufgaben/imageio/Komprimierung1.java b/src/aufgaben/imageio/Komprimierung1.java
new file mode 100755
index 0000000..2265560
--- /dev/null
+++ b/src/aufgaben/imageio/Komprimierung1.java
@@ -0,0 +1,225 @@
+package aufgaben.imageio;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.image.BufferedImage;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+
+import javax.imageio.ImageIO;
+import javax.swing.JButton;
+import javax.swing.JPanel;
+import javax.swing.JSpinner;
+import javax.swing.JTextField;
+import javax.swing.SpinnerNumberModel;
+
+import aufgaben.Aufgabe;
+import misc.CompressedImage;
+import misc.Stapel;
+import misc.Utils;
+
+public class Komprimierung1 extends Aufgabe {
+
+ private final int width = 500, height = 500;
+ private boolean done = false;
+ private String name;
+ private JPanel panel;
+ private JTextField inPath, outPath;
+ private JButton readButton, writeButton, compButton;
+ private JSpinner spinnerWidth, spinnerHeight, spinnerColors;
+ private BufferedImage bild, towrite;
+ private CompressedImage compressed;
+ private ArrayList<Byte[]> compData;
+
+ public Komprimierung1() {
+ name = "Komprimierung 1";
+ this.setSize(width, height);
+ this.setTitle(name);
+ this.addWindowListener(
+ new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent e) {
+ System.out.println("close");
+ done = true;
+ }
+ }
+ );
+ this.setLayout(null);
+ }
+
+ @Override
+ public boolean done() {
+ return done;
+ }
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public void init() {
+ inPath = new JTextField();
+ inPath.setBounds(20, 40, 150, 20);
+ outPath = new JTextField();
+ outPath.setBounds(20, 70, 150, 20);
+ readButton = new JButton("Lesen");
+ readButton.setBounds(180, 40, 100, 20);
+ readButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ //bild = readImage(new File(inPath.getText()));
+ try {
+ bild = ImageIO.read(new File("title.png"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ towrite = new BufferedImage(bild.getWidth(), bild.getHeight(), bild.getType());
+ for(int x = 0, width = towrite.getWidth(); x<width; x++) {
+ for(int y = 0, height = towrite.getHeight(); y<height; y++) {
+ towrite.setRGB(x, y, bild.getRGB(x, y));
+ }
+ }
+
+ panel.repaint();
+ }
+ });
+ writeButton = new JButton("Schreiben");
+ writeButton.setBounds(180, 70, 100, 20);
+ writeButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ writeImage(new File(outPath.getText()));
+ }
+ });
+ spinnerWidth = new JSpinner(new SpinnerNumberModel(32, 1, 4096, 1));
+ spinnerWidth.setBounds(290, 40, 50, 20);
+ spinnerHeight = new JSpinner(new SpinnerNumberModel(32, 1, 4096, 1));
+ spinnerHeight.setBounds(350, 40, 50, 20);
+ spinnerColors = new JSpinner(new SpinnerNumberModel(8, 1, 4096, 1));
+ spinnerColors.setBounds(410, 40, 50, 20);
+ compButton = new JButton("Komprimieren");
+ compButton.setBounds(290, 70, 120, 20);
+ compButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ compressed = new CompressedImage(bild, (int)spinnerWidth.getValue(), (int)spinnerHeight.getValue(), (int)spinnerColors.getValue());
+ towrite = compressed.getImage();
+ panel.repaint();
+ }
+ });
+
+ panel = new JPanel() {
+ public void paint(Graphics g) {
+ if(towrite != null)
+ g.drawImage(towrite, 0, 0, 500, 390, this);
+ }
+ };
+ panel.setBounds(0, 110, 500, 390);
+ this.add(inPath);
+ this.add(outPath);
+ this.add(readButton);
+ this.add(writeButton);
+ this.add(spinnerWidth);
+ this.add(spinnerHeight);
+ this.add(spinnerColors);
+ this.add(compButton);
+ this.add(panel);
+ this.repaint();
+ }
+
+ private BufferedImage getGreyImage(BufferedImage i) {
+ BufferedImage image = new BufferedImage(i.getWidth(), i.getHeight(), i.getType());
+ for(int x = 0, width = i.getWidth(); x<width; x++) {
+ for(int y = 0, height = i.getHeight(); y<height; y++) {
+ Color c = new Color(i.getRGB(x, y));
+ int h = (int)(((double)(c.getRed()+c.getGreen()+c.getBlue())/765)*255);
+ image.setRGB(x, y, new Color(h, h, h).getRGB());
+ }
+ }
+ return image;
+ }
+
+ private BufferedImage readImage(File file) {
+ FileInputStream fis;
+ BufferedInputStream bis;
+ try {
+ fis = new FileInputStream(file);
+ bis = new BufferedInputStream(fis);
+ byte[] b = new byte[1];
+ bis.read(b);
+ switch(b[0]) {
+ case ImageType.RAW:
+
+ break;
+ case ImageType.COMPRESSED:
+ break;
+ }
+ int width = Utils.byteArrayToInt(b);
+ bis.read(b);
+ int height = Utils.byteArrayToInt(b);
+ bis.read(b);
+ int type = Utils.byteArrayToInt(b);
+ BufferedImage image = new BufferedImage(width, height, type);
+ for(int x = 0; x<width; x++) {
+ for(int y = 0; y<height; y++) {
+ bis.read(b);
+ image.setRGB(x, y, Utils.byteArrayToInt(b));
+ }
+ }
+ bis.close();
+ fis.close();
+ return image;
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private void writeImage(File file) {
+ FileOutputStream fos;
+ BufferedOutputStream bos;
+ try {
+ fos = new FileOutputStream(file);
+ bos = new BufferedOutputStream(fos);
+ if(compData == null) {
+ bos.write(ImageType.RAW);
+ } else {
+ bos.write(ImageType.COMPRESSED);
+ }
+ if(compData == null) {
+ bos.write(Utils.intTobyteArray(towrite.getWidth()));
+ bos.write(Utils.intTobyteArray(towrite.getHeight()));
+ bos.write(Utils.intTobyteArray(towrite.getType()));
+ for(int x = 0, width = towrite.getWidth(); x<width; x++) {
+ for(int y = 0, height = towrite.getHeight(); y<height; y++) {
+ bos.write(Utils.intTobyteArray(towrite.getRGB(x, y)));
+ }
+ }
+ } else {
+ bos.write(compressed.getData());
+ }
+ bos.close();
+ fos.close();
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}