Skip to content

Instantly share code, notes, and snippets.

@hpwit
Created May 27, 2018 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpwit/51c4b71535b9d504849416c218a29825 to your computer and use it in GitHub Desktop.
Save hpwit/51c4b71535b9d504849416c218a29825 to your computer and use it in GitHub Desktop.
Stream bmp files
package com.company;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.io.*;
import java.text.*;
import java.util.concurrent.TimeUnit;
public class Main {
public static double gammaCorrection=1.8;
public static int tableBrightness=100;
public static void calculateGammaTable()
{
double correctGamma=1;Math.pow(tableBrightness/255,1/gammaCorrection);
correctGamma=1;
for (int i=0;i<256;i++)
{
double newValue=255*Math.pow((float)i/255,correctGamma*gammaCorrection);
//Serial.println(newValue);
gamma8[i]=(int)newValue;
//Serial.println(gamma8[i]);
newValue=255*Math.pow((float)i/255,correctGamma*(gammaCorrection+0.2));
gammar[i]=(int)newValue;
//Serial.println(gamma8[i]);
}
}
public final static int port = 100;
public static int gamma8[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,13,13,14,14,14,15,15,16,16,17,17,18,18,19,19,20,21,21,22,22,23,23,24,25,25,26,27,27,28,29,29,30,31,31,32,33,34,34,35,36,37,37,38,39,40,41,42,42,43,44,45,46,47,48,49,50,51,52,52,53,54,55,56,57,59,60,61,62,63,64,65,66,67,68,69,71,72,73,74,75,77,78,79,80,82,83,84,85,87,88,89,91,92,93,95,96,98,99,100,102,103,105,106,108,109,111,112,114,115,117,119,120,122,123,125,127,128,130,132,133,135,137,138,140,142,144,145,147,149,151,153,155,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,197,199,201,203,205,207,210,212,214,216,219,221,223,226,228,230,233,235,237,240,242,245,247,250,252,255,
};
public static int gammar[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,13,13,14,14,14,15,15,16,16,17,17,18,18,19,19,20,21,21,22,22,23,23,24,25,25,26,27,27,28,29,29,30,31,31,32,33,34,34,35,36,37,37,38,39,40,41,42,42,43,44,45,46,47,48,49,50,51,52,52,53,54,55,56,57,59,60,61,62,63,64,65,66,67,68,69,71,72,73,74,75,77,78,79,80,82,83,84,85,87,88,89,91,92,93,95,96,98,99,100,102,103,105,106,108,109,111,112,114,115,117,119,120,122,123,125,127,128,130,132,133,135,137,138,140,142,144,145,147,149,151,153,155,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,197,199,201,203,205,207,210,212,214,216,219,221,223,226,228,230,233,235,237,240,242,245,247,250,252,255,
};
public static String films[]={"kp2","medu","brit","kt","ky","ky2","ky3","gaga","tel","reine","rup","rup2","love","wonder","star","rachi","offer"};
public static int longueur[]={7680,186,4220,4816,4554,4282,4645,4308,9999,4659,4388,4196,1901,1938,8976,1266,5853};
public static int index=0;
public static void main(String[] args) {
calculateGammaTable();
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
DecimalFormat formater = new DecimalFormat("0000");
try (DatagramSocket server = new DatagramSocket(port)) {
//on envoie le code pour la luminosité
byte[] buffer2 = new byte[2];
buffer2[0]=(byte)255;
buffer2[1]=(byte)tableBrightness;
try {
DatagramPacket packet2 = new DatagramPacket(
buffer2, //Les données
2, //La taille des données
InetAddress.getByName("192.168.1.57"), //L'adresse de l'émetteur
port //Le port de l'émetteur
);
//Et on envoie vers l'émetteur du datagramme reçu précédemment
server.send(packet2);
packet2.setLength(buffer2.length);
}
catch(SocketException e){}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int image = 1; image < longueur[index]; image++) {
TimeUnit.MICROSECONDS.sleep(47500); // SECONDS.sleep(1/5);
File file = new File("/Users/yvesbazin/Downloads/testExteacgt/"+films[index]+"-" + formater.format(image) + ".bmp");
byte[] fileData = new byte[(int) file.length()];
try {
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();
int w = ((int) fileData[18] < 0) ? 256 + (int) fileData[18] - 1 : (int) fileData[18];
int w2 = ((int) fileData[19] < 0) ? 256 + (int) fileData[19] - 1 : (int) fileData[19];
//System.out.println("taille x:"+w);
//System.out.println("taille x:"+w2);
w = w2 * 256 + w;
int h = ((int) fileData[22] < 0) ? 256 + (int) fileData[22] - 1 : (int) fileData[22];
// System.out.println("taille x:" + w);
// System.out.println("taille y:" + h);
// System.out.println("offset:" + (fileData[10] + 256 * fileData[11]));
System.out.println(formater.format(image));
int offset = (int) (fileData[10] + 256 * fileData[11]);
//h=84;
int de = 4 - 3 * w % 4;
if (de == 4)
de = 0;
w = w * 3;
byte[] buffer = new byte[369 * 2 + 1];
//470 pixel opar packet
//16 packets => 16*369 (123*3)
for (int i = 0; i < 24; i++) {
for (int y = 2 * i; y < (2 * i + 2); y++) {
buffer[0] = (byte) i;
if (y % 2 == 0) {
for (int x = 0; x < w / 3; x++) {
if (i % 2 == 0) {
buffer[(y - 2 * i) * w + 3 * x + 1] = (byte) getgammar(fileData[y * w + 3 * x + 2 + y * de + offset]);
buffer[(y - 2 * i) * w + 3 * x + 2] = (byte) getgamma(fileData[y * w + 3 * x + 1 + y * de + offset]);
buffer[(y - 2 * i) * w + 3 * x + 3] = (byte) getgamma(fileData[y * w + 3 * x + y * de + offset]);
} else {
int offset2 = 0;
buffer[(y - 2 * i) * w + 3 * x + 1] = (byte) getgammar(fileData[y * w + 3 * x + 2 + y * de + offset]);
buffer[(y - 2 * i) * w + 3 * x + 2] = (byte) getgamma(fileData[y * w + 3 * x + 1 + y * de + offset]);
buffer[(y - 2 * i) * w + 3 * x + 3] = (byte) getgamma(fileData[y * w + 3 * x + y * de + offset]);
}
}
} else {
//System.out.println(y+":"+i+":"+(2 * w * ((int) Math.floor((y - 3 * i) / 2) + 1) - 1));
for (int x = 0; x < w / 3; x++) {
int offset2 = 0;
// if(i%2==0)
offset2 = 2 * w * ((int) Math.floor((y - 2 * i) / 2) + 1) - 3 - 3 * x;
// else
// offset2 = w * (y-i+1) - 3 - 3*x;
// System.out.println(y+":"+i+":"+offset2);
// offset2=3*offset2;
buffer[offset2 + 1] = (byte) getgammar(fileData[y * w + 3 * x + 2 + y * de + offset]);
buffer[offset2 + 2] = (byte) getgamma(fileData[y * w + 3 * x + 1 + y * de + offset]);
buffer[offset2 + 3] = (byte) getgamma(fileData[y * w + 3 * x + y * de + offset]);
//buffer[offset2 + 3] = (byte) getgamma(fileData[y * w + 3 * x + y * de + offset]);
}
}
}
try {
//Création de la connexion côté serveur, en spécifiant un port d'écoute
//while(true){
//On s'occupe maintenant de l'objet paquet
/* byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
//Cette méthode permet de récupérer le datagramme envoyé par le client
//Elle bloque le thread jusqu'à ce que celui-ci ait reçu quelque chose.
server.receive(packet);
//nous récupérons le contenu de celui-ci et nous l'affichons
String str = new String(packet.getData());
print("Reçu de la part de " + packet.getAddress()
+ " sur le port " + packet.getPort() + " : ");
println(str);
//On réinitialise la taille du datagramme, pour les futures réceptions
packet.setLength(buffer.length);*/
//et nous allons répondre à notre client, donc même principe
//byte[] buffer2 = new String("Réponse du serveur à ! ").getBytes();
DatagramPacket packet2 = new DatagramPacket(
buffer, //Les données
369 * 2 + 1, //La taille des données
InetAddress.getByName("192.168.1.57"), //L'adresse de l'émetteur
port //Le port de l'émetteur
);
//Et on envoie vers l'émetteur du datagramme reçu précédemment
server.send(packet2);
packet2.setLength(buffer.length);
// }
} catch (SocketException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
index =(index+1)%films.length;
e.printStackTrace();
}
}
//Lancement du serve
} catch (SocketException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
index =(index+1)%films.length;
}
}
public static int getgamma(int i)
{
int w = ((int) i < 0) ? 256 + (int)i - 1 : (int) i;
return gamma8[w];
}
public static int getgammar(int i)
{
int w = ((int) i < 0) ? 256 + (int)i - 1 : (int) i;
return gammar[w];
}
public static synchronized void print(String str){
System.out.print(str);
}
public static synchronized void println(String str){
System.err.println(str);
}
public static class UDPClient implements Runnable{
String name = "";
long sleepTime = 1000;
public UDPClient(String pName, long sleep){
name = pName;
sleepTime = sleep;
}
public void run(){
int nbre = 0;
while(true){
String envoi = name + "-" + (++nbre);
byte[] buffer = envoi.getBytes();
try {
//On initialise la connexion côté client
DatagramSocket client = new DatagramSocket();
//On crée notre datagramme
InetAddress adresse = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, adresse, port);
//On lui affecte les données à envoyer
packet.setData(buffer);
//On envoie au serveur
client.send(packet);
//Et on récupère la réponse du serveur
byte[] buffer2 = new byte[8196];
DatagramPacket packet2 = new DatagramPacket(buffer2, buffer2.length, adresse, port);
client.receive(packet2);
print(envoi + " a reçu une réponse du serveur : ");
println(new String(packet2.getData()));
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment