Image file read

Hi ,

Would appreciate if this problem is resolved.

I am trying to read data from an Image file (.raw - 800600 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 →

`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!

Maybe try

  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

In reply to dave_59:

Hi Dave,

As $fgetc doesn’t give the input data in correct format i used $fscanf.
The file actually contains 1440000 bytes (8006008). 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

In reply to rockgarden333:

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.

In reply to dave_59:

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

  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).

	 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 ?

In reply to rockgarden333:

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.

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

# 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

In reply to dave_59:

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?

In reply to rockgarden333:

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.

In reply to dave_59:

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?

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!

In reply to rockgarden333:

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