package sig; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.regex.Pattern; import javax.imageio.ImageIO; import javax.swing.JFrame; import java.awt.Toolkit; import sig.engine.Panel; import java.awt.AWTException; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.Robot; import java.awt.Graphics2D; public class SigShare { static Robot r; public static final String PROGRAM_NAME="SigShare"; public static void main(String[] args) throws AWTException { r = new Robot(); if (args.length==2&&args[1].equalsIgnoreCase("server")) { ServerSocket socket; try { socket = new ServerSocket(4191); System.out.println("Listening on port 4191."); try (Socket client = socket.accept()) { System.out.println("New client connection detected: "+client.toString()); System.out.println("Sending initial data..."); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(),"ISO-8859-1")); DataOutputStream clientOutput = new DataOutputStream(client.getOutputStream()); double SCREEN_MULT=2; int SCREEN_WIDTH=(int)(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getWidth()/SCREEN_MULT); int SCREEN_HEIGHT=(int)(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getHeight()/SCREEN_MULT); int[] pixels = new int[SCREEN_WIDTH*SCREEN_HEIGHT]; clientOutput.write(("DESKTOP "+SCREEN_WIDTH+" "+SCREEN_HEIGHT+"\r\n").getBytes()); System.out.println("Send initial screen"); //char[] screen = new char[SCREEN_WIDTH*SCREEN_HEIGHT]; BufferedImage screenshot = CaptureScreen(SCREEN_WIDTH,SCREEN_HEIGHT); for (int y=0;y>>16)/8; int g = ((col&0x0000FF00)>>>8)/8; int b = ((col&0x000000FF))/8; char compressedCol=(char)((r<<10)+(g<<5)+b); clientOutput.writeChar(compressedCol); //screen[y*SCREEN_WIDTH+x]=compressedCol; } } System.out.println("Begin diff monitoring..."); int frame=0; while (true) { screenshot = CaptureScreen(SCREEN_WIDTH,SCREEN_HEIGHT); for (int y=0;y>>8)+((y&0xF)<<4)); //bits 9-12 for x, bits 1-4 for y b3=(byte)((y>>>4)&0xFF);//bits 5-12 for y pixels[y*SCREEN_WIDTH+x]=col; int r = ((col&0x00FF0000)>>>16)/8; int g = ((col&0x0000FF00)>>>8)/8; int b = ((col&0x000000FF))/8; char compressedCol=(char)((r<<10)+(g<<5)+b); clientOutput.writeChar(compressedCol); clientOutput.writeByte(b1); clientOutput.writeByte(b2); clientOutput.writeByte(b3); System.out.println(" Pixel ("+x+","+y+") "+b1+"/"+(b2&0xF)+"/"+((b2&0xF0)>>>4)+"/"+b3+" sent"); } //screen[y*SCREEN_WIDTH+x]=compressedCol; } } System.out.println("Frame "+frame+++" processed"); } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e1) { e1.printStackTrace(); } } else if (args.length==2&&args[1].equalsIgnoreCase("client")) { Socket socket; PrintWriter out; DataInputStream in; JFrame f = new JFrame(PROGRAM_NAME); Panel p = new Panel(f); try { socket = new Socket(args[0],4191); out = new PrintWriter(socket.getOutputStream(),true); in=new DataInputStream(socket.getInputStream()); while (true) { String line; if (in.available()>0) { line=in.readLine(); //System.out.println(line); if (line.contains("DESKTOP")) { String[] split = line.split(Pattern.quote(" ")); int SCREEN_WIDTH=Integer.parseInt(split[1]); int SCREEN_HEIGHT=Integer.parseInt(split[2]); p.init(SCREEN_WIDTH,SCREEN_HEIGHT); f.add(p); f.setSize(SCREEN_WIDTH,SCREEN_HEIGHT); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); int expectedChars = SCREEN_WIDTH*SCREEN_HEIGHT; System.out.println("Expecting "+(expectedChars)+" of data."); int arrayIndex = 0; while (expectedChars>0) { if (in.available()>0) { char col = in.readChar(); int convert = ((((col&0b0111110000000000)>>>10)*8)<<16)+ ((((col&0b0000001111100000)*8)>>>5)<<8)+ ((((col&0b0000000000011111))*8)); Panel.pixel[arrayIndex++]=convert; //System.out.println("Received "+col+" / "+convert); expectedChars--; } } p.render(); System.out.println("Initial image processed!"); int frame=0; while (true) { if (in.available()>0) { char col = in.readChar(); int convert = ((((col&0b0111110000000000)>>>10)*8)<<16)+ ((((col&0b0000001111100000)*8)>>>5)<<8)+ ((((col&0b0000000000011111))*8)); int b1=in.readUnsignedByte()&0xff,b2=in.readUnsignedByte()&0xff,b3=in.readUnsignedByte()&0xff; int x = b1+((b2&0xF)<<8); int y = (b3<<4)+((b2&0xF0)>>>4); System.out.println(" Pixel "+frame+++" ("+x+","+y+") "+b1+"/"+(b2&0xF)+"/"+((b2&0xF0)>>>4)+"/"+b3+" processed"); Panel.pixel[y*SCREEN_WIDTH+x]=convert; } } } } } } catch (IOException e) { e.printStackTrace(); } } else { System.err.println("Args: server|client"); return; } } private static BufferedImage CaptureScreen(int w,int h) throws IOException { BufferedImage screenshot = toBufferedImage(r.createScreenCapture(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()).getScaledInstance(w, h, Image.SCALE_DEFAULT)); ImageIO.write(screenshot,"jpg",new File("/home/niconiconii/screenshot.jpg")); return screenshot; } public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; } }