# Image file read

**URL:** <https://verificationacademy.com/forums/t/image-file-read/29828>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [August 19, 2014, 1:48pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828 "2014-08-19T13:48:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rockgarden333](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rockgarden333](https://verificationacademy.com/forums/u/rockgarden333)\
**Post date:** [August 19, 2014, 1:48pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/1 "2014-08-19T13:48:27Z")

</div>

Hi ,

Would appreciate if this problem is resolved.

I am trying to read data from an Image file (_.raw - 800_600 resolution);  
I could only read the data from the file partially , afterwards the file pointer does not seem to move further.  
Say , if the file has 200 items, it reads only 150 items and doesn’t seem to move further. Is it something wrong with the EOF?  
i am not sure.

My code is as follows →

```verilog
`define EOF 32'hFFFF_FFFF
class input_video_IF;
    integer input_file, read_char,output_file;
	
    function new ();
	 
        input_file = $fopen("test_800.raw", "r");
		if(input_file)
		begin
		   $display("File OPENED");
		end
		else
		begin
		   $display("File error");
		end
	
		output_file = $fopen("output_800.raw", "w"); 
    endfunction:new 
	
    task image_file_read();
        while (!$feof(input_file))
        begin
            read_char = $fgetc(input_file);
			$display("%h",read_char);
			$fwrite(output_file,read_char);
	    end 
        $fclose(input_file);
    endtask : image_file_read
	
endclass

program Image_read_test;

int input_file;
input_video_IF In_IF = new();
initial
In_IF.image_file_read();

endprogram

```

Request your earliest reply…

Thanks!

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 20, 2014, 8:41pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/2 "2014-08-20T20:41:01Z")

</div>

Maybe try

```verilog
  function void image_file_read();
        while ((read_char = $fgetc(input_file)) != -1)
        begin
          $display("%h",read_char);
	  $fwrite(output_file,read_char);
	 end 
        $fclose(input_file);
    endfunction : image_file_read

```

---

<div class="post-metadata">

**Author:** ![rockgarden333](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rockgarden333](https://verificationacademy.com/forums/u/rockgarden333)\
**Post date:** [August 21, 2014, 1:12pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/3 "2014-08-21T13:12:09Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/image-file-read/29828/2):_

Hi Dave,

As $fgetc doesn’t give the input data in correct format i used $fscanf.  
The file actually contains 1440000 bytes (800_600_8). The positive thing is , now it reads all the data  
and goes beyond it. So now i have to check the counter value. Why it doesn’t find the EOF?

I have read the same image using VHDL which uses EOF file checking. So we can conclude the file is not corrupt with EOF.

i changed the code

function void image\_file\_read();  
count = 0;  
while (data\_byte != -1)  
begin  
file\_status = $fscanf(input\_file,“%h”,data\_byte);  
count++;  
$display(“%d %h”,count,data\_byte);

```
		if(count == 1440000)
		    $finish;
        //$fwrite(output_file,read_char);
    end 
    $fclose(input_file);
endfunction : image_file_read

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 21, 2014, 2:53pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/4 "2014-08-21T14:53:31Z")

</div>

