Reference:
the same as
in the reference, just only read
package
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
package bmp_read_package is
constant cMAX_X : integer := 1300; -- 1280
constant cMAX_Y : integer := 1300; -- 1024
constant cBytesPerPixel : integer := 3;
constant cMaxMemSize : integer := cMAX_X * cMAX_Y * cBytesPerPixel;
subtype bmp_element is std_logic_vector(7 downto 0);
type mem_array is array(cMaxMemSize downto 0) of bmp_element;
type header_array is array(53 downto 0) of bmp_element;
procedure ReadFile(FileName : in string);
procedure ReadByteFromMemory(addr : in integer; variable data: out std_logic_vector(7 downto 0));
function GetWidth(header : in header_array) return integer;
procedure GetWidth(signal width : out integer);
function GetHeight(header : in header_array) return integer;
procedure GetHeight(signal height : out integer);
procedure GetPixel(x : in integer; y : in integer; signal data : out std_logic_vector(23 downto 0));
end package bmp_read_package;
package body bmp_read_package is
shared variable memory_in : mem_array;
shared variable memory_out : mem_array;
shared variable header : header_array;
shared variable BmpSize : integer;
shared variable BmpWidth : integer;
shared variable BmpHeight : integer;
procedure ReadFile(FileName : in string) is
variable next_vector : bit_vector(0 downto 0);
variable actual_length : natural;
variable index : integer := 0;
type bit_vector_file is file of bit_vector;
file read_file : bit_vector_file open read_mode is FileName;
begin
report " Read File";
report FileName;
index := 0;
report " Read Header";
for i in 0 to 53 loop
read(read_file,next_vector,actual_length);
if actual_length > next_vector'length then
report " vector too long";
else
header(index) := conv_std_logic_vector(bit'pos(next_vector(0)),8);
index := index + 1 ;
end if;
end loop;
BmpWidth := GetWidth(header);
BmpHeight := GetHeight(header);
BmpSize := BmpWidth * BmpHeight;
report " Read Image";
index := 0;
while not endfile(read_file) loop
read(read_file,next_vector,actual_length);
if actual_length > next_vector'length then
report " vector too long";
else
memory_in(index) := conv_std_logic_vector(bit'pos(next_vector(0)),8);
memory_out(index) := x"45";
index := index + 1;
end if;
end loop;
report "Okay";
END ReadFile;
procedure ReadByteFromMemory(addr : in integer; variable data : out std_logic_vector(7 downto 0)) is
begin
data := memory_in(addr);
end ReadByteFromMemory;
procedure GetPixel(x : in integer; y : in integer; signal data : out std_logic_vector(23 downto 0)) is
begin
if( x >= 0 and x < cMAX_X ) and ( y >= 0 and y < cMAX_Y ) then
data(23 downto 16) <= memory_in(x*3 + 3*y*GetWidth(header));
data(15 downto 8) <= memory_in(x*3+1 + 3*y*GetWidth(header));
data(7 downto 0) <= memory_in(x*3+2 + 3*y*GetWidth(header));
end if;
end GetPixel;
-- Get Width of Image
function GetWidth(header : in header_array) return integer is
begin
return conv_integer(header(21) & header(20) & header(19) & header(18));
end function GetWidth;
procedure GetWidth(signal width : out integer) is
begin
width <= BmpWidth;
end GetWidth;
-- Get Height of Image
function GetHeight(header : in header_array) return integer is
begin
return conv_integer(header(25) & header(24) & header(23) & header(22));
end function GetHeight;
procedure GetHeight(signal height : out integer) is
begin
height <= BmpHeight;
end GetHeight;
end bmp_read_package;
沒有留言:
張貼留言