Code:
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include"openBMP.h"
void SetDAC(BYTE DAC,BYTE R,BYTE G,BYTE B) {
outportb(0x3C8, DAC);
outportb(0x3C9, R);
outportb(0x3C9, G);
outportb(0x3C9, B);
}
void openBMP(const char *BMPFile) {
FILE *BMP;
int i,j,index;
BYTE Pixel;
BITMAPHEADER Header;
BMP = fopen(BMPFile,"rb");
if(!BMP) {
printf("Error Opening File");
getchar();
return;
}
fread(&Header,sizeof(BITMAPHEADER),1,BMP);
if(Header.biHeight > 200 || Header.biWidth > 320) {
printf("Size not Supported!");
getchar();
fclose(BMP);
return;
}
for(index=0;index<pow(2,Header.biBitCount);++index) {
fread(&Color[index],sizeof(struct RGB),1,BMP);
SetDAC(i,Color[index].Red >> 2,Color[index].Green >> 2,Color[index].Blue >> 2);
}
for(i=0;i<Header.biHeight;++i) {
for(j=0;j<Header.biWidth;++j) {
fread(&Pixel,sizeof(BYTE),1,BMP);
if(Pixel) putpixel(j,Header.biHeight-i,Pixel);
}
}
fclose(BMP);
}
int main() {
int graphDriver = DETECT ,graphMode;
int errorCode;
int i,x,y;
initgraph(&graphDriver,&graphMode,"");
errorCode = graphresult();
if(errorCode!=grOk) {
printf("Error Initializing Driver! - Code = %d",errorCode);
getchar();
return 0;
}
openBMP("dora.bmp");
getchar();
closegraph();
return 0;
}
And the openBMP.h
Code:
typedef unsigned char BYTE;
typedef unsigned int UINT;
typedef unsigned long DWORD;
typedef unsigned int WORD;
typedef signed long LONG;
typedef struct BITMAPFILEHEADER {
UINT bfType;
DWORD bfSize;
UINT bfReserved1;
UINT bfReserved2;
DWORD bfOffBits;
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPHEADER;
struct RGB {
BYTE DAC;
BYTE Blue;
BYTE Green;
BYTE Red;
} Color[256];
Hey guys, I tried to open bitmaps using C, but the color's all messed up.
Can someone point me out what I did wrong?
I'd be glad since this is the first time I use graphics.h