Files
PixelFloodClient/de/darkress/pixelfood/Main.java
Darkress be0ad9e905 offset
2023-06-09 05:05:01 +02:00

86 lines
2.8 KiB
Java

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 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<String> prepareArray(BufferedImage image, int xOffset, int yOffset) {
ArrayList<String> 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+xOffset, y+yOffset, 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";
final int xOffset = 0;
final int yOffset = 0;
InetAddress serverAddress = null;
try {
serverAddress = InetAddress.getByName(HOSTNAME);
} catch(UnknownHostException e) {
e.printStackTrace();
}
// Read the PNG file
BufferedImage image = processImage("girl.png");
ArrayList<String> pixelArray = prepareArray(image, xOffset, yOffset);
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) {
e.printStackTrace();
}
}
}