View Single Post
  #3 (permalink)  
Old 09-16-2006, 06:03 AM
Dal
Guest
 
Posts: n/a
Default Re: opening an image, using it for simulation stimulus

Here's a example of a function that reads data from a file. Makes use
of stdio_h and a few of my own procedures (get_word). I expect it
could be done more elagantly but I hope it gets you started.


impure function read_pnm (
fname : IN string
) return img_info_t is
variable img_open : boolean := false;
variable img_info : img_info_t;
variable line_str : string(1 to MAX_LINE_SIZE);
variable word : string(1 to MAX_WORD_SIZE);
variable IMG_FH : CFILE;
variable pnm_state : pnm_state_t := PNM_TYPE;
variable pix_count : integer := 0;
begin

if (not img_open) then
IMG_FH := fopen(fname, "r");
if (IMG_FH=0) then
report "Couldn't open image file: " & fname
severity error;
else
img_open := true;
end if;
end if;

line_loop : while (not feof(IMG_FH)) and (img_open) and (pnm_state /=
DONE) loop
-- Get line.
fgets(line_str,line_str'length,IMG_FH);
-- trim lf.
if (strchr(line_str, LF) > 0) then
line_str(strchr(line_str, LF)) := NUL;
end if;
-- trim comment.
trim_comment(line_str);
-- Skip blank lines
if (line_str(1) = NUL) then
next line_loop;
end if;

-- Extract data from the image file line
get_word(line_str,word);
while (word(1) /= NUL) loop
case pnm_state is
when PNM_TYPE =>
sscanf(word,"%s",img_info.pnm_type);
if (strcmp(img_info.pnm_type,"P2")=0) then
pnm_state := WIDTH;
else
pnm_state := DONE;
report "Image file type not supported"
severity failure;
end if;
when WIDTH =>
sscanf(word,"%d",img_info.width);
pnm_state := HEIGHT;
when HEIGHT =>
sscanf(word,"%d",img_info.height);
img_info.num_pix := img_info.width * img_info.height;
pnm_state := PIX_RANGE;
when PIX_RANGE =>
sscanf(word,"%d",img_info.pix_range);
pnm_state := PIX;
when PIX =>
sscanf(word,"%d",img_info.pix(pix_count));
pix_count := pix_count + 1;
if (pix_count = img_info.num_pix) then
pnm_state := DONE;
exit;
end if;
when others => null;
end case;
get_word(line_str,word);
end loop;

end loop;

if (img_open) then
fclose(IMG_FH);
img_open := false;
end if;

return img_info;

end function read_pnm;



wallge wrote:
> I am working on a video processing system with FPGAs. I would like to
> tell my simulator
> through my VHDL testbench to open a raw image file (say pgm) and use
> each pixel in the image as stimulous for the simulation.
>
> Is it possible to read all the pixels in the image into an array
> variable, and then send them into the simulation line by line?
>
> Anyone have some pointers on how to get this done?
>
> It would also be nice to write the processed results back to a pgm file
> when the simulation was done in order to see how the FPGA
> implementation of the algorithm performed. Maybe save the output from
> the entity to another array variable, and then write the results to
> another pgm file at the end...


Reply With Quote