Read a bitmap in C and print it binairy

Started by
8 comments, last by ApochPiQ 13 years, 5 months ago
We have to make a program that read in a .bmp file.
We need to print the headerfile and the data.

We've already the headerfile, but now, we don't now how to do it for the data.
This is programmed in Visual C. Sorry but the comment is in dutch.
The .bmp is here: https://lms.xios.be/courses/NTECHPBAEATOEINF205/document/Oefeningen/Project/Resources/x_2k.bmp?cidReq=NTECHPBAEATOEINF205

And our output have to be like you can see in this pdf on page 4:https://lms.xios.be/courses/NTECHPBAEATOEINF205/document/Oefeningen/Project/Project_C.pdf?cidReq=NTECHPBAEATOEINF205

I hope you can open this.


Program:

#include <stdio.h>


/* We moeten een apparte struct aanmaken voor de Magic Number, anders krijgen we een foutieve waarde hiervoor.*/

struct MagicNumber{
unsigned char type[2]; //Magicnumber, gebruikt om de BMP file te identificeren. Meestal staat hier “BM”//
}BmpMN;

struct INFOHEADER{

unsigned int sizeByte; //De grootte van dit bestand, uitgedrukt in bytes.//
unsigned short reserved1; //Enkele gereserveerde bytes, deze mogen ingevuld worden door het programma dat de afbeelding aanmaakt//
unsigned short reserved2; //Enkele gereserveerde bytes, deze mogen ingevuld worden door het programma dat de afbeelding aanmaakt//
unsigned int offsetbits; //De offset:het begin adres van de byte waarop de bitmap data kan gevonden worden. Dit is meestal gelijk aan de grootte van de header+ palette//
unsigned int sizeHeader; //Grootte van de header(in dit geval 40 bytes)//
int width; //Breedte van de bitmap in pixels//
int heigth; //Hoogte van de bitmap in pixels//
unsigned short planes; //Het aantal kleurvlakken dat gebruikt werd, deze waarde staat op 1//
unsigned short bitcount; //Aantal bits per pixel (1 4 8 16 24 of 32)//
unsigned int compression; //Compressie methode (een getal tussen 0 en 5, meestal 0)//
unsigned int sizeImage; //Grootte van de bitmap image data in bytes//
int xpelspermeter; //Horizontale resolutie,in pixels per meter//
int ypelspermeter; //Verticale resolutie, in pixels per meter//
unsigned int colorsused; //Het aantal kleuren in de kleurenpalet. Indien hier 0 staat is het 2^n.//
unsigned int colorsimportant; //Het aantal belangrijke kleuren in het palet(meestal 0)//
unsigned int palette1; //Waarde van de kleur zwart in R-G-B//
unsigned int palette2; //Waarde van de kleur wit in R-G-B//


} BitmapInfoHeader;


void leesBestand() {
FILE *filepointer; //Pointer aanmaken van het type 'FILE'//
char mode[]="rb"; //Modus: w voor 'write', r voor 'read'//
char INfile[]="C:\\x_2k.bmp"; //Bestandsnaam//

if ((filepointer = fopen(INfile, mode)) == NULL) //Maak een pointer aan naar het bestand op de harde schijf //
{
printf("Cannot open %s file\n", INfile); //Geef melding als pointer een NULL-return geeft//
return 0; //Anders loopt het programma nog vast als er een foutieve bestandsnaam is ingevoerd//
}
else //Anders begin programma om header in te lezen//
{
printf("\n** READING FILE **\n");

fread(&BmpMN,sizeof(BmpMN),1,filepointer);
printf("Magic Number: %s\n",BmpMN.type);

fread(&BitmapInfoHeader,sizeof(BitmapInfoHeader),1,filepointer);
printf("Size in bytes: %d\n",BitmapInfoHeader.sizeByte);
printf("Reserved bytes: %d %d\n",BitmapInfoHeader.reserved1,BitmapInfoHeader.reserved2);
printf("Starting byte: %d\n",BitmapInfoHeader.offsetbits);
printf("Size of header: %d\n",BitmapInfoHeader.sizeHeader);
printf("Dimensions of bitmap: %d by %d\n",BitmapInfoHeader.width,BitmapInfoHeader.heigth);
printf("Color planes: %d\n",BitmapInfoHeader.planes);
printf("Bits per pixel: %d\n",BitmapInfoHeader.bitcount);
printf("Compression type: %d\n",BitmapInfoHeader.compression);
printf("Image content <in bytes>: %d\n",BitmapInfoHeader.sizeImage);
printf("Horizontal resolution: %d\n",BitmapInfoHeader.xpelspermeter);
printf("Vertical resolution: %d\n",BitmapInfoHeader.ypelspermeter);
printf("Colors in palette: %d\n",BitmapInfoHeader.colorsused);
printf("Important colors used: %d\n",BitmapInfoHeader.colorsimportant);
printf("Palette: %x %x\n",BitmapInfoHeader.palette1,BitmapInfoHeader.palette2);
}

fclose(filepointer); // sluit het bestand //
};



int main(void){


leesBestand();

return 0;
}




Advertisement
I'm sorry, but this is not a homework forum.
Quote:Original post by AndyEsser
I'm sorry, but this is not a homework forum.




I know that, I don't ask for making my homework.
I just ask for a little bit of help.

I don't know how to continu my program...
Have you read the Bitmap specification.

Bitmap is one of the easier specs to use.

What exactly are you stuck on?
Quote:Original post by SnoopMoop
I don't know how to continu my program...


We can help you if you ask a more specific question. What works and what doesn't? where are you stuck exactly?
Now, we can read in the bmp header file.
But the next step is to read in the real data of the picture.
And we have to read them in binary.
But I don't know how to do that.

If this step is done, we need to print this binary data in the window.
So that we can see the "x" of the picture , but than with "1"s for white color
and "0" for the black color of the "x".

So each '1' or '0' is the same as 1 pixel.
What have you tried?
Quote:Original post by SnoopMoop
If this step is done, we need to print this binary data in the window.
So that we can see the "x" of the picture , but than with "1"s for white color
and "0" for the black color of the "x".

How many 1s and 0s should you have on a row? How would you figure that out?
How many rows of 1s and 0s should you have? How would you figure that out?
Given m rows and n columns, how do you find the value of the pixel at coordinate (p, q)?
Quote:How many 1s and 0s should you have on a row? How would you figure that out?
How many rows of 1s and 0s should you have? How would you figure that out?
Given m rows and n columns, how do you find the value of the pixel at coordinate (p, q)?



You can see this with the width and the heigth of the picture that we had read in with the header.
We know that the picture is 50 by 50 pixels.

But we first do need to save them in a file I think. Before you can print all of this pixels. And before we save this. I should read the pixels. And this is the problem. Do I have need to do the same thing like the header?
Make a pointer an read binary in? And this is than the data?
Moving to For Beginners.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement