将C++程序转换成matlab程序

来源:百度知道 编辑:UC知道 时间:2024/05/05 17:57:16
#include "stdio.h"
main( ) { /*只处理16、24、32真彩色位图*/
long biwidth,biheight,i,j,N,M,T;
short bibitcount;
unsigned char x;
FILE *fp1; FILE *fp2; FILE *fp3;
fp1=fopen("flower.bmp","rb");
fseek(fp1,18,0);
fread(&biwidth,4,1,fp1); /*读图像的宽度*/
fseek(fp1,22,0);
fread(&biheight,4,1,fp1); /*读图像的高度*/
fseek(fp1,28,0);
fread(&bibitcount,2,1,fp1); /*读颜色位数*/
printf("The biwidth of the image is:%d\n",biwidth); /*显示图像的宽度*/
printf("The biheight of the image is:%d\n",biheight); /*高度*/
printf("The bibitcount of the image is:%d\n",bibitcount); /*颜色位数*/
fp2=fopen("head.txt","wb");
for(i=0;i<54;i++) {
fseek(fp1,i,0);
fread(&x,1,1,fp1);
fprintf(fp2,"%x\t",x); /*把位图文件头、信息头写到head.txt中去*/

生成的速度比较慢。因为是直接一个个写文件。

fp1=fopen('flower.bmp','r');
fseek(fp1,18,'bof');
biwidth=fread(fp1,1,'int32');
fseek(fp1,22,'bof');
biheight=fread(fp1,1,'int32');
fseek(fp1,28,'bof');
bibitcount=fread(fp1,1,'int16');
disp(['The biwidth of the image is:',num2str(biwidth)])
disp(['The biheight of the image is:',num2str(biheight)])
disp(['The bibitcount of the image is:',num2str(bibitcount)])

fp2=fopen('head1.txt','w');
fseek(fp1,0,'bof');
for i=1:54
x=fread(fp1,1,'uint8');
fprintf(fp2,'%s\t',dec2hex(x));
end
fclose(fp2);

N=biwidth*(bibitcount/8);
if mod(N,4)==0
M=N;
else
M=N+4-mod(N,4);
end

fp3=fopen('data1.txt','w');
for i=1:biheight
for j=1:N
T=54+(M*(i-1))+j-1;
fseek(fp1,T,&#