Advertisement
Etgadefait

re ada

Mar 28th, 2023
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 7.63 KB | None | 0 0
  1. --
  2. -- Programme à compléter
  3. --
  4. with GAda.Text_IO, GAda.Graphics, GAda.Plus, JPG;
  5. with Caracteres; use Caracteres;
  6.  
  7. procedure Mission35 is
  8.    
  9.    package Graph renames GAda.Graphics;
  10.    package Plus renames GAda.Plus;
  11.    package Tet renames GAda.Text_IO;
  12.    
  13.    ------------------------------------------ Version 1 de l'affichage
  14.    
  15.    type T_Qua is (Basse, Moyenne, Haute);
  16.    type T_Res is record
  17.       Long : Float;
  18.       Haut : Float;
  19.    end record;
  20.    
  21.    function Get_Char (Nuance : Integer ; Qualite : T_Qua) return Character is
  22.       -- Cette fonction utilise une liste déja triée de caractères et
  23.       -- va chercher le bon caractere avec un produit en croix
  24.       -- (-1 et +1 pour éviter des problemes d'index
  25.       Index_Ch : Integer := 1;
  26.       Ch_Ret : Character;
  27.       Encod_Bas : String := "# ";
  28.       Encod_Moy : String := "@B%8#O([koi+-.' ";
  29.       Encod_Hau : String := "@B%8&WM#*ZO0QLCJUYXhkbqwmzcunxr/\|()1{}[]?-_+~<>i!l;:,`'. ";
  30.    begin
  31.       case Qualite is
  32.      when Basse => Ch_Ret := Encod_Bas(1 + Integer(Float(Nuance)*(Float(Encod_Bas'Length-1))/255.0));
  33.      when Moyenne => Ch_Ret := Encod_Moy(1 + Integer(Float(Nuance)*(Float(Encod_Moy'Length-1))/255.0));
  34.      when Haute => Ch_Ret := Encod_Hau(1 + Integer(Float(Nuance)*(Float(Encod_Hau'Length-1))/255.0));
  35.       end case;
  36.       -- Nuance de 0 a 255, indexChar de 1 a longeur de l'encodage
  37.       return Ch_Ret;
  38.    end Get_Char;
  39.    
  40.    procedure Affiche_Ascii(Path : String ; Qualite : T_Qua ; FlipV, FlipH, FlipNEB : Boolean) is
  41.       Img : JPG.T_Image := JPG.Lire_Image(path);
  42.       Haut : Integer := Img'Length(1);
  43.       Larg : Integer := Img'Length(2);
  44.       LigneCor, ColCor : Integer;
  45.       CTemp : Graph.T_Couleur;
  46.       Nua_Temp : Integer;
  47.    begin
  48.       for Ligne in Img'Range(1) loop
  49.          if FlipV then
  50.             LigneCor := Haut - Ligne + 1;
  51.          else
  52.             LigneCor := Ligne;
  53.          end if;
  54.          for Colonne in Img'Range(2) loop
  55.             -- Récupère les valeurs RGB et prend en compte des flips
  56.             -- horizontal : lignes inversees, vertical : l'autre
  57.             if FlipH then
  58.                ColCor := Larg - Colonne + 1;
  59.             else
  60.                ColCor := Colonne;
  61.             end if;
  62.             CTemp := Img(LigneCor, ColCor);
  63.             Nua_Temp := (CTemp.Rouge + CTemp.Vert + CTemp.Bleu) / 3;
  64.             if FlipNEB then
  65.                Nua_Temp := 255 - Nua_Temp;
  66.             end if;
  67.             Tet.Put(Get_Char(Nua_Temp, Qualite));
  68.          end loop;
  69.          Tet.New_Line;
  70.       end loop;
  71.    end Affiche_Ascii;
  72.    
  73.    ------------------------------------------ Version 1.5 de l'affichage (redimmensionnement image)
  74.    
  75.    procedure Affiche_Ascii_Blocs(Path:String ; RecadL,RecadH:Integer ; Qualite:T_Qua ; FlipNEB:Boolean) is
  76.       Img : JPG.T_Image := JPG.Lire_Image(path);
  77.       ImgH : Integer := Img'Length(1);
  78.       ImgL : Integer := Img'Length(2);
  79.      
  80.       Xmin, Ymin : Integer;
  81.       BlocL : Integer := Integer(Float(ImgL)/Float(RecadL))-1;
  82.       BlocH : Integer := Integer(Float(ImgH)/Float(RecadH))-1;
  83.      
  84.       CTemp : Graph.T_Couleur;
  85.       Nua_Temp : Integer;
  86.       Compt : Integer;
  87.    begin
  88.       -- Pour tous les blocs (nouveaux pixels)
  89.       for Y in 1..RecadH loop
  90.          Ymin := ((Y-1) * BlocL ) + 1;
  91.          for X in 1..RecadL loop
  92.             Xmin := ((X-1) * BlocH) + 1;
  93.            
  94.             -- Calcule la lum moyenne du bloc
  95.         Nua_Temp := 0;
  96.         Compt := 0;
  97.             for Ylum in Ymin..Ymin+BlocH-1 loop
  98.                for Xlum in Xmin..Xmin+blocL-1 loop
  99.                   CTemp := Img(Ylum, Xlum);
  100.                   Nua_Temp := Nua_Temp + (CTemp.Rouge + CTemp.Vert + CTemp.Bleu) / 3;
  101.           Compt := Compt + 1;
  102.                end loop;
  103.             end loop;
  104.             Nua_Temp := Nua_Temp / Compt;
  105.             if FlipNEB then
  106.                Nua_Temp := 255 - Nua_Temp;
  107.             end if;
  108.            
  109.             -- Affiche le bon caractere
  110.             Tet.Put(Get_Char(Nua_Temp, Qualite));
  111.          end loop;
  112.      Tet.New_Line;
  113.       end loop;
  114.    end Affiche_Ascii_Blocs;
  115.    
  116.    ------------------------------------------ Version 2 (version "normale")
  117.    
  118.    type T_Dico is record
  119.       Car : Character;
  120.       Lum : Integer;
  121.    end record;
  122.    type T_Infos is array(Integer range <>) of T_Dico;
  123.    
  124.    procedure remplisInfos(Inf : in out T_Infos) is
  125.       lum_temp : Integer;
  126.       lum_max : Integer := 0;
  127.       lum_min : Integer := 255;
  128.    begin
  129.       for I in 1..Inf'length loop
  130.          -- Moyenne
  131.          lum_temp := 0;
  132.          for Y in 1..Table(I).Trame'length(1) loop
  133.             for X in 1..Table(I).Trame'length(2) loop
  134.                case Table(I).Trame(Y,X) is
  135.                   when Allume => lum_temp := lum_temp + 1;
  136.                   when Eteint => null;
  137.                end case;
  138.             end loop;
  139.          end loop;
  140.          lum_temp := integer( Float(Lum_Temp)*255.0 / Float(Table(I).Trame'length(1)*Table(I).Trame'length(2)) );
  141.          
  142.          -- Min-max pour apres
  143.          if lum_temp < lum_min then
  144.             lum_min := lum_temp;
  145.         Tet.Put_Line("min " & Lum_Min'Image);
  146.          elsif lum_temp > lum_max then
  147.             lum_max := lum_temp;
  148.         Tet.Put_Line("max " & Lum_Max'Image);
  149.          end if;
  150.          
  151.          -- On complete petit a petit Inf
  152.          Inf(I).Car := Table(I).Car;
  153.          Inf(I).Lum := lum_temp;
  154.       end loop;
  155.       Tet.Put_Line(Lum_Min'Image & " et " & Lum_Max'Image);
  156.       -- On normalise / fait en sorte que tous vont de 0 a 255
  157.       for I in 1..Inf'length loop
  158.          Inf(I).Lum := Integer(  float(Inf(I).Lum) * (255.0 / float(lum_max - lum_min))  );
  159.       end loop;
  160.      
  161.       -- Pas besoin de trier dans tous les cas on ira chercher le meilleur sur toute la liste
  162.    end RemplisInfos;
  163.    
  164.    function Get_Char2(lum_voulu : Integer ; Infos : T_Infos) return Character is
  165.       DeltaMin : Integer := 9999;
  166.       IndexMin : Integer := 35;
  167.    begin
  168.       for I in 1..Infos'Length loop
  169.          -- Minimum du delta donc recherche du plus proche
  170.          if abs(Infos(I).Lum - Lum_Voulu) < DeltaMin then
  171.             DeltaMin := Infos(I).Lum;
  172.             IndexMin := I;
  173.          end if;
  174.      Tet.Put_Line(Integer'image(Infos(I).Lum - Lum_Voulu));
  175.       end loop;
  176.      
  177.       return Infos(IndexMin).Car;
  178.    end Get_Char2;
  179.    
  180.    procedure Affiche2(Path:String ; FlipNEB:Boolean ; Infos:T_Infos) is
  181.       Img : JPG.T_Image := JPG.Lire_Image(path);
  182.       ImgH : Integer := Img'Length(1);
  183.       ImgL : Integer := Img'Length(2);
  184.      
  185.       CTemp : Graph.T_Couleur;
  186.       Nua_Temp : Integer;
  187.    begin
  188.       for Y in 1..Img'Length(1)-1 loop
  189.          for X in 1..Img'Length(2)-1 loop
  190.             CTemp := Img(Y,X);
  191.             Nua_Temp := (CTemp.Rouge + CTemp.Vert + CTemp.Bleu) / 3;
  192.             if FlipNEB then
  193.                Nua_Temp := 255 - Nua_Temp;
  194.             end if;
  195.             Tet.Put(Get_Char2(Nua_Temp, Infos));
  196.          end loop;
  197.      Tet.New_Line;
  198.       end loop;
  199.      
  200.    end Affiche2;
  201.    
  202.    -- trop chiant de devoir le mettre la paske monsieur veut pas alors qu'il le fait aussi
  203.    Infos : T_Infos(1..Nombre_De_Caracteres);
  204.    
  205. begin
  206.    --Affiche_Ascii("/home/gadefait/1A-Algo-S2/algo2/logoBD.jpg", Haute, False, False, True);
  207.    --Affiche_Ascii_blocs("/home/gadefait/1A-Algo-S2/algo2/logo.jpg", 30, 30, Haute, False);
  208.    
  209.    RemplisInfos(Infos);
  210.    Tet.Put(Infos(34).Car & Get_Char2(Infos(34).Lum, Infos));
  211.    --Affiche2("/home/gadefait/1A-Algo-S2/algo2/logoBD.jpg", True, Infos);
  212.    
  213. end Mission35;
  214.  
  215.  
  216. -- @B%8&WM#*ZO0QLCJUYXhkbqwmzcunxr/\|()1{}[]?-_+~<>i!l;:,"`'. 59 char encoding
  217. -- @B%8#O([koi+-.' 16 char encoding
  218. -- # 2 char encoding
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement