Saad Rafey
Newbie level 1
- Joined
- Jan 21, 2013
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,292
I am converting a 24 bit bmp image into a halftone bmp image using C and Verilog HDL.
First of all I have written a C program to read pixel values of 24 bit bmp image and write them into a text file(1).
The C program is given below:-
Then I have written a C program to make header of halftone mbp image.
Then I have written Verilog code to convert 24 bit pixel value into a halftone(1 bit) pixel value.
The Verilog code is given below:-
The 24 bit bmp image is given below link:-
https://i.stack.imgur.com/S7302.png
And the output image I got is below link:-
https://i.stack.imgur.com/NddIn.png
The haeder of the output image:-
42 4D 6A E7 00 00 00 00 00 00 00 36 00 00 00 28 00 00 00 32 01 00 00 C1 00 00 00 01 00 00 00 00 00 33 E7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF
The halftone(output) pixels values are:-00000000000000000001111111111111111111111111111100000001111111111111. . . . . . . . . . . . . 111111111111111111111111111111111111111111111111111111111111111111111111111
The output halftone image should consist of black and white pixels only. I am unable to sort out why I am getting this output.
First of all I have written a C program to read pixel values of 24 bit bmp image and write them into a text file(1).
The C program is given below:-
Code:
#include"stdio.h"
#include"stdafx.h"
void con(unsigned char x)
{
char a;
int j;
FILE *f1;
f1=fopen("image_24_bit.txt","a");
for(j=0;j<=7;j++)
{
a=(x<<j & 1<<7)?1:0;
printf("%d",a);
fprintf(f1,"%d",a);
}
fclose(f1);
printf("\n");
}
void main()
{
FILE *f;
long int i=0;
unsigned char c;
f=fopen("sample.bmp","rb");
for(i=1;i<=54;i++)
{
fread(&c,sizeof(c),1,f);
}
for(i=55;i<=177560;i++)
{
fread(&c,sizeof(c),1,f);
con(c);
}
fclose(f);
}
Then I have written Verilog code to convert 24 bit pixel value into a halftone(1 bit) pixel value.
The Verilog code is given below:-
Code:
module read(dataout,htpv);
output reg [0:1420479]dataout;
output reg [1:59186]htpv;
reg [24:1]error[59186:0];
reg [25:0]cpv,cpv_round,e_av;
reg [0:1420479]data[0:0];
reg [1:24]data_out;
parameter threshold =8388608,w1=2,w2=8,w3=4,w4=2;
integer i=0,f1;
initial
begin
$readmemb("image_24_bit.txt",data);
dataout=data[0];
end
initial
begin
error[0]=24'b0;
f1=$fopen("HTPimage.bmp","a");
for(i=0;i<306;i=i+1)
begin
data_out=dataout[(i*24)+:24];
e_av=(w1*error[i])>>4;
cpv= data_out + e_av;
cpv_round=(cpv<threshold)?0:16777215;
htpv[i+1]=(cpv_round==0)?0:1;
error[i+1]=cpv-cpv_round;
if(htpv[i]==1)
begin
$fwrite(f1,"1");
end
else
begin
$fwrite(f1,"0");
end
#1;
end
for(i=306;i<591886;i=i+1)
begin
data_out=dataout[(i*24)+:24];
e_av=(2*error[i]+8*error[i-306]+4*error[i-305]+2*error[i-304])>>4;
cpv= data_out + e_av;
cpv_round=(cpv<threshold)?0:16777215;
htpv[i+1]=(cpv_round==0)?0:1;
error[i+1]=cpv-cpv_round;
if(htpv[i]==1)
begin
$fwrite(f1,"1");
end
else
begin
$fwrite(f1,"0");
end
#1;
end
$fclose(f1);
end
endmodule
https://i.stack.imgur.com/S7302.png
And the output image I got is below link:-
https://i.stack.imgur.com/NddIn.png
The haeder of the output image:-
42 4D 6A E7 00 00 00 00 00 00 00 36 00 00 00 28 00 00 00 32 01 00 00 C1 00 00 00 01 00 00 00 00 00 33 E7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF
The halftone(output) pixels values are:-00000000000000000001111111111111111111111111111100000001111111111111. . . . . . . . . . . . . 111111111111111111111111111111111111111111111111111111111111111111111111111
The output halftone image should consist of black and white pixels only. I am unable to sort out why I am getting this output.
Last edited by a moderator: