Image processing in Ada (v) โ the main program
16 February 2023 at 17:47
The last piece of the program binds the packages together. Here we read in the filename using the procedure getfilename()
, and the main program which provides a menu based system to access all the subprograms in the prorgam.
with ada.Text_IO; use Ada.Text_IO; with ada.strings.unbounded; use ada.strings.unbounded; with ada.strings.unbounded.Text_IO; use ada.strings.unbounded.Text_IO; with ada.directories; use ada.directories; with imagestr; use imagestr; with imagepgm; use imagepgm; with imageprocess; use imageprocess; procedure image is fnameI, fnameO : unbounded_string; img0 : imagep; opt : character; buf : unbounded_string; lb,ub : float; bit : integer; -- Procedure to procedure getfilename(fname: out unbounded_string; t: in character) is res : character; buf : unbounded_string; begin if t = 'r' then loop put_line("Enter PGM filename: "); get_line(fname); exit when exists(to_string(fname)); end loop; elsif t = 'w' then loop put_line("Enter PGM filename: "); get_line(fname); if exists(to_string(fname)) then put_line("Overwrite? (y/n):"); buf := get_line; res := element(buf,1); if res = 'y' then exit; end if; else exit; end if; end loop; end if; end getfilename; begin loop put_line(" Image Processing"); new_line; put_line(" 1. Read in PGM image from file"); put_line(" 2. Apply image invertion"); put_line(" 3. Apply LOG function"); put_line(" 4. Apply contrast stretching"); put_line(" 5. Apply histogram equalization"); put_line(" 6. Write PGM image to file"); put_line(" 7. Apply reduce grays"); put_line(" 8. Quit"); put_line(" Choice? "); buf := get_line; opt := element(buf,1); case opt is when '1' => getfilename(fnameI,'r'); readpgm(img0,fnameI); when '2' => imageinv(img0); when '3' => imagelog(img0); when '4' => put_line("lower bound:"); buf := get_line; lb := float'value(to_string(buf)); put_line("upper bound:"); buf := get_line; ub := float'value(to_string(buf)); imagestretch(img0,lb,ub); when '5' => histequal(img0); when '6' => getfilename(fnameO,'w'); writepgm(img0,fnameO); when '7' => put_line("bit value (1-8):"); buf := get_line; bit := integer'value(to_string(buf)); reducegrays(img0,bit); when others => exit; end case; end loop; end image;
The getfilename()
, will get a user-input name for a file, based on whether it is for reading or writing. For a file that is to be read, the procedure will continue looping until a valid filename is input. For writing, if the file already exists the user is prompted to indicate whether or not they want the file overwritten.