چگونه یک فایل Bitmap در سی پلاس پلاس لود کنیم
سلام . برنامه زیر قادر است یک فایل .bmp را در برنامه ی ++TC اجرا کند .
برای این کار شما باید یک فایل با نام Bitmap.h ایجاد کنید . سپس کد زیر را در آن قرار دهید.
//BitMap Structure
struct A{
char type[2]; /* Magic identifier */
unsigned long size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned long offset; /* Offset to image data, bytes */
};
extern A HEADER,HEADER1;
struct B{
unsigned long size; /* Header size in bytes */
unsigned long width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned long compression; /* Compression type */
unsigned long imagesize; /* Image size in bytes */
unsigned long xresolution,yresolution; /* Pixels per meter */
unsigned long ncolours; /* Number of colours */
unsigned long importantcolours; /* Important colours */
};
extern B INFOHEADER,INFOHEADER1;
برای این کار شما باید یک فایل با نام Bitmap.h ایجاد کنید . سپس کد زیر را در آن قرار دهید.
//BitMap Structure
struct A{
char type[2]; /* Magic identifier */
unsigned long size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned long offset; /* Offset to image data, bytes */
};
extern A HEADER,HEADER1;
struct B{
unsigned long size; /* Header size in bytes */
unsigned long width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned long compression; /* Compression type */
unsigned long imagesize; /* Image size in bytes */
unsigned long xresolution,yresolution; /* Pixels per meter */
unsigned long ncolours; /* Number of colours */
unsigned long importantcolours; /* Important colours */
};
extern B INFOHEADER,INFOHEADER1;
پس از ایجاد فایل بالا شما باید برنامه ی زیر را با کمی تغییر پیاده سازی کنید .
تغییرات در بخش آدرس فایل عکسی است که قرار است آنرا باز کنید . البته فراموش نشود که فایل Bitmap.h را هم باید در آدرس C:\tc\include کپی کنید وسپس برنامه زیر را اجرا کنید .
تغییرات در بخش آدرس فایل عکسی است که قرار است آنرا باز کنید . البته فراموش نشود که فایل Bitmap.h را هم باید در آدرس C:\tc\include کپی کنید وسپس برنامه زیر را اجرا کنید .
//MAIN FILE
#include"iostream.h"
#include"graphics.h"
#include"fstream.h"
#include "bitmap.h"
// This Global Function is used for the resolution of the bitmap. You can set the return value either 1,2 or 3. For me 3 is the best combination.
huge DetectSvga()
{
return 3;
}
void main()
{
int gd = DETECT, md, a;
installuserdriver("SVGA256",&DetectSvga);
initgraph(&gd,&md,"c:\\tc\\bgi"); //Path may be different in your computer.
Show();
Show1();
}
//Suppose you have one show function which read the bitmap from the disk. Then this show function looks like this.
void Show()
{
fstream File;
//Here you have to define the path of the bitmap file. Like according to this example i have to open one Board1.bmp file. So write you bitmap file path here.
File.open("d:\\Chess\\Bitmaps\\Board1.bmp",ios::in);
unsigned char Ch;
File.read((char*)&HEADER,14); //This is the header part of the Bitmap. It always looks like same. Don't change the content hear. The value remains 14 here.
File.read((char*)&INFOHEADER,40); //This is another part of the bitmap, here also the value remains same like 40 here.
unsigned int i;
char ColorBytes[4];
char*PaletteData;
PaletteData=new char[256*3];
if(PaletteData)//if memory allocated successfully
{
//read color data
for(i=0;i<256;i++)
{
//Don't change the code here because i have done some shifting here. Its working fine.
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
outp(0x03c8,0); //tell DAC that data is coming
for(i=0;i<256*3;i++) //send data to SVGA DAC
{
outp(0x03c9,PaletteData[i]);
}
delete[]PaletteData;
}
for(i=0;i
{
for(int j=0;j
{
File.read(&Ch,1); // Here Ch reads the color of your bitmap.
putpixel(XCor+j++,YCor+INFOHEADER.height-i-1,Ch); //XCor and YCor are the X and Y cordinates. It depends upon you.
}
}
File.close();
}
//Another way to display the Bitmap is. Suppose i have another Show1() Function. This is simple as compare to previous show function.
void Show1()
{
Char Ch;
fstream File;
File.open("d:\\Chess\\Bitmaps\\KingA.bmp",ios::in);
File.seekg(54,ios::beg); //Its remains same. Means value always remains 54
File.seekg(256*4,ios::cur); //Its remains same means value always remain 256*4.
for(int i=0;i<40;i++) //Here 40 shows the height of the bitmap. It may be differ it depends upon the size of your bitmap.
{
for(int j=0;j<36;j++) //Here 36 shows the width of the bitmap. It may be differ it depends upon the size of your bitmap.
{
File.read(&Ch,1); //Here Ch is the character which reads the color of your bitmap.
putpixel(this->XCor+j,this->YCor+39-i ,Ch);
}
}
}
#include"iostream.h"
#include"graphics.h"
#include"fstream.h"
#include "bitmap.h"
// This Global Function is used for the resolution of the bitmap. You can set the return value either 1,2 or 3. For me 3 is the best combination.
huge DetectSvga()
{
return 3;
}
void main()
{
int gd = DETECT, md, a;
installuserdriver("SVGA256",&DetectSvga);
initgraph(&gd,&md,"c:\\tc\\bgi"); //Path may be different in your computer.
Show();
Show1();
}
//Suppose you have one show function which read the bitmap from the disk. Then this show function looks like this.
void Show()
{
fstream File;
//Here you have to define the path of the bitmap file. Like according to this example i have to open one Board1.bmp file. So write you bitmap file path here.
File.open("d:\\Chess\\Bitmaps\\Board1.bmp",ios::in);
unsigned char Ch;
File.read((char*)&HEADER,14); //This is the header part of the Bitmap. It always looks like same. Don't change the content hear. The value remains 14 here.
File.read((char*)&INFOHEADER,40); //This is another part of the bitmap, here also the value remains same like 40 here.
unsigned int i;
char ColorBytes[4];
char*PaletteData;
PaletteData=new char[256*3];
if(PaletteData)//if memory allocated successfully
{
//read color data
for(i=0;i<256;i++)
{
//Don't change the code here because i have done some shifting here. Its working fine.
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
outp(0x03c8,0); //tell DAC that data is coming
for(i=0;i<256*3;i++) //send data to SVGA DAC
{
outp(0x03c9,PaletteData[i]);
}
delete[]PaletteData;
}
for(i=0;i
{
for(int j=0;j
{
File.read(&Ch,1); // Here Ch reads the color of your bitmap.
putpixel(XCor+j++,YCor+INFOHEADER.height-i-1,Ch); //XCor and YCor are the X and Y cordinates. It depends upon you.
}
}
File.close();
}
//Another way to display the Bitmap is. Suppose i have another Show1() Function. This is simple as compare to previous show function.
void Show1()
{
Char Ch;
fstream File;
File.open("d:\\Chess\\Bitmaps\\KingA.bmp",ios::in);
File.seekg(54,ios::beg); //Its remains same. Means value always remains 54
File.seekg(256*4,ios::cur); //Its remains same means value always remain 256*4.
for(int i=0;i<40;i++) //Here 40 shows the height of the bitmap. It may be differ it depends upon the size of your bitmap.
{
for(int j=0;j<36;j++) //Here 36 shows the width of the bitmap. It may be differ it depends upon the size of your bitmap.
{
File.read(&Ch,1); //Here Ch is the character which reads the color of your bitmap.
putpixel(this->XCor+j,this->YCor+39-i ,Ch);
}
}
}
+ نوشته شده در ۱۳۸۶/۱۰/۱۵ ساعت 2:40 توسط آرش وهابی
|
a day without learning is a lost day