Reading image files with VHDL part 3

[Having written a function](/node/66/) to read a PGM image, how do we use it?

Well, the testbench at the bottom of the pgm vhdl file has an example:
[vhdl]
variable i : pixel_array_ptr;
— Without the transpose function, we would have to present the initialisation data in a non-intuitive way.
constant testdata : pixel_array(0 to 7, 0 to 3) := transpose(pixel_array'(
(000, 027, 062, 095, 130, 163, 198, 232),
(000, 000, 000, 000, 000, 000, 000, 000),
(255, 255, 255, 255, 255, 255, 255, 255),

Reading image files with VHDL part 2

Having [set up a library](/node/65/) for reading images, let’s now go on to read an image in!

pgm_read
========

Recall, we have a function:
[vhdl]
impure function pgm_read (filename : string)
return pixel_array_ptr;
[/vhdl]

So, some declarations – fairly self-explanatory:
[vhdl]
file pgmfile : text;
variable width, height : coordinate; — storage for image dimensions
variable l : line; — buffer for a line of text
variable s : string(1 to 2); — to check the P2 header

Reading image files with VHDL part 1 (again)

Data storage
============
One things VHDL can do ([contrary to popular belief :)](/node/60/)) is 2-D arrays. So reading images into a 2-D array is a very natural way to store the data.

We’ll create a package called `pgm` to keep all our image reading and writing code together:

[vhdl]
package pgm is
subtype coordinate : natural;
subtype pixel is integer range 0 to 255;
type pixel_array is array (coordinate range <>, coordinate range <>) of pixel;
type pixel_array_ptr is access pixel_array;
— Function: transpose