קריאת קובץ BMP בC

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);
}
}
 

Matan Ben Noach

New member
סורי לא שמתי לב לאפשרות להכניס קוד בתוך ההודעה, הקוד פה

#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); }}
 

Matan Ben Noach

New member
תיקון קטן לקוד

שכחתי להוסיף את הסוף, הנה הקוד במלואו
#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),2,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); }printf("%d\n",information.imagesize); if(data==NULL) { printf("Error reading data from image\n"); exit(1); }n=fread(data,information.imagesize,1,fin);if (n<1){ printf("Unable to load data\n"); exit(1);}}
 

BravoMan

Active member
לא כתבת מה בדיוק הבעיה שאתה נתקל בה או איזו שגיאה

אתה מקבל.
לא קראתי את כל הקוד שלך, אבל על ההתחלה אתה לא קורא נכון את מבנה ה-Header.
&nbsp
בגלל שה-magic הוא שני בית בלבד, וברוב המערכות היום יש יישור זיכרון של 4 בית, רוב הסיכויים שמבנה ה-struct הזה בזיכרון לא תואם את המבנה בקובץ בגלל הריפוד.
קרא אותו שדה שדה.
 

Matan Ben Noach

New member
אממ

1. האמת שהנושא של זכרון משתנים טיפה מבולבל אצלי, בספר שלמדתי ממנו (שההוצאה הייתה ב2001) היה רשום שכל int תופס 2 בתים, וכל long תופס 4 בתים, אז היום זה כבר השתנה ולמעשה כל int תופס 4 בתים, וכל long תופס 8 בתים (כלומר כל משתנה תופס פי 2)?
2. בכל מקרה אם אני משנה את הtype לint או לlong הוא לא מצליח לזהות שזה bmp.
3. הבעיה הקודמת הייתה בשורה הזאת, הוא לא מצליח לקרוא מהקובץ אל המצביע data.
n=fread(data,information.imagesize,1,fin);if (n<1)
{
printf("Unable to load data\n"); exit(1);
 

BravoMan

Active member
ספר ממש גרוע אם הוא לא הסביר

שבשפת C גודל הטיפוסים תלוי במערכת הפעלה ומעבד, ולכן המציאו את הפונקציה sizeof.
בד"כ, היום על PC טיפוס short תופס 2 בתים, int תופס 4 בתים, ו-long יכול להיות בגודל 4 או 8 תלוי במערכת הפעלה ומעבד.
הנה טבלה נחמדה של אינטל:
https://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-os
&nbsp
וכאמור, כשאתה שם משתנים במבנה, הקומפיילר מרפד אותם לפי הצורך. אם תסתכל מישהו שאל בדיוק על זה לפני שתי הודעות.
&nbsp
בגלל שלא התחשבת בריפוד וקראת בתים לא נכון מהקובץ, קיבלת ערך לא נכון ב-imagesize, וגם היית במקום הלא נכון בקובץ להתחיל לקרוא מידע על פיקסלים, ולכן read לא הצליח לקרוא את כמות הבתים שביקשת ממנו.
 
למעלה