_In reply to [rockgarden333](https://verificationacademy.com/forums/t/image-file-read/29828/3):_

The functionality of $fgetc and $fscanf are very different. Apparently, you are not reading a raw binary image, you are reading an ASCII hex encoded file.  
You need to check the file\_status, which will return 0 when reaching the EOF.

---

<div class="post-metadata">

**Author:** ![rockgarden333](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rockgarden333](https://verificationacademy.com/forums/u/rockgarden333)\
**Post date:** [August 22, 2014, 5:34am UTC](https://verificationacademy.com/forums/t/image-file-read/29828/5 "2014-08-22T05:34:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/image-file-read/29828/4):_

Yes Dave, you are right.  
Sorry for confusing you. $fscanf , I used for reading .hex file of the same .raw image.

Input file here is \*.raw image

```verilog
  function void image_file_read();
		    count = 0;
			$display("Entered Image read");
		    while ((data_byte = $fgetc(input_file)) != -1)
            begin
                $display("%d %h",count,data_byte);
				count++;
				//if(count == 1440000)
				   //$finish;
	            //$fwrite(output_file,read_char);
	        end 
        $fclose(input_file);

```

Now the input data shows “ff” after a certain count. the last values are  
1439993 ff

# 1439994 ff

# 1439995 ff

# 1439996 ff

# 1439997 ff

# 1439998 ff

# 1439999 ff

Input file changed to \*.hex of the same image file  
Code changed as follows. file\_status = 0 checked. but here it goes beyond the data count (1440000).

```verilog
	 function void image_file_read();
	    count = 0;
		file_status = 1;
        while (file_status != 0)
        begin
		    file_status = $fscanf(input_file,"%h",data_byte);
            count++;
		    $display("%d %h",count,data_byte);
			
			//if(count == 1440000)
			// $finish;
	        //$fwrite(output_file,read_char);
	    end 
        $fclose(input_file);
    endfunction : image_file_read

```

Now i have 2 questions

1. How can i correctly read \*.raw image completely?
2. How can i fetch the EOF in \*.hex ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 25, 2014, 8:09pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/6 "2014-08-25T20:09:22Z")

</div>

_In reply to [rockgarden333](https://verificationacademy.com/forums/t/image-file-read/29828/5):_

You will have to show a complete example of what you want to do, not just a piece of it.  
The following worked for me and it may help you figure out what you want.

```verilog
module top;
   int data,f;
   initial begin
      f = $fopen("file","r");
      while ((data=$fgetc(f)) !=-1) // raw binary
	$display("fgetc:%0d %c",data, data);
      $fclose(f);
      f = $fopen("file","r");
      while ($fscanf(f,"%h",data) !=-1) // hex formatted
	$display("fscanf: %h",data);
      $fclose(f);
   end
endmodule

```

> 0123 4567  
> 89AB CDEF

Produced the following output

```auto
# run -all 
# fgetc:48 0
# fgetc:49 1
# fgetc:50 2
# fgetc:51 3
# fgetc:32  
# fgetc:52 4
# fgetc:53 5
# fgetc:54 6
# fgetc:55 7
# fgetc:10 
# 
# fgetc:56 8
# fgetc:57 9
# fgetc:65 A
# fgetc:66 B
# fgetc:32  
# fgetc:67 C
# fgetc:68 D
# fgetc:69 E
# fgetc:70 F
# fgetc:10 
# 
# fscanf: 00000123
# fscanf: 00004567
# fscanf: 000089ab
# fscanf: 0000cdef
# quit -f

```

---

<div class="post-metadata">

**Author:** ![rockgarden333](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rockgarden333](https://verificationacademy.com/forums/u/rockgarden333)\
**Post date:** [August 26, 2014, 5:14am UTC](https://verificationacademy.com/forums/t/image-file-read/29828/7 "2014-08-26T05:14:18Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/image-file-read/29828/6):_

Once again thanks for your kind reply.

Dave, $fgetc and $fscanf works fine here. But the code is only reading the input file partially.  
My input file is ASCII formatted .raw image file. (Not binary or hex). Now your code is reading correctly  
in necessary format. but the file pointer is not moving after a certain point. The file seems not corruptive , as i could  
read it fully using VHDL file I/O operation.

How can i send the input file as attachment? Any means?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 27, 2014, 3:16am UTC](https://verificationacademy.com/forums/t/image-file-read/29828/8 "2014-08-27T03:16:54Z")

</div>

_In reply to [rockgarden333](https://verificationacademy.com/forums/t/image-file-read/29828/7):_

This could be a tool issue, or a coding error. You have never shown enough code to eliminate the latter. If it is a tool issue, you should contact your vendor’s support channel. I cannot help you further on this forum.

---

<div class="post-metadata">

**Author:** ![rockgarden333](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rockgarden333](https://verificationacademy.com/forums/u/rockgarden333)\
**Post date:** [August 27, 2014, 11:52am UTC](https://verificationacademy.com/forums/t/image-file-read/29828/9 "2014-08-27T11:52:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/image-file-read/29828/8):_

Dave,

My requirement is to **read** from a ASCII formatted input image file and **write** the data to another ASCII formatted output image file.  
I used a 800\*600 \*.raw(RGB formatted ASCII) file. I used IrfanView tool to generate \*.raw(RGB) from \*.JPG.

here is my full code. If wanted i can send the input image file as attachment.  
I don’t see an option to attach files here in this forum how?

```verilog
class input_video_IF;
    int data;
    int count;
    integer input_file, file_status,output_file,read_char;
	reg [7:0]data_byte;
	
    function new ();
	
	 	input_file = $fopen("test_800.raw","r");
		
		if(input_file)
		begin
		   $display("File OPENED");
		end
		else
		begin
		   $display("File error");
		end
	    endfunction:new 
	

	
	function void image_file_read();
            count = 0;
            while ((data = $fgetc(input_file)) != -1) // raw binary
				$display("fgetc:%0d %h %d",data, data , count);
            $fclose(file_status);
           /* file_status = $fopen("input_file","r");
            while ($fscanf(file_status,"%h",data) != -1) // hex formatted
	            $display("fscanf: %h",data);
            $fclose(file_status); */
    endfunction : image_file_read

		
	endclass

program Image_read_test;
int input_file;
input_video_IF In_IF = new();
initial
    In_IF.image_file_read();

endprogram

```

Thanks!

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 27, 2014, 1:55pm UTC](https://verificationacademy.com/forums/t/image-file-read/29828/10 "2014-08-27T13:55:23Z")

</div>

_In reply to [rockgarden333](https://verificationacademy.com/forums/t/image-file-read/29828/9):_

The only thing I am left with is a tool issue. Contact your vendor’s support line.
