Image 如何在vhdl中读取图像文件并将其转换为位
我正在尝试使用vhdl中的textio包读取图像文件。 如果我用记事本打开一个.jpg,我会得到一些垃圾数据,但实际上它是ASCII数据。在这里,我试图读取这些ascii数据并将其转换为字节 下面是我的代码:Image 如何在vhdl中读取图像文件并将其转换为位,image,io,vhdl,Image,Io,Vhdl,我正在尝试使用vhdl中的textio包读取图像文件。 如果我用记事本打开一个.jpg,我会得到一些垃圾数据,但实际上它是ASCII数据。在这里,我试图读取这些ascii数据并将其转换为字节 下面是我的代码: library ieee; use ieee.std_logic_1164.all; use std.textio.all; use ieee.std_logic_textio.all; entity file_io is port ( clk: i
library ieee;
use ieee.std_logic_1164.all;
use std.textio.all;
use ieee.std_logic_textio.all;
entity file_io is
port (
clk: in std_logic;
Data: out std_logic_vector(7 downto 0)
);
end entity;
architecture behav of file_io is
signal test_data : std_logic_vector(7 downto 0);
use ieee.numeric_std.all;
use std.textio.all;
use ieee.std_logic_textio.all;
begin
File_reader:process(clk)
file f : text open read_mode is "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg";
variable L: line;
variable var_int: integer:= 0;
variable var_char: character;
begin
if rising_edge(clk) then
while not endfile(f) loop
readline(f, L);
read(L, var_char);
var_int := character'pos(var_char);
test_data <= std_logic_vector(to_unsigned(var_int, test_data'length));
end loop;
end if;
Data <= test_data;
end process;
end architecture behav;
ieee库;
使用ieee.std_logic_1164.all;
使用std.textio.all;
使用ieee.std_logic_textio.all;
实体文件是
港口(
clk:标准逻辑中;
数据:输出标准逻辑向量(7到0)
);
终端实体;
文件的体系结构行为是
信号测试数据:标准逻辑向量(7到0);
使用ieee.numeric_std.all;
使用std.textio.all;
使用ieee.std_logic_textio.all;
开始
文件读取器:进程(clk)
文件f:文本打开读取模式为“C:\Users\Public\Pictures\Sample Pictures\Mystry.jpg”;
变量L:直线;
变量var_int:integer:=0;
变量var_char:字符;
开始
如果上升沿(clk),则
而不是结束文件(f)循环
读线(f,L);
读取(L,var_char);
var_int:=字符位置(var_char);
测试数据您有一个循环,而循环被放置在流程的上升沿
部分。发生的情况是,当第一个时钟边缘出现时,while
循环迭代到文件末尾,并给出输入图像的最后一个字节
删除while
循环语句应该可以解决您的问题。这个问题有一个基本缺陷。您不能使用textio读取二进制值,它是用于文本的
见IEEE标准1076-2008 16.4包文本第3(部分)和第4段:
程序READLINE、WRITELINE和TEE在程序包TEXTIO中声明读取和写入TEXT类型文件的整行。过程READLINE导致从文件中读取下一行,并将指定代表该行的对象的访问值作为参数L的值返回。如果参数L在调用开始时包含一个非空的访问值,则过程可能会取消分配该值指定的对象。线条的表示不包含线条末端的表示
该语言不定义线条端点的表示形式。实现应允许将所有可能的字符和字符串类型的值写入文件。但是,由于允许实现使用字符和字符串类型的某些值作为行分隔符,因此可能无法从文本文件中读取这些值
这可以通过您的justry.jpg进行演示:
在VHDL中,可以一次读取一个原始字符(符合您的需要)
参见IEEE标准1076-2008 5.5文件类型:
所以我们所要做的就是声明一个文件类型,然后隐式地定义这些过程
我们可以使用它们来调用原始读取,而不会出现由textio引起的任何行尾问题:
library ieee;
use ieee.std_logic_1164.all;
entity file_io is
port (
clk: in std_logic;
Data: out std_logic_vector(7 downto 0);
done: out boolean
);
end entity;
architecture foo of file_io is
use ieee.numeric_std.all;
begin
File_reader:
process (clk)
-- "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg";
constant filename: string := "Chrysanthemum.jpg"; -- local to sim
variable char_val: character;
variable status: FILE_OPEN_STATUS;
variable openfile: boolean; -- FALSE by default
type f is file of character;
file ffile: f;
variable char_count: natural := 0;
begin
if rising_edge (clk) then
if not openfile then
file_open (status, ffile, filename, READ_MODE);
if status /= OPEN_OK then
report "FILE_OPEN_STATUS = " &
FILE_OPEN_STATUS'IMAGE(status)
severity FAILURE;
end if;
report "FILE_OPEN_STATUS = " & FILE_OPEN_STATUS'IMAGE(status);
openfile := TRUE;
else
if not endfile(ffile) then
read(ffile, char_val);
-- report "char_val = " & character'image(char_val);
char_count := char_count + 1;
Data <= std_logic_vector (
to_unsigned(character'pos(char_val),
Data'length) );
end if;
if endfile(ffile) then -- can occur after last character
report "ENDFILE, read " &
integer'image(char_count) & "characters";
done <= TRUE;
FILE_CLOSE(ffile);
end if;
end if;
end if;
end process;
end architecture foo;
library ieee;
use ieee.std_logic_1164.all;
entity file_io_test is
end file_io_test;
architecture behavior of file_io_test is
signal clk: std_logic := '0';
signal data: std_logic_vector(7 downto 0);
signal done: boolean;
constant clk_period: time := 10 ns;
begin
uut:
entity work.file_io(foo)
port map (
clk => clk,
data => data,
done => done
);
clk_process:
process
begin
if not done then
clk <= '1';
wait for clk_period/2;
clk <= '0';
wait for clk_period/2;
else
wait;
end if;
end process;
end architecture behavior;
ieee库;
使用ieee.std_logic_1164.all;
实体文件是
港口(
clk:标准逻辑中;
数据:输出标准逻辑向量(7到0);
完成:输出布尔值
);
终端实体;
文件io的体系结构foo是
使用ieee.numeric_std.all;
开始
文件阅读器:
过程(clk)
--“C:\Users\Public\Pictures\Sample Pictures\jummy.jpg”;
常量文件名:字符串:=“jummy.jpg”;--本地到sim卡
变量字符:字符;
变量状态:文件\打开\状态;
变量openfile:boolean;--默认为FALSE
类型f是字符文件;
文件ffile:f;
变量字符计数:自然:=0;
开始
如果上升沿(clk),则
如果不是openfile,那么
文件打开(状态、文件名、文件名、读取模式);
如果状态/=打开_正常,则
报告“文件打开状态=&”
文件\打开\状态'图像(状态)
严重故障;
如果结束;
报告“文件打开状态”(&FILE\u OPEN\u STATUS)图像(状态);
openfile:=TRUE;
其他的
如果不是endfile(ffile),则
读取(文件、字符);
--报告“char_val=“&character”图像(char_val);
字符计数:=字符计数+1;
数据IEEE Std 1076-2008 16.4包文本IO第4段“该语言不定义线条末端的表示形式。实现应允许将所有可能的字符和字符串类型的值写入文件。但是,由于允许实现使用字符和字符串类型的某些值作为行分隔符,因此可能无法从文本文件中读取这些值。“我不知道是谁否决了你,但我认为你问的问题是正确的。这个答案不全面,没有显示出任何变化。由于问题本身的问题,它也没有用处。textio不应用于读取二进制值。这是由于readline试图解释文本文件中的行尾。
library ieee;
use ieee.std_logic_1164.all;
entity file_io is
port (
clk: in std_logic;
Data: out std_logic_vector(7 downto 0);
done: out boolean
);
end entity;
architecture foo of file_io is
use ieee.numeric_std.all;
begin
File_reader:
process (clk)
-- "C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg";
constant filename: string := "Chrysanthemum.jpg"; -- local to sim
variable char_val: character;
variable status: FILE_OPEN_STATUS;
variable openfile: boolean; -- FALSE by default
type f is file of character;
file ffile: f;
variable char_count: natural := 0;
begin
if rising_edge (clk) then
if not openfile then
file_open (status, ffile, filename, READ_MODE);
if status /= OPEN_OK then
report "FILE_OPEN_STATUS = " &
FILE_OPEN_STATUS'IMAGE(status)
severity FAILURE;
end if;
report "FILE_OPEN_STATUS = " & FILE_OPEN_STATUS'IMAGE(status);
openfile := TRUE;
else
if not endfile(ffile) then
read(ffile, char_val);
-- report "char_val = " & character'image(char_val);
char_count := char_count + 1;
Data <= std_logic_vector (
to_unsigned(character'pos(char_val),
Data'length) );
end if;
if endfile(ffile) then -- can occur after last character
report "ENDFILE, read " &
integer'image(char_count) & "characters";
done <= TRUE;
FILE_CLOSE(ffile);
end if;
end if;
end if;
end process;
end architecture foo;
library ieee;
use ieee.std_logic_1164.all;
entity file_io_test is
end file_io_test;
architecture behavior of file_io_test is
signal clk: std_logic := '0';
signal data: std_logic_vector(7 downto 0);
signal done: boolean;
constant clk_period: time := 10 ns;
begin
uut:
entity work.file_io(foo)
port map (
clk => clk,
data => data,
done => done
);
clk_process:
process
begin
if not done then
clk <= '1';
wait for clk_period/2;
clk <= '0';
wait for clk_period/2;
else
wait;
end if;
end process;
end architecture behavior;