读Bmp的问题.. [英] Problem Reading Bmp's ..

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

问题描述

大家好,


我试着用fread读取24位bmp,我有一些问题..我想要读取整个结构的b $ b在一次,但我不知道为什么,它

只读好第一个成员..


我有两个问题..


1-如果我改变fread(bmp1,sizeof(bmp1),1,fin)的原因; to fread(bmp1,

sizeof(struct bmp),1,fin);我有一个段违规?


2-我可以一次读完整个结构吗? (如果我按成员阅读,我可以很好地阅读

结构)

代码>>

#include< stdio.h> ;

#include< stdlib.h>


typedef unsigned char BYTE,uc;

typedef unsigned short WORD,us ;

typedef unsigned long DWORD,ul;

typedef unsigned long LONG;


struct bmp {


WORD FileType; / *文件类型,总是4D42h(" BM")* /

DWORD FileSize; / *文件大小(字节)* /

WORD Reserved1; / *总是0 * /

WORD Reserved2; / *总是0 * /

DWORD BitmapOffset; / *图像数据的起始位置,以字节为单位

* /

DWORD大小; / *此标题的大小(以字节为单位)* /

LONG Width; / *图像宽度(以像素为单位)* /

LONG高度; / *图像高度(以像素为单位)* /

WORD Planes; / *彩色平面数* /

WORD BitsPerPixel; / *每像素的位数* /

DWORD压缩; / *使用的压缩方法* /

DWORD SizeOfBitmap; / *位图的大小(以字节为单位)* /

LONG HorzResolution; / *水平分辨率,以像素为单位

米* /

LONG VertResolution; / *垂直分辨率,以每米像素为单位

* /

DWORD ColorsUsed; / *图像中的颜色数* /

DWORD ColorsImportant; / *最小重要颜色数* /

};


