How to resolve the algorithm Bitmap/Write a PPM file step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitmap/Write a PPM file step by step in the Ada programming language

Table of Contents

Problem Statement

Using the data storage type defined on this page for raster images, write the image to a PPM file (binary P6 preferred). (Read the definition of PPM file on Wikipedia.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Write a PPM file step by step in the Ada programming language

Source code in the ada programming language

with Ada.Characters.Latin_1;
with Ada.Streams.Stream_IO;  use Ada.Streams.Stream_IO;

procedure Put_PPM (File : File_Type; Picture : Image) is
   use Ada.Characters.Latin_1;
   Size   : constant String := Integer'Image (Picture'Length (2)) & Integer'Image (Picture'Length (1));
   Buffer : String (1..Picture'Length (2) * 3);
   Color  : Pixel;
   Index  : Positive;
begin
   String'Write (Stream (File), "P6" & LF);
   String'Write (Stream (File), Size (2..Size'Last) & LF);
   String'Write (Stream (File), "255" & LF);
   for I in Picture'Range (1) loop
      Index := Buffer'First;
      for J in Picture'Range (2) loop
         Color := Picture (I, J);
         Buffer (Index)     := Character'Val (Color.R);
         Buffer (Index + 1) := Character'Val (Color.G);
         Buffer (Index + 2) := Character'Val (Color.B);
         Index := Index + 3;
      end loop;
      String'Write (Stream (File), Buffer);
   end loop;
   Character'Write (Stream (File), LF);
end Put_PPM;


  

You may also check:How to resolve the algorithm Variables step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Phix programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the Quackery programming language
You may also check:How to resolve the algorithm Number names step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Delegates step by step in the Clojure programming language