Modifications apportés
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
|
||||
public class DecodeNode {
|
||||
public DecodeNode left;
|
||||
public DecodeNode right;
|
||||
public Integer value; // null si pas une feuille
|
||||
|
||||
public DecodeNode() {
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
public DecodeNode(DecodeNode left, DecodeNode right, Integer value) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isLeaf() {
|
||||
return this.left == null && this.right == null;
|
||||
}
|
||||
}
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
|
||||
public class DecodeNode {
|
||||
public DecodeNode left;
|
||||
public DecodeNode right;
|
||||
public Integer value; // null si pas une feuille
|
||||
|
||||
public DecodeNode() {
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
public DecodeNode(DecodeNode left, DecodeNode right, Integer value) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isLeaf() {
|
||||
return this.left == null && this.right == null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,82 @@
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
|
||||
import fr.iutfbleau.sae.util.BitInputStream;
|
||||
import fr.iutfbleau.sae.util.BitOutputStream;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import fr.iutfbleau.sae.mimage.RGBImage;
|
||||
|
||||
|
||||
public class PIFReader {
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private int[] lenR;
|
||||
private int[] lenG;
|
||||
private int[] lenB;
|
||||
|
||||
public RGBImage read(String filepath)
|
||||
throws Exception {
|
||||
FileInputStream fis = new FileInputStream(filepath);
|
||||
BitInputStream lecteur = new BitInputStream(fis);
|
||||
|
||||
// je lis l'entête et les tables canoniques
|
||||
this.readHeader(lecteur);
|
||||
this.readCanonicalTables(lecteur);
|
||||
|
||||
// je reconstructe les tables canoniques car dans le fichier on a juste les longueurs en bits
|
||||
Map<String, Integer> canonR = rebuildCanonical(lenR);
|
||||
Map<String, Integer> canonG = rebuildCanonical(lenG);
|
||||
Map<String, Integer> canonB = rebuildCanonical(lenB);
|
||||
|
||||
DecodeNode trieR = buildDecodageTree(canonR);
|
||||
DecodeNode trieG = buildDecodageTree(canonG);
|
||||
DecodeNode trieB = buildDecodageTree(canonB);
|
||||
|
||||
RGBImage img = decodePixels(lecteur, trieR, trieG, trieB);
|
||||
|
||||
lecteur.closeFlux();
|
||||
return img;
|
||||
}
|
||||
|
||||
public void readHeader(BitInputStream in) {
|
||||
// TODO: Implement header reading
|
||||
}
|
||||
|
||||
public void readCanonicalTables(BitInputStream in) {
|
||||
// TODO: Implement canonical table reading
|
||||
}
|
||||
|
||||
public Map<String,Integer> rebuildCanonical(int[] lengths) {
|
||||
// TODO: Implement canonical table reconstruction
|
||||
return null;
|
||||
}
|
||||
|
||||
public RGBImage decodePixels(BitInputStream in, DecodeNode red, DecodeNode green, DecodeNode blue) {
|
||||
// TODO: Implement pixel decoding
|
||||
return null;
|
||||
}
|
||||
|
||||
public DecodeNode buildDecodageTree(Map<String,Integer> codes) {
|
||||
// TODO: Implement trie building
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
import fr.iutfbleau.sae.util.BitInputStream;
|
||||
import fr.iutfbleau.sae.util.BitOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
import fr.iutfbleau.sae.mimage.RGBImage;
|
||||
|
||||
|
||||
public class PIFReader {
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private int[] lenR;
|
||||
private int[] lenG;
|
||||
private int[] lenB;
|
||||
|
||||
public RGBImage read(String filepath)
|
||||
throws Exception {
|
||||
FileInputStream fis = new FileInputStream(filepath);
|
||||
BitInputStream lecteur = new BitInputStream(fis);
|
||||
|
||||
// je lis l'entête et les tables canoniques
|
||||
this.readHeader(lecteur);
|
||||
this.readCanonicalTables(lecteur);
|
||||
|
||||
// je reconstructe les tables canoniques car dans le fichier on a juste les longueurs en bits
|
||||
Map<String, Integer> canonR = rebuildCanonical(lenR);
|
||||
Map<String, Integer> canonG = rebuildCanonical(lenG);
|
||||
Map<String, Integer> canonB = rebuildCanonical(lenB);
|
||||
|
||||
DecodeNode trieR = buildDecodageTree(canonR);
|
||||
DecodeNode trieG = buildDecodageTree(canonG);
|
||||
DecodeNode trieB = buildDecodageTree(canonB);
|
||||
|
||||
RGBImage img = decodePixels(lecteur, trieR, trieG, trieB);
|
||||
|
||||
lecteur.closeFlux();
|
||||
return img;
|
||||
}
|
||||
|
||||
public void readHeader(BitInputStream in) {
|
||||
// La largeur et l'hauteur de l'image occupe chaqun deux octets soit 16 bits :
|
||||
this.width = in.readBits(16);
|
||||
this.height = in.readBits(16);
|
||||
}
|
||||
|
||||
public void readCanonicalTables(BitInputStream in) {
|
||||
this.lenR = new int[256];
|
||||
this.lenG = new int[256];
|
||||
this.lenB = new int[256];
|
||||
|
||||
for (int i = 0; i < 256; i++){
|
||||
lenR[i] = in.readBits(8);
|
||||
}
|
||||
for (int j = 0; j < 256; j++){
|
||||
lenG[j] = in.readBits(8);
|
||||
}
|
||||
|
||||
for (int k = 0; k < 256; k++){
|
||||
lenB[k] = in.readBits(8);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String,Integer> rebuildCanonical(int[] lengths) {
|
||||
// TODO: Implement canonical table reconstruction
|
||||
return null;
|
||||
}
|
||||
|
||||
public RGBImage decodePixels(BitInputStream in, DecodeNode red, DecodeNode green, DecodeNode blue) {
|
||||
// TODO: Implement pixel decoding
|
||||
return null;
|
||||
}
|
||||
|
||||
public DecodeNode buildDecodageTree(Map<String,Integer> codes) {
|
||||
// TODO: Implement trie building
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,126 +1,126 @@
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
|
||||
import fr.iutfbleau.sae.mimage.RGBImage;
|
||||
import fr.iutfbleau.sae.util.BitOutputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class PIFWriter {
|
||||
|
||||
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);
|
||||
BufferedOutputStream bos =new BufferedOutputStream(fos);
|
||||
BitOutputStream ecriveur =new BitOutputStream(bos);
|
||||
|
||||
// É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");
|
||||
}
|
||||
|
||||
// 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 l’en-tête du fichier PIF");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 < 256; 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 < 256; i++) {
|
||||
int len;
|
||||
if (canonG.containsKey(i)) {
|
||||
len = canonG.get(i).length();
|
||||
}else {
|
||||
len = 0;
|
||||
}
|
||||
out.writeBits(len, 8);
|
||||
}
|
||||
for (int i = 0; i < 256; i++) {
|
||||
int len;
|
||||
if (canonB.containsKey(i)) {
|
||||
len = canonB.get(i).length();
|
||||
}else {
|
||||
len = 0;
|
||||
}
|
||||
out.writeBits(len, 8);
|
||||
}
|
||||
} catch (IOException 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 (IOException 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){
|
||||
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));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package fr.iutfbleau.sae.mpif;
|
||||
|
||||
import fr.iutfbleau.sae.mimage.RGBImage;
|
||||
import fr.iutfbleau.sae.util.BitOutputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class PIFWriter {
|
||||
|
||||
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);
|
||||
BufferedOutputStream bos =new BufferedOutputStream(fos);
|
||||
BitOutputStream ecriveur =new BitOutputStream(bos);
|
||||
|
||||
// É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");
|
||||
}
|
||||
|
||||
// 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 l’en-tête du fichier PIF");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 < 256; 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 < 256; i++) {
|
||||
int len;
|
||||
if (canonG.containsKey(i)) {
|
||||
len = canonG.get(i).length();
|
||||
}else {
|
||||
len = 0;
|
||||
}
|
||||
out.writeBits(len, 8);
|
||||
}
|
||||
for (int i = 0; i < 256; i++) {
|
||||
int len;
|
||||
if (canonB.containsKey(i)) {
|
||||
len = canonB.get(i).length();
|
||||
}else {
|
||||
len = 0;
|
||||
}
|
||||
out.writeBits(len, 8);
|
||||
}
|
||||
} catch (IOException 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 (IOException 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){
|
||||
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));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user