- #1
oreocookie14
- 1
- 0
I am taking a beginners programming course and we have to manipulate a Bitmap image with code in C. Currently I am trying to create a function which reads the Bitmap file, and stores the contents (Width,Height,Palette Info, and Pixel Data) into structs, so that it can be accessed by other functions. I think I've managed to do an okay job at extracting data, but I am having trouble storing it into the structs. When I access the struct by another simple function it just gives me 0 as the height and width, but I can't find what is wrong with my code. I would really appreciate it if anyone could help me figure out how to do this, because I don't think I did the malloc right, which is why I can't access the struct. Do I even need a malloc? By the way, the function names/parameters are already predetermined, so I can't change any of that.
This is what I have so far:
/* Create a structure for a bitmap. */
struct BM
{
short int width;
short int height;
short int storedWidth;
int* palette;
char *data;
};
struct BM *Bitmap;
struct BM MyBM;
/**************************************************************************
* fskip *
* Skips bytes in a file. *
**************************************************************************/
void fskip(FILE *fp, int num_bytes){
int i;
for(i=0; i<num_bytes; i++){
fgetc(fp);
}
}
/*********************************************************
* readBMP
* Take the name of a bitmap file, open the file
* and store its contents into appropriate structs.
* Returns 0 if invalid filename, 1 otherwise.
*********************************************************/
int readBMP(char* filename){
FILE *fileptr;
short int num_colors;
int index, x;
/* Allocate memory for the struct */
Bitmap = (struct BM *) malloc(sizeof(struct BM));
if ((Bitmap = (struct BM *) malloc(sizeof(struct BM))) == NULL){
fclose(fileptr);
printf("Error allocating memory for file %s.\n",filename);
exit(1);
}
/* Open the file */
if ((fileptr = fopen(filename,"rb")) == NULL) {
printf("Error opening file %s.\n",filename);
return 0;
exit(1);
}
/* Check that the file is a bitmap */
if (fgetc(fileptr)!='B' || fgetc(fileptr)!='M') {
fclose(fileptr);
printf("%s is not an invalid file.\n",filename);
return 0;
exit(1);
}else{
return 1;
}
/* read in the width and height of the image, and the
number of colors used; ignore the rest */
fskip(fileptr,16);
fread(&Bitmap->width, sizeof(short int), 1, fileptr);
Bitmap->storedWidth = Bitmap->width + Bitmap->width%4;
fskip(fileptr,2);
fread(&Bitmap->height,sizeof(short int), 1, fileptr);
fskip(fileptr,22);
fread(&num_colors,sizeof(short int), 1, fileptr);
fskip(fileptr,6);
/* assume we are working with an 8-bit file */
if (num_colors==0){
num_colors=256;
}
/* The palette information . */
for (index = 0; index <= num_colors*4; index++){
fread(&Bitmap->palette[index],sizeof(int), 1, fileptr);
}
/* Read the bitmap data*/
for(index=(Bitmap->height-1)*Bitmap->storedWidth;index>=0;index-=Bitmap->storedWidth) {
for(x=0;x<Bitmap->storedWidth;x++) {
Bitmap->data[(short int)index+x]=(char)fgetc(fileptr);
}
}
fclose(fileptr);
}
/*********************************************************
* getHeight
* Get height of the original image.
*********************************************************/
int getHeight(){
printf("Bitmap->height has: %d\n", Bitmap->height);
return Bitmap->height;
}
/*********************************************************
* getWidth
* Get width of the original image.
*********************************************************/
int getWidth(){
return Bitmap->width;
}int main(){
int result = readBMP("test.bmp");
printf("Result: %d\n", result);
int booga = getHeight();
printf("Height: %d\n", booga);
int hey = getWidth();
printf("Width: %d\n", hey);
free(Bitmap);
return 0;
}
This is what I have so far:
/* Create a structure for a bitmap. */
struct BM
{
short int width;
short int height;
short int storedWidth;
int* palette;
char *data;
};
struct BM *Bitmap;
struct BM MyBM;
/**************************************************************************
* fskip *
* Skips bytes in a file. *
**************************************************************************/
void fskip(FILE *fp, int num_bytes){
int i;
for(i=0; i<num_bytes; i++){
fgetc(fp);
}
}
/*********************************************************
* readBMP
* Take the name of a bitmap file, open the file
* and store its contents into appropriate structs.
* Returns 0 if invalid filename, 1 otherwise.
*********************************************************/
int readBMP(char* filename){
FILE *fileptr;
short int num_colors;
int index, x;
/* Allocate memory for the struct */
Bitmap = (struct BM *) malloc(sizeof(struct BM));
if ((Bitmap = (struct BM *) malloc(sizeof(struct BM))) == NULL){
fclose(fileptr);
printf("Error allocating memory for file %s.\n",filename);
exit(1);
}
/* Open the file */
if ((fileptr = fopen(filename,"rb")) == NULL) {
printf("Error opening file %s.\n",filename);
return 0;
exit(1);
}
/* Check that the file is a bitmap */
if (fgetc(fileptr)!='B' || fgetc(fileptr)!='M') {
fclose(fileptr);
printf("%s is not an invalid file.\n",filename);
return 0;
exit(1);
}else{
return 1;
}
/* read in the width and height of the image, and the
number of colors used; ignore the rest */
fskip(fileptr,16);
fread(&Bitmap->width, sizeof(short int), 1, fileptr);
Bitmap->storedWidth = Bitmap->width + Bitmap->width%4;
fskip(fileptr,2);
fread(&Bitmap->height,sizeof(short int), 1, fileptr);
fskip(fileptr,22);
fread(&num_colors,sizeof(short int), 1, fileptr);
fskip(fileptr,6);
/* assume we are working with an 8-bit file */
if (num_colors==0){
num_colors=256;
}
/* The palette information . */
for (index = 0; index <= num_colors*4; index++){
fread(&Bitmap->palette[index],sizeof(int), 1, fileptr);
}
/* Read the bitmap data*/
for(index=(Bitmap->height-1)*Bitmap->storedWidth;index>=0;index-=Bitmap->storedWidth) {
for(x=0;x<Bitmap->storedWidth;x++) {
Bitmap->data[(short int)index+x]=(char)fgetc(fileptr);
}
}
fclose(fileptr);
}
/*********************************************************
* getHeight
* Get height of the original image.
*********************************************************/
int getHeight(){
printf("Bitmap->height has: %d\n", Bitmap->height);
return Bitmap->height;
}
/*********************************************************
* getWidth
* Get width of the original image.
*********************************************************/
int getWidth(){
return Bitmap->width;
}int main(){
int result = readBMP("test.bmp");
printf("Result: %d\n", result);
int booga = getHeight();
printf("Height: %d\n", booga);
int hey = getWidth();
printf("Width: %d\n", hey);
free(Bitmap);
return 0;
}