int main(){


struct bmp * bmp1;


ul i;

FILE * fin;


fin = fopen(" 1。 bmp"," rb");

if(fin == NULL)返回0;

fread(bmp1,sizeof(bmp1),1,fin);

printf(" \ n Propiedades de la imagen \ n");

printf("

__________________________________________________ ____________ \ n" );

printf(" \ n Tipo de fichero:%d \ n",bmp1-> FileType);

printf(" \ n Tama?o del fichero en bytes:%d \ n",bmp1-> FileSize);

printf(" \\\
Zona reservada:%d \ n",bmp1-Reserved1 );

printf(" \ n Zona reservada:%d \ n",bmp1-Reserved2);

printf(" \\\
Offset hasta el mapa de bits:%d \ n",bmp1-

Hi all,

Im trying to read a 24bit bmp with fread, and i have some problems.. I
want to read the whole structure in one time, but i dont know why, it
only reads the first member well..

I have two questions..

1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1,
sizeof(struct bmp), 1, fin); i have a Segment violation ??

2- can i read the whole structure in one time ?? ( i can read the
structure well if i read it member by member)
Code>>
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char BYTE,uc;
typedef unsigned short WORD,us;
typedef unsigned long DWORD,ul;
typedef unsigned long LONG;

struct bmp{

WORD FileType; /* File type, always 4D42h ("BM") */
DWORD FileSize; /* Size of the file in bytes */
WORD Reserved1; /* Always 0 */
WORD Reserved2; /* Always 0 */
DWORD BitmapOffset; /* Starting position of image data in bytes
*/
DWORD Size; /* Size of this header in bytes */
LONG Width; /* Image width in pixels */
LONG Height; /* Image height in pixels */
WORD Planes; /* Number of color planes */
WORD BitsPerPixel; /* Number of bits per pixel */
DWORD Compression; /* Compression methods used */
DWORD SizeOfBitmap; /* Size of bitmap in bytes */
LONG HorzResolution; /* Horizontal resolution in pixels per
meter */
LONG VertResolution; /* Vertical resolution in pixels per meter
*/
DWORD ColorsUsed; /* Number of colors in the image */
DWORD ColorsImportant; /* Minimum number of important colors */
};

int main(){

struct bmp *bmp1;

ul i;
FILE *fin;

fin=fopen("1.bmp","rb");
if (fin == NULL) return 0;
fread(bmp1, sizeof(bmp1), 1, fin);
printf("\n Propiedades de la imagen\n");
printf("
__________________________________________________ ____________\n");
printf("\n Tipo de fichero: %d\n",bmp1->FileType);
printf("\n Tama?o del fichero en bytes: %d\n",bmp1->FileSize);
printf("\n Zona reservada: %d\n",bmp1-Reserved1);
printf("\n Zona reservada: %d\n",bmp1-Reserved2);
printf("\n Offset hasta el mapa de bits:%d\n",bmp1-


> BitmapOffset);
>BitmapOffset);



printf(" \ n Tama?o de la cabecera:%d \ n",bmp1-> Size);

printf(" \\\
Ancho de la imagen:%d \ n",bmp1-> Width);

printf(" \\\
Altura de la imagen:%d \ n",bmp1->高度);

printf(" \\\
Númerodeplanos:%d \ n",bmp1-> Planes);

printf(" \ n Bits por pixel:%d \ n",bmp1-> BitsPerPixel);

printf(" \\\
Compresión:%d \ n" ;,bmp1->压缩);

printf(&\\; Tama?o de la imagen en bytes:%d \ n",bmp1-

printf("\n Tama?o de la cabecera: %d\n",bmp1->Size);
printf("\n Ancho de la imagen: %d \n",bmp1->Width);
printf("\n Altura de la imagen: %d \n",bmp1->Height);
printf("\n Número de planos: %d \n",bmp1->Planes);
printf("\n Bits por pixel: %d \n",bmp1->BitsPerPixel);
printf("\n Compresión: %d \n",bmp1->Compression);
printf("\n Tama?o de la imagen en bytes: %d \n",bmp1-


> SizeOfBitmap);
>SizeOfBitmap);



printf(" \\\
Resoluciónhorizo​​ntal:%d \ n",bmp1-> HorzResolution);

printf( \\\
Resoluciónvertical:%d \ n,bmp1-> VertResolution);

printf(" \\\
Tama?o de la paleta:%d \ n" ,bmp1-> ColorsUsed);

printf(" \ n Colores importantes:%d \ n \ n",bmp1-> ColorsImportant);

unsigned char * puntero = malloc(sizeof(uc)*((bmp1-> SizeOfBitmap)* 3));


FILE * fout;

fout = fopen(" 2.bmp"," wb");


fwrite(bmp1,sizeof(struct bmp),1,fout );

fclose(fin);

fclose(fout);

免费(puntero);


};


__________________________________________


控制台结果>>



Propiedades de la imagen

__________________________________________________ ____________


Tipo de fichero:19778 *****(这是BM,没关系)


Tama?o del fichero en bytes: - 1208460616 ********(???)


Zona reservada:6944 *********(??? )


Zona reservada:47097

Offset hasta el mapa de bits:-1209676762


Tama?o de la cabecera:-1209347184

Ancho de la imagen:-1209676730

Altura de la imagen:-1209676714

$ b $bNúmerodeplanos:52326


比特像素:47077


压缩:-1209676682

Tama?o de la imagen en bytes:-1209676666

$ b $bResoluciónhorizo​​ntal:0

$ b $bResoluciónvertical :0


Tama?o de la paleta:0


Colores importantes:0

printf("\n Resolución horizontal: %d \n",bmp1->HorzResolution);
printf("\n Resolución vertical: %d \n",bmp1->VertResolution);
printf("\n Tama?o de la paleta: %d \n",bmp1->ColorsUsed);
printf("\n Colores importantes: %d \n\n",bmp1->ColorsImportant);

unsigned char *puntero=malloc(sizeof(uc)*((bmp1->SizeOfBitmap)*3));


FILE *fout;
fout=fopen("2.bmp","wb");

fwrite(bmp1,sizeof(struct bmp),1,fout);
fclose(fin);
fclose(fout);
free(puntero);

};

__________________________________________

Console Result>>


Propiedades de la imagen
__________________________________________________ ____________

Tipo de fichero: 19778 ***** (this is BM, its ok)

Tama?o del fichero en bytes: -1208460616 ******** ( ???)

Zona reservada: 6944 ********* ( ??? )

Zona reservada: 47097

Offset hasta el mapa de bits:-1209676762

Tama?o de la cabecera: -1209347184

Ancho de la imagen: -1209676730

Altura de la imagen: -1209676714

Número de planos: 52326

Bits por pixel: 47077

Compresión: -1209676682

Tama?o de la imagen en bytes: -1209676666

Resolución horizontal: 0

Resolución vertical: 0

Tama?o de la paleta: 0

Colores importantes: 0

推荐答案



< Jm ************ @ gmail.comwrote in message

新闻:11 ********************** @ o5g2000hsb.googlegro ups.com ...

<Jm************@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...

>
我试图用fread读取24位bmp,我有一些问题..我想一次读完整个结构,但我不知道为什么呢,
只能很好地读取第一个成员..
>
Im trying to read a 24bit bmp with fread, and i have some problems.. I
want to read the whole structure in one time, but i dont know why, it
only reads the first member well..



如果你声明一个结构并将它变成它可能会匹配,它可能不匹配。 />
它也将在不同的编译器上中断。

处理二进制文件格式的方法是声明以字节为单位加载

字段的函数从流中重新组装它们。例如图像

宽度是32位整数,我认为是小端。


将整个读取包装成一个函数。可能你只想要图像

尺寸和栅格数据。但是你可能不希望微软使用bgr-padding

格式,并且没有理由你应该返回

那个订单中的栅格。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

If you declare a structure and fred it it might match up and it might not.
It will also break on a different compiler.
The way to treat a binary file format is to declare functions that load the
fields byte by byte from the stream and reassemble them. For instance image
width is a 32 bit integer, I think little-endian.

Wrap up the entire read into one function. Probably you only want image
dimensions and raster data. However you might not want the bgr-padding
format Microsoft use, and there is no reason you should return the raster in
that order.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


处理二进制文件的方法format是声明从流中逐字节加载
The way to treat a binary file format is to declare functions that load the

字段并重新组装它们的函数。例如图像

宽度是32位整数,我认为是小端。


将整个读取包装成一个函数。可能你只想要图像

尺寸和栅格数据。但是你可能不希望微软使用bgr-padding

格式,并且没有理由你应该返回

那个订单中的栅格。
fields byte by byte from the stream and reassemble them. For instance image
width is a 32 bit integer, I think little-endian.

Wrap up the entire read into one function. Probably you only want image
dimensions and raster data. However you might not want the bgr-padding
format Microsoft use, and there is no reason you should return the raster in
that order.



我使用gcc ..所以..你认为最好是按成员阅读它的成员

或有什么东西可以解决它?


谢谢

I use gcc.. so.. do you think its better to read it member by member
or there is something to fix it?

Thanks


这是我旧代码的样本..你的意思是这样吗?

我能解决#pragma或类似问题吗?谢谢


代码:


int openBmp(char * nombre,unsigned char * puntero){


struct bmp * bmp1;

ul i;

FILE * fin;


fin = fopen(" 1" ,rb;;

if(fin == NULL)返回0;

fopen(& bmp1-> FileType,sizeof(bmp1-> FileType) ),1,fout);

fopen(& bmp1-> FileSize,sizeof(bmp1-> FileSize),1,fout);

fopen(& ; bmp1-> Reserved1,sizeof(bmp1-> Reserved1),1,fout);

fopen(& bmp1-> Reserved2,sizeof(bmp1-> Reserved2),1, fout);

fopen(& bmp1-> BitmapOffset,sizeof(bmp1-> BitmapOffset),1,fout);

fopen(& bmp1-> ; size,sizeof(bmp1-> Size),1,fout);

fopen(& bmp1-> Width,sizeof(bmp1-> Width),1,fout);

fopen(& bmp1->高度,sizeof(bmp1->高度),1,fout);

fopen(& bmp1-> Planes,sizeof (bmp1-> Planes),1,fout);

fopen(& bmp1-> BitsP erPixel,sizeof(bmp1-> BitsPerPixel),1,fout);

fopen(& bmp1->压缩,sizeof(bmp1->压缩),1,fout);

fopen(& bmp1-> SizeOfBitmap,sizeof(bmp1-> SizeOfBitmap),1,fout);

fopen(& bmp1-> HorzResolution,sizeof( bmp1-> HorzResolution),1,fout);

fopen(& bmp1-> VertResolution,sizeof(bmp1-> VertResolution),1,fout);

fopen(& bmp1-> ColorsUsed,sizeof(bmp1-> ColorsUsed),1,fout);

fopen(& bmp1-> ColorsImportant,sizeof(bmp1-> ColorsImportant),1,fout);

puntero = malloc(sizeof(uc)*(SizeOfBitmap * 3));

for(i = 0; i<(bmp1 - > SizeOfBitmap * 3); i ++){

fread(puntero,sizeof(uc),1,fin);

};


fclose(fin);


};

Here its a sample of my old code.. do you mean something like this?
can i fix the problem with #pragma or something like that? Thanks

CODE:

int openBmp(char *nombre,unsigned char *puntero) {

struct bmp *bmp1;
ul i;
FILE *fin;

fin=fopen("1","rb");
if (fin == NULL) return 0;
fopen(&bmp1->FileType, sizeof(bmp1->FileType), 1, fout);
fopen(&bmp1->FileSize, sizeof(bmp1->FileSize), 1, fout);
fopen(&bmp1->Reserved1, sizeof(bmp1->Reserved1), 1, fout);
fopen(&bmp1->Reserved2, sizeof(bmp1->Reserved2), 1, fout);
fopen(&bmp1->BitmapOffset, sizeof(bmp1->BitmapOffset), 1, fout);
fopen(&bmp1->Size, sizeof(bmp1->Size), 1, fout);
fopen(&bmp1->Width, sizeof(bmp1->Width), 1, fout);
fopen(&bmp1->Height, sizeof(bmp1->Height), 1, fout);
fopen(&bmp1->Planes, sizeof(bmp1->Planes), 1, fout);
fopen(&bmp1->BitsPerPixel, sizeof(bmp1->BitsPerPixel), 1, fout);
fopen(&bmp1->Compression, sizeof(bmp1->Compression), 1, fout);
fopen(&bmp1->SizeOfBitmap, sizeof(bmp1->SizeOfBitmap), 1, fout);
fopen(&bmp1->HorzResolution, sizeof(bmp1->HorzResolution), 1, fout);
fopen(&bmp1->VertResolution, sizeof(bmp1->VertResolution), 1, fout);
fopen(&bmp1->ColorsUsed, sizeof(bmp1->ColorsUsed), 1, fout);
fopen(&bmp1->ColorsImportant, sizeof(bmp1->ColorsImportant), 1, fout);
puntero=malloc(sizeof(uc)*(SizeOfBitmap*3));
for (i=0;i<(bmp1->SizeOfBitmap*3);i++) {
fread(puntero,sizeof(uc),1,fin);
};

fclose(fin);

};


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

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