Files
PixelFloodClient/de/darkress/pixelfood/Main.java
2023-06-09 19:07:12 +02:00

83 lines
2.7 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;
}
//Syntax blabla.jar x y Hostname Port Image.png
public static void main(String[] args) {
final int PORT = Integer.parseInt(args[3]);;
final String HOSTNAME = args[2];
final int xOffset = Integer.parseInt(args[0]);
final int yOffset = Integer.parseInt(args[1]);
final String imageName = args[4];
// Read the PNG file
BufferedImage image = processImage(imageName);
ArrayList<String> pixelArray = prepareArray(image, xOffset, yOffset);
try {
Socket socket = new Socket(HOSTNAME, PORT);
//PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true) {
for(int i = 0; i < pixelArray.size(); i++) {
//out.println(pixelArray.get(i));
byte[] payload = pixelArray.get(i).getBytes();
socket.getOutputStream().write(payload);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}