J
Jm.GlezdeRueda
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-
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-
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
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-
printf("\n Tamaño de la cabecera: %d\n",bmp1->Size);BitmapOffset);
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-
printf("\n Resolución horizontal: %d \n",bmp1->HorzResolution);SizeOfBitmap);
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