在C中读取.bmp文件 [英] Reading .bmp file in C

查看:63
本文介绍了在C中读取.bmp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将.bmp图像的像素值(将输入到代码中的
)读入矩阵... plz帮助我,因为我无法理解

如何实现这一目标...


提前感谢


biplab

解决方案

biplab说:


大家好,

我想读取.bmp图像的像素值(将输入到代码中的
)到矩阵中... plz帮助我,因为我无法理解如何

来实现这个目标...



到目前为止你尝试了什么?


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


9月10日,6:12 * pm,Richard Heathfield< r ... @ see.sig.invalidwrote:
< blockquote class =post_quotes>
biplab说:


大家好,

我想读取一个像素值.bmp图片(将输入到代码中的
)到矩阵中... plz帮助我,因为我无法理解

如何实现这个...



到目前为止你尝试了什么?


-

Richard Heathfield< http ://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日



我用fopen读取文件...然后读取值... bt

没有什么是阅读...我在C中有一个访问视频内存的代码,以便

打印图像....你可以帮我解决一下这个代码我怎么能

读取像素值,并输出与输入相同的图像...


#include< stdio.h>

#include< stdlib.h>

#include< dos.h>

#include< mem.h>

#include< string。 h>

#include< conio.h>

#include< fcntl.h>

#include< io.h>

#include< math.h>


unsigned char r [360],mini,mode;

unsigned char rgb [256] [3];

char * fullname;

char * fname;

unsigned int screen_width = 320;

unsigned int first_row = 0,first_column = 0,last_row = 0,last_column = 0;

int handle,i,j,rem,color_bits;

long int

file_size,data_offset,size_info_header,width,heigh t,compression,

image_size,xpelspermeter,ypelspermeter,colorsused,colorsimpt;

float theta ;

void drawdot(int y,int x,int pal_index){

_AX = pal_index;

_CX = x;

_DX = y;

_AH = 0x0c;

_BH = 0x00;

geninterrupt(0x10);

}


void main(){


得到(fname);

strcpy(fullname, fname);

strcat(fullname," .bmp");

handle = _open(fullname,O_RDONLY);

if(handle == - 1)退出(EXIT_FAILURE);

_read(句柄,r,2);

if(r [0]!=''B''| r [1]!=''M'')退出(EXIT_FAILURE);

_read(句柄,r,

4); file_size = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_read(句柄,r,2);

_read(句柄,r ,2);

_read(句柄,r,4);

data_offset = r [0] + 256 * r [1] + 65536 * r [2] + 0x1000000 * r [3];

_read(句柄,r,4);

size_info_header = r [0] + 256 * r [1] + 65536 * r [ 2] + 0x1000000 * r [3];

如果(size_info_header!= 40)退出(EXIT_FAILURE);

_read(handle,r,4);

width = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

if(width screen_width)exit(0) ;

_read(句柄,r,4);

height = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_read(句柄,r,2);

如果(r [0]!= 1)退出(EXIT_FAILURE);

_read(handle,r,2);

color_bits = r [0];

_read(handle,r,4);

compression = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_read(handle,r,4);

image_size = r [0] + 256 * r [1] + 256 * 256 * r [2] + 256 * 256 * 256 * r [3];

_read(句柄,r,4);

xpelspermeter = r [0] + 0x100 * r [1] + 0x10000 * r [2 ] + 0x1000000 * r [3];

_read(句柄,r,4);

ypelspermeter = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_read(句柄,r,4);

colorsused = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_read(句柄,r,4);

colorsimpt = r [0] + 0x100 * r [1] + 0x10000 * r [2] + 0x1000000 * r [3];

_AX = 0x1201; _BL = 0x33; geninterrupt(0x10); //不添加RGB

_AX = 0x0013; geninterrupt(0x10); //设置模式13即320x200x256


for(i = 0; i< 256; i ++){

_read(handle,r,4);

rgb [i] [0] = r [2]; rgb [i] [1] = r [1]; rgb [i ] [2] = r [0];

}


_ES = FP_SEG(rgb); _ DX = FP_OFF(rgb);

_BX = 0; _CX = 0x100; _AX = 0x1012;

geninterrupt(0x10); //从rgb设置DAC

first_column =(screen_width-width)/ 2;

last_row = first_row + height-1;

last_column = first_column + width-1;

rem = 4 - (width%4);

for(i = 0; i< = height-1; i ++){

_read(handle,r,width);

for(j = 0; j< = width-1; j ++)drawdot(first_column + j,last_row-i,r [j]);

if(rem == 1)_read(handle,r,1);

else if(rem == 2)_read(handle,r,2);

else if(rem == 3)_read(handle,r,3); / / getch();

}

_close(句柄);

getch();

_AX = 0x1201 ; _BL = 0x33; geninterrupt(0x10); //不添加RGB

_AX = 0x0003; geninterrupt(0x10); //设置模式3即16色文本


}


