Bitmap Loader : Turbo C++

โ€ƒโ€ƒBitmap image files (or just bitmaps) are used by programmers for displaying pre-defined graphics in their programs. These files have the extension .BMP or .DIB (Device Independent Bitmap). Bitmaps are used widely in Windows programs. These are uncompressed, lossless and have the advantage of having a simple structure. The Bitmap Loader program shows how you can easily load bitmaps in your C++ program.



Bitmap Loader (Updated)

Here, you will learn how to load a bitmap image in a C++ program (in Turbo C++). Please note that this program uses BGI graphics. To learn BGI library functions in detail, view BGI library functions.
#include <fstream.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
#include <dos.h>

typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;

#define VIDEO_SEGMENT 0xA000
#define SCREEN_WIDTH 320
#define PALETTE_INDEX 0x03c8
#define PALETTE_DATA 0x03c9

BYTE Palette_Data[256*3];
enum bool{false, true};

/*

I have used Video Mode 13h.
Mode 13h is by far simplest mode to use.
It is a 256 colour mode with a 320×200 resolution
(320 columns and 200 rows of pixels).

The SetGraphicsMode() function sets the video mode to 13h.

*/

void SetGraphicsMode()
{
_AH = 0;
_AL = 0x13;
geninterrupt(0x10);
}

/*

SetTextMode() function sets the video mode to 3h.
Mode 3h is the standard text mode.

*/

void SetTextMode()
{

_AH = 0;
_AL = 0x03;
geninterrupt(0x10);

}

/*
Putpixel() function plots a pixel at (x,y) coordinates.

*/

void Putpixel(int x, int y, BYTE color)
{
pokeb(VIDEO_SEGMENT,(y*SCREEN_WIDTH)+x,color);
}

/*

ImgType() function checks that the bitmap file is valid or not.

*/

bool ImgType(char *bmpfile)
{
WORD type;
fstream file;
file.open(bmpfile,ios::in|ios::binary);
file.read((char *) &type,sizeof(WORD));
file.close();
if(type == 19778)
return true;
else
return false;
}

/*
LoadBitmap() function reads and display bitmap file.
*/

void LoadBitmap(int Left, int Top, int Width, int Height, char *bmpfile)
{
fstream bitmap_file;

/*****************************************************/

DWORD index;
BYTE ch,str;
BYTE rgbquad[4];

int Xpos = Left;
int Ypos = Top;

/*****************************************************/

/* Open bitmap file. */

bitmap_file.open(bmpfile,ios::in|ios::binary);

if(!bitmap_file)
{
cout<<"Error opening file "<>2;
Palette_Data[(int)index*3+1] = rgbquad[1]>>2;
Palette_Data[(int)index*3+0] = rgbquad[2]>>2;
}

outp(PALETTE_INDEX,0); /* tell the VGA that palette data is coming. */

for(int _i=0;_i<256*3;_i++) outp(PALETTE_DATA,Palette_Data[_i]); /* write the data */ /* Draw Bitmap */ for(int i=0;i>1,(Ypos+Height-1-i)>>1,ch);
}
}

bitmap_file.close();

}

/* Display the Author Profile */

void Start()
{
clrscr();
textcolor(12);
gotoxy(15,6);
cprintf(“Program : Bitmap Loader++”);
gotoxy(15,8);
cprintf(“Programmer : Manish Kumar”);
gotoxy(15,10);
cprintf(“Email ID : justdocodings@gmail.com”);
gotoxy(15,12);
cprintf(“Developed in : C++”);
gotoxy(15,14);
cprintf(“Compiler used : TC++ 3.0”);
gotoxy(15,16);
cprintf(“Website : www.justdocodings.blogspot.in”);

gotoxy(20,20);
cprintf(“Press any key to continue.”);
getch();
clrscr();
}

void main()
{
Start();
SetGraphicsMode();

while(!kbhit())
{
LoadBitmap(200,100,240,200,”IMG.bmp”);
delay(1000);
LoadBitmap(200,100,240,200,”IMG_1.bmp”);
delay(1000);
LoadBitmap(200,100,240,200,”IMG_2.bmp”);
delay(1000);

}

getch();
SetTextMode();

}

Bitmap Loader - Blank Screen
Bitmap Loader – Blank Screen
Bitmap Loader - Loading bitmaps
Bitmap Loader – Loading bitmaps
Bitmap Loader - bitmaps loaded
Bitmap Loader – bitmaps loaded
Bitmap Loader - Unloading bitmaps
Bitmap Loader – Unloading bitmaps
Bitmap Loader - Unloading bitmaps
Bitmap Loader – Unloading bitmaps

The above program loads a heart shaped bitmap and creates multiple copies of it. Starting with a single heart, the whole screen soon become full of hearts. Once the screen is full, unloading starts and the screen becomes blank again. The process continues (until the user presses a key) and looks like a very nice animation.



The following video shows the Bitmap Loader in action :


Click here to download the source code of Bitmap Loader (Updated). You can download heart.bmp here.

Click here to download the executable of Bitmap Loader (Updated).

Please let us know if you liked this program (in the comment box).ย 

(Visited 1,058 times, 3 visits today)

9 comments

Leave a Reply

Your email address will not be published. Required fields are marked *