diff --git a/de/darkress/pixelfood/Main.java b/de/darkress/pixelfood/Main.java index bab5e36..00564a8 100644 --- a/de/darkress/pixelfood/Main.java +++ b/de/darkress/pixelfood/Main.java @@ -1,45 +1,79 @@ package de.darkress.pixelfood; import javax.imageio.ImageIO; +import javax.sound.sampled.Port; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; +import java.util.ArrayList; class Main { - private final int PORT = 1234; - private final String HOSTNAME = "localhost"; - private byte[] message; + private static BufferedImage processImage(String imageFile) { + BufferedImage image; + try { + File file = new File(imageFile); + image = ImageIO.read(file); + return image; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private static ArrayList prepareArray(BufferedImage image) { + ArrayList pixelArray = new ArrayList<>(); + + int width = image.getWidth(); + int height = image.getHeight(); + + // Iterate over each pixel + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + // Get the RGBA value of the pixel + int pixel = image.getRGB(x, y); + + // Extract individual color components + int alpha = (pixel >> 24) & 0xFF; + int red = (pixel >> 16) & 0xFF; + int green = (pixel >> 8) & 0xFF; + int blue = pixel & 0xFF; + String rgbValue = String.format("%06X", (pixel & 0xFFFFFF)); + String messageParameters = String.format("PX %s %s %s\n", x, y, rgbValue); + if(alpha != 0) + { + pixelArray.add(messageParameters); + } + + // Print RGBA values + System.out.println("Pixel at (" + x + ", " + y + "):"); + System.out.println(rgbValue); + } + } + return pixelArray; + } public static void main(String[] args) { + final int PORT = 1234; + final String HOSTNAME = "localhost"; + InetAddress serverAddress = null; try { - // Read the PNG file - File file = new File("uc.png"); - BufferedImage image = ImageIO.read(file); + serverAddress = InetAddress.getByName(HOSTNAME); + } catch(UnknownHostException e) { + e.printStackTrace(); + } - // Get image dimensions - int width = image.getWidth(); - int height = image.getHeight(); + // Read the PNG file + BufferedImage image = processImage("girl.png"); + ArrayList pixelArray = prepareArray(image); - // Iterate over each pixel - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - // Get the RGBA value of the pixel - int pixel = image.getRGB(x, y); - - // Extract individual color components - int alpha = (pixel >> 24) & 0xFF; - int red = (pixel >> 16) & 0xFF; - int green = (pixel >> 8) & 0xFF; - int blue = pixel & 0xFF; - - // Print RGBA values - /*System.out.println("Pixel at (" + x + ", " + y + "):"); - System.out.println(" Alpha: " + alpha); - System.out.println(" Red: " + red); - System.out.println(" Green: " + green); - System.out.println(" Blue: " + blue);*/ - System.out.println(Integer.toHexString(pixel)); + try { + DatagramSocket socket = new DatagramSocket(); + while(true) { + for(int i = 0; i < pixelArray.size(); i++) { + byte[] messageBytes = pixelArray.get(0).getBytes(); + DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, serverAddress, PORT); + socket.send(packet); } } } catch (IOException e) {