如果你让我知道代码

正在做什么以及怎么做的话,我将非常感激。 /因为我在互联网上找到了代码并且在C中是一个新的朋友...

提前感谢...

bipla b


biplab写道:


9月10日下午6:12,Richard Heathfield< r。 .. @ see.sig.invalidwrote:


> biplab说:


我已经使用fopen读取了文件...然后读取了值... bt

没有读取...我在C中有一个访问vedio内存的代码

打印图像....你可以帮我解决一下这段代码我怎么能用b $ b读取像素值并输出与输出相同的图像.. 。


unsigned char r [360],mini,mode;



< snip bad code>


}


首先,如果你让我知道代码是什么代码

正在做什么以及如何?/我已经在互联网上找到代码并且我是一个

新手在C ...

提前感谢...

biplab



似乎读取一个bmp文件,非常糟糕(并假设它是8位颜色

索引),然后以高度机器特定的方式显示它,这可能会赢得'b
'在现代机器上工作。


我建议你在互联网上找到一些更新的代码。并从显示屏中分离bmp加载单独的

。您可以尝试google onbmp file

format。


-

Bartc


Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

thanks in advance

biplab

解决方案

biplab said:

Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

What have you tried so far?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Sep 10, 6:12*pm, Richard Heathfield <r...@see.sig.invalidwrote:

biplab said:

Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...


What have you tried so far?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>

unsigned char r[360],mini,mode;
unsigned char rgb[256][3];
char *fullname;
char *fname;
unsigned int screen_width=320;
unsigned int first_row=0,first_column=0,last_row=0,last_column= 0;
int handle,i,j,rem,color_bits;
long int
file_size,data_offset,size_info_header,width,heigh t,compression,
image_size,xpelspermeter,ypelspermeter,colorsused, colorsimpt;
float theta;
void drawdot(int y, int x, int pal_index){
_AX=pal_index;
_CX=x;
_DX=y;
_AH=0x0c;
_BH=0x00;
geninterrupt(0x10);
}

void main(){

gets(fname);
strcpy(fullname,fname);
strcat(fullname,".bmp");
handle=_open(fullname,O_RDONLY);
if (handle==-1) exit(EXIT_FAILURE);
_read(handle,r,2);
if (r[0] !=''B'' | r[1] !=''M'') exit(EXIT_FAILURE);
_read(handle,r,
4);file_size=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
_read(handle,r,2);
_read(handle,r,4);
data_offset=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
_read(handle,r,4);
size_info_header=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
if (size_info_header !=40) exit(EXIT_FAILURE);
_read(handle,r,4);
width=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
if (width screen_width) exit(0);
_read(handle,r,4);
height=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
if (r[0] != 1) exit(EXIT_FAILURE);
_read(handle,r,2);
color_bits=r[0];
_read(handle,r,4);
compression=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
image_size=r[0]+256*r[1]+256*256*r[2]+256*256*256*r[3];
_read(handle,r,4);
xpelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
ypelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsused=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsimpt=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don''t add RGB
_AX=0x0013;geninterrupt(0x10);//set mode 13 i.e. 320x200x256

for (i=0;i<256;i++) {
_read(handle,r,4);
rgb[i][0]=r[2];rgb[i][1]=r[1];rgb[i][2]=r[0];
}

_ES=FP_SEG(rgb);_DX=FP_OFF(rgb);
_BX=0;_CX=0x100; _AX=0x1012;
geninterrupt(0x10);//set DAC from rgb

first_column=(screen_width-width)/2;
last_row=first_row+height-1;
last_column=first_column+width-1;
rem=4 - (width % 4);
for (i=0;i<=height-1;i++) {
_read(handle,r,width);
for (j=0;j<=width-1;j++) drawdot(first_column+j,last_row-i,r[j]);
if (rem==1) _read(handle,r,1);
else if (rem==2) _read(handle,r,2);
else if (rem==3) _read(handle,r,3);//getch();
}
_close(handle);
getch();
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don''t add RGB
_AX=0x0003;geninterrupt(0x10);//set mode 3 i.e. 16-color text

}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab


biplab wrote:

On Sep 10, 6:12 pm, Richard Heathfield <r...@see.sig.invalidwrote:

>biplab said:

I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...

unsigned char r[360],mini,mode;

<snip bad code>

}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab

It seems to read a bmp file, very badly (and assumes it is 8-bit colour
indexed), then displays it in a highly machine-specific manner, which
probably won''t work on a modern machine.

I suggest you find some more up-to-date code on the internet. And separate
out the bmp loading from the display. You might try google on "bmp file
format".

--
Bartc


这篇关于在C中读取.bmp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