Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Computer GraphicsRSS Feeds

Program to display a 16 color bitmap

Posted By: Easy Tutor     Category: C Programming     Views: 5082

This program shows how to display a 16 color bitmap. In this program palettes are not used for bmp,hence all default 16 colors are used. If you know BMP structure you can try to add palettes.

Code for Program to display a 16 color bitmap in C Programming

#include <alloc.h>
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+#define UL unsigned long#define UI unsigned int#define UC unsigned char//+-+-+-+-+-+-+-+-+-+-+-+-+-+< BMP Structures >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
typedef struct
{
    char Type[2];
    UL Size;
    UI R1;
    UI R2;
    UL OffSet;
}BMP1;
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
typedef struct
{
    UL headsize;
    UL Hlen;
    UL Vlen;
    UI planes;
    UI BPP;
    UL Method;
    UL BmpSize;
    UL HRes;
    UL VRes;
    UL Colors;
    UL IColors;
}BMP2;
//+-+-+-+-+-+-+-+-+-+-+-+-+-< Display BMP >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+int ShowBMP(int x, int y, char* FileName)
{
      int b,a;
      BMP1 Obj1;
      BMP2 Obj2;
      UC * Holder;
      intin=0;
      UC c=0;
      FILE * fp;

      fp = fopen(FileName,"rb");
      if(fp==NULL)
          return 0;
      fread(&Obj1, sizeof(Obj1), 1, fp);
      fread(&Obj2, sizeof(Obj2), 1, fp);
      if(Obj2.BPP!=4)  // This isn't a 16 color bmp we can read;
      {
        fclose(fp);
        return 0;
      };
      fseek(fp,Obj1.OffSet,SEEK_SET);
      Holder=(UC *) calloc(Obj2.Hlen/2+1, sizeof(UC));
      for(b=Obj2.Vlen;b>=0;b--)
      {
        fread(Holder, sizeof(UC), Obj2.Hlen/2, fp);
        c=0;
        in=0;
        for(a=0;a<=Obj2.Hlen;a+=2)
        {
            c = (Holder[in] | 0x00) >>4;
            putpixel(a+x,b+y,c);
            c = (Holder[in] | 0xF0) & 0x0F;
            putpixel(a+1+x,b+y,c);
            in++;
        }
      }
      free (Holder);
      fclose(fp);
      return 1;
}
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-< **** >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+// Two bmp demo.bmp & demo1.bmp are provided.// open these bmp in windows paint & change.(do not change size of bmp)//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-< Main >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+void main()
{
 int color,D=DETECT,E;
// registerfarbgidriver(EGAVGA_driver_far);
 initgraph(&D,&E,"");
 E=0;
 if(!ShowBMP(0,0,"neeraj.bmp")) E=1;
 getch();
 closegraph();
 if(E) printf("\nError : Unable to open file.");
 else printf("Sucess !");
}
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  
Share: 


Didn't find what you were looking for? Find more on Program to display a 16 color bitmap Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to display a 16 color bitmap is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Indranil Banerjee from India Comment on: Aug 09
Many many thanks to you..
God bless u.
email: indranil2121@gmail.com
I need more help from u.

View All Comments