Matan Ben Noach
New member
קריאת קובץ BMP בC
היי,
אני מנסה לקרוא את תוכן הפיקסלים של קובץ BMP, חיפשתי אתמול שעות איך structure של הקובץ הזה בנוי ואני עדיין לא מצליח להוציא את הדאטא, אם מישהו יודע איפה הטעות שלי או מכיר מדריך טוב לזה אשמח אם תפרסמו (רק בלי ספריות מיובאות, אני רוצה להתמודד עם הקובץ בעצמי), תודה.
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
typedef struct {
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned int reserved1, reserved2;
long offset; /* Offset to image data, bytes */
} Header;
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned int planes; /* Number of colour planes */
unsigned int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} Infoheader;
void main()
{
int n;
FILE *fin;
Header fileheader;
unsigned char *data;
fin=fopen("Check.bmp","rb");
if (fin==NULL)
{
printf("Error reading image\n");
exit(1);
}
fread(&fileheader,sizeof(Header),1,fin);
if (fileheader.type!=0x4D42)
{
printf("Not a bmp file\n");
exit(1);
}
Infoheader information;
n=fread(&information, sizeof(Infoheader),1,fin);
if(n!=1)
{
printf("Error reading image information\n");
exit(1);
}
fseek(fin,fileheader.offset,SEEK_SET);
data=(unsigned char *)malloc(information.imagesize);
if(data==NULL)
{
printf("Error allocating memory\n");
exit(1);
}
if(data==NULL)
{
printf("Error reading data from image\n");
exit(1);
}
}
היי,
אני מנסה לקרוא את תוכן הפיקסלים של קובץ BMP, חיפשתי אתמול שעות איך structure של הקובץ הזה בנוי ואני עדיין לא מצליח להוציא את הדאטא, אם מישהו יודע איפה הטעות שלי או מכיר מדריך טוב לזה אשמח אם תפרסמו (רק בלי ספריות מיובאות, אני רוצה להתמודד עם הקובץ בעצמי), תודה.
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
typedef struct {
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned int reserved1, reserved2;
long offset; /* Offset to image data, bytes */
} Header;
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned int planes; /* Number of colour planes */
unsigned int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} Infoheader;
void main()
{
int n;
FILE *fin;
Header fileheader;
unsigned char *data;
fin=fopen("Check.bmp","rb");
if (fin==NULL)
{
printf("Error reading image\n");
exit(1);
}
fread(&fileheader,sizeof(Header),1,fin);
if (fileheader.type!=0x4D42)
{
printf("Not a bmp file\n");
exit(1);
}
Infoheader information;
n=fread(&information, sizeof(Infoheader),1,fin);
if(n!=1)
{
printf("Error reading image information\n");
exit(1);
}
fseek(fin,fileheader.offset,SEEK_SET);
data=(unsigned char *)malloc(information.imagesize);
if(data==NULL)
{
printf("Error allocating memory\n");
exit(1);
}
if(data==NULL)
{
printf("Error reading data from image\n");
exit(1);
}
}