的DOS看真彩BMP位图的小程序给贴上来了,有兴趣的就自己编译着玩玩,里边还有我写的一个画直线的算法,不过很笨拙。 初学者可以试着看看源码挺简单的,看到有不少人看这个贴子我就给注释了,不过画直线的函数就不用看了,很乱我自己都懒得看所以就没有注释
另外几个函数的功能在后面的跟贴我简单的介绍了一下
A.c main文件
[code:1:9253783525]
#include <stdio.h>
#include <conio.h>
#include "a.h"
/*extern void print();
*extern void init(int);
*extern void vinfo(int)
*extern void demo(int,int,int);
*extern void Pixel16HiC(int,int,int);
*extern void Pixel24TrC(unsigned long int,int,int);
*extern void Pixel256C(char,int,int);
*#define Hi15(R,G,B)
*#define HiColor(R,G,B) R*2048+G*32+B
*#define TrColor(R,G,B) R*65536+G*256+B
*#define Tr_HColor(R,G,B)
*#define H_TColor(RGB)
*#define T_HColor(RGB)
*/
main()
{
unsigned int i,j,x,y;
FILE *fp;
if((fp=fopen("d:\\BC4\\Ship\\24.bmp","rb"))==NULL)/*自己选择路径*/
printf("Can't open 256.bmp\n");
init(Tr800_600);/*初始化显示模式为24位真彩色,800*600分辨率*/
readpic(fp);/*主函数*/
fclose(fp);
/*for(i=0;i<=767;i++)
* line(i,i,1023,0,TrC(i,i*3,i/2));/*这里是画直线函数*/
*/
getch();
return;
}
[/code:1:9253783525]
BMP.C 主文件,里边有画直线函数
[code:1:9253783525]
#include<stdio.h>
#include<alloc.h>
#include<dos.h>
#include"a.h"
unsigned far* readpic(FILE *fp)
{
struct bmp_head far* bhead;
int x,y;
unsigned long size,length,width;
void far * farmem;
unsigned char far *RGB;
char temp[30],over;
fread(temp,30L,1,fp); /*读BMP文件头信息*/
bhead=(char far*)temp;
over=bhead->width%4;/*BMP格式每行多出的位,我也不清楚为什么要多出这几位,当时我这里让我很头痛费了好大劲才查出来*/
width=bhead->width*3+over;/*位图宽*/
if((farmem=farmalloc(width*10+10))==NULL)/*分配缓冲区因为DOS最大可分配内存是一个段64K字节这里我分配了位图宽度*10 +10*/
printf("Not enough Memory\n");
fseek(fp,bhead->offset,0);/*读指针跳到位图数据起点*/
fread(farmem,width,10,fp);/*读位图数据到缓冲区*/
RGB=farmem;/*RGB画像素用的指针,每像素3个字节*/
for(y=bhead->hight;y>=0;y--)/*bhead->hight 位图高度*/
{
for(x=0;x<bhead->width;x++)/*bhead->width 位图宽度*/
{
Pixel24TrC(TrC(*(RGB+2),*(RGB+1),*RGB),x,y);/*画点*/
RGB=RGB+3;/*一个像素点3个字节*/
if(FP_OFF(RGB)==width*10+FP_OFF(farmem)||FP_OFF(RGB)==width*10+FP_OFF(farmem)+3)/*FP_OFF取远指针偏移,这个条件语句判断画完一个缓冲区*/
{
fread(farmem,width,10,fp);
RGB=farmem;
}/*把BMP位图分成缓冲区大小的段*/
}
RGB=RGB+over;/*每行最后多出over个字节*/
}
farfree(farmem);
return farmem;
}
/*画直线函数*/
void line(unsigned long left_x,unsigned long left_y,unsigned long right_x,unsigned long right_y,unsigned long color)
{
unsigned long x=left_x,y=left_y;
if(left_x>right_x)
if(left_y>right_y)
{
while(left_x-right_x>left_x-x)
{
Pixel24TrC(color,x,y);
if((left_x-right_x)>(left_y-right_y))
while((left_y-right_y)*(left_x-x)<(left_x-right_x)*(left_y-y))
{
x--;
Pixel24TrC(color,x,y);
}
x--;
if(y<=right_y)
continue;
if((left_y-right_y)>(left_x-right_x))
while((left_y-right_y)*(left_x-x)>(left_x-right_x)*(left_y-y))
{
y--;
Pixel24TrC(color,x,y);
}
y--;
};
while(y>right_y)
{
Pixel24TrC(color,x,y);
y--;
};
}
else
{
while(x>right_x)
{
Pixel24TrC(color,x,y);
if((left_x-right_x)>(right_y-left_y))
while((right_y-left_y)*(left_x-x)<(left_x-right_x)*(y-left_y))
{
x--;
Pixel24TrC(color,x,y);
}
x--;
if(!(y<right_y))
continue;
if((right_y-left_y)>(left_x-right_x))
while((right_y-left_y)*(left_x-x)>(left_x-right_x)*(y-left_y))
{
y++;
Pixel24TrC(color,x,y);
}
y++;
};
while(y<=right_y)
{
Pixel24TrC(color,x,y);
y++;
};
}
else
if(left_y>right_y)
{
while(x<right_x)
{
Pixel24TrC(color,x,y);
if((right_x-left_x)>(left_y-right_y))
while((left_y-right_y)*(x-left_x)<(right_x-left_x)*(left_y-y))
{
x++;
Pixel24TrC(color,x,y);
}
x++;
if(left_y-y>left_y-right_y)
continue;
if((left_y-right_y)>(right_x-left_x))
while((left_y-right_y)*(x-left_x)>(right_x-left_x)*(left_y-y))
{
y--;
Pixel24TrC(color,x,y);
}
y--;
};
while(left_y-y<=left_y-right_y)
{
Pixel24TrC(color,x,y);
y--;
};
}
else
{
while(x<right_x)
{
Pixel24TrC(color,x,y);
if((right_x-left_x)>(right_y-left_y))
while((right_y-left_y)*(x-left_x)<(right_x-left_x)*(y-left_y))
{
x++;
Pixel24TrC(color,x,y);
}
x++;
if(y>=right_y)
continue;
if((right_y-left_y)>(right_x-left_x))
while((right_y-left_y)*(x-left_x)>(right_x-left_x)*(y-left_y))
{
y++;
Pixel24TrC(color,x,y);
}
y++;
};
while(y<=right_y)
{
Pixel24TrC(color,x,y);
y++;
};
}
return 0;
}
[/code:1:9253783525]
a.h 头文件
[code:1:9253783525]
#define LINE 640
#define C256(R,G,B) (unsigned char)R*0x20%0x100+(unsigned char)G*0x4%0x20+B
#define Hi15(R,G,B) (unsigned)R*0x400%0x8000+(unsigned)G*0x20%0x400+(char)B%0x20
#define HiC(R,G,B) (unsigned)R*0x800%0x10000+(unsigned)G*32%0x800+(char)B%0x20
#define TrC(R,G,B) (unsigned long)R*0x10000%0x1000000+(unsigned long)G*0x100%0x10000+(unsigned)B%0x100
#define Hi-TrC(RGB) (unsigned long)RGB%0x10000/2048*65536+(unsigned long)RGB%2048/32*256+(unsigned char)RGB%32
#define Tr-HiC(RGB) (unsigned long)RGB%0x1000000/65536%32*2048+(unsigned long)RGB%65536/64*32+(unsigned char)(RGB%32)
#define C256_640_400 0x100
#define C256_640_480 0x101
#define C16_800_600 0x102
#define C256_800_600 0x103
#define C16_1024_768 0x104
#define C256_1024_768 0x105
#define Hi15_640_480 0x110
#define Hi640_480 0x111
#define Tr640_480 0x112
#define Hi15_800_600 0x113
#define Hi800_600 0x114
#define Tr800_600 0x115
#define Hi15_1024_768 0x116
#define Hi1024_768 0x117
#define Tr1024_768 0x118
extern void Demo(unsigned long,int,int);
extern void init(int);
extern void Pixel16HiC(int,int,int);
extern void Pixel24TrC(unsigned long,int,int);
extern void Pixel256C(char,int,int);
extern void line(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2,unsigned long color);
/*seepic*/
extern unsigned far* readpic(FILE *fp);
extern void mouse(void);
struct bmp_head
{
int other1;
unsigned long length;
char other2[4];
unsigned int offset;
char other3[6];
unsigned int width;
char other4[2];
unsigned int hight;
char other5[4];
unsigned char color;
};
[/code:1:9253783525]
mybmp.c这是嵌入式汇编的画点函数
[code:1:9253783525]
void init(int mode)
{
asm mov bx,mode
asm mov ax,4f02h
asm int 10h
return;
}
void Pixel24TrC(unsigned long color,int x,int y)
{
asm push es
asm push di
asm mov ax,4f03h
asm int 10h
pi112:
asm cmp bx,112h
asm jnz pi115
asm mov dx,640
asm jmp write_24t
pi115:
asm cmp bx,115h
asm jnz pi118
asm mov dx,800
asm jmp write_24t
pi118:
asm cmp bx,118h
asm jnz Tr_exit
asm mov dx,1024
write_24t:
asm mov ax,0a000h
asm mov es,ax
asm mov ax,y
asm mul dx
asm mov bx,ax
asm mov cx,dx
asm add ax,bx
asm adc dx,cx
asm add ax,bx
asm adc dx,cx
asm add ax,bx
asm adc dx,cx
asm mov bx,x
asm add bx,x
asm add bx,x
asm add bx,x
asm add ax,bx
asm adc dx,0
asm mov di,ax
asm mov ax,4f05h
asm mov bx,0
asm int 10h
asm mov cx,word ptr color
asm mov word ptr es:[di],cx
asm add di,2
asm mov cx,word ptr color+2
asm mov word ptr es:[di],cx
Tr_exit:
asm pop di
asm pop es
return;
}
void Pixel16HiC(int color,int x,int y)
{
asm push es
asm push di
asm mov ax,4f03h
asm int 10h
pi110:
asm cmp bx,110h
asm jnz pi111
asm mov dx,640
asm jmp write_16h
pi111:
asm cmp bx,111h
asm jnz pi113
asm mov dx,640
asm jmp write_16h
pi113:
asm cmp bx,113h
asm jnz pi114
asm mov dx,800
asm jmp write_16h
pi114:
asm cmp bx,114h
asm jnz pi116
asm mov dx,800
asm jmp write_16h
pi116:
asm cmp bx,116h
asm jnz pi117
asm mov dx,1024
asm jmp write_16h
pi117:
asm cmp bx,117h
asm jnz Hi_exit
asm mov dx,1024
write_16h:
asm mo