This commit is contained in:
AlgaLaptop
2025-12-29 00:09:11 +01:00
parent 608853fcfe
commit f6f0eca79f
6 changed files with 149 additions and 26 deletions
+109 -2
View File
@@ -1,17 +1,124 @@
package fr.iutfbleau.sae.mpif;
import fr.iutfbleau.sae.mimage.RGBImage;
import fr.iutfbleau.sae.util.BitOutputStream;
import java.io.FileOutputStream;
import java.util.Map;
public class PIFWriter {
// Ecriture de l'en-tête du fichier PIF (largeur et hauteur)
public void writeHeader(BitOutputStream out,int width, int height){
try {
out.writeBits(width >> 8 & 0xFF, 8); // octet de poids fort
out.writeBits(width & 0xFF, 8); // octet de poids faible
out.writeBits(height >> 8 & 0xFF, 8); // octet de poids fort
out.writeBits(height & 0xFF, 8); // octet de poids faible
} catch (Exception e) {
System.err.println("Erreur lors de l’écriture de len-tête du fichier PIF");
}
}
public void writeTables(BitOutputStream out, Map<Integer, String> canonicalCodes){
public void writeTables(BitOutputStream out, Map<Integer, String> canonR,
Map<Integer, String> canonG, Map<Integer, String> canonB){
try {
// Écriture des longueurs des codes canoniques pour chaque composante
for (int i = 0; i < 10; i++) {
int len;
if (canonR.containsKey(i)) { // petite securité (au cas où)
len = canonR.get(i).length();
}else {
len = 0;
}
out.writeBits(len, 8);
}
for (int i = 0; i < 10; i++) {
int len;
if (canonG.containsKey(i)) {
len = canonG.get(i).length();
}else {
len = 0;
}
out.writeBits(len, 8);
}
for (int i = 0; i < 10; i++) {
int len;
if (canonB.containsKey(i)) {
len = canonB.get(i).length();
}else {
len = 0;
}
out.writeBits(len, 8);
}
} catch (Exception e) {
System.err.println("Erreur lors de l’écriture des tables de fréquences dans le fichier PIF");
}
}
private void writeBitFromString(BitOutputStream out, String code){
try {
for (int i = 0; i < code.length(); i++) {
if (code.charAt(i) == '1') {
out.writeBit(1);
} else {
out.writeBit(0);
}
}
} catch (Exception e) {
System.err.println("Erreur lors de l’écriture des bits dans le fichier PIF");
}
}
// Méthode pour encoder les pixels de l'image en utilisant les codes canoniques
public void encodePixels(BitOutputStream out, RGBImage image, Map<Integer, String> canonRED, Map<Integer, String> canonGREEN, Map<Integer, String> canonBLUE){
try {
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Récupérer les valeurs R, G, B du pixel
int r = image.getPixel(x, y).getR();
int g = image.getPixel(x, y).getG();
int b = image.getPixel(x, y).getB();
// Écrire les codes dans le flux binaire
writeBitFromString(out, canonRED.get(r));
writeBitFromString(out, canonGREEN.get(g));
writeBitFromString(out, canonBLUE.get(b));
}
}
} catch (Exception e) {
System.err.println("Erreur lors de lencodage des pixels dans le fichier PIF");
}
}
public void encodePixels(BitOutputStream out){
public void writeTOFile(String filepath, RGBImage image,
Map<Integer,String> canonR,
Map<Integer,String> canonG,
Map<Integer,String> canonB)
throws Exception {
// Création du flux de sortie binaire
FileOutputStream fos =new FileOutputStream(filepath);
BitOutputStream ecriveur =new BitOutputStream(fos);
// Écriture de l'en-tête
writeHeader(ecriveur, image.getWidth(), image.getHeight());
// Écriture des tables de longueurs des codes canoniques
writeTables(ecriveur, canonR, canonG, canonB);
// Ecriture des pixels
encodePixels(ecriveur, image, canonR, canonG, canonB);
ecriveur.fermerFlux();
System.err.println("SYSTEME");
}
}