#include   <stdlib.h>
#include   <graphics.h>
#include   <math.h>
#include   <dos.h>
#include   <stdio.h>
#include   <process.h>
#include   <bios.h>
#include   <conio.h>
#include   <malloc.h>
#include   <mem.h>#define   Width   640
#define   Height   480
unsigned   char   bin[8]   =   {
0x80,0x40,0x20,0x10,
0x08,0x04,0x02,0x01
};
int   vga_default[16][3]={
0   ,0   ,0   ,0   ,0   ,42,
0   ,42,0   ,
0   ,42,42,
42,0   ,0,
42,0   ,42,
42,21,0   ,
42,42,42,
21,21,21,
21,21,63,
0   ,63,0   ,
0   ,63,63,
63,21,17,
63,19,63,
63,63,0   ,
63,63,63};
typedef   struct   BMP_file
        {
unsigned   short   bfType;
unsigned   long     bfSize;
unsigned   int       Reserved1;
unsigned   int       Reserved2;
unsigned   long     bfOffset;
        }bitmapfile;typedef   struct   BMP_info
        {
unsigned   long   biSize;
unsigned   long   biWidth;
unsigned   long   biHeight;
int                       biPlanes;
int                       biBitCount;
unsigned   long   biCompression;
unsigned   long   biSizeImage;
unsigned   long   biXpelspermeter;
unsigned   long   biYpelspermeter;
unsigned   long   biClrUsed;
unsigned   long   biClrImportant;
        }bitmapinfo;typedef   struct   RGB_BMP_typ
{
unsigned   char   blue;
unsigned   char   green;
unsigned   char   red;
unsigned   char   reserved;
}RGB_BMP,*RGB_BMP_ptr;typedef   struct   bmp_picture_typ
{
bitmapfile   file;
bitmapinfo   info;
RGB_BMP   palette[256];
char   far       *buffer;
}bmp_picture,*bmp_picture_ptr;void   Check_Bmp(bmp_picture_ptr   bmp_ptr)
{
        if(bmp_ptr->file.bfType!=0x4d42)
        {
            printf("Not   a   BMP   file!n");
            exit(1);
        }
        if(bmp_ptr->info.biCompression!=0)
        {
            printf("Can   not   display   a   compressed   bmp   file!n");
            exit(1);
        }
        if(bmp_ptr->info.biBitCount!=8)
        {
            printf("Not   a   index   256color   bmp   file!n");
            exit(1);
        }
}void   Set_BMP_Palette_Register(int   index,RGB_BMP_ptr   color)
{
outportb(0x3c6,0xff);
outportb(0x3c8,index);
outportb(0x3c9,color->red);
outportb(0x3c9,color->green);
outportb(0x3c9,color->blue);
}void   save_bmp()
{
    FILE   *fp;
    FILE   *fh;
    FILE   *fd;
    int   *bmp256;
    int   i,j,k,x;
    int   temp   =   0;
    long   width,height;
    int   color;
    unsigned   char   tempbuffer[640];
    bmp_picture   pic;    width=Width;height=Height;    fp=fopen("mset.bmp","a+b"
    if(fp==NULL){
        printf("can   not   open   the   file!");
exit(1);
    };
 
    fd=fopen("msetdata.bmp","w+b"
    if(fd==NULL){
printf("can not open the filedata!");
exit(1);
    }
    fseek(fh,0,SEEK_SET);    pic.file.bfType   =   0x4d42;
    pic.file.bfSize   =   1078+640*480;
    pic.file.Reserved1   =   0;
    pic.file.Reserved2   =   0;
    pic.file.bfOffset     =   1078;    pic.info.biSize     =   40;
    pic.info.biWidth   =   640;
    pic.info.biHeight=   480;
    pic.info.biPlanes=   1;
    pic.info.biBitCount   =   8;
    pic.info.biCompression   =   0;
    pic.info.biSizeImage   =   640*480;
    pic.info.biXpelspermeter   =   3780;
    pic.info.biYpelspermeter   =   3780;
    pic.info.biClrUsed               =   0;
    pic.info.biClrImportant     =   0;    fwrite(&pic.file,sizeof(bitmapfile),1,fp);
    fwrite(&pic.info,sizeof(bitmapinfo),1,fp);    for   (i=0;i<16;i++)
    {
pic.palette[i].red     =   vga_default[i][0]*4;
pic.palette[i].green=   vga_default[i][1]*4;
pic.palette[i].blue   =   vga_default[i][2]*4;
    }
   fwrite(pic.palette,1,1024,fp);    fseek(fd,0L,SEEK_SET);
 pic.buffer   =   malloc(660);    for   (i=479;i>=0;i--)
{getimage(0,i,639,i,tempbuffer);
for   (j=0;j<640;j++)
{
          pic.buffer[j]=
((tempbuffer[     4+j/8]&bin[j%8])/bin[j%8])*0x08|
((tempbuffer[   84+j/8]&bin[j%8])/bin[j%8])*0x04|
((tempbuffer[164+j/8]&bin[j%8])/bin[j%8])*0x02|
((tempbuffer[244+j/8]&bin[j%8])/bin[j%8])*0x01;
}fwrite(pic.buffer,1,640,fd);
}
    rewind(fd);
   for(i=479;i>=0;i--)
 
    {
    fwrite(fd,1,640,fp);
    }
  
    fclose(fd);
    fclose(fp);
}void   BMP_Delete(bmp_picture_ptr   image)
{
  free(image->buffer);
}void   mandelbrot(cxmin,cxmax,cymin,cymax,kmax)
float   cxmin,cxmax,cymin,cymax;
int   kmax;
{
int   k,i,j;
float   R=4,sx=640,sy=480,r,dx,dy,x0,y0,x,y,x1;
dx=(cxmax-cxmin)/sx;
dy=(cymax-cymin)/sy;for(i=0;i<sx;i++)
for(j=0;j<sy;j++)
{
x0=cxmin+i*dx;
y0=cymin+j*dy;
x=0;y=0;k=0;
r=x*x+y*y;while(r<R   &&   k<kmax)
{
x1=x*x-y*y+x0;
y=2*x*y+y0;
x=x1;
r=x*x+y*y;
k++;
}if(k<kmax)
putpixel(i,j,k/10+1);
}
}void   main()
{
    int   driver,mode;
    driver=DETECT;
    mode=0;
    initgraph(&driver,&mode,"");
    mandelbrot(-2.25,0.75,-1.5,1.5,100);
     if   (getch()=='s')
    {    printf("The   picture   is   saving   now!\n");
    save_bmp();
    printf("The   picture   had   saving   ok   !\n");
    }
   
    getch();
    closegraph();
}