Thread: Wrong Colors while Opening Bitmaps

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Wrong Colors while Opening Bitmaps

    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 = &#37;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
    Last edited by ThLstN; 04-29-2008 at 04:42 PM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It could be an endian thing; as in you need to reverse the byte ordering on the pixels if you havent already done so. I think that happened to me in the past.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my file reverse program
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 05-21-2008, 11:15 AM
  2. opening seperate window to display bitmaps
    By Isometric in forum Windows Programming
    Replies: 24
    Last Post: 11-12-2003, 03:48 AM
  3. using standard bitmaps in the toolbar editor
    By Kibble in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2002, 08:43 PM
  4. different colors
    By Paveltc in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-19-2002, 10:21 AM
  5. Bitmaps (without api support)
    By master5001 in forum Windows Programming
    Replies: 11
    Last Post: 07-14-2002, 11:31 